2
0

utils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var Utils = {
  2. createLocateRewrite: function(execName) {
  3. function rw(path) {
  4. if (path.endsWith('.worker.js')) {
  5. return execName + '.worker.js';
  6. } else if (path.endsWith('.js')) {
  7. return execName + '.js';
  8. } else if (path.endsWith('.wasm')) {
  9. return execName + '.wasm';
  10. }
  11. }
  12. return rw;
  13. },
  14. createInstantiatePromise: function(wasmLoader) {
  15. function instantiateWasm(imports, onSuccess) {
  16. wasmLoader.then(function(xhr) {
  17. WebAssembly.instantiate(xhr.response, imports).then(function(result) {
  18. onSuccess(result['instance'], result['module']);
  19. });
  20. });
  21. wasmLoader = null;
  22. return {};
  23. };
  24. return instantiateWasm;
  25. },
  26. copyToFS: function(fs, path, buffer) {
  27. var p = path.lastIndexOf("/");
  28. var dir = "/";
  29. if (p > 0) {
  30. dir = path.slice(0, path.lastIndexOf("/"));
  31. }
  32. try {
  33. fs.stat(dir);
  34. } catch (e) {
  35. if (e.errno !== 44) { // 'ENOENT', see https://github.com/emscripten-core/emscripten/blob/master/system/lib/libc/musl/arch/emscripten/bits/errno.h
  36. throw e;
  37. }
  38. fs['mkdirTree'](dir);
  39. }
  40. // With memory growth, canOwn should be false.
  41. fs['writeFile'](path, new Uint8Array(buffer), {'flags': 'wx+'});
  42. },
  43. findCanvas: function() {
  44. var nodes = document.getElementsByTagName('canvas');
  45. if (nodes.length && nodes[0] instanceof HTMLCanvasElement) {
  46. return nodes[0];
  47. }
  48. throw new Error("No canvas found");
  49. },
  50. isWebGLAvailable: function(majorVersion = 1) {
  51. var testContext = false;
  52. try {
  53. var testCanvas = document.createElement('canvas');
  54. if (majorVersion === 1) {
  55. testContext = testCanvas.getContext('webgl') || testCanvas.getContext('experimental-webgl');
  56. } else if (majorVersion === 2) {
  57. testContext = testCanvas.getContext('webgl2') || testCanvas.getContext('experimental-webgl2');
  58. }
  59. } catch (e) {}
  60. return !!testContext;
  61. }
  62. };