diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2020-03-11 18:24:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-11 18:24:55 +0100 |
commit | aeff25f3134c1af34285bb1021c709936cf5377d (patch) | |
tree | 1dbbb9e75cd26db30587a50945012a87af5eae61 /platform/javascript/engine/utils.js | |
parent | 98cdf50a5591e7629bde7688ceda82b15bd9e858 (diff) | |
parent | 919bbf8077e25b9250bf3ee84220b3c4b46a3e3f (diff) |
Merge pull request #36983 from Faless/js/threads_and_more
[HTML5] Refactor JS, threads support, closures.
Diffstat (limited to 'platform/javascript/engine/utils.js')
-rw-r--r-- | platform/javascript/engine/utils.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/platform/javascript/engine/utils.js b/platform/javascript/engine/utils.js new file mode 100644 index 0000000000..fdff90a923 --- /dev/null +++ b/platform/javascript/engine/utils.js @@ -0,0 +1,69 @@ +var Utils = { + + createLocateRewrite: function(execName) { + function rw(path) { + if (path.endsWith('.worker.js')) { + return execName + '.worker.js'; + } else if (path.endsWith('.js')) { + return execName + '.js'; + } else if (path.endsWith('.wasm')) { + return execName + '.wasm'; + } + } + return rw; + }, + + createInstantiatePromise: function(wasmLoader) { + function instantiateWasm(imports, onSuccess) { + wasmLoader.then(function(xhr) { + WebAssembly.instantiate(xhr.response, imports).then(function(result) { + onSuccess(result['instance'], result['module']); + }); + }); + wasmLoader = null; + return {}; + }; + + return instantiateWasm; + }, + + copyToFS: function(fs, path, buffer) { + var p = path.lastIndexOf("/"); + var dir = "/"; + if (p > 0) { + dir = path.slice(0, path.lastIndexOf("/")); + } + try { + fs.stat(dir); + } catch (e) { + if (e.errno !== 44) { // 'ENOENT', see https://github.com/emscripten-core/emscripten/blob/master/system/lib/libc/musl/arch/emscripten/bits/errno.h + throw e; + } + fs['mkdirTree'](dir); + } + // With memory growth, canOwn should be false. + fs['writeFile'](path, new Uint8Array(buffer), {'flags': 'wx+'}); + }, + + findCanvas: function() { + var nodes = document.getElementsByTagName('canvas'); + if (nodes.length && nodes[0] instanceof HTMLCanvasElement) { + return nodes[0]; + } + throw new Error("No canvas found"); + }, + + isWebGLAvailable: function(majorVersion = 1) { + + var testContext = false; + try { + var testCanvas = document.createElement('canvas'); + if (majorVersion === 1) { + testContext = testCanvas.getContext('webgl') || testCanvas.getContext('experimental-webgl'); + } else if (majorVersion === 2) { + testContext = testCanvas.getContext('webgl2') || testCanvas.getContext('experimental-webgl2'); + } + } catch (e) {} + return !!testContext; + } +}; |