summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/exceptions/import-export.js
blob: fe7646678fb62814001a150b40ef5dc178eef793 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Tests for Wasm exception import and export.

function testImports() {
  var mod = `
    (module
      (type (func (param i32 i32)))
      (import "m" "exn" (tag (type 0))))
 `;

  assertErrorMessage(
    () => wasmEvalText(mod, { m: { exn: "not a tag" } }),
    WebAssembly.LinkError,
    /import object field 'exn' is not a Tag/
  );
}

function testExports() {
  var exports1 = wasmEvalText(`
    (module (type (func)) (tag (export "exn") (type 0)))
  `).exports;

  assertEq(typeof exports1.exn, "object");
  assertEq(exports1.exn instanceof WebAssembly.Tag, true);

  var exports2 = wasmEvalText(`
    (module
      (type (func (param i32 i32)))
      (tag (export "exn") (type 0)))
  `).exports;

  assertEq(typeof exports2.exn, "object");
  assertEq(exports2.exn instanceof WebAssembly.Tag, true);
}

function testImportExport() {
  var exports = wasmEvalText(`
    (module
      (type (func (param i32)))
      (tag (export "exn") (type 0)))
  `).exports;

  wasmEvalText(
    `
    (module
      (type (func (param i32)))
      (import "m" "exn" (tag (type 0))))
  `,
    { m: exports }
  );

  assertErrorMessage(
    () => {
      wasmEvalText(
        `
      (module
        (type (func (param)))
        (import "m" "exn" (tag (type 0))))
    `,
        { m: exports }
      );
    },
    WebAssembly.LinkError,
    /imported tag 'm.exn' signature mismatch/
  );
}

// Test imports/exports descriptions.
function testDescriptions() {
  const imports = WebAssembly.Module.imports(
    new WebAssembly.Module(
      wasmTextToBinary(`
        (module $m
          (type (func))
          (import "m" "e" (tag (type 0))))
      `)
    )
  );

  const exports = WebAssembly.Module.exports(
    new WebAssembly.Module(
      wasmTextToBinary(`
        (module
          (type (func))
          (tag (export "e") (type 0)))
      `)
    )
  );

  assertEq(imports[0].module, "m");
  assertEq(imports[0].name, "e");
  assertEq(imports[0].kind, "tag");

  assertEq(exports[0].name, "e");
  assertEq(exports[0].kind, "tag");
}

testImports();
testExports();
testImportExport();
testDescriptions();