Skip to content

Commit 310eedf

Browse files
committed
Avoid repeating wasm data in SINGLE_FILE mode with closure. NFC
This change removes the `wasmBinaryFile` global in SINGLE_FILE mode. Instead the `wasmBinaryFile` is inlined into the `createWasm` function. As part of this change we now also use the same embedding method as is used in `MINIMAL_RUNTIME`, avoiding the need for the `tryParseAsDataURI` helper. Fixes: #23938
1 parent a14f471 commit 310eedf

7 files changed

+17
-49
lines changed

src/lib/libbase64.js

-20
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,4 @@ addToLibrary({
4343
}
4444
return output;
4545
},
46-
47-
// Prefix of data URIs emitted by SINGLE_FILE and related options.
48-
// Double quotes here needed, othersise jsifier will not include any in the
49-
// output.
50-
$dataURIPrefix: "'data:application/octet-stream;base64,'",
51-
52-
/**
53-
* Indicates whether filename is a base64 data URI.
54-
*/
55-
$isDataURI: (filename) => filename.startsWith(dataURIPrefix),
56-
57-
// If filename is a base64 data URI, parses and returns data (Buffer on node,
58-
// Uint8Array otherwise). If filename is not a base64 data URI, returns
59-
// undefined.
60-
$tryParseAsDataURI__deps: ['$base64Decode', '$isDataURI', '$dataURIPrefix'],
61-
$tryParseAsDataURI: (filename) => {
62-
if (isDataURI(filename)) {
63-
return base64Decode(filename.slice(dataURIPrefix.length));
64-
}
65-
},
6646
});

src/preamble.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -578,13 +578,12 @@ function resetPrototype(constructor, attrs) {
578578
#endif
579579

580580
#if !SOURCE_PHASE_IMPORTS
581-
#if SINGLE_FILE
582-
// In SINGLE_FILE mode the wasm binary is encoded inline here as a data: URL.
583-
var wasmBinaryFile = '{{{ WASM_BINARY_FILE }}}';
584-
#else
585581
var wasmBinaryFile;
586582

587583
function findWasmBinary() {
584+
#if SINGLE_FILE && WASM == 1 && !WASM2JS
585+
return base64Decode('<<< WASM_BINARY_DATA >>>');
586+
#else
588587
#if EXPORT_ES6 && !AUDIO_WORKLET
589588
if (Module['locateFile']) {
590589
#endif
@@ -599,18 +598,19 @@ function findWasmBinary() {
599598
// Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
600599
return new URL('{{{ WASM_BINARY_FILE }}}', import.meta.url).href;
601600
#endif
602-
}
603601
#endif
602+
}
604603

605604
function getBinarySync(file) {
605+
#if SINGLE_FILE && WASM == 1 && !WASM2JS
606+
if (ArrayBuffer.isView(file)) {
607+
return file;
608+
}
609+
#endif
610+
#if expectToReceiveOnModule('wasmBinary') || MAYBE_WASM2JS
606611
if (file == wasmBinaryFile && wasmBinary) {
607612
return new Uint8Array(wasmBinary);
608613
}
609-
#if SUPPORT_BASE64_EMBEDDING
610-
var binary = tryParseAsDataURI(file);
611-
if (binary) {
612-
return binary;
613-
}
614614
#endif
615615
if (readBinary) {
616616
return readBinary(file);
@@ -1024,9 +1024,7 @@ function getWasmImports() {
10241024
var exports = receiveInstantiationResult({instance, 'module':wasmModule});
10251025
return exports;
10261026
#else
1027-
#if !SINGLE_FILE
10281027
wasmBinaryFile ??= findWasmBinary();
1029-
#endif
10301028
#if WASM_ASYNC_COMPILATION
10311029
#if RUNTIME_DEBUG
10321030
dbg('asynchronously preparing wasm');
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3826
1+
3720
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
12217
1+
6824
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
50961
1+
50872

tools/emscripten.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,8 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
297297
# to use emscripten's wasm<->JS ABI
298298
# * Use the metadata to generate the JS glue that goes with the wasm
299299

300-
if settings.SINGLE_FILE:
301-
# placeholder strings for JS glue, to be replaced with subresource locations in do_binaryen
302-
settings.WASM_BINARY_FILE = '<<< WASM_BINARY_FILE >>>'
303-
else:
304-
# set file locations, so that JS glue can find what it needs
305-
settings.WASM_BINARY_FILE = js_manipulation.escape_for_js_string(os.path.basename(out_wasm))
300+
# set file locations, so that JS glue can find what it needs
301+
settings.WASM_BINARY_FILE = js_manipulation.escape_for_js_string(os.path.basename(out_wasm))
306302

307303
if finalize:
308304
metadata = finalize_wasm(in_wasm, out_wasm, js_syms)

tools/link.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1951,10 +1951,7 @@ def phase_emscript(in_wasm, wasm_target, js_syms, base_metadata):
19511951
# the module is already in JS format.
19521952
if settings.SINGLE_FILE and not settings.WASM2JS:
19531953
settings.SUPPORT_BASE64_EMBEDDING = 1
1954-
if settings.MINIMAL_RUNTIME:
1955-
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$base64Decode')
1956-
else:
1957-
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$tryParseAsDataURI')
1954+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$base64Decode')
19581955

19591956
if shared.SKIP_SUBPROCS:
19601957
return
@@ -2371,10 +2368,7 @@ def phase_binaryen(target, options, wasm_target):
23712368
if final_js and settings.SINGLE_FILE and not settings.WASM2JS:
23722369
js = read_file(final_js)
23732370

2374-
if settings.MINIMAL_RUNTIME:
2375-
js = do_replace(js, '<<< WASM_BINARY_DATA >>>', base64_encode(wasm_target))
2376-
else:
2377-
js = do_replace(js, '<<< WASM_BINARY_FILE >>>', get_subresource_location(wasm_target))
2371+
js = do_replace(js, '<<< WASM_BINARY_DATA >>>', base64_encode(wasm_target))
23782372
delete_file(wasm_target)
23792373
write_file(final_js, js)
23802374

0 commit comments

Comments
 (0)