Skip to content

Commit

Permalink
[js-api] Add more tests reusing payload (#266)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aheejin committed Mar 1, 2023
1 parent 585c7da commit 95d87ba
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion test/js-api/exception/identity.tentative.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test(() => {

// Tag defined in JavaScript and imported into Wasm
const jsTag = new WebAssembly.Tag({ parameters: ["i32"] });
const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i)
const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i);
const jsTagExn = new WebAssembly.Exception(jsTag, [42]);
const jsTagExnSamePayload = new WebAssembly.Exception(jsTag, [42]);
const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]);
Expand Down Expand Up @@ -84,6 +84,33 @@ test(() => {
])
.exportFunc();

// Call a JS function that throws an exception, catches it with a 'catch'
// instruction, and returns its i32 payload.
builder
.addFunction("catch_js_tag_return_payload", kSig_i_v)
.addBody([
kExprTry, kWasmI32,
kExprCallFunction, throwJSTagExnIndex,
kExprI32Const, 0x00,
kExprCatch, jsTagIndex,
kExprReturn,
kExprEnd
])
.exportFunc();

// Call a JS function that throws an exception, catches it with a 'catch'
// instruction, and throws a new exception using that payload.
builder
.addFunction("catch_js_tag_throw_payload", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprCallFunction, throwJSTagExnIndex,
kExprCatch, jsTagIndex,
kExprThrow, jsTagIndex,
kExprEnd
])
.exportFunc();

const buffer = builder.toBuffer();

WebAssembly.instantiate(buffer, imports).then(result => {
Expand Down Expand Up @@ -125,5 +152,19 @@ test(() => {
assert_not_equals(e, wasmTagExnSamePayload);
assert_not_equals(e, wasmTagExnDiffPayload);
}

// This function catches the exception and returns its i32 payload, which
// should match the original payload.
assert_equals(result.instance.exports.catch_js_tag_return_payload(), 42);

// This function catches the exception and throws a new exception using the
// its payload. Even if the payload is reused, the exception objects should
// not compare equal.
try {
result.instance.exports.catch_js_tag_throw_payload();
} catch (e) {
assert_equals(e.getArg(jsTag, 0), 42);
assert_not_equals(e, jsTagExn);
}
});
}, "Identity check");

0 comments on commit 95d87ba

Please sign in to comment.