Skip to content

Commit 61bd42b

Browse files
committed
Add a setting option -s TEXTDECODER=0/1 which can be used to control whether the TextDecoder API is used or not. Enabled by default.
1 parent 1d9f405 commit 61bd42b

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/preamble.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,11 @@ function stringToAscii(str, outPtr) {
515515
// Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns
516516
// a copy of that string as a Javascript String object.
517517

518+
#if TEXTDECODER
518519
var UTF8Decoder = typeof TextDecoder !== 'undefined' ? new TextDecoder('utf8') : undefined;
520+
#endif
519521
function UTF8ArrayToString(u8Array, idx) {
522+
#if TEXTDECODER
520523
var endPtr = idx;
521524
// TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself.
522525
// Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage.
@@ -525,6 +528,7 @@ function UTF8ArrayToString(u8Array, idx) {
525528
if (endPtr - idx > 16 && u8Array.subarray && UTF8Decoder) {
526529
return UTF8Decoder.decode(u8Array.subarray(idx, endPtr));
527530
} else {
531+
#endif
528532
var u0, u1, u2, u3, u4, u5;
529533

530534
var str = '';
@@ -559,7 +563,9 @@ function UTF8ArrayToString(u8Array, idx) {
559563
str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
560564
}
561565
}
566+
#if TEXTDECODER
562567
}
568+
#endif
563569
}
564570
{{{ maybeExport('UTF8ArrayToString') }}}
565571

@@ -684,6 +690,7 @@ function UTF16ToString(ptr) {
684690
#if ASSERTIONS
685691
assert(ptr % 2 == 0, 'Pointer passed to UTF16ToString must be aligned to two bytes!');
686692
#endif
693+
#if TEXTDECODER
687694
var endPtr = ptr;
688695
// TextDecoder needs to know the byte length in advance, it doesn't stop on null terminator by itself.
689696
// Also, use the length info to avoid running tiny strings through TextDecoder, since .subarray() allocates garbage.
@@ -694,6 +701,7 @@ function UTF16ToString(ptr) {
694701
if (endPtr - ptr > 32 && UTF16Decoder) {
695702
return UTF16Decoder.decode(HEAPU8.subarray(ptr, endPtr));
696703
} else {
704+
#endif
697705
var i = 0;
698706

699707
var str = '';
@@ -704,7 +712,9 @@ function UTF16ToString(ptr) {
704712
// fromCharCode constructs a character from a UTF-16 code unit, so we can pass the UTF16 string right through.
705713
str += String.fromCharCode(codeUnit);
706714
}
715+
#if TEXTDECODER
707716
}
717+
#endif
708718
}
709719
{{{ maybeExport('UTF16ToString') }}}
710720

src/settings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,4 +754,6 @@ var CYBERDWARF = 0; // see http://kripken.github.io/emscripten-site/docs/debuggi
754754

755755
var BUNDLED_CD_DEBUG_FILE = ""; // Path to the CyberDWARF debug file passed to the compiler
756756

757+
var TEXTDECODER = 1; // Is enabled, use the JavaScript TextDecoder API for string marshalling.
758+
// Enabled by default, set this to 0 to disable.
757759
// Reserved: variables containing POINTER_MASKING.

0 commit comments

Comments
 (0)