@@ -329,7 +329,7 @@ class ModuleJobBase {
329329 debug ( `ModuleJobBase.syncLink() ${ this . url } -> ${ request . specifier } ` , job ) ;
330330 assert ( ! isPromise ( job ) ) ;
331331 assert ( job . module instanceof ModuleWrap ) ;
332- if ( this . shouldRunModule ( request . phase ) ) {
332+ if ( this . shouldEvaluateModule ( request . phase ) ) {
333333 ArrayPrototypePush ( evaluationDepJobs , job ) ;
334334 }
335335 modules [ idx ] = job . module ;
@@ -383,7 +383,7 @@ class ModuleJobBase {
383383 shouldLinkModule ( phase ) {
384384 return phase >= kDeferPhase ;
385385 }
386- shouldRunModule ( phase ) {
386+ shouldEvaluateModule ( phase ) {
387387 return phase === kEvaluationPhase ;
388388 }
389389}
@@ -466,7 +466,7 @@ class ModuleJob extends ModuleJobBase {
466466 const dependencyJobPromise = this . loader . getOrCreateModuleJob ( this . url , request , requestType ) ;
467467 const modulePromise = PromisePrototypeThen ( dependencyJobPromise , ( job ) => {
468468 debug ( `ModuleJob.asyncLink() ${ this . url } -> ${ request . specifier } ` , job ) ;
469- if ( this . shouldRunModule ( request . phase ) ) {
469+ if ( this . shouldEvaluateModule ( request . phase ) ) {
470470 ArrayPrototypePush ( evaluationDepJobs , job ) ;
471471 }
472472 return job . modulePromise ;
@@ -521,11 +521,11 @@ class ModuleJob extends ModuleJobBase {
521521 }
522522
523523 runSync ( parent ) {
524- assert ( this . shouldRunModule ( this . phase ) ) ;
524+ assert ( this . shouldEvaluateModule ( this . phase ) ) ;
525525 assert ( this . module instanceof ModuleWrap ) ;
526526 let status = this . module . getStatus ( ) ;
527527
528- debug ( ' ModuleJob.runSync()' , status , this . module ) ;
528+ debug ( ` ModuleJob.runSync(); status: ${ status } ` , this . module ) ;
529529 if ( status === kUninstantiated ) {
530530 // TODO(joyeecheung): Reject graphs with top-level await _before_ instantiation, so that
531531 // the async graph error supersedes instantiation (mismatch export) errors in the graph.
@@ -541,7 +541,8 @@ class ModuleJob extends ModuleJobBase {
541541 this . throwIfAsyncGraph ( parent ) ;
542542 if ( status === kInstantiated ) {
543543 setHasStartedUserESMExecution ( ) ;
544- const namespace = this . module . evaluateSync ( ) ;
544+ const namespace = this . phase === kEvaluationPhase ?
545+ this . module . evaluateSync ( ) : this . module . evaluateSync ( this . phase ) ;
545546 return { __proto__ : null , module : this . module , namespace } ;
546547 }
547548 throw this . module . getError ( ) ;
@@ -561,17 +562,17 @@ class ModuleJob extends ModuleJobBase {
561562 }
562563
563564 async run ( isEntryPoint = false ) {
564- debug ( 'ModuleJob.run()' , this . module ) ;
565- assert ( this . shouldRunModule ( this . phase ) ) ;
565+ debug ( 'ModuleJob.run(); phase: ' , this . phase , this . module ) ;
566566 await this . #instantiate( ) ;
567567 if ( isEntryPoint ) {
568568 globalThis [ entry_point_module_private_symbol ] = this . module ;
569569 }
570+
570571 const timeout = - 1 ;
571572 const breakOnSigint = false ;
572573 setHasStartedUserESMExecution ( ) ;
573574 try {
574- await this . module . evaluate ( timeout , breakOnSigint ) ;
575+ await this . module . evaluate ( timeout , breakOnSigint , this . phase ) ;
575576 } catch ( e ) {
576577 explainCommonJSGlobalLikeNotDefinedError ( e , this . module . url , this . module . hasTopLevelAwait ) ;
577578 throw e ;
@@ -641,9 +642,9 @@ class ModuleJobSync extends ModuleJobBase {
641642 }
642643
643644 async run ( isEntryPoint = false ) {
644- assert ( this . shouldRunModule ( this . phase ) ) ;
645+ // This path is hit by a require'd module that is imported again.
645646 const status = this . module . getStatus ( ) ;
646- debug ( ' ModuleJobSync.run()' , status , this . module ) ;
647+ debug ( ` ModuleJobSync.run() at phase ${ this . phase } ; status: ${ status } ` , this . module ) ;
647648 // If the module was previously required and errored, reject from import() again.
648649 if ( status === kErrored ) {
649650 throw this . module . getError ( ) ;
@@ -674,17 +675,18 @@ class ModuleJobSync extends ModuleJobBase {
674675 const breakOnSigint = false ;
675676 if ( this . inspectBrk ) {
676677 const { callAndPauseOnStart } = internalBinding ( 'inspector' ) ;
677- this . evaluationPromise = callAndPauseOnStart ( this . module . evaluate , this . module , timeout , breakOnSigint ) ;
678+ this . evaluationPromise = callAndPauseOnStart (
679+ this . module . evaluate , this . module , timeout , breakOnSigint , this . phase ) ;
678680 } else {
679- this . evaluationPromise = this . module . evaluate ( timeout , breakOnSigint ) ;
681+ this . evaluationPromise = this . module . evaluate ( timeout , breakOnSigint , this . phase ) ;
680682 }
681683 await this . evaluationPromise ;
682684 this . evaluationPromise = undefined ;
683685 return { __proto__ : null , module : this . module } ;
684686 }
685687 // No top-level `await`: evaluate synchronously
686688 try {
687- this . #evaluateSync( ) ;
689+ this . #evaluateSync( this . phase ) ;
688690 } catch ( evaluateError ) {
689691 explainCommonJSGlobalLikeNotDefinedError ( evaluateError , this . module . url , this . module . hasTopLevelAwait ) ;
690692 throw evaluateError ;
@@ -694,17 +696,23 @@ class ModuleJobSync extends ModuleJobBase {
694696
695697 // Evaluate this module synchronously, pausing at the first line of user land JS
696698 // that runs if --inspect-brk is active - that may well be inside a dependency.
697- #evaluateSync( ) {
699+ #evaluateSync( phase ) {
698700 if ( this . inspectBrk ) {
699701 const { callAndPauseOnStart } = internalBinding ( 'inspector' ) ;
700- return callAndPauseOnStart ( this . module . evaluateSync , this . module ) ;
702+ if ( phase === kEvaluationPhase ) {
703+ return callAndPauseOnStart ( this . module . evaluateSync , this . module ) ;
704+ }
705+ return callAndPauseOnStart ( this . module . evaluateSync , this . module , phase ) ;
706+ }
707+ if ( phase !== kEvaluationPhase ) {
708+ return this . module . evaluateSync ( phase ) ;
701709 }
702710 return this . module . evaluateSync ( ) ;
703711 }
704712
705713 runSync ( parent ) {
706714 debug ( 'ModuleJobSync.runSync()' , this . module ) ;
707- assert ( this . shouldRunModule ( this . phase ) ) ;
715+ assert ( this . shouldEvaluateModule ( this . phase ) ) ;
708716 // TODO(joyeecheung): Reject graphs with top-level await _before_ instantiation, so that the
709717 // async graph error supersedes instantiation (mismatch export) errors in the graph.
710718 this . instantiateSync ( ) ;
@@ -714,7 +722,7 @@ class ModuleJobSync extends ModuleJobBase {
714722 this . throwIfAsyncGraph ( parent ) ;
715723 setHasStartedUserESMExecution ( ) ;
716724 try {
717- const namespace = this . #evaluateSync( ) ;
725+ const namespace = this . #evaluateSync( this . phase ) ;
718726 return { __proto__ : null , module : this . module , namespace } ;
719727 } catch ( e ) {
720728 explainCommonJSGlobalLikeNotDefinedError ( e , this . module . url , this . module . hasTopLevelAwait ) ;
0 commit comments