@@ -56,6 +56,7 @@ using v8::ObjectTemplate;
56
56
using v8::PrimitiveArray;
57
57
using v8::Promise;
58
58
using v8::PromiseRejectEvent;
59
+ using v8::PropertyCallbackInfo;
59
60
using v8::ScriptCompiler;
60
61
using v8::ScriptOrigin;
61
62
using v8::String;
@@ -158,6 +159,8 @@ ModuleWrap::ModuleWrap(Realm* realm,
158
159
159
160
if (!synthetic_evaluation_step->IsUndefined ()) {
160
161
synthetic_ = true ;
162
+ // Synthetic modules have no dependencies.
163
+ linked_ = true ;
161
164
}
162
165
MakeWeak ();
163
166
module_.SetWeak ();
@@ -240,7 +243,7 @@ Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
240
243
return Just (true );
241
244
}
242
245
243
- if (!module -> IsGraphAsync ()) { // There is no TLA, no need to check.
246
+ if (!HasAsyncGraph ()) { // There is no TLA, no need to check.
244
247
return Just (true );
245
248
}
246
249
@@ -263,6 +266,16 @@ Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
263
266
return Just (false );
264
267
}
265
268
269
+ bool ModuleWrap::HasAsyncGraph () {
270
+ if (!has_async_graph_.has_value ()) {
271
+ Isolate* isolate = env ()->isolate ();
272
+ HandleScope scope (isolate);
273
+
274
+ has_async_graph_ = module_.Get (isolate)->IsGraphAsync ();
275
+ }
276
+ return has_async_graph_.value ();
277
+ }
278
+
266
279
Local<PrimitiveArray> ModuleWrap::GetHostDefinedOptions (
267
280
Isolate* isolate, Local<Symbol> id_symbol) {
268
281
Local<PrimitiveArray> host_defined_options =
@@ -687,25 +700,28 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
687
700
ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
688
701
Local<Context> context = obj->context ();
689
702
Local<Module> module = obj->module_ .Get (isolate);
703
+ Environment* env = realm->env ();
690
704
691
705
if (!obj->IsLinked ()) {
692
- THROW_ERR_VM_MODULE_LINK_FAILURE (realm-> env () , " module is not linked" );
706
+ THROW_ERR_VM_MODULE_LINK_FAILURE (env, " module is not linked" );
693
707
return ;
694
708
}
695
709
696
- TryCatchScope try_catch (realm->env ());
697
- USE (module ->InstantiateModule (
698
- context, ResolveModuleCallback, ResolveSourceCallback));
710
+ {
711
+ TryCatchScope try_catch (env);
712
+ USE (module ->InstantiateModule (
713
+ context, ResolveModuleCallback, ResolveSourceCallback));
699
714
700
- if (try_catch.HasCaught () && !try_catch.HasTerminated ()) {
701
- CHECK (!try_catch.Message ().IsEmpty ());
702
- CHECK (!try_catch.Exception ().IsEmpty ());
703
- AppendExceptionLine (realm->env (),
704
- try_catch.Exception (),
705
- try_catch.Message (),
706
- ErrorHandlingMode::MODULE_ERROR);
707
- try_catch.ReThrow ();
708
- return ;
715
+ if (try_catch.HasCaught () && !try_catch.HasTerminated ()) {
716
+ CHECK (!try_catch.Message ().IsEmpty ());
717
+ CHECK (!try_catch.Exception ().IsEmpty ());
718
+ AppendExceptionLine (env,
719
+ try_catch.Exception (),
720
+ try_catch.Message (),
721
+ ErrorHandlingMode::MODULE_ERROR);
722
+ try_catch.ReThrow ();
723
+ return ;
724
+ }
709
725
}
710
726
}
711
727
@@ -790,37 +806,6 @@ void ModuleWrap::Evaluate(const FunctionCallbackInfo<Value>& args) {
790
806
}
791
807
}
792
808
793
- void ModuleWrap::InstantiateSync (const FunctionCallbackInfo<Value>& args) {
794
- Realm* realm = Realm::GetCurrent (args);
795
- Isolate* isolate = args.GetIsolate ();
796
- ModuleWrap* obj;
797
- ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
798
- Local<Context> context = obj->context ();
799
- Local<Module> module = obj->module_ .Get (isolate);
800
- Environment* env = realm->env ();
801
-
802
- {
803
- TryCatchScope try_catch (env);
804
- USE (module ->InstantiateModule (
805
- context, ResolveModuleCallback, ResolveSourceCallback));
806
-
807
- if (try_catch.HasCaught () && !try_catch.HasTerminated ()) {
808
- CHECK (!try_catch.Message ().IsEmpty ());
809
- CHECK (!try_catch.Exception ().IsEmpty ());
810
- AppendExceptionLine (env,
811
- try_catch.Exception (),
812
- try_catch.Message (),
813
- ErrorHandlingMode::MODULE_ERROR);
814
- try_catch.ReThrow ();
815
- return ;
816
- }
817
- }
818
-
819
- // TODO(joyeecheung): record Module::HasTopLevelAwait() in every ModuleWrap
820
- // and infer the asynchronicity from a module's children during linking.
821
- args.GetReturnValue ().Set (module ->IsGraphAsync ());
822
- }
823
-
824
809
Maybe<void > ThrowIfPromiseRejected (Realm* realm, Local<Promise> promise) {
825
810
Isolate* isolate = realm->isolate ();
826
811
Local<Context> context = realm->context ();
@@ -886,7 +871,7 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
886
871
return ;
887
872
}
888
873
889
- if (module -> IsGraphAsync ()) {
874
+ if (obj-> HasAsyncGraph ()) {
890
875
CHECK (env->options ()->print_required_tla );
891
876
auto stalled_messages =
892
877
std::get<1 >(module ->GetStalledTopLevelAwaitMessages (isolate));
@@ -908,52 +893,15 @@ void ModuleWrap::EvaluateSync(const FunctionCallbackInfo<Value>& args) {
908
893
args.GetReturnValue ().Set (module ->GetModuleNamespace ());
909
894
}
910
895
911
- void ModuleWrap::GetNamespaceSync (const FunctionCallbackInfo<Value>& args) {
912
- Realm* realm = Realm::GetCurrent (args);
913
- Isolate* isolate = args.GetIsolate ();
914
- ModuleWrap* obj;
915
- ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
916
- Local<Module> module = obj->module_ .Get (isolate);
917
-
918
- switch (module ->GetStatus ()) {
919
- case Module::Status::kUninstantiated :
920
- case Module::Status::kInstantiating :
921
- return realm->env ()->ThrowError (
922
- " Cannot get namespace, module has not been instantiated" );
923
- case Module::Status::kInstantiated :
924
- case Module::Status::kEvaluating :
925
- case Module::Status::kEvaluated :
926
- case Module::Status::kErrored :
927
- break ;
928
- }
929
-
930
- if (module ->IsGraphAsync ()) {
931
- return THROW_ERR_REQUIRE_ASYNC_MODULE (realm->env (), args[0 ], args[1 ]);
932
- }
933
- Local<Value> result = module ->GetModuleNamespace ();
934
- args.GetReturnValue ().Set (result);
935
- }
936
-
937
896
void ModuleWrap::GetNamespace (const FunctionCallbackInfo<Value>& args) {
938
897
Realm* realm = Realm::GetCurrent (args);
939
898
Isolate* isolate = args.GetIsolate ();
940
899
ModuleWrap* obj;
941
900
ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
942
901
943
902
Local<Module> module = obj->module_ .Get (isolate);
944
-
945
- switch (module ->GetStatus ()) {
946
- case Module::Status::kUninstantiated :
947
- case Module::Status::kInstantiating :
948
- return realm->env ()->ThrowError (
949
- " cannot get namespace, module has not been instantiated" );
950
- case Module::Status::kInstantiated :
951
- case Module::Status::kEvaluating :
952
- case Module::Status::kEvaluated :
953
- case Module::Status::kErrored :
954
- break ;
955
- default :
956
- UNREACHABLE ();
903
+ if (module ->GetStatus () < Module::kInstantiated ) {
904
+ return THROW_ERR_MODULE_NOT_INSTANTIATED (realm->env ());
957
905
}
958
906
959
907
Local<Value> result = module ->GetModuleNamespace ();
@@ -1004,23 +952,28 @@ void ModuleWrap::GetStatus(const FunctionCallbackInfo<Value>& args) {
1004
952
args.GetReturnValue ().Set (module ->GetStatus ());
1005
953
}
1006
954
1007
- void ModuleWrap::IsGraphAsync (const FunctionCallbackInfo<Value>& args) {
955
+ void ModuleWrap::GetError (const FunctionCallbackInfo<Value>& args) {
1008
956
Isolate* isolate = args.GetIsolate ();
1009
957
ModuleWrap* obj;
1010
958
ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
1011
959
1012
960
Local<Module> module = obj->module_ .Get (isolate);
1013
-
1014
- args.GetReturnValue ().Set (module ->IsGraphAsync ());
961
+ args.GetReturnValue ().Set (module ->GetException ());
1015
962
}
1016
963
1017
- void ModuleWrap::GetError (const FunctionCallbackInfo<Value>& args) {
964
+ void ModuleWrap::HasAsyncGraph (Local<Name> property,
965
+ const PropertyCallbackInfo<Value>& args) {
1018
966
Isolate* isolate = args.GetIsolate ();
967
+ Environment* env = Environment::GetCurrent (isolate);
1019
968
ModuleWrap* obj;
1020
969
ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
1021
970
1022
971
Local<Module> module = obj->module_ .Get (isolate);
1023
- args.GetReturnValue ().Set (module ->GetException ());
972
+ if (module ->GetStatus () < Module::kInstantiated ) {
973
+ return THROW_ERR_MODULE_NOT_INSTANTIATED (env);
974
+ }
975
+
976
+ args.GetReturnValue ().Set (obj->HasAsyncGraph ());
1024
977
}
1025
978
1026
979
// static
@@ -1424,10 +1377,8 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
1424
1377
1425
1378
SetProtoMethod (isolate, tpl, " link" , Link);
1426
1379
SetProtoMethod (isolate, tpl, " getModuleRequests" , GetModuleRequests);
1427
- SetProtoMethod (isolate, tpl, " instantiateSync" , InstantiateSync);
1428
- SetProtoMethod (isolate, tpl, " evaluateSync" , EvaluateSync);
1429
- SetProtoMethod (isolate, tpl, " getNamespaceSync" , GetNamespaceSync);
1430
1380
SetProtoMethod (isolate, tpl, " instantiate" , Instantiate);
1381
+ SetProtoMethod (isolate, tpl, " evaluateSync" , EvaluateSync);
1431
1382
SetProtoMethod (isolate, tpl, " evaluate" , Evaluate);
1432
1383
SetProtoMethod (isolate, tpl, " setExport" , SetSyntheticExport);
1433
1384
SetProtoMethod (isolate, tpl, " setModuleSourceObject" , SetModuleSourceObject);
@@ -1436,9 +1387,12 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
1436
1387
isolate, tpl, " createCachedData" , CreateCachedData);
1437
1388
SetProtoMethodNoSideEffect (isolate, tpl, " getNamespace" , GetNamespace);
1438
1389
SetProtoMethodNoSideEffect (isolate, tpl, " getStatus" , GetStatus);
1439
- SetProtoMethodNoSideEffect (isolate, tpl, " isGraphAsync" , IsGraphAsync);
1440
1390
SetProtoMethodNoSideEffect (isolate, tpl, " getError" , GetError);
1441
1391
SetConstructorFunction (isolate, target, " ModuleWrap" , tpl);
1392
+
1393
+ tpl->InstanceTemplate ()->SetLazyDataProperty (
1394
+ FIXED_ONE_BYTE_STRING (isolate, " hasAsyncGraph" ), HasAsyncGraph);
1395
+
1442
1396
isolate_data->set_module_wrap_constructor_template (tpl);
1443
1397
1444
1398
SetMethod (isolate,
@@ -1486,9 +1440,7 @@ void ModuleWrap::RegisterExternalReferences(
1486
1440
1487
1441
registry->Register (Link);
1488
1442
registry->Register (GetModuleRequests);
1489
- registry->Register (InstantiateSync);
1490
1443
registry->Register (EvaluateSync);
1491
- registry->Register (GetNamespaceSync);
1492
1444
registry->Register (Instantiate);
1493
1445
registry->Register (Evaluate);
1494
1446
registry->Register (SetSyntheticExport);
@@ -1498,7 +1450,7 @@ void ModuleWrap::RegisterExternalReferences(
1498
1450
registry->Register (GetNamespace);
1499
1451
registry->Register (GetStatus);
1500
1452
registry->Register (GetError);
1501
- registry->Register (IsGraphAsync );
1453
+ registry->Register (HasAsyncGraph );
1502
1454
1503
1455
registry->Register (CreateRequiredModuleFacade);
1504
1456
0 commit comments