2018-10-11 10:58:24 +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: --expose-wasm --experimental-wasm-eh --allow-natives-syntax
|
|
|
|
|
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2018-12-10 09:14:30 +00:00
|
|
|
load("test/mjsunit/wasm/exceptions-utils.js");
|
2018-10-11 10:58:24 +00:00
|
|
|
|
2020-12-03 18:00:05 +00:00
|
|
|
// Test that rethrow expressions can target catch blocks.
|
2018-10-11 10:58:24 +00:00
|
|
|
(function TestRethrowInCatch() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction("rethrow0", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprThrow, except,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprRethrow, 0,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction("rethrow1", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprI32Eqz,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprRethrow, 1,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, 23,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2018-12-10 09:14:30 +00:00
|
|
|
assertWasmThrows(instance, except, [], () => instance.exports.rethrow0());
|
|
|
|
assertWasmThrows(instance, except, [], () => instance.exports.rethrow1(0));
|
2018-10-11 10:58:24 +00:00
|
|
|
assertEquals(23, instance.exports.rethrow1(1));
|
|
|
|
})();
|
|
|
|
|
2020-12-03 18:00:05 +00:00
|
|
|
// Test that rethrow expressions can target catch-all blocks.
|
|
|
|
(function TestRethrowInCatchAll() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction("rethrow0", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprThrow, except,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprRethrow, 0,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
2020-12-03 18:00:05 +00:00
|
|
|
builder.addFunction("rethrow1", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprI32Eqz,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprRethrow, 1,
|
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, 23,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
2020-12-03 18:00:05 +00:00
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertWasmThrows(instance, except, [], () => instance.exports.rethrow0());
|
|
|
|
assertWasmThrows(instance, except, [], () => instance.exports.rethrow1(0));
|
|
|
|
assertEquals(23, instance.exports.rethrow1(1));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test that rethrow expression properly target the correct surrounding try
|
|
|
|
// block even in the presence of multiple handlers being involved.
|
2018-10-11 10:58:24 +00:00
|
|
|
(function TestRethrowNested() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
let except2 = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction("rethrow_nested", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except2,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except2,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except1,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except1,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprI32Const, 0,
|
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprRethrow, 1,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprEnd,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprRethrow, 2,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, 23,
|
|
|
|
kExprEnd,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2018-12-10 09:14:30 +00:00
|
|
|
assertWasmThrows(instance, except1, [], () => instance.exports.rethrow_nested(0));
|
|
|
|
assertWasmThrows(instance, except2, [], () => instance.exports.rethrow_nested(1));
|
2018-10-11 10:58:24 +00:00
|
|
|
assertEquals(23, instance.exports.rethrow_nested(2));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test that an exception being rethrow can be caught by another local catch
|
|
|
|
// block in the same function without ever unwinding the activation.
|
|
|
|
(function TestRethrowRecatch() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction("rethrow_recatch", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprI32Eqz,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprRethrow, 2,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, 42,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-10-11 10:58:24 +00:00
|
|
|
kExprI32Const, 23,
|
|
|
|
kExprEnd,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(23, instance.exports.rethrow_recatch(0));
|
|
|
|
assertEquals(42, instance.exports.rethrow_recatch(1));
|
|
|
|
})();
|