Skip to content

Commit

Permalink
[js-api] Add more tests reusing payload
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aheejin committed Feb 24, 2023
1 parent e987be4 commit 016a733
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions test/js-api/exception/identity.tentative.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,39 @@ 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_i_v)
.addBody([
kExprTry, kWasmI32,
kExprCallFunction, throwJSTagExnIndex,
kExprI32Const, 0x00,
kExprCatch, jsTagIndex,
kExprThrow, jsTagIndex,
kExprEnd,
])
.exportFunc();

const buffer = builder.toBuffer();

// The exception object's identity should be preserved across 'rethrow's in
// Wasm code. Do tests with a tag defined in JS.
WebAssembly.instantiate(buffer, imports).then(result => {
// The exception object's identity should be preserved across 'rethrow's in
// Wasm code. Do tests with a tag defined in JS.
try {
result.instance.exports.catch_js_tag_rethrow();
} catch (e) {
Expand All @@ -68,5 +96,19 @@ test(() => {
assert_not_equals(e, jsTagExnSamePayload);
assert_not_equals(e, jsTagExnDiffPayload);
}

// 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 016a733

Please sign in to comment.