4
4
ArrayIsArray,
5
5
SafeSet,
6
6
SafeWeakMap,
7
- Symbol,
8
7
ObjectFreeze,
9
8
} = primordials ;
10
9
@@ -14,8 +13,10 @@ const {
14
13
} ,
15
14
} = internalBinding ( 'util' ) ;
16
15
const {
17
- default_host_defined_options,
16
+ vm_dynamic_import_default_internal,
17
+ vm_dynamic_import_main_context_default,
18
18
vm_dynamic_import_missing_flag,
19
+ vm_dynamic_import_no_callback,
19
20
} = internalBinding ( 'symbols' ) ;
20
21
21
22
const {
@@ -28,12 +29,19 @@ const {
28
29
loadPreloadModules,
29
30
initializeFrozenIntrinsics,
30
31
} = require ( 'internal/process/pre_execution' ) ;
31
- const { getCWDURL } = require ( 'internal/util' ) ;
32
+ const {
33
+ emitExperimentalWarning,
34
+ getCWDURL,
35
+ getLazy,
36
+ } = require ( 'internal/util' ) ;
32
37
const {
33
38
setImportModuleDynamicallyCallback,
34
39
setInitializeImportMetaObjectCallback,
35
40
} = internalBinding ( 'module_wrap' ) ;
36
41
const assert = require ( 'internal/assert' ) ;
42
+ const {
43
+ normalizeReferrerURL,
44
+ } = require ( 'internal/modules/helpers' ) ;
37
45
38
46
let defaultConditions ;
39
47
/**
@@ -145,8 +153,10 @@ const moduleRegistries = new SafeWeakMap();
145
153
*/
146
154
function registerModule ( referrer , registry ) {
147
155
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 ) {
150
160
// The referrer is compiled without custom callbacks, so there is
151
161
// no registry to hold on to. We'll throw
152
162
// ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING when a callback is
@@ -158,26 +168,6 @@ function registerModule(referrer, registry) {
158
168
moduleRegistries . set ( idSymbol , registry ) ;
159
169
}
160
170
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
-
181
171
/**
182
172
* Defines the `import.meta` object for a given module.
183
173
* @param {symbol } symbol - Reference to the module.
@@ -191,16 +181,44 @@ function initializeImportMetaObject(symbol, meta) {
191
181
}
192
182
}
193
183
}
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
+ }
194
199
195
200
/**
196
201
* Asynchronously imports a module dynamically using a callback function. The native callback.
197
202
* @param {symbol } referrerSymbol - Referrer symbol of the registered script, function, module, or contextified object.
198
203
* @param {string } specifier - The module specifier string.
199
204
* @param {Record<string, string> } attributes - The import attributes object.
205
+ * @param {string|null|undefined } referrerName - name of the referrer.
200
206
* @returns {Promise<import('internal/modules/esm/loader.js').ModuleExports> } - The imported module object.
201
207
* @throws {ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING } - If the callback function is missing.
202
208
*/
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
+
204
222
if ( moduleRegistries . has ( referrerSymbol ) ) {
205
223
const { importModuleDynamically, callbackReferrer } = moduleRegistries . get ( referrerSymbol ) ;
206
224
if ( importModuleDynamically !== undefined ) {
@@ -275,7 +293,6 @@ async function initializeHooks() {
275
293
276
294
module . exports = {
277
295
registerModule,
278
- registerRealm,
279
296
initializeESM,
280
297
initializeHooks,
281
298
getDefaultConditions,
0 commit comments