Skip to content

Commit

Permalink
lib: always initialize esm loader callbackMap
Browse files Browse the repository at this point in the history
PR-URL: #34127
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gus Caplan <me@gus.host>
  • Loading branch information
codebytere authored and addaleax committed Sep 23, 2020
1 parent 5e28660 commit efd46e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
7 changes: 3 additions & 4 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,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 @@ -416,6 +413,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

0 comments on commit efd46e3

Please sign in to comment.