@@ -21,8 +21,13 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
21
21
debug = fn ;
22
22
} ) ;
23
23
24
- const { ModuleWrap, kInstantiated } = internalBinding ( 'module_wrap' ) ;
25
-
24
+ const {
25
+ ModuleWrap,
26
+ kErrored,
27
+ kEvaluated,
28
+ kInstantiated,
29
+ kUninstantiated,
30
+ } = internalBinding ( 'module_wrap' ) ;
26
31
const { decorateErrorStack, kEmptyObject } = require ( 'internal/util' ) ;
27
32
const {
28
33
getSourceMapsEnabled,
@@ -242,17 +247,34 @@ class ModuleJob extends ModuleJobBase {
242
247
243
248
runSync ( parent ) {
244
249
assert ( this . module instanceof ModuleWrap ) ;
245
- if ( this . instantiated !== undefined ) {
246
- return { __proto__ : null , module : this . module } ;
250
+ let status = this . module . getStatus ( ) ;
251
+
252
+ debug ( 'ModuleJob.runSync' , this . module ) ;
253
+ // FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking
254
+ // fully synchronous instead.
255
+ if ( status === kUninstantiated ) {
256
+ this . module . async = this . module . instantiateSync ( ) ;
257
+ status = this . module . getStatus ( ) ;
247
258
}
259
+ if ( status === kInstantiated || status === kErrored ) {
260
+ const filename = urlToFilename ( this . url ) ;
261
+ const parentFilename = urlToFilename ( parent ?. filename ) ;
262
+ this . module . async ??= this . module . isGraphAsync ( ) ;
248
263
249
- this . module . instantiate ( ) ;
250
- this . instantiated = PromiseResolve ( ) ;
251
- setHasStartedUserESMExecution ( ) ;
252
- const filename = urlToFilename ( this . url ) ;
253
- const parentFilename = urlToFilename ( parent ?. filename ) ;
254
- const namespace = this . module . evaluateSync ( filename , parentFilename ) ;
255
- return { __proto__ : null , module : this . module , namespace } ;
264
+ if ( this . module . async && ! getOptionValue ( '--experimental-print-required-tla' ) ) {
265
+ throw new ERR_REQUIRE_ASYNC_MODULE ( filename , parentFilename ) ;
266
+ }
267
+ if ( status === kInstantiated ) {
268
+ setHasStartedUserESMExecution ( ) ;
269
+ const namespace = this . module . evaluateSync ( filename , parentFilename ) ;
270
+ return { __proto__ : null , module : this . module , namespace } ;
271
+ }
272
+ throw this . module . getError ( ) ;
273
+
274
+ } else if ( status === kEvaluated ) {
275
+ return { __proto__ : null , module : this . module , namespace : this . module . getNamespaceSync ( ) } ;
276
+ }
277
+ assert . fail ( `Unexpected module status ${ status } .` ) ;
256
278
}
257
279
258
280
async run ( ) {
0 commit comments