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

[js-api] Test Wasm-defined tag in rethrow identity test #265

Merged
merged 3 commits into from
Feb 28, 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
61 changes: 59 additions & 2 deletions test/js-api/exception/identity.tentative.any.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ test(() => {
const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]);
const throwJSTagExnIndex = builder.addImport("module", "throwJSTagExn", kSig_v_v);

// Tag defined in Wasm and exported to JS
const wasmTagIndex = builder.addTag(kSig_v_i);
builder.addExportOfKind("wasmTag", kExternalTag, wasmTagIndex);
const throwWasmTagExnIndex = builder.addImport("module", "throwWasmTagExn", kSig_v_v);
// Will be assigned after an instance is created
let wasmTagExn = null;
let wasmTagExnSamePayload = null;
let wasmTagExnDiffPayload = null;

const imports = {
module: {
throwJSTagExn: function() { throw jsTagExn; },
throwWasmTagExn: function() { throw wasmTagExn; },
jsTag: jsTag
}
};
Expand All @@ -34,6 +44,20 @@ test(() => {
])
.exportFunc();

// Call a JS function that throws an exception using a Wasm-defined tag,
// catches it with a 'catch' instruction, and rethrows it.
builder
.addFunction("catch_wasm_tag_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprCallFunction, throwWasmTagExnIndex,
kExprCatch, wasmTagIndex,
kExprDrop,
kExprRethrow, 0x00,
kExprEnd
])
.exportFunc();

// Call a JS function that throws an exception using a JS-defined tag, catches
// it with a 'catch_all' instruction, and rethrows it.
builder
Expand All @@ -47,11 +71,24 @@ test(() => {
])
.exportFunc();

// Call a JS function that throws an exception using a Wasm-defined tag,
// catches it with a 'catch_all' instruction, and rethrows it.
builder
.addFunction("catch_all_wasm_tag_rethrow", kSig_v_v)
.addBody([
kExprTry, kWasmStmt,
kExprCallFunction, throwWasmTagExnIndex,
kExprCatchAll,
kExprRethrow, 0x00,
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 +105,25 @@ test(() => {
assert_not_equals(e, jsTagExnSamePayload);
assert_not_equals(e, jsTagExnDiffPayload);
}

// Do the same tests with a tag defined in Wasm.
const wasmTag = result.instance.exports.wasmTag;
wasmTagExn = new WebAssembly.Exception(wasmTag, [42]);
wasmTagExnSamePayload = new WebAssembly.Exception(wasmTag, [42]);
wasmTagExnDiffPayload = new WebAssembly.Exception(wasmTag, [53]);
try {
result.instance.exports.catch_wasm_tag_rethrow();
} catch (e) {
assert_equals(e, wasmTagExn);
assert_not_equals(e, wasmTagExnSamePayload);
assert_not_equals(e, wasmTagExnDiffPayload);
}
try {
result.instance.exports.catch_all_wasm_tag_rethrow();
} catch (e) {
assert_equals(e, wasmTagExn);
assert_not_equals(e, wasmTagExnSamePayload);
assert_not_equals(e, wasmTagExnDiffPayload);
}
});
}, "Identity check");