Skip to content

Commit 04a6ed7

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

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

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

+31-22
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,68 @@
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

2123
builder
22-
.addFunction("catch_rethrow", kSig_v_v)
24+
.addFunction("catch_js_tag_rethrow", kSig_v_v)
2325
.addBody([
2426
kExprTry, kWasmStmt,
25-
kExprCallFunction, jsfuncIndex,
26-
kExprCatch, tagIndex,
27+
kExprCallFunction, throwJSTagExnIndex,
28+
kExprCatch, jsTagIndex,
2729
kExprDrop,
2830
kExprRethrow, 0x00,
2931
kExprEnd
3032
])
3133
.exportFunc();
3234

3335
builder
34-
.addFunction("catch_all_rethrow", kSig_v_v)
36+
.addFunction("catch_all_js_tag_rethrow", kSig_v_v)
3537
.addBody([
3638
kExprTry, kWasmStmt,
37-
kExprCallFunction, jsfuncIndex,
39+
kExprCallFunction, throwJSTagExnIndex,
3840
kExprCatchAll,
3941
kExprRethrow, 0x00,
4042
kExprEnd
4143
])
4244
.exportFunc();
4345

4446
const buffer = builder.toBuffer();
47+
48+
// The exception object's identity should be preserved throw 'rethrow's in
49+
// Wasm code. Do tests with a tag defined in JS.
4550
WebAssembly.instantiate(buffer, imports).then(result => {
4651
try {
47-
result.instance.exports.catch_rethrow();
52+
result.instance.exports.catch_js_tag_rethrow();
4853
} catch (e) {
49-
assert_equals(e, exn);
50-
assert_not_equals(e, exn_same_payload);
51-
assert_not_equals(e, exn_diff_payload);
54+
assert_equals(e, jsTagExn);
55+
assert_equals(e.getArg(jsTag, 0), jsTagExn.getArg(jsTag, 0));
56+
// Even if they have the same payload, they are different objects, so they
57+
// shouldn't compare equal.
58+
assert_not_equals(e, jsTagExnSamePayload);
59+
assert_not_equals(e, jsTagExnDiffPayload);
5260
}
5361
try {
54-
result.instance.exports.catch_all_rethrow();
62+
result.instance.exports.catch_all_js_tag_rethrow();
5563
} catch (e) {
56-
assert_equals(e, exn);
57-
assert_not_equals(e, exn_same_payload);
58-
assert_not_equals(e, exn_diff_payload);
64+
assert_equals(e, jsTagExn);
65+
assert_equals(e.getArg(jsTag, 0), jsTagExn.getArg(jsTag, 0));
66+
assert_not_equals(e, jsTagExnSamePayload);
67+
assert_not_equals(e, jsTagExnDiffPayload);
5968
}
6069
});
6170
}, "Identity check");

test/js-api/wasm-module-builder.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -754,9 +754,9 @@ class WasmModuleBuilder {
754754

755755
addTag(type) {
756756
let type_index = (typeof type) == "number" ? type : this.addType(type);
757-
let except_index = this.tags.length + this.num_imported_tags;
757+
let tag_index = this.tags.length + this.num_imported_tags;
758758
this.tags.push(type_index);
759-
return except_index;
759+
return tag_index;
760760
}
761761

762762
addFunction(name, type) {

0 commit comments

Comments
 (0)