GeezerVison is showing github.com — src: support import() and import.meta in embedder-run modules · nodejs/node@fb2db5f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/embedding.js +8-53Lines changed: 8 additions & 53 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,16 +15,12 @@ const { | 15 | 15 | const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea'); | 16 | 16 | const { emitExperimentalWarning } = require('internal/util'); | 17 | 17 | const { emitWarningSync } = require('internal/process/warning'); | 18 | | -const { BuiltinModule } = require('internal/bootstrap/realm'); | 19 | | -const { normalizeRequirableId } = BuiltinModule; | 20 | 18 | const { Module } = require('internal/modules/cjs/loader'); | 21 | 19 | const { compileFunctionForCJSLoader } = internalBinding('contextify'); | 22 | 20 | const { maybeCacheSourceMap } = require('internal/source_map/source_map_cache'); | 23 | | -const { codes: { | 24 | | - ERR_UNKNOWN_BUILTIN_MODULE, | 25 | | -} } = require('internal/errors'); | 26 | 21 | const { pathToFileURL } = require('internal/url'); | 27 | | -const { loadBuiltinModule } = require('internal/modules/helpers'); | | 22 | +const { loadBuiltinModuleForEmbedder } = require('internal/modules/helpers'); | | 23 | +const { compileSourceTextModule, SourceTextModuleTypes: { kEmbedder } } = require('internal/modules/esm/utils'); | 28 | 24 | const { moduleFormats } = internalBinding('modules'); | 29 | 25 | const assert = require('internal/assert'); | 30 | 26 | const path = require('path'); | @@ -34,7 +30,6 @@ const path = require('path'); | 34 | 30 | prepareMainThreadExecution(false, true); | 35 | 31 |
| 36 | 32 | const isLoadingSea = isSea(); | 37 | | -const isBuiltinWarningNeeded = isLoadingSea && isExperimentalSeaWarningNeeded(); | 38 | 33 | if (isExperimentalSeaWarningNeeded()) { | 39 | 34 | emitExperimentalWarning('Single executable application'); | 40 | 35 | } | @@ -65,6 +60,7 @@ function embedderRunCjs(content, filename) { | 65 | 60 | filename, | 66 | 61 | isLoadingSea, // is_sea_main | 67 | 62 | false, // should_detect_module, ESM should be supported differently for embedded code | | 63 | + true, // is_embedder | 68 | 64 | ); | 69 | 65 | // Cache the source map for the module if present. | 70 | 66 | if (sourceMapURL) { | @@ -103,28 +99,8 @@ function embedderRunCjs(content, filename) { | 103 | 99 | ); | 104 | 100 | } | 105 | 101 |
| 106 | | -let warnedAboutBuiltins = false; | 107 | | -function warnNonBuiltinInSEA() { | 108 | | - if (isBuiltinWarningNeeded && !warnedAboutBuiltins) { | 109 | | - emitWarningSync( | 110 | | - 'Currently the require() provided to the main script embedded into ' + | 111 | | - 'single-executable applications only supports loading built-in modules.\n' + | 112 | | - 'To load a module from disk after the single executable application is ' + | 113 | | - 'launched, use require("module").createRequire().\n' + | 114 | | - 'Support for bundled module loading or virtual file systems are under ' + | 115 | | - 'discussions in https://github.com/nodejs/single-executable'); | 116 | | - warnedAboutBuiltins = true; | 117 | | - } | 118 | | -} | 119 | | - | 120 | 102 | function embedderRequire(id) { | 121 | | - const normalizedId = normalizeRequirableId(id); | 122 | | - | 123 | | - if (!normalizedId) { | 124 | | - warnNonBuiltinInSEA(); | 125 | | - throw new ERR_UNKNOWN_BUILTIN_MODULE(id); | 126 | | - } | 127 | | - return require(normalizedId); | | 103 | + return loadBuiltinModuleForEmbedder(id).exports; | 128 | 104 | } | 129 | 105 |
| 130 | 106 | function embedderRunESM(content, filename) { | @@ -134,31 +110,10 @@ function embedderRunESM(content, filename) { | 134 | 110 | } else { | 135 | 111 | resourceName = filename; | 136 | 112 | } | 137 | | - const { compileSourceTextModule } = require('internal/modules/esm/utils'); | 138 | | - // TODO(joyeecheung): support code cache, dynamic import() and import.meta. | 139 | | - const wrap = compileSourceTextModule(resourceName, content); | 140 | | - // Cache the source map for the module if present. | 141 | | - if (wrap.sourceMapURL) { | 142 | | - maybeCacheSourceMap(resourceName, content, wrap, false, undefined, wrap.sourceMapURL); | 143 | | - } | 144 | | - const requests = wrap.getModuleRequests(); | 145 | | - const modules = []; | 146 | | - for (let i = 0; i < requests.length; ++i) { | 147 | | - const { specifier } = requests[i]; | 148 | | - const normalizedId = normalizeRequirableId(specifier); | 149 | | - if (!normalizedId) { | 150 | | - warnNonBuiltinInSEA(); | 151 | | - throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); | 152 | | - } | 153 | | - const mod = loadBuiltinModule(normalizedId); | 154 | | - if (!mod) { | 155 | | - throw new ERR_UNKNOWN_BUILTIN_MODULE(specifier); | 156 | | - } | 157 | | - modules.push(mod.getESMFacade()); | 158 | | - } | 159 | | - wrap.link(modules); | 160 | | - wrap.instantiate(); | 161 | | - wrap.evaluate(-1, false); | | 113 | + // TODO(joyeecheung): allow configuration from node::ModuleData, | | 114 | + // either via a more generic context object, or something like import.meta extensions. | | 115 | + const context = { isMain: true, __proto__: null }; | | 116 | + const wrap = compileSourceTextModule(resourceName, content, kEmbedder, context); | 162 | 117 |
| 163 | 118 | // TODO(joyeecheung): we may want to return the v8::Module via a vm.SourceTextModule | 164 | 119 | // when vm.SourceTextModule stablizes, or put it in an out parameter. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/create_dynamic_module.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,8 +58,10 @@ ${ArrayPrototypeJoin(ArrayPrototypeMap(imports, createImport), '\n')} | 58 | 58 | ${ArrayPrototypeJoin(ArrayPrototypeMap(exports, createExport), '\n')} | 59 | 59 | import.meta.done(); | 60 | 60 | `; | 61 | | - const { registerModule, compileSourceTextModule } = require('internal/modules/esm/utils'); | 62 | | - const m = compileSourceTextModule(`${url}`, source); | | 61 | + const { | | 62 | + registerModule, compileSourceTextModule, SourceTextModuleTypes: { kFacade }, | | 63 | + } = require('internal/modules/esm/utils'); | | 64 | + const m = compileSourceTextModule(`${url}`, source, kFacade); | 63 | 65 |
| 64 | 66 | const readyfns = new SafeSet(); | 65 | 67 | /** @type {DynamicModuleReflect} */ | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,7 @@ const { isURL, pathToFileURL } = require('internal/url'); | 34 | 34 | const { kEmptyObject } = require('internal/util'); | 35 | 35 | const { | 36 | 36 | compileSourceTextModule, | | 37 | + SourceTextModuleTypes: { kUser }, | 37 | 38 | getDefaultConditions, | 38 | 39 | shouldSpawnLoaderHookWorker, | 39 | 40 | requestTypes: { kImportInRequiredESM, kRequireInImportedCJS, kImportInImportedESM }, | @@ -244,7 +245,7 @@ class ModuleLoader { | 244 | 245 | * @returns {object} The module wrap object. | 245 | 246 | */ | 246 | 247 | createModuleWrap(source, url, context = kEmptyObject) { | 247 | | - return compileSourceTextModule(url, source, this, context); | | 248 | + return compileSourceTextModule(url, source, kUser, context); | 248 | 249 | } | 249 | 250 |
| 250 | 251 | /** | @@ -371,7 +372,7 @@ class ModuleLoader { | 371 | 372 | // TODO(joyeecheung): refactor this so that we pre-parse in C++ and hit the | 372 | 373 | // cache here, or use a carrier object to carry the compiled module script | 373 | 374 | // into the constructor to ensure cache hit. | 374 | | - const wrap = compileSourceTextModule(url, source, this); | | 375 | + const wrap = compileSourceTextModule(url, source, kUser); | 375 | 376 | const inspectBrk = (isMain && getOptionValue('--inspect-brk')); | 376 | 377 |
| 377 | 378 | const { ModuleJobSync } = require('internal/modules/esm/module_job'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -93,9 +93,11 @@ translators.set('module', function moduleStrategy(url, translateContext, parentU | 93 | 93 | assertBufferSource(source, true, 'load'); | 94 | 94 | source = stringify(source); | 95 | 95 | debug(`Translating StandardModule ${url}`, translateContext); | 96 | | - const { compileSourceTextModule } = require('internal/modules/esm/utils'); | | 96 | + const { | | 97 | + compileSourceTextModule, SourceTextModuleTypes: { kUser }, | | 98 | + } = require('internal/modules/esm/utils'); | 97 | 99 | const context = isMain ? { isMain } : undefined; | 98 | | - const module = compileSourceTextModule(url, source, this, context); | | 100 | + const module = compileSourceTextModule(url, source, kUser, context); | 99 | 101 | return module; | 100 | 102 | }); | 101 | 103 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/utils.js +66-7Lines changed: 66 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ const { | 14 | 14 | }, | 15 | 15 | } = internalBinding('util'); | 16 | 16 | const { | | 17 | + embedder_module_hdo, | 17 | 18 | source_text_module_default_hdo, | 18 | 19 | vm_dynamic_import_default_internal, | 19 | 20 | vm_dynamic_import_main_context_default, | @@ -43,6 +44,7 @@ const { | 43 | 44 | const assert = require('internal/assert'); | 44 | 45 | const { | 45 | 46 | normalizeReferrerURL, | | 47 | + loadBuiltinModuleForEmbedder, | 46 | 48 | } = require('internal/modules/helpers'); | 47 | 49 |
| 48 | 50 | let defaultConditions; | @@ -226,6 +228,28 @@ function defaultImportModuleDynamicallyForScript(specifier, phase, attributes, r | 226 | 228 | return cascadedLoader.import(specifier, parentURL, attributes, phase); | 227 | 229 | } | 228 | 230 |
| | 231 | +/** | | 232 | + * Loads the built-in and wraps it in a ModuleWrap for embedder ESM. | | 233 | + * @param {string} specifier | | 234 | + * @returns {ModuleWrap} | | 235 | + */ | | 236 | +function getBuiltinModuleWrapForEmbedder(specifier) { | | 237 | + return loadBuiltinModuleForEmbedder(specifier).getESMFacade(); | | 238 | +} | | 239 | + | | 240 | +/** | | 241 | + * Get the built-in module dynamically for embedder ESM. | | 242 | + * @param {string} specifier - The module specifier string. | | 243 | + * @param {number} phase - The module import phase. Ignored for now. | | 244 | + * @param {Record<string, string>} attributes - The import attributes object. Ignored for now. | | 245 | + * @param {string|null|undefined} referrerName - name of the referrer. | | 246 | + * @returns {import('internal/modules/esm/loader.js').ModuleExports} - The imported module object. | | 247 | + */ | | 248 | +function importModuleDynamicallyForEmbedder(specifier, phase, attributes, referrerName) { | | 249 | + // Ignore phase and attributes for embedder ESM for now, because this only supports loading builtins. | | 250 | + return getBuiltinModuleWrapForEmbedder(specifier).getNamespace(); | | 251 | +} | | 252 | + | 229 | 253 | /** | 230 | 254 | * Asynchronously imports a module dynamically using a callback function. The native callback. | 231 | 255 | * @param {symbol} referrerSymbol - Referrer symbol of the registered script, function, module, or contextified object. | @@ -253,6 +277,10 @@ async function importModuleDynamicallyCallback(referrerSymbol, specifier, phase, | 253 | 277 | if (referrerSymbol === source_text_module_default_hdo) { | 254 | 278 | return defaultImportModuleDynamicallyForModule(specifier, phase, attributes, referrerName); | 255 | 279 | } | | 280 | + // For embedder entry point ESM, only allow built-in modules. | | 281 | + if (referrerSymbol === embedder_module_hdo) { | | 282 | + return importModuleDynamicallyForEmbedder(specifier, phase, attributes, referrerName); | | 283 | + } | 256 | 284 |
| 257 | 285 | if (moduleRegistries.has(referrerSymbol)) { | 258 | 286 | const { importModuleDynamically, callbackReferrer } = moduleRegistries.get(referrerSymbol); | @@ -290,21 +318,42 @@ function shouldSpawnLoaderHookWorker() { | 290 | 318 | return _shouldSpawnLoaderHookWorker; | 291 | 319 | } | 292 | 320 |
| | 321 | +const SourceTextModuleTypes = { | | 322 | + kInternal: 'internal', // TODO(joyeecheung): support internal ESM. | | 323 | + kEmbedder: 'embedder', // Embedder ESM, also used by SEA | | 324 | + kUser: 'user', // User-land ESM | | 325 | + kFacade: 'facade', // Currently only used by the facade that proxies WASM module import/exports. | | 326 | +}; | | 327 | + | 293 | 328 | /** | 294 | 329 | * Compile a SourceTextModule for the built-in ESM loader. Register it for default | 295 | 330 | * source map and import.meta and dynamic import() handling if cascadedLoader is provided. | 296 | 331 | * @param {string} url URL of the module. | 297 | 332 | * @param {string} source Source code of the module. | 298 | | - * @param {typeof import('./loader.js').ModuleLoader|undefined} cascadedLoader If provided, | 299 | | - * register the module for default handling. | | 333 | + * @param {string} type Type of the source text module, one of SourceTextModuleTypes. | 300 | 334 | * @param {{ isMain?: boolean }|undefined} context - context object containing module metadata. | 301 | 335 | * @returns {ModuleWrap} | 302 | 336 | */ | 303 | | -function compileSourceTextModule(url, source, cascadedLoader, context = kEmptyObject) { | 304 | | - const hostDefinedOption = cascadedLoader ? source_text_module_default_hdo : undefined; | 305 | | - const wrap = new ModuleWrap(url, undefined, source, 0, 0, hostDefinedOption); | | 337 | +function compileSourceTextModule(url, source, type, context = kEmptyObject) { | | 338 | + let hostDefinedOptions; | | 339 | + switch (type) { | | 340 | + case SourceTextModuleTypes.kFacade: | | 341 | + case SourceTextModuleTypes.kInternal: | | 342 | + hostDefinedOptions = undefined; | | 343 | + break; | | 344 | + case SourceTextModuleTypes.kEmbedder: | | 345 | + hostDefinedOptions = embedder_module_hdo; | | 346 | + break; | | 347 | + case SourceTextModuleTypes.kUser: | | 348 | + hostDefinedOptions = source_text_module_default_hdo; | | 349 | + break; | | 350 | + default: | | 351 | + assert.fail(`Unknown SourceTextModule type: ${type}`); | | 352 | + } | | 353 | + | | 354 | + const wrap = new ModuleWrap(url, undefined, source, 0, 0, hostDefinedOptions); | 306 | 355 |
| 307 | | - if (!cascadedLoader) { | | 356 | + if (type === SourceTextModuleTypes.kFacade) { | 308 | 357 | return wrap; | 309 | 358 | } | 310 | 359 |
| @@ -317,10 +366,18 @@ function compileSourceTextModule(url, source, cascadedLoader, context = kEmptyOb | 317 | 366 | if (wrap.sourceMapURL) { | 318 | 367 | maybeCacheSourceMap(url, source, wrap, false, wrap.sourceURL, wrap.sourceMapURL); | 319 | 368 | } | | 369 | + | | 370 | + if (type === SourceTextModuleTypes.kEmbedder) { | | 371 | + // For embedder ESM, we also handle the linking and evaluation. | | 372 | + const requests = wrap.getModuleRequests(); | | 373 | + const modules = requests.map(({ specifier }) => getBuiltinModuleWrapForEmbedder(specifier)); | | 374 | + wrap.link(modules); | | 375 | + wrap.instantiate(); | | 376 | + wrap.evaluate(-1, false); | | 377 | + } | 320 | 378 | return wrap; | 321 | 379 | } | 322 | 380 |
| 323 | | - | 324 | 381 | const kImportInImportedESM = Symbol('kImportInImportedESM'); | 325 | 382 | const kImportInRequiredESM = Symbol('kImportInRequiredESM'); | 326 | 383 | const kRequireInImportedCJS = Symbol('kRequireInImportedCJS'); | @@ -331,11 +388,13 @@ const kRequireInImportedCJS = Symbol('kRequireInImportedCJS'); | 331 | 388 | const requestTypes = { kImportInImportedESM, kImportInRequiredESM, kRequireInImportedCJS }; | 332 | 389 |
| 333 | 390 | module.exports = { | | 391 | + embedder_module_hdo, | 334 | 392 | registerModule, | 335 | 393 | initializeESM, | 336 | 394 | getDefaultConditions, | 337 | 395 | getConditionsSet, | 338 | 396 | shouldSpawnLoaderHookWorker, | 339 | 397 | compileSourceTextModule, | | 398 | + SourceTextModuleTypes, | 340 | 399 | requestTypes, | 341 | 400 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/helpers.js +39Lines changed: 39 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,7 @@ const { | 15 | 15 | const { | 16 | 16 | ERR_INVALID_ARG_TYPE, | 17 | 17 | ERR_INVALID_RETURN_PROPERTY_VALUE, | | 18 | + ERR_UNKNOWN_BUILTIN_MODULE, | 18 | 19 | } = require('internal/errors').codes; | 19 | 20 | const { BuiltinModule } = require('internal/bootstrap/realm'); | 20 | 21 |
| @@ -28,6 +29,7 @@ const assert = require('internal/assert'); | 28 | 29 | const { getOptionValue } = require('internal/options'); | 29 | 30 | const { setOwnProperty, getLazy } = require('internal/util'); | 30 | 31 | const { inspect } = require('internal/util/inspect'); | | 32 | +const { emitWarningSync } = require('internal/process/warning'); | 31 | 33 |
| 32 | 34 | const lazyTmpdir = getLazy(() => require('os').tmpdir()); | 33 | 35 | const { join } = path; | @@ -126,6 +128,42 @@ function loadBuiltinModule(id) { | 126 | 128 | return mod; | 127 | 129 | } | 128 | 130 |
| | 131 | +let isSEABuiltinWarningNeeded_; | | 132 | +function isSEABuiltinWarningNeeded() { | | 133 | + if (isSEABuiltinWarningNeeded_ === undefined) { | | 134 | + const { isExperimentalSeaWarningNeeded, isSea } = internalBinding('sea'); | | 135 | + isSEABuiltinWarningNeeded_ = isSea() && isExperimentalSeaWarningNeeded(); | | 136 | + } | | 137 | + return isSEABuiltinWarningNeeded_; | | 138 | +} | | 139 | + | | 140 | +let warnedAboutBuiltins = false; | | 141 | +/** | | 142 | + * Helpers to load built-in modules for embedder modules. | | 143 | + * @param {string} id | | 144 | + * @returns {import('internal/bootstrap/realm.js').BuiltinModule} | | 145 | + */ | | 146 | +function loadBuiltinModuleForEmbedder(id) { | | 147 | + const normalized = BuiltinModule.normalizeRequirableId(id); | | 148 | + if (normalized) { | | 149 | + const mod = loadBuiltinModule(normalized); | | 150 | + if (mod) { | | 151 | + return mod; | | 152 | + } | | 153 | + } | | 154 | + if (isSEABuiltinWarningNeeded() && !warnedAboutBuiltins) { | | 155 | + emitWarningSync( | | 156 | + 'Currently the require() provided to the main script embedded into ' + | | 157 | + 'single-executable applications only supports loading built-in modules.\n' + | | 158 | + 'To load a module from disk after the single executable application is ' + | | 159 | + 'launched, use require("module").createRequire().\n' + | | 160 | + 'Support for bundled module loading or virtual file systems are under ' + | | 161 | + 'discussions in https://github.com/nodejs/single-executable'); | | 162 | + warnedAboutBuiltins = true; | | 163 | + } | | 164 | + throw new ERR_UNKNOWN_BUILTIN_MODULE(id); | | 165 | +} | | 166 | + | 129 | 167 | /** @type {Module} */ | 130 | 168 | let $Module = null; | 131 | 169 | /** | @@ -469,6 +507,7 @@ module.exports = { | 469 | 507 | getCjsConditionsArray, | 470 | 508 | getCompileCacheDir, | 471 | 509 | initializeCjsConditions, | | 510 | + loadBuiltinModuleForEmbedder, | 472 | 511 | loadBuiltinModule, | 473 | 512 | makeRequireFunction, | 474 | 513 | normalizeReferrerURL, | |
Collapse file Copy file name to clipboardExpand all lines: src/env_properties.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 58 | 58 | V(resource_symbol, "resource_symbol") \ | 59 | 59 | V(trigger_async_id_symbol, "trigger_async_id_symbol") \ | 60 | 60 | V(builtin_source_text_module_hdo, "builtin_source_text_module_hdo") \ | | 61 | + V(embedder_module_hdo, "embedder_module_hdo") \ | 61 | 62 | V(source_text_module_default_hdo, "source_text_module_default_hdo") \ | 62 | 63 | V(vm_context_no_contextify, "vm_context_no_contextify") \ | 63 | 64 | V(vm_dynamic_import_default_internal, "vm_dynamic_import_default_internal") \ | |
Collapse file Copy file name to clipboardExpand all lines: src/module_wrap.cc +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1397,7 +1397,8 @@ void ModuleWrap::HostInitializeImportMetaObjectCallback( | 1397 | 1397 |
| 1398 | 1398 | // Use the default initializer for source text modules without custom | 1399 | 1399 | // callbacks. | 1400 | | - if (id == env->source_text_module_default_hdo()) { | | 1400 | + if (id == env->source_text_module_default_hdo() || | | 1401 | + id == env->embedder_module_hdo()) { | 1401 | 1402 | USE(DefaultImportMetaObjectInitializer(realm, wrap, meta)); | 1402 | 1403 | return; | 1403 | 1404 | } | |
|