@@ -18,6 +18,8 @@ namespace ts {
18
18
const names : string [ ] = [ ] ;
19
19
let nameToNameIndexMap : ESMap < string , number > | undefined ;
20
20
const mappingCharCodes : number [ ] = [ ] ;
21
+ // We will create a string from the char code buffer whenever it exceeds this length
22
+ const mappingCommitThreshold = 1000 ;
21
23
let mappings = "" ;
22
24
23
25
// Last recorded and encoded mappings
@@ -221,12 +223,14 @@ namespace ts {
221
223
// Line/Comma delimiters
222
224
if ( lastGeneratedLine < pendingGeneratedLine ) {
223
225
// Emit line delimiters
226
+ // This loop can potentially overflow the stack on the char code conversion if it were a single operation
224
227
do {
225
228
mappingCharCodes . push ( CharacterCodes . semicolon ) ; // ';'
226
229
lastGeneratedLine ++ ;
227
- lastGeneratedCharacter = 0 ;
228
230
}
229
231
while ( lastGeneratedLine < pendingGeneratedLine ) ;
232
+ // Only need to set this once
233
+ lastGeneratedCharacter = 0 ;
230
234
}
231
235
else {
232
236
Debug . assertEqual ( lastGeneratedLine , pendingGeneratedLine , "generatedLine cannot backtrack" ) ;
@@ -260,20 +264,26 @@ namespace ts {
260
264
}
261
265
}
262
266
267
+ if ( mappings . length > mappingCommitThreshold ) {
268
+ flushMappingBuffer ( ) ;
269
+ }
270
+
263
271
hasLast = true ;
264
272
exit ( ) ;
265
273
}
266
274
267
- function serializeMappings ( ) : void {
268
- for ( let i = 0 , len = mappingCharCodes . length ; i < len ; i += 1024 ) {
275
+ function flushMappingBuffer ( ) : void {
276
+ // If there are a very large number of skipped lines in the source mapping, this loop can iterate multiple times
277
+ // Otherwise it should always have 1 iteration
278
+ for ( let i = 0 , len = mappingCharCodes . length ; i < len ; i + 1024 ) {
269
279
mappings += String . fromCharCode . apply ( undefined , mappingCharCodes . slice ( i , i + 1024 ) ) ;
270
280
}
271
281
mappingCharCodes . length = 0 ;
272
282
}
273
283
274
284
function toJSON ( ) : RawSourceMap {
275
285
commitPendingMapping ( ) ;
276
- serializeMappings ( ) ;
286
+ flushMappingBuffer ( ) ;
277
287
return {
278
288
version : 3 ,
279
289
file,
0 commit comments