Commit b9f282a9 authored by Andreas Haas's avatar Andreas Haas Committed by V8 LUCI CQ

[wasm][externref] Support default value for the table constructor

The WebAssembly.Table constructor supports a second parameter that was
not supported by V8 so far.

R=thibaudm@chromium.org

Bug: v8:7581
Change-Id: Id74c53a6b1bde7f49a4edea8397d1cab253e1a0e
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3141571
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarThibaud Michaud <thibaudm@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76660}
parent 61521ebd
......@@ -1117,12 +1117,25 @@ void WebAssemblyTable(const v8::FunctionCallbackInfo<v8::Value>& args) {
}
i::Handle<i::FixedArray> fixed_array;
i::Handle<i::JSObject> table_obj =
i::Handle<i::WasmTableObject> table_obj =
i::WasmTableObject::New(i_isolate, i::Handle<i::WasmInstanceObject>(),
type, static_cast<uint32_t>(initial), has_maximum,
static_cast<uint32_t>(maximum), &fixed_array);
if (initial > 0 && args.Length() >= 2 && !args[1]->IsUndefined()) {
i::Handle<i::Object> element = Utils::OpenHandle(*args[1]);
if (!i::WasmTableObject::IsValidElement(i_isolate, table_obj, element)) {
thrower.TypeError(
"Argument 2 must be undefined, null, or a value of type compatible "
"with the type of the new table.");
return;
}
for (uint32_t index = 0; index < static_cast<uint32_t>(initial); ++index) {
i::WasmTableObject::Set(i_isolate, table_obj, index, element);
}
}
v8::ReturnValue<v8::Value> return_value = args.GetReturnValue();
return_value.Set(Utils::ToLocal(table_obj));
return_value.Set(Utils::ToLocal(i::Handle<i::JSObject>::cast(table_obj)));
}
void WebAssemblyMemory(const v8::FunctionCallbackInfo<v8::Value>& args) {
......
......@@ -79,3 +79,36 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
assertTraps(kTrapTableOutOfBounds, () => instance.exports.init());
})();
(function TestExternRefTableConstructorWithDefaultValue() {
print(arguments.callee.name);
const testObject = {};
const argument = { "element": "externref", "initial": 3 };
const table = new WebAssembly.Table(argument, testObject);
assertEquals(table.length, 3);
assertEquals(table.get(0), testObject);
assertEquals(table.get(1), testObject);
assertEquals(table.get(2), testObject);
})();
(function TestFuncRefTableConstructorWithDefaultValue() {
print(arguments.callee.name);
const expected = 6;
let dummy =
(() => {
let builder = new WasmModuleBuilder();
builder.addFunction('dummy', kSig_i_v)
.addBody([kExprI32Const, expected])
.exportAs('dummy');
return builder.instantiate().exports.dummy;
})();
const argument = { "element": "anyfunc", "initial": 3 };
const table = new WebAssembly.Table(argument, dummy);
assertEquals(table.length, 3);
assertEquals(table.get(0)(), expected);
assertEquals(table.get(1)(), expected);
assertEquals(table.get(2)(), expected);
})();
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