From 8bafba2e560a65dffabccc56ac1c270442e249f2 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 1 Jul 2020 12:05:54 -0700 Subject: [PATCH] lib: always initialize esm loader callbackMap PR-URL: https://github.com/nodejs/node/pull/34127 Reviewed-By: Anna Henningsen Reviewed-By: Gus Caplan --- lib/internal/bootstrap/pre_execution.js | 7 ++--- test/cctest/test_environment.cc | 41 +++++++++++++++++-------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/lib/internal/bootstrap/pre_execution.js b/lib/internal/bootstrap/pre_execution.js index 0e1b4898ef1301..e227e7853f5462 100644 --- a/lib/internal/bootstrap/pre_execution.js +++ b/lib/internal/bootstrap/pre_execution.js @@ -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); @@ -414,6 +411,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 diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index 0314c7e5b8ad04..11fc99a73bc5bb 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -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) { @@ -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) {