2019-01-17 11:12:47 +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.
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
// Flags: --expose-wasm --experimental-wasm-reftypes --expose-gc
|
2019-01-17 11:12:47 +00:00
|
|
|
|
2019-05-13 09:45:06 +00:00
|
|
|
load('test/mjsunit/wasm/wasm-module-builder.js');
|
2019-01-17 11:12:47 +00:00
|
|
|
|
2019-01-22 10:31:06 +00:00
|
|
|
(function testAnyFuncIdentityFunction() {
|
2019-01-17 11:12:47 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_a_a)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0])
|
2019-01-17 11:12:47 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertThrows(() => instance.exports.main(print), TypeError);
|
2019-05-13 09:45:06 +00:00
|
|
|
assertThrows(() => instance.exports.main({'hello': 'world'}), TypeError);
|
2019-01-17 11:12:47 +00:00
|
|
|
assertSame(
|
|
|
|
instance.exports.main, instance.exports.main(instance.exports.main));
|
|
|
|
})();
|
|
|
|
|
2019-01-22 10:31:06 +00:00
|
|
|
(function testPassAnyFuncToImportedFunction() {
|
2019-01-17 11:12:47 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_v_a);
|
2019-05-13 09:45:06 +00:00
|
|
|
const imp_index = builder.addImport('q', 'func', sig_index);
|
2019-01-17 11:12:47 +00:00
|
|
|
builder.addFunction('main', sig_index)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprCallFunction, imp_index])
|
2019-01-17 11:12:47 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate({q: {func: checkFunction}}).exports.main;
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertSame(main, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
main(main);
|
|
|
|
})();
|
2019-01-22 10:29:30 +00:00
|
|
|
|
2019-01-22 10:31:06 +00:00
|
|
|
(function testPassAnyFuncWithGCWithLocals() {
|
2019-01-22 10:29:30 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const ref_sig = builder.addType(kSig_v_a);
|
|
|
|
const void_sig = builder.addType(kSig_v_v);
|
2019-05-13 09:45:06 +00:00
|
|
|
const imp_index = builder.addImport('q', 'func', ref_sig);
|
|
|
|
const gc_index = builder.addImport('q', 'gc', void_sig);
|
2019-01-22 10:29:30 +00:00
|
|
|
// First call the gc, then check if the object still exists.
|
|
|
|
builder.addFunction('main', ref_sig)
|
|
|
|
.addLocals({anyfunc_count: 10})
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 1, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 2, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 3, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 4, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 5, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 6, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 7, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 8, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 9, // Set local
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalSet, 10, // Set local
|
2019-05-13 09:45:06 +00:00
|
|
|
kExprCallFunction, gc_index, // call gc
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 9,
|
2019-05-13 09:45:06 +00:00
|
|
|
kExprCallFunction, imp_index // call import
|
2019-01-22 10:29:30 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main =
|
|
|
|
builder.instantiate({q: {func: checkFunction, gc: gc}}).exports.main;
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertSame(main, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
main(main);
|
|
|
|
})();
|
|
|
|
|
2019-01-22 10:31:06 +00:00
|
|
|
(function testPassAnyFuncWithGC() {
|
2019-01-22 10:29:30 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const ref_sig = builder.addType(kSig_v_a);
|
|
|
|
const void_sig = builder.addType(kSig_v_v);
|
2019-05-13 09:45:06 +00:00
|
|
|
const imp_index = builder.addImport('q', 'func', ref_sig);
|
|
|
|
const gc_index = builder.addImport('q', 'gc', void_sig);
|
2019-01-22 10:29:30 +00:00
|
|
|
// First call the gc, then check if the object still exists.
|
|
|
|
builder.addFunction('main', ref_sig)
|
|
|
|
.addBody([
|
|
|
|
kExprCallFunction, gc_index, // call gc
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprCallFunction, imp_index // call import
|
2019-01-22 10:29:30 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertSame(main, value);
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:45:06 +00:00
|
|
|
const main =
|
|
|
|
builder.instantiate({q: {func: checkFunction, gc: gc}}).exports.main;
|
2019-01-22 10:29:30 +00:00
|
|
|
|
|
|
|
main(main);
|
|
|
|
})();
|
|
|
|
|
2019-01-22 10:31:06 +00:00
|
|
|
(function testPassAnyFuncWithGCInWrapper() {
|
2019-01-22 10:29:30 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
2019-05-13 09:45:06 +00:00
|
|
|
const kSig_a_iai =
|
|
|
|
makeSig([kWasmI32, kWasmAnyFunc, kWasmI32], [kWasmAnyFunc]);
|
2019-01-22 10:29:30 +00:00
|
|
|
const sig_index = builder.addType(kSig_a_iai);
|
|
|
|
builder.addFunction('main', sig_index)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 1])
|
2019-01-22 10:29:30 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
|
|
|
|
const triggerGCParam = {
|
|
|
|
valueOf: () => {
|
|
|
|
gc();
|
|
|
|
return 17;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = main(triggerGCParam, main, triggerGCParam);
|
|
|
|
assertSame(main, result);
|
|
|
|
})();
|
2019-01-22 10:31:06 +00:00
|
|
|
|
|
|
|
(function testAnyFuncDefaultValue() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_a_v);
|
|
|
|
builder.addFunction('main', sig_index)
|
|
|
|
.addLocals({anyfunc_count: 1})
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0])
|
2019-01-22 10:31:06 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
assertEquals(null, main());
|
|
|
|
})();
|
2019-01-23 12:11:08 +00:00
|
|
|
|
|
|
|
(function testAssignNullRefToAnyFuncLocal() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_a_a);
|
|
|
|
builder.addFunction('main', sig_index)
|
2020-06-03 12:48:16 +00:00
|
|
|
.addBody([kExprRefNull, kWasmAnyFunc, kExprLocalSet, 0, kExprLocalGet, 0])
|
2019-01-23 12:11:08 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
assertEquals(null, main(main));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testImplicitReturnNullRefAsAnyFunc() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_a_v);
|
2020-06-03 12:48:16 +00:00
|
|
|
builder.addFunction('main', sig_index)
|
|
|
|
.addBody([kExprRefNull, kWasmAnyFunc])
|
|
|
|
.exportFunc();
|
2019-01-23 12:11:08 +00:00
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
assertEquals(null, main());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testExplicitReturnNullRefAsAnyFunc() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_a_v);
|
|
|
|
builder.addFunction('main', sig_index)
|
2020-06-03 12:48:16 +00:00
|
|
|
.addBody([kExprRefNull, kWasmAnyFunc, kExprReturn])
|
2019-01-23 12:11:08 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
assertEquals(null, main());
|
|
|
|
})();
|
|
|
|
|
2019-05-13 09:45:06 +00:00
|
|
|
(function testRefFuncOutOfBounds() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_a_v).addBody([kExprRefFunc, 10]);
|
|
|
|
|
|
|
|
assertThrows(() => builder.toModule(), WebAssembly.CompileError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testRefFuncIsCallable() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const expected = 54;
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const function_index = builder.addFunction('hidden', kSig_i_v)
|
|
|
|
.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_a_v)
|
|
|
|
.addBody([kExprRefFunc, function_index])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
assertEquals(expected, instance.exports.main()());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testRefFuncPreservesIdentity() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const expected = 54;
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const foo = builder.addFunction('foo', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, expected])
|
|
|
|
.exportFunc();
|
2020-02-17 16:21:02 +00:00
|
|
|
builder.addDeclarativeElementSegment([foo.index]);
|
2019-05-13 09:45:06 +00:00
|
|
|
builder.addFunction('main', kSig_a_v)
|
|
|
|
.addBody([kExprRefFunc, foo.index])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
assertSame(instance.exports.foo, instance.exports.main());
|
|
|
|
})();
|