ladybird/Libraries/LibJS/Tests/for-scopes.js
Yonatan Goldschmidt b184f12aaf LibJS: Limit scope of 'for' loop variables
This required 2 changes:
1. In the parser, create a new variable scope, so the variable is
   declared in it instead of the scope in which the 'for' is found.
2. On execute, push the variable into the newly created block. Existing
   code created an empty block (no variables, no arguments) which
   allows Interpreter::enter_scope() to skip the creation of a new
   environment, therefore when the variable initializer is executed, it
   sets the variable to the outer scope. By attaching the variable to
   the new block, the block gets a new environment.

This is only needed for 'let' / 'const' declarations, since 'var'
declarations are expected to leak.

Fixes: #2103
2020-05-07 23:31:49 +02:00

22 lines
387 B
JavaScript

load("test-common.js");
try {
for (var v = 5; false; );
assert(v == 5);
const options = {error: ReferenceError};
assertThrowsError(() => {
for (let l = 5; false; );
l;
}, options);
assertThrowsError(() => {
for (const c = 5; false; );
c;
}, options)
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}