| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9a68b47 commit 0211a3d
15 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -294,7 +294,6 @@ require('url'); // eslint-disable-line no-restricted-modules | |||
| 294 | 294 | internalBinding('module_wrap'); | |
| 295 | 295 | require('internal/modules/cjs/loader'); | |
| 296 | 296 | require('internal/modules/esm/utils'); | |
| 297 | - require('internal/vm/module'); | ||
| 298 | 297 | ||
| 299 | 298 | // Needed to refresh the time origin. | |
| 300 | 299 | require('internal/perf/utils'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,7 +52,6 @@ const { | |||
| 52 | 52 | SafeMap, | |
| 53 | 53 | SafeWeakMap, | |
| 54 | 54 | String, | |
| 55 | - Symbol, | ||
| 56 | 55 | StringPrototypeCharAt, | |
| 57 | 56 | StringPrototypeCharCodeAt, | |
| 58 | 57 | StringPrototypeEndsWith, | |
@@ -114,7 +113,6 @@ const { | |||
| 114 | 113 | initializeCjsConditions, | |
| 115 | 114 | loadBuiltinModule, | |
| 116 | 115 | makeRequireFunction, | |
| 117 | - normalizeReferrerURL, | ||
| 118 | 116 | stripBOM, | |
| 119 | 117 | toRealPath, | |
| 120 | 118 | } = require('internal/modules/helpers'); | |
@@ -125,12 +123,10 @@ const policy = getLazy( | |||
| 125 | 123 | ); | |
| 126 | 124 | const shouldReportRequiredModules = getLazy(() => process.env.WATCH_REPORT_DEPENDENCIES); | |
| 127 | 125 | ||
| 128 | - const getCascadedLoader = getLazy( | ||
| 129 | - () => require('internal/process/esm_loader').esmLoader, | ||
| 130 | - ); | ||
| 131 | - | ||
| 132 | 126 | const permission = require('internal/process/permission'); | |
| 133 | - | ||
| 127 | + const { | ||
| 128 | + vm_dynamic_import_default_internal, | ||
| 129 | + } = internalBinding('symbols'); | ||
| 134 | 130 | // Whether any user-provided CJS modules had been loaded (executed). | |
| 135 | 131 | // Used for internal assertions. | |
| 136 | 132 | let hasLoadedAnyUserCJSModule = false; | |
@@ -1258,12 +1254,8 @@ let hasPausedEntry = false; | |||
| 1258 | 1254 | * @param {object} codeCache The SEA code cache | |
| 1259 | 1255 | */ | |
| 1260 | 1256 | function wrapSafe(filename, content, cjsModuleInstance, codeCache) { | |
| 1261 | - const hostDefinedOptionId = Symbol(`cjs:${filename}`); | ||
| 1262 | - async function importModuleDynamically(specifier, _, importAttributes) { | ||
| 1263 | - const cascadedLoader = getCascadedLoader(); | ||
| 1264 | - return cascadedLoader.import(specifier, normalizeReferrerURL(filename), | ||
| 1265 | - importAttributes); | ||
| 1266 | - } | ||
| 1257 | + const hostDefinedOptionId = vm_dynamic_import_default_internal; | ||
| 1258 | + const importModuleDynamically = vm_dynamic_import_default_internal; | ||
| 1267 | 1259 | if (patched) { | |
| 1268 | 1260 | const wrapped = Module.wrap(content); | |
| 1269 | 1261 | const script = makeContextifyScript( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,7 +15,6 @@ const { | |||
| 15 | 15 | StringPrototypeReplaceAll, | |
| 16 | 16 | StringPrototypeSlice, | |
| 17 | 17 | StringPrototypeStartsWith, | |
| 18 | - Symbol, | ||
| 19 | 18 | SyntaxErrorPrototype, | |
| 20 | 19 | globalThis: { WebAssembly }, | |
| 21 | 20 | } = primordials; | |
@@ -59,7 +58,9 @@ const { ModuleWrap } = moduleWrap; | |||
| 59 | 58 | const asyncESM = require('internal/process/esm_loader'); | |
| 60 | 59 | const { emitWarningSync } = require('internal/process/warning'); | |
| 61 | 60 | const { internalCompileFunction } = require('internal/vm'); | |
| 62 | - | ||
| 61 | + const { | ||
| 62 | + vm_dynamic_import_default_internal, | ||
| 63 | + } = internalBinding('symbols'); | ||
| 63 | 64 | // Lazy-loading to avoid circular dependencies. | |
| 64 | 65 | let getSourceSync; | |
| 65 | 66 | /** | |
@@ -206,9 +207,8 @@ function enrichCJSError(err, content, filename) { | |||
| 206 | 207 | */ | |
| 207 | 208 | function loadCJSModule(module, source, url, filename) { | |
| 208 | 209 | let compiledWrapper; | |
| 209 | - async function importModuleDynamically(specifier, _, importAttributes) { | ||
| 210 | - return asyncESM.esmLoader.import(specifier, url, importAttributes); | ||
| 211 | - } | ||
| 210 | + const hostDefinedOptionId = vm_dynamic_import_default_internal; | ||
| 211 | + const importModuleDynamically = vm_dynamic_import_default_internal; | ||
| 212 | 212 | try { | |
| 213 | 213 | compiledWrapper = internalCompileFunction( | |
| 214 | 214 | source, // code, | |
@@ -226,8 +226,8 @@ function loadCJSModule(module, source, url, filename) { | |||
| 226 | 226 | '__filename', | |
| 227 | 227 | '__dirname', | |
| 228 | 228 | ], | |
| 229 | - Symbol(`cjs:${filename}`), // hostDefinedOptionsId | ||
| 230 | - importModuleDynamically, // importModuleDynamically | ||
| 229 | + hostDefinedOptionId, // hostDefinedOptionsId | ||
| 230 | + importModuleDynamically, // importModuleDynamically | ||
| 231 | 231 | ).function; | |
| 232 | 232 | } catch (err) { | |
| 233 | 233 | enrichCJSError(err, source, filename); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,6 @@ const { | |||
| 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, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,16 +23,19 @@ const { validateString } = require('internal/validators'); | |||
| 23 | 23 | const fs = require('fs'); // Import all of `fs` so that it can be monkey-patched. | |
| 24 | 24 | const internalFS = require('internal/fs/utils'); | |
| 25 | 25 | const path = require('path'); | |
| 26 | - const { pathToFileURL, fileURLToPath, URL } = require('internal/url'); | ||
| 26 | + const { pathToFileURL, fileURLToPath } = require('internal/url'); | ||
| 27 | + const assert = require('internal/assert'); | ||
| 27 | 28 | ||
| 28 | 29 | const { getOptionValue } = require('internal/options'); | |
| 29 | 30 | const { setOwnProperty } = require('internal/util'); | |
| 31 | + const { inspect } = require('internal/util/inspect'); | ||
| 30 | 32 | ||
| 31 | 33 | const { | |
| 32 | 34 | privateSymbols: { | |
| 33 | 35 | require_private_symbol, | |
| 34 | 36 | }, | |
| 35 | 37 | } = internalBinding('util'); | |
| 38 | + const { canParse: URLCanParse } = internalBinding('url'); | ||
| 36 | 39 | ||
| 37 | 40 | let debug = require('internal/util/debuglog').debuglog('module', (fn) => { | |
| 38 | 41 | debug = fn; | |
@@ -288,14 +291,32 @@ function addBuiltinLibsToObject(object, dummyModuleName) { | |||
| 288 | 291 | } | |
| 289 | 292 | ||
| 290 | 293 | /** | |
| 291 | - * If a referrer is an URL instance or absolute path, convert it into an URL string. | ||
| 292 | - * @param {string | URL} referrer | ||
| 294 | + * Normalize the referrer name as a URL. | ||
| 295 | + * If it's a string containing an absolute path or a URL it's normalized as | ||
| 296 | + * a URL string. | ||
| 297 | + * Otherwise it's returned as undefined. | ||
| 298 | + * @param {string | null | undefined} referrerName | ||
| 299 | + * @returns {string | undefined} | ||
| 293 | 300 | */ | |
| 294 | - function normalizeReferrerURL(referrer) { | ||
| 295 | - if (typeof referrer === 'string' && path.isAbsolute(referrer)) { | ||
| 296 | - return pathToFileURL(referrer).href; | ||
| 301 | + function normalizeReferrerURL(referrerName) { | ||
| 302 | + if (referrerName === null || referrerName === undefined) { | ||
| 303 | + return undefined; | ||
| 297 | 304 | } | |
| 298 | - return new URL(referrer).href; | ||
| 305 | + | ||
| 306 | + if (typeof referrerName === 'string') { | ||
| 307 | + if (path.isAbsolute(referrerName)) { | ||
| 308 | + return pathToFileURL(referrerName).href; | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + if (StringPrototypeStartsWith(referrerName, 'file://') || | ||
| 312 | + URLCanParse(referrerName)) { | ||
| 313 | + return referrerName; | ||
| 314 | + } | ||
| 315 | + | ||
| 316 | + return undefined; | ||
| 317 | + } | ||
| 318 | + | ||
| 319 | + assert.fail('Unreachable code reached by ' + inspect(referrerName)); | ||
| 299 | 320 | } | |
| 300 | 321 | ||
| 301 | 322 | module.exports = { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -67,23 +67,31 @@ function prepareWorkerThreadExecution() { | |||
| 67 | 67 | } | |
| 68 | 68 | ||
| 69 | 69 | function prepareShadowRealmExecution() { | |
| 70 | - const { registerRealm } = require('internal/modules/esm/utils'); | ||
| 71 | 70 | // Patch the process object with legacy properties and normalizations. | |
| 72 | 71 | // Do not expand argv1 as it is not available in ShadowRealm. | |
| 73 | 72 | patchProcessObject(false); | |
| 74 | 73 | setupDebugEnv(); | |
| 75 | 74 | ||
| 76 | 75 | // Disable custom loaders in ShadowRealm. | |
| 77 | 76 | setupUserModules(true); | |
| 78 | - registerRealm(globalThis, { | ||
| 79 | - __proto__: null, | ||
| 80 | - importModuleDynamically: (specifier, _referrer, attributes) => { | ||
| 81 | - // The handler for `ShadowRealm.prototype.importValue`. | ||
| 82 | - const { esmLoader } = require('internal/process/esm_loader'); | ||
| 83 | - // `parentURL` is not set in the case of a ShadowRealm top-level import. | ||
| 84 | - return esmLoader.import(specifier, undefined, attributes); | ||
| 77 | + const { | ||
| 78 | + privateSymbols: { | ||
| 79 | + host_defined_option_symbol, | ||
| 85 | 80 | }, | |
| 86 | - }); | ||
| 81 | + } = internalBinding('util'); | ||
| 82 | + const { | ||
| 83 | + vm_dynamic_import_default_internal, | ||
| 84 | + } = internalBinding('symbols'); | ||
| 85 | + | ||
| 86 | + // For ShadowRealm.prototype.importValue(), the referrer name is | ||
| 87 | + // always null, so the native ImportModuleDynamically() callback would | ||
| 88 | + // always fallback to look up the host-defined option from the | ||
| 89 | + // global object using host_defined_option_symbol. Using | ||
| 90 | + // vm_dynamic_import_default_internal as the host-defined option | ||
| 91 | + // instructs the JS-land importModuleDynamicallyCallback() to | ||
| 92 | + // proxy the request to defaultImportModuleDynamically(). | ||
| 93 | + globalThis[host_defined_option_symbol] = | ||
| 94 | + vm_dynamic_import_default_internal; | ||
| 87 | 95 | } | |
| 88 | 96 | ||
| 89 | 97 | function prepareExecution(options) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -107,12 +107,10 @@ function extractSourceMapURLMagicComment(content) { | |||
| 107 | 107 | function maybeCacheSourceMap(filename, content, cjsModuleInstance, isGeneratedSource, sourceURL, sourceMapURL) { | |
| 108 | 108 | const sourceMapsEnabled = getSourceMapsEnabled(); | |
| 109 | 109 | if (!(process.env.NODE_V8_COVERAGE || sourceMapsEnabled)) return; | |
| 110 | - try { | ||
| 111 | - const { normalizeReferrerURL } = require('internal/modules/helpers'); | ||
| 112 | - filename = normalizeReferrerURL(filename); | ||
| 113 | - } catch (err) { | ||
| 110 | + const { normalizeReferrerURL } = require('internal/modules/helpers'); | ||
| 111 | + filename = normalizeReferrerURL(filename); | ||
| 112 | + if (filename === undefined) { | ||
| 114 | 113 | // This is most likely an invalid filename in sourceURL of [eval]-wrapper. | |
| 115 | - debug(err); | ||
| 116 | 114 | return; | |
| 117 | 115 | } | |
| 118 | 116 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments