Skip to content

Commit 82d865e

Browse files
committed
[GR-40100] Do not wrap Commonjs module sources.
PullRequest: js/2540
2 parents 229d6c5 + 9029b1a commit 82d865e

File tree

7 files changed

+39
-14
lines changed

7 files changed

+39
-14
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public ScriptNode parseEval(JSContext context, Node lastNode, Source source) {
152152
*/
153153
@TruffleBoundary(transferToInterpreterOnException = false)
154154
@Override
155-
public ScriptNode parseFunction(JSContext context, String parameterList, String body, boolean generatorFunction, boolean asyncFunction, String sourceName) {
155+
public ScriptNode parseFunction(JSContext context, String parameterList, String body, boolean generatorFunction, boolean asyncFunction, String sourceName, Source inheritFrom) {
156156
String wrappedBody = "\n" + body + "\n";
157157
try {
158158
GraalJSParserHelper.checkFunctionSyntax(context, context.getParserOptions(), parameterList, wrappedBody, generatorFunction, asyncFunction, sourceName);
@@ -182,8 +182,12 @@ public ScriptNode parseFunction(JSContext context, String parameterList, String
182182
code.append(") {");
183183
code.append(wrappedBody);
184184
code.append("})");
185-
Source source = Source.newBuilder(JavaScriptLanguage.ID, code.toString(), sourceName).build();
186-
185+
Source source;
186+
if (inheritFrom == null) {
187+
source = Source.newBuilder(JavaScriptLanguage.ID, code.toString(), sourceName).build();
188+
} else {
189+
source = Source.newBuilder(inheritFrom).content(code.toString()).name(sourceName).build();
190+
}
187191
return parseEval(context, null, source, false, null);
188192
}
189193

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
10+
console.log("This shouldn't work!");
11+
12+
(function () {

graal-js/src/com.oracle.truffle.js.test/src/com/oracle/truffle/js/test/builtins/CommonJSRequireTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,17 @@ public void requireEmpty() {
617617
}
618618
}
619619

620+
@Test
621+
public void testGit621() {
622+
Path f = getTestRootFolder();
623+
try (Context cx = testContext(f)) {
624+
cx.eval(ID, "require('./github-621').foo;");
625+
assert false : "Should throw";
626+
} catch (PolyglotException e) {
627+
Assert.assertTrue(e.getMessage().contains("Expected eof but found }"));
628+
}
629+
}
630+
620631
// ##### ES Modules
621632

622633
@Test

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/ConstructorBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,7 @@ protected ScriptNode cacheLookup(LRUCache<CachedSourceKey, ScriptNode> cache, Ca
21432143
@TruffleBoundary(transferToInterpreterOnException = false)
21442144
protected final ScriptNode parseFunction(String paramList, String body, String sourceName) {
21452145
CompilerAsserts.neverPartOfCompilation();
2146-
return context.getEvaluator().parseFunction(context, paramList, body, generatorFunction, asyncFunction, sourceName);
2146+
return context.getEvaluator().parseFunction(context, paramList, body, generatorFunction, asyncFunction, sourceName, null);
21472147
}
21482148

21492149
@TruffleBoundary(transferToInterpreterOnException = false)

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/commonjs/CommonJSRequireBuiltin.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@
4444
import java.util.Objects;
4545
import java.util.Stack;
4646

47-
import com.oracle.truffle.api.CallTarget;
4847
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4948
import com.oracle.truffle.api.TruffleFile;
5049
import com.oracle.truffle.api.TruffleLanguage;
5150
import com.oracle.truffle.api.dsl.Specialization;
5251
import com.oracle.truffle.api.source.Source;
5352
import com.oracle.truffle.api.strings.TruffleString;
5453
import com.oracle.truffle.js.builtins.GlobalBuiltins;
55-
import com.oracle.truffle.js.lang.JavaScriptLanguage;
54+
import com.oracle.truffle.js.nodes.ScriptNode;
5655
import com.oracle.truffle.js.nodes.function.JSBuiltin;
5756
import com.oracle.truffle.js.runtime.Errors;
5857
import com.oracle.truffle.js.runtime.JSArguments;
@@ -122,8 +121,7 @@ private static void debugStackPop() {
122121
}
123122
}
124123

125-
private static final String MODULE_END = "\n});";
126-
private static final String MODULE_PREAMBLE = "(function (exports, require, module, __filename, __dirname) {";
124+
private static final String MODULE_FUNCTION_ARGS = "exports, require, module, __filename, __dirname";
127125

128126
@TruffleBoundary
129127
static TruffleFile getModuleResolveCurrentWorkingDirectory(JSContext context, TruffleLanguage.Env env) {
@@ -224,10 +222,10 @@ private Object evalJavaScriptFile(TruffleFile modulePath, String moduleIdentifie
224222
JSObject env = JSOrdinary.create(getContext(), getRealm());
225223
JSObject.set(env, Strings.ENV_PROPERTY_NAME, JSOrdinary.create(getContext(), getRealm()));
226224
// Parse the module
227-
CharSequence characters = MODULE_PREAMBLE + source.getCharacters() + MODULE_END;
228-
Source moduleSources = Source.newBuilder(source).content(characters).mimeType(JavaScriptLanguage.TEXT_MIME_TYPE).build();
229-
CallTarget moduleCallTarget = realm.getEnv().parsePublic(moduleSources);
230-
Object moduleExecutableFunction = moduleCallTarget.call();
225+
JSContext context = realm.getContext();
226+
String body = source.getCharacters().toString();
227+
ScriptNode scriptNode = context.getEvaluator().parseFunction(context, MODULE_FUNCTION_ARGS, body, false, false, source.getPath(), source);
228+
Object moduleExecutableFunction = scriptNode.run(realm);
231229
// Execute the module.
232230
if (JSFunction.isJSFunction(moduleExecutableFunction)) {
233231
log("adding to cache ", normalizedPath);

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/Evaluator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public interface Evaluator {
121121
/**
122122
* Parse function using parameter list and body, to be used by the {@code Function} constructor.
123123
*/
124-
ScriptNode parseFunction(JSContext context, String parameterList, String body, boolean generatorFunction, boolean asyncFunction, String sourceName);
124+
ScriptNode parseFunction(JSContext context, String parameterList, String body, boolean generatorFunction, boolean asyncFunction, String sourceName, Source inheritFrom);
125125

126126
default ScriptNode parseScript(JSContext context, Source source) {
127127
return parseScript(context, source, "", "", context.getParserOptions().isStrict());

graal-nodejs/mx.graal-nodejs/com.oracle.truffle.trufflenode/src/com/oracle/truffle/trufflenode/GraalJSAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ public Object scriptCompilerCompileFunctionInContext(Object context, Object sour
21982198
GraalJSParserHelper.checkFunctionSyntax(jsContext, parserOptions, parameterList, bodyJavaString, false, false, sourceNameJavaString);
21992199
} catch (com.oracle.js.parser.ParserException ex) {
22002200
// throw the correct JS error
2201-
nodeEvaluator.parseFunction(jsContext, parameterList, bodyJavaString, false, false, sourceNameJavaString);
2201+
nodeEvaluator.parseFunction(jsContext, parameterList, bodyJavaString, false, false, sourceNameJavaString, null);
22022202
}
22032203

22042204
StringBuilder code = new StringBuilder();

0 commit comments

Comments
 (0)