Skip to content

Commit c9dbab8

Browse files
woessgilles-duboscq
authored andcommitted
[GR-41406] Fix eval in with statement.
PullRequest: js/2605
2 parents 4792d4f + 506ba18 commit c9dbab8

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

graal-js/src/com.oracle.truffle.js.parser/src/com/oracle/truffle/js/parser/GraalJSTranslator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3656,8 +3656,9 @@ public JavaScriptNode enterWithNode(com.oracle.js.parser.ir.WithNode withNode) {
36563656
if (context.isOptionDisableWith()) {
36573657
throw Errors.createSyntaxError("with statement is disabled.");
36583658
}
3659-
// Store with object in synthetic block environment that can be captured by closures.
3660-
Environment withParentEnv = lc.getCurrentFunction().hasClosures() ? new BlockEnvironment(environment, factory, context) : environment;
3659+
// Store with object in synthetic block environment that can be captured by closures/eval.
3660+
FunctionNode function = lc.getCurrentFunction();
3661+
Environment withParentEnv = (function.hasClosures() || function.hasEval()) ? new BlockEnvironment(environment, factory, context) : environment;
36613662
try (EnvironmentCloseable withParent = new EnvironmentCloseable(withParentEnv)) {
36623663
JavaScriptNode withExpression = transform(withNode.getExpression());
36633664
JavaScriptNode toObject = factory.createToObjectFromWith(context, withExpression, true);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
6+
*/
7+
8+
/**
9+
* Verify correct handling of with statement with nested direct eval.
10+
*/
11+
12+
load("assert.js");
13+
14+
var res;
15+
var expected = 42;
16+
var chk = function chk() { assertSame(expected, res); res = undefined; };
17+
18+
var b = [42];
19+
with({}) { for(let w of eval("var a = b; a")) res = w; } chk();
20+
with({}) { for(let w of eval("b;")) res = w; } chk();
21+
b = 42;
22+
with({}) { let w = eval("var a = b"); res = a; } chk();
23+
with([1]) { let w = eval("var a = b"); res = a; } chk();
24+
var t={}; with(t) { let w = eval("var b = a"); res = b; } chk();
25+
with("abc") { let w = eval("var b = a"); res = b; } chk();
26+
with("") { let w = eval("b"); res = w; } chk();
27+
28+
expected = 43;
29+
30+
var wee = {b: [43]};
31+
with(wee) { with({}) { for(let w of eval("var a = b; a")) { res = w; } } } chk();
32+
with(wee) { with({}) { for(let w of eval("b;")) { res = w; } } } chk();
33+
wee.b = 43;
34+
with(wee) { with({}) { let w = eval("var a = b"); res = a; } } chk();
35+
expected = 42;
36+
with([1]) { with({}) { let w = eval("var a = b"); res = a; } } chk();
37+
var t={}; with(t) { with({}) { let w = eval("var b = a"); res = b; } } chk();
38+
with("abc") { with({}) { let w = eval("var b = a"); res = b; } } chk();
39+
with("") { with({}) { let w = eval("b"); res = w; } } chk();
40+
41+
expected = 43;
42+
43+
var wee = {b: [43]};
44+
with(wee) { with({}) { for(let w of eval("'use strict'; var a = b; a")) { res = w; } } } chk();
45+
with(wee) { with({}) { for(let w of eval("'use strict'; b;")) { res = w; } } } chk();
46+
wee.b = 43;
47+
with(wee) { with({}) { let w = eval("'use strict'; b"); res = w; } } chk();

0 commit comments

Comments
 (0)