Skip to content

Commit

Permalink
[GR-41406] Fix eval in with statement.
Browse files Browse the repository at this point in the history
PullRequest: js/2605
  • Loading branch information
woess authored and gilles-duboscq committed Oct 9, 2022
2 parents 4792d4f + 506ba18 commit c9dbab8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3656,8 +3656,9 @@ public JavaScriptNode enterWithNode(com.oracle.js.parser.ir.WithNode withNode) {
if (context.isOptionDisableWith()) {
throw Errors.createSyntaxError("with statement is disabled.");
}
// Store with object in synthetic block environment that can be captured by closures.
Environment withParentEnv = lc.getCurrentFunction().hasClosures() ? new BlockEnvironment(environment, factory, context) : environment;
// Store with object in synthetic block environment that can be captured by closures/eval.
FunctionNode function = lc.getCurrentFunction();
Environment withParentEnv = (function.hasClosures() || function.hasEval()) ? new BlockEnvironment(environment, factory, context) : environment;
try (EnvironmentCloseable withParent = new EnvironmentCloseable(withParentEnv)) {
JavaScriptNode withExpression = transform(withNode.getExpression());
JavaScriptNode toObject = factory.createToObjectFromWith(context, withExpression, true);
Expand Down
47 changes: 47 additions & 0 deletions graal-js/src/com.oracle.truffle.js.test/js/GR-41406.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/

/**
* Verify correct handling of with statement with nested direct eval.
*/

load("assert.js");

var res;
var expected = 42;
var chk = function chk() { assertSame(expected, res); res = undefined; };

var b = [42];
with({}) { for(let w of eval("var a = b; a")) res = w; } chk();
with({}) { for(let w of eval("b;")) res = w; } chk();
b = 42;
with({}) { let w = eval("var a = b"); res = a; } chk();
with([1]) { let w = eval("var a = b"); res = a; } chk();
var t={}; with(t) { let w = eval("var b = a"); res = b; } chk();
with("abc") { let w = eval("var b = a"); res = b; } chk();
with("") { let w = eval("b"); res = w; } chk();

expected = 43;

var wee = {b: [43]};
with(wee) { with({}) { for(let w of eval("var a = b; a")) { res = w; } } } chk();
with(wee) { with({}) { for(let w of eval("b;")) { res = w; } } } chk();
wee.b = 43;
with(wee) { with({}) { let w = eval("var a = b"); res = a; } } chk();
expected = 42;
with([1]) { with({}) { let w = eval("var a = b"); res = a; } } chk();
var t={}; with(t) { with({}) { let w = eval("var b = a"); res = b; } } chk();
with("abc") { with({}) { let w = eval("var b = a"); res = b; } } chk();
with("") { with({}) { let w = eval("b"); res = w; } } chk();

expected = 43;

var wee = {b: [43]};
with(wee) { with({}) { for(let w of eval("'use strict'; var a = b; a")) { res = w; } } } chk();
with(wee) { with({}) { for(let w of eval("'use strict'; b;")) { res = w; } } } chk();
wee.b = 43;
with(wee) { with({}) { let w = eval("'use strict'; b"); res = w; } } chk();

0 comments on commit c9dbab8

Please sign in to comment.