Skip to content

Commit 01df7f8

Browse files
committed
Add experimental supprot for the proposed JS base64 API
See https://github.com/tc39/proposal-arraybuffer-base64
1 parent 279f298 commit 01df7f8

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/lib/libbase64.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ addToLibrary({
2222
`,
2323
$base64Decode__docs: '/** @noinline */',
2424
$base64Decode: (b64) => {
25+
#if JS_BASE64_API
26+
return Uint8Array.fromBase64(b64);
27+
#else
2528
#if ENVIRONMENT_MAY_BE_NODE
2629
if (ENVIRONMENT_IS_NODE) {
2730
var buf = Buffer.from(b64, 'base64');
@@ -42,5 +45,6 @@ addToLibrary({
4245
output[j+2] = b2 << 6 | base64ReverseLookup[b64.charCodeAt(i+3)];
4346
}
4447
return output;
48+
#endif
4549
},
4650
});

src/settings.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,12 @@ var SOURCE_PHASE_IMPORTS = false;
21932193
// [link]
21942194
var WASM_ESM_INTEGRATION = false;
21952195

2196+
// Enable to use of the proposed arraybuffer-base64 API:
2197+
// https://github.com/tc39/proposal-arraybuffer-base64
2198+
// To run the resulting code currently requires passing `--js_base_64` to node
2199+
// or chrome.
2200+
var JS_BASE64_API = false;
2201+
21962202
// For renamed settings the format is:
21972203
// [OLD_NAME, NEW_NAME]
21982204
// For removed settings (which now effectively have a fixed value and can no

test/test_other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16017,3 +16017,12 @@ def test_locate_file_abspath_esm(self, args):
1601716017
output_suffix='.mjs',
1601816018
emcc_args=['--pre-js', 'pre.js',
1601916019
'--extern-post-js', test_file('modularize_post_js.js')] + args)
16020+
16021+
@requires_node_canary
16022+
def test_js_base64_api(self):
16023+
self.node_args += ['--js_base_64']
16024+
self.do_runf('hello_world.c', 'hello, world!', emcc_args=['-sSINGLE_FILE'], output_basename='baseline')
16025+
self.do_runf('hello_world.c', 'hello, world!', emcc_args=['-sSINGLE_FILE', '-sJS_BASE64_API', '-Wno-experimental'])
16026+
baseline_size = os.path.getsize('baseline.js')
16027+
js_api_size = os.path.getsize('hello_world.js')
16028+
self.assertLess(js_api_size, baseline_size)

tools/link.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,9 @@ def phase_linker_setup(options, linker_args): # noqa: C901, PLR0912, PLR0915
784784
if options.oformat == OFormat.MJS:
785785
default_setting('EXPORT_ES6', 1)
786786

787+
if settings.JS_BASE64_API:
788+
diagnostics.warning('experimental', '-sJS_BASE64_API is still experimental and not yet supported in browsers')
789+
787790
if settings.WASM_ESM_INTEGRATION:
788791
diagnostics.warning('experimental', '-sWASM_ESM_INTEGRATION is still experimental and not yet supported in browsers')
789792
default_setting('EXPORT_ES6', 1)

0 commit comments

Comments
 (0)