test_webassembly_compile_worker.js (1672B)
1 const sampleURL = "test_webassembly_compile_sample.wasm"; 2 const sampleExportName = "run"; 3 const sampleResult = 1275; 4 5 /* eslint-disable no-throw-literal */ 6 7 function checkSampleModule(m) { 8 if (!(m instanceof WebAssembly.Module)) { 9 throw "not a module"; 10 } 11 var i = new WebAssembly.Instance(m); 12 if (!(i instanceof WebAssembly.Instance)) { 13 throw "not an instance"; 14 } 15 if (i.exports[sampleExportName]() !== sampleResult) { 16 throw "wrong result"; 17 } 18 } 19 20 function checkSampleInstance(i) { 21 if (!(i instanceof WebAssembly.Instance)) { 22 throw "not an instance"; 23 } 24 if (i.exports[sampleExportName]() !== sampleResult) { 25 throw "wrong result"; 26 } 27 } 28 29 const initObj = { headers: { "Content-Type": "application/wasm" } }; 30 31 onmessage = e => { 32 WebAssembly.compile(e.data) 33 .then(m => checkSampleModule(m)) 34 .then(() => WebAssembly.instantiate(e.data)) 35 .then(({ module, instance }) => { 36 checkSampleModule(module); 37 checkSampleInstance(instance); 38 }) 39 .then(() => WebAssembly.compileStreaming(new Response(e.data, initObj))) 40 .then(m => checkSampleModule(m)) 41 .then(() => WebAssembly.instantiateStreaming(new Response(e.data, initObj))) 42 .then(({ module, instance }) => { 43 checkSampleModule(module); 44 checkSampleInstance(instance); 45 }) 46 .then(() => WebAssembly.compileStreaming(fetch(sampleURL))) 47 .then(m => checkSampleModule(m)) 48 .then(() => WebAssembly.instantiateStreaming(fetch(sampleURL))) 49 .then(({ module, instance }) => { 50 checkSampleModule(module); 51 checkSampleInstance(instance); 52 }) 53 .then(() => postMessage("ok")) 54 .catch(err => postMessage("fail: " + err)); 55 };