-
Notifications
You must be signed in to change notification settings - Fork 42
/
load-wasm.js
37 lines (33 loc) · 1.31 KB
/
load-wasm.js
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
// This file implements a browser utility function to asychronously fetch,
// decode and compile a given WebAssembly module. The caller passes in the url
// of the .wasm file and the returned promise resolves to a compiled (but
// unlinked) module.
var loadWebAssembly = (function() {
var globalNameCounter = 0;
return function(packedURL, workerURL) {
workerURL = workerURL || 'load-wasm-worker.js';
return new Promise(function(resolve, reject) {
var callbackName = "";
do {
callbackName = "onFinishLoadWebAssembly_" + globalNameCounter++;
} while (Object.prototype.hasOwnProperty.call(window, callbackName));
window[callbackName] = function(asmModule) {
delete window[callbackName];
resolve(asmModule);
}
var worker = new Worker(workerURL);
worker.postMessage({url:packedURL, callbackName:callbackName});
worker.onmessage = function (e) {
if (!(e.data instanceof Blob)) {
reject("load-wasm-worker.js failed with: " + e.data);
return;
}
var script = document.createElement('script');
var url = URL.createObjectURL(e.data);
script.onload = script.onerror = function() { URL.revokeObjectURL(url) }
script.src = url;
document.body.appendChild(script);
}
});
}
})();