Commit e3c9f266 authored by Ben Smith's avatar Ben Smith Committed by Commit Bot

[wasm] Update WebAssembly.Global constructor

The new spec has two arguments, the first is the global descriptor, and
the second is the initial value:

    new WebAssembly.Global({type: i32}, 42);

If the initial value argument is omitted, the value is set to 0.

Bug: v8:7625
Change-Id: I679d4b7c49c69ec7ffcdfeb8ae506fa7ab9bba95
Reviewed-on: https://chromium-review.googlesource.com/1028847Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Commit-Queue: Ben Smith <binji@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52822}
parent b3ff8eb9
......@@ -686,10 +686,6 @@ void WebAssemblyGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
Local<Context> context = isolate->GetCurrentContext();
Local<v8::Object> descriptor = Local<Object>::Cast(args[0]);
// The descriptor's 'value'.
v8::MaybeLocal<v8::Value> maybe_value =
descriptor->Get(context, v8_str(isolate, "value"));
// The descriptor's 'mutable'.
bool is_mutable = false;
{
......@@ -738,38 +734,43 @@ void WebAssemblyGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
return;
}
// Convert value to a WebAssembly value.
v8::Local<v8::Value> value;
if (maybe_value.ToLocal(&value)) {
switch (type) {
case i::wasm::kWasmI32: {
int32_t i32_value = 0;
// Convert value to a WebAssembly value, the default value is 0.
Local<v8::Value> value = Local<Value>::Cast(args[1]);
switch (type) {
case i::wasm::kWasmI32: {
int32_t i32_value = 0;
if (!value->IsUndefined()) {
v8::Local<v8::Int32> int32_value;
if (!value->ToInt32(context).ToLocal(&int32_value)) return;
if (!int32_value->Int32Value(context).To(&i32_value)) return;
global_obj->SetI32(i32_value);
break;
}
case i::wasm::kWasmF32: {
global_obj->SetI32(i32_value);
break;
}
case i::wasm::kWasmF32: {
float f32_value = 0;
if (!value->IsUndefined()) {
double f64_value = 0;
v8::Local<v8::Number> number_value;
if (!value->ToNumber(context).ToLocal(&number_value)) return;
if (!number_value->NumberValue(context).To(&f64_value)) return;
float f32_value = static_cast<float>(f64_value);
global_obj->SetF32(f32_value);
break;
f32_value = static_cast<float>(f64_value);
}
case i::wasm::kWasmF64: {
double f64_value = 0;
global_obj->SetF32(f32_value);
break;
}
case i::wasm::kWasmF64: {
double f64_value = 0;
if (!value->IsUndefined()) {
v8::Local<v8::Number> number_value;
if (!value->ToNumber(context).ToLocal(&number_value)) return;
if (!number_value->NumberValue(context).To(&f64_value)) return;
global_obj->SetF64(f64_value);
break;
}
default:
UNREACHABLE();
global_obj->SetF64(f64_value);
break;
}
default:
UNREACHABLE();
}
i::Handle<i::JSObject> global_js_object(global_obj);
......
......@@ -8,7 +8,7 @@ load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
(function TestBasic() {
let global = new WebAssembly.Global({type: 'i32', value: 1});
let global = new WebAssembly.Global({type: 'i32'}, 1);
let builder = new WasmModuleBuilder();
builder.addImportedGlobal("mod", "g", kWasmI32);
builder.addFunction("main", kSig_i_v)
......@@ -20,7 +20,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
})();
(function TestTypeMismatch() {
let global = new WebAssembly.Global({type: 'f32', value: 1});
let global = new WebAssembly.Global({type: 'f32'}, 1);
let builder = new WasmModuleBuilder();
builder.addImportedGlobal("mod", "g", kWasmI32);
builder.addFunction("main", kSig_i_v)
......
......@@ -215,24 +215,21 @@ const f64_values = [
];
function GlobalI32(value) {
return new WebAssembly.Global({type: 'i32', value});
return new WebAssembly.Global({type: 'i32'}, value);
}
function GlobalF32(value) {
return new WebAssembly.Global({type: 'f32', value});
return new WebAssembly.Global({type: 'f32'}, value);
}
function GlobalF64(value) {
return new WebAssembly.Global({type: 'f64', value});
return new WebAssembly.Global({type: 'f64'}, value);
}
(function TestDefaultValue() {
assertSame(0, GlobalI32().value);
// TODO(binji): The spec currently requires the default value to be NaN, but
// I think we should change this to 0. See
// https://github.com/WebAssembly/mutable-global/issues/6
assertSame(NaN, GlobalF32().value);
assertSame(NaN, GlobalF64().value);
assertSame(0, GlobalF32().value);
assertSame(0, GlobalF64().value);
})();
(function TestValueOf() {
......
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