@@ -22,7 +22,14 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
2222 debug = fn ;
2323} ) ;
2424
25- const { ModuleWrap, kEvaluationPhase, kInstantiated } = internalBinding ( 'module_wrap' ) ;
25+ const {
26+ ModuleWrap,
27+ kErrored,
28+ kEvaluated,
29+ kEvaluationPhase,
30+ kInstantiated,
31+ kUninstantiated,
32+ } = internalBinding ( 'module_wrap' ) ;
2633const {
2734 privateSymbols : {
2835 entry_point_module_private_symbol,
@@ -280,17 +287,34 @@ class ModuleJob extends ModuleJobBase {
280287 runSync ( parent ) {
281288 assert ( this . phase === kEvaluationPhase ) ;
282289 assert ( this . module instanceof ModuleWrap ) ;
283- if ( this . instantiated !== undefined ) {
284- return { __proto__ : null , module : this . module } ;
290+ let status = this . module . getStatus ( ) ;
291+
292+ debug ( 'ModuleJob.runSync' , this . module ) ;
293+ // FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking
294+ // fully synchronous instead.
295+ if ( status === kUninstantiated ) {
296+ this . module . async = this . module . instantiateSync ( ) ;
297+ status = this . module . getStatus ( ) ;
285298 }
299+ if ( status === kInstantiated || status === kErrored ) {
300+ const filename = urlToFilename ( this . url ) ;
301+ const parentFilename = urlToFilename ( parent ?. filename ) ;
302+ this . module . async ??= this . module . isGraphAsync ( ) ;
286303
287- this . module . instantiate ( ) ;
288- this . instantiated = PromiseResolve ( ) ;
289- setHasStartedUserESMExecution ( ) ;
290- const filename = urlToFilename ( this . url ) ;
291- const parentFilename = urlToFilename ( parent ?. filename ) ;
292- const namespace = this . module . evaluateSync ( filename , parentFilename ) ;
293- return { __proto__ : null , module : this . module , namespace } ;
304+ if ( this . module . async && ! getOptionValue ( '--experimental-print-required-tla' ) ) {
305+ throw new ERR_REQUIRE_ASYNC_MODULE ( filename , parentFilename ) ;
306+ }
307+ if ( status === kInstantiated ) {
308+ setHasStartedUserESMExecution ( ) ;
309+ const namespace = this . module . evaluateSync ( filename , parentFilename ) ;
310+ return { __proto__ : null , module : this . module , namespace } ;
311+ }
312+ throw this . module . getError ( ) ;
313+
314+ } else if ( status === kEvaluated ) {
315+ return { __proto__ : null , module : this . module , namespace : this . module . getNamespaceSync ( ) } ;
316+ }
317+ assert . fail ( `Unexpected module status ${ status } .` ) ;
294318 }
295319
296320 async run ( isEntryPoint = false ) {
0 commit comments