|
51 | 51 | <figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
|
52 | 52 | <div class="emscripten" id="status">Downloading...</div>
|
53 | 53 | <div class="emscripten">
|
54 |
| - <progress value="0" max="100" id="progress" hidden=1></progress> |
| 54 | + <progress value="0" max="100" id="progress" hidden=1></progress> |
55 | 55 | </div>
|
56 | 56 | <div class="emscripten_border">
|
57 | 57 | <canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
|
|
61 | 61 | <input type="checkbox" id="resize">Resize canvas
|
62 | 62 | <input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer
|
63 | 63 |
|
64 |
| - <input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked, |
| 64 | + <input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked, |
65 | 65 | document.getElementById('resize').checked)">
|
66 | 66 | </div>
|
67 |
| - |
| 67 | + |
68 | 68 | <hr/>
|
69 | 69 | <textarea class="emscripten" id="output" rows="8"></textarea>
|
70 | 70 | <hr>
|
|
76 | 76 | var Module = {
|
77 | 77 | preRun: [],
|
78 | 78 | postRun: [],
|
79 |
| - print: (function() { |
| 79 | + print: (() => { |
80 | 80 | var element = document.getElementById('output');
|
81 | 81 | if (element) element.value = ''; // clear browser cache
|
82 |
| - return function(text) { |
83 |
| - if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); |
| 82 | + return (...args) => { |
| 83 | + var text = args.join(' '); |
84 | 84 | // These replacements are necessary if you render to raw HTML
|
85 | 85 | //text = text.replace(/&/g, "&");
|
86 | 86 | //text = text.replace(/</g, "<");
|
|
93 | 93 | }
|
94 | 94 | };
|
95 | 95 | })(),
|
96 |
| - printErr: function(text) { |
97 |
| - if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' '); |
| 96 | + printErr: (...args) => { |
| 97 | + var text = args.join(' '); |
98 | 98 | if (0) { // XXX disabled for safety typeof dump == 'function') {
|
99 | 99 | dump(text + '\n'); // fast, straight to the real console
|
100 | 100 | } else {
|
101 | 101 | console.error(text);
|
102 | 102 | }
|
103 | 103 | },
|
104 |
| - canvas: (function() { |
| 104 | + canvas: (() => { |
105 | 105 | var canvas = document.getElementById('canvas');
|
106 | 106 |
|
107 | 107 | // As a default initial behavior, pop up an alert when webgl context is lost. To make your
|
108 | 108 | // application robust, you may want to override this behavior before shipping!
|
109 | 109 | // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
|
110 |
| - canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); |
| 110 | + canvas.addEventListener("webglcontextlost", (e) => { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); |
111 | 111 |
|
112 | 112 | return canvas;
|
113 | 113 | })(),
|
114 |
| - setStatus: function(text) { |
| 114 | + setStatus: (text) => { |
115 | 115 | if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
|
116 | 116 | if (text === Module.setStatus.text) return;
|
117 | 117 | var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
|
|
132 | 132 | statusElement.innerHTML = text;
|
133 | 133 | },
|
134 | 134 | totalDependencies: 0,
|
135 |
| - monitorRunDependencies: function(left) { |
| 135 | + monitorRunDependencies: (left) => { |
136 | 136 | this.totalDependencies = Math.max(this.totalDependencies, left);
|
137 | 137 | Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
|
138 | 138 | }
|
139 | 139 | };
|
140 | 140 | Module.setStatus('Downloading...');
|
141 |
| - window.onerror = function() { |
| 141 | + window.onerror = () => { |
142 | 142 | Module.setStatus('Exception thrown, see JavaScript console');
|
143 | 143 | spinnerElement.style.display = 'none';
|
144 |
| - Module.setStatus = function(text) { |
| 144 | + Module.setStatus = (text) => { |
145 | 145 | if (text) Module.printErr('[post-exception status] ' + text);
|
146 | 146 | };
|
147 | 147 | };
|
148 | 148 |
|
149 | 149 | function downloadWasm(url) {
|
150 |
| - return new Promise(function(resolve, reject) { |
151 |
| - var wasmXHR = new XMLHttpRequest(); |
152 |
| - wasmXHR.open('GET', url, true); |
153 |
| - wasmXHR.responseType = 'arraybuffer'; |
154 |
| - wasmXHR.onload = function() { resolve(wasmXHR.response); } |
155 |
| - wasmXHR.onerror = function() { reject('error ' + wasmXHR.status); } |
156 |
| - wasmXHR.send(null); |
157 |
| - }); |
| 150 | + console.log('fetching wasm: ', url); |
| 151 | + return fetch(url).then((response) => response.arrayBuffer()); |
158 | 152 | }
|
159 | 153 |
|
160 | 154 | var wasm = downloadWasm('manual_wasm_instantiate.wasm');
|
161 | 155 |
|
162 |
| - // Module.instantiateWasm is a user-implemented callback which the Emscripten runtime calls to perform |
163 |
| - // the WebAssembly instantiation action. The callback function will be called with two parameters, imports |
164 |
| - // and successCallback. imports is a JS object which contains all the function imports that need to be passed |
165 |
| - // to the Module when instantiating, and once instantiated, the function should call successCallback() with |
166 |
| - // the WebAssembly Instance object. |
167 |
| - // The instantiation can be performed either synchronously or asynchronously. The return value of this function |
168 |
| - // should contain the exports object of the instantiated Module, or an empty dictionary object {} if the |
169 |
| - // instantiation is performed asynchronously, or false if instantiation failed. |
170 |
| - Module.instantiateWasm = function(imports, successCallback) { |
| 156 | + // Module.instantiateWasm is a user-implemented callback which the |
| 157 | + // Emscripten runtime calls to perform the WebAssembly instantiation |
| 158 | + // action. The callback function will be called with two parameters, |
| 159 | + // imports and successCallback. imports is a JS object which contains |
| 160 | + // all the function imports that need to be passed to the Module when |
| 161 | + // instantiating, and once instantiated, the function should call |
| 162 | + // successCallback() with the WebAssembly Instance object. |
| 163 | + // The instantiation can be performed either synchronously or |
| 164 | + // asynchronously. The return value of this function should contain the |
| 165 | + // exports object of the instantiated Module, or an empty dictionary |
| 166 | + // object {} if the instantiation is performed asynchronously, or false |
| 167 | + // if instantiation failed. |
| 168 | + Module.instantiateWasm = (imports, successCallback) => { |
171 | 169 | console.log('instantiateWasm: instantiating asynchronously');
|
172 |
| - wasm.then((wasmBinary) => { |
| 170 | + wasm.then((bytes) => { |
173 | 171 | console.log('wasm download finished, begin instantiating');
|
174 |
| - var wasmInstantiate = WebAssembly.instantiate(new Uint8Array(wasmBinary), imports).then((output) => { |
| 172 | + var wasmInstantiate = WebAssembly.instantiate(bytes, imports).then((output) => { |
175 | 173 | // When overriding instantiateWasm, in asan builds, we also need
|
176 | 174 | // to take care of creating the WasmOffsetConverter
|
177 | 175 | if (typeof WasmOffsetConverter != "undefined") {
|
178 |
| - wasmOffsetConverter = new WasmOffsetConverter(wasmBinary, output.module); |
| 176 | + wasmOffsetConverter = new WasmOffsetConverter(bytes, output.module); |
179 | 177 | }
|
180 | 178 | console.log('wasm instantiation succeeded');
|
181 | 179 | Module.testWasmInstantiationSucceeded = 1;
|
182 | 180 | successCallback(output.instance);
|
183 |
| - }).catch(function(e) { |
| 181 | + }).catch((e) => { |
184 | 182 | console.log('wasm instantiation failed! ' + e);
|
185 | 183 | });
|
186 | 184 | });
|
|
0 commit comments