Skip to content

Commit 89c2a83

Browse files
authored
Improve manual_wasm_instantiate.html (#21258)
Modernize this test a little by using fetch API and JS spread operator.
1 parent 00e107f commit 89c2a83

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

test/manual_wasm_instantiate.html

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
<figure style="overflow:visible;" id="spinner"><div class="spinner"></div><center style="margin-top:0.5em"><strong>emscripten</strong></center></figure>
5252
<div class="emscripten" id="status">Downloading...</div>
5353
<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>
5555
</div>
5656
<div class="emscripten_border">
5757
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
@@ -61,10 +61,10 @@
6161
<input type="checkbox" id="resize">Resize canvas
6262
<input type="checkbox" id="pointerLock" checked>Lock/hide mouse pointer
6363
&nbsp;&nbsp;&nbsp;
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,
6565
document.getElementById('resize').checked)">
6666
</div>
67-
67+
6868
<hr/>
6969
<textarea class="emscripten" id="output" rows="8"></textarea>
7070
<hr>
@@ -76,11 +76,11 @@
7676
var Module = {
7777
preRun: [],
7878
postRun: [],
79-
print: (function() {
79+
print: (() => {
8080
var element = document.getElementById('output');
8181
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(' ');
8484
// These replacements are necessary if you render to raw HTML
8585
//text = text.replace(/&/g, "&amp;");
8686
//text = text.replace(/</g, "&lt;");
@@ -93,25 +93,25 @@
9393
}
9494
};
9595
})(),
96-
printErr: function(text) {
97-
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
96+
printErr: (...args) => {
97+
var text = args.join(' ');
9898
if (0) { // XXX disabled for safety typeof dump == 'function') {
9999
dump(text + '\n'); // fast, straight to the real console
100100
} else {
101101
console.error(text);
102102
}
103103
},
104-
canvas: (function() {
104+
canvas: (() => {
105105
var canvas = document.getElementById('canvas');
106106

107107
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
108108
// application robust, you may want to override this behavior before shipping!
109109
// 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);
111111

112112
return canvas;
113113
})(),
114-
setStatus: function(text) {
114+
setStatus: (text) => {
115115
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
116116
if (text === Module.setStatus.text) return;
117117
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
@@ -132,55 +132,53 @@
132132
statusElement.innerHTML = text;
133133
},
134134
totalDependencies: 0,
135-
monitorRunDependencies: function(left) {
135+
monitorRunDependencies: (left) => {
136136
this.totalDependencies = Math.max(this.totalDependencies, left);
137137
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
138138
}
139139
};
140140
Module.setStatus('Downloading...');
141-
window.onerror = function() {
141+
window.onerror = () => {
142142
Module.setStatus('Exception thrown, see JavaScript console');
143143
spinnerElement.style.display = 'none';
144-
Module.setStatus = function(text) {
144+
Module.setStatus = (text) => {
145145
if (text) Module.printErr('[post-exception status] ' + text);
146146
};
147147
};
148148

149149
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());
158152
}
159153

160154
var wasm = downloadWasm('manual_wasm_instantiate.wasm');
161155

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) => {
171169
console.log('instantiateWasm: instantiating asynchronously');
172-
wasm.then((wasmBinary) => {
170+
wasm.then((bytes) => {
173171
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) => {
175173
// When overriding instantiateWasm, in asan builds, we also need
176174
// to take care of creating the WasmOffsetConverter
177175
if (typeof WasmOffsetConverter != "undefined") {
178-
wasmOffsetConverter = new WasmOffsetConverter(wasmBinary, output.module);
176+
wasmOffsetConverter = new WasmOffsetConverter(bytes, output.module);
179177
}
180178
console.log('wasm instantiation succeeded');
181179
Module.testWasmInstantiationSucceeded = 1;
182180
successCallback(output.instance);
183-
}).catch(function(e) {
181+
}).catch((e) => {
184182
console.log('wasm instantiation failed! ' + e);
185183
});
186184
});

0 commit comments

Comments
 (0)