Skip to content

Commit 8bfd11a

Browse files
buu700kripken
authored andcommitted
SINGLE_FILE + locateFile fix (#5952) (#5959)
* add isDataURI helper
1 parent 66bbf8f commit 8bfd11a

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

src/base64Utils.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
// public domain. It would be nice if you left this header intact.
77
// Base64 code from Tyler Akins -- http://rumkin.com
88

9-
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
10-
9+
/**
10+
* Decodes a base64 string.
11+
* @param {String} input The string to decode.
12+
*/
1113
var decodeBase64 = typeof atob === 'function' ? atob : function (input) {
12-
/**
13-
* Decodes a base64 string.
14-
* @param {String} input The string to decode.
15-
*/
14+
var keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
15+
1616
var output = '';
1717
var chr1, chr2, chr3;
1818
var enc1, enc2, enc3, enc4;
@@ -69,13 +69,7 @@ function intArrayFromBase64(s) {
6969
// If filename is a base64 data URI, parses and returns data (Buffer on node,
7070
// Uint8Array otherwise). If filename is not a base64 data URI, returns undefined.
7171
function tryParseAsDataURI(filename) {
72-
var dataURIPrefix = 'data:application/octet-stream;base64,';
73-
74-
if (!(
75-
String.prototype.startsWith ?
76-
filename.startsWith(dataURIPrefix) :
77-
filename.indexOf(dataURIPrefix) === 0
78-
)) {
72+
if (!isDataURI(filename)) {
7973
return;
8074
}
8175

src/postamble.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ if (memoryInitializer && !ENVIRONMENT_IS_PTHREAD) {
4040
#else
4141
if (memoryInitializer) {
4242
#endif
43-
if (typeof Module['locateFile'] === 'function') {
44-
memoryInitializer = Module['locateFile'](memoryInitializer);
45-
} else if (Module['memoryInitializerPrefixURL']) {
46-
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
43+
if (!isDataURI(memoryInitializer)) {
44+
if (typeof Module['locateFile'] === 'function') {
45+
memoryInitializer = Module['locateFile'](memoryInitializer);
46+
} else if (Module['memoryInitializerPrefixURL']) {
47+
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
48+
}
4749
}
4850
if (ENVIRONMENT_IS_NODE || ENVIRONMENT_IS_SHELL) {
4951
var data = Module['readBinary'](memoryInitializer);

src/preamble.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,17 @@ Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
19871987
var cyberDWARFFile = '{{{ BUNDLED_CD_DEBUG_FILE }}}';
19881988
#endif
19891989

1990+
// Prefix of data URIs emitted by SINGLE_FILE and related options.
1991+
var dataURIPrefix = 'data:application/octet-stream;base64,';
1992+
1993+
// Indicates whether filename is a base64 data URI.
1994+
function isDataURI(filename) {
1995+
return String.prototype.startsWith ?
1996+
filename.startsWith(dataURIPrefix) :
1997+
filename.indexOf(dataURIPrefix) === 0
1998+
;
1999+
}
2000+
19902001
#if BINARYEN
19912002
function integrateWasmJS() {
19922003
// wasm.js has several methods for creating the compiled code module here:
@@ -2009,9 +2020,15 @@ function integrateWasmJS() {
20092020
var asmjsCodeFile = '{{{ ASMJS_CODE_FILE }}}';
20102021

20112022
if (typeof Module['locateFile'] === 'function') {
2012-
wasmTextFile = Module['locateFile'](wasmTextFile);
2013-
wasmBinaryFile = Module['locateFile'](wasmBinaryFile);
2014-
asmjsCodeFile = Module['locateFile'](asmjsCodeFile);
2023+
if (!isDataURI(wasmTextFile)) {
2024+
wasmTextFile = Module['locateFile'](wasmTextFile);
2025+
}
2026+
if (!isDataURI(wasmBinaryFile)) {
2027+
wasmBinaryFile = Module['locateFile'](wasmBinaryFile);
2028+
}
2029+
if (!isDataURI(asmjsCodeFile)) {
2030+
asmjsCodeFile = Module['locateFile'](asmjsCodeFile);
2031+
}
20152032
}
20162033

20172034
// utilities
@@ -2257,7 +2274,7 @@ function integrateWasmJS() {
22572274
// Prefer streaming instantiation if available.
22582275
if (!Module['wasmBinary'] &&
22592276
typeof WebAssembly.instantiateStreaming === 'function' &&
2260-
wasmBinaryFile.indexOf('data:') !== 0 &&
2277+
!isDataURI(wasmBinaryFile) &&
22612278
typeof fetch === 'function') {
22622279
WebAssembly.instantiateStreaming(fetch(wasmBinaryFile, { credentials: 'same-origin' }), info)
22632280
.then(receiveInstantiatedSource)

tests/test_browser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,6 +3708,35 @@ def test_single_file_html(self):
37083708
self.btest('emscripten_main_loop_setimmediate.cpp', '1', args=['-s', 'SINGLE_FILE=1', '-s', 'WASM=1', '-s', "BINARYEN_METHOD='native-wasm'"], also_proxied=True)
37093709
assert os.path.exists('test.html') and not os.path.exists('test.js') and not os.path.exists('test.worker.js')
37103710

3711+
# Tests that SINGLE_FILE works as intended with locateFile
3712+
def test_single_file_locate_file(self):
3713+
open('src.cpp', 'w').write(self.with_report_result(open(path_from_root('tests', 'browser_test_hello_world.c')).read()))
3714+
3715+
for wasm_enabled in [True, False]:
3716+
args = [PYTHON, EMCC, 'src.cpp', '-o', 'test.js', '-s', 'SINGLE_FILE=1']
3717+
3718+
if wasm_enabled:
3719+
args += ['-s', 'WASM=1']
3720+
3721+
run_process(args)
3722+
3723+
open('test.html', 'w').write('''
3724+
<script>
3725+
var Module = {
3726+
locateFile: function (path) {
3727+
if (path.indexOf('data:') === 0) {
3728+
throw new Error('Unexpected data URI.');
3729+
}
3730+
3731+
return path;
3732+
}
3733+
};
3734+
</script>
3735+
<script src="test.js"></script>
3736+
''')
3737+
3738+
self.run_browser('test.html', None, '/report_result?0')
3739+
37113740
# Tests that SINGLE_FILE works as intended in a Worker in JS output
37123741
def test_single_file_worker_js(self):
37133742
open('src.cpp', 'w').write(self.with_report_result(open(path_from_root('tests', 'browser_test_hello_world.c')).read()))

0 commit comments

Comments
 (0)