Skip to content

Commit 0285743

Browse files
committed
[js-api] Add more tests reusing payload
As suggested in WebAssembly#256 (comment), this adds two more tests: - JS throws an exception, Wasm catches it and returns the payload. - JS throws an exception, Wasm catches it and throws a new exception using that payload.
1 parent e987be4 commit 0285743

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

test/js-api/exception/identity.tentative.any.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,38 @@ test(() => {
4747
])
4848
.exportFunc();
4949

50+
// Call a JS function that throws an exception, catches it with a 'catch'
51+
// instruction, and returns its i32 payload.
52+
builder
53+
.addFunction("catch_js_tag_return_payload", kSig_i_v)
54+
.addBody([
55+
kExprTry, kWasmI32,
56+
kExprCallFunction, throwJSTagExnIndex,
57+
kExprI32Const, 0x00,
58+
kExprCatch, jsTagIndex,
59+
kExprReturn,
60+
kExprEnd,
61+
])
62+
.exportFunc();
63+
64+
// Call a JS function that throws an exception, catches it with a 'catch'
65+
// instruction, and throws a new exception using that payload.
66+
builder
67+
.addFunction("catch_js_tag_throw_payload", kSig_v_v)
68+
.addBody([
69+
kExprTry, kWasmStmt,
70+
kExprCallFunction, throwJSTagExnIndex,
71+
kExprCatch, jsTagIndex,
72+
kExprThrow, jsTagIndex,
73+
kExprEnd,
74+
])
75+
.exportFunc();
76+
5077
const buffer = builder.toBuffer();
5178

52-
// The exception object's identity should be preserved across 'rethrow's in
53-
// Wasm code. Do tests with a tag defined in JS.
5479
WebAssembly.instantiate(buffer, imports).then(result => {
80+
// The exception object's identity should be preserved across 'rethrow's in
81+
// Wasm code. Do tests with a tag defined in JS.
5582
try {
5683
result.instance.exports.catch_js_tag_rethrow();
5784
} catch (e) {
@@ -68,5 +95,19 @@ test(() => {
6895
assert_not_equals(e, jsTagExnSamePayload);
6996
assert_not_equals(e, jsTagExnDiffPayload);
7097
}
98+
99+
// This function catches the exception and returns its i32 payload, which
100+
// should match the original payload.
101+
assert_equals(result.instance.exports.catch_js_tag_return_payload(), 42);
102+
103+
// This function catches the exception and throws a new exception using the
104+
// its payload. Even if the payload is reused, the exception objects should
105+
// not compare equal.
106+
try {
107+
result.instance.exports.catch_js_tag_throw_payload();
108+
} catch (e) {
109+
assert_equals(e.getArg(jsTag, 0), 42);
110+
assert_not_equals(e, jsTagExn);
111+
}
71112
});
72113
}, "Identity check");

0 commit comments

Comments
 (0)