Skip to content

Commit 21e8ac0

Browse files
committed
Test normal char code array for source mappings
1 parent 96c48b7 commit 21e8ac0

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

src/compiler/sourcemap.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace ts {
1717

1818
const names: string[] = [];
1919
let nameToNameIndexMap: ESMap<string, number> | undefined;
20+
const mappingCharCodes: number[] = [];
2021
let mappings = "";
2122

2223
// Last recorded and encoded mappings
@@ -221,7 +222,7 @@ namespace ts {
221222
if (lastGeneratedLine < pendingGeneratedLine) {
222223
// Emit line delimiters
223224
do {
224-
mappings += ";";
225+
mappingCharCodes.push(CharacterCodes.semicolon); // ';'
225226
lastGeneratedLine++;
226227
lastGeneratedCharacter = 0;
227228
}
@@ -231,30 +232,30 @@ namespace ts {
231232
Debug.assertEqual(lastGeneratedLine, pendingGeneratedLine, "generatedLine cannot backtrack");
232233
// Emit comma to separate the entry
233234
if (hasLast) {
234-
mappings += ",";
235+
mappingCharCodes.push(CharacterCodes.comma); // ','
235236
}
236237
}
237238

238239
// 1. Relative generated character
239-
mappings += base64VLQFormatEncode(pendingGeneratedCharacter - lastGeneratedCharacter);
240+
appendBase64VLQ(pendingGeneratedCharacter - lastGeneratedCharacter);
240241
lastGeneratedCharacter = pendingGeneratedCharacter;
241242

242243
if (hasPendingSource) {
243244
// 2. Relative sourceIndex
244-
mappings += base64VLQFormatEncode(pendingSourceIndex - lastSourceIndex);
245+
appendBase64VLQ(pendingSourceIndex - lastSourceIndex);
245246
lastSourceIndex = pendingSourceIndex;
246247

247248
// 3. Relative source line
248-
mappings += base64VLQFormatEncode(pendingSourceLine - lastSourceLine);
249+
appendBase64VLQ(pendingSourceLine - lastSourceLine);
249250
lastSourceLine = pendingSourceLine;
250251

251252
// 4. Relative source character
252-
mappings += base64VLQFormatEncode(pendingSourceCharacter - lastSourceCharacter);
253+
appendBase64VLQ(pendingSourceCharacter - lastSourceCharacter);
253254
lastSourceCharacter = pendingSourceCharacter;
254255

255256
if (hasPendingName) {
256257
// 5. Relative nameIndex
257-
mappings += base64VLQFormatEncode(pendingNameIndex - lastNameIndex);
258+
appendBase64VLQ(pendingNameIndex - lastNameIndex);
258259
lastNameIndex = pendingNameIndex;
259260
}
260261
}
@@ -263,8 +264,16 @@ namespace ts {
263264
exit();
264265
}
265266

267+
function serializeMappings(): void {
268+
for (let i = 0, len = mappingCharCodes.length; i < len; i += 1024) {
269+
mappings += String.fromCharCode.apply(undefined, mappingCharCodes.slice(i, i + 1024));
270+
}
271+
mappingCharCodes.length = 0;
272+
}
273+
266274
function toJSON(): RawSourceMap {
267275
commitPendingMapping();
276+
serializeMappings();
268277
return {
269278
version: 3,
270279
file,
@@ -275,6 +284,31 @@ namespace ts {
275284
sourcesContent,
276285
};
277286
}
287+
288+
function appendBase64VLQ(inValue: number): void {
289+
// Add a new least significant bit that has the sign of the value.
290+
// if negative number the least significant bit that gets added to the number has value 1
291+
// else least significant bit value that gets added is 0
292+
// eg. -1 changes to binary : 01 [1] => 3
293+
// +1 changes to binary : 01 [0] => 2
294+
if (inValue < 0) {
295+
inValue = ((-inValue) << 1) + 1;
296+
}
297+
else {
298+
inValue = inValue << 1;
299+
}
300+
301+
// Encode 5 bits at a time starting from least significant bits
302+
do {
303+
let currentDigit = inValue & 31; // 11111
304+
inValue = inValue >> 5;
305+
if (inValue > 0) {
306+
// There are still more digits to decode, set the msb (6th bit)
307+
currentDigit = currentDigit | 32;
308+
}
309+
mappingCharCodes.push(base64FormatEncode(currentDigit));
310+
} while (inValue > 0);
311+
}
278312
}
279313

280314
// Sometimes tools can see the following line as a source mapping url comment, so we mangle it a bit (the [M])
@@ -544,34 +578,6 @@ namespace ts {
544578
-1;
545579
}
546580

547-
function base64VLQFormatEncode(inValue: number) {
548-
// Add a new least significant bit that has the sign of the value.
549-
// if negative number the least significant bit that gets added to the number has value 1
550-
// else least significant bit value that gets added is 0
551-
// eg. -1 changes to binary : 01 [1] => 3
552-
// +1 changes to binary : 01 [0] => 2
553-
if (inValue < 0) {
554-
inValue = ((-inValue) << 1) + 1;
555-
}
556-
else {
557-
inValue = inValue << 1;
558-
}
559-
560-
// Encode 5 bits at a time starting from least significant bits
561-
let encodedStr = "";
562-
do {
563-
let currentDigit = inValue & 31; // 11111
564-
inValue = inValue >> 5;
565-
if (inValue > 0) {
566-
// There are still more digits to decode, set the msb (6th bit)
567-
currentDigit = currentDigit | 32;
568-
}
569-
encodedStr = encodedStr + String.fromCharCode(base64FormatEncode(currentDigit));
570-
} while (inValue > 0);
571-
572-
return encodedStr;
573-
}
574-
575581
interface MappedPosition {
576582
generatedPosition: number;
577583
source: string | undefined;

0 commit comments

Comments
 (0)