2018-04-06 13:18:49 +00:00
|
|
|
// Copyright 2018 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 --expose-gc --allow-natives-syntax
|
2018-04-06 13:18:49 +00:00
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2018-04-06 13:18:49 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testExternRefIdentityFunction() {
|
2018-04-06 13:18:49 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_r_r)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0])
|
2018-04-06 13:18:49 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
let obj = {'hello' : 'world'};
|
|
|
|
assertEquals(obj, instance.exports.main(obj));
|
|
|
|
assertEquals(1234, instance.exports.main(1234));
|
|
|
|
assertEquals(123.4, instance.exports.main(123.4));
|
|
|
|
assertEquals(undefined, instance.exports.main(undefined));
|
|
|
|
assertEquals(null, instance.exports.main(null));
|
|
|
|
assertEquals(print, instance.exports.main(print));
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testPassExternRefToImportedFunction() {
|
2018-04-06 13:18:49 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_v_r);
|
|
|
|
const imp_index = builder.addImport("q", "func", sig_index);
|
|
|
|
builder.addFunction('main', sig_index)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0,
|
2018-04-06 13:18:49 +00:00
|
|
|
kExprCallFunction, imp_index])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertEquals('world', value.hello);
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance = builder.instantiate({q: {func: checkFunction}});
|
|
|
|
|
|
|
|
instance.exports.main({hello: 'world'});
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testPassExternRefWithGCWithLocals() {
|
2018-12-10 17:28:51 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const ref_sig = builder.addType(kSig_v_r);
|
|
|
|
const void_sig = builder.addType(kSig_v_v);
|
|
|
|
const imp_index = builder.addImport("q", "func", ref_sig);
|
|
|
|
const gc_index = builder.addImport("q", "gc", void_sig);
|
|
|
|
// First call the gc, then check if the object still exists.
|
|
|
|
builder.addFunction('main', ref_sig)
|
2020-09-10 12:39:52 +00:00
|
|
|
.addLocals(kWasmExternRef, 10)
|
2018-12-10 17:28:51 +00:00
|
|
|
.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
|
2018-12-10 17:28:51 +00:00
|
|
|
kExprCallFunction, gc_index, // call gc
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 9, kExprCallFunction, imp_index // call import
|
2018-12-10 17:28:51 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertEquals('world', value.hello);
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});
|
|
|
|
|
|
|
|
instance.exports.main({hello: 'world'});
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testPassExternRefWithGC() {
|
2018-04-06 13:18:49 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const ref_sig = builder.addType(kSig_v_r);
|
|
|
|
const void_sig = builder.addType(kSig_v_v);
|
|
|
|
const imp_index = builder.addImport("q", "func", ref_sig);
|
|
|
|
const gc_index = builder.addImport("q", "gc", void_sig);
|
|
|
|
// 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
|
2018-04-06 13:18:49 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertEquals('world', value.hello);
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});
|
|
|
|
|
|
|
|
instance.exports.main({hello: 'world'});
|
|
|
|
})();
|
2018-04-09 09:00:53 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testPassExternRefWithGCWithStackParameters() {
|
2019-01-21 16:38:01 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const num_params = 15;
|
|
|
|
for (let index = 0; index < num_params; index++) {
|
|
|
|
const builder = new WasmModuleBuilder();
|
2020-06-09 15:54:14 +00:00
|
|
|
// Make a signature with {num_params} many externref parameters.
|
|
|
|
const mysig = makeSig(Array(num_params).fill(kWasmExternRef), []);
|
2019-01-21 16:38:01 +00:00
|
|
|
const main_sig = builder.addType(mysig);
|
|
|
|
const ref_sig = builder.addType(kSig_v_r);
|
|
|
|
const void_sig = builder.addType(kSig_v_v);
|
|
|
|
const imp_index = builder.addImport('q', 'func', ref_sig);
|
|
|
|
const gc_index = builder.addImport('q', 'gc', void_sig);
|
|
|
|
// First call the gc, then check if the object still exists.
|
|
|
|
builder.addFunction('main', main_sig)
|
|
|
|
.addBody([
|
|
|
|
kExprCallFunction, gc_index, // call gc
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, index, kExprCallFunction, imp_index // call import
|
2019-01-21 16:38:01 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
function checkFunction(value) {
|
|
|
|
assertEquals(index, value.hello);
|
|
|
|
}
|
|
|
|
|
|
|
|
const instance = builder.instantiate({q: {func: checkFunction, gc: gc}});
|
|
|
|
|
|
|
|
// Pass {num_params} many parameters to main. Note that it is important
|
|
|
|
// that no other references to these objects exist. They are kept alive
|
|
|
|
// only through references stored in the parameters slots of a stack frame.
|
|
|
|
instance.exports.main(
|
|
|
|
{hello: 0}, {hello: 1}, {hello: 2}, {hello: 3}, {hello: 4}, {hello: 5},
|
|
|
|
{hello: 6}, {hello: 7}, {hello: 8}, {hello: 9}, {hello: 10},
|
|
|
|
{hello: 11}, {hello: 12}, {hello: 13}, {hello: 14});
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testPassExternRefWithGCInWrapper() {
|
2018-12-10 17:28:51 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
2020-06-09 15:54:14 +00:00
|
|
|
const kSig_r_iri = makeSig([kWasmI32, kWasmExternRef, kWasmI32], [kWasmExternRef]);
|
2018-12-10 17:28:51 +00:00
|
|
|
const sig_index = builder.addType(kSig_r_iri);
|
|
|
|
builder.addFunction('main', sig_index)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 1])
|
2018-12-10 17:28:51 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
const triggerGCParam = {
|
|
|
|
valueOf: () => {
|
|
|
|
gc();
|
|
|
|
return 17;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = instance.exports.main(triggerGCParam, {hello: 'world'}, triggerGCParam);
|
|
|
|
assertEquals('world', result.hello);
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testExternRefNull() {
|
2018-04-09 09:00:53 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_r_v)
|
2021-08-09 15:48:11 +00:00
|
|
|
.addBody([kExprRefNull, kExternRefCode])
|
2018-04-09 09:00:53 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(null, instance.exports.main());
|
|
|
|
})();
|
2018-04-09 09:05:30 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testExternRefIsNull() {
|
2018-04-09 09:05:30 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_i_r)
|
2020-06-23 13:41:10 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprRefIsNull])
|
2018-04-09 09:05:30 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(0, instance.exports.main({'hello' : 'world'}));
|
|
|
|
assertEquals(0, instance.exports.main(1234));
|
|
|
|
assertEquals(0, instance.exports.main(0));
|
|
|
|
assertEquals(0, instance.exports.main(123.4));
|
|
|
|
assertEquals(0, instance.exports.main(undefined));
|
|
|
|
assertEquals(1, instance.exports.main(null));
|
|
|
|
assertEquals(0, instance.exports.main(print));
|
2018-09-03 10:39:55 +00:00
|
|
|
})();
|
2018-04-09 09:05:30 +00:00
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testExternRefNullIsNull() {
|
2018-12-10 17:28:51 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_i_v)
|
2021-08-09 15:48:11 +00:00
|
|
|
.addBody([kExprRefNull, kExternRefCode, kExprRefIsNull])
|
2018-12-10 17:28:51 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(1, instance.exports.main());
|
|
|
|
})();
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
(function testExternRefLocalDefaultValue() {
|
2018-09-03 10:39:55 +00:00
|
|
|
print(arguments.callee.name);
|
2021-01-18 08:20:32 +00:00
|
|
|
const numLocals = 3;
|
|
|
|
for (let i = 0; i < numLocals; ++i) {
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_r_v)
|
|
|
|
.addBody([kExprLocalGet, i])
|
|
|
|
.addLocals(kWasmExternRef, numLocals)
|
|
|
|
.exportFunc();
|
2018-09-03 10:39:55 +00:00
|
|
|
|
2021-01-18 08:20:32 +00:00
|
|
|
const instance = builder.instantiate();
|
2018-09-03 10:39:55 +00:00
|
|
|
|
2021-01-18 08:20:32 +00:00
|
|
|
assertEquals(null, instance.exports.main());
|
|
|
|
}
|
2018-04-09 09:05:30 +00:00
|
|
|
})();
|
2019-01-23 12:11:08 +00:00
|
|
|
|
2020-06-10 07:22:22 +00:00
|
|
|
(function testImplicitReturnNullAsExternRef() {
|
2019-01-23 12:11:08 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_r_v);
|
|
|
|
builder.addFunction('main', sig_index)
|
2021-08-09 15:48:11 +00:00
|
|
|
.addBody([kExprRefNull, kExternRefCode])
|
2019-01-23 12:11:08 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
assertEquals(null, main());
|
|
|
|
})();
|
|
|
|
|
2020-06-10 07:22:22 +00:00
|
|
|
(function testExplicitReturnNullAsExternRef() {
|
2019-01-23 12:11:08 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
const sig_index = builder.addType(kSig_r_v);
|
|
|
|
builder.addFunction('main', sig_index)
|
2021-08-09 15:48:11 +00:00
|
|
|
.addBody([kExprRefNull, kExternRefCode, kExprReturn])
|
2019-01-23 12:11:08 +00:00
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const main = builder.instantiate().exports.main;
|
|
|
|
assertEquals(null, main());
|
|
|
|
})();
|
2020-11-30 14:33:24 +00:00
|
|
|
|
|
|
|
(function testGCInStackCheck() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
const gc_sig = builder.addType(kSig_v_v);
|
2020-12-16 12:41:19 +00:00
|
|
|
const mysig = makeSig(
|
|
|
|
[
|
|
|
|
kWasmExternRef, kWasmI32, kWasmExternRef, kWasmExternRef, kWasmExternRef
|
|
|
|
],
|
|
|
|
[]);
|
|
|
|
const func_sig = builder.addType(mysig);
|
2020-11-30 14:33:24 +00:00
|
|
|
const triggerGC_index = builder.addImport('q', 'triggerGC', gc_sig);
|
|
|
|
const func_index = builder.addImport('q', 'func', func_sig);
|
|
|
|
|
2020-12-16 12:41:19 +00:00
|
|
|
const foo = builder.addFunction('foo', func_sig).addBody([
|
|
|
|
kExprLocalGet, 0, // --
|
|
|
|
kExprLocalGet, 1, // --
|
|
|
|
kExprLocalGet, 2, // --
|
|
|
|
kExprLocalGet, 3, // --
|
|
|
|
kExprLocalGet, 4, // --
|
|
|
|
kExprCallFunction, func_index
|
2020-11-30 14:33:24 +00:00
|
|
|
]);
|
|
|
|
|
2020-12-16 12:41:19 +00:00
|
|
|
builder.addFunction('main', func_sig)
|
2020-11-30 14:33:24 +00:00
|
|
|
.addBody([
|
2020-12-16 12:41:19 +00:00
|
|
|
kExprCallFunction, triggerGC_index, // --
|
|
|
|
kExprLocalGet, 0, // --
|
|
|
|
kExprLocalGet, 1, // --
|
|
|
|
kExprLocalGet, 2, // --
|
|
|
|
kExprLocalGet, 3, // --
|
|
|
|
kExprLocalGet, 4, // --
|
|
|
|
kExprCallFunction, foo.index
|
2020-11-30 14:33:24 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate({
|
|
|
|
q: {
|
|
|
|
triggerGC: () => %ScheduleGCInStackCheck(),
|
|
|
|
func: (ref) => assertEquals(ref.hello, 4)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-12-16 12:41:19 +00:00
|
|
|
instance.exports.main({hello: 4}, 5, {world: 6}, null, {bar: 7});
|
2020-11-30 14:33:24 +00:00
|
|
|
})();
|
2021-01-25 21:55:14 +00:00
|
|
|
|
|
|
|
(function testGCInStackCheckUnalignedFrameSize() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
const builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
const gc_sig = builder.addType(kSig_v_v);
|
|
|
|
const mysig = makeSig(
|
|
|
|
[
|
|
|
|
kWasmExternRef, kWasmI32, kWasmExternRef, kWasmExternRef, kWasmExternRef
|
|
|
|
],
|
|
|
|
[]);
|
|
|
|
const func_sig = builder.addType(mysig);
|
|
|
|
const triggerGC_index = builder.addImport('q', 'triggerGC', gc_sig);
|
|
|
|
const func_index = builder.addImport('q', 'func', func_sig);
|
|
|
|
|
|
|
|
const foo = builder.addFunction('foo', func_sig).addBody([
|
|
|
|
kExprLocalGet, 0, // --
|
|
|
|
kExprLocalGet, 1, // --
|
|
|
|
kExprLocalGet, 2, // --
|
|
|
|
kExprLocalGet, 3, // --
|
|
|
|
kExprLocalGet, 4, // --
|
|
|
|
kExprCallFunction, func_index
|
|
|
|
]).addLocals(kWasmI32, 1);
|
|
|
|
|
|
|
|
builder.addFunction('main', func_sig)
|
|
|
|
.addBody([
|
|
|
|
kExprCallFunction, triggerGC_index, // --
|
|
|
|
kExprLocalGet, 0, // --
|
|
|
|
kExprLocalGet, 1, // --
|
|
|
|
kExprLocalGet, 2, // --
|
|
|
|
kExprLocalGet, 3, // --
|
|
|
|
kExprLocalGet, 4, // --
|
|
|
|
kExprCallFunction, foo.index
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
const instance = builder.instantiate({
|
|
|
|
q: {
|
|
|
|
triggerGC: () => %ScheduleGCInStackCheck(),
|
|
|
|
func: (ref) => assertEquals(ref.hello, 4)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.exports.main({hello: 4}, 5, {world: 6}, null, {bar: 7});
|
|
|
|
})();
|
2021-03-23 15:11:52 +00:00
|
|
|
|
|
|
|
(function MultiReturnRefTest() {
|
|
|
|
print("MultiReturnTest");
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let sig = makeSig([kWasmExternRef],
|
|
|
|
[kWasmExternRef, kWasmExternRef, kWasmExternRef, kWasmExternRef]);
|
|
|
|
|
|
|
|
builder.addFunction("callee", sig)
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
]);
|
|
|
|
builder.addFunction("main", sig)
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprCallFunction, 0
|
|
|
|
])
|
|
|
|
.exportAs("main");
|
|
|
|
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
let instance = new WebAssembly.Instance(module);
|
|
|
|
assertEquals(instance.exports.main(null), [null, null, null, null]);
|
|
|
|
})();
|