2016-10-06 15:43:10 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2017-10-16 08:49:45 +00:00
|
|
|
(function TestMultipleInstances() {
|
|
|
|
print("TestMultipleInstances");
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
let g = builder.addGlobal(kWasmI32, true);
|
|
|
|
let sig_index = builder.addType(kSig_i_v);
|
|
|
|
builder.addFunction("get", sig_index)
|
|
|
|
.addBody([
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalGet, g.index])
|
2017-10-16 08:49:45 +00:00
|
|
|
.exportAs("get");
|
|
|
|
builder.addFunction("set", kSig_v_i)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalSet, g.index])
|
2017-10-16 08:49:45 +00:00
|
|
|
.exportAs("set");
|
|
|
|
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
|
|
|
|
let a = new WebAssembly.Instance(module);
|
|
|
|
let b = new WebAssembly.Instance(module);
|
|
|
|
|
|
|
|
assertEquals(0, a.exports.get());
|
|
|
|
assertEquals(0, b.exports.get());
|
|
|
|
|
|
|
|
a.exports.set(1);
|
|
|
|
|
|
|
|
assertEquals(1, a.exports.get());
|
|
|
|
assertEquals(0, b.exports.get());
|
|
|
|
|
|
|
|
b.exports.set(6);
|
|
|
|
|
|
|
|
assertEquals(1, a.exports.get());
|
|
|
|
assertEquals(6, b.exports.get());
|
|
|
|
|
|
|
|
a.exports.set(7);
|
|
|
|
|
|
|
|
assertEquals(7, a.exports.get());
|
|
|
|
assertEquals(6, b.exports.get());
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
function TestImported(type, val, expected) {
|
|
|
|
print("TestImported " + type + "(" + val +")" + " = " + expected);
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var sig = makeSig([], [type]);
|
2016-12-20 15:32:56 +00:00
|
|
|
var g = builder.addImportedGlobal("uuu", "foo", type);
|
2016-10-06 15:43:10 +00:00
|
|
|
builder.addFunction("main", sig)
|
2019-10-08 13:16:30 +00:00
|
|
|
.addBody([kExprGlobalGet, g])
|
2016-10-06 15:43:10 +00:00
|
|
|
.exportAs("main");
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = builder.instantiate({uuu: {foo: val}});
|
2016-10-06 15:43:10 +00:00
|
|
|
assertEquals(expected, instance.exports.main());
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestImported(kWasmI32, 300.1, 300);
|
|
|
|
TestImported(kWasmF32, 87234.87238, Math.fround(87234.87238));
|
|
|
|
TestImported(kWasmF64, 77777.88888, 77777.88888);
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
|
2017-10-16 08:49:45 +00:00
|
|
|
(function TestImportedMultipleInstances() {
|
|
|
|
print("TestImportedMultipleInstances");
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
let g = builder.addImportedGlobal("mod", "g", kWasmI32);
|
|
|
|
let sig_index = builder.addType(kSig_i_v);
|
|
|
|
builder.addFunction("main", sig_index)
|
|
|
|
.addBody([
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalGet, g])
|
2017-10-16 08:49:45 +00:00
|
|
|
.exportAs("main");
|
|
|
|
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
|
|
|
|
print(" i 100...");
|
|
|
|
let i100 = new WebAssembly.Instance(module, {mod: {g: 100}});
|
|
|
|
assertEquals(100, i100.exports.main());
|
|
|
|
|
|
|
|
print(" i 300...");
|
|
|
|
let i300 = new WebAssembly.Instance(module, {mod: {g: 300}});
|
|
|
|
assertEquals(300, i300.exports.main());
|
|
|
|
})();
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
function TestExported(type, val, expected) {
|
|
|
|
print("TestExported " + type + "(" + val +")" + " = " + expected);
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2021-04-27 15:05:16 +00:00
|
|
|
builder.addGlobal(type, false, val).exportAs("foo");
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
var instance = builder.instantiate();
|
2018-06-12 22:23:35 +00:00
|
|
|
assertEquals(expected, instance.exports.foo.value);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-20 14:37:49 +00:00
|
|
|
TestExported(kWasmI32, wasmI32Const(455.5), 455);
|
|
|
|
TestExported(kWasmF32, wasmF32Const(-999.34343),
|
2021-04-27 15:05:16 +00:00
|
|
|
Math.fround(-999.34343));
|
2022-06-20 14:37:49 +00:00
|
|
|
TestExported(kWasmF64, wasmF64Const(87347.66666), 87347.66666);
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2017-01-13 14:38:37 +00:00
|
|
|
(function TestI64Exported() {
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2022-06-20 14:37:49 +00:00
|
|
|
builder.addGlobal(kWasmI64, false, wasmI64Const(1234)).exportAs("foo");
|
2017-01-13 14:38:37 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
|
|
|
|
2018-06-12 22:23:35 +00:00
|
|
|
var instance = builder.instantiate();
|
|
|
|
assertTrue(instance.exports.foo instanceof WebAssembly.Global);
|
2020-06-12 17:32:50 +00:00
|
|
|
assertEquals(instance.exports.foo.value, 1234n);
|
2017-01-13 14:38:37 +00:00
|
|
|
})();
|
2016-10-06 15:43:10 +00:00
|
|
|
|
|
|
|
function TestImportedExported(type, val, expected) {
|
2021-04-27 15:05:16 +00:00
|
|
|
print("TestImportedExported " + type + "(" + val + ")" + " = " + expected);
|
2016-10-06 15:43:10 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
2016-12-20 15:32:56 +00:00
|
|
|
var i = builder.addImportedGlobal("ttt", "foo", type);
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2022-06-20 14:37:49 +00:00
|
|
|
builder.addGlobal(type, false, [kExprGlobalGet, i]).exportAs("bar");
|
2016-12-21 13:43:00 +00:00
|
|
|
builder.addGlobal(kWasmI32); // pad
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = builder.instantiate({ttt: {foo: val}});
|
2018-06-12 22:23:35 +00:00
|
|
|
assertEquals(expected, instance.exports.bar.value);
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestImportedExported(kWasmI32, 415.5, 415);
|
|
|
|
TestImportedExported(kWasmF32, -979.34343, Math.fround(-979.34343));
|
|
|
|
TestImportedExported(kWasmF64, 81347.66666, 81347.66666);
|
2016-10-12 15:19:22 +00:00
|
|
|
|
|
|
|
function TestGlobalIndexSpace(type, val) {
|
|
|
|
print("TestGlobalIndexSpace(" + val + ") = " + val);
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-12-20 15:32:56 +00:00
|
|
|
var im = builder.addImportedGlobal("nnn", "foo", type);
|
2016-10-12 15:19:22 +00:00
|
|
|
assertEquals(0, im);
|
2022-06-20 14:37:49 +00:00
|
|
|
var def = builder.addGlobal(type, false, [kExprGlobalGet, im]);
|
2016-10-12 15:19:22 +00:00
|
|
|
assertEquals(1, def.index);
|
|
|
|
|
|
|
|
var sig = makeSig([], [type]);
|
|
|
|
builder.addFunction("main", sig)
|
2019-10-08 13:16:30 +00:00
|
|
|
.addBody([kExprGlobalGet, def.index])
|
2016-10-12 15:19:22 +00:00
|
|
|
.exportAs("main");
|
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
var instance = builder.instantiate({nnn: {foo: val}});
|
2016-10-12 15:19:22 +00:00
|
|
|
assertEquals(val, instance.exports.main());
|
|
|
|
}
|
|
|
|
|
2016-12-21 13:43:00 +00:00
|
|
|
TestGlobalIndexSpace(kWasmI32, 123);
|
|
|
|
TestGlobalIndexSpace(kWasmF32, 54321.125);
|
|
|
|
TestGlobalIndexSpace(kWasmF64, 12345.678);
|
2017-10-16 08:49:45 +00:00
|
|
|
|
|
|
|
(function TestAccessesInBranch() {
|
|
|
|
print("TestAccessesInBranches");
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
let g = builder.addGlobal(kWasmI32, true);
|
|
|
|
let h = builder.addGlobal(kWasmI32, true);
|
|
|
|
let sig_index = builder.addType(kSig_i_i);
|
|
|
|
builder.addFunction("get", sig_index)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2017-10-16 08:49:45 +00:00
|
|
|
kExprIf, kWasmI32,
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalGet, g.index,
|
2017-10-16 08:49:45 +00:00
|
|
|
kExprElse,
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalGet, h.index,
|
2017-10-16 08:49:45 +00:00
|
|
|
kExprEnd])
|
|
|
|
.exportAs("get");
|
|
|
|
builder.addFunction("set", kSig_v_ii)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalSet, g.index,
|
2017-10-16 08:49:45 +00:00
|
|
|
kExprElse,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2019-10-08 13:16:30 +00:00
|
|
|
kExprGlobalSet, h.index,
|
2017-10-16 08:49:45 +00:00
|
|
|
kExprEnd])
|
|
|
|
.exportAs("set");
|
|
|
|
|
|
|
|
let module = new WebAssembly.Module(builder.toBuffer());
|
|
|
|
|
|
|
|
let a = new WebAssembly.Instance(module);
|
|
|
|
let get = a.exports.get;
|
|
|
|
let set = a.exports.set;
|
|
|
|
|
|
|
|
assertEquals(0, get(0));
|
|
|
|
assertEquals(0, get(1));
|
|
|
|
set(0, 1);
|
|
|
|
assertEquals(1, get(0));
|
|
|
|
assertEquals(0, get(1));
|
|
|
|
|
|
|
|
set(0, 7);
|
|
|
|
assertEquals(7, get(0));
|
|
|
|
assertEquals(0, get(1));
|
|
|
|
|
|
|
|
set(1, 9);
|
|
|
|
assertEquals(7, get(0));
|
|
|
|
assertEquals(9, get(1));
|
|
|
|
|
|
|
|
})();
|
2021-06-07 13:03:19 +00:00
|
|
|
|
|
|
|
(function testAssignUndefinedToGlobal() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let i32_global = new WebAssembly.Global({mutable: true, value: 'i32'});
|
|
|
|
i32_global.value = undefined;
|
|
|
|
assertSame(0, i32_global.value);
|
|
|
|
let i64_global = new WebAssembly.Global({mutable: true, value: 'i64'});
|
|
|
|
assertThrows(() => {
|
|
|
|
i64_global.value = undefined;
|
|
|
|
}, TypeError);
|
|
|
|
let f32_global = new WebAssembly.Global({mutable: true, value: 'f32'});
|
|
|
|
f32_global.value = undefined;
|
|
|
|
assertSame(NaN, f32_global.value);
|
|
|
|
let f64_global = new WebAssembly.Global({mutable: true, value: 'f64'});
|
|
|
|
f64_global.value = undefined;
|
|
|
|
assertSame(NaN, f64_global.value);
|
|
|
|
})();
|