2018-12-07 19:50:26 +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.
|
|
|
|
|
|
|
|
// Flags: --experimental-wasm-bigint
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
|
|
|
|
(function TestWasmI64ToJSBigInt() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
builder
|
|
|
|
.addFunction("fn", kSig_l_v) // () -> i64
|
|
|
|
.addBody([
|
|
|
|
kExprI64Const, 0x3,
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let module = builder.instantiate();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
assertEquals(typeof module.exports.fn(), "bigint");
|
|
|
|
assertEquals(module.exports.fn(), 3n);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestJSBigIntToWasmI64Global() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let a_global_index = builder
|
2019-09-09 13:17:46 +00:00
|
|
|
.addImportedGlobal("mod", "a", kWasmI64);
|
2018-12-07 19:50:26 +00:00
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let b_global_index = builder
|
2018-12-07 19:50:26 +00:00
|
|
|
.addImportedGlobal("mod", "b", kWasmI64);
|
|
|
|
|
|
|
|
builder
|
|
|
|
.addExportOfKind('a', kExternalGlobal, a_global_index)
|
|
|
|
.addExportOfKind('b', kExternalGlobal, b_global_index)
|
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let module = builder.instantiate({
|
2018-12-07 19:50:26 +00:00
|
|
|
mod: {
|
|
|
|
a: 1n,
|
|
|
|
b: 2n ** 63n,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(module.exports.a.value, 1n);
|
|
|
|
assertEquals(module.exports.b.value, - (2n ** 63n));
|
2019-09-09 13:17:46 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestJSBigIntGlobalImportInvalidType() {
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addImportedGlobal("mod", "a", kWasmI64);
|
|
|
|
assertThrows(() => builder.instantiate({mod: { a: {} } }), WebAssembly.LinkError);
|
2018-12-07 19:50:26 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestJSBigIntToWasmI64MutableGlobal() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let a_global_index = builder
|
2018-12-07 19:50:26 +00:00
|
|
|
.addImportedGlobal("mod", "a", kWasmI64, /* mutable = */ true)
|
|
|
|
|
|
|
|
builder
|
|
|
|
.addExportOfKind('a', kExternalGlobal, a_global_index);
|
|
|
|
|
|
|
|
// as non object
|
2019-08-20 12:55:43 +00:00
|
|
|
let fn = () => builder.instantiate({
|
2018-12-07 19:50:26 +00:00
|
|
|
mod: {
|
|
|
|
a: 1n,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
assertThrows(fn, WebAssembly.LinkError);
|
|
|
|
|
|
|
|
// as WebAssembly.Global object
|
2019-08-20 12:55:43 +00:00
|
|
|
let module = builder.instantiate({
|
2018-12-07 19:50:26 +00:00
|
|
|
mod: {
|
|
|
|
a: new WebAssembly.Global({ value: "i64", mutable: true }, 1n),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
assertEquals(module.exports.a.value, 1n);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestJSBigIntToWasmI64Identity() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
builder
|
|
|
|
.addFunction("f", kSig_l_l) // i64 -> i64
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-12-07 19:50:26 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let module = builder.instantiate();
|
|
|
|
let f = module.exports.f;
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
assertEquals(f(0n), 0n);
|
|
|
|
assertEquals(f(123n), 123n);
|
|
|
|
assertEquals(f(-123n), -123n);
|
|
|
|
|
|
|
|
assertEquals(f("5"), 5n);
|
|
|
|
|
|
|
|
assertThrows(() => f(5), TypeError);
|
|
|
|
})();
|
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
(function TestJSBigIntToWasmI64Projection() {
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder
|
|
|
|
.addFunction("f", kSig_l_ll) // i64 -> i64
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2019-08-20 12:55:43 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
let module = builder.instantiate();
|
|
|
|
let f = module.exports.f;
|
|
|
|
|
|
|
|
assertEquals(f(1n, 0n), 0n);
|
|
|
|
assertEquals(f(1n, 123n), 123n);
|
|
|
|
assertEquals(f(1n, -123n), -123n);
|
|
|
|
|
|
|
|
assertEquals(f(1n, "5"), 5n);
|
|
|
|
|
|
|
|
assertThrows(() => f(1n, 5), TypeError);
|
|
|
|
})();
|
|
|
|
|
2018-12-07 19:50:26 +00:00
|
|
|
(function TestI64Global() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let argument = { "value": "i64", "mutable": true };
|
|
|
|
let global = new WebAssembly.Global(argument);
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
assertEquals(global.value, 0n); // initial value
|
|
|
|
|
|
|
|
global.value = 123n;
|
|
|
|
assertEquals(global.valueOf(), 123n);
|
|
|
|
|
|
|
|
global.value = 2n ** 63n;
|
|
|
|
assertEquals(global.valueOf(), - (2n ** 63n));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestI64GlobalValueOf() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let argument = { "value": "i64" };
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
// as literal
|
2019-08-20 12:55:43 +00:00
|
|
|
let global = new WebAssembly.Global(argument, {
|
2018-12-07 19:50:26 +00:00
|
|
|
valueOf() {
|
|
|
|
return 123n;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assertEquals(global.value, 123n);
|
|
|
|
|
|
|
|
// as string
|
2019-08-20 12:55:43 +00:00
|
|
|
let global2 = new WebAssembly.Global(argument, {
|
2018-12-07 19:50:26 +00:00
|
|
|
valueOf() {
|
|
|
|
return "321";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assertEquals(global2.value, 321n);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestInvalidValtypeGlobalErrorMessage() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let argument = { "value": "some string" };
|
2018-12-07 19:50:26 +00:00
|
|
|
assertThrows(() => new WebAssembly.Global(argument), TypeError);
|
|
|
|
|
|
|
|
try {
|
|
|
|
new WebAssembly.Global(argument);
|
|
|
|
} catch (e) {
|
2020-06-16 13:53:59 +00:00
|
|
|
assertContains("'value' must be a WebAssembly type", e.message);
|
2018-12-07 19:50:26 +00:00
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestGlobalI64ValueWrongType() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let argument = { "value": "i64" };
|
2018-12-07 19:50:26 +00:00
|
|
|
assertThrows(() => new WebAssembly.Global(argument, 666), TypeError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestGlobalI64SetWrongType() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let argument = { "value": "i64", "mutable": true };
|
|
|
|
let global = new WebAssembly.Global(argument);
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
assertThrows(() => global.value = 1, TypeError);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestFuncParamF64PassingBigInt() {
|
2019-08-20 12:55:43 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
builder
|
|
|
|
.addFunction("f", kSig_v_d) // f64 -> ()
|
|
|
|
.addBody([])
|
|
|
|
.exportFunc();
|
|
|
|
|
2019-08-20 12:55:43 +00:00
|
|
|
let module = builder.instantiate();
|
2018-12-07 19:50:26 +00:00
|
|
|
|
|
|
|
assertThrows(() => module.exports.f(123n), TypeError);
|
|
|
|
})();
|