Skip to content

Commit 21bcd0e

Browse files
joyeecheungaduh95
authored andcommitted
deps: V8: cherry-pick 3d0f462a17ff
Original commit message: [api] Add index-based module resolution in InstantiateModule() Add new InstantiateModule() overload that allows embedders to identify modules requests by index in the module requests array rather than using specifier and import attributes. When embedders want to fetch all the modules using information from module->GetModuleRequests() before calling InstantiateModule() instead of having to do the fetching inside the InstantiateModule() callback, they could just maintain a simple array of modules indexed by module request positions and look up the fetched the module by index in the new callback. Previously this has to be done by mapping from specifier and import attributes to module objects cached on the embedder side, leading to an overhead to hash the specifier and import attributes for each module request. Refs: #59396 Bug: 435317398 Change-Id: Ie017d2f3ccc605e0f58aa423504b5fa5fdbcc633 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6804466 Commit-Queue: Joyee Cheung <[email protected]> Reviewed-by: Camillo Bruni <[email protected]> Cr-Commit-Position: refs/heads/main@{#102613} Refs: v8/v8@3d0f462 PR-URL: #59396 Refs: https://chromium-review.googlesource.com/c/v8/v8/+/6804466 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent f1aa1ea commit 21bcd0e

File tree

8 files changed

+386
-34
lines changed

8 files changed

+386
-34
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
# Reset this number to 0 on major V8 upgrades.
4040
# Increment by one for each non-official patch applied to deps/v8.
41-
'v8_embedder_string': '-node.10',
41+
'v8_embedder_string': '-node.11',
4242

4343
##### V8 defaults for Node.js #####
4444

deps/v8/include/v8-script.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ class V8_EXPORT Module : public Data {
220220
Local<Context> context, Local<String> specifier,
221221
Local<FixedArray> import_attributes, Local<Module> referrer);
222222

223+
using ResolveModuleByIndexCallback = MaybeLocal<Module> (*)(
224+
Local<Context> context, size_t module_request_index,
225+
Local<Module> referrer);
226+
using ResolveSourceByIndexCallback = MaybeLocal<Object> (*)(
227+
Local<Context> context, size_t module_request_index,
228+
Local<Module> referrer);
229+
223230
/**
224231
* Instantiates the module and its dependencies.
225232
*
@@ -231,6 +238,16 @@ class V8_EXPORT Module : public Data {
231238
Local<Context> context, ResolveModuleCallback module_callback,
232239
ResolveSourceCallback source_callback = nullptr);
233240

241+
/**
242+
* Similar to the variant that takes ResolveModuleCallback and
243+
* ResolveSourceCallback, but uses the index into the array that is returned
244+
* by GetModuleRequests() instead of the specifier and import attributes to
245+
* identify the requests.
246+
*/
247+
V8_WARN_UNUSED_RESULT Maybe<bool> InstantiateModule(
248+
Local<Context> context, ResolveModuleByIndexCallback module_callback,
249+
ResolveSourceByIndexCallback source_callback = nullptr);
250+
234251
/**
235252
* Evaluates the module and its dependencies.
236253
*

deps/v8/src/api/api.cc

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2266,14 +2266,34 @@ int Module::GetIdentityHash() const {
22662266
return self->hash();
22672267
}
22682268

2269+
Maybe<bool> Module::InstantiateModule(
2270+
Local<Context> context, ResolveModuleByIndexCallback module_callback,
2271+
ResolveSourceByIndexCallback source_callback) {
2272+
auto i_isolate = i::Isolate::Current();
2273+
EnterV8Scope<> api_scope{i_isolate, context,
2274+
RCCId::kAPI_Module_InstantiateModule};
2275+
2276+
i::Module::UserResolveCallbacks callbacks;
2277+
callbacks.module_callback_by_index = module_callback;
2278+
callbacks.source_callback_by_index = source_callback;
2279+
if (!i::Module::Instantiate(i_isolate, Utils::OpenHandle(this), context,
2280+
callbacks)) {
2281+
return {};
2282+
}
2283+
return Just(true);
2284+
}
2285+
22692286
Maybe<bool> Module::InstantiateModule(Local<Context> context,
22702287
ResolveModuleCallback module_callback,
22712288
ResolveSourceCallback source_callback) {
22722289
auto i_isolate = i::Isolate::Current();
22732290
EnterV8Scope<> api_scope{i_isolate, context,
22742291
RCCId::kAPI_Module_InstantiateModule};
2292+
i::Module::UserResolveCallbacks callbacks;
2293+
callbacks.module_callback = module_callback;
2294+
callbacks.source_callback = source_callback;
22752295
if (!i::Module::Instantiate(i_isolate, Utils::OpenHandle(this), context,
2276-
module_callback, source_callback)) {
2296+
callbacks)) {
22772297
return {};
22782298
}
22792299
return Just(true);

deps/v8/src/objects/module.cc

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,12 @@ MaybeHandle<Cell> Module::ResolveExport(Isolate* isolate, Handle<Module> module,
205205

206206
bool Module::Instantiate(Isolate* isolate, Handle<Module> module,
207207
v8::Local<v8::Context> context,
208-
v8::Module::ResolveModuleCallback module_callback,
209-
v8::Module::ResolveSourceCallback source_callback) {
208+
const Module::UserResolveCallbacks& callbacks) {
210209
#ifdef DEBUG
211210
PrintStatusMessage(*module, "Instantiating module ");
212211
#endif // DEBUG
213212

214-
if (!PrepareInstantiate(isolate, module, context, module_callback,
215-
source_callback)) {
213+
if (!PrepareInstantiate(isolate, module, context, callbacks)) {
216214
ResetGraph(isolate, module);
217215
DCHECK_EQ(module->status(), kUnlinked);
218216
return false;
@@ -231,11 +229,9 @@ bool Module::Instantiate(Isolate* isolate, Handle<Module> module,
231229
return true;
232230
}
233231

234-
bool Module::PrepareInstantiate(
235-
Isolate* isolate, DirectHandle<Module> module,
236-
v8::Local<v8::Context> context,
237-
v8::Module::ResolveModuleCallback module_callback,
238-
v8::Module::ResolveSourceCallback source_callback) {
232+
bool Module::PrepareInstantiate(Isolate* isolate, DirectHandle<Module> module,
233+
v8::Local<v8::Context> context,
234+
const UserResolveCallbacks& callbacks) {
239235
DCHECK_NE(module->status(), kEvaluating);
240236
DCHECK_NE(module->status(), kLinking);
241237
if (module->status() >= kPreLinking) return true;
@@ -244,8 +240,7 @@ bool Module::PrepareInstantiate(
244240

245241
if (IsSourceTextModule(*module)) {
246242
return SourceTextModule::PrepareInstantiate(
247-
isolate, Cast<SourceTextModule>(module), context, module_callback,
248-
source_callback);
243+
isolate, Cast<SourceTextModule>(module), context, callbacks);
249244
} else {
250245
return SyntheticModule::PrepareInstantiate(
251246
isolate, Cast<SyntheticModule>(module), context);

deps/v8/src/objects/module.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,20 @@ class Module : public TorqueGeneratedModule<Module, HeapObject> {
5757
// i.e. has a top-level await.
5858
V8_WARN_UNUSED_RESULT bool IsGraphAsync(Isolate* isolate) const;
5959

60+
struct UserResolveCallbacks {
61+
v8::Module::ResolveModuleCallback module_callback = nullptr;
62+
v8::Module::ResolveSourceCallback source_callback = nullptr;
63+
v8::Module::ResolveModuleByIndexCallback module_callback_by_index = nullptr;
64+
v8::Module::ResolveSourceByIndexCallback source_callback_by_index = nullptr;
65+
};
66+
6067
// Implementation of spec operation ModuleDeclarationInstantiation.
6168
// Returns false if an exception occurred during instantiation, true
6269
// otherwise. (In the case where the callback throws an exception, that
6370
// exception is propagated.)
6471
static V8_WARN_UNUSED_RESULT bool Instantiate(
6572
Isolate* isolate, Handle<Module> module, v8::Local<v8::Context> context,
66-
v8::Module::ResolveModuleCallback module_callback,
67-
v8::Module::ResolveSourceCallback source_callback);
73+
const UserResolveCallbacks& callbacks);
6874

6975
// Implementation of spec operation ModuleEvaluation.
7076
static V8_WARN_UNUSED_RESULT MaybeDirectHandle<Object> Evaluate(
@@ -99,9 +105,7 @@ class Module : public TorqueGeneratedModule<Module, HeapObject> {
99105

100106
static V8_WARN_UNUSED_RESULT bool PrepareInstantiate(
101107
Isolate* isolate, DirectHandle<Module> module,
102-
v8::Local<v8::Context> context,
103-
v8::Module::ResolveModuleCallback module_callback,
104-
v8::Module::ResolveSourceCallback source_callback);
108+
v8::Local<v8::Context> context, const UserResolveCallbacks& callbacks);
105109
static V8_WARN_UNUSED_RESULT bool FinishInstantiate(
106110
Isolate* isolate, Handle<Module> module,
107111
ZoneForwardList<Handle<SourceTextModule>>* stack, unsigned* dfs_index,

deps/v8/src/objects/source-text-module.cc

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,10 @@ MaybeHandle<Cell> SourceTextModule::ResolveExportUsingStarExports(
346346
bool SourceTextModule::PrepareInstantiate(
347347
Isolate* isolate, DirectHandle<SourceTextModule> module,
348348
v8::Local<v8::Context> context,
349-
v8::Module::ResolveModuleCallback module_callback,
350-
v8::Module::ResolveSourceCallback source_callback) {
351-
DCHECK_NE(module_callback, nullptr);
349+
const Module::UserResolveCallbacks& callbacks) {
350+
// One of the callbacks must be set, otherwise we cannot resolve.
351+
DCHECK_IMPLIES(callbacks.module_callback == nullptr,
352+
callbacks.module_callback_by_index != nullptr);
352353
// Obtain requested modules.
353354
DirectHandle<SourceTextModuleInfo> module_info(module->info(), isolate);
354355
DirectHandle<FixedArray> module_requests(module_info->module_requests(),
@@ -364,11 +365,23 @@ bool SourceTextModule::PrepareInstantiate(
364365
switch (module_request->phase()) {
365366
case ModuleImportPhase::kEvaluation: {
366367
v8::Local<v8::Module> api_requested_module;
367-
if (!module_callback(context, v8::Utils::ToLocal(specifier),
368-
v8::Utils::FixedArrayToLocal(import_attributes),
369-
v8::Utils::ToLocal(Cast<Module>(module)))
370-
.ToLocal(&api_requested_module)) {
371-
return false;
368+
if (callbacks.module_callback != nullptr) {
369+
if (!callbacks
370+
.module_callback(
371+
context, v8::Utils::ToLocal(specifier),
372+
v8::Utils::FixedArrayToLocal(import_attributes),
373+
v8::Utils::ToLocal(Cast<Module>(module)))
374+
.ToLocal(&api_requested_module)) {
375+
return false;
376+
}
377+
} else {
378+
DCHECK_NOT_NULL(callbacks.module_callback_by_index);
379+
if (!callbacks
380+
.module_callback_by_index(
381+
context, i, v8::Utils::ToLocal(Cast<Module>(module)))
382+
.ToLocal(&api_requested_module)) {
383+
return false;
384+
}
372385
}
373386
DirectHandle<Module> requested_module =
374387
Utils::OpenDirectHandle(*api_requested_module);
@@ -378,11 +391,23 @@ bool SourceTextModule::PrepareInstantiate(
378391
case ModuleImportPhase::kSource: {
379392
DCHECK(v8_flags.js_source_phase_imports);
380393
v8::Local<v8::Object> api_requested_module_source;
381-
if (!source_callback(context, v8::Utils::ToLocal(specifier),
382-
v8::Utils::FixedArrayToLocal(import_attributes),
383-
v8::Utils::ToLocal(Cast<Module>(module)))
384-
.ToLocal(&api_requested_module_source)) {
385-
return false;
394+
if (callbacks.source_callback != nullptr) {
395+
if (!callbacks
396+
.source_callback(
397+
context, v8::Utils::ToLocal(specifier),
398+
v8::Utils::FixedArrayToLocal(import_attributes),
399+
v8::Utils::ToLocal(Cast<Module>(module)))
400+
.ToLocal(&api_requested_module_source)) {
401+
return false;
402+
}
403+
} else {
404+
DCHECK_NOT_NULL(callbacks.source_callback_by_index);
405+
if (!callbacks
406+
.source_callback_by_index(
407+
context, i, v8::Utils::ToLocal(Cast<Module>(module)))
408+
.ToLocal(&api_requested_module_source)) {
409+
return false;
410+
}
386411
}
387412
DirectHandle<JSReceiver> requested_module_source =
388413
Utils::OpenDirectHandle(*api_requested_module_source);
@@ -404,7 +429,7 @@ bool SourceTextModule::PrepareInstantiate(
404429
DirectHandle<Module> requested_module(
405430
Cast<Module>(requested_modules->get(i)), isolate);
406431
if (!Module::PrepareInstantiate(isolate, requested_module, context,
407-
module_callback, source_callback)) {
432+
callbacks)) {
408433
return false;
409434
}
410435
}

deps/v8/src/objects/source-text-module.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ class SourceTextModule
172172
static V8_WARN_UNUSED_RESULT bool PrepareInstantiate(
173173
Isolate* isolate, DirectHandle<SourceTextModule> module,
174174
v8::Local<v8::Context> context,
175-
v8::Module::ResolveModuleCallback module_callback,
176-
v8::Module::ResolveSourceCallback source_callback);
175+
const Module::UserResolveCallbacks& callbacks);
177176
static V8_WARN_UNUSED_RESULT bool FinishInstantiate(
178177
Isolate* isolate, Handle<SourceTextModule> module,
179178
ZoneForwardList<Handle<SourceTextModule>>* stack, unsigned* dfs_index,

0 commit comments

Comments
 (0)