forked from playcanvas/playcanvas.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasm-loader.js
43 lines (40 loc) · 1.29 KB
/
wasm-loader.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
38
39
40
41
42
43
/* eslint-disable no-unused-vars */
// check for wasm module support
function wasmSupported() {
try {
if (typeof WebAssembly === "object" && typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module)
return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
}
} catch (e) { }
return false;
}
// load a script
function loadScriptAsync(url, doneCallback) {
var tag = document.createElement('script');
tag.onload = function () {
doneCallback();
};
tag.onerror = function () {
throw new Error('failed to load ' + url);
};
tag.async = true;
tag.src = url;
document.head.appendChild(tag);
}
// load and initialize a wasm module
function loadWasmModuleAsync(moduleName, jsUrl, binaryUrl, doneCallback) {
loadScriptAsync(jsUrl, function () {
var lib = window[moduleName];
window[moduleName + 'Lib'] = lib;
lib({
locateFile: function () {
return binaryUrl;
}
}).then(function (instance) {
window[moduleName] = instance;
doneCallback();
});
});
}