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

Commit e987be4

Browse files
authored
[js-api] Tidy up rethrow identity test (#256)
This makes some cosmetic changes to the identity-preserving `rethrow` test added in #253. This is to prepare for adding the same set of tests for a Wasm-defined-and-exported tag, as requrested in #253 (comment).
1 parent 6612531 commit e987be4

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

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

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,70 @@
33
// META: script=/wasm/jsapi/wasm-module-builder.js
44

55
test(() => {
6-
const tag = new WebAssembly.Tag({ parameters: ["i32"] });
7-
const exn = new WebAssembly.Exception(tag, [42]);
8-
const exn_same_payload = new WebAssembly.Exception(tag, [42]);
9-
const exn_diff_payload = new WebAssembly.Exception(tag, [53]);
10-
116
const builder = new WasmModuleBuilder();
12-
const jsfuncIndex = builder.addImport("module", "jsfunc", kSig_v_v);
13-
const tagIndex = builder.addImportedTag("module", "tag", kSig_v_i);
7+
8+
// Tag defined in JavaScript and imported into Wasm
9+
const jsTag = new WebAssembly.Tag({ parameters: ["i32"] });
10+
const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i)
11+
const jsTagExn = new WebAssembly.Exception(jsTag, [42]);
12+
const jsTagExnSamePayload = new WebAssembly.Exception(jsTag, [42]);
13+
const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]);
14+
const throwJSTagExnIndex = builder.addImport("module", "throwJSTagExn", kSig_v_v);
15+
1416
const imports = {
1517
module: {
16-
jsfunc: function() { throw exn; },
17-
tag: tag
18+
throwJSTagExn: function() { throw jsTagExn; },
19+
jsTag: jsTag
1820
}
1921
};
2022

23+
// Call a JS function that throws an exception using a JS-defined tag, catches
24+
// it with a 'catch' instruction, and rethrows it.
2125
builder
22-
.addFunction("catch_rethrow", kSig_v_v)
26+
.addFunction("catch_js_tag_rethrow", kSig_v_v)
2327
.addBody([
2428
kExprTry, kWasmStmt,
25-
kExprCallFunction, jsfuncIndex,
26-
kExprCatch, tagIndex,
29+
kExprCallFunction, throwJSTagExnIndex,
30+
kExprCatch, jsTagIndex,
2731
kExprDrop,
2832
kExprRethrow, 0x00,
2933
kExprEnd
3034
])
3135
.exportFunc();
3236

37+
// Call a JS function that throws an exception using a JS-defined tag, catches
38+
// it with a 'catch_all' instruction, and rethrows it.
3339
builder
34-
.addFunction("catch_all_rethrow", kSig_v_v)
40+
.addFunction("catch_all_js_tag_rethrow", kSig_v_v)
3541
.addBody([
3642
kExprTry, kWasmStmt,
37-
kExprCallFunction, jsfuncIndex,
43+
kExprCallFunction, throwJSTagExnIndex,
3844
kExprCatchAll,
3945
kExprRethrow, 0x00,
4046
kExprEnd
4147
])
4248
.exportFunc();
4349

4450
const buffer = builder.toBuffer();
51+
52+
// The exception object's identity should be preserved across 'rethrow's in
53+
// Wasm code. Do tests with a tag defined in JS.
4554
WebAssembly.instantiate(buffer, imports).then(result => {
4655
try {
47-
result.instance.exports.catch_rethrow();
56+
result.instance.exports.catch_js_tag_rethrow();
4857
} catch (e) {
49-
assert_equals(e, exn);
50-
assert_not_equals(e, exn_same_payload);
51-
assert_not_equals(e, exn_diff_payload);
58+
assert_equals(e, jsTagExn);
59+
// Even if they have the same payload, they are different objects, so they
60+
// shouldn't compare equal.
61+
assert_not_equals(e, jsTagExnSamePayload);
62+
assert_not_equals(e, jsTagExnDiffPayload);
5263
}
5364
try {
54-
result.instance.exports.catch_all_rethrow();
65+
result.instance.exports.catch_all_js_tag_rethrow();
5566
} catch (e) {
56-
assert_equals(e, exn);
57-
assert_not_equals(e, exn_same_payload);
58-
assert_not_equals(e, exn_diff_payload);
67+
assert_equals(e, jsTagExn);
68+
assert_not_equals(e, jsTagExnSamePayload);
69+
assert_not_equals(e, jsTagExnDiffPayload);
5970
}
6071
});
6172
}, "Identity check");

0 commit comments

Comments
 (0)