Skip to content

Commit e18a140

Browse files
legendecasaduh95
authored andcommitted
lib: move source map VLQ decoding into its own module
Signed-off-by: Chengzhong Wu <cwu631@bloomberg.net> PR-URL: #67594 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fc28b70 commit e18a140

2 files changed

Lines changed: 131 additions & 82 deletions

File tree

‎lib/internal/source_map/source_map.js‎

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const {
7575
MathMax,
7676
ObjectFreeze,
7777
ObjectPrototypeHasOwnProperty,
78-
StringPrototypeCharAt,
7978
Symbol,
8079
TypedArrayPrototypeSet,
8180
TypedArrayPrototypeSlice,
@@ -86,12 +85,10 @@ const {
8685
validateInt32,
8786
validateObject,
8887
} = require('internal/validators');
89-
90-
let base64Map;
91-
92-
const VLQ_BASE_SHIFT = 5;
93-
const VLQ_BASE_MASK = (1 << 5) - 1;
94-
const VLQ_CONTINUATION_MASK = 1 << 5;
88+
const {
89+
StringCharIterator,
90+
decodeSignedVLQ,
91+
} = require('internal/source_map/vlq');
9592

9693
const kFirstEntry = Symbol('kFirstEntry');
9794

@@ -106,37 +103,6 @@ const kNoSource = -1;
106103
const kUnknownSource = -2;
107104
const kNoName = -1;
108105

109-
class StringCharIterator {
110-
/**
111-
* @param {string} string
112-
*/
113-
constructor(string) {
114-
this._string = string;
115-
this._position = 0;
116-
}
117-
118-
/**
119-
* @returns {string}
120-
*/
121-
next() {
122-
return StringPrototypeCharAt(this._string, this._position++);
123-
}
124-
125-
/**
126-
* @returns {string}
127-
*/
128-
peek() {
129-
return StringPrototypeCharAt(this._string, this._position);
130-
}
131-
132-
/**
133-
* @returns {boolean}
134-
*/
135-
hasNext() {
136-
return this._position < this._string.length;
137-
}
138-
}
139-
140106
/**
141107
* @class
142108
* Implements Source Map V3 model.
@@ -162,13 +128,6 @@ class SourceMap {
162128
* @param {SourceMapV3} payload
163129
*/
164130
constructor(payload, { lineLengths } = { __proto__: null }) {
165-
if (!base64Map) {
166-
const base64Digits =
167-
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
168-
base64Map = {};
169-
for (let i = 0; i < base64Digits.length; ++i)
170-
base64Map[base64Digits[i]] = i;
171-
}
172131
this.#payload = cloneSourceMapV3(payload);
173132
this.#parseMappingPayload();
174133
if (ArrayIsArray(lineLengths) && lineLengths.length) {
@@ -431,21 +390,21 @@ class SourceMap {
431390
break;
432391
}
433392

434-
columnNumber += decodeVLQ(stringCharIterator);
393+
columnNumber += decodeSignedVLQ(stringCharIterator);
435394
if (!stringCharIterator.hasNext() ||
436395
isSeparator(stringCharIterator.peek())) {
437396
this.#pushEntry(lineNumber, columnNumber, kNoSource, 0, 0, kNoName);
438397
continue;
439398
}
440399

441-
sourceIndex += decodeVLQ(stringCharIterator);
442-
sourceLineNumber += decodeVLQ(stringCharIterator);
443-
sourceColumnNumber += decodeVLQ(stringCharIterator);
400+
sourceIndex += decodeSignedVLQ(stringCharIterator);
401+
sourceLineNumber += decodeSignedVLQ(stringCharIterator);
402+
sourceColumnNumber += decodeSignedVLQ(stringCharIterator);
444403

445404
let entryNameIndex = kNoName;
446405
if (stringCharIterator.hasNext() &&
447406
!isSeparator(stringCharIterator.peek())) {
448-
nameIndex += decodeVLQ(stringCharIterator);
407+
nameIndex += decodeSignedVLQ(stringCharIterator);
449408
if (nameIndex >= 0 && nameIndex < names.length) {
450409
entryNameIndex = nameBase + nameIndex;
451410
}
@@ -488,38 +447,6 @@ function isSeparator(char) {
488447
return char === ',' || char === ';';
489448
}
490449

491-
/**
492-
* @param {SourceMap.StringCharIterator} stringCharIterator
493-
* @returns {number}
494-
*/
495-
function decodeVLQ(stringCharIterator) {
496-
// Read unsigned value.
497-
let result = 0;
498-
let shift = 0;
499-
let digit;
500-
do {
501-
digit = base64Map[stringCharIterator.next()];
502-
result += (digit & VLQ_BASE_MASK) << shift;
503-
shift += VLQ_BASE_SHIFT;
504-
} while (digit & VLQ_CONTINUATION_MASK);
505-
506-
// Fix the sign.
507-
const negative = result & 1;
508-
// Use unsigned right shift, so that the 32nd bit is properly shifted to the
509-
// 31st, and the 32nd becomes unset.
510-
result >>>= 1;
511-
if (!negative) {
512-
return result;
513-
}
514-
515-
// We need to OR here to ensure the 32nd bit (the sign bit in an Int32) is
516-
// always set for negative numbers. If `result` were 1, (meaning `negate` is
517-
// true and all other bits were zeros), `result` would now be 0. But -0
518-
// doesn't flip the 32nd bit as intended. All other numbers will successfully
519-
// set the 32nd bit without issue, so doing this is a noop for them.
520-
return -result | (1 << 31);
521-
}
522-
523450
/**
524451
* @param {SourceMapV3} payload
525452
* @returns {SourceMapV3}

‎lib/internal/source_map/vlq.js‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'use strict';
2+
3+
const {
4+
Int8Array,
5+
StringPrototypeCharAt,
6+
StringPrototypeCharCodeAt,
7+
TypedArrayPrototypeFill,
8+
} = primordials;
9+
10+
// The base64 digit value of each char code below 128, or -1.
11+
const kDigitValues = TypedArrayPrototypeFill(new Int8Array(128), -1);
12+
{
13+
const digits =
14+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
15+
for (let i = 0; i < digits.length; ++i) {
16+
kDigitValues[StringPrototypeCharCodeAt(digits, i)] = i;
17+
}
18+
}
19+
20+
class StringCharIterator {
21+
/**
22+
* @param {string} string
23+
*/
24+
constructor(string) {
25+
this._string = string;
26+
this._position = 0;
27+
}
28+
29+
/**
30+
* @returns {string}
31+
*/
32+
next() {
33+
return StringPrototypeCharAt(this._string, this._position++);
34+
}
35+
36+
/**
37+
* @returns {string}
38+
*/
39+
peek() {
40+
return StringPrototypeCharAt(this._string, this._position);
41+
}
42+
43+
/**
44+
* @returns {boolean}
45+
*/
46+
hasNext() {
47+
return this._position < this._string.length;
48+
}
49+
}
50+
51+
/**
52+
* VLQUnsignedValue ( ): an non-negative integer
53+
* @see https://tc39.es/ecma426/#sec-VLQUnsignedValue
54+
* @param {StringCharIterator} iterator positioned at a Vlq, and moved past it
55+
* @returns {number} the VLQUnsignedValue of the Vlq, or -1 if the iterator
56+
* isn't at a Vlq or the value is out of range
57+
*/
58+
function decodeUnsignedVLQ(iterator) {
59+
const string = iterator._string;
60+
let position = iterator._position;
61+
let result = 0;
62+
let shift = 0;
63+
let digit;
64+
do {
65+
// Past the end of the string, the char code is NaN.
66+
const code = StringPrototypeCharCodeAt(string, position++);
67+
digit = code < 128 ? kDigitValues[code] : -1;
68+
if (digit === -1) {
69+
iterator._position = position;
70+
return -1;
71+
}
72+
const bits = digit & 0x1f;
73+
// Vlq :: VlqDigitList
74+
// 2. If _value_ is >= 2<sup>32</sup>, throw an error.
75+
if (shift >= 30 && (shift > 30 || bits > 3)) {
76+
iterator._position = position;
77+
return -1;
78+
}
79+
// VlqDigitList :: ContinuationDigit VlqDigitList
80+
// 3. Return _left_ + _right_ * 2<sup>5</sup>.
81+
result |= bits << shift;
82+
shift += 5;
83+
} while (digit & 0x20);
84+
iterator._position = position;
85+
return result >>> 0;
86+
}
87+
88+
/**
89+
* VLQSignedValue ( ): an integer
90+
* @see https://tc39.es/ecma426/#sec-VLQSignedValue
91+
* @param {number} unsigned the VLQUnsignedValue of the VlqDigitList
92+
* @returns {number}
93+
*/
94+
function toSigned(unsigned) {
95+
// 2. If _unsigned_ modulo 2 = 1, let _sign_ be -1.
96+
// 3. Else, let _sign_ be 1.
97+
// 4. Let _value_ be floor(_unsigned_ / 2).
98+
const value = unsigned >>> 1;
99+
if ((unsigned & 1) === 0) return value;
100+
// 5. If _value_ is 0 and _sign_ is -1, return -2<sup>31</sup>.
101+
// 7. Return _sign_ * _value_.
102+
return value === 0 ? -2147483648 : -value;
103+
}
104+
105+
/**
106+
* VLQSignedValue ( ): an integer
107+
* @see https://tc39.es/ecma426/#sec-VLQSignedValue
108+
* @param {StringCharIterator} iterator positioned at a Vlq, and moved past it
109+
* @returns {number} the VLQSignedValue of the Vlq, or 0 if the iterator isn't
110+
* at a Vlq or the value is out of range
111+
*/
112+
function decodeSignedVLQ(iterator) {
113+
const unsigned = decodeUnsignedVLQ(iterator);
114+
return unsigned === -1 ? 0 : toSigned(unsigned);
115+
}
116+
117+
module.exports = {
118+
StringCharIterator,
119+
decodeSignedVLQ,
120+
decodeUnsignedVLQ,
121+
toSigned,
122+
};

0 commit comments

Comments
 (0)