@@ -37,20 +37,26 @@ const ARRAY_SIZE = 16;
37
37
const BIGINT = typeof BigUint64Array !== "undefined" ;
38
38
const THIS = Symbol ( ) ;
39
39
40
- const STRING_CHUNKSIZE = 1024 ;
40
+ const STRING_SMALLSIZE = 192 ; // break-even point in V8
41
+ const STRING_CHUNKSIZE = 1024 ; // mitigate stack overflow
42
+ const utf16 = new TextDecoder ( "utf-16le" ) ; // != wtf16
41
43
42
44
/** Gets a string from memory. */
43
45
function getStringImpl ( buffer , ptr ) {
44
46
let len = new Uint32Array ( buffer ) [ ptr + SIZE_OFFSET >>> 2 ] >>> 1 ;
45
47
const wtf16 = new Uint16Array ( buffer , ptr , len ) ;
46
- if ( len <= STRING_CHUNKSIZE ) return String . fromCharCode . apply ( String , wtf16 ) ;
47
- let str = "" ;
48
- let off = 0 ;
49
- do {
50
- str += String . fromCharCode . apply ( String , wtf16 . subarray ( off , off += STRING_CHUNKSIZE ) ) ;
51
- len -= STRING_CHUNKSIZE ;
52
- } while ( len > STRING_CHUNKSIZE ) ;
53
- return str + String . fromCharCode . apply ( String , wtf16 . subarray ( off , off + len ) ) ;
48
+ if ( len <= STRING_SMALLSIZE ) return String . fromCharCode . apply ( String , wtf16 ) ;
49
+ try {
50
+ return utf16 . decode ( wtf16 , { fatal : true } ) ;
51
+ } catch {
52
+ let str = "" ;
53
+ let off = 0 ;
54
+ while ( len > STRING_CHUNKSIZE ) {
55
+ str += String . fromCharCode . apply ( String , wtf16 . subarray ( off , off += STRING_CHUNKSIZE ) ) ;
56
+ len -= STRING_CHUNKSIZE ;
57
+ }
58
+ return str + String . fromCharCode . apply ( String , wtf16 . subarray ( off , off + len ) ) ;
59
+ }
54
60
}
55
61
56
62
/** Prepares the base module prior to instantiation. */
0 commit comments