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
|
|
|
|
|
|
|
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-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();
|
|
|
|
let except = builder.addException(kSig_v_r);
|
|
|
|
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();
|
|
|
|
let except = builder.addException(kSig_v_r);
|
|
|
|
builder.addFunction("throw_catch_null", kSig_i_i)
|
|
|
|
.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
|
|
|
kExprI32Eqz,
|
2020-06-09 15:54:14 +00:00
|
|
|
kExprIf, kWasmExternRef,
|
|
|
|
kExprRefNull, kWasmExternRef,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprElse,
|
|
|
|
kExprI32Const, 42,
|
2019-01-15 13:44:03 +00:00
|
|
|
kExprReturn,
|
2018-10-25 14:52:29 +00:00
|
|
|
kExprEnd,
|
2019-01-15 13:44:03 +00:00
|
|
|
kExprCatch,
|
|
|
|
kExprBrOnExn, 0, except,
|
|
|
|
kExprRethrow,
|
|
|
|
kExprEnd,
|
2020-06-23 13:41:10 +00:00
|
|
|
kExprRefIsNull,
|
2019-01-15 13:44:03 +00:00
|
|
|
kExprIf, kWasmI32,
|
|
|
|
kExprI32Const, 23,
|
|
|
|
kExprElse,
|
|
|
|
kExprUnreachable,
|
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();
|
|
|
|
let except = builder.addException(kSig_v_r);
|
|
|
|
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();
|
|
|
|
let except = builder.addException(kSig_v_r);
|
|
|
|
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,
|
2019-01-15 13:44:03 +00:00
|
|
|
kExprCatch,
|
|
|
|
kExprBrOnExn, 0, except,
|
|
|
|
kExprRethrow,
|
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"));
|
|
|
|
})();
|
2019-04-24 10:58:33 +00:00
|
|
|
|
|
|
|
// Test throwing/catching a function reference type value.
|
|
|
|
(function TestThrowCatchAnyFunc() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_a);
|
2020-05-29 15:23:27 +00:00
|
|
|
builder.addFunction("throw_catch_local", kSig_a_v)
|
2020-09-10 12:39:52 +00:00
|
|
|
.addLocals(kWasmAnyFunc, 1)
|
2019-04-24 10:58:33 +00:00
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmAnyFunc,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2019-04-24 10:58:33 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprCatch,
|
|
|
|
kExprBrOnExn, 0, except,
|
|
|
|
kExprRethrow,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
|
|
|
assertEquals(null, instance.exports.throw_catch_local());
|
|
|
|
})();
|
|
|
|
|
|
|
|
// Test throwing/catching an encapsulated exception type value.
|
2019-07-15 08:24:37 +00:00
|
|
|
(function TestThrowCatchExnRef() {
|
2019-04-24 10:58:33 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_e);
|
|
|
|
builder.addFunction("throw_catch_param", kSig_e_e)
|
|
|
|
.addBody([
|
2019-07-15 08:24:37 +00:00
|
|
|
kExprTry, kWasmExnRef,
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0,
|
2019-04-24 10:58:33 +00:00
|
|
|
kExprThrow, except,
|
|
|
|
kExprCatch,
|
|
|
|
kExprBrOnExn, 0, except,
|
|
|
|
kExprRethrow,
|
|
|
|
kExprEnd,
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let e = new Error("my encapsulated error");
|
|
|
|
|
|
|
|
assertEquals(e, instance.exports.throw_catch_param(e));
|
|
|
|
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"));
|
|
|
|
})();
|
2019-10-16 13:34:08 +00:00
|
|
|
|
|
|
|
// Test storing and loading to/from an exception type table.
|
|
|
|
(function TestTableExnRef() {
|
2020-03-23 10:46:51 +00:00
|
|
|
print(arguments.callee.name);
|
2019-10-16 13:34:08 +00:00
|
|
|
let kSig_e_i = makeSig([kWasmI32], [kWasmExnRef]);
|
|
|
|
let kSig_v_ie = makeSig([kWasmI32, kWasmExnRef], []);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let table = builder.addTable(kWasmExnRef, 2).exportAs("table");
|
|
|
|
builder.addFunction("table_load", kSig_e_i)
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprTableGet, table.index
|
|
|
|
]).exportFunc();
|
|
|
|
builder.addFunction("table_store", kSig_v_ie)
|
|
|
|
.addBody([
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprLocalGet, 1,
|
|
|
|
kExprTableSet, table.index
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
let e0 = new Error("my encapsulated error");
|
|
|
|
let e1 = new Error("my other encapsulated error");
|
|
|
|
|
|
|
|
assertNull(instance.exports.table_load(0));
|
|
|
|
assertNull(instance.exports.table_load(1));
|
|
|
|
assertNull(instance.exports.table.get(0));
|
|
|
|
assertNull(instance.exports.table.get(1));
|
|
|
|
|
|
|
|
instance.exports.table_store(0, e0);
|
|
|
|
assertSame(e0, instance.exports.table_load(0));
|
|
|
|
assertNull(instance.exports.table_load(1));
|
|
|
|
assertSame(e0, instance.exports.table.get(0));
|
|
|
|
assertNull(instance.exports.table.get(1));
|
|
|
|
|
|
|
|
instance.exports.table.set(1, e1);
|
|
|
|
assertSame(e0, instance.exports.table_load(0));
|
|
|
|
assertSame(e1, instance.exports.table_load(1));
|
|
|
|
assertSame(e0, instance.exports.table.get(0));
|
|
|
|
assertSame(e1, instance.exports.table.get(1));
|
|
|
|
})();
|
2020-03-23 10:46:51 +00:00
|
|
|
|
|
|
|
// 'br_on_exn' on a null-ref value should trap.
|
2020-06-10 07:22:22 +00:00
|
|
|
(function TestBrOnExnNullSimple() {
|
2020-03-23 10:46:51 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_r);
|
2020-06-10 07:22:22 +00:00
|
|
|
builder.addFunction('br_on_exn_null', kSig_v_v)
|
2020-03-23 10:46:51 +00:00
|
|
|
.addBody([
|
2020-06-03 12:48:16 +00:00
|
|
|
kExprRefNull, kWasmExnRef,
|
2020-03-23 10:46:51 +00:00
|
|
|
kExprBrOnExn, 0, except,
|
|
|
|
kExprDrop
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2020-06-10 07:22:22 +00:00
|
|
|
assertTraps(kTrapBrOnExnNull, () => instance.exports.br_on_exn_null());
|
2020-03-23 10:46:51 +00:00
|
|
|
})();
|
|
|
|
|
2020-06-10 07:22:22 +00:00
|
|
|
(function TestBrOnExnNullFromJS() {
|
2020-03-23 10:46:51 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
|
|
|
let imp = builder.addImport('imp', 'ort', kSig_i_i);
|
|
|
|
let kConstant0 = 11;
|
|
|
|
let kNoMatch = 13;
|
|
|
|
builder.addFunction('call_import', kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprCallFunction, imp,
|
|
|
|
kExprCatch,
|
|
|
|
kExprBrOnExn, 0, except,
|
|
|
|
kExprDrop,
|
|
|
|
kExprI32Const, kNoMatch,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance;
|
|
|
|
function js_import(i) {
|
|
|
|
if (i == 0) return kConstant0; // Will return kConstant0.
|
|
|
|
if (i == 1) throw new Error('1'); // Will not match.
|
|
|
|
if (i == 2) throw null; // Will trap.
|
|
|
|
throw undefined; // Will not match.
|
|
|
|
}
|
|
|
|
instance = builder.instantiate({imp: {ort: js_import}});
|
|
|
|
|
|
|
|
assertEquals(kConstant0, instance.exports.call_import(0));
|
|
|
|
assertEquals(kNoMatch, instance.exports.call_import(1));
|
2020-06-10 07:22:22 +00:00
|
|
|
assertTraps(kTrapBrOnExnNull, () => instance.exports.call_import(2));
|
2020-03-23 10:46:51 +00:00
|
|
|
assertEquals(kNoMatch, instance.exports.call_import(3));
|
|
|
|
})();
|
2020-03-23 10:50:37 +00:00
|
|
|
|
|
|
|
// 'rethrow' on a null-ref value should trap.
|
2020-06-10 07:22:22 +00:00
|
|
|
(function TestRethrowNullSimple() {
|
2020-03-23 10:50:37 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_r);
|
2020-06-10 07:22:22 +00:00
|
|
|
builder.addFunction('rethrow_null', kSig_v_v)
|
2020-03-23 10:50:37 +00:00
|
|
|
.addBody([
|
2020-06-03 12:48:16 +00:00
|
|
|
kExprRefNull, kWasmExnRef,
|
2020-03-23 10:50:37 +00:00
|
|
|
kExprRethrow
|
|
|
|
]).exportFunc();
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
|
2020-06-10 07:22:22 +00:00
|
|
|
assertTraps(kTrapRethrowNull, () => instance.exports.rethrow_null());
|
2020-03-23 10:50:37 +00:00
|
|
|
})();
|
|
|
|
|
2020-06-10 07:22:22 +00:00
|
|
|
(function TestRethrowNullFromJS() {
|
2020-03-23 10:50:37 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let except = builder.addException(kSig_v_i);
|
|
|
|
let imp = builder.addImport('imp', 'ort', kSig_i_i);
|
|
|
|
let kSuccess = 11;
|
|
|
|
builder.addFunction('call_import', kSig_i_i)
|
|
|
|
.addBody([
|
|
|
|
kExprTry, kWasmI32,
|
|
|
|
kExprLocalGet, 0,
|
|
|
|
kExprCallFunction, imp,
|
|
|
|
kExprCatch,
|
|
|
|
kExprRethrow,
|
|
|
|
kExprEnd
|
|
|
|
]).exportFunc();
|
|
|
|
let instance;
|
|
|
|
function js_import(i) {
|
|
|
|
if (i == 0) return kSuccess; // Will return kSuccess.
|
|
|
|
if (i == 1) throw new Error('1'); // Will rethrow.
|
|
|
|
if (i == 2) throw null; // Will trap.
|
|
|
|
throw undefined; // Will rethrow.
|
|
|
|
}
|
|
|
|
instance = builder.instantiate({imp: {ort: js_import}});
|
|
|
|
|
|
|
|
assertEquals(kSuccess, instance.exports.call_import(0));
|
|
|
|
assertThrows(() => instance.exports.call_import(1), Error, '1');
|
2020-06-10 07:22:22 +00:00
|
|
|
assertTraps(kTrapRethrowNull, () => instance.exports.call_import(2));
|
2020-03-23 10:50:37 +00:00
|
|
|
assertThrowsEquals(() => instance.exports.call_import(3), undefined);
|
|
|
|
})();
|