Skip to content

Commit 81ae876

Browse files
authored
Fix loading wasm in electron by falling back to XHR (#12921)
fixes mono/mono#20592 should fix #11671
1 parent af9a397 commit 81ae876

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ a license to everyone to use it as detailed in LICENSE.)
525525
* Jean-Sébastien Nadeau <[email protected]> (copyright owned by Foundry Interactive Inc.)
526526
* Wouter van Oortmerssen <[email protected]> (copyright owned by Google, LLC)
527527
* Alexey Sokolov <[email protected]> (copyright owned by Google, LLC)
528+
* Lukas Rieger <[email protected]>
528529
* Ivan Romanovski <[email protected]>
529530
* Max Brunsfeld <[email protected]>
530531
* Basil Fierz <[email protected]>

src/preamble.js

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -767,23 +767,38 @@ function getBinary(file) {
767767
}
768768

769769
function getBinaryPromise() {
770-
// If we don't have the binary yet, and have the Fetch api, use that;
771-
// in some environments, like Electron's render process, Fetch api may be present, but have a different context than expected, let's only use it on the Web
772-
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === 'function'
770+
// If we don't have the binary yet, try to to load it asynchronously.
771+
// Fetch has some additional restrictions over XHR, like it can't be used on a file:// url.
772+
// See https://github.com/github/fetch/pull/92#issuecomment-140665932
773+
// Cordova or Electron apps are typically loaded from a file:// url.
774+
// So use fetch if it is available and the url is not a file, otherwise fall back to XHR.
775+
if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) {
776+
if (typeof fetch === 'function'
773777
#if ENVIRONMENT_MAY_BE_WEBVIEW
774-
// Let's not use fetch to get objects over file:// as it's most likely Cordova which doesn't support fetch for file://
775778
&& !isFileURI(wasmBinaryFile)
776779
#endif
777-
) {
778-
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
779-
if (!response['ok']) {
780-
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
780+
) {
781+
return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) {
782+
if (!response['ok']) {
783+
throw "failed to load wasm binary file at '" + wasmBinaryFile + "'";
784+
}
785+
return response['arrayBuffer']();
786+
}).catch(function () {
787+
return getBinary(wasmBinaryFile);
788+
});
789+
}
790+
#if ENVIRONMENT_MAY_BE_WEBVIEW
791+
else {
792+
if (readAsync) {
793+
// fetch is not available or url is file => try XHR (readAsync uses XHR internally)
794+
return new Promise(function(resolve, reject) {
795+
readAsync(wasmBinaryFile, function(response) { resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))) }, reject)
796+
});
781797
}
782-
return response['arrayBuffer']();
783-
}).catch(function () {
784-
return getBinary(wasmBinaryFile);
785-
});
798+
}
799+
#endif
786800
}
801+
787802
// Otherwise, getBinary should be able to get it synchronously
788803
return Promise.resolve().then(function() { return getBinary(wasmBinaryFile); });
789804
}

src/web_or_worker_shell_read.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7-
read_ = function shell_read(url) {
7+
read_ = function(url) {
88
#if SUPPORT_BASE64_EMBEDDING
99
try {
1010
#endif
@@ -24,7 +24,7 @@
2424
};
2525

2626
if (ENVIRONMENT_IS_WORKER) {
27-
readBinary = function readBinary(url) {
27+
readBinary = function(url) {
2828
#if SUPPORT_BASE64_EMBEDDING
2929
try {
3030
#endif
@@ -45,11 +45,11 @@
4545
};
4646
}
4747

48-
readAsync = function readAsync(url, onload, onerror) {
48+
readAsync = function(url, onload, onerror) {
4949
var xhr = new XMLHttpRequest();
5050
xhr.open('GET', url, true);
5151
xhr.responseType = 'arraybuffer';
52-
xhr.onload = function xhr_onload() {
52+
xhr.onload = function() {
5353
if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0
5454
onload(xhr.response);
5555
return;

tests/test_other.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8781,7 +8781,7 @@ def test(args):
87818781
# Changing this option to [] should decrease code size.
87828782
self.assertLess(changed, normal)
87838783
# Check an absolute code size as well, with some slack.
8784-
self.assertLess(abs(changed - 5627), 150)
8784+
self.assertLess(abs(changed - 5872), 150)
87858785

87868786
def test_llvm_includes(self):
87878787
create_test_file('atomics.c', '#include <stdatomic.h>')

0 commit comments

Comments
 (0)