Skip to content

Commit c74c02e

Browse files
committed
[GR-43146] Fix dynamic import in an indirect eval/dynamic function (23.0).
PullRequest: js/2811
2 parents a143651 + da0a749 commit c74c02e

29 files changed

+632
-557
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -43,6 +43,7 @@
4343
import com.oracle.js.parser.ir.ClassNode;
4444
import com.oracle.js.parser.ir.Scope;
4545
import com.oracle.truffle.js.parser.env.Environment;
46+
import com.oracle.truffle.js.runtime.objects.ScriptOrModule;
4647

4748
/**
4849
* Saved parser environment and lexical context information for direct eval.
@@ -51,10 +52,12 @@ public final class DirectEvalContext {
5152
final Scope scope;
5253
final Environment env;
5354
final ClassNode enclosingClass;
55+
final ScriptOrModule activeScriptOrModule;
5456

55-
DirectEvalContext(Scope scope, Environment env, ClassNode enclosingClass) {
57+
DirectEvalContext(Scope scope, Environment env, ClassNode enclosingClass, ScriptOrModule activeScriptOrModule) {
5658
this.scope = scope;
5759
this.env = env;
5860
this.enclosingClass = enclosingClass;
61+
this.activeScriptOrModule = activeScriptOrModule;
5962
}
6063
}

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -142,16 +142,16 @@ public final class GraalJSEvaluator implements JSParser {
142142
* Evaluate indirect eval.
143143
*/
144144
@Override
145-
public ScriptNode parseEval(JSContext context, Node lastNode, Source source) {
146-
return parseEval(context, lastNode, source, false, null);
145+
public ScriptNode parseEval(JSContext context, Node lastNode, Source source, ScriptOrModule activeScriptOrModule) {
146+
return parseEval(context, lastNode, source, false, null, activeScriptOrModule);
147147
}
148148

149149
/**
150150
* Evaluate Function(parameterList, body).
151151
*/
152152
@TruffleBoundary(transferToInterpreterOnException = false)
153153
@Override
154-
public ScriptNode parseFunction(JSContext context, String parameterList, String body, boolean generatorFunction, boolean asyncFunction, String sourceName) {
154+
public ScriptNode parseFunction(JSContext context, String parameterList, String body, boolean generatorFunction, boolean asyncFunction, String sourceName, ScriptOrModule activeScriptOrModule) {
155155
String wrappedBody = "\n" + body + "\n";
156156
try {
157157
GraalJSParserHelper.checkFunctionSyntax(context, context.getParserOptions(), parameterList, wrappedBody, generatorFunction, asyncFunction, sourceName);
@@ -181,8 +181,8 @@ public ScriptNode parseFunction(JSContext context, String parameterList, String
181181
code.append(") {");
182182
code.append(wrappedBody);
183183
code.append("})");
184-
Source source = Source.newBuilder(JavaScriptLanguage.ID, code.toString(), sourceName).build();
185-
return parseEval(context, null, source, false, null);
184+
Source source = Source.newBuilder(JavaScriptLanguage.ID, code.toString(), sourceName).cached(false).build();
185+
return parseEval(context, null, source, false, null, activeScriptOrModule);
186186
}
187187

188188
/**
@@ -192,15 +192,15 @@ public ScriptNode parseFunction(JSContext context, String parameterList, String
192192
@Override
193193
public ScriptNode parseDirectEval(JSContext context, Node lastNode, Source source, Object evalEnv) {
194194
DirectEvalContext directEval = (DirectEvalContext) evalEnv;
195-
return parseEval(context, lastNode, source, directEval.env.isStrictMode(), directEval);
195+
return parseEval(context, lastNode, source, directEval.env.isStrictMode(), directEval, directEval.activeScriptOrModule);
196196
}
197197

198198
@TruffleBoundary(transferToInterpreterOnException = false)
199-
private static ScriptNode parseEval(JSContext context, Node lastNode, Source source, boolean isStrict, DirectEvalContext directEval) {
199+
private static ScriptNode parseEval(JSContext context, Node lastNode, Source source, boolean isStrict, DirectEvalContext directEval, ScriptOrModule activeScriptOrModule) {
200200
context.checkEvalAllowed();
201201
NodeFactory nodeFactory = NodeFactory.getInstance(context);
202202
try {
203-
return JavaScriptTranslator.translateEvalScript(nodeFactory, context, source, isStrict, directEval);
203+
return JavaScriptTranslator.translateEvalScript(nodeFactory, context, source, isStrict, directEval, activeScriptOrModule);
204204
} catch (com.oracle.js.parser.ParserException e) {
205205
throw parserToJSError(lastNode, e, context);
206206
}
@@ -993,7 +993,8 @@ public JavaScriptNode parseInlineScript(JSContext context, Source source, Materi
993993
scope = null;
994994
env = null;
995995
}
996-
ScriptNode script = JavaScriptTranslator.translateInlineScript(NodeFactory.getInstance(context), context, env, source, isStrict);
996+
ScriptNode script = JavaScriptTranslator.translateInlineScript(NodeFactory.getInstance(context), context, env, source, isStrict,
997+
EvalNode.findActiveScriptOrModule(locationNode));
997998
return createInlineScriptCallNode(context, script.getFunctionData(), script.getCallTarget(), locationNode);
998999
}
9991000

@@ -1028,7 +1029,7 @@ public void checkFunctionSyntax(JSContext context, JSParserOptions parserOptions
10281029
GraalJSParserHelper.checkFunctionSyntax(context, parserOptions, parameterList, body, generator, async, sourceName);
10291030
} catch (com.oracle.js.parser.ParserException ex) {
10301031
// throw the correct JS error
1031-
parseFunction(context, parameterList, body, false, false, sourceName);
1032+
parseFunction(context, parameterList, body, false, false, sourceName, null);
10321033
}
10331034
}
10341035
}

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

+17-28
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import java.util.Iterator;
5050
import java.util.List;
5151
import java.util.Objects;
52-
import java.util.function.Consumer;
5352
import java.util.function.Supplier;
5453
import java.util.function.UnaryOperator;
5554

@@ -223,11 +222,11 @@ public Object execute(VirtualFrame frame) {
223222
protected final List<String> argumentNames;
224223
protected final int sourceLength;
225224
protected final int prologLength;
225+
protected final ScriptOrModule activeScriptOrModule;
226226
private final boolean isParentStrict;
227-
private Consumer<ScriptOrModule> scriptOrModuleResolver;
228227

229228
protected GraalJSTranslator(LexicalContext lc, NodeFactory factory, JSContext context, Source source, List<String> argumentNames, int prologLength, Environment environment,
230-
boolean isParentStrict) {
229+
boolean isParentStrict, ScriptOrModule scriptOrModule) {
231230
super(lc);
232231
this.context = context;
233232
this.environment = environment;
@@ -237,6 +236,7 @@ protected GraalJSTranslator(LexicalContext lc, NodeFactory factory, JSContext co
237236
this.isParentStrict = isParentStrict;
238237
this.sourceLength = source.getCharacters().length();
239238
this.prologLength = prologLength;
239+
this.activeScriptOrModule = scriptOrModule;
240240
}
241241

242242
protected final JavaScriptNode transform(com.oracle.js.parser.ir.Node node) {
@@ -318,12 +318,6 @@ protected final JavaScriptNode transformFunction(FunctionNode functionNode) {
318318

319319
protected abstract GraalJSTranslator newTranslator(Environment env, LexicalContext savedLC);
320320

321-
protected final void resolveScriptOrModule(ScriptOrModule scriptOrModule) {
322-
if (scriptOrModuleResolver != null) {
323-
scriptOrModuleResolver.accept(scriptOrModule);
324-
}
325-
}
326-
327321
// ---
328322

329323
@Override
@@ -464,6 +458,7 @@ JavaScriptNode translateFunctionBody(FunctionNode functionNode, List<JavaScriptN
464458
if (functionNode.isAsync() && !functionNode.isGenerator()) {
465459
ensureHasSourceSection(body, functionNode);
466460
body = handleAsyncFunctionBody(body);
461+
ensureHasSourceSection(body, functionNode);
467462
}
468463

469464
if (!declarations.isEmpty()) {
@@ -514,11 +509,7 @@ private FunctionRootNode createFunctionRoot(FunctionNode functionNode, JSFunctio
514509
SourceSection functionSourceSection = createSourceSection(functionNode);
515510
FunctionBodyNode functionBody = factory.createFunctionBody(body);
516511
FunctionRootNode functionRoot = factory.createFunctionRootNode(functionBody, environment.getFunctionFrameDescriptor().toFrameDescriptor(), functionData, functionSourceSection,
517-
currentFunction.getInternalFunctionName());
518-
519-
if (currentFunction.isScriptOrModule()) {
520-
scriptOrModuleResolver = currentFunction.getScriptOrModuleResolver();
521-
}
512+
activeScriptOrModule, currentFunction.getInternalFunctionName());
522513

523514
if (JSConfig.PrintAst) {
524515
printAST(functionRoot);
@@ -534,8 +525,6 @@ private FunctionRootNode createFunctionRoot(FunctionNode functionNode, JSFunctio
534525
* @see #splitModuleBodyAtYield
535526
*/
536527
private FunctionRootNode createModuleRoot(FunctionNode functionNode, JSFunctionData functionData, FunctionEnvironment currentFunction, JavaScriptNode body) {
537-
scriptOrModuleResolver = currentFunction.getScriptOrModuleResolver();
538-
539528
if (JSConfig.PrintAst) {
540529
printAST(body);
541530
}
@@ -562,7 +551,7 @@ private FunctionRootNode createModuleRoot(FunctionNode functionNode, JSFunctionD
562551
FunctionBodyNode linkBody = factory.createFunctionBody(linkBlock);
563552
FunctionBodyNode evalBody = factory.createFunctionBody(evalBlock);
564553
return factory.createModuleRootNode(linkBody, evalBody, environment.getFunctionFrameDescriptor().toFrameDescriptor(), functionData,
565-
moduleSourceSection, internalFunctionName);
554+
moduleSourceSection, activeScriptOrModule, internalFunctionName);
566555
}
567556
}
568557
}
@@ -571,7 +560,7 @@ private FunctionRootNode createModuleRoot(FunctionNode functionNode, JSFunctionD
571560
currentFunction.addYield(); // yield has not been added yet.
572561
FunctionBodyNode generatorBody = factory.createFunctionBody(handleModuleBody(body));
573562
return factory.createModuleRootNode(generatorBody, generatorBody, environment.getFunctionFrameDescriptor().toFrameDescriptor(), functionData,
574-
moduleSourceSection, internalFunctionName);
563+
moduleSourceSection, activeScriptOrModule, internalFunctionName);
575564
}
576565

577566
private static void printAST(Node functionRoot) {
@@ -600,7 +589,8 @@ private JavaScriptNode handleAsyncFunctionBody(JavaScriptNode body) {
600589
JSWriteFrameSlotNode writeContextNode = (JSWriteFrameSlotNode) asyncContextVar.createWriteNode(null);
601590
JSReadFrameSlotNode readContextNode = (JSReadFrameSlotNode) asyncContextVar.createReadNode();
602591
JavaScriptNode instrumentedBody = instrumentSuspendNodes(body);
603-
return factory.createAsyncFunctionBody(context, instrumentedBody, writeContextNode, readContextNode, writeResultNode);
592+
return factory.createAsyncFunctionBody(context, instrumentedBody, writeContextNode, readContextNode, writeResultNode,
593+
createSourceSection(lc.getCurrentFunction()), currentFunction().getExplicitOrInternalFunctionName(), activeScriptOrModule);
604594
}
605595

606596
/**
@@ -625,7 +615,8 @@ private JavaScriptNode handleGeneratorBody(JavaScriptNode body) {
625615
VarRef yieldVar = environment.findYieldValueVar();
626616
JSWriteFrameSlotNode writeYieldValueNode = (JSWriteFrameSlotNode) yieldVar.createWriteNode(null);
627617
JSReadFrameSlotNode readYieldResultNode = JSConfig.YieldResultInFrame ? (JSReadFrameSlotNode) environment.findTempVar(currentFunction().getYieldResultSlot()).createReadNode() : null;
628-
return factory.createGeneratorBody(context, instrumentedBody, writeYieldValueNode, readYieldResultNode);
618+
return factory.createGeneratorBody(context, instrumentedBody, writeYieldValueNode, readYieldResultNode,
619+
createSourceSection(lc.getCurrentFunction()), currentFunction().getExplicitOrInternalFunctionName(), activeScriptOrModule);
629620
}
630621

631622
private JavaScriptNode handleAsyncGeneratorBody(JavaScriptNode body) {
@@ -637,7 +628,8 @@ private JavaScriptNode handleAsyncGeneratorBody(JavaScriptNode body) {
637628
JSReadFrameSlotNode readAsyncContextNode = (JSReadFrameSlotNode) asyncContextVar.createReadNode();
638629
JSWriteFrameSlotNode writeYieldValueNode = (JSWriteFrameSlotNode) yieldVar.createWriteNode(null);
639630
JSReadFrameSlotNode readYieldResultNode = JSConfig.YieldResultInFrame ? (JSReadFrameSlotNode) environment.findTempVar(currentFunction().getYieldResultSlot()).createReadNode() : null;
640-
return factory.createAsyncGeneratorBody(context, instrumentedBody, writeYieldValueNode, readYieldResultNode, writeAsyncContextNode, readAsyncContextNode);
631+
return factory.createAsyncGeneratorBody(context, instrumentedBody, writeYieldValueNode, readYieldResultNode, writeAsyncContextNode, readAsyncContextNode,
632+
createSourceSection(lc.getCurrentFunction()), currentFunction().getExplicitOrInternalFunctionName(), activeScriptOrModule);
641633
}
642634

643635
private JavaScriptNode handleModuleBody(JavaScriptNode body) {
@@ -648,7 +640,8 @@ private JavaScriptNode handleModuleBody(JavaScriptNode body) {
648640
VarRef yieldVar = environment.findAsyncResultVar();
649641
JSWriteFrameSlotNode writeAsyncContextNode = (JSWriteFrameSlotNode) asyncContextVar.createWriteNode(null);
650642
JSWriteFrameSlotNode writeYieldValueNode = (JSWriteFrameSlotNode) yieldVar.createWriteNode(null);
651-
return factory.createTopLevelAsyncModuleBody(context, instrumentedBody, writeYieldValueNode, writeAsyncContextNode);
643+
return factory.createTopLevelAsyncModuleBody(context, instrumentedBody, writeYieldValueNode, writeAsyncContextNode,
644+
createSourceSection(lc.getCurrentFunction()), activeScriptOrModule);
652645
} else {
653646
JavaScriptNode instrumentedBody = instrumentSuspendNodes(body);
654647
return factory.createModuleBody(instrumentedBody);
@@ -1867,10 +1860,6 @@ private JavaScriptNode getActiveModule() {
18671860
return environment.findActiveModule().createReadNode();
18681861
}
18691862

1870-
private JavaScriptNode getActiveScriptOrModule() {
1871-
return environment.findActiveScriptOrModule().createReadNode();
1872-
}
1873-
18741863
private VarRef findScopeVar(TruffleString name, boolean skipWith) {
18751864
return environment.findVar(name, skipWith);
18761865
}
@@ -2640,7 +2629,8 @@ private JavaScriptNode initializeInstanceElements(JavaScriptNode thisValueNode)
26402629
private JavaScriptNode createCallEvalNode(JavaScriptNode function, JavaScriptNode[] args) {
26412630
assert (currentFunction().isGlobal() || currentFunction().isStrictMode() || currentFunction().isDirectEval()) || currentFunction().isDynamicallyScoped();
26422631
currentFunction().prepareForDirectEval();
2643-
return EvalNode.create(context, function, args, createThisNodeUnchecked(), new DirectEvalContext(lc.getCurrentScope(), environment, lc.getCurrentClass()),
2632+
return EvalNode.create(context, function, args, createThisNodeUnchecked(),
2633+
new DirectEvalContext(lc.getCurrentScope(), environment, lc.getCurrentClass(), activeScriptOrModule),
26442634
environment.getCurrentBlockScopeSlot());
26452635
}
26462636

@@ -2659,7 +2649,6 @@ private JavaScriptNode createCallDirectSuper(JavaScriptNode function, JavaScript
26592649

26602650
private JavaScriptNode createImportCallNode(JavaScriptNode[] args) {
26612651
assert args.length == 1 || (context.getContextOptions().isImportAssertions() && args.length == 2);
2662-
JavaScriptNode activeScriptOrModule = getActiveScriptOrModule();
26632652
if (context.getContextOptions().isImportAssertions() && args.length == 2) {
26642653
return factory.createImportCall(context, args[0], activeScriptOrModule, args[1]);
26652654
}

0 commit comments

Comments
 (0)