2018-10-25 14:52:29 +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.
|
|
|
|
|
2020-06-09 15:54:14 +00:00
|
|
|
// Flags: --experimental-wasm-eh --experimental-wasm-reftypes --allow-natives-syntax
|
2018-10-25 14:52:29 +00:00
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
d8.file.execute("test/mjsunit/wasm/exceptions-utils.js");
|
2018-10-25 14:52:29 +00:00
|
|
|
|
|
|
|
// Test the encoding of a thrown exception with a null-ref value.
|
|
|
|
(function TestThrowRefNull() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2021-07-29 11:09:02 +00:00
|
|
|
let except = builder.addTag(kSig_v_r);
|
2018-10-25 14:52:29 +00:00
|
|
|
builder.addFunction("throw_null", kSig_v_v)
|
|
|
|
.addBody([
|
2020-06-09 15:54:14 +00:00
|
|
|
kExprRefNull, kWasmExternRef,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertWasmThrows(instance, except, [null], () => instance.exports.throw_null());
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching the null-ref value.
|
|
|
|
(function TestThrowCatchRefNull() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2021-07-29 11:09:02 +00:00
|
|
|
let except = builder.addTag(kSig_v_r);
|
2018-10-25 14:52:29 +00:00
|
|
|
builder.addFunction("throw_catch_null", kSig_i_i)
|
|
|
|
.addBody([
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprI32Eqz,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprIf, kWasmI32,
|
2020-06-09 15:54:14 +00:00
|
|
|
kExprRefNull, kWasmExternRef,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprElse,
|
|
|
|
kExprI32Const, 42,
|
|
|
|
kExprEnd,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprRefIsNull,
|
|
|
|
kExprIf, kWasmI32,
|
|
|
|
kExprI32Const, 23,
|
|
|
|
kExprElse,
|
|
|
|
kExprUnreachable,
|
|
|
|
kExprEnd,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(23, instance.exports.throw_catch_null(0));
|
|
|
|
assertEquals(42, instance.exports.throw_catch_null(1));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test the encoding of a thrown exception with a reference type value.
|
|
|
|
(function TestThrowRefParam() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2021-07-29 11:09:02 +00:00
|
|
|
let except = builder.addTag(kSig_v_r);
|
2018-10-25 14:52:29 +00:00
|
|
|
builder.addFunction("throw_param", kSig_v_r)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let o = new Object();
|
|
|
|
|
|
|
|
assertWasmThrows(instance, except, [o], () => instance.exports.throw_param(o));
|
|
|
|
assertWasmThrows(instance, except, [1], () => instance.exports.throw_param(1));
|
|
|
|
assertWasmThrows(instance, except, [2.3], () => instance.exports.throw_param(2.3));
|
|
|
|
assertWasmThrows(instance, except, ["str"], () => instance.exports.throw_param("str"));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching the reference type value.
|
|
|
|
(function TestThrowCatchRefParam() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
2021-07-29 11:09:02 +00:00
|
|
|
let except = builder.addTag(kSig_v_r);
|
2018-10-25 14:52:29 +00:00
|
|
|
builder.addFunction("throw_catch_param", kSig_r_r)
|
|
|
|
.addBody([
|
2020-06-09 15:54:14 +00:00
|
|
|
kExprTry, kWasmExternRef,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprThrow, except,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
// fall-through
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let o = new Object();
|
|
|
|
|
|
|
|
assertEquals(o, instance.exports.throw_catch_param(o));
|
|
|
|
assertEquals(1, instance.exports.throw_catch_param(1));
|
|
|
|
assertEquals(2.3, instance.exports.throw_catch_param(2.3));
|
|
|
|
assertEquals("str", instance.exports.throw_catch_param("str"));
|
|
|
|
})();
|