44 ArrayIsArray,
55 SafeSet,
66 SafeWeakMap,
7- Symbol,
87 ObjectFreeze,
98} = primordials ;
109
@@ -14,8 +13,10 @@ const {
1413 } ,
1514} = internalBinding ( 'util' ) ;
1615const {
17- default_host_defined_options,
16+ vm_dynamic_import_default_internal,
17+ vm_dynamic_import_main_context_default,
1818 vm_dynamic_import_missing_flag,
19+ vm_dynamic_import_no_callback,
1920} = internalBinding ( 'symbols' ) ;
2021
2122const {
@@ -28,12 +29,19 @@ const {
2829 loadPreloadModules,
2930 initializeFrozenIntrinsics,
3031} = require ( 'internal/process/pre_execution' ) ;
31- const { getCWDURL } = require ( 'internal/util' ) ;
32+ const {
33+ emitExperimentalWarning,
34+ getCWDURL,
35+ getLazy,
36+ } = require ( 'internal/util' ) ;
3237const {
3338 setImportModuleDynamicallyCallback,
3439 setInitializeImportMetaObjectCallback,
3540} = internalBinding ( 'module_wrap' ) ;
3641const assert = require ( 'internal/assert' ) ;
42+ const {
43+ normalizeReferrerURL,
44+ } = require ( 'internal/modules/helpers' ) ;
3745
3846let defaultConditions ;
3947/**
@@ -145,8 +153,10 @@ const moduleRegistries = new SafeWeakMap();
145153 */
146154function registerModule ( referrer , registry ) {
147155 const idSymbol = referrer [ host_defined_option_symbol ] ;
148- if ( idSymbol === default_host_defined_options ||
149- idSymbol === vm_dynamic_import_missing_flag ) {
156+ if ( idSymbol === vm_dynamic_import_no_callback ||
157+ idSymbol === vm_dynamic_import_missing_flag ||
158+ idSymbol === vm_dynamic_import_main_context_default ||
159+ idSymbol === vm_dynamic_import_default_internal ) {
150160 // The referrer is compiled without custom callbacks, so there is
151161 // no registry to hold on to. We'll throw
152162 // ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING when a callback is
@@ -158,26 +168,6 @@ function registerModule(referrer, registry) {
158168 moduleRegistries . set ( idSymbol , registry ) ;
159169}
160170
161- /**
162- * Registers the ModuleRegistry for dynamic import() calls with a realm
163- * as the referrer. Similar to {@link registerModule}, but this function
164- * generates a new id symbol instead of using the one from the referrer
165- * object.
166- * @param {globalThis } globalThis The globalThis object of the realm.
167- * @param {ModuleRegistry } registry
168- */
169- function registerRealm ( globalThis , registry ) {
170- let idSymbol = globalThis [ host_defined_option_symbol ] ;
171- // If the per-realm host-defined options is already registered, do nothing.
172- if ( idSymbol ) {
173- return ;
174- }
175- // Otherwise, register the per-realm host-defined options.
176- idSymbol = Symbol ( 'Realm globalThis' ) ;
177- globalThis [ host_defined_option_symbol ] = idSymbol ;
178- moduleRegistries . set ( idSymbol , registry ) ;
179- }
180-
181171/**
182172 * Defines the `import.meta` object for a given module.
183173 * @param {symbol } symbol - Reference to the module.
@@ -191,16 +181,44 @@ function initializeImportMetaObject(symbol, meta) {
191181 }
192182 }
193183}
184+ const getCascadedLoader = getLazy (
185+ ( ) => require ( 'internal/process/esm_loader' ) . esmLoader ,
186+ ) ;
187+
188+ /**
189+ * Proxy the dynamic import to the default loader.
190+ * @param {string } specifier - The module specifier string.
191+ * @param {Record<string, string> } attributes - The import attributes object.
192+ * @param {string|null|undefined } referrerName - name of the referrer.
193+ * @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports> } - The imported module object.
194+ */
195+ function defaultImportModuleDynamically ( specifier , attributes , referrerName ) {
196+ const parentURL = normalizeReferrerURL ( referrerName ) ;
197+ return getCascadedLoader ( ) . import ( specifier , parentURL , attributes ) ;
198+ }
194199
195200/**
196201 * Asynchronously imports a module dynamically using a callback function. The native callback.
197202 * @param {symbol } referrerSymbol - Referrer symbol of the registered script, function, module, or contextified object.
198203 * @param {string } specifier - The module specifier string.
199204 * @param {Record<string, string> } attributes - The import attributes object.
205+ * @param {string|null|undefined } referrerName - name of the referrer.
200206 * @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports> } - The imported module object.
201207 * @throws {ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING } - If the callback function is missing.
202208 */
203- async function importModuleDynamicallyCallback ( referrerSymbol , specifier , attributes ) {
209+ async function importModuleDynamicallyCallback ( referrerSymbol , specifier , attributes , referrerName ) {
210+ // For user-provided vm.constants.USE_MAIN_CONTEXT_DEFAULT_LOADER, emit the warning
211+ // and fall back to the default loader.
212+ if ( referrerSymbol === vm_dynamic_import_main_context_default ) {
213+ emitExperimentalWarning ( 'vm.USE_MAIN_CONTEXT_DEFAULT_LOADER' ) ;
214+ return defaultImportModuleDynamically ( specifier , attributes , referrerName ) ;
215+ }
216+ // For script compiled internally that should use the default loader to handle dynamic
217+ // import, proxy the request to the default loader without the warning.
218+ if ( referrerSymbol === vm_dynamic_import_default_internal ) {
219+ return defaultImportModuleDynamically ( specifier , attributes , referrerName ) ;
220+ }
221+
204222 if ( moduleRegistries . has ( referrerSymbol ) ) {
205223 const { importModuleDynamically, callbackReferrer } = moduleRegistries . get ( referrerSymbol ) ;
206224 if ( importModuleDynamically !== undefined ) {
@@ -275,7 +293,6 @@ async function initializeHooks() {
275293
276294module . exports = {
277295 registerModule,
278- registerRealm,
279296 initializeESM,
280297 initializeHooks,
281298 getDefaultConditions,
0 commit comments