@@ -158,6 +158,8 @@ ModuleWrap::ModuleWrap(Realm* realm,
158158
159159 if (!synthetic_evaluation_step->IsUndefined ()) {
160160 synthetic_ = true ;
161+ // Synthetic modules have no dependencies.
162+ linked_ = true ;
161163 }
162164 MakeWeak ();
163165 module_.SetWeak ();
@@ -240,7 +242,7 @@ Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
240242 return Just (true );
241243 }
242244
243- if (!module -> IsGraphAsync ()) { // There is no TLA, no need to check.
245+ if (!HasAsyncGraph ()) { // There is no TLA, no need to check.
244246 return Just (true );
245247 }
246248
@@ -263,6 +265,16 @@ Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
263265 return Just (false );
264266}
265267
268+ bool ModuleWrap::HasAsyncGraph () {
269+ if (!has_async_graph_.has_value ()) {
270+ Isolate* isolate = env ()->isolate ();
271+ HandleScope scope (isolate);
272+
273+ has_async_graph_ = module_.Get (isolate)->IsGraphAsync ();
274+ }
275+ return has_async_graph_.value ();
276+ }
277+
266278Local<PrimitiveArray> ModuleWrap::GetHostDefinedOptions (
267279 Isolate* isolate, Local<Symbol> id_symbol) {
268280 Local<PrimitiveArray> host_defined_options =
@@ -673,35 +685,6 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
673685 dependent->linked_ = true ;
674686}
675687
676- void ModuleWrap::Instantiate (const FunctionCallbackInfo<Value>& args) {
677- Realm* realm = Realm::GetCurrent (args);
678- Isolate* isolate = args.GetIsolate ();
679- ModuleWrap* obj;
680- ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
681- Local<Context> context = obj->context ();
682- Local<Module> module = obj->module_ .Get (isolate);
683-
684- if (!obj->IsLinked ()) {
685- THROW_ERR_VM_MODULE_LINK_FAILURE (realm->env (), " module is not linked" );
686- return ;
687- }
688-
689- TryCatchScope try_catch (realm->env ());
690- USE (module ->InstantiateModule (
691- context, ResolveModuleCallback, ResolveSourceCallback));
692-
693- if (try_catch.HasCaught () && !try_catch.HasTerminated ()) {
694- CHECK (!try_catch.Message ().IsEmpty ());
695- CHECK (!try_catch.Exception ().IsEmpty ());
696- AppendExceptionLine (realm->env (),
697- try_catch.Exception (),
698- try_catch.Message (),
699- ErrorHandlingMode::MODULE_ERROR);
700- try_catch.ReThrow ();
701- return ;
702- }
703- }
704-
705688void ModuleWrap::Evaluate (const FunctionCallbackInfo<Value>& args) {
706689 Realm* realm = Realm::GetCurrent (args);
707690 Isolate* isolate = realm->isolate ();
@@ -783,7 +766,7 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
783766 }
784767}
785768
786- void ModuleWrap::InstantiateSync (const FunctionCallbackInfo<Value>& args) {
769+ void ModuleWrap::Instantiate (const FunctionCallbackInfo<Value>& args) {
787770 Realm* realm = Realm::GetCurrent (args);
788771 Isolate* isolate = args.GetIsolate ();
789772 ModuleWrap* obj;
@@ -792,6 +775,11 @@ void ModuleWrap::InstantiateSync(const FunctionCallbackInfo<Value>& args) {
792775 Local<Module> module = obj->module_ .Get (isolate);
793776 Environment* env = realm->env ();
794777
778+ if (!obj->IsLinked ()) {
779+ THROW_ERR_VM_MODULE_LINK_FAILURE (env, " module is not linked" );
780+ return ;
781+ }
782+
795783 {
796784 TryCatchScope try_catch (env);
797785 USE (module ->InstantiateModule (
@@ -808,10 +796,6 @@ void ModuleWrap::InstantiateSync(const FunctionCallbackInfo<Value>& args) {
808796 return ;
809797 }
810798 }
811-
812- // TODO(joyeecheung): record Module::HasTopLevelAwait() in every ModuleWrap
813- // and infer the asynchronicity from a module's children during linking.
814- args.GetReturnValue ().Set (module ->IsGraphAsync ());
815799}
816800
817801Maybe<void > ThrowIfPromiseRejected (Realm* realm, Local<Promise> promise) {
@@ -879,7 +863,7 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
879863 return ;
880864 }
881865
882- if (module -> IsGraphAsync ()) {
866+ if (obj-> HasAsyncGraph ()) {
883867 CHECK (env->options ()->print_required_tla );
884868 auto stalled_messages =
885869 std::get<1 >(module ->GetStalledTopLevelAwaitMessages (isolate));
@@ -901,52 +885,15 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
901885 args.GetReturnValue ().Set (module ->GetModuleNamespace ());
902886}
903887
904- void ModuleWrap::GetNamespaceSync (const FunctionCallbackInfo<Value>& args) {
905- Realm* realm = Realm::GetCurrent (args);
906- Isolate* isolate = args.GetIsolate ();
907- ModuleWrap* obj;
908- ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
909- Local<Module> module = obj->module_ .Get (isolate);
910-
911- switch (module ->GetStatus ()) {
912- case Module::Status::kUninstantiated :
913- case Module::Status::kInstantiating :
914- return realm->env ()->ThrowError (
915- " Cannot get namespace, module has not been instantiated" );
916- case Module::Status::kInstantiated :
917- case Module::Status::kEvaluating :
918- case Module::Status::kEvaluated :
919- case Module::Status::kErrored :
920- break ;
921- }
922-
923- if (module ->IsGraphAsync ()) {
924- return THROW_ERR_REQUIRE_ASYNC_MODULE (realm->env (), args[0 ], args[1 ]);
925- }
926- Local<Value> result = module ->GetModuleNamespace ();
927- args.GetReturnValue ().Set (result);
928- }
929-
930888void ModuleWrap::GetNamespace (const FunctionCallbackInfo<Value>& args) {
931889 Realm* realm = Realm::GetCurrent (args);
932890 Isolate* isolate = args.GetIsolate ();
933891 ModuleWrap* obj;
934892 ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
935893
936894 Local<Module> module = obj->module_ .Get (isolate);
937-
938- switch (module ->GetStatus ()) {
939- case Module::Status::kUninstantiated :
940- case Module::Status::kInstantiating :
941- return realm->env ()->ThrowError (
942- " cannot get namespace, module has not been instantiated" );
943- case Module::Status::kInstantiated :
944- case Module::Status::kEvaluating :
945- case Module::Status::kEvaluated :
946- case Module::Status::kErrored :
947- break ;
948- default :
949- UNREACHABLE ();
895+ if (module ->GetStatus () < Module::kInstantiated ) {
896+ return THROW_ERR_MODULE_NOT_INSTANTIATED (realm->env ());
950897 }
951898
952899 Local<Value> result = module ->GetModuleNamespace ();
@@ -997,14 +944,18 @@ void ModuleWrap::GetStatus(const FunctionCallbackInfo<Value>& args) {
997944 args.GetReturnValue ().Set (module ->GetStatus ());
998945}
999946
1000- void ModuleWrap::IsGraphAsync (const FunctionCallbackInfo<Value>& args) {
947+ void ModuleWrap::HasAsyncGraph (const FunctionCallbackInfo<Value>& args) {
1001948 Isolate* isolate = args.GetIsolate ();
949+ Environment* env = Environment::GetCurrent (isolate);
1002950 ModuleWrap* obj;
1003951 ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
1004952
1005953 Local<Module> module = obj->module_ .Get (isolate);
954+ if (module ->GetStatus () < Module::kInstantiated ) {
955+ return THROW_ERR_MODULE_NOT_INSTANTIATED (env);
956+ }
1006957
1007- args.GetReturnValue ().Set (module -> IsGraphAsync ());
958+ args.GetReturnValue ().Set (obj-> HasAsyncGraph ());
1008959}
1009960
1010961void ModuleWrap::GetError (const FunctionCallbackInfo<Value>& args) {
@@ -1417,9 +1368,7 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
14171368
14181369 SetProtoMethod (isolate, tpl, " link" , Link);
14191370 SetProtoMethod (isolate, tpl, " getModuleRequests" , GetModuleRequests);
1420- SetProtoMethod (isolate, tpl, " instantiateSync" , InstantiateSync);
14211371 SetProtoMethod (isolate, tpl, " evaluateSync" , EvaluateSync);
1422- SetProtoMethod (isolate, tpl, " getNamespaceSync" , GetNamespaceSync);
14231372 SetProtoMethod (isolate, tpl, " instantiate" , Instantiate);
14241373 SetProtoMethod (isolate, tpl, " evaluate" , Evaluate);
14251374 SetProtoMethod (isolate, tpl, " setExport" , SetSyntheticExport);
@@ -1429,7 +1378,7 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
14291378 isolate, tpl, " createCachedData" , CreateCachedData);
14301379 SetProtoMethodNoSideEffect (isolate, tpl, " getNamespace" , GetNamespace);
14311380 SetProtoMethodNoSideEffect (isolate, tpl, " getStatus" , GetStatus);
1432- SetProtoMethodNoSideEffect (isolate, tpl, " isGraphAsync " , IsGraphAsync );
1381+ SetProtoMethodNoSideEffect (isolate, tpl, " hasAsyncGraph " , HasAsyncGraph );
14331382 SetProtoMethodNoSideEffect (isolate, tpl, " getError" , GetError);
14341383 SetConstructorFunction (isolate, target, " ModuleWrap" , tpl);
14351384 isolate_data->set_module_wrap_constructor_template (tpl);
@@ -1479,9 +1428,7 @@ void ModuleWrap::RegisterExternalReferences(
14791428
14801429 registry->Register (Link);
14811430 registry->Register (GetModuleRequests);
1482- registry->Register (InstantiateSync);
14831431 registry->Register (EvaluateSync);
1484- registry->Register (GetNamespaceSync);
14851432 registry->Register (Instantiate);
14861433 registry->Register (Evaluate);
14871434 registry->Register (SetSyntheticExport);
@@ -1491,7 +1438,7 @@ void ModuleWrap::RegisterExternalReferences(
14911438 registry->Register (GetNamespace);
14921439 registry->Register (GetStatus);
14931440 registry->Register (GetError);
1494- registry->Register (IsGraphAsync );
1441+ registry->Register (HasAsyncGraph );
14951442
14961443 registry->Register (CreateRequiredModuleFacade);
14971444
0 commit comments