Commit 39cc69dc authored by Thibaud Michaud's avatar Thibaud Michaud Committed by V8 LUCI CQ

[wasm] Factorize wasm-js constructor setup

R=ahaas@chromium.org

Change-Id: I860cd7aebdf46f323c7f6ad0cd563246a061c4d1
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3226338
Commit-Queue: Thibaud Michaud <thibaudm@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77525}
parent 23af060d
......@@ -2572,6 +2572,26 @@ void SetDummyInstanceTemplate(Isolate* isolate, Handle<JSFunction> fun) {
instance_template);
}
Handle<JSObject> SetupConstructor(Isolate* isolate,
Handle<JSFunction> constructor,
InstanceType instance_type, int instance_size,
const char* name = nullptr) {
SetDummyInstanceTemplate(isolate, constructor);
JSFunction::EnsureHasInitialMap(constructor);
Handle<JSObject> proto(JSObject::cast(constructor->instance_prototype()),
isolate);
Handle<Map> map = isolate->factory()->NewMap(instance_type, instance_size);
JSFunction::SetInitialMap(isolate, constructor, map, proto);
constexpr PropertyAttributes ro_attributes =
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY);
if (name) {
JSObject::AddProperty(isolate, proto,
isolate->factory()->to_string_tag_symbol(),
v8_str(isolate, name), ro_attributes);
}
return proto;
}
// static
void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
Handle<JSGlobalObject> global = isolate->global_object();
......@@ -2625,15 +2645,9 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
// Setup Module
Handle<JSFunction> module_constructor =
InstallConstructorFunc(isolate, webassembly, "Module", WebAssemblyModule);
SetupConstructor(isolate, module_constructor, i::WASM_MODULE_OBJECT_TYPE,
WasmModuleObject::kHeaderSize, "WebAssembly.Module");
context->set_wasm_module_constructor(*module_constructor);
SetDummyInstanceTemplate(isolate, module_constructor);
JSFunction::EnsureHasInitialMap(module_constructor);
Handle<JSObject> module_proto(
JSObject::cast(module_constructor->instance_prototype()), isolate);
Handle<Map> module_map = isolate->factory()->NewMap(
i::WASM_MODULE_OBJECT_TYPE, WasmModuleObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, module_constructor, module_map,
module_proto);
InstallFunc(isolate, module_constructor, "imports", WebAssemblyModuleImports,
1, false, NONE, SideEffectType::kHasNoSideEffect);
InstallFunc(isolate, module_constructor, "exports", WebAssemblyModuleExports,
......@@ -2641,26 +2655,16 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
InstallFunc(isolate, module_constructor, "customSections",
WebAssemblyModuleCustomSections, 2, false, NONE,
SideEffectType::kHasNoSideEffect);
JSObject::AddProperty(isolate, module_proto, factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Module"), ro_attributes);
// Setup Instance
Handle<JSFunction> instance_constructor = InstallConstructorFunc(
isolate, webassembly, "Instance", WebAssemblyInstance);
Handle<JSObject> instance_proto = SetupConstructor(
isolate, instance_constructor, i::WASM_INSTANCE_OBJECT_TYPE,
WasmInstanceObject::kHeaderSize, "WebAssembly.Instance");
context->set_wasm_instance_constructor(*instance_constructor);
SetDummyInstanceTemplate(isolate, instance_constructor);
JSFunction::EnsureHasInitialMap(instance_constructor);
Handle<JSObject> instance_proto(
JSObject::cast(instance_constructor->instance_prototype()), isolate);
Handle<Map> instance_map = isolate->factory()->NewMap(
i::WASM_INSTANCE_OBJECT_TYPE, WasmInstanceObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, instance_constructor, instance_map,
instance_proto);
InstallGetter(isolate, instance_proto, "exports",
WebAssemblyInstanceGetExports);
JSObject::AddProperty(isolate, instance_proto,
factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Instance"), ro_attributes);
// The context is not set up completely yet. That's why we cannot use
// {WasmFeatures::FromIsolate} and have to use {WasmFeatures::FromFlags}
......@@ -2670,14 +2674,10 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
// Setup Table
Handle<JSFunction> table_constructor =
InstallConstructorFunc(isolate, webassembly, "Table", WebAssemblyTable);
Handle<JSObject> table_proto =
SetupConstructor(isolate, table_constructor, i::WASM_TABLE_OBJECT_TYPE,
WasmTableObject::kHeaderSize, "WebAssembly.Table");
context->set_wasm_table_constructor(*table_constructor);
SetDummyInstanceTemplate(isolate, table_constructor);
JSFunction::EnsureHasInitialMap(table_constructor);
Handle<JSObject> table_proto(
JSObject::cast(table_constructor->instance_prototype()), isolate);
Handle<Map> table_map = isolate->factory()->NewMap(
i::WASM_TABLE_OBJECT_TYPE, WasmTableObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, table_constructor, table_map, table_proto);
InstallGetter(isolate, table_proto, "length", WebAssemblyTableGetLength);
InstallFunc(isolate, table_proto, "grow", WebAssemblyTableGrow, 1);
InstallFunc(isolate, table_proto, "get", WebAssemblyTableGet, 1, false, NONE,
......@@ -2687,42 +2687,28 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
InstallFunc(isolate, table_proto, "type", WebAssemblyTableType, 0, false,
NONE, SideEffectType::kHasNoSideEffect);
}
JSObject::AddProperty(isolate, table_proto, factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Table"), ro_attributes);
// Setup Memory
Handle<JSFunction> memory_constructor =
InstallConstructorFunc(isolate, webassembly, "Memory", WebAssemblyMemory);
Handle<JSObject> memory_proto =
SetupConstructor(isolate, memory_constructor, i::WASM_MEMORY_OBJECT_TYPE,
WasmMemoryObject::kHeaderSize, "WebAssembly.Memory");
context->set_wasm_memory_constructor(*memory_constructor);
SetDummyInstanceTemplate(isolate, memory_constructor);
JSFunction::EnsureHasInitialMap(memory_constructor);
Handle<JSObject> memory_proto(
JSObject::cast(memory_constructor->instance_prototype()), isolate);
Handle<Map> memory_map = isolate->factory()->NewMap(
i::WASM_MEMORY_OBJECT_TYPE, WasmMemoryObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, memory_constructor, memory_map,
memory_proto);
InstallFunc(isolate, memory_proto, "grow", WebAssemblyMemoryGrow, 1);
InstallGetter(isolate, memory_proto, "buffer", WebAssemblyMemoryGetBuffer);
if (enabled_features.has_type_reflection()) {
InstallFunc(isolate, memory_proto, "type", WebAssemblyMemoryType, 0, false,
NONE, SideEffectType::kHasNoSideEffect);
}
JSObject::AddProperty(isolate, memory_proto, factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Memory"), ro_attributes);
// Setup Global
Handle<JSFunction> global_constructor =
InstallConstructorFunc(isolate, webassembly, "Global", WebAssemblyGlobal);
Handle<JSObject> global_proto =
SetupConstructor(isolate, global_constructor, i::WASM_GLOBAL_OBJECT_TYPE,
WasmGlobalObject::kHeaderSize, "WebAssembly.Global");
context->set_wasm_global_constructor(*global_constructor);
SetDummyInstanceTemplate(isolate, global_constructor);
JSFunction::EnsureHasInitialMap(global_constructor);
Handle<JSObject> global_proto(
JSObject::cast(global_constructor->instance_prototype()), isolate);
Handle<Map> global_map = isolate->factory()->NewMap(
i::WASM_GLOBAL_OBJECT_TYPE, WasmGlobalObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, global_constructor, global_map,
global_proto);
InstallFunc(isolate, global_proto, "valueOf", WebAssemblyGlobalValueOf, 0,
false, NONE, SideEffectType::kHasNoSideEffect);
InstallGetterSetter(isolate, global_proto, "value", WebAssemblyGlobalGetValue,
......@@ -2731,28 +2717,19 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
InstallFunc(isolate, global_proto, "type", WebAssemblyGlobalType, 0, false,
NONE, SideEffectType::kHasNoSideEffect);
}
JSObject::AddProperty(isolate, global_proto, factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Global"), ro_attributes);
// Setup Exception
if (enabled_features.has_eh()) {
Handle<JSFunction> tag_constructor =
InstallConstructorFunc(isolate, webassembly, "Tag", WebAssemblyTag);
Handle<JSObject> tag_proto =
SetupConstructor(isolate, tag_constructor, i::WASM_TAG_OBJECT_TYPE,
WasmTagObject::kHeaderSize, "WebAssembly.Tag");
context->set_wasm_tag_constructor(*tag_constructor);
SetDummyInstanceTemplate(isolate, tag_constructor);
JSFunction::EnsureHasInitialMap(tag_constructor);
Handle<JSObject> tag_proto(
JSObject::cast(tag_constructor->instance_prototype()), isolate);
JSObject::AddProperty(isolate, tag_proto, factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Tag"), ro_attributes);
if (enabled_features.has_type_reflection()) {
InstallFunc(isolate, tag_proto, "type", WebAssemblyTagType, 0);
}
Handle<Map> tag_map = isolate->factory()->NewMap(
i::WASM_TAG_OBJECT_TYPE, WasmTagObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, tag_constructor, tag_map, tag_proto);
// Set up runtime exception constructor.
Handle<JSFunction> exception_constructor = InstallConstructorFunc(
isolate, webassembly, "Exception", WebAssemblyException);
......@@ -2863,16 +2840,12 @@ void WasmJs::InstallConditionalFeatures(Isolate* isolate,
}
// Install the constructor on the context.
context->set_wasm_tag_constructor(*tag_constructor);
SetDummyInstanceTemplate(isolate, tag_constructor);
JSFunction::EnsureHasInitialMap(tag_constructor);
Handle<JSObject> tag_proto(
JSObject::cast(tag_constructor->instance_prototype()), isolate);
Handle<JSObject> tag_proto =
SetupConstructor(isolate, tag_constructor, i::WASM_TAG_OBJECT_TYPE,
WasmTagObject::kHeaderSize, "WebAssembly.Tag");
if (enabled_features.has_type_reflection()) {
InstallFunc(isolate, tag_proto, "type", WebAssemblyTagType, 0);
}
Handle<Map> tag_map = isolate->factory()->NewMap(
i::WASM_TAG_OBJECT_TYPE, WasmTagObject::kHeaderSize);
JSFunction::SetInitialMap(isolate, tag_constructor, tag_map, tag_proto);
}
}
#undef ASSIGN
......
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