diff --git a/Sources/Fuzzilli/Compiler/Compiler.swift b/Sources/Fuzzilli/Compiler/Compiler.swift index d9a7af04..5acd6749 100644 --- a/Sources/Fuzzilli/Compiler/Compiler.swift +++ b/Sources/Fuzzilli/Compiler/Compiler.swift @@ -338,9 +338,10 @@ public class JavaScriptCompiler { case .whileLoop(let whileLoop): emit(BeginWhileLoopHeader()) - let cond = try compileExpression(whileLoop.test) - - emit(BeginWhileLoopBody(), withInputs: [cond]) + try enterNewScope { + let cond = try compileExpression(whileLoop.test) + emit(BeginWhileLoopBody(), withInputs: [cond]) + } try enterNewScope { try compileBody(whileLoop.body) @@ -357,9 +358,10 @@ public class JavaScriptCompiler { emit(BeginDoWhileLoopHeader()) - let cond = try compileExpression(doWhileLoop.test) - - emit(EndDoWhileLoop(), withInputs: [cond]) + try enterNewScope { + let cond = try compileExpression(doWhileLoop.test) + emit(EndDoWhileLoop(), withInputs: [cond]) + } case .forLoop(let forLoop): var loopVariables = [String]() diff --git a/Tests/FuzzilliTests/CompilerTests/advanced_loops.js b/Tests/FuzzilliTests/CompilerTests/advanced_loops.js index 998ee415..7f40a8bf 100644 --- a/Tests/FuzzilliTests/CompilerTests/advanced_loops.js +++ b/Tests/FuzzilliTests/CompilerTests/advanced_loops.js @@ -89,12 +89,3 @@ for (output("inside for loop initializer"); output("inside for loop condition"), if (!countdown()) break; } resetCounter(); - -// Test scoping in the different parts of a for loop. -{ - global = { start: 0, end: 3, step: 1, value: 42 }; -} -for (let i = global.start; i < global.end; i += global.step) { - output("inside for loop body with global value", global.value); -} - diff --git a/Tests/FuzzilliTests/CompilerTests/named_variables_scoping.js b/Tests/FuzzilliTests/CompilerTests/named_variables_scoping.js new file mode 100644 index 00000000..4e56d435 --- /dev/null +++ b/Tests/FuzzilliTests/CompilerTests/named_variables_scoping.js @@ -0,0 +1,38 @@ +if (typeof output === 'undefined') output = console.log; + +// Test to ensure that multiple uses of the same named variable compile correctly. + +{ + // This named variable will go out of scope, so subsequent uses require creating a new one (with the same name). + global = { start: 0, end: 3, step: 1, value: 42 }; +} + +{ + if (global.value) { + output("inside if with global value", global.value); + } else { + output("inside else with global value", global.value); + } +} + +{ + for (let i = global.start; i < global.end; i += global.step) { + output("inside for loop body with global value", global.value); + } +} + +{ + let i = 0; + while (i < global.end) { + i += global.step; + output("inside while loop body with global value", global.value); + } +} + +{ + let i = 0; + do { + i += global.step; + output("inside do-while loop body with global value", global.value); + } while (i < global.end); +}