diff --git a/lib/internal/main/embedding.js b/lib/internal/main/embedding.js index 91a12f755e6abc..863f90a32f40ac 100644 --- a/lib/internal/main/embedding.js +++ b/lib/internal/main/embedding.js @@ -15,16 +15,12 @@ const { const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea'); const { emitExperimentalWarning } = require('internal/util'); const { emitWarningSync } = require('internal/process/warning'); -const { BuiltinModule } = require('internal/bootstrap/realm'); -const { normalizeRequirableId } = BuiltinModule; const { Module } = require('internal/modules/cjs/loader'); const { compileFunctionForCJSLoader } = internalBinding('contextify'); const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); -const { codes: { - ERR_UNKNOWN_BUILTIN_MODULE, -} } = require('internal/errors'); const { pathToFileURL } = require('internal/url'); -const { loadBuiltinModule } = require('internal/modules/helpers'); +const { loadBuiltinModuleForEmbedder } = require('internal/modules/helpers'); +const { compileSourceTextModule, SourceTextModuleTypes: { kEmbedder } } = require('internal/modules/esm/utils'); const { moduleFormats } = internalBinding('modules'); const assert = require('internal/assert'); const path = require('path'); @@ -34,7 +30,6 @@ const path = require('path'); prepareMainThreadExecution(false, true); const isLoadingSea = isSea(); -const isBuiltinWarningNeeded = isLoadingSea && isExperimentalSeaWarningNeeded(); if (isExperimentalSeaWarningNeeded()) { emitExperimentalWarning('Single executable application'); } @@ -65,6 +60,7 @@ function embedderRunCjs(content, filename) { filename, isLoadingSea, // is_sea_main false, // should_detect_module, ESM should be supported differently for embedded code + true, // is_embedder ); // Cache the source map for the module if present. if (sourceMapURL) { @@ -103,28 +99,8 @@ function embedderRunCjs(content, filename) { ); } -let warnedAboutBuiltins = false; -function warnNonBuiltinInSEA() { - if (isBuiltinWarningNeeded && !warnedAboutBuiltins) { - emitWarningSync( - 'Currently the require() provided to the main script embedded into ' + - 'single-executable applications only supports loading built-in modules.\n' + - 'To load a module from disk after the single executable application is ' + - 'launched, use require("module").createRequire().\n' + - 'Support for bundled module loading or virtual file systems are under ' + - 'discussions in https://github.com/nodejs/single-executable'); - warnedAboutBuiltins = true; - } -} - function embedderRequire(id) { - const normalizedId = normalizeRequirableId(id); - - if (!normalizedId) { - warnNonBuiltinInSEA(); - throw new ERR_UNKNOWN_BUILTIN_MODULE(id); - } - return require(normalizedId); + return loadBuiltinModuleForEmbedder(id).exports; } function embedderRunESM(content, filename) { @@ -134,31 +110,10 @@ function embedderRunESM(content, filename) { } else { resourceName = filename; } - const { compileSourceTextModule } = require('internal/modules/esm/utils'); - // TODO(joyeecheung): support code cache, dynamic import() and import.meta. - const wrap = compileSourceTextModule(resourceName, content); - // Cache the source map for the module if present. - if (wrap.sourceMapURL) { - maybeCacheSourceMap(resourceName, content, wrap, false, undefined, wrap.sourceMapURL); - } - const requests = wrap.getModuleRequests(); - const modules = []; - for (let i = 0; i < requests.length; ++i) { - const { specifier } = requests[i]; - const normalizedId = normalizeRequirableId(specifier); - if (!normalizedId) { - warnNonBuiltinInSEA(); - throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); - } - const mod = loadBuiltinModule(normalizedId); - if (!mod) { - throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); - } - modules.push(mod.getESMFacade()); - } - wrap.link(modules); - wrap.instantiate(); - wrap.evaluate(-1, false); + // TODO(joyeecheung): allow configuration from node::ModuleData, + // either via a more generic context object, or something like import.meta extensions. + const context = { isMain: true, __proto__: null }; + const wrap = compileSourceTextModule(resourceName, content, kEmbedder, context); // TODO(joyeecheung): we may want to return the v8::Module via a vm.SourceTextModule // when vm.SourceTextModule stablizes, or put it in an out parameter. diff --git a/lib/internal/modules/esm/create_dynamic_module.js b/lib/internal/modules/esm/create_dynamic_module.js index 068893ce4361f1..e38a350ba39d13 100644 --- a/lib/internal/modules/esm/create_dynamic_module.js +++ b/lib/internal/modules/esm/create_dynamic_module.js @@ -58,8 +58,10 @@ ${ArrayPrototypeJoin(ArrayPrototypeMap(imports, createImport), '\n')} ${ArrayPrototypeJoin(ArrayPrototypeMap(exports, createExport), '\n')} import.meta.done(); `; - const { registerModule, compileSourceTextModule } = require('internal/modules/esm/utils'); - const m = compileSourceTextModule(`${url}`, source); + const { + registerModule, compileSourceTextModule, SourceTextModuleTypes: { kDynamic }, + } = require('internal/modules/esm/utils'); + const m = compileSourceTextModule(`${url}`, source, kDynamic); const readyfns = new SafeSet(); /** @type {DynamicModuleReflect} */ diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 0bff0763fcf58f..41513d1b9f3658 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -34,6 +34,7 @@ const { isURL, pathToFileURL } = require('internal/url'); const { kEmptyObject } = require('internal/util'); const { compileSourceTextModule, + SourceTextModuleTypes: { kUser }, getDefaultConditions, shouldSpawnLoaderHookWorker, requestTypes: { kImportInRequiredESM, kRequireInImportedCJS, kImportInImportedESM }, @@ -244,7 +245,7 @@ class ModuleLoader { * @returns {object} The module wrap object. */ createModuleWrap(source, url, context = kEmptyObject) { - return compileSourceTextModule(url, source, this, context); + return compileSourceTextModule(url, source, kUser, context); } /** @@ -371,7 +372,7 @@ class ModuleLoader { // TODO(joyeecheung): refactor this so that we pre-parse in C++ and hit the // cache here, or use a carrier object to carry the compiled module script // into the constructor to ensure cache hit. - const wrap = compileSourceTextModule(url, source, this); + const wrap = compileSourceTextModule(url, source, kUser); const inspectBrk = (isMain && getOptionValue('--inspect-brk')); const { ModuleJobSync } = require('internal/modules/esm/module_job'); diff --git a/lib/internal/modules/esm/translators.js b/lib/internal/modules/esm/translators.js index 6658b302c19bd4..62aae58891dcb7 100644 --- a/lib/internal/modules/esm/translators.js +++ b/lib/internal/modules/esm/translators.js @@ -93,9 +93,11 @@ translators.set('module', function moduleStrategy(url, translateContext, parentU assertBufferSource(source, true, 'load'); source = stringify(source); debug(`Translating StandardModule ${url}`, translateContext); - const { compileSourceTextModule } = require('internal/modules/esm/utils'); + const { + compileSourceTextModule, SourceTextModuleTypes: { kUser }, + } = require('internal/modules/esm/utils'); const context = isMain ? { isMain } : undefined; - const module = compileSourceTextModule(url, source, this, context); + const module = compileSourceTextModule(url, source, kUser, context); return module; }); diff --git a/lib/internal/modules/esm/utils.js b/lib/internal/modules/esm/utils.js index e28ecd923cb597..6b8243a5efc36f 100644 --- a/lib/internal/modules/esm/utils.js +++ b/lib/internal/modules/esm/utils.js @@ -14,6 +14,7 @@ const { }, } = internalBinding('util'); const { + embedder_module_hdo, source_text_module_default_hdo, vm_dynamic_import_default_internal, vm_dynamic_import_main_context_default, @@ -43,6 +44,7 @@ const { const assert = require('internal/assert'); const { normalizeReferrerURL, + loadBuiltinModuleForEmbedder, } = require('internal/modules/helpers'); let defaultConditions; @@ -226,6 +228,28 @@ function defaultImportModuleDynamicallyForScript(specifier, phase, attributes, r return cascadedLoader.import(specifier, parentURL, attributes, phase); } +/** + * Loads the built-in and wraps it in a ModuleWrap for embedder ESM. + * @param {string} specifier + * @returns {ModuleWrap} + */ +function getBuiltinModuleWrapForEmbedder(specifier) { + return loadBuiltinModuleForEmbedder(specifier).getESMFacade(); +} + +/** + * Get the built-in module dynamically for embedder ESM. + * @param {string} specifier - The module specifier string. + * @param {number} phase - The module import phase. Ignored for now. + * @param {Record} attributes - The import attributes object. Ignored for now. + * @param {string|null|undefined} referrerName - name of the referrer. + * @returns {import('internal/modules/esm/loader.js').ModuleExports} - The imported module object. + */ +function importModuleDynamicallyForEmbedder(specifier, phase, attributes, referrerName) { + // Ignore phase and attributes for embedder ESM for now, because this only supports loading builtins. + return getBuiltinModuleWrapForEmbedder(specifier).getNamespace(); +} + /** * Asynchronously imports a module dynamically using a callback function. The native callback. * @param {symbol} referrerSymbol - Referrer symbol of the registered script, function, module, or contextified object. @@ -253,6 +277,10 @@ async function importModuleDynamicallyCallback(referrerSymbol, specifier, phase, if (referrerSymbol === source_text_module_default_hdo) { return defaultImportModuleDynamicallyForModule(specifier, phase, attributes, referrerName); } + // For embedder entry point ESM, only allow built-in modules. + if (referrerSymbol === embedder_module_hdo) { + return importModuleDynamicallyForEmbedder(specifier, phase, attributes, referrerName); + } if (moduleRegistries.has(referrerSymbol)) { const { importModuleDynamically, callbackReferrer } = moduleRegistries.get(referrerSymbol); @@ -290,21 +318,42 @@ function shouldSpawnLoaderHookWorker() { return _shouldSpawnLoaderHookWorker; } +const SourceTextModuleTypes = { + kInternal: 'internal', // TODO(joyeecheung): support internal ESM. + kEmbedder: 'embedder', // Embedder ESM, also used by SEA + kUser: 'user', // User-land ESM + kDynamic: 'dynamic', // Currently only used by the facade that proxies WASM module import/exports. +}; + /** * Compile a SourceTextModule for the built-in ESM loader. Register it for default * source map and import.meta and dynamic import() handling if cascadedLoader is provided. * @param {string} url URL of the module. * @param {string} source Source code of the module. - * @param {typeof import('./loader.js').ModuleLoader|undefined} cascadedLoader If provided, - * register the module for default handling. + * @param {string} type Type of the source text module, one of SourceTextModuleTypes. * @param {{ isMain?: boolean }|undefined} context - context object containing module metadata. * @returns {ModuleWrap} */ -function compileSourceTextModule(url, source, cascadedLoader, context = kEmptyObject) { - const hostDefinedOption = cascadedLoader ? source_text_module_default_hdo : undefined; - const wrap = new ModuleWrap(url, undefined, source, 0, 0, hostDefinedOption); +function compileSourceTextModule(url, source, type, context = kEmptyObject) { + let hostDefinedOptions; + switch (type) { + case SourceTextModuleTypes.kDynamic: + case SourceTextModuleTypes.kInternal: + hostDefinedOptions = undefined; + break; + case SourceTextModuleTypes.kEmbedder: + hostDefinedOptions = embedder_module_hdo; + break; + case SourceTextModuleTypes.kUser: + hostDefinedOptions = source_text_module_default_hdo; + break; + default: + assert.fail(`Unknown SourceTextModule type: ${type}`); + } + + const wrap = new ModuleWrap(url, undefined, source, 0, 0, hostDefinedOptions); - if (!cascadedLoader) { + if (type === SourceTextModuleTypes.kDynamic) { return wrap; } @@ -317,10 +366,18 @@ function compileSourceTextModule(url, source, cascadedLoader, context = kEmptyOb if (wrap.sourceMapURL) { maybeCacheSourceMap(url, source, wrap, false, wrap.sourceURL, wrap.sourceMapURL); } + + if (type === SourceTextModuleTypes.kEmbedder) { + // For embedder ESM, we also handle the linking and evaluation. + const requests = wrap.getModuleRequests(); + const modules = requests.map(({ specifier }) => getBuiltinModuleWrapForEmbedder(specifier)); + wrap.link(modules); + wrap.instantiate(); + wrap.evaluate(-1, false); + } return wrap; } - const kImportInImportedESM = Symbol('kImportInImportedESM'); const kImportInRequiredESM = Symbol('kImportInRequiredESM'); const kRequireInImportedCJS = Symbol('kRequireInImportedCJS'); @@ -331,11 +388,13 @@ const kRequireInImportedCJS = Symbol('kRequireInImportedCJS'); const requestTypes = { kImportInImportedESM, kImportInRequiredESM, kRequireInImportedCJS }; module.exports = { + embedder_module_hdo, registerModule, initializeESM, getDefaultConditions, getConditionsSet, shouldSpawnLoaderHookWorker, compileSourceTextModule, + SourceTextModuleTypes, requestTypes, }; diff --git a/lib/internal/modules/helpers.js b/lib/internal/modules/helpers.js index b04ac126cd35b9..01739fefd6a7f1 100644 --- a/lib/internal/modules/helpers.js +++ b/lib/internal/modules/helpers.js @@ -15,6 +15,7 @@ const { const { ERR_INVALID_ARG_TYPE, ERR_INVALID_RETURN_PROPERTY_VALUE, + ERR_UNKNOWN_BUILTIN_MODULE, } = require('internal/errors').codes; const { BuiltinModule } = require('internal/bootstrap/realm'); @@ -28,6 +29,7 @@ const assert = require('internal/assert'); const { getOptionValue } = require('internal/options'); const { setOwnProperty, getLazy } = require('internal/util'); const { inspect } = require('internal/util/inspect'); +const { emitWarningSync } = require('internal/process/warning'); const lazyTmpdir = getLazy(() => require('os').tmpdir()); const { join } = path; @@ -126,6 +128,42 @@ function loadBuiltinModule(id) { return mod; } +let isSEABuiltinWarningNeeded_; +function isSEABuiltinWarningNeeded() { + if (isSEABuiltinWarningNeeded_ === undefined) { + const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea'); + isSEABuiltinWarningNeeded_ = isSea() && isExperimentalSeaWarningNeeded(); + } + return isSEABuiltinWarningNeeded_; +} + +let warnedAboutBuiltins = false; +/** + * Helpers to load built-in modules for embedder modules. + * @param {string} id + * @returns {import('internal/bootstrap/realm.js').BuiltinModule} + */ +function loadBuiltinModuleForEmbedder(id) { + const normalized = BuiltinModule.normalizeRequirableId(id); + if (normalized) { + const mod = loadBuiltinModule(normalized); + if (mod) { + return mod; + } + } + if (isSEABuiltinWarningNeeded() && !warnedAboutBuiltins) { + emitWarningSync( + 'Currently the require() provided to the main script embedded into ' + + 'single-executable applications only supports loading built-in modules.\n' + + 'To load a module from disk after the single executable application is ' + + 'launched, use require("module").createRequire().\n' + + 'Support for bundled module loading or virtual file systems are under ' + + 'discussions in https://github.com/nodejs/single-executable'); + warnedAboutBuiltins = true; + } + throw new ERR_UNKNOWN_BUILTIN_MODULE(id); +} + /** @type {Module} */ let $Module = null; /** @@ -469,6 +507,7 @@ module.exports = { getCjsConditionsArray, getCompileCacheDir, initializeCjsConditions, + loadBuiltinModuleForEmbedder, loadBuiltinModule, makeRequireFunction, normalizeReferrerURL, diff --git a/src/env_properties.h b/src/env_properties.h index 903158ebbdc2b7..ea65dd91f13322 100644 --- a/src/env_properties.h +++ b/src/env_properties.h @@ -58,6 +58,7 @@ V(resource_symbol, "resource_symbol") \ V(trigger_async_id_symbol, "trigger_async_id_symbol") \ V(builtin_source_text_module_hdo, "builtin_source_text_module_hdo") \ + V(embedder_module_hdo, "embedder_module_hdo") \ V(source_text_module_default_hdo, "source_text_module_default_hdo") \ V(vm_context_no_contextify, "vm_context_no_contextify") \ V(vm_dynamic_import_default_internal, "vm_dynamic_import_default_internal") \ diff --git a/src/module_wrap.cc b/src/module_wrap.cc index d9711500f178c7..354b45bda9ccc7 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -1397,7 +1397,8 @@ void ModuleWrap::HostInitializeImportMetaObjectCallback( // Use the default initializer for source text modules without custom // callbacks. - if (id == env->source_text_module_default_hdo()) { + if (id == env->source_text_module_default_hdo() || + id == env->embedder_module_hdo()) { USE(DefaultImportMetaObjectInitializer(realm, wrap, meta)); return; } diff --git a/src/node_contextify.cc b/src/node_contextify.cc index c6a34f1ae5a19b..cf5d9ad2255dca 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1644,13 +1644,13 @@ static MaybeLocal CompileFunctionForCJSLoader( Local filename, bool* cache_rejected, bool is_cjs_scope, - ScriptCompiler::CachedData* cached_data) { + ScriptCompiler::CachedData* cached_data, + Local host_defined_option_symbol) { Isolate* isolate = Isolate::GetCurrent(); EscapableHandleScope scope(isolate); - Local symbol = env->vm_dynamic_import_default_internal(); - Local hdo = - loader::ModuleWrap::GetHostDefinedOptions(isolate, symbol); + Local hdo = loader::ModuleWrap::GetHostDefinedOptions( + isolate, host_defined_option_symbol); ScriptOrigin origin(filename, 0, // line offset 0, // column offset @@ -1743,6 +1743,12 @@ static void CompileFunctionForCJSLoader( Realm* realm = Realm::GetCurrent(context); Environment* env = realm->env(); + Local host_defined_option_symbol = + env->vm_dynamic_import_default_internal(); + if (args.Length() > 4 && args[4].As()->Value()) { + host_defined_option_symbol = env->embedder_module_hdo(); + } + bool cache_rejected = false; Local fn; Local cjs_exception; @@ -1771,8 +1777,14 @@ static void CompileFunctionForCJSLoader( { ShouldNotAbortOnUncaughtScope no_abort_scope(realm->env()); TryCatchScope try_catch(env); - if (!CompileFunctionForCJSLoader( - env, context, code, filename, &cache_rejected, true, cached_data) + if (!CompileFunctionForCJSLoader(env, + context, + code, + filename, + &cache_rejected, + true, + cached_data, + host_defined_option_symbol) .ToLocal(&fn)) { CHECK(try_catch.HasCaught()); CHECK(!try_catch.HasTerminated()); @@ -1933,8 +1945,14 @@ static void ContainsModuleSyntax(const FunctionCallbackInfo& args) { Local fn; TryCatchScope try_catch(env); ShouldNotAbortOnUncaughtScope no_abort_scope(env); - if (CompileFunctionForCJSLoader( - env, context, code, filename, &cache_rejected, cjs_var, nullptr) + if (CompileFunctionForCJSLoader(env, + context, + code, + filename, + &cache_rejected, + cjs_var, + nullptr, + env->vm_dynamic_import_default_internal()) .ToLocal(&fn)) { args.GetReturnValue().Set(false); return; diff --git a/test/cctest/test_environment.cc b/test/cctest/test_environment.cc index 3e8e4a65a81bdd..19a31e9642d14e 100644 --- a/test/cctest/test_environment.cc +++ b/test/cctest/test_environment.cc @@ -1,6 +1,7 @@ +#include "libplatform/libplatform.h" #include "node_buffer.h" #include "node_internals.h" -#include "libplatform/libplatform.h" +#include "node_url.h" #include "util.h" #include @@ -849,6 +850,146 @@ TEST_F(EnvironmentTest, LoadEnvironmentWithESModule) { EXPECT_EQ(frame_str.ToString(), " at embedded:esm.mjs:3:15"); } +TEST_F(EnvironmentTest, LoadEnvironmentWithESModuleDynamicImport) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env env{handle_scope, argv}; + + node::ModuleData entry_point; + // Test dynamic import('node:process') in ESM entry point. + std::string source = + "export const importedPromise = import('node:process');\n"; + entry_point.set_source(source); + entry_point.set_format(node::ModuleFormat::kModule); + entry_point.set_resource_name("embedded:dynamic-import.mjs"); + + v8::Local result = + node::LoadEnvironment(*env, &entry_point).ToLocalChecked(); + + // The ESM entry point returns the module namespace object. + EXPECT_TRUE(result->IsObject()); + v8::Local imported_promise = + result.As() + ->Get(isolate_->GetCurrentContext(), + v8::String::NewFromUtf8Literal(isolate_, "importedPromise")) + .ToLocalChecked(); + EXPECT_TRUE(imported_promise->IsPromise()); + + v8::Local context = isolate_->GetCurrentContext(); + // Finish the await. + context->GetMicrotaskQueue()->PerformCheckpoint(isolate_); + + v8::Local imported_value = + imported_promise.As()->Result(); + EXPECT_TRUE(imported_value->IsObject()); + v8::Local dynamic_process = + imported_value.As() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "default")) + .ToLocalChecked(); + EXPECT_TRUE(dynamic_process->IsObject()); + + v8::Local global_process_value = + context->Global() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "process")) + .ToLocalChecked(); + EXPECT_TRUE(global_process_value->IsObject()); + EXPECT_TRUE(dynamic_process->StrictEquals(global_process_value)); +} + +TEST_F(EnvironmentTest, LoadEnvironmentWithESModuleImportMeta) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env env{handle_scope, argv}; + + node::ModuleData entry_point; + // Test import.meta properties in ESM entry point. + std::string source = "export const url = import.meta.url;\n" + "export const main = import.meta.main;\n" + "export const filename = import.meta.filename;\n" + "export const dirname = import.meta.dirname;\n"; + std::string exec_path = (*env)->exec_path(); + std::string url = node::url::FromFilePath(exec_path); + entry_point.set_source(source); + entry_point.set_format(node::ModuleFormat::kModule); + entry_point.set_resource_name(url); + + v8::Local result = + node::LoadEnvironment(*env, &entry_point).ToLocalChecked(); + + EXPECT_TRUE(result->IsObject()); + v8::Local context = isolate_->GetCurrentContext(); + + // Check import.meta.url + v8::Local url_value = + result.As() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "url")) + .ToLocalChecked(); + EXPECT_TRUE(url_value->IsString()); + node::Utf8Value url_str(isolate_, url_value); + EXPECT_EQ(url_str.ToStringView(), url); + + // Check import.meta.main + v8::Local main_value = + result.As() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "main")) + .ToLocalChecked(); + EXPECT_TRUE(main_value->IsBoolean()); + EXPECT_TRUE(main_value->BooleanValue(isolate_)); + + // Check import.meta.filename + v8::Local filename_value = + result.As() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "filename")) + .ToLocalChecked(); + EXPECT_TRUE(filename_value->IsString()); + node::Utf8Value filename_str(isolate_, filename_value); + EXPECT_EQ(filename_str.ToStringView(), exec_path); + + // Check import.meta.dirname + v8::Local dirname_value = + result.As() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "dirname")) + .ToLocalChecked(); + EXPECT_TRUE(dirname_value->IsString()); + node::Utf8Value dirname_str(isolate_, dirname_value); + // Just check that dirname is a substring of exec_path + EXPECT_NE(exec_path.find(dirname_str.ToStringView()), std::string::npos); +} + +TEST_F(EnvironmentTest, LoadEnvironmentWithCommonJSDynamicImport) { + const v8::HandleScope handle_scope(isolate_); + const Argv argv; + Env env{handle_scope, argv}; + + node::ModuleData entry_point; + // Test dynamic import('node:process') in CJS entry point. + std::string source = "return import('node:process');\n"; + entry_point.set_source(source); + entry_point.set_format(node::ModuleFormat::kCommonJS); + entry_point.set_resource_name("/test-cjs-dynamic-import.js"); + + v8::Local result = + node::LoadEnvironment(*env, &entry_point).ToLocalChecked(); + EXPECT_TRUE(result->IsPromise()); + v8::Local context = isolate_->GetCurrentContext(); + // Finish the await. + context->GetMicrotaskQueue()->PerformCheckpoint(isolate_); + v8::Local imported_value = result.As()->Result(); + EXPECT_TRUE(imported_value->IsObject()); + v8::Local dynamic_process = + imported_value.As() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "default")) + .ToLocalChecked(); + EXPECT_TRUE(dynamic_process->IsObject()); + + v8::Local global_process_value = + context->Global() + ->Get(context, v8::String::NewFromUtf8Literal(isolate_, "process")) + .ToLocalChecked(); + EXPECT_TRUE(global_process_value->IsObject()); + EXPECT_TRUE(dynamic_process->StrictEquals(global_process_value)); +} + TEST_F(EnvironmentTest, LoadEnvironmentWithCallbackWithCommonJSModule) { const v8::HandleScope handle_scope(isolate_); const Argv argv;