| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d785929 commit ff13d1d
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -359,7 +359,6 @@ class BuiltinModule { | |||
| 359 | 359 | this.setExport('default', builtin.exports); | |
| 360 | 360 | }); | |
| 361 | 361 | // Ensure immediate sync execution to capture exports now | |
| 362 | - this.module.link([]); | ||
| 363 | 362 | this.module.instantiate(); | |
| 364 | 363 | this.module.evaluate(-1, false); | |
| 365 | 364 | return this.module; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -393,13 +393,14 @@ class ModuleLoader { | |||
| 393 | 393 | if (!job.module) { | |
| 394 | 394 | assert.fail(getRaceMessage(filename, parentFilename)); | |
| 395 | 395 | } | |
| 396 | - if (job.module.hasAsyncGraph) { | ||
| 397 | - throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename); | ||
| 398 | - } | ||
| 399 | 396 | const status = job.module.getStatus(); | |
| 400 | 397 | debug('Module status', job, status); | |
| 398 | + // hasAsyncGraph is available after module been instantiated. | ||
| 399 | + if (status >= kInstantiated && job.module.hasAsyncGraph) { | ||
| 400 | + throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename); | ||
| 401 | + } | ||
| 401 | 402 | if (status === kEvaluated) { | |
| 402 | - return { wrap: job.module, namespace: job.module.getNamespaceSync(filename, parentFilename) }; | ||
| 403 | + return { wrap: job.module, namespace: job.module.getNamespace() }; | ||
| 403 | 404 | } else if (status === kInstantiated) { | |
| 404 | 405 | // When it's an async job cached by another import request, | |
| 405 | 406 | // which has finished linking but has not started its | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -324,17 +324,18 @@ class ModuleJob extends ModuleJobBase { | |||
| 324 | 324 | let status = this.module.getStatus(); | |
| 325 | 325 | ||
| 326 | 326 | debug('ModuleJob.runSync()', status, this.module); | |
| 327 | - // FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking | ||
| 328 | - // fully synchronous instead. | ||
| 329 | 327 | if (status === kUninstantiated) { | |
| 330 | - this.module.hasAsyncGraph = this.module.instantiateSync(); | ||
| 328 | + // FIXME(joyeecheung): this cannot fully handle < kInstantiated. Make the linking | ||
| 329 | + // fully synchronous instead. | ||
| 330 | + if (this.module.getModuleRequests().length === 0) { | ||
| 331 | + this.module.link([]); | ||
| 332 | + } | ||
| 333 | + this.module.instantiate(); | ||
| 331 | 334 | status = this.module.getStatus(); | |
| 332 | 335 | } | |
| 333 | 336 | if (status === kInstantiated || status === kErrored) { | |
| 334 | 337 | const filename = urlToFilename(this.url); | |
| 335 | 338 | const parentFilename = urlToFilename(parent?.filename); | |
| 336 | - this.module.hasAsyncGraph ??= this.module.isGraphAsync(); | ||
| 337 | - | ||
| 338 | 339 | if (this.module.hasAsyncGraph && !getOptionValue('--experimental-print-required-tla')) { | |
| 339 | 340 | throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename); | |
| 340 | 341 | } | |
@@ -345,14 +346,19 @@ class ModuleJob extends ModuleJobBase { | |||
| 345 | 346 | } | |
| 346 | 347 | throw this.module.getError(); | |
| 347 | 348 | } else if (status === kEvaluating || status === kEvaluated) { | |
| 349 | + if (this.module.hasAsyncGraph) { | ||
| 350 | + const filename = urlToFilename(this.url); | ||
| 351 | + const parentFilename = urlToFilename(parent?.filename); | ||
| 352 | + throw new ERR_REQUIRE_ASYNC_MODULE(filename, parentFilename); | ||
| 353 | + } | ||
| 348 | 354 | // kEvaluating can show up when this is being used to deal with CJS <-> CJS cycles. | |
| 349 | 355 | // Allow it for now, since we only need to ban ESM <-> CJS cycles which would be | |
| 350 | 356 | // detected earlier during the linking phase, though the CJS handling in the ESM | |
| 351 | 357 | // loader won't be able to emit warnings on pending circular exports like what | |
| 352 | 358 | // the CJS loader does. | |
| 353 | 359 | // TODO(joyeecheung): remove the re-invented require() in the ESM loader and | |
| 354 | 360 | // always handle CJS using the CJS loader to eliminate the quirks. | |
| 355 | - return { __proto__: null, module: this.module, namespace: this.module.getNamespaceSync() }; | ||
| 361 | + return { __proto__: null, module: this.module, namespace: this.module.getNamespace() }; | ||
| 356 | 362 | } | |
| 357 | 363 | assert.fail(`Unexpected module status ${status}.`); | |
| 358 | 364 | } | |
@@ -472,7 +478,7 @@ class ModuleJobSync extends ModuleJobBase { | |||
| 472 | 478 | } | |
| 473 | 479 | return { __proto__: null, module: this.module }; | |
| 474 | 480 | } else if (status === kInstantiated) { | |
| 475 | - // The evaluation may have been canceled because instantiateSync() detected TLA first. | ||
| 481 | + // The evaluation may have been canceled because instantiate() detected TLA first. | ||
| 476 | 482 | // But when it is imported again, it's fine to re-evaluate it asynchronously. | |
| 477 | 483 | const timeout = -1; | |
| 478 | 484 | const breakOnSigint = false; | |
@@ -490,7 +496,7 @@ class ModuleJobSync extends ModuleJobBase { | |||
| 490 | 496 | debug('ModuleJobSync.runSync()', this.module); | |
| 491 | 497 | assert(this.phase === kEvaluationPhase); | |
| 492 | 498 | // TODO(joyeecheung): add the error decoration logic from the async instantiate. | |
| 493 | - this.module.hasAsyncGraph = this.module.instantiateSync(); | ||
| 499 | + this.module.instantiate(); | ||
| 494 | 500 | // If --experimental-print-required-tla is true, proceeds to evaluation even | |
| 495 | 501 | // if it's async because we want to search for the TLA and help users locate | |
| 496 | 502 | // them. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -469,7 +469,6 @@ class SyntheticModule extends Module { | |||
| 469 | 469 | identifier, | |
| 470 | 470 | }); | |
| 471 | 471 | // A synthetic module does not have dependencies. | |
| 472 | - this[kWrap].link([]); | ||
| 473 | 472 | this[kWrap].instantiate(); | |
| 474 | 473 | } | |
| 475 | 474 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments