Skip to content

Commit 585c7da

Browse files
authored
[js-api] Test Wasm-defined tag in rethrow identity test (#265)
This addresses #253 (comment) by adding a set of tests that use a Wasm-defined and exported tag.
1 parent 0f577bd commit 585c7da

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

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

+59-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ test(() => {
1313
const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]);
1414
const throwJSTagExnIndex = builder.addImport("module", "throwJSTagExn", kSig_v_v);
1515

16+
// Tag defined in Wasm and exported to JS
17+
const wasmTagIndex = builder.addTag(kSig_v_i);
18+
builder.addExportOfKind("wasmTag", kExternalTag, wasmTagIndex);
19+
const throwWasmTagExnIndex = builder.addImport("module", "throwWasmTagExn", kSig_v_v);
20+
// Will be assigned after an instance is created
21+
let wasmTagExn = null;
22+
let wasmTagExnSamePayload = null;
23+
let wasmTagExnDiffPayload = null;
24+
1625
const imports = {
1726
module: {
1827
throwJSTagExn: function() { throw jsTagExn; },
28+
throwWasmTagExn: function() { throw wasmTagExn; },
1929
jsTag: jsTag
2030
}
2131
};
@@ -34,6 +44,20 @@ test(() => {
3444
])
3545
.exportFunc();
3646

47+
// Call a JS function that throws an exception using a Wasm-defined tag,
48+
// catches it with a 'catch' instruction, and rethrows it.
49+
builder
50+
.addFunction("catch_wasm_tag_rethrow", kSig_v_v)
51+
.addBody([
52+
kExprTry, kWasmStmt,
53+
kExprCallFunction, throwWasmTagExnIndex,
54+
kExprCatch, wasmTagIndex,
55+
kExprDrop,
56+
kExprRethrow, 0x00,
57+
kExprEnd
58+
])
59+
.exportFunc();
60+
3761
// Call a JS function that throws an exception using a JS-defined tag, catches
3862
// it with a 'catch_all' instruction, and rethrows it.
3963
builder
@@ -47,11 +71,24 @@ test(() => {
4771
])
4872
.exportFunc();
4973

74+
// Call a JS function that throws an exception using a Wasm-defined tag,
75+
// catches it with a 'catch_all' instruction, and rethrows it.
76+
builder
77+
.addFunction("catch_all_wasm_tag_rethrow", kSig_v_v)
78+
.addBody([
79+
kExprTry, kWasmStmt,
80+
kExprCallFunction, throwWasmTagExnIndex,
81+
kExprCatchAll,
82+
kExprRethrow, 0x00,
83+
kExprEnd
84+
])
85+
.exportFunc();
86+
5087
const buffer = builder.toBuffer();
5188

52-
// The exception object's identity should be preserved across 'rethrow's in
53-
// Wasm code. Do tests with a tag defined in JS.
5489
WebAssembly.instantiate(buffer, imports).then(result => {
90+
// The exception object's identity should be preserved across 'rethrow's in
91+
// Wasm code. Do tests with a tag defined in JS.
5592
try {
5693
result.instance.exports.catch_js_tag_rethrow();
5794
} catch (e) {
@@ -68,5 +105,25 @@ test(() => {
68105
assert_not_equals(e, jsTagExnSamePayload);
69106
assert_not_equals(e, jsTagExnDiffPayload);
70107
}
108+
109+
// Do the same tests with a tag defined in Wasm.
110+
const wasmTag = result.instance.exports.wasmTag;
111+
wasmTagExn = new WebAssembly.Exception(wasmTag, [42]);
112+
wasmTagExnSamePayload = new WebAssembly.Exception(wasmTag, [42]);
113+
wasmTagExnDiffPayload = new WebAssembly.Exception(wasmTag, [53]);
114+
try {
115+
result.instance.exports.catch_wasm_tag_rethrow();
116+
} catch (e) {
117+
assert_equals(e, wasmTagExn);
118+
assert_not_equals(e, wasmTagExnSamePayload);
119+
assert_not_equals(e, wasmTagExnDiffPayload);
120+
}
121+
try {
122+
result.instance.exports.catch_all_wasm_tag_rethrow();
123+
} catch (e) {
124+
assert_equals(e, wasmTagExn);
125+
assert_not_equals(e, wasmTagExnSamePayload);
126+
assert_not_equals(e, wasmTagExnDiffPayload);
127+
}
71128
});
72129
}, "Identity check");

0 commit comments

Comments
 (0)