2018-08-28 11:36:28 +00:00
|
|
|
// Copyright 2018 the V8 project authors. All rights reserved.
|
2016-09-15 15:04:03 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2018-09-04 09:51:28 +00:00
|
|
|
// Flags: --expose-wasm --experimental-wasm-eh --allow-natives-syntax
|
2016-09-15 15:04:03 +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-09-04 09:51:28 +00:00
|
|
|
|
2017-08-01 20:56:39 +00:00
|
|
|
// The following method doesn't attempt to catch an raised exception.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowSimple() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
2017-08-01 20:56:39 +00:00
|
|
|
builder.addFunction("throw_if_param_not_zero", kSig_i_i)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2017-08-01 20:56:39 +00:00
|
|
|
kExprI32Const, 0,
|
|
|
|
kExprI32Ne,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-08-01 20:56:39 +00:00
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, 1
|
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-08-01 20:56:39 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
assertEquals(1, instance.exports.throw_if_param_not_zero(0));
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [], () => instance.exports.throw_if_param_not_zero(10));
|
|
|
|
assertWasmThrows(instance, except, [], () => instance.exports.throw_if_param_not_zero(-1));
|
2017-08-01 20:56:39 +00:00
|
|
|
})();
|
|
|
|
|
2018-09-04 10:44:53 +00:00
|
|
|
// Test that empty try/catch blocks work.
|
|
|
|
(function TestCatchEmptyBlocks() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-09-04 10:44:53 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2020-12-03 18:00:05 +00:00
|
|
|
let except = builder.addException(kSig_v_v);
|
2018-09-04 10:44:53 +00:00
|
|
|
builder.addFunction("catch_empty_try", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-09-04 10:44:53 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertDoesNotThrow(instance.exports.catch_empty_try);
|
|
|
|
})();
|
|
|
|
|
2017-08-01 20:56:39 +00:00
|
|
|
// Now that we know throwing works, we test catching the exceptions we raise.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestCatchSimple() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
2017-08-01 20:56:39 +00:00
|
|
|
builder.addFunction("simple_throw_catch_to_0_1", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2017-08-01 20:56:39 +00:00
|
|
|
kExprI32Eqz,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-08-01 20:56:39 +00:00
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 42,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 23,
|
2017-08-01 20:56:39 +00:00
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-08-01 20:56:39 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
assertEquals(23, instance.exports.simple_throw_catch_to_0_1(0));
|
|
|
|
assertEquals(42, instance.exports.simple_throw_catch_to_0_1(1));
|
2017-08-01 20:56:39 +00:00
|
|
|
})();
|
|
|
|
|
2020-03-23 12:56:45 +00:00
|
|
|
(function TestTrapNotCaught() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('unreachable_in_try', kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2020-03-23 12:56:45 +00:00
|
|
|
kExprUnreachable,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction('unreachable_in_try_unwind', kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprUnreachable,
|
|
|
|
kExprUnwind,
|
2020-03-23 12:56:45 +00:00
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertTraps(kTrapUnreachable, () => instance.exports.unreachable_in_try());
|
2021-02-04 13:27:16 +00:00
|
|
|
assertTraps(kTrapUnreachable, () => instance.exports.unreachable_in_try_unwind());
|
2020-03-23 12:56:45 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestTrapInCalleeNotCaught() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let func_div = builder.addFunction('div', kSig_i_ii).addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprI32DivU
|
|
|
|
]);
|
|
|
|
builder.addFunction('trap_in_callee', kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprCallFunction, func_div.index,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprI32Const, 11,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction('trap_in_callee_unwind', kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprCallFunction, func_div.index,
|
|
|
|
kExprUnwind,
|
2020-03-23 12:56:45 +00:00
|
|
|
kExprI32Const, 11,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(3, instance.exports.trap_in_callee(7, 2));
|
|
|
|
assertTraps(kTrapDivByZero, () => instance.exports.trap_in_callee(1, 0));
|
2021-02-04 13:27:16 +00:00
|
|
|
assertTraps(kTrapDivByZero, () => instance.exports.trap_in_callee_unwind(1, 0));
|
2020-03-23 12:56:45 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestTrapViaJSNotCaught() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let imp = builder.addImport('imp', 'ort', kSig_i_v);
|
|
|
|
builder.addFunction('div', kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprI32DivU
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction('call_import', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprCallFunction, imp,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprI32Const, 11,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction('call_import_unwind', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprCallFunction, imp,
|
|
|
|
kExprUnwind,
|
2020-03-23 12:56:45 +00:00
|
|
|
kExprI32Const, 11,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let exception = undefined;
|
|
|
|
let instance;
|
|
|
|
function js_import() {
|
|
|
|
try {
|
|
|
|
instance.exports.div(1, 0);
|
|
|
|
} catch (e) {
|
|
|
|
exception = e;
|
|
|
|
}
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
instance = builder.instantiate({imp: {ort: js_import}});
|
2021-02-04 13:27:16 +00:00
|
|
|
|
2020-03-23 12:56:45 +00:00
|
|
|
let caught = undefined;
|
|
|
|
try {
|
|
|
|
let res = instance.exports.call_import();
|
|
|
|
assertUnreachable('call_import should trap, but returned with ' + res);
|
|
|
|
} catch (e) {
|
|
|
|
caught = e;
|
|
|
|
}
|
|
|
|
assertSame(exception, caught);
|
|
|
|
assertInstanceof(exception, WebAssembly.RuntimeError);
|
|
|
|
assertEquals(exception.message, kTrapMsgs[kTrapDivByZero]);
|
2021-02-04 13:27:16 +00:00
|
|
|
|
|
|
|
// Same test with unwind instead of catch_all.
|
|
|
|
caught = undefined;
|
|
|
|
try {
|
|
|
|
let res = instance.exports.call_import_unwind();
|
|
|
|
assertUnreachable('call_import_unwind should trap, but returned with ' + res);
|
|
|
|
} catch (e) {
|
|
|
|
caught = e;
|
|
|
|
}
|
|
|
|
assertSame(exception, caught);
|
|
|
|
assertInstanceof(exception, WebAssembly.RuntimeError);
|
|
|
|
assertEquals(exception.message, kTrapMsgs[kTrapDivByZero]);
|
2020-03-23 12:56:45 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestManuallyThrownRuntimeErrorCaught() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let imp = builder.addImport('imp', 'ort', kSig_i_v);
|
|
|
|
builder.addFunction('call_import', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprCallFunction, imp,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprI32Const, 11,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction('call_import_unwind', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprCallFunction, imp,
|
|
|
|
kExprUnwind,
|
2020-03-23 12:56:45 +00:00
|
|
|
kExprI32Const, 11,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
function throw_exc() {
|
2020-04-27 12:01:31 +00:00
|
|
|
throw new WebAssembly.RuntimeError('My user text');
|
2020-03-23 12:56:45 +00:00
|
|
|
}
|
|
|
|
let instance = builder.instantiate({imp: {ort: throw_exc}});
|
|
|
|
|
|
|
|
assertEquals(11, instance.exports.call_import());
|
2021-02-16 17:36:18 +00:00
|
|
|
assertThrows(instance.exports.call_import_unwind, WebAssembly.RuntimeError, "My user text");
|
2020-03-23 12:56:45 +00:00
|
|
|
})();
|
|
|
|
|
2020-04-27 12:01:31 +00:00
|
|
|
(function TestExnWithWasmProtoNotCaught() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
let imp = builder.addImport('imp', 'ort', kSig_v_v);
|
|
|
|
let throw_fn = builder.addFunction('throw', kSig_v_v)
|
|
|
|
.addBody([kExprThrow, except])
|
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction('test', kSig_v_v)
|
|
|
|
.addBody([
|
|
|
|
// Calling "throw" directly should produce the expected exception.
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2020-04-27 12:01:31 +00:00
|
|
|
kExprCallFunction, throw_fn.index,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2020-04-27 12:01:31 +00:00
|
|
|
kExprEnd,
|
|
|
|
// Calling through JS produces a wrapped exceptions which does not match
|
2021-01-15 15:30:01 +00:00
|
|
|
// the catch.
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2020-04-27 12:01:31 +00:00
|
|
|
kExprCallFunction, imp,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2020-04-27 12:01:31 +00:00
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance;
|
|
|
|
let wrapped_exn;
|
|
|
|
function js_import() {
|
|
|
|
try {
|
|
|
|
instance.exports.throw();
|
|
|
|
} catch (e) {
|
|
|
|
wrapped_exn = new Error();
|
|
|
|
wrapped_exn.__proto__ = e;
|
|
|
|
throw wrapped_exn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
instance = builder.instantiate({imp: {ort: js_import}});
|
|
|
|
let caught = undefined;
|
|
|
|
try {
|
|
|
|
instance.exports.test();
|
|
|
|
} catch (e) {
|
|
|
|
caught = e;
|
|
|
|
}
|
|
|
|
assertTrue(!!caught, 'should have trapped');
|
|
|
|
assertEquals(caught, wrapped_exn);
|
|
|
|
assertInstanceof(caught.__proto__, WebAssembly.RuntimeError);
|
|
|
|
})();
|
|
|
|
|
2021-05-03 14:29:06 +00:00
|
|
|
(function TestStackOverflowNotCaught() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
function stack_overflow() {
|
|
|
|
%ThrowStackOverflow();
|
|
|
|
}
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let sig_v_v = builder.addType(kSig_v_v);
|
|
|
|
let kStackOverflow = builder.addImport('', 'stack_overflow', sig_v_v);
|
|
|
|
builder.addFunction('try_stack_overflow', kSig_v_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmVoid,
|
|
|
|
kExprCallFunction, 0,
|
|
|
|
kExprCatchAll,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate({'': {'stack_overflow': stack_overflow}});
|
|
|
|
|
|
|
|
assertThrows(() => instance.exports.try_stack_overflow(),
|
|
|
|
RangeError, 'Maximum call stack size exceeded');
|
|
|
|
})();
|
|
|
|
|
2018-10-09 11:16:52 +00:00
|
|
|
// Test that we can distinguish which exception was thrown by using a cascaded
|
2020-12-03 18:00:05 +00:00
|
|
|
// sequence of nested try blocks with a single catch block each.
|
2018-10-09 11:16:52 +00:00
|
|
|
(function TestCatchComplex1() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
let except2 = builder.addException(kSig_v_v);
|
|
|
|
let except3 = builder.addException(kSig_v_v);
|
2018-10-09 11:16:52 +00:00
|
|
|
builder.addFunction("catch_complex", kSig_i_i)
|
2017-09-25 15:57:50 +00:00
|
|
|
.addBody([
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprI32Eqz,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprThrow, except1,
|
|
|
|
kExprElse,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprThrow, except2,
|
|
|
|
kExprElse,
|
|
|
|
kExprThrow, except3,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprEnd,
|
|
|
|
kExprEnd,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprCatch, except1,
|
|
|
|
kExprI32Const, 3,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprEnd,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except2,
|
|
|
|
kExprI32Const, 4,
|
2018-10-09 11:16:52 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(3, instance.exports.catch_complex(0));
|
|
|
|
assertEquals(4, instance.exports.catch_complex(1));
|
|
|
|
assertWasmThrows(instance, except3, [], () => instance.exports.catch_complex(2));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test that we can distinguish which exception was thrown by using a single
|
2020-12-03 18:00:05 +00:00
|
|
|
// try block with multiple associated catch blocks in sequence.
|
2018-10-09 11:16:52 +00:00
|
|
|
(function TestCatchComplex2() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
let except2 = builder.addException(kSig_v_v);
|
|
|
|
let except3 = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction("catch_complex", kSig_i_i)
|
|
|
|
.addBody([
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprI32Eqz,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprThrow, except1,
|
|
|
|
kExprElse,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprThrow, except2,
|
|
|
|
kExprElse,
|
|
|
|
kExprThrow, except3,
|
2018-10-09 11:16:52 +00:00
|
|
|
kExprEnd,
|
|
|
|
kExprEnd,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprCatch, except1,
|
2018-10-09 11:16:52 +00:00
|
|
|
kExprI32Const, 3,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except2,
|
|
|
|
kExprI32Const, 4,
|
2018-10-09 11:16:52 +00:00
|
|
|
kExprEnd,
|
2017-09-25 15:57:50 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-10-09 11:16:52 +00:00
|
|
|
assertEquals(3, instance.exports.catch_complex(0));
|
|
|
|
assertEquals(4, instance.exports.catch_complex(1));
|
|
|
|
assertWasmThrows(instance, except3, [], () => instance.exports.catch_complex(2));
|
2018-08-28 11:36:28 +00:00
|
|
|
})();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
|
|
|
// Test throwing an exception with multiple values.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowMultipleValues() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_ii);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_1_2", kSig_v_v)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprI32Const, 2,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [0, 1, 0, 2], () => instance.exports.throw_1_2());
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching the i32 parameter value.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowCatchParamI() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_catch_param", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprI32Const, 2,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprReturn,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
assertEquals(0, instance.exports.throw_catch_param(0));
|
|
|
|
assertEquals(1, instance.exports.throw_catch_param(1));
|
|
|
|
assertEquals(10, instance.exports.throw_catch_param(10));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test the encoding of a thrown exception with an integer exception.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowParamI() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_param", kSig_v_i)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [0, 5], () => instance.exports.throw_param(5));
|
|
|
|
assertWasmThrows(instance, except, [6, 31026], () => instance.exports.throw_param(424242));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching the f32 parameter value.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowCatchParamF() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_f);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_catch_param", kSig_f_f)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmF32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprF32Const, 0, 0, 0, 0,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprReturn,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
assertEquals(5.0, instance.exports.throw_catch_param(5.0));
|
|
|
|
assertEquals(10.5, instance.exports.throw_catch_param(10.5));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test the encoding of a thrown exception with a float value.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowParamF() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_f);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_param", kSig_v_f)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [16544, 0], () => instance.exports.throw_param(5.0));
|
|
|
|
assertWasmThrows(instance, except, [16680, 0], () => instance.exports.throw_param(10.5));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching an I64 value
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowCatchParamL() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_l);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_catch_param", kSig_i_i)
|
2021-03-17 12:01:57 +00:00
|
|
|
.addLocals(kWasmI64, 1)
|
2017-09-25 15:57:50 +00:00
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprI64UConvertI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalSet, 1,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprCatch, except,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprI64Eq,
|
|
|
|
kExprIf, kWasmI32,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprElse,
|
|
|
|
kExprI32Const, 0,
|
|
|
|
kExprEnd,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprEnd,
|
2021-03-17 12:01:57 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
assertEquals(1, instance.exports.throw_catch_param(5));
|
|
|
|
assertEquals(1, instance.exports.throw_catch_param(0));
|
|
|
|
assertEquals(1, instance.exports.throw_catch_param(-1));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test the encoding of a thrown exception with an I64 value.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowParamL() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_l);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_param", kSig_v_ii)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprI64UConvertI32,
|
|
|
|
kExprI64Const, 32,
|
|
|
|
kExprI64Shl,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprI64UConvertI32,
|
|
|
|
kExprI64Ior,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [0, 10, 0, 5], () => instance.exports.throw_param(10, 5));
|
|
|
|
assertWasmThrows(instance, except, [65535, 65535, 0, 13], () => instance.exports.throw_param(-1, 13));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching the F64 parameter value
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowCatchParamD() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_d);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_catch_param", kSig_d_d)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmF64,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprReturn,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
assertEquals(5.0, instance.exports.throw_catch_param(5.0));
|
|
|
|
assertEquals(10.5, instance.exports.throw_catch_param(10.5));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Test the encoding of a thrown exception with an f64 value.
|
2018-08-28 11:36:28 +00:00
|
|
|
(function TestThrowParamD() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_d);
|
2017-09-25 15:57:50 +00:00
|
|
|
builder.addFunction("throw_param", kSig_v_f)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2017-09-25 15:57:50 +00:00
|
|
|
kExprF64ConvertF32,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2017-09-25 15:57:50 +00:00
|
|
|
]).exportFunc();
|
2018-08-28 11:36:28 +00:00
|
|
|
let instance = builder.instantiate();
|
2017-09-25 15:57:50 +00:00
|
|
|
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [16404, 0, 0, 0], () => instance.exports.throw_param(5.0));
|
|
|
|
assertWasmThrows(instance, except, [16739, 4816, 0, 0], () => instance.exports.throw_param(10000000.5));
|
2017-09-25 15:57:50 +00:00
|
|
|
})();
|
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
// Test the encoding of a computed parameter value.
|
|
|
|
(function TestThrowParamComputed() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
2016-09-15 15:04:03 +00:00
|
|
|
builder.addFunction("throw_expr_with_params", kSig_v_ddi)
|
2018-08-28 11:36:28 +00:00
|
|
|
.addBody([
|
|
|
|
// p2 * (p0 + min(p0, p1))|0 - 20
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 2,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprF64Min,
|
|
|
|
kExprF64Add,
|
|
|
|
kExprI32SConvertF64,
|
|
|
|
kExprI32Mul,
|
|
|
|
kExprI32Const, 20,
|
|
|
|
kExprI32Sub,
|
|
|
|
kExprThrow, except,
|
|
|
|
]).exportFunc()
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2018-09-17 12:19:53 +00:00
|
|
|
assertWasmThrows(instance, except, [65535, 65536-8], () => instance.exports.throw_expr_with_params(1.5, 2.5, 4));
|
|
|
|
assertWasmThrows(instance, except, [0, 12], () => instance.exports.throw_expr_with_params(5.7, 2.5, 4));
|
2016-09-15 15:04:03 +00:00
|
|
|
})();
|
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
// Now that we know catching works locally, we test catching exceptions that
|
|
|
|
// cross function boundaries and/or raised by JavaScript.
|
|
|
|
(function TestCatchCrossFunctions() {
|
2018-10-09 09:28:01 +00:00
|
|
|
print(arguments.callee.name);
|
2018-08-28 11:36:28 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
|
|
|
|
|
|
|
// Helper function for throwing from JS. It is imported by the Wasm module
|
|
|
|
// as throw_i.
|
|
|
|
function throw_value(value) {
|
|
|
|
throw value;
|
|
|
|
}
|
|
|
|
let sig_index = builder.addType(kSig_v_i);
|
|
|
|
let kJSThrowI = builder.addImport("", "throw_i", sig_index);
|
|
|
|
|
|
|
|
// Helper function that throws a string. Wasm should not catch it.
|
|
|
|
function throw_string() {
|
2018-08-29 09:10:57 +00:00
|
|
|
throw "use wasm";
|
2018-08-28 11:36:28 +00:00
|
|
|
}
|
|
|
|
sig_index = builder.addType(kSig_v_v);
|
|
|
|
let kJSThrowString = builder.addImport("", "throw_string", sig_index);
|
|
|
|
|
|
|
|
// Helper function that throws undefined. Wasm should not catch it.
|
|
|
|
function throw_undefined() {
|
|
|
|
throw undefined;
|
|
|
|
}
|
|
|
|
let kJSThrowUndefined = builder.addImport("", "throw_undefined", sig_index);
|
|
|
|
|
|
|
|
// Helper function that throws an fp. Wasm should not catch it.
|
|
|
|
function throw_fp() {
|
|
|
|
throw 10.5;
|
|
|
|
}
|
|
|
|
let kJSThrowFP = builder.addImport("", "throw_fp", sig_index);
|
|
|
|
|
|
|
|
// Helper function that throws a large number. Wasm should not catch it.
|
|
|
|
function throw_large() {
|
|
|
|
throw 1e+28;
|
|
|
|
}
|
|
|
|
let kJSThrowLarge = builder.addImport("", "throw_large", sig_index);
|
|
|
|
|
|
|
|
// Helper function for throwing from WebAssembly.
|
|
|
|
let kWasmThrowFunction =
|
|
|
|
builder.addFunction("throw", kSig_v_i)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
])
|
|
|
|
.index;
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
// Scenario 1: Throw and catch appear on the same function. This should
|
|
|
|
// happen in case of inlining, for example.
|
|
|
|
builder.addFunction("same_scope", kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 0,
|
|
|
|
kExprI32Ne,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprUnreachable,
|
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, 63,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprEnd
|
|
|
|
])
|
2018-08-29 09:10:57 +00:00
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
builder.addFunction("same_scope_ignore", kSig_i_i)
|
|
|
|
.addBody([
|
2016-12-21 13:43:00 +00:00
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprUnreachable,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprEnd,
|
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
builder.addFunction("same_scope_multiple", kSig_i_i)
|
|
|
|
// path = 0;
|
|
|
|
//
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// if (p == 1)
|
|
|
|
// throw 1;
|
|
|
|
// path |= 2
|
|
|
|
// } catch (v) {
|
|
|
|
// path |= v | 4;
|
|
|
|
// throw path;
|
|
|
|
// }
|
|
|
|
// if (p == 2)
|
|
|
|
// throw path|8;
|
|
|
|
// path |= 16;
|
|
|
|
// } catch (v) {
|
|
|
|
// path |= v | 32;
|
|
|
|
// throw path;
|
|
|
|
// }
|
|
|
|
// if (p == 3)
|
|
|
|
// throw path|64;
|
|
|
|
// path |= 128
|
|
|
|
// } catch (v) {
|
|
|
|
// path |= v | 256;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return path;
|
|
|
|
//
|
|
|
|
// p == 1 -> path == 293
|
|
|
|
// p == 2 -> path == 298
|
|
|
|
// p == 3 -> path == 338
|
|
|
|
// else -> path == 146
|
2021-03-17 12:01:57 +00:00
|
|
|
.addLocals(kWasmI32, 1)
|
2018-08-28 11:36:28 +00:00
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2016-12-21 13:43:00 +00:00
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 1,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprThrow, except,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprUnreachable,
|
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 2,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 4,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprI32Ior,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprUnreachable,
|
|
|
|
kExprEnd,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalTee, 1,
|
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 2,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 8,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprI32Ior,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprUnreachable,
|
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 16,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprI32Ior,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 32,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprI32Ior,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprUnreachable,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprEnd,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalTee, 1,
|
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, 3,
|
|
|
|
kExprI32Eq,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, /*64=*/ 192, 0,
|
|
|
|
kExprI32Ior,
|
|
|
|
kExprThrow, except,
|
|
|
|
kExprUnreachable,
|
|
|
|
kExprEnd,
|
|
|
|
kExprI32Const, /*128=*/ 128, 1,
|
|
|
|
kExprI32Ior,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprI32Const, /*256=*/ 128, 2,
|
|
|
|
kExprI32Ior,
|
2016-09-30 16:09:12 +00:00
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
// Scenario 2: Catches an exception raised from the direct callee.
|
2018-09-04 13:56:54 +00:00
|
|
|
builder.addFunction("from_direct_callee", kSig_i_i)
|
2018-08-28 11:36:28 +00:00
|
|
|
.addBody([
|
2018-09-04 13:56:54 +00:00
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-09-04 13:56:54 +00:00
|
|
|
kExprCallFunction, kWasmThrowFunction,
|
|
|
|
kExprUnreachable,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprEnd,
|
2018-09-04 13:56:54 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-09-04 13:56:54 +00:00
|
|
|
// Scenario 3: Catches an exception raised from an indirect callee.
|
|
|
|
let sig_v_i = builder.addType(kSig_v_i);
|
|
|
|
builder.appendToTable([kWasmThrowFunction, kWasmThrowFunction]);
|
|
|
|
builder.addFunction("from_indirect_callee", kSig_i_ii)
|
2018-08-28 11:36:28 +00:00
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
2018-09-04 13:56:54 +00:00
|
|
|
kExprCallIndirect, sig_v_i, kTableZero,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprUnreachable,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprEnd
|
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-09-17 11:20:02 +00:00
|
|
|
// Scenario 4: Does not catch an exception raised in JS, even if primitive
|
|
|
|
// values are being used as exceptions.
|
|
|
|
builder.addFunction("i_from_js", kSig_i_i)
|
2018-08-28 11:36:28 +00:00
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprCallFunction, kJSThrowI,
|
|
|
|
kExprUnreachable,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprUnreachable,
|
2018-08-28 11:36:28 +00:00
|
|
|
kExprEnd,
|
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
builder.addFunction("string_from_js", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprCallFunction, kJSThrowString,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprUnreachable,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
builder.addFunction("fp_from_js", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprCallFunction, kJSThrowFP,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprUnreachable,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
|
|
|
builder.addFunction("large_from_js", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprCallFunction, kJSThrowLarge,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprUnreachable,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-28 11:36:28 +00:00
|
|
|
builder.addFunction("undefined_from_js", kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprCallFunction, kJSThrowUndefined,
|
2020-12-03 18:00:05 +00:00
|
|
|
kExprCatch, except,
|
|
|
|
kExprUnreachable,
|
2018-09-17 11:20:02 +00:00
|
|
|
kExprEnd,
|
2018-08-28 11:36:28 +00:00
|
|
|
])
|
|
|
|
.exportFunc();
|
|
|
|
|
2018-08-29 09:10:57 +00:00
|
|
|
let instance = builder.instantiate({"": {
|
2016-09-30 16:09:12 +00:00
|
|
|
throw_i: throw_value,
|
|
|
|
throw_string: throw_string,
|
|
|
|
throw_fp: throw_fp,
|
|
|
|
throw_large, throw_large,
|
|
|
|
throw_undefined: throw_undefined
|
2016-12-20 15:32:56 +00:00
|
|
|
}});
|
2016-09-30 16:09:12 +00:00
|
|
|
|
2018-08-29 09:10:57 +00:00
|
|
|
assertEquals(63, instance.exports.same_scope(0));
|
|
|
|
assertEquals(1024, instance.exports.same_scope(1024));
|
|
|
|
assertEquals(-3, instance.exports.same_scope(-3));
|
|
|
|
assertEquals(-1, instance.exports.same_scope_ignore(-1));
|
|
|
|
assertEquals(1, instance.exports.same_scope_ignore(1));
|
|
|
|
assertEquals(0x7FFFFFFF, instance.exports.same_scope_ignore(0x7FFFFFFF));
|
|
|
|
assertEquals(1024, instance.exports.same_scope_ignore(1024));
|
|
|
|
assertEquals(-1, instance.exports.same_scope_ignore(-1));
|
|
|
|
assertEquals(293, instance.exports.same_scope_multiple(1));
|
|
|
|
assertEquals(298, instance.exports.same_scope_multiple(2));
|
|
|
|
assertEquals(338, instance.exports.same_scope_multiple(3));
|
|
|
|
assertEquals(146, instance.exports.same_scope_multiple(0));
|
|
|
|
assertEquals(-10024, instance.exports.from_direct_callee(-10024));
|
|
|
|
assertEquals(3334333, instance.exports.from_direct_callee(3334333));
|
|
|
|
assertEquals(-1, instance.exports.from_direct_callee(0xFFFFFFFF));
|
|
|
|
assertEquals(0x7FFFFFFF, instance.exports.from_direct_callee(0x7FFFFFFF));
|
2018-09-04 13:56:54 +00:00
|
|
|
assertEquals(10, instance.exports.from_indirect_callee(10, 0));
|
|
|
|
assertEquals(77, instance.exports.from_indirect_callee(77, 1));
|
2018-08-29 09:10:57 +00:00
|
|
|
|
2018-09-17 11:20:02 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.i_from_js(10), 10);
|
|
|
|
assertThrowsEquals(() => instance.exports.i_from_js(-10), -10);
|
2018-08-29 09:10:57 +00:00
|
|
|
assertThrowsEquals(instance.exports.string_from_js, "use wasm");
|
|
|
|
assertThrowsEquals(instance.exports.fp_from_js, 10.5);
|
|
|
|
assertThrowsEquals(instance.exports.large_from_js, 1e+28);
|
|
|
|
assertThrowsEquals(instance.exports.undefined_from_js, undefined);
|
2018-08-28 11:36:28 +00:00
|
|
|
})();
|
2020-12-10 10:39:00 +00:00
|
|
|
|
|
|
|
// Delegate with a try block that never throws.
|
|
|
|
(function TestDelegateNoThrow() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction('test', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprDelegate, 0,
|
|
|
|
kExprCatch, except1,
|
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertEquals(1, instance.exports.test());
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Delegate exception handling to outer try/catch block.
|
|
|
|
(function TestDelegateThrow() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
let throw_if = builder.addFunction('throw', kSig_v_i)
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprIf, kWasmVoid,
|
2020-12-10 10:39:00 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprEnd]).exportFunc();
|
|
|
|
builder.addFunction('test', kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprCallFunction, throw_if.index,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprDelegate, 0,
|
|
|
|
kExprCatch, except,
|
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertEquals(1, instance.exports.test(0));
|
|
|
|
assertEquals(2, instance.exports.test(1));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// No catch block matching the exception in the delegate target.
|
|
|
|
(function TestDelegateThrowNoCatch() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
let except2 = builder.addException(kSig_v_v);
|
|
|
|
let throw_fn = builder.addFunction('throw', kSig_v_v)
|
|
|
|
.addBody([kExprThrow, except1])
|
|
|
|
.exportFunc();
|
|
|
|
let throw_fn_2 = builder.addFunction('throw_2', kSig_v_v)
|
|
|
|
.addBody([kExprThrow, except2])
|
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction('test', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprCallFunction, throw_fn.index,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprDelegate, 0,
|
|
|
|
kExprCatch, except2,
|
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertTraps(WebAssembly.RuntimeError, instance.exports.test);
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Check that the exception is merged properly when both scopes can throw.
|
|
|
|
(function TestDelegateMerge() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
let except2 = builder.addException(kSig_v_v);
|
|
|
|
// throw_fn: 0 -> returns
|
|
|
|
// 1 -> throw except1
|
|
|
|
// 2 -> throw except2
|
|
|
|
let throw_fn = builder.addFunction('throw', kSig_v_i)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprBlock, kWasmVoid,
|
|
|
|
kExprBlock, kWasmVoid,
|
|
|
|
kExprBlock, kWasmVoid,
|
2020-12-10 10:39:00 +00:00
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprBrTable, 2, 0, 1, 2,
|
|
|
|
kExprEnd,
|
|
|
|
kExprReturn,
|
|
|
|
kExprEnd,
|
|
|
|
kExprThrow, except1,
|
|
|
|
kExprEnd,
|
|
|
|
kExprThrow, except2])
|
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction('test', kSig_i_ii)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprCallFunction, throw_fn.index,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprCallFunction, throw_fn.index,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprDelegate, 0,
|
|
|
|
kExprCatch, except1,
|
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertEquals(2, instance.exports.test(1, 0));
|
|
|
|
assertTraps(WebAssembly.RuntimeError, () => instance.exports.test(2, 0));
|
|
|
|
assertEquals(2, instance.exports.test(0, 1));
|
|
|
|
assertTraps(WebAssembly.RuntimeError, () => instance.exports.test(0, 2));
|
|
|
|
assertEquals(1, instance.exports.test(0, 0));
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Delegate to second enclosing try scope.
|
|
|
|
(function TestDelegate1() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
let throw_fn = builder.addFunction('throw', kSig_v_v)
|
|
|
|
.addBody([kExprThrow, except])
|
|
|
|
.exportFunc();
|
|
|
|
builder.addFunction('test', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprCallFunction, throw_fn.index,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprDelegate, 1,
|
|
|
|
kExprCatch, except,
|
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprEnd,
|
|
|
|
kExprCatch, except,
|
|
|
|
kExprI32Const, 3,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertEquals(3, instance.exports.test());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestDelegateUnreachable() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except1 = builder.addException(kSig_v_v);
|
|
|
|
let except2 = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction('test', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
2020-12-10 10:39:00 +00:00
|
|
|
kExprThrow, except1,
|
|
|
|
kExprDelegate, 0,
|
|
|
|
kExprI32Const, 1,
|
|
|
|
kExprCatch, except1,
|
|
|
|
kExprI32Const, 2,
|
|
|
|
kExprCatch, except2,
|
|
|
|
kExprI32Const, 3,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertEquals(2, instance.exports.test());
|
|
|
|
})();
|
2021-01-08 13:07:11 +00:00
|
|
|
|
|
|
|
(function TestDelegateToCaller() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction('test', kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
|
|
|
kExprTry, kWasmVoid,
|
2021-01-08 13:07:11 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprDelegate, 1,
|
2021-02-17 11:53:28 +00:00
|
|
|
kExprCatchAll,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction('test_unwind', kSig_v_v)
|
|
|
|
.addBody([
|
2021-03-22 06:56:01 +00:00
|
|
|
kExprTry, kWasmVoid,
|
|
|
|
kExprTry, kWasmVoid,
|
2021-02-04 13:27:16 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprDelegate, 1,
|
|
|
|
kExprUnwind,
|
2021-01-08 13:07:11 +00:00
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
instance = builder.instantiate();
|
|
|
|
assertTraps(WebAssembly.RuntimeError, () => instance.exports.test());
|
2021-02-04 13:27:16 +00:00
|
|
|
assertTraps(WebAssembly.RuntimeError, () => instance.exports.test_unwind());
|
2021-01-08 13:07:11 +00:00
|
|
|
})();
|
2021-03-09 14:15:32 +00:00
|
|
|
|
|
|
|
(function TestThrowBeforeUnreachable() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction('throw_before_unreachable', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except,
|
|
|
|
kExprUnreachable,
|
|
|
|
kExprCatchAll,
|
|
|
|
kExprI32Const, 42,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
assertEquals(42, instance.exports.throw_before_unreachable());
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function TestUnreachableInCatchAll() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction('throw_before_unreachable', kSig_i_v)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprThrow, except,
|
|
|
|
kExprCatchAll,
|
|
|
|
kExprUnreachable,
|
|
|
|
kExprI32Const, 42,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
})();
|
2021-03-17 12:01:57 +00:00
|
|
|
|
|
|
|
(function TestThrowWithLocal() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_v);
|
|
|
|
builder.addFunction('throw_with_local', kSig_i_v)
|
|
|
|
.addLocals(kWasmI32, 4)
|
|
|
|
.addBody([
|
|
|
|
kExprI32Const, 42,
|
|
|
|
kExprF64Const, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
kExprTry, kWasmF32,
|
|
|
|
kExprThrow, except,
|
|
|
|
kExprCatchAll,
|
|
|
|
kExprF32Const, 0, 0, 0, 0,
|
|
|
|
kExprEnd,
|
|
|
|
kExprDrop, // Drop the f32.
|
|
|
|
kExprDrop, // Drop the f64.
|
|
|
|
// Leave the '42' on the stack.
|
|
|
|
]).exportFunc();
|
|
|
|
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
assertEquals(42, instance.exports.throw_with_local());
|
|
|
|
})();
|