From be5323459058ed14f33df75096ea51cecde8d422 Mon Sep 17 00:00:00 2001 From: Alexey Shvayka Date: Thu, 28 Sep 2023 17:16:33 +0100 Subject: [PATCH] Test that `HasVarDeclaration` accounts for bindings created via `eval()` (#3914) Co-authored-by: Alexey Shvayka --- ...cript-decl-lex-collision-in-sloppy-mode.js | 21 +++++++++++++ ...pt-decl-lex-no-collision-in-strict-mode.js | 21 +++++++++++++ .../global-code/script-decl-lex-collision.js | 23 ++++++++++++++ ...t-decl-lex-var-declared-via-eval-sloppy.js | 30 +++++++++++++++++++ ...t-decl-lex-var-declared-via-eval-strict.js | 24 +++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 test/annexB/language/eval-code/direct/script-decl-lex-collision-in-sloppy-mode.js create mode 100644 test/annexB/language/eval-code/direct/script-decl-lex-no-collision-in-strict-mode.js create mode 100644 test/annexB/language/global-code/script-decl-lex-collision.js create mode 100644 test/language/global-code/script-decl-lex-var-declared-via-eval-sloppy.js create mode 100644 test/language/global-code/script-decl-lex-var-declared-via-eval-strict.js diff --git a/test/annexB/language/eval-code/direct/script-decl-lex-collision-in-sloppy-mode.js b/test/annexB/language/eval-code/direct/script-decl-lex-collision-in-sloppy-mode.js new file mode 100644 index 0000000000..bb4489d34a --- /dev/null +++ b/test/annexB/language/eval-code/direct/script-decl-lex-collision-in-sloppy-mode.js @@ -0,0 +1,21 @@ +// Copyright (C) 2023 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-globaldeclarationinstantiation +description: Let binding collision with existing var declaration that was created for hoisted function. +info: | + [...] + 3. For each element name of lexNames, do + a. If env.HasVarDeclaration(name) is true, throw a SyntaxError exception. +flags: [noStrict] +---*/ + +eval('if (true) { function test262Fn() {} }'); + +assert.throws(SyntaxError, function() { + $262.evalScript('var x; let test262Fn;'); +}); + +assert.throws(ReferenceError, function() { + x; +}, 'no bindings created'); diff --git a/test/annexB/language/eval-code/direct/script-decl-lex-no-collision-in-strict-mode.js b/test/annexB/language/eval-code/direct/script-decl-lex-no-collision-in-strict-mode.js new file mode 100644 index 0000000000..a0b854c8ed --- /dev/null +++ b/test/annexB/language/eval-code/direct/script-decl-lex-no-collision-in-strict-mode.js @@ -0,0 +1,21 @@ +// Copyright (C) 2023 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-globaldeclarationinstantiation +description: No let binding collision with existing var declaration due to strict-mode eval(). +info: | + PerformEval ( x, strictCaller, direct ) + + [...] + 16. If direct is true, then + a. Let lexEnv be NewDeclarativeEnvironment(runningContext's LexicalEnvironment). + [...] + 18. If strictEval is true, set varEnv to lexEnv. +flags: [onlyStrict] +---*/ + +eval('if (true) { function test262Fn() {} }'); + +$262.evalScript('let test262Fn = 1;'); + +assert.sameValue(test262Fn, 1); diff --git a/test/annexB/language/global-code/script-decl-lex-collision.js b/test/annexB/language/global-code/script-decl-lex-collision.js new file mode 100644 index 0000000000..4a2c34ccda --- /dev/null +++ b/test/annexB/language/global-code/script-decl-lex-collision.js @@ -0,0 +1,23 @@ +// Copyright (C) 2023 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-globaldeclarationinstantiation +description: Let binding collision with existing var declaration that was created for hoisted function. +info: | + [...] + 3. For each element name of lexNames, do + a. If env.HasVarDeclaration(name) is true, throw a SyntaxError exception. +flags: [noStrict] +---*/ + +if (true) { + function test262Fn() {} +} + +assert.throws(SyntaxError, function() { + $262.evalScript('var x; let test262Fn;'); +}); + +assert.throws(ReferenceError, function() { + x; +}, 'no bindings created'); diff --git a/test/language/global-code/script-decl-lex-var-declared-via-eval-sloppy.js b/test/language/global-code/script-decl-lex-var-declared-via-eval-sloppy.js new file mode 100644 index 0000000000..cfece095b3 --- /dev/null +++ b/test/language/global-code/script-decl-lex-var-declared-via-eval-sloppy.js @@ -0,0 +1,30 @@ +// Copyright (C) 2023 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-globaldeclarationinstantiation +description: Let binding collision with existing var declaration that was created via eval(). +info: | + [...] + 3. For each element name of lexNames, do + a. If env.HasVarDeclaration(name) is true, throw a SyntaxError exception. +flags: [noStrict] +---*/ + +eval('var test262Var;'); +eval('function test262Fn() {}'); + +assert.throws(SyntaxError, function() { + $262.evalScript('var x; let test262Var;'); +}, 'variable'); + +assert.throws(ReferenceError, function() { + x; +}, 'no bindings created (script shadowing variable)'); + +assert.throws(SyntaxError, function() { + $262.evalScript('var x; let test262Fn;'); +}, 'function'); + +assert.throws(ReferenceError, function() { + x; +}, 'no bindings created (script shadowing function)'); diff --git a/test/language/global-code/script-decl-lex-var-declared-via-eval-strict.js b/test/language/global-code/script-decl-lex-var-declared-via-eval-strict.js new file mode 100644 index 0000000000..b0f0ac8a27 --- /dev/null +++ b/test/language/global-code/script-decl-lex-var-declared-via-eval-strict.js @@ -0,0 +1,24 @@ +// Copyright (C) 2023 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-globaldeclarationinstantiation +description: No let binding collision with existing var declaration due to strict-mode eval(). +info: | + PerformEval ( x, strictCaller, direct ) + + [...] + 16. If direct is true, then + a. Let lexEnv be NewDeclarativeEnvironment(runningContext's LexicalEnvironment). + [...] + 18. If strictEval is true, set varEnv to lexEnv. +flags: [onlyStrict] +---*/ + +eval('var test262Var;'); +eval('function test262Fn() {}'); + +$262.evalScript('let test262Var = 1;'); +assert.sameValue(test262Var, 1); + +$262.evalScript('const test262Fn = 2;'); +assert.sameValue(test262Fn, 2);