Commit 74714df2 authored by Aseem Garg's avatar Aseem Garg Committed by Commit Bot

[wasm] Add type function to WebAssembly.Global

R=binji@chromium.org,adamk@chromium.org
Bug=v8:7742

Change-Id: I5b9a614dd0c8b028d756cbd401c803de4cb9437f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1513159
Commit-Queue: Aseem Garg <aseemgarg@chromium.org>
Reviewed-by: 's avatarBen Smith <binji@chromium.org>
Reviewed-by: 's avatarAdam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#60252}
parent bff04095
......@@ -193,6 +193,7 @@ Local<String> v8_str(Isolate* isolate, const char* str) {
GET_FIRST_ARGUMENT_AS(Module)
GET_FIRST_ARGUMENT_AS(Memory)
GET_FIRST_ARGUMENT_AS(Table)
GET_FIRST_ARGUMENT_AS(Global)
#undef GET_FIRST_ARGUMENT_AS
......@@ -1710,6 +1711,60 @@ void WebAssemblyGlobalSetValue(
}
}
// WebAssembly.Global.type(WebAssembly.Global) -> GlobalType
void WebAssemblyGlobalGetType(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
ScheduledErrorThrower thrower(i_isolate, "WebAssembly.Global.type()");
auto maybe_global = GetFirstArgumentAsGlobal(args, &thrower);
if (thrower.error()) return;
i::Handle<i::WasmGlobalObject> global = maybe_global.ToHandleChecked();
v8::Local<v8::Object> ret = v8::Object::New(isolate);
if (!ret->CreateDataProperty(isolate->GetCurrentContext(),
v8_str(isolate, "mutable"),
v8::Boolean::New(isolate, global->is_mutable()))
.IsJust()) {
return;
}
Local<String> type;
switch (global->type()) {
case i::wasm::kWasmI32: {
type = v8_str(isolate, "i32");
break;
}
case i::wasm::kWasmI64: {
type = v8_str(isolate, "i64");
break;
}
case i::wasm::kWasmF32: {
type = v8_str(isolate, "f32");
break;
}
case i::wasm::kWasmF64: {
type = v8_str(isolate, "f64");
break;
}
case i::wasm::kWasmAnyRef: {
type = v8_str(isolate, "anyref");
break;
}
default:
UNREACHABLE();
}
if (!ret->CreateDataProperty(isolate->GetCurrentContext(),
v8_str(isolate, "value"), type)
.IsJust()) {
return;
}
v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
return_value.Set(ret);
}
} // namespace
// TODO(titzer): we use the API to create the function template because the
......@@ -1945,6 +2000,10 @@ void WasmJs::Install(Isolate* isolate, bool exposed_on_global_object) {
InstallFunc(isolate, global_proto, "valueOf", WebAssemblyGlobalValueOf, 0);
InstallGetterSetter(isolate, global_proto, "value", WebAssemblyGlobalGetValue,
WebAssemblyGlobalSetValue);
if (enabled_features.type_reflection) {
InstallFunc(isolate, global_constructor, "type", WebAssemblyGlobalGetType,
1);
}
JSObject::AddProperty(isolate, global_proto, factory->to_string_tag_symbol(),
v8_str(isolate, "WebAssembly.Global"), ro_attributes);
......
......@@ -20,3 +20,17 @@ load('test/mjsunit/wasm/wasm-module-builder.js');
assertEquals("anyref", type.element);
assertEquals(3, Object.getOwnPropertyNames(type).length);
})();
(function TestGlobalType() {
let global = new WebAssembly.Global({value: "anyref", mutable: true});
let type = WebAssembly.Global.type(global);
assertEquals("anyref", type.value);
assertEquals(true, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
global = new WebAssembly.Global({value: "anyref"});
type = WebAssembly.Global.type(global);
assertEquals("anyref", type.value);
assertEquals(false, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
})();
......@@ -10,10 +10,13 @@ load('test/mjsunit/wasm/wasm-module-builder.js');
["abc", 123, {}].forEach(function(invalidInput) {
assertThrows(
() => WebAssembly.Memory.type(invalidInput), TypeError,
"WebAssembly.Memory.type(): Argument 0 must be a WebAssembly.Memory");
"WebAssembly.Memory.type(): Argument 0 must be a WebAssembly.Memory");
assertThrows(
() => WebAssembly.Table.type(invalidInput), TypeError,
"WebAssembly.Table.type(): Argument 0 must be a WebAssembly.Table");
assertThrows(
() => WebAssembly.Global.type(invalidInput), TypeError,
"WebAssembly.Global.type(): Argument 0 must be a WebAssembly.Global");
});
assertThrows(
......@@ -26,6 +29,11 @@ load('test/mjsunit/wasm/wasm-module-builder.js');
() => WebAssembly.Table.type(
new WebAssembly.Memory({initial:1})), TypeError,
"WebAssembly.Table.type(): Argument 0 must be a WebAssembly.Table");
assertThrows(
() => WebAssembly.Global.type(
new WebAssembly.Memory({initial:1})), TypeError,
"WebAssembly.Global.type(): Argument 0 must be a WebAssembly.Global");
})();
(function TestMemoryType() {
......@@ -56,3 +64,35 @@ load('test/mjsunit/wasm/wasm-module-builder.js');
assertEquals("anyfunc", type.element);
assertEquals(3, Object.getOwnPropertyNames(type).length);
})();
(function TestGlobalType() {
let global = new WebAssembly.Global({value: "i32", mutable: true});
let type = WebAssembly.Global.type(global);
assertEquals("i32", type.value);
assertEquals(true, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
global = new WebAssembly.Global({value: "i32"});
type = WebAssembly.Global.type(global);
assertEquals("i32", type.value);
assertEquals(false, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
global = new WebAssembly.Global({value: "i64"});
type = WebAssembly.Global.type(global);
assertEquals("i64", type.value);
assertEquals(false, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
global = new WebAssembly.Global({value: "f32"});
type = WebAssembly.Global.type(global);
assertEquals("f32", type.value);
assertEquals(false, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
global = new WebAssembly.Global({value: "f64"});
type = WebAssembly.Global.type(global);
assertEquals("f64", type.value);
assertEquals(false, type.mutable);
assertEquals(2, Object.getOwnPropertyNames(type).length);
})();
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