2019-03-12 15:45:13 +00:00
|
|
|
// Copyright 2019 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-26 13:16:13 +00:00
|
|
|
// Flags: --experimental-wasm-typed-funcref --experimental-wasm-type-reflection
|
2019-03-12 15:45:13 +00:00
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2019-03-12 15:45:13 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function TestExternRefTableSetWithMultipleTypes() {
|
2019-03-12 15:45:13 +00:00
|
|
|
print(arguments.callee.name);
|
2020-06-09 15:54:14 +00:00
|
|
|
let table = new WebAssembly.Table({element: "externref", initial: 10});
|
2019-03-12 15:45:13 +00:00
|
|
|
|
2021-09-17 05:48:47 +00:00
|
|
|
// Table should be initialized with undefined.
|
|
|
|
assertEquals(undefined, table.get(1));
|
2019-03-12 15:45:13 +00:00
|
|
|
let obj = {'hello' : 'world'};
|
|
|
|
table.set(2, obj);
|
|
|
|
assertSame(obj, table.get(2));
|
|
|
|
table.set(3, 1234);
|
|
|
|
assertEquals(1234, table.get(3));
|
|
|
|
table.set(4, 123.5);
|
|
|
|
assertEquals(123.5, table.get(4));
|
|
|
|
table.set(5, undefined);
|
|
|
|
assertEquals(undefined, table.get(5));
|
|
|
|
// Overwrite entry 4, because null would otherwise be the default value.
|
|
|
|
table.set(4, null);
|
|
|
|
assertEquals(null, table.get(4));
|
|
|
|
table.set(7, print);
|
|
|
|
assertEquals(print, table.get(7));
|
|
|
|
|
|
|
|
assertThrows(() => table.set(12), RangeError);
|
|
|
|
})();
|
2019-04-30 14:55:07 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function TestImportExternRefTable() {
|
2019-04-30 14:55:07 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
2022-06-09 15:04:35 +00:00
|
|
|
const table_index = builder.addImportedTable(
|
|
|
|
"imp", "table", 3, 10, kWasmExternRef);
|
2019-04-30 14:55:07 +00:00
|
|
|
builder.addFunction('get', kSig_r_v)
|
2019-07-08 11:49:58 +00:00
|
|
|
.addBody([kExprI32Const, 0, kExprTableGet, table_index]);
|
2019-04-30 14:55:07 +00:00
|
|
|
|
2022-06-09 15:04:35 +00:00
|
|
|
let table_ref = new WebAssembly.Table(
|
|
|
|
{ element: "externref", initial: 3, maximum: 10 });
|
2019-04-30 14:55:07 +00:00
|
|
|
builder.instantiate({imp:{table: table_ref}});
|
|
|
|
|
2022-06-09 15:04:35 +00:00
|
|
|
let table_func = new WebAssembly.Table(
|
|
|
|
{ element: "anyfunc", initial: 3, maximum: 10 });
|
2019-04-30 14:55:07 +00:00
|
|
|
assertThrows(() => builder.instantiate({ imp: { table: table_func } }),
|
|
|
|
WebAssembly.LinkError, /imported table does not match the expected type/);
|
|
|
|
})();
|
2020-02-17 16:21:02 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function TestExternRefDropDeclarativeElementSegment() {
|
2020-02-17 16:21:02 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
2022-06-20 14:37:49 +00:00
|
|
|
builder.addDeclarativeElementSegment([[kExprRefNull, kFuncRefCode]],
|
2021-05-05 07:14:19 +00:00
|
|
|
kWasmFuncRef);
|
2020-02-17 16:21:02 +00:00
|
|
|
builder.addFunction('drop', kSig_v_v)
|
|
|
|
.addBody([kNumericPrefix, kExprElemDrop, 0])
|
|
|
|
.exportFunc();
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
// Counts as double-drop because declarative segments are dropped on
|
|
|
|
// initialization and is therefore not expected to throw.
|
|
|
|
instance.exports.drop();
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function TestExternRefTableInitFromDeclarativeElementSegment() {
|
2020-02-17 16:21:02 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const table = builder.addTable(kWasmAnyFunc, 10);
|
2022-06-20 14:37:49 +00:00
|
|
|
builder.addDeclarativeElementSegment([[kExprRefNull, kFuncRefCode]],
|
2021-05-05 07:14:19 +00:00
|
|
|
kWasmFuncRef);
|
2020-02-17 16:21:02 +00:00
|
|
|
builder.addFunction('init', kSig_v_v)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 0, kExprI32Const, 0, kExprI32Const, 1, kNumericPrefix,
|
|
|
|
kExprTableInit, table.index, 0
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
2022-06-09 15:04:35 +00:00
|
|
|
assertTraps(kTrapElementSegmentOutOfBounds, () => instance.exports.init());
|
2020-02-17 16:21:02 +00:00
|
|
|
})();
|
2021-09-03 14:58:27 +00:00
|
|
|
|
2022-07-26 06:03:39 +00:00
|
|
|
(function TestTableInitializer() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
let test = function(is_nullable) {
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig = builder.addType(kSig_i_i);
|
|
|
|
const func = builder.addFunction("func", kSig_i_i)
|
|
|
|
.addBody([kExprLocalGet, 0]);
|
|
|
|
builder.addTable(is_nullable ? wasmRefNullType(sig) : wasmRefType(sig),
|
|
|
|
10, 10, [kExprRefFunc, func.index]);
|
|
|
|
builder.addFunction("main", kSig_i_ii)
|
|
|
|
.addBody([kExprLocalGet, 1, kExprLocalGet, 0, kExprTableGet, 0,
|
2022-08-30 16:48:43 +00:00
|
|
|
kExprCallRef, sig])
|
2022-07-26 06:03:39 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(1, instance.exports.main(0, 1));
|
|
|
|
assertEquals(33, instance.exports.main(5, 33));
|
|
|
|
}
|
|
|
|
|
|
|
|
test(true);
|
|
|
|
test(false);
|
|
|
|
})();
|
2021-09-03 14:58:27 +00:00
|
|
|
|
|
|
|
(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);
|
|
|
|
})();
|
|
|
|
|
2021-09-16 11:59:17 +00:00
|
|
|
function getDummy(val) {
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('dummy', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, val])
|
|
|
|
.exportAs('dummy');
|
|
|
|
return builder.instantiate().exports.dummy;
|
|
|
|
}
|
|
|
|
|
2021-09-03 14:58:27 +00:00
|
|
|
(function TestFuncRefTableConstructorWithDefaultValue() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const expected = 6;
|
2021-09-16 11:59:17 +00:00
|
|
|
let dummy = getDummy(expected);
|
2021-09-03 14:58:27 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
})();
|
2021-09-16 11:59:17 +00:00
|
|
|
|
|
|
|
(function TestExternFuncTableSetWithoutValue() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const expected = 6;
|
|
|
|
const dummy = getDummy(expected);
|
2021-09-17 05:48:47 +00:00
|
|
|
const argument = { "element": "anyfunc", "initial": 3 };
|
2021-09-16 11:59:17 +00:00
|
|
|
const table = new WebAssembly.Table(argument, dummy);
|
|
|
|
assertEquals(table.get(1)(), expected);
|
|
|
|
table.set(1);
|
|
|
|
assertEquals(table.get(1), null);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestExternRefTableSetWithoutValue() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const testObject = {};
|
|
|
|
const argument = { "element": "externref", "initial": 3 };
|
|
|
|
const table = new WebAssembly.Table(argument, testObject);
|
|
|
|
assertEquals(table.get(1), testObject);
|
|
|
|
table.set(1);
|
2021-09-17 05:48:47 +00:00
|
|
|
assertEquals(table.get(1), undefined);
|
2021-09-16 11:59:17 +00:00
|
|
|
})();
|
2021-11-26 13:16:13 +00:00
|
|
|
|
|
|
|
(function TestFunctionExternRefTableRoundtrip() {
|
|
|
|
// Test that
|
|
|
|
// - initialization, setting, and growing an externref table, and
|
|
|
|
// - (imported) externref globals
|
|
|
|
// preserve function references.
|
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const js_function = function (i) { return i + 1; };
|
|
|
|
const wasm_js_function = new WebAssembly.Function(
|
|
|
|
{parameters:['i32', 'i32'], results: ['i32']},
|
|
|
|
function(a, b) { return a * b; })
|
|
|
|
|
|
|
|
let extern_type = wasmRefType(kWasmExternRef);
|
|
|
|
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let imported_global = builder.addImportedGlobal('m', 'n', extern_type, false);
|
|
|
|
let global = builder.addGlobal(kWasmExternRef, true).exportAs('global');
|
|
|
|
let table = builder.addTable(extern_type, 2, 10,
|
2022-06-20 14:37:49 +00:00
|
|
|
[kExprGlobalGet, imported_global])
|
2021-11-26 13:16:13 +00:00
|
|
|
builder.addFunction(
|
|
|
|
'setup', makeSig([extern_type, extern_type], []))
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0, kExprGlobalSet, global.index,
|
|
|
|
kExprI32Const, 1, kExprLocalGet, 0, kExprTableSet, table.index,
|
|
|
|
kExprLocalGet, 1, kExprI32Const, 1, kNumericPrefix,
|
|
|
|
kExprTableGrow, table.index, kExprDrop])
|
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction('get', makeSig([kWasmI32], [kWasmExternRef]))
|
|
|
|
.addBody([kExprLocalGet, 0, kExprTableGet, table.index])
|
|
|
|
.exportFunc();
|
|
|
|
let instance = builder.instantiate({m : {n : js_function}});
|
|
|
|
|
|
|
|
instance.exports.setup(wasm_js_function, instance.exports.setup);
|
|
|
|
|
|
|
|
assertEquals(instance.exports.global.value, wasm_js_function);
|
|
|
|
assertEquals(instance.exports.get(0), js_function);
|
|
|
|
assertEquals(instance.exports.get(1), wasm_js_function);
|
|
|
|
assertEquals(instance.exports.get(2), instance.exports.setup);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFunctionExternRefTableRoundtrip2() {
|
|
|
|
// Test that initialization, setting, and growing an externref table in the JS
|
|
|
|
// API preserves function references.
|
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('dummy', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, 0])
|
|
|
|
.exportAs('dummy');
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
const js_function = function (i) { return i + 1; };
|
|
|
|
const wasm_js_function = new WebAssembly.Function(
|
|
|
|
{parameters:['i32', 'i32'], results: ['i32']},
|
|
|
|
function(a, b) { return a * b; })
|
|
|
|
|
|
|
|
const argument = { "element": "externref", "initial": 3 };
|
|
|
|
const table = new WebAssembly.Table(argument, js_function);
|
|
|
|
table.set(1, wasm_js_function);
|
|
|
|
table.set(2, instance.exports.dummy);
|
|
|
|
table.grow(1, wasm_js_function);
|
|
|
|
assertEquals(table.get(0), js_function);
|
|
|
|
assertEquals(table.get(1), wasm_js_function);
|
|
|
|
assertEquals(table.get(2), instance.exports.dummy);
|
|
|
|
assertEquals(table.get(3), wasm_js_function);
|
|
|
|
})();
|