Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ function prepareMainThreadExecution(expandArgv1 = false) {
initializeDeprecations();
initializeWASI();
initializeCJSLoader();

if (!shouldNotRegisterESMLoader) {
initializeESMLoader();
}
initializeESMLoader();

const CJSLoader = require('internal/modules/cjs/loader');
assert(!CJSLoader.hasLoadedAnyUserCJSModule);
Expand Down Expand Up @@ -406,6 +403,8 @@ function initializeESMLoader() {
// Create this WeakMap in js-land because V8 has no C++ API for WeakMap.
internalBinding('module_wrap').callbackMap = new SafeWeakMap();

if (shouldNotRegisterESMLoader) return;

const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback
Expand Down
41 changes: 29 additions & 12 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ TEST_F(EnvironmentTest, EnvironmentWithESMLoader) {
node::LoadEnvironment(
*env,
"const { SourceTextModule } = require('vm');"
"try {"
"new SourceTextModule('export const a = 1;');"
"(async () => {"
"const stmString = 'globalThis.importResult = import(\"\")';"
"const m = new SourceTextModule(stmString, {"
"importModuleDynamically: (async () => {"
"const m = new SourceTextModule('');"
"await m.link(() => 0);"
"await m.evaluate();"
"return m.namespace;"
"}),"
"});"
"await m.link(() => 0);"
"await m.evaluate();"
"delete globalThis.importResult;"
"process.exit(0);"
"} catch {"
"process.exit(42);"
"}");
"})()");
}

TEST_F(EnvironmentTest, EnvironmentWithNoESMLoader) {
Expand All @@ -67,19 +76,27 @@ TEST_F(EnvironmentTest, EnvironmentWithNoESMLoader) {

SetProcessExitHandler(*env, [&](node::Environment* env_, int exit_code) {
EXPECT_EQ(*env, env_);
EXPECT_EQ(exit_code, 42);
EXPECT_EQ(exit_code, 1);
node::Stop(*env);
});

node::LoadEnvironment(
*env,
"const { SourceTextModule } = require('vm');"
"try {"
"new SourceTextModule('export const a = 1;');"
"process.exit(0);"
"} catch {"
"process.exit(42);"
"}");
"(async () => {"
"const stmString = 'globalThis.importResult = import(\"\")';"
"const m = new SourceTextModule(stmString, {"
"importModuleDynamically: (async () => {"
"const m = new SourceTextModule('');"
"await m.link(() => 0);"
"await m.evaluate();"
"return m.namespace;"
"}),"
"});"
"await m.link(() => 0);"
"await m.evaluate();"
"delete globalThis.importResult;"
"})()");
}

TEST_F(EnvironmentTest, PreExecutionPreparation) {
Expand Down