2019-03-08 14:19:31 +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-12-13 13:35:47 +00:00
|
|
|
// Flags: --expose-wasm
|
2019-03-08 14:19:31 +00:00
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2019-03-08 14:19:31 +00:00
|
|
|
|
2019-03-18 10:26:12 +00:00
|
|
|
(function TestGetAndSet() {
|
2019-03-08 14:19:31 +00:00
|
|
|
function addTableWithAccessors(builder, type, size, name) {
|
|
|
|
const table = builder.addTable(type, size);
|
|
|
|
const set_sig = makeSig([kWasmI32, type], []);
|
|
|
|
builder.addFunction('set_' + name, set_sig)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
2019-07-08 11:49:58 +00:00
|
|
|
kExprTableSet, table.index])
|
2019-03-08 14:19:31 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const get_sig = makeSig([kWasmI32], [type]);
|
|
|
|
builder.addFunction('get_' + name, get_sig)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprTableGet, table.index])
|
2019-03-08 14:19:31 +00:00
|
|
|
.exportFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
addTableWithAccessors(builder, kWasmAnyFunc, 10, 'table_func1');
|
2020-06-09 15:54:14 +00:00
|
|
|
addTableWithAccessors(builder, kWasmExternRef, 20, 'table_ref1');
|
|
|
|
addTableWithAccessors(builder, kWasmExternRef, 9, 'table_ref2');
|
2019-03-08 14:19:31 +00:00
|
|
|
addTableWithAccessors(builder, kWasmAnyFunc, 12, 'table_func2');
|
|
|
|
|
|
|
|
let exports = builder.instantiate().exports;
|
|
|
|
const dummy_ref = {foo : 1, bar : 3};
|
|
|
|
const dummy_func = exports.set_table_func1;
|
|
|
|
|
|
|
|
(function testTableGetInitialValue() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
// Tables are initialized with `null`.
|
|
|
|
assertSame(null, exports.get_table_func1(1));
|
|
|
|
assertSame(null, exports.get_table_func2(2));
|
|
|
|
assertSame(null, exports.get_table_ref1(3));
|
|
|
|
assertSame(null, exports.get_table_ref2(4));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testTableGetOOB() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
assertSame(null, exports.get_table_func2(11));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_func1(11));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_func2(21));
|
2019-03-18 10:26:12 +00:00
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_func1(-1));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_func2(-2));
|
2019-03-08 14:19:31 +00:00
|
|
|
assertSame(null, exports.get_table_ref1(14));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_ref2(14));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_ref1(44));
|
2019-03-18 10:26:12 +00:00
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_ref2(-1));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.get_table_ref1(-4));
|
2019-03-08 14:19:31 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function testTableSetOOB() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
exports.set_table_func2(11, dummy_func);
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.set_table_func1(11, dummy_func));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.set_table_func2(21, dummy_func));
|
|
|
|
exports.set_table_ref1(14, dummy_ref);
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.set_table_ref2(14, dummy_ref));
|
|
|
|
assertTraps(kTrapTableOutOfBounds, () => exports.set_table_ref1(44, dummy_ref));
|
|
|
|
})();
|
|
|
|
|
2019-07-08 11:49:58 +00:00
|
|
|
(function testTableSet() {
|
2019-03-08 14:19:31 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
assertSame(null, exports.get_table_func1(3));
|
|
|
|
exports.set_table_func1(3, dummy_func);
|
|
|
|
assertSame(dummy_func, exports.get_table_func1(3));
|
|
|
|
assertSame(null, exports.get_table_func2(3));
|
|
|
|
|
|
|
|
assertSame(null, exports.get_table_func2(7));
|
|
|
|
exports.set_table_func2(7, dummy_func);
|
|
|
|
assertSame(dummy_func, exports.get_table_func2(7));
|
|
|
|
assertSame(null, exports.get_table_func1(7));
|
|
|
|
|
|
|
|
assertSame(null, exports.get_table_ref1(3));
|
|
|
|
exports.set_table_ref1(3, dummy_ref);
|
|
|
|
assertSame(dummy_ref, exports.get_table_ref1(3));
|
|
|
|
assertSame(null, exports.get_table_ref2(3));
|
|
|
|
|
|
|
|
assertSame(null, exports.get_table_ref2(7));
|
|
|
|
exports.set_table_ref2(7, dummy_ref);
|
|
|
|
assertSame(dummy_ref, exports.get_table_ref2(7));
|
|
|
|
assertSame(null, exports.get_table_ref1(7));
|
|
|
|
})();
|
2019-03-18 10:26:12 +00:00
|
|
|
|
|
|
|
(function testSetFunctionTableInvalidType() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
assertThrows(() => exports.set_table_func2(7, dummy_ref), TypeError);
|
|
|
|
})();
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testGetFunctionFromInitializedTable() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const value1 = 11;
|
|
|
|
const value2 = 22;
|
|
|
|
const value3 = 13;
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const t1 = builder.addTable(kWasmAnyFunc, 10).index;
|
|
|
|
const t2 = builder.addTable(kWasmAnyFunc, 12).index;
|
|
|
|
|
|
|
|
const f1 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, value1]);
|
|
|
|
const f2 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, value2]);
|
|
|
|
const f3 = builder.addFunction('f', kSig_i_v).addBody([kExprI32Const, value3]);
|
|
|
|
builder.addFunction('get_t1', kSig_a_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprTableGet, t1])
|
2019-03-18 10:26:12 +00:00
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction('get_t2', kSig_a_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprTableGet, t2])
|
2019-03-18 10:26:12 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const offset1 = 3;
|
|
|
|
const offset2 = 9;
|
2022-06-20 14:37:49 +00:00
|
|
|
builder.addActiveElementSegment(t1, wasmI32Const(offset1),
|
2021-05-05 07:14:19 +00:00
|
|
|
[f1.index, f2.index]);
|
2022-06-20 14:37:49 +00:00
|
|
|
builder.addActiveElementSegment(t2, wasmI32Const(offset2),
|
2021-05-05 07:14:19 +00:00
|
|
|
[f3.index, f1.index]);
|
2019-03-18 10:26:12 +00:00
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(value1, instance.exports.get_t1(offset1)());
|
|
|
|
assertEquals(value2, instance.exports.get_t1(offset1 + 1)());
|
|
|
|
assertEquals(value3, instance.exports.get_t2(offset2)());
|
|
|
|
assertEquals(value1, instance.exports.get_t2(offset2 + 1)());
|
|
|
|
})();
|
2019-05-13 09:45:06 +00:00
|
|
|
|
|
|
|
(function testRefFuncInTableIsCallable() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const expected = 54;
|
|
|
|
const index = 3;
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const table_index = builder.addTable(kWasmAnyFunc, 15, 15).index;
|
|
|
|
const sig_index = builder.addType(kSig_i_v);
|
|
|
|
const function_index = builder.addFunction('hidden', sig_index)
|
|
|
|
.addBody([kExprI32Const, expected])
|
|
|
|
.index;
|
2020-02-17 16:21:02 +00:00
|
|
|
builder.addDeclarativeElementSegment([function_index]);
|
2019-05-13 09:45:06 +00:00
|
|
|
|
|
|
|
builder.addFunction('main', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, index, // entry index
|
|
|
|
kExprRefFunc, function_index, // function reference
|
2019-07-08 11:49:58 +00:00
|
|
|
kExprTableSet, table_index, // --
|
2019-05-13 09:45:06 +00:00
|
|
|
kExprI32Const, index, // entry index
|
|
|
|
kExprCallIndirect, sig_index, table_index // --
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
assertEquals(expected, instance.exports.main());
|
|
|
|
})();
|