2020-11-23 11:13:52 +00:00
|
|
|
const Utils = { // eslint-disable-line no-unused-vars
|
2020-11-19 15:54:07 +00:00
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
createLocateRewrite: function (execName) {
|
2020-03-11 10:55:28 +00:00
|
|
|
function rw(path) {
|
|
|
|
if (path.endsWith('.worker.js')) {
|
2020-11-23 11:13:52 +00:00
|
|
|
return `${execName}.worker.js`;
|
2020-11-10 10:05:22 +00:00
|
|
|
} else if (path.endsWith('.audio.worklet.js')) {
|
2020-11-23 11:13:52 +00:00
|
|
|
return `${execName}.audio.worklet.js`;
|
2020-03-11 10:55:28 +00:00
|
|
|
} else if (path.endsWith('.js')) {
|
2020-11-23 11:13:52 +00:00
|
|
|
return `${execName}.js`;
|
2020-03-11 10:55:28 +00:00
|
|
|
} else if (path.endsWith('.wasm')) {
|
2020-11-23 11:13:52 +00:00
|
|
|
return `${execName}.wasm`;
|
2020-03-11 10:55:28 +00:00
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
return path;
|
2020-03-11 10:55:28 +00:00
|
|
|
}
|
|
|
|
return rw;
|
|
|
|
},
|
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
createInstantiatePromise: function (wasmLoader) {
|
2020-11-19 15:54:07 +00:00
|
|
|
let loader = wasmLoader;
|
2020-03-11 10:55:28 +00:00
|
|
|
function instantiateWasm(imports, onSuccess) {
|
2020-11-23 11:13:52 +00:00
|
|
|
loader.then(function (xhr) {
|
|
|
|
WebAssembly.instantiate(xhr.response, imports).then(function (result) {
|
2020-03-11 10:55:28 +00:00
|
|
|
onSuccess(result['instance'], result['module']);
|
|
|
|
});
|
|
|
|
});
|
2020-11-19 15:54:07 +00:00
|
|
|
loader = null;
|
2020-03-11 10:55:28 +00:00
|
|
|
return {};
|
2020-11-23 11:13:52 +00:00
|
|
|
}
|
2020-03-11 10:55:28 +00:00
|
|
|
|
|
|
|
return instantiateWasm;
|
|
|
|
},
|
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
findCanvas: function () {
|
|
|
|
const nodes = document.getElementsByTagName('canvas');
|
2020-03-11 10:55:28 +00:00
|
|
|
if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
|
|
|
|
return nodes[0];
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
return null;
|
2020-03-11 10:55:28 +00:00
|
|
|
},
|
|
|
|
|
2020-11-23 11:13:52 +00:00
|
|
|
isWebGLAvailable: function (majorVersion = 1) {
|
|
|
|
let testContext = false;
|
2020-03-11 10:55:28 +00:00
|
|
|
try {
|
2020-11-23 11:13:52 +00:00
|
|
|
const testCanvas = document.createElement('canvas');
|
2020-03-11 10:55:28 +00:00
|
|
|
if (majorVersion === 1) {
|
|
|
|
testContext = testCanvas.getContext('webgl') || testCanvas.getContext('experimental-webgl');
|
|
|
|
} else if (majorVersion === 2) {
|
|
|
|
testContext = testCanvas.getContext('webgl2') || testCanvas.getContext('experimental-webgl2');
|
|
|
|
}
|
2020-11-19 15:54:07 +00:00
|
|
|
} catch (e) {
|
|
|
|
// Not available
|
|
|
|
}
|
2020-03-11 10:55:28 +00:00
|
|
|
return !!testContext;
|
2020-11-23 11:13:52 +00:00
|
|
|
},
|
2020-03-11 10:55:28 +00:00
|
|
|
};
|