• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: global=window,dedicatedworker,jsshell
2// META: script=/wasm/jsapi/assertions.js
3// META: script=/wasm/jsapi/wasm-module-builder.js
4
5test(() => {
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  const builder = new WasmModuleBuilder();
12  const jsfuncIndex = builder.addImport("module", "jsfunc", kSig_v_v);
13  const tagIndex = builder.addImportedTag("module", "tag", kSig_v_i);
14  const imports = {
15    module: {
16      jsfunc: function() { throw exn; },
17      tag: tag
18    }
19  };
20
21  builder
22    .addFunction("catch_rethrow", kSig_v_v)
23    .addBody([
24      kExprTry, kWasmStmt,
25        kExprCallFunction, jsfuncIndex,
26      kExprCatch, tagIndex,
27        kExprDrop,
28        kExprRethrow, 0x00,
29      kExprEnd
30    ])
31    .exportFunc();
32
33  builder
34    .addFunction("catch_all_rethrow", kSig_v_v)
35    .addBody([
36      kExprTry, kWasmStmt,
37        kExprCallFunction, jsfuncIndex,
38      kExprCatchAll,
39        kExprRethrow, 0x00,
40      kExprEnd
41    ])
42    .exportFunc();
43
44  const buffer = builder.toBuffer();
45  WebAssembly.instantiate(buffer, imports).then(result => {
46    try {
47      result.instance.exports.catch_rethrow();
48    } catch (e) {
49      assert_equals(e, exn);
50      assert_not_equals(e, exn_same_payload);
51      assert_not_equals(e, exn_diff_payload);
52    }
53    try {
54      result.instance.exports.catch_all_rethrow();
55    } catch (e) {
56      assert_equals(e, exn);
57      assert_not_equals(e, exn_same_payload);
58      assert_not_equals(e, exn_diff_payload);
59    }
60  });
61}, "Identity check");
62