@@ -22,7 +22,14 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
22
22
debug = fn ;
23
23
} ) ;
24
24
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' ) ;
26
33
const {
27
34
privateSymbols : {
28
35
entry_point_module_private_symbol,
@@ -277,17 +284,34 @@ class ModuleJob extends ModuleJobBase {
277
284
runSync ( parent ) {
278
285
assert ( this . phase === kEvaluationPhase ) ;
279
286
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 ( ) ;
282
295
}
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 ( ) ;
283
300
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 } .` ) ;
291
315
}
292
316
293
317
async run ( isEntryPoint = false ) {
0 commit comments