Commit 9bccbee4 authored by Daniel Clark's avatar Daniel Clark Committed by Commit Bot

[modules] Allow Module::GetModuleRequests() to be called on a SyntheticModule

I'd implemented Module::GetModuleRequests() such that it can only be
called on a SourceTextModuleRecord, forgetting that one of the APIs
it replaces, Module::GetModuleRequestsLength(), could be called on a
synthetic module.  The old behavior allowed embedders to write code that
iterates over a module's requests without needing to handle synthetic
modules as a special case.  GetModuleRequestsLength() would just return
0 for all synthetic modules and the code to process the requests
would be skipped seamlessly for them.

With the new GetModuleRequests() API, this would no longer be possible,
and embedders would explicitly need to check IsSyntheticModule()
before calling it.  Thus, to reach parity with the old API, this change
allows embedders to call GetModuleRequests() on a synthetic module,
which will always result in an empty FixedArray.

Bug: v8:10958
Change-Id: I6024261fe46d18fa7acc83b0ec8f69d6af21b28d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2570146Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: Dan Clark <daniec@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#71599}
parent 13e6a318
......@@ -2316,14 +2316,18 @@ Location Module::GetModuleRequestLocation(int i) const {
Local<FixedArray> Module::GetModuleRequests() const {
i::Handle<i::Module> self = Utils::OpenHandle(this);
Utils::ApiCheck(
self->IsSourceTextModule(), "v8::Module::GetModuleRequests",
"v8::Module::GetModuleRequests must be used on an SourceTextModule");
i::Isolate* isolate = self->GetIsolate();
i::Handle<i::FixedArray> module_requests(
i::Handle<i::SourceTextModule>::cast(self)->info().module_requests(),
isolate);
return ToApiHandle<FixedArray>(module_requests);
if (self->IsSyntheticModule()) {
// Synthetic modules are leaf nodes in the module graph. They have no
// ModuleRequests.
return ToApiHandle<FixedArray>(
self->GetReadOnlyRoots().empty_fixed_array_handle());
} else {
i::Isolate* isolate = self->GetIsolate();
i::Handle<i::FixedArray> module_requests(
i::Handle<i::SourceTextModule>::cast(self)->info().module_requests(),
isolate);
return ToApiHandle<FixedArray>(module_requests);
}
}
Location Module::SourceOffsetToLocation(int offset) const {
......
......@@ -24258,6 +24258,7 @@ TEST(CreateSyntheticModule) {
CHECK_EQ(i_module->status(), i::Module::kInstantiated);
CHECK(module->IsSyntheticModule());
CHECK(!module->IsSourceTextModule());
CHECK_EQ(module->GetModuleRequests()->Length(), 0);
}
TEST(CreateSyntheticModuleGC) {
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment