Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js-api] Add more tests reusing payload #266

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");