Skip to content

Commit

Permalink
Simplify findWasmBinary() and reduce usage of isDataURI(). NFC (#23549)
Browse files Browse the repository at this point in the history
The `findWasmBinary()` function only exists when SINGLE_FILE is not
true.

Also, the only time `WASM_BINARY_FILE` can be a data URL is in
SINGLE_FILE mode.
  • Loading branch information
sbc100 authored Jan 31, 2025
1 parent b31dd92 commit 1a1c303
Show file tree
Hide file tree
Showing 88 changed files with 99 additions and 109 deletions.
8 changes: 8 additions & 0 deletions src/base64Decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ function base64Decode(b64) {
#endif // ~WASM2JS

#if !MINIMAL_RUNTIME
// Prefix of data URIs emitted by SINGLE_FILE and related options.
var dataURIPrefix = 'data:application/octet-stream;base64,';

/**
* Indicates whether filename is a base64 data URI.
*/
var isDataURI = (uri) => uri.startsWith(dataURIPrefix);

// If filename is a base64 data URI, parses and returns data (Buffer on node,
// Uint8Array otherwise). If filename is not a base64 data URI, returns undefined.
function tryParseAsDataURI(filename) {
Expand Down
26 changes: 5 additions & 21 deletions src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,6 @@ var runtimeInitialized = false;
var runtimeExited = false;
#endif

// Prefix of data URIs emitted by SINGLE_FILE and related options.
var dataURIPrefix = 'data:application/octet-stream;base64,';

/**
* Indicates whether filename is a base64 data URI.
* @noinline
*/
var isDataURI = (filename) => filename.startsWith(dataURIPrefix);

/**
* Indicates whether filename is delivered via file protocol (as opposed to http/https)
* @noinline
Expand Down Expand Up @@ -606,18 +597,13 @@ function findWasmBinary() {
#if EXPORT_ES6 && USE_ES6_IMPORT_META && !AUDIO_WORKLET
if (Module['locateFile']) {
#endif
var f = '{{{ WASM_BINARY_FILE }}}';
#if !SINGLE_FILE
if (!isDataURI(f)) {
return locateFile(f);
}
#endif
return f;
#if EXPORT_ES6 && USE_ES6_IMPORT_META && !AUDIO_WORKLET // In single-file mode, repeating WASM_BINARY_FILE would emit the contents again. For an Audio Worklet, we cannot use `new URL()`.
return locateFile('{{{ WASM_BINARY_FILE }}}');
#if EXPORT_ES6 && USE_ES6_IMPORT_META && !AUDIO_WORKLET // For an Audio Worklet, we cannot use `new URL()`.
}
#if ENVIRONMENT_MAY_BE_SHELL
if (ENVIRONMENT_IS_SHELL)
if (ENVIRONMENT_IS_SHELL) {
return '{{{ WASM_BINARY_FILE }}}';
}
#endif
// Use bundler-friendly `new URL(..., import.meta.url)` pattern; works in browsers too.
return new URL('{{{ WASM_BINARY_FILE }}}', import.meta.url).href;
Expand Down Expand Up @@ -800,9 +786,7 @@ async function instantiateArrayBuffer(binaryFile, imports) {

async function instantiateAsync(binary, binaryFile, imports) {
#if !SINGLE_FILE
if (!binary &&
typeof WebAssembly.instantiateStreaming == 'function' &&
!isDataURI(binaryFile)
if (!binary && typeof WebAssembly.instantiateStreaming == 'function'
#if ENVIRONMENT_MAY_BE_WEBVIEW
// Don't use streaming for file:// delivered objects in a webview, fetch them synchronously.
&& !isFileURI(binaryFile)
Expand Down
4 changes: 1 addition & 3 deletions src/source_map_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ WasmSourceMap.prototype.normalizeOffset = function (offset) {
}

var wasmSourceMapFile = '{{{ WASM_BINARY_FILE }}}.map';
if (!isDataURI(wasmSourceMapFile)) {
wasmSourceMapFile = locateFile(wasmSourceMapFile);
}
wasmSourceMapFile = locateFile(wasmSourceMapFile);

function getSourceMap() {
var buf = readBinary(wasmSourceMapFile);
Expand Down
2 changes: 1 addition & 1 deletion test/browser/test_small_js_flags.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4343
4248
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8357
8308
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20268
20171
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8341
8291
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20247
20150
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9363
9309
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24036
23939
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8305
8252
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20161
20063
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8305
8252
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20161
20063
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8365
8318
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20344
20247
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9368
9312
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24037
23939
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8357
8308
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20268
20171
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3564
3517
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7677
7580
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7658
7610
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18815
18718
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2798
2751
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5986
5890
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7880
7829
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20996
20888
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2659
2612
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6851
6744
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2320
2273
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4780
4684
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2279
2226
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4722
4626
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_Os.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2279
2226
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4722
4626
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_Oz.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2279
2226
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4722
4626
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5990
5945
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13242
13145
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1677
1638
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3633
3539
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2279
2226
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_hello_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4722
4626
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1871
1827
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3996
3902
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1914
1870
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4053
3959
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2326
2280
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4890
4794
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3_grow.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2473
2429
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3_grow.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5174
5078
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2178
2133
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4592
4496
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3_standalone.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2144
2099
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_mem_O3_standalone.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4522
4428
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1901
1856
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4052
3958
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1914
1870
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4053
3959
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1914
1870
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4053
3959
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_64.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1430
1389
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_64.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3004
2908
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O0.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6425
6381
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16914
16805
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1514
1470
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3529
3423
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1371
1329
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2752
2656
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O3.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1335
1294
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2701
2605
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Os.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1335
1294
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2701
2605
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Oz-ctors.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1331
1290
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Oz-ctors.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2695
2599
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Oz.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1335
1294
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2701
2605
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1533
1495
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_esm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3169
3074
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4195
4152
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_pthreads.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8732
8634
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1335
1294
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_minimal_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2701
2605
2 changes: 1 addition & 1 deletion test/other/test_INCOMING_MODULE_JS_API.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3736
3639
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
52784
52415
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
28532
28163
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51567
51198

0 comments on commit 1a1c303

Please sign in to comment.