@@ -11,6 +11,7 @@ const { |
11 | 11 | PromisePrototypeThen, |
12 | 12 | RegExpPrototypeSymbolReplace, |
13 | 13 | encodeURIComponent, |
| 14 | + globalThis, |
14 | 15 | hardenRegExp, |
15 | 16 | } = primordials; |
16 | 17 |
|
@@ -22,6 +23,11 @@ const { |
22 | 23 | kRequiredModuleSymbol, |
23 | 24 | } = require('internal/modules/cjs/loader'); |
24 | 25 | const { imported_cjs_symbol } = internalBinding('symbols'); |
| 26 | +const { |
| 27 | + privateSymbols: { |
| 28 | + entry_point_module_private_symbol, |
| 29 | + }, |
| 30 | +} = internalBinding('util'); |
25 | 31 |
|
26 | 32 | const assert = require('internal/assert'); |
27 | 33 | const { |
@@ -350,6 +356,39 @@ class ModuleLoader { |
350 | 356 | return { wrap: job.module, namespace: job.runSync(parent).namespace }; |
351 | 357 | } |
352 | 358 |
|
| 359 | + /** |
| 360 | + * Load and evaluate the entry point module. |
| 361 | + * The graph is always loaded and linked synchronously. When it contains no top-level |
| 362 | + * await it is evaluated synchronously too, so startup creates no promises at all. |
| 363 | + * When it does contain top-level await, evaluation falls back to `job.run()` here, |
| 364 | + * reusing the job that was just built rather than making the caller re-enter the |
| 365 | + * loader and redo the resolution and linking for the same module. |
| 366 | + * @param {string} url The URL of the entry point module. |
| 367 | + * @returns {ModuleJobSync|undefined} The job still needing asynchronous evaluation when |
| 368 | + * the graph has top-level await, otherwise `undefined` - the module is then already |
| 369 | + * fully evaluated and no promise was ever created. |
| 370 | + */ |
| 371 | + importForEntryPoint(url) { |
| 372 | + return onImport.traceSync(() => { |
| 373 | + const request = { specifier: url, phase: kEvaluationPhase, attributes: kEmptyObject, __proto__: null }; |
| 374 | + const job = this.getOrCreateModuleJob(undefined, request, kImportInImportedESM); |
| 375 | + job.instantiateSync(); |
| 376 | + if (job.module.hasAsyncGraph) { |
| 377 | + // Hand the job back so the caller can evaluate it through the ordinary |
| 378 | + // asynchronous entry point machinery, without re-resolving and re-linking |
| 379 | + // the module the way re-entering `import()` would. |
| 380 | + return job; |
| 381 | + } |
| 382 | + globalThis[entry_point_module_private_symbol] = job.module; |
| 383 | + job.runSync(); |
| 384 | + return undefined; |
| 385 | + }, { |
| 386 | + __proto__: null, |
| 387 | + parentURL: undefined, |
| 388 | + url, |
| 389 | + }); |
| 390 | + } |
| 391 | + |
353 | 392 | /** |
354 | 393 | * Check invariants on a cached module job when require()'d from ESM. |
355 | 394 | * @param {string} specifier The first parameter of require(). |
@@ -572,10 +611,14 @@ class ModuleLoader { |
572 | 611 | assert(moduleOrModulePromise instanceof ModuleWrap, `Expected ModuleWrap for loading ${url}`); |
573 | 612 | } |
574 | 613 |
|
575 | | - // TODO(joyeecheung): use ModuleJobSync for kRequireInImportedCJS too. |
576 | | - const ModuleJobCtor = (requestType === kImportInRequiredESM ? ModuleJobSync : ModuleJob); |
577 | 614 | const isMain = (parentURL === undefined); |
578 | 615 | const inspectBrk = (isMain && getOptionValue('--inspect-brk')); |
| 616 | + // Use ModuleJobSync whenever we're on the main thread (not the async loader hook worker), |
| 617 | + // except for kRequireInImportedCJS (TODO: consolidate that case too). |
| 618 | + // TODO(joyeecheung): use ModuleJobSync for kRequireInImportedCJS too. |
| 619 | + const ModuleJobCtor = (this.isForAsyncLoaderHookWorker || |
| 620 | + requestType === kRequireInImportedCJS) ? |
| 621 | + ModuleJob : ModuleJobSync; |
579 | 622 | job = new ModuleJobCtor( |
580 | 623 | this, |
581 | 624 | url, |
@@ -602,8 +645,9 @@ class ModuleLoader { |
602 | 645 | */ |
603 | 646 | getOrCreateModuleJob(parentURL, request, requestType) { |
604 | 647 | let maybePromise; |
605 | | - if (requestType === kRequireInImportedCJS || requestType === kImportInRequiredESM) { |
606 | | - // In these two cases, resolution must be synchronous. |
| 648 | + if (!this.isForAsyncLoaderHookWorker) { |
| 649 | + // On the main thread, always resolve synchronously; |
| 650 | + // `resolveSync` coordinates with the async loader hook worker if needed. |
607 | 651 | maybePromise = this.resolveSync(parentURL, request); |
608 | 652 | assert(!isPromise(maybePromise)); |
609 | 653 | } else { |
|