@@ -22,7 +22,14 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
2222 debug = fn ;
2323} ) ;
2424
25- const { ModuleWrap, kInstantiated, kEvaluationPhase } = 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,
@@ -277,17 +284,34 @@ class ModuleJob extends ModuleJobBase {
277284 runSync ( parent ) {
278285 assert ( this . phase === kEvaluationPhase ) ;
279286 assert ( this . module instanceof ModuleWrap ) ;
280- if ( this . instantiated !== undefined ) {
281- return { __proto__ : null , module : this . module } ;
287+ let status = this . module . getStatus ( ) ;
288+
289+ debug ( 'ModuleJob.runSync' , this . module ) ;
290+ // FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking
291+ // fully synchronous instead.
292+ if ( status === kUninstantiated ) {
293+ this . module . async = this . module . instantiateSync ( ) ;
294+ status = this . module . getStatus ( ) ;
282295 }
296+ if ( status === kInstantiated || status === kErrored ) {
297+ const filename = urlToFilename ( this . url ) ;
298+ const parentFilename = urlToFilename ( parent ?. filename ) ;
299+ this . module . async ??= this . module . isGraphAsync ( ) ;
283300
284- this . module . instantiate ( ) ;
285- this . instantiated = PromiseResolve ( ) ;
286- setHasStartedUserESMExecution ( ) ;
287- const filename = urlToFilename ( this . url ) ;
288- const parentFilename = urlToFilename ( parent ?. filename ) ;
289- const namespace = this . module . evaluateSync ( filename , parentFilename ) ;
290- return { __proto__ : null , module : this . module , namespace } ;
301+ if ( this . module . async && ! getOptionValue ( '--experimental-print-required-tla' ) ) {
302+ throw new ERR_REQUIRE_ASYNC_MODULE ( filename , parentFilename ) ;
303+ }
304+ if ( status === kInstantiated ) {
305+ setHasStartedUserESMExecution ( ) ;
306+ const namespace = this . module . evaluateSync ( filename , parentFilename ) ;
307+ return { __proto__ : null , module : this . module , namespace } ;
308+ }
309+ throw this . module . getError ( ) ;
310+
311+ } else if ( status === kEvaluated ) {
312+ return { __proto__ : null , module : this . module , namespace : this . module . getNamespaceSync ( ) } ;
313+ }
314+ assert . fail ( `Unexpected module status ${ status } .` ) ;
291315 }
292316
293317 async run ( isEntryPoint = false ) {
0 commit comments