|
3 | 3 | // META: script=/wasm/jsapi/wasm-module-builder.js
|
4 | 4 |
|
5 | 5 | 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 |
| - |
11 | 6 | 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 | + |
14 | 16 | const imports = {
|
15 | 17 | module: {
|
16 |
| - jsfunc: function() { throw exn; }, |
17 |
| - tag: tag |
| 18 | + throwJSTagExn: function() { throw jsTagExn; }, |
| 19 | + jsTag: jsTag |
18 | 20 | }
|
19 | 21 | };
|
20 | 22 |
|
21 | 23 | builder
|
22 |
| - .addFunction("catch_rethrow", kSig_v_v) |
| 24 | + .addFunction("catch_js_tag_rethrow", kSig_v_v) |
23 | 25 | .addBody([
|
24 | 26 | kExprTry, kWasmStmt,
|
25 |
| - kExprCallFunction, jsfuncIndex, |
26 |
| - kExprCatch, tagIndex, |
| 27 | + kExprCallFunction, throwJSTagExnIndex, |
| 28 | + kExprCatch, jsTagIndex, |
27 | 29 | kExprDrop,
|
28 | 30 | kExprRethrow, 0x00,
|
29 | 31 | kExprEnd
|
30 | 32 | ])
|
31 | 33 | .exportFunc();
|
32 | 34 |
|
33 | 35 | builder
|
34 |
| - .addFunction("catch_all_rethrow", kSig_v_v) |
| 36 | + .addFunction("catch_all_js_tag_rethrow", kSig_v_v) |
35 | 37 | .addBody([
|
36 | 38 | kExprTry, kWasmStmt,
|
37 |
| - kExprCallFunction, jsfuncIndex, |
| 39 | + kExprCallFunction, throwJSTagExnIndex, |
38 | 40 | kExprCatchAll,
|
39 | 41 | kExprRethrow, 0x00,
|
40 | 42 | kExprEnd
|
41 | 43 | ])
|
42 | 44 | .exportFunc();
|
43 | 45 |
|
44 | 46 | 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. |
45 | 50 | WebAssembly.instantiate(buffer, imports).then(result => {
|
46 | 51 | try {
|
47 |
| - result.instance.exports.catch_rethrow(); |
| 52 | + result.instance.exports.catch_js_tag_rethrow(); |
48 | 53 | } 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); |
52 | 60 | }
|
53 | 61 | try {
|
54 |
| - result.instance.exports.catch_all_rethrow(); |
| 62 | + result.instance.exports.catch_all_js_tag_rethrow(); |
55 | 63 | } 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); |
59 | 68 | }
|
60 | 69 | });
|
61 | 70 | }, "Identity check");
|
0 commit comments