Skip to content

Commit 003fcd2

Browse files
codebytereMylesBorins
authored andcommitted
lib: always initialize esm loader callbackMap
PR-URL: #34127 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gus Caplan <[email protected]>
1 parent 74e30ee commit 003fcd2

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

lib/internal/bootstrap/pre_execution.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ function prepareMainThreadExecution(expandArgv1 = false) {
6767
initializeDeprecations();
6868
initializeWASI();
6969
initializeCJSLoader();
70-
71-
if (!shouldNotRegisterESMLoader) {
72-
initializeESMLoader();
73-
}
70+
initializeESMLoader();
7471

7572
const CJSLoader = require('internal/modules/cjs/loader');
7673
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
@@ -414,6 +411,8 @@ function initializeESMLoader() {
414411
// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
415412
internalBinding('module_wrap').callbackMap = new SafeWeakMap();
416413

414+
if (shouldNotRegisterESMLoader) return;
415+
417416
const {
418417
setImportModuleDynamicallyCallback,
419418
setInitializeImportMetaObjectCallback

test/cctest/test_environment.cc

+29-12
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,21 @@ TEST_F(EnvironmentTest, EnvironmentWithESMLoader) {
4949
node::LoadEnvironment(
5050
*env,
5151
"const { SourceTextModule } = require('vm');"
52-
"try {"
53-
"new SourceTextModule('export const a = 1;');"
52+
"(async () => {"
53+
"const stmString = 'globalThis.importResult = import(\"\")';"
54+
"const m = new SourceTextModule(stmString, {"
55+
"importModuleDynamically: (async () => {"
56+
"const m = new SourceTextModule('');"
57+
"await m.link(() => 0);"
58+
"await m.evaluate();"
59+
"return m.namespace;"
60+
"}),"
61+
"});"
62+
"await m.link(() => 0);"
63+
"await m.evaluate();"
64+
"delete globalThis.importResult;"
5465
"process.exit(0);"
55-
"} catch {"
56-
"process.exit(42);"
57-
"}");
66+
"})()");
5867
}
5968

6069
TEST_F(EnvironmentTest, EnvironmentWithNoESMLoader) {
@@ -67,19 +76,27 @@ TEST_F(EnvironmentTest, EnvironmentWithNoESMLoader) {
6776

6877
SetProcessExitHandler(*env, [&](node::Environment* env_, int exit_code) {
6978
EXPECT_EQ(*env, env_);
70-
EXPECT_EQ(exit_code, 42);
79+
EXPECT_EQ(exit_code, 1);
7180
node::Stop(*env);
7281
});
7382

7483
node::LoadEnvironment(
7584
*env,
7685
"const { SourceTextModule } = require('vm');"
77-
"try {"
78-
"new SourceTextModule('export const a = 1;');"
79-
"process.exit(0);"
80-
"} catch {"
81-
"process.exit(42);"
82-
"}");
86+
"(async () => {"
87+
"const stmString = 'globalThis.importResult = import(\"\")';"
88+
"const m = new SourceTextModule(stmString, {"
89+
"importModuleDynamically: (async () => {"
90+
"const m = new SourceTextModule('');"
91+
"await m.link(() => 0);"
92+
"await m.evaluate();"
93+
"return m.namespace;"
94+
"}),"
95+
"});"
96+
"await m.link(() => 0);"
97+
"await m.evaluate();"
98+
"delete globalThis.importResult;"
99+
"})()");
83100
}
84101

85102
TEST_F(EnvironmentTest, PreExecutionPreparation) {

0 commit comments

Comments
 (0)