Skip to content

Commit 3cd9f31

Browse files
committed
[GR-44877] Remove 'node:' prefix when looking for core module replacement.
PullRequest: js/2752
2 parents 4c35427 + 5081045 commit 3cd9f31

File tree

5 files changed

+46
-30
lines changed

5 files changed

+46
-30
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -476,6 +476,19 @@ public void testCustomNodeBuiltin() {
476476
}
477477
}
478478

479+
@Test
480+
public void testCustomNodeBuiltinNodePrefix() {
481+
Path root = getTestRootFolder();
482+
Map<String, String> options = new HashMap<>();
483+
options.put(COMMONJS_REQUIRE_NAME, "true");
484+
options.put(COMMONJS_REQUIRE_CWD_NAME, root.toAbsolutePath().toString());
485+
options.put(COMMONJS_CORE_MODULES_REPLACEMENTS_NAME, "util:./module");
486+
try (Context cx = testContext(options)) {
487+
Value js = cx.eval(ID, "require('node:util').foo");
488+
Assert.assertEquals(42, js.asInt());
489+
}
490+
}
491+
479492
@Test
480493
public void testNotNodeBuiltin() {
481494
Path root = getTestRootFolder();

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -45,7 +45,7 @@
4545
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.JS_EXT;
4646
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.MJS_EXT;
4747
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.NODE_EXT;
48-
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.hasCoreModuleReplacement;
48+
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.getCoreModuleReplacement;
4949

5050
import java.util.Map;
5151
import java.util.Objects;
@@ -170,14 +170,11 @@ protected static Object fallback(@SuppressWarnings("unused") Object function, Ob
170170
@TruffleBoundary
171171
private Object requireImpl(String moduleIdentifier, TruffleFile entryPath, JSRealm realm) {
172172
log("required module '", moduleIdentifier, "' from path ", entryPath);
173-
if (hasCoreModuleReplacement(getContext(), moduleIdentifier)) {
174-
String moduleReplacementName = getContext().getContextOptions().getCommonJSRequireBuiltins().get(moduleIdentifier);
175-
if (moduleReplacementName != null && !moduleReplacementName.isEmpty()) {
176-
log("using module replacement for module '", moduleIdentifier, "' with ", moduleReplacementName);
177-
return requireImpl(moduleReplacementName, getRequireCwd(getContext(), realm.getEnv()), realm);
178-
}
179-
// no core module replacement alias was found: continue and search in the FS.
180-
}
173+
String moduleReplacementName = getCoreModuleReplacement(getContext(), moduleIdentifier);
174+
if (moduleReplacementName != null) {
175+
log("using module replacement for module '", moduleIdentifier, "' with ", moduleReplacementName);
176+
return requireImpl(moduleReplacementName, getRequireCwd(getContext(), realm.getEnv()), realm);
177+
} // no core module replacement alias was found: continue and search in the FS.
181178
TruffleFile maybeModule;
182179
try {
183180
maybeModule = CommonJSResolution.resolve(realm, moduleIdentifier, entryPath);

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -84,11 +84,14 @@ public final class CommonJSResolution {
8484
public static final String JSON_EXT = ".json";
8585
public static final String NODE_EXT = ".node";
8686

87+
private static final String NODE_PREFIX = "node:";
88+
8789
private CommonJSResolution() {
8890
}
8991

90-
public static boolean hasCoreModuleReplacement(JSContext context, String moduleIdentifier) {
91-
return context.getContextOptions().getCommonJSRequireBuiltins().containsKey(moduleIdentifier);
92+
public static String getCoreModuleReplacement(JSContext context, String moduleIdentifier) {
93+
String identifier = moduleIdentifier.startsWith(NODE_PREFIX) ? moduleIdentifier.substring(NODE_PREFIX.length()) : moduleIdentifier;
94+
return context.getContextOptions().getCommonJSRequireBuiltins().get(identifier);
9295
}
9396

9497
static String getCurrentFileNameFromStack() {

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2022, 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
@@ -48,7 +48,7 @@
4848
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.MJS_EXT;
4949
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.NODE_MODULES;
5050
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.PACKAGE_JSON;
51-
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.hasCoreModuleReplacement;
51+
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.getCoreModuleReplacement;
5252
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.joinPaths;
5353
import static com.oracle.truffle.js.builtins.commonjs.CommonJSResolution.loadJsonObject;
5454
import static com.oracle.truffle.js.lang.JavaScriptLanguage.ID;
@@ -125,8 +125,9 @@ private NpmCompatibleESModuleLoader(JSRealm realm) {
125125
public JSModuleRecord resolveImportedModule(ScriptOrModule referencingModule, ModuleRequest moduleRequest) {
126126
String specifier = moduleRequest.getSpecifier().toJavaStringUncached();
127127
log("IMPORT resolve ", specifier);
128-
if (hasCoreModuleReplacement(realm.getContext(), specifier)) {
129-
return loadCoreModuleReplacement(referencingModule, moduleRequest);
128+
String moduleReplacementName = getCoreModuleReplacement(realm.getContext(), specifier);
129+
if (moduleReplacementName != null) {
130+
return loadCoreModuleReplacement(referencingModule, moduleRequest, moduleReplacementName);
130131
}
131132
try {
132133
TruffleLanguage.Env env = realm.getEnv();
@@ -156,17 +157,16 @@ public JSModuleRecord resolveImportedModule(ScriptOrModule referencingModule, Mo
156157
}
157158
}
158159

159-
private JSModuleRecord loadCoreModuleReplacement(ScriptOrModule referencingModule, ModuleRequest moduleRequest) {
160+
private JSModuleRecord loadCoreModuleReplacement(ScriptOrModule referencingModule, ModuleRequest moduleRequest, String moduleReplacementName) {
160161
String specifier = moduleRequest.getSpecifier().toJavaStringUncached();
161162
log("IMPORT resolve built-in ", specifier);
162163
JSModuleRecord existingModule = moduleMap.get(specifier);
163164
if (existingModule != null) {
164165
log("IMPORT resolve built-in from cache ", specifier);
165166
return existingModule;
166167
}
167-
String moduleReplacementName = realm.getContext().getContextOptions().getCommonJSRequireBuiltins().get(specifier);
168168
Source src;
169-
if (moduleReplacementName != null && moduleReplacementName.endsWith(MJS_EXT)) {
169+
if (moduleReplacementName.endsWith(MJS_EXT)) {
170170
URI maybeUri = asURI(moduleReplacementName);
171171
if (maybeUri != null) {
172172
// Load from URI
@@ -193,7 +193,7 @@ private JSModuleRecord loadCoreModuleReplacement(ScriptOrModule referencingModul
193193
}
194194
} else {
195195
// Else, try loading as commonjs built-in module replacement
196-
return tryLoadingAsCommonjsModule(moduleRequest.getSpecifier().toJavaStringUncached());
196+
return tryLoadingAsCommonjsModule(specifier);
197197
}
198198
JSModuleData parsedModule = realm.getContext().getEvaluator().envParseModule(realm, src);
199199
JSModuleRecord record = new JSModuleRecord(parsedModule, this);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,22 @@ public String apply(String tz) {
306306
new OptionType<>("commonjs-require-globals", new Function<String, Map<String, String>>() {
307307
@Override
308308
public Map<String, String> apply(String value) {
309-
Map<String, String> map = new HashMap<>();
310-
if ("".equals(value)) {
311-
return map;
309+
if (value.isEmpty()) {
310+
return Collections.emptyMap();
312311
}
312+
Map<String, String> map = new HashMap<>();
313313
String[] options = value.split(",");
314314
for (String s : options) {
315315
String[] builtin = s.split(":", 2);
316-
if (builtin.length != 2) {
317-
throw new IllegalArgumentException("Unexpected builtin arguments: " + s);
316+
if (builtin.length == 2) {
317+
String key = builtin[0];
318+
String val = builtin[1];
319+
if (!key.isEmpty() && !val.isEmpty()) {
320+
map.put(key, val);
321+
continue;
322+
}
318323
}
319-
String key = builtin[0];
320-
String val = builtin[1];
321-
map.put(key, val);
324+
throw new IllegalArgumentException("Unexpected builtin arguments: " + s);
322325
}
323326
return map;
324327
}

0 commit comments

Comments
 (0)