summaryrefslogtreecommitdiffstats
path: root/dom/promise/tests/test_webassembly_compile_worker.js
blob: 90c3551137b03cbadc2317e8b2bcb6fcb9286a52 (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
const sampleURL = "test_webassembly_compile_sample.wasm";
const sampleExportName = "run";
const sampleResult = 1275;

/* eslint-disable no-throw-literal */

function checkSampleModule(m) {
  if (!(m instanceof WebAssembly.Module)) {
    throw "not a module";
  }
  var i = new WebAssembly.Instance(m);
  if (!(i instanceof WebAssembly.Instance)) {
    throw "not an instance";
  }
  if (i.exports[sampleExportName]() !== sampleResult) {
    throw "wrong result";
  }
}

function checkSampleInstance(i) {
  if (!(i instanceof WebAssembly.Instance)) {
    throw "not an instance";
  }
  if (i.exports[sampleExportName]() !== sampleResult) {
    throw "wrong result";
  }
}

const initObj = { headers: { "Content-Type": "application/wasm" } };

onmessage = e => {
  WebAssembly.compile(e.data)
    .then(m => checkSampleModule(m))
    .then(() => WebAssembly.instantiate(e.data))
    .then(({ module, instance }) => {
      checkSampleModule(module);
      checkSampleInstance(instance);
    })
    .then(() => WebAssembly.compileStreaming(new Response(e.data, initObj)))
    .then(m => checkSampleModule(m))
    .then(() => WebAssembly.instantiateStreaming(new Response(e.data, initObj)))
    .then(({ module, instance }) => {
      checkSampleModule(module);
      checkSampleInstance(instance);
    })
    .then(() => WebAssembly.compileStreaming(fetch(sampleURL)))
    .then(m => checkSampleModule(m))
    .then(() => WebAssembly.instantiateStreaming(fetch(sampleURL)))
    .then(({ module, instance }) => {
      checkSampleModule(module);
      checkSampleInstance(instance);
    })
    .then(() => postMessage("ok"))
    .catch(err => postMessage("fail: " + err));
};