6
6
7
7
// runtime_strings.js: Strings related runtime functions that are part of both MINIMAL_RUNTIME and regular runtime.
8
8
9
- // Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns
10
- // a copy of that string as a Javascript String object.
11
-
12
- #if SHARED_MEMORY && TEXTDECODER
13
- /**
14
- * UTF8Decoder.decode may not work with a view of a SharedArrayBuffer, see
15
- * https://github.com/whatwg/encoding/issues/172
16
- * To avoid that, we wrap around it and add a copy into a normal ArrayBuffer,
17
- * which can still be much faster than creating a string character by
18
- * character.
19
- * @constructor
20
- */
21
- function TextDecoderWrapper ( encoding ) {
22
- var textDecoder = new TextDecoder ( encoding ) ;
23
- this . decode = ( data ) => {
24
- #if ASSERTIONS
25
- assert ( data instanceof Uint8Array ) ;
26
- #endif
27
- // While we compile with pthreads, this method can be called on side buffers
28
- // as well, such as the stdout buffer in the filesystem code. Only copy when
29
- // we have to.
30
- return textDecoder . decode ( data . buffer instanceof SharedArrayBuffer ? new Uint8Array ( data ) : data ) ;
31
- } ;
32
- }
33
- #endif
34
-
35
9
#if TEXTDECODER == 2
36
- var UTF8Decoder = new TextDecoder { { { SHARED_MEMORY ? 'Wrapper' : '' } } } ( 'utf8' ) ;
10
+ var UTF8Decoder = new TextDecoder ( 'utf8' ) ;
37
11
#elif TEXTDECODER == 1
38
- var UTF8Decoder = typeof TextDecoder != 'undefined' ? new TextDecoder { { { SHARED_MEMORY ? 'Wrapper' : '' } } } ( 'utf8' ) : undefined ;
12
+ var UTF8Decoder = typeof TextDecoder != 'undefined' ? new TextDecoder ( 'utf8' ) : undefined ;
39
13
#endif
40
14
15
+ // Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the given array that contains uint8 values, returns
16
+ // a copy of that string as a Javascript String object.
41
17
/**
42
18
* heapOrArray is either a regular array, or a JavaScript typed array view.
43
19
* @param {number } idx
@@ -58,11 +34,11 @@ function UTF8ArrayToString(heapOrArray, idx, maxBytesToRead) {
58
34
#endif // TEXTDECODER
59
35
60
36
#if TEXTDECODER == 2
61
- return UTF8Decoder . decode ( heapOrArray . buffer ? heapOrArray . subarray ( idx , endPtr ) : new Uint8Array ( heapOrArray . slice ( idx , endPtr ) ) ) ;
37
+ return UTF8Decoder . decode ( heapOrArray . buffer ? { { { getUnsharedTextDecoderView ( 'heapOrArray' , ' idx' , ' endPtr' ) } } } : new Uint8Array ( heapOrArray . slice ( idx , endPtr ) ) ) ;
62
38
#else // TEXTDECODER == 2
63
39
#if TEXTDECODER
64
40
if ( endPtr - idx > 16 && heapOrArray . buffer && UTF8Decoder ) {
65
- return UTF8Decoder . decode ( heapOrArray . subarray ( idx , endPtr ) ) ;
41
+ return UTF8Decoder . decode ( { { { getUnsharedTextDecoderView ( 'heapOrArray' , ' idx' , ' endPtr' ) } } } ) ;
66
42
} else {
67
43
#endif // TEXTDECODER
68
44
var str = '' ;
@@ -133,7 +109,7 @@ function UTF8ToString(ptr, maxBytesToRead) {
133
109
if ( ! ptr ) return '' ;
134
110
var maxPtr = ptr + maxBytesToRead ;
135
111
for ( var end = ptr ; ! ( end >= maxPtr ) && HEAPU8 [ end ] ; ) ++ end ;
136
- return UTF8Decoder . decode ( HEAPU8 . subarray ( ptr , end ) ) ;
112
+ return UTF8Decoder . decode ( { { { getUnsharedTextDecoderView ( 'HEAPU8' , ' ptr' , ' end' ) } } } ) ;
137
113
#else
138
114
return ptr ? UTF8ArrayToString ( HEAPU8 , ptr , maxBytesToRead ) : '' ;
139
115
#endif
0 commit comments