@@ -343,136 +343,105 @@ static Local<Object> createImportAttributesContainer(
343
343
Local<FixedArray> raw_attributes,
344
344
const int elements_per_attribute) {
345
345
CHECK_EQ (raw_attributes->Length () % elements_per_attribute, 0 );
346
- Local<Object> attributes =
347
- Object::New (isolate, v8::Null (isolate), nullptr , nullptr , 0 );
346
+ size_t num_attributes = raw_attributes->Length () / elements_per_attribute;
347
+ std::vector<Local<v8::Name>> names (num_attributes);
348
+ std::vector<Local<v8::Value>> values (num_attributes);
349
+
348
350
for (int i = 0 ; i < raw_attributes->Length (); i += elements_per_attribute) {
349
- attributes
350
- ->Set (realm->context (),
351
- raw_attributes->Get (realm->context (), i).As <String>(),
352
- raw_attributes->Get (realm->context (), i + 1 ).As <Value>())
353
- .ToChecked ();
351
+ int idx = i / elements_per_attribute;
352
+ names[idx] = raw_attributes->Get (realm->context (), i).As <v8::Name>();
353
+ values[idx] = raw_attributes->Get (realm->context (), i + 1 ).As <Value>();
354
354
}
355
355
356
- return attributes;
356
+ return Object::New (
357
+ isolate, v8::Null (isolate), names.data (), values.data (), num_attributes);
357
358
}
358
359
359
- void ModuleWrap::GetModuleRequestsSync (
360
- const FunctionCallbackInfo<Value>& args) {
361
- Realm* realm = Realm::GetCurrent (args);
362
- Isolate* isolate = args.GetIsolate ();
363
-
364
- Local<Object> that = args.This ();
365
-
366
- ModuleWrap* obj;
367
- ASSIGN_OR_RETURN_UNWRAP (&obj, that);
368
-
369
- CHECK (!obj->linked_ );
360
+ static Local<Array> createModuleRequestsContainer (
361
+ Realm* realm, Isolate* isolate, Local<FixedArray> raw_requests) {
362
+ std::vector<Local<Value>> requests (raw_requests->Length ());
370
363
371
- Local<Module> module = obj->module_ .Get (isolate);
372
- Local<FixedArray> module_requests = module->GetModuleRequests ();
373
- const int module_requests_length = module_requests->Length ();
374
-
375
- std::vector<Local<Value>> requests;
376
- requests.reserve (module_requests_length);
377
- // call the dependency resolve callbacks
378
- for (int i = 0 ; i < module_requests_length; i++) {
364
+ for (int i = 0 ; i < raw_requests->Length (); i++) {
379
365
Local<ModuleRequest> module_request =
380
- module_requests->Get (realm->context (), i).As <ModuleRequest>();
381
- Local<FixedArray> raw_attributes = module_request->GetImportAssertions ();
382
- std::vector<Local<Value>> request = {
383
- module_request->GetSpecifier (),
384
- createImportAttributesContainer (realm, isolate, raw_attributes, 3 ),
385
- };
386
- requests.push_back (Array::New (isolate, request.data (), request.size ()));
387
- }
366
+ raw_requests->Get (realm->context (), i).As <ModuleRequest>();
388
367
389
- args.GetReturnValue ().Set (
390
- Array::New (isolate, requests.data (), requests.size ()));
391
- }
392
-
393
- void ModuleWrap::CacheResolvedWrapsSync (
394
- const FunctionCallbackInfo<Value>& args) {
395
- Isolate* isolate = args.GetIsolate ();
368
+ Local<String> specifier = module_request->GetSpecifier ();
396
369
397
- CHECK_EQ (args.Length (), 3 );
398
- CHECK (args[0 ]->IsString ());
399
- CHECK (args[1 ]->IsPromise ());
400
- CHECK (args[2 ]->IsBoolean ());
370
+ // Contains the import assertions for this request in the form:
371
+ // [key1, value1, source_offset1, key2, value2, source_offset2, ...].
372
+ Local<FixedArray> raw_attributes = module_request->GetImportAssertions ();
373
+ Local<Object> attributes =
374
+ createImportAttributesContainer (realm, isolate, raw_attributes, 3 );
401
375
402
- ModuleWrap* dependent;
403
- ASSIGN_OR_RETURN_UNWRAP (&dependent, args.This ());
376
+ Local<v8::Name> names[] = {
377
+ realm->isolate_data ()->specifier_string (),
378
+ realm->isolate_data ()->attributes_string (),
379
+ };
380
+ Local<Value> values[] = {
381
+ specifier,
382
+ attributes,
383
+ };
384
+ DCHECK_EQ (arraysize (names), arraysize (values));
404
385
405
- Utf8Value specifier (isolate, args[0 ]);
406
- dependent->resolve_cache_ [specifier.ToString ()].Reset (isolate,
407
- args[1 ].As <Promise>());
386
+ Local<Object> request = Object::New (
387
+ isolate, v8::Null (isolate), names, values, arraysize (names));
408
388
409
- if (args[2 ].As <v8::Boolean >()->Value ()) {
410
- dependent->linked_ = true ;
389
+ requests[i] = request;
411
390
}
391
+
392
+ return Array::New (isolate, requests.data (), requests.size ());
412
393
}
413
394
414
- void ModuleWrap::Link (const FunctionCallbackInfo<Value>& args) {
395
+ void ModuleWrap::GetModuleRequests (const FunctionCallbackInfo<Value>& args) {
415
396
Realm* realm = Realm::GetCurrent (args);
416
397
Isolate* isolate = args.GetIsolate ();
417
-
418
- CHECK_EQ (args.Length (), 1 );
419
- CHECK (args[0 ]->IsFunction ());
420
-
421
398
Local<Object> that = args.This ();
422
399
423
400
ModuleWrap* obj;
424
401
ASSIGN_OR_RETURN_UNWRAP (&obj, that);
425
402
426
- if (obj->linked_ )
427
- return ;
428
- obj->linked_ = true ;
403
+ Local<Module> module = obj->module_ .Get (isolate);
404
+ args.GetReturnValue ().Set (createModuleRequestsContainer (
405
+ realm, isolate, module->GetModuleRequests ()));
406
+ }
429
407
430
- Local<Function> resolver_arg = args[0 ].As <Function>();
408
+ // moduleWrap.link(specifiers, moduleWraps)
409
+ void ModuleWrap::Link (const FunctionCallbackInfo<Value>& args) {
410
+ Realm* realm = Realm::GetCurrent (args);
411
+ Isolate* isolate = args.GetIsolate ();
412
+ Local<Context> context = realm->context ();
431
413
432
- Local<Context> mod_context = obj-> context () ;
433
- Local<Module> module = obj-> module_ . Get (isolate );
414
+ ModuleWrap* dependent ;
415
+ ASSIGN_OR_RETURN_UNWRAP (&dependent, args. This () );
434
416
435
- Local<FixedArray> module_requests = module->GetModuleRequests ();
436
- const int module_requests_length = module_requests->Length ();
437
- MaybeStackBuffer<Local<Value>, 16 > promises (module_requests_length);
417
+ CHECK_EQ (args.Length (), 2 );
438
418
439
- // call the dependency resolve callbacks
440
- for (int i = 0 ; i < module_requests_length; i++) {
441
- Local<ModuleRequest> module_request =
442
- module_requests->Get (realm->context (), i).As <ModuleRequest>();
443
- Local<String> specifier = module_request->GetSpecifier ();
444
- Utf8Value specifier_utf8 (realm->isolate (), specifier);
445
- std::string specifier_std (*specifier_utf8, specifier_utf8.length ());
419
+ Local<Array> specifiers = args[0 ].As <Array>();
420
+ Local<Array> modules = args[1 ].As <Array>();
421
+ CHECK_EQ (specifiers->Length (), modules->Length ());
446
422
447
- Local<FixedArray> raw_attributes = module_request->GetImportAssertions ();
448
- Local<Object> attributes =
449
- createImportAttributesContainer (realm, isolate, raw_attributes, 3 );
423
+ std::vector<v8::Global<Value>> specifiers_buffer;
424
+ if (FromV8Array (context, specifiers, &specifiers_buffer).IsNothing ()) {
425
+ return ;
426
+ }
427
+ std::vector<v8::Global<Value>> modules_buffer;
428
+ if (FromV8Array (context, modules, &modules_buffer).IsNothing ()) {
429
+ return ;
430
+ }
450
431
451
- Local<Value> argv[] = {
452
- specifier,
453
- attributes,
454
- } ;
432
+ for ( uint32_t i = 0 ; i < specifiers-> Length (); i++) {
433
+ Local<String> specifier_str =
434
+ specifiers_buffer[i]. Get (isolate). As <String>();
435
+ Local<Object> module_object = modules_buffer[i]. Get (isolate). As <Object>() ;
455
436
456
- MaybeLocal<Value> maybe_resolve_return_value =
457
- resolver_arg->Call (mod_context, that, arraysize (argv), argv);
458
- if (maybe_resolve_return_value.IsEmpty ()) {
459
- return ;
460
- }
461
- Local<Value> resolve_return_value =
462
- maybe_resolve_return_value.ToLocalChecked ();
463
- if (!resolve_return_value->IsPromise ()) {
464
- THROW_ERR_VM_MODULE_LINK_FAILURE (
465
- realm, " request for '%s' did not return promise" , specifier_std);
466
- return ;
467
- }
468
- Local<Promise> resolve_promise = resolve_return_value.As <Promise>();
469
- obj->resolve_cache_ [specifier_std].Reset (isolate, resolve_promise);
437
+ CHECK (
438
+ realm->isolate_data ()->module_wrap_constructor_template ()->HasInstance (
439
+ module_object));
470
440
471
- promises[i] = resolve_promise;
441
+ Utf8Value specifier (isolate, specifier_str);
442
+ dependent->resolve_cache_ [specifier.ToString ()].Reset (isolate,
443
+ module_object);
472
444
}
473
-
474
- args.GetReturnValue ().Set (
475
- Array::New (isolate, promises.out (), promises.length ()));
476
445
}
477
446
478
447
void ModuleWrap::Instantiate (const FunctionCallbackInfo<Value>& args) {
@@ -733,29 +702,6 @@ void ModuleWrap::GetStatus(const FunctionCallbackInfo<Value>& args) {
733
702
args.GetReturnValue ().Set (module->GetStatus ());
734
703
}
735
704
736
- void ModuleWrap::GetStaticDependencySpecifiers (
737
- const FunctionCallbackInfo<Value>& args) {
738
- Realm* realm = Realm::GetCurrent (args);
739
- ModuleWrap* obj;
740
- ASSIGN_OR_RETURN_UNWRAP (&obj, args.This ());
741
-
742
- Local<Module> module = obj->module_ .Get (realm->isolate ());
743
-
744
- Local<FixedArray> module_requests = module->GetModuleRequests ();
745
- int count = module_requests->Length ();
746
-
747
- MaybeStackBuffer<Local<Value>, 16 > specifiers (count);
748
-
749
- for (int i = 0 ; i < count; i++) {
750
- Local<ModuleRequest> module_request =
751
- module_requests->Get (realm->context (), i).As <ModuleRequest>();
752
- specifiers[i] = module_request->GetSpecifier ();
753
- }
754
-
755
- args.GetReturnValue ().Set (
756
- Array::New (realm->isolate (), specifiers.out (), count));
757
- }
758
-
759
705
void ModuleWrap::GetError (const FunctionCallbackInfo<Value>& args) {
760
706
Isolate* isolate = args.GetIsolate ();
761
707
ModuleWrap* obj;
@@ -793,16 +739,8 @@ MaybeLocal<Module> ModuleWrap::ResolveModuleCallback(
793
739
return MaybeLocal<Module>();
794
740
}
795
741
796
- Local<Promise> resolve_promise =
742
+ Local<Object> module_object =
797
743
dependent->resolve_cache_ [specifier_std].Get (isolate);
798
-
799
- if (resolve_promise->State () != Promise::kFulfilled ) {
800
- THROW_ERR_VM_MODULE_LINK_FAILURE (
801
- env, " request for '%s' is not yet fulfilled" , specifier_std);
802
- return MaybeLocal<Module>();
803
- }
804
-
805
- Local<Object> module_object = resolve_promise->Result ().As <Object>();
806
744
if (module_object.IsEmpty () || !module_object->IsObject ()) {
807
745
THROW_ERR_VM_MODULE_LINK_FAILURE (
808
746
env, " request for '%s' did not return an object" , specifier_std);
@@ -1029,9 +967,7 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
1029
967
ModuleWrap::kInternalFieldCount );
1030
968
1031
969
SetProtoMethod (isolate, tpl, " link" , Link);
1032
- SetProtoMethod (isolate, tpl, " getModuleRequestsSync" , GetModuleRequestsSync);
1033
- SetProtoMethod (
1034
- isolate, tpl, " cacheResolvedWrapsSync" , CacheResolvedWrapsSync);
970
+ SetProtoMethod (isolate, tpl, " getModuleRequests" , GetModuleRequests);
1035
971
SetProtoMethod (isolate, tpl, " instantiateSync" , InstantiateSync);
1036
972
SetProtoMethod (isolate, tpl, " evaluateSync" , EvaluateSync);
1037
973
SetProtoMethod (isolate, tpl, " getNamespaceSync" , GetNamespaceSync);
@@ -1043,12 +979,8 @@ void ModuleWrap::CreatePerIsolateProperties(IsolateData* isolate_data,
1043
979
SetProtoMethodNoSideEffect (isolate, tpl, " getNamespace" , GetNamespace);
1044
980
SetProtoMethodNoSideEffect (isolate, tpl, " getStatus" , GetStatus);
1045
981
SetProtoMethodNoSideEffect (isolate, tpl, " getError" , GetError);
1046
- SetProtoMethodNoSideEffect (isolate,
1047
- tpl,
1048
- " getStaticDependencySpecifiers" ,
1049
- GetStaticDependencySpecifiers);
1050
-
1051
982
SetConstructorFunction (isolate, target, " ModuleWrap" , tpl);
983
+ isolate_data->set_module_wrap_constructor_template (tpl);
1052
984
1053
985
SetMethod (isolate,
1054
986
target,
@@ -1086,8 +1018,7 @@ void ModuleWrap::RegisterExternalReferences(
1086
1018
registry->Register (New);
1087
1019
1088
1020
registry->Register (Link);
1089
- registry->Register (GetModuleRequestsSync);
1090
- registry->Register (CacheResolvedWrapsSync);
1021
+ registry->Register (GetModuleRequests);
1091
1022
registry->Register (InstantiateSync);
1092
1023
registry->Register (EvaluateSync);
1093
1024
registry->Register (GetNamespaceSync);
@@ -1098,7 +1029,6 @@ void ModuleWrap::RegisterExternalReferences(
1098
1029
registry->Register (GetNamespace);
1099
1030
registry->Register (GetStatus);
1100
1031
registry->Register (GetError);
1101
- registry->Register (GetStaticDependencySpecifiers);
1102
1032
1103
1033
registry->Register (SetImportModuleDynamicallyCallback);
1104
1034
registry->Register (SetInitializeImportMetaObjectCallback);
0 commit comments