Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 95d87ba

Browse files
authored
[js-api] Add more tests reusing payload (#266)
As suggested in #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 585c7da commit 95d87ba

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test(() => {
77

88
// Tag defined in JavaScript and imported into Wasm
99
const jsTag = new WebAssembly.Tag({ parameters: ["i32"] });
10-
const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i)
10+
const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i);
1111
const jsTagExn = new WebAssembly.Exception(jsTag, [42]);
1212
const jsTagExnSamePayload = new WebAssembly.Exception(jsTag, [42]);
1313
const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]);
@@ -84,6 +84,33 @@ test(() => {
8484
])
8585
.exportFunc();
8686

87+
// Call a JS function that throws an exception, catches it with a 'catch'
88+
// instruction, and returns its i32 payload.
89+
builder
90+
.addFunction("catch_js_tag_return_payload", kSig_i_v)
91+
.addBody([
92+
kExprTry, kWasmI32,
93+
kExprCallFunction, throwJSTagExnIndex,
94+
kExprI32Const, 0x00,
95+
kExprCatch, jsTagIndex,
96+
kExprReturn,
97+
kExprEnd
98+
])
99+
.exportFunc();
100+
101+
// Call a JS function that throws an exception, catches it with a 'catch'
102+
// instruction, and throws a new exception using that payload.
103+
builder
104+
.addFunction("catch_js_tag_throw_payload", kSig_v_v)
105+
.addBody([
106+
kExprTry, kWasmStmt,
107+
kExprCallFunction, throwJSTagExnIndex,
108+
kExprCatch, jsTagIndex,
109+
kExprThrow, jsTagIndex,
110+
kExprEnd
111+
])
112+
.exportFunc();
113+
87114
const buffer = builder.toBuffer();
88115

89116
WebAssembly.instantiate(buffer, imports).then(result => {
@@ -125,5 +152,19 @@ test(() => {
125152
assert_not_equals(e, wasmTagExnSamePayload);
126153
assert_not_equals(e, wasmTagExnDiffPayload);
127154
}
155+
156+
// This function catches the exception and returns its i32 payload, which
157+
// should match the original payload.
158+
assert_equals(result.instance.exports.catch_js_tag_return_payload(), 42);
159+
160+
// This function catches the exception and throws a new exception using the
161+
// its payload. Even if the payload is reused, the exception objects should
162+
// not compare equal.
163+
try {
164+
result.instance.exports.catch_js_tag_throw_payload();
165+
} catch (e) {
166+
assert_equals(e.getArg(jsTag, 0), 42);
167+
assert_not_equals(e, jsTagExn);
168+
}
128169
});
129170
}, "Identity check");

0 commit comments

Comments
 (0)