diff --git a/AUTHORS b/AUTHORS index ed2988d66e4a7..076b8343a52fa 100644 --- a/AUTHORS +++ b/AUTHORS @@ -525,6 +525,7 @@ a license to everyone to use it as detailed in LICENSE.) * Jean-Sébastien Nadeau (copyright owned by Foundry Interactive Inc.) * Wouter van Oortmerssen (copyright owned by Google, LLC) * Alexey Sokolov (copyright owned by Google, LLC) +* Lukas Rieger * Ivan Romanovski * Max Brunsfeld * Basil Fierz diff --git a/src/preamble.js b/src/preamble.js index 260d568f6af78..36ebb3d59da7e 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -767,23 +767,38 @@ function getBinary(file) { } function getBinaryPromise() { - // If we don't have the binary yet, and have the Fetch api, use that; - // 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 - if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) && typeof fetch === 'function' + // If we don't have the binary yet, try to to load it asynchronously. + // Fetch has some additional restrictions over XHR, like it can't be used on a file:// url. + // See https://github.com/github/fetch/pull/92#issuecomment-140665932 + // Cordova or Electron apps are typically loaded from a file:// url. + // So use fetch if it is available and the url is not a file, otherwise fall back to XHR. + if (!wasmBinary && (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER)) { + if (typeof fetch === 'function' #if ENVIRONMENT_MAY_BE_WEBVIEW - // Let's not use fetch to get objects over file:// as it's most likely Cordova which doesn't support fetch for file:// && !isFileURI(wasmBinaryFile) #endif - ) { - return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) { - if (!response['ok']) { - throw "failed to load wasm binary file at '" + wasmBinaryFile + "'"; + ) { + return fetch(wasmBinaryFile, { credentials: 'same-origin' }).then(function(response) { + if (!response['ok']) { + throw "failed to load wasm binary file at '" + wasmBinaryFile + "'"; + } + return response['arrayBuffer'](); + }).catch(function () { + return getBinary(wasmBinaryFile); + }); + } +#if ENVIRONMENT_MAY_BE_WEBVIEW + else { + if (readAsync) { + // fetch is not available or url is file => try XHR (readAsync uses XHR internally) + return new Promise(function(resolve, reject) { + readAsync(wasmBinaryFile, function(response) { resolve(new Uint8Array(/** @type{!ArrayBuffer} */(response))) }, reject) + }); } - return response['arrayBuffer'](); - }).catch(function () { - return getBinary(wasmBinaryFile); - }); + } +#endif } + // Otherwise, getBinary should be able to get it synchronously return Promise.resolve().then(function() { return getBinary(wasmBinaryFile); }); } diff --git a/src/web_or_worker_shell_read.js b/src/web_or_worker_shell_read.js index d9865d0e40837..6daf23067cd99 100644 --- a/src/web_or_worker_shell_read.js +++ b/src/web_or_worker_shell_read.js @@ -4,7 +4,7 @@ * SPDX-License-Identifier: MIT */ - read_ = function shell_read(url) { + read_ = function(url) { #if SUPPORT_BASE64_EMBEDDING try { #endif @@ -24,7 +24,7 @@ }; if (ENVIRONMENT_IS_WORKER) { - readBinary = function readBinary(url) { + readBinary = function(url) { #if SUPPORT_BASE64_EMBEDDING try { #endif @@ -45,11 +45,11 @@ }; } - readAsync = function readAsync(url, onload, onerror) { + readAsync = function(url, onload, onerror) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; - xhr.onload = function xhr_onload() { + xhr.onload = function() { if (xhr.status == 200 || (xhr.status == 0 && xhr.response)) { // file URLs can return 0 onload(xhr.response); return; diff --git a/tests/test_other.py b/tests/test_other.py index ecafba77a9530..1259cd00a48f0 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -8780,7 +8780,7 @@ def test(args): # Changing this option to [] should decrease code size. self.assertLess(changed, normal) # Check an absolute code size as well, with some slack. - self.assertLess(abs(changed - 5627), 150) + self.assertLess(abs(changed - 5872), 150) def test_llvm_includes(self): create_test_file('atomics.c', '#include ')