summaryrefslogtreecommitdiffstats
path: root/dom/promise/tests/test_webassembly_compile.html
blob: 0243df49caab47a97ee73463dc95ed2533a62aea (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>WebAssembly.compile Test</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script>
const wasmTextToBinary = SpecialPowers.unwrap(SpecialPowers.Cu.getJSTestingFunctions().wasmTextToBinary);
const wasmIsSupported = SpecialPowers.Cu.getJSTestingFunctions().wasmIsSupported
const fooModuleCode = wasmTextToBinary(`(module
  (func $foo (result i32) (i32.const 42))
  (export "foo" $foo)
)`, 'new-format');

function checkFooModule(m) {
  ok(m instanceof WebAssembly.Module, "got a module");
  var i = new WebAssembly.Instance(m);
  ok(i instanceof WebAssembly.Instance, "got an instance");
  ok(i.exports.foo() === 42, "got 42");
}

function checkFooInstance(i) {
  ok(i instanceof WebAssembly.Instance, "got a module");
  ok(i.exports.foo() === 42, "got 42");
}

function propertiesExist() {
  if (!wasmIsSupported()) {
    ok(!this["WebAssembly"], "If the device doesn't support, there will be no WebAssembly object");
    SimpleTest.finish();
    return;
  }

  ok(WebAssembly, "WebAssembly object should exist");
  ok(WebAssembly.compile, "WebAssembly.compile function should exist");
  runTest();
}

function compileFail() {
  WebAssembly.compile().then(
    () => { ok(false, "should have failed"); runTest() }
  ).catch(
    err => { ok(err instanceof TypeError, "empty compile failed"); runTest() }
  );
}

function compileSuccess() {
  WebAssembly.compile(fooModuleCode).then(
    m => { checkFooModule(m); runTest() }
  ).catch(
    err => { ok(false, String(err)); runTest() }
  );
}

function compileManySuccess() {
  const N = 100;

  var arr = [];
  for (var i = 0; i < N; i++)
    arr.push(WebAssembly.compile(fooModuleCode));

  SpecialPowers.gc();

  Promise.all(arr).then (ms => {
    ok(ms.length === N, "got the right number");
    for (var i = 0; i < N; i++)
      checkFooModule(ms[i]);
    runTest();
  }).catch(
    err => { ok(false, String(err)); runTest() }
  );
}

function compileInWorker() {
  var w = new Worker(`data:text/plain,
    onmessage = e => {
      WebAssembly.compile(e.data).then(m => {
          var i = new WebAssembly.Instance(m);
          if (i.exports.foo() !== 42)
            throw "bad i.exports.foo() result";
          postMessage("ok");
          close();
      }).catch(err => { throw err });
    }
  `);
  w.postMessage(fooModuleCode);
  w.onmessage = e => {
    ok(e.data === "ok", "worker test");
    runTest();
  }
}

function terminateCompileInWorker() {
    var w = new Worker(`data:text/plain,
      var fooModuleCode;
      function spawnWork() {
        const N = 100;
        var arr = [];
        for (var i = 0; i < N; i++)
          arr.push(WebAssembly.compile(fooModuleCode));
        Promise.all(arr).then(spawnWork);
      }
      onmessage = e => {
        fooModuleCode = e.data;
        spawnWork();
        postMessage("ok");
      }
    `);
    w.postMessage(fooModuleCode);
    w.onmessage = e => {
      ok(e.data === "ok", "worker finished first step");
      w.terminate();
      runTest();
    }
}

function instantiateFail() {
  WebAssembly.instantiate().then(
    () => { ok(false, "should have failed"); runTest() }
  ).catch(
    err => { ok(err instanceof TypeError, "empty compile failed"); runTest() }
  );
}

function instantiateSuccess() {
  WebAssembly.instantiate(fooModuleCode).then(
    r => { checkFooModule(r.module); checkFooInstance(r.instance); runTest() }
  ).catch(
    err => { ok(false, String(err)); runTest() }
  );
}

function chainSuccess() {
  WebAssembly.compile(fooModuleCode).then(
    m => WebAssembly.instantiate(m)
  ).then(
    i => { checkFooInstance(i); runTest() }
  ).catch(
    err => { ok(false, String(err)); runTest() }
  );
}

var tests = [ propertiesExist,
              compileFail,
              compileSuccess,
              compileManySuccess,
              compileInWorker,
              terminateCompileInWorker,
              instantiateFail,
              instantiateSuccess,
              chainSuccess
            ];

function runTest() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({"set": [["javascript.options.wasm", true]]}, runTest);
</script>
</body>
</html>