Commit de05e68e authored by Alex Turner's avatar Alex Turner Committed by Commit Bot

[api] Add a ScriptId getter for Module objects

There is currently no API call that allows access to the id of the
script underlying a Module. As this function can only be used for
SourceTextModules, we also add IsSourceTextModule() and
IsSyntheticModule() to allow callers to distinguish them.

Bug: v8:10616
Change-Id: Ia55ea8e6993922b695019852e38ca54ffce32cbf
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2248199Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Commit-Queue: Alex Turner <alexmt@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68487}
parent e1a9c1e8
......@@ -1545,6 +1545,23 @@ class V8_EXPORT Module : public Data {
*/
Local<UnboundModuleScript> GetUnboundModuleScript();
/**
* Returns the underlying script's id.
*
* The module must be a SourceTextModule and must not have a kErrored status.
*/
int ScriptId();
/**
* Returns whether the module is a SourceTextModule.
*/
bool IsSourceTextModule() const;
/**
* Returns whether the module is a SyntheticModule.
*/
bool IsSyntheticModule() const;
/*
* Callback defined in the embedder. This is responsible for setting
* the module's exported values with calls to SetSyntheticModuleExport().
......
......@@ -2246,6 +2246,28 @@ Local<UnboundModuleScript> Module::GetUnboundModuleScript() {
self->GetIsolate()));
}
int Module::ScriptId() {
i::Handle<i::Module> self = Utils::OpenHandle(this);
Utils::ApiCheck(self->IsSourceTextModule(), "v8::Module::ScriptId",
"v8::Module::ScriptId must be used on an SourceTextModule");
// The SharedFunctionInfo is not available for errored modules.
Utils::ApiCheck(GetStatus() != kErrored, "v8::Module::ScriptId",
"v8::Module::ScriptId must not be used on an errored module");
i::Handle<i::SharedFunctionInfo> sfi(
i::Handle<i::SourceTextModule>::cast(self)->GetSharedFunctionInfo(),
self->GetIsolate());
return ToApiHandle<UnboundScript>(sfi)->GetId();
}
bool Module::IsSourceTextModule() const {
return Utils::OpenHandle(this)->IsSourceTextModule();
}
bool Module::IsSyntheticModule() const {
return Utils::OpenHandle(this)->IsSyntheticModule();
}
int Module::GetIdentityHash() const { return Utils::OpenHandle(this)->hash(); }
Maybe<bool> Module::InstantiateModule(Local<Context> context,
......
......@@ -24020,6 +24020,8 @@ TEST(CreateSyntheticModule) {
CHECK_EQ(i_module->export_names().length(), 1);
CHECK(i::String::cast(i_module->export_names().get(0)).Equals(*default_name));
CHECK_EQ(i_module->status(), i::Module::kInstantiated);
CHECK(module->IsSyntheticModule());
CHECK(!module->IsSourceTextModule());
}
TEST(CreateSyntheticModuleGC) {
......@@ -25908,6 +25910,47 @@ TEST(ModuleGetUnboundModuleScript) {
}
}
TEST(ModuleScriptId) {
LocalContext context;
v8::Isolate* isolate = context->GetIsolate();
v8::HandleScope scope(isolate);
Local<String> url = v8_str("www.google.com");
Local<String> source_text = v8_str("export default 5; export const a = 10;");
v8::ScriptOrigin origin(url, Local<v8::Integer>(), Local<v8::Integer>(),
Local<v8::Boolean>(), Local<v8::Integer>(),
Local<v8::Value>(), Local<v8::Boolean>(),
Local<v8::Boolean>(), True(isolate));
v8::ScriptCompiler::Source source(source_text, origin);
Local<Module> module =
v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
int id_before_instantiation = module->ScriptId();
module->InstantiateModule(context.local(), UnexpectedModuleResolveCallback)
.ToChecked();
int id_after_instantiation = module->ScriptId();
CHECK_EQ(id_before_instantiation, id_after_instantiation);
CHECK_NE(id_before_instantiation, v8::UnboundScript::kNoScriptId);
}
TEST(ModuleIsSourceTextModule) {
LocalContext context;
v8::Isolate* isolate = context->GetIsolate();
v8::HandleScope scope(isolate);
Local<String> url = v8_str("www.google.com");
Local<String> source_text = v8_str("export default 5; export const a = 10;");
v8::ScriptOrigin origin(url, Local<v8::Integer>(), Local<v8::Integer>(),
Local<v8::Boolean>(), Local<v8::Integer>(),
Local<v8::Value>(), Local<v8::Boolean>(),
Local<v8::Boolean>(), True(isolate));
v8::ScriptCompiler::Source source(source_text, origin);
Local<Module> module =
v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
CHECK(module->IsSourceTextModule());
CHECK(!module->IsSyntheticModule());
}
TEST(GlobalTemplateWithDoubleProperty) {
v8::Isolate* isolate = CcTest::isolate();
v8::HandleScope handle_scope(isolate);
......
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