Skip to content

Commit 51587b1

Browse files
authored
Remove duplicate base64 decoding code. NFC (emscripten-core#23321)
The optimized `base64Decode` was only being using by minimal runtime. After this change it used in the regular runtime too which should speed up decoding. The optimized version also avoids the dependency of `atob` which means we can also drop the atob polyfill.
1 parent 844e8ca commit 51587b1

File tree

12 files changed

+25
-111
lines changed

12 files changed

+25
-111
lines changed

eslint.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ export default [{
4848
'src/settings_internal.js',
4949
'src/arrayUtils.js',
5050
'src/deterministic.js',
51-
'src/base64Utils.js',
5251
'src/base64Decode.js',
5352
'src/proxyWorker.js',
5453
'src/proxyClient.js',

src/base64Decode.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function base64Decode(b64) {
3939
#if ENVIRONMENT_MAY_BE_NODE
4040
if (typeof ENVIRONMENT_IS_NODE != 'undefined' && ENVIRONMENT_IS_NODE) {
4141
var buf = Buffer.from(b64, 'base64');
42-
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
42+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.length);
4343
}
4444
#endif
4545

@@ -58,3 +58,15 @@ function base64Decode(b64) {
5858
}
5959

6060
#endif // ~WASM2JS
61+
62+
#if !MINIMAL_RUNTIME
63+
// If filename is a base64 data URI, parses and returns data (Buffer on node,
64+
// Uint8Array otherwise). If filename is not a base64 data URI, returns undefined.
65+
function tryParseAsDataURI(filename) {
66+
if (!isDataURI(filename)) {
67+
return;
68+
}
69+
70+
return base64Decode(filename.slice(dataURIPrefix.length));
71+
}
72+
#endif

src/base64Utils.js

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/modules.mjs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,6 @@ function exportRuntime() {
445445
runtimeElements.push('checkStackCookie');
446446
}
447447

448-
if (SUPPORT_BASE64_EMBEDDING) {
449-
runtimeElements.push('intArrayFromBase64');
450-
runtimeElements.push('tryParseAsDataURI');
451-
}
452-
453448
if (RETAIN_COMPILER_SETTINGS) {
454449
runtimeElements.push('getCompilerSetting');
455450
}

src/polyfill/atob.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/postamble_minimal.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ var imports = {
110110
#endif // MINIFY_WASM_IMPORTED_MODULES
111111
};
112112

113+
#if SINGLE_FILE && WASM == 1 && !WASM2JS
114+
Module['wasm'] = base64Decode('<<< WASM_BINARY_DATA >>>');
115+
#endif
116+
113117
#if MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION
114118
// https://caniuse.com/#feat=wasm and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiateStreaming
115119
// Firefox 52 added Wasm support, but only Firefox 58 added instantiateStreaming.

src/preamble.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ if (typeof WebAssembly != 'object') {
4444
}
4545
#endif
4646

47-
#if SUPPORT_BASE64_EMBEDDING || FORCE_FILESYSTEM
48-
#include "base64Utils.js"
49-
#endif
50-
5147
// Wasm globals
5248

5349
var wasmMemory;

src/preamble_minimal.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ if (Module['doWasm2JS']) {
3232
#endif
3333
#endif
3434

35-
#if SINGLE_FILE && WASM == 1 && !WASM2JS
36-
#include "base64Decode.js"
37-
Module['wasm'] = base64Decode('<<< WASM_BINARY_DATA >>>');
38-
#endif
39-
4035
var HEAP8, HEAP16, HEAP32, HEAPU8, HEAPU16, HEAPU32, HEAPF32, HEAPF64,
4136
#if WASM_BIGINT
4237
HEAP64, HEAPU64,

src/runtime_shared.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
#include "growableHeap.js"
1818
#endif
1919

20+
#if SUPPORT_BASE64_EMBEDDING
21+
#include "base64Decode.js"
22+
#endif
23+
2024
#if USE_ASAN
2125
#include "runtime_asan.js"
2226
#endif

src/settings_internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ var AUDIO_WORKLET_FILE = '';
140140
// Base URL the source mapfile, if relevant
141141
var SOURCE_MAP_BASE = '';
142142

143-
// If set to 1, src/base64Utils.js will be included in the bundle.
143+
// If set to 1 then base64 decoding functions will be included in the bundle.
144144
// This is set internally when needed (SINGLE_FILE)
145145
var SUPPORT_BASE64_EMBEDDING = false;
146146

0 commit comments

Comments
 (0)