Commit b11733a3515 for nodejs
commit b11733a35152161f8ebb8be6835fadcaf444c9fe
Author: Chengzhong Wu <legendecas@gmail.com>
Date: Wed Nov 18 12:17:57 2026 -0500
lib: move source map VLQ decoding into its own module
Signed-off-by: Chengzhong Wu <cwu631@bloomberg.net>
PR-URL: https://github.com/nodejs/node/pull/67594
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/lib/internal/source_map/source_map.js b/lib/internal/source_map/source_map.js
index d1d801ce583..f9eaf6c22ff 100644
--- a/lib/internal/source_map/source_map.js
+++ b/lib/internal/source_map/source_map.js
@@ -75,7 +75,6 @@ const {
MathMax,
ObjectFreeze,
ObjectPrototypeHasOwnProperty,
- StringPrototypeCharAt,
Symbol,
TypedArrayPrototypeSet,
TypedArrayPrototypeSlice,
@@ -86,12 +85,10 @@ const {
validateInt32,
validateObject,
} = require('internal/validators');
-
-let base64Map;
-
-const VLQ_BASE_SHIFT = 5;
-const VLQ_BASE_MASK = (1 << 5) - 1;
-const VLQ_CONTINUATION_MASK = 1 << 5;
+const {
+ StringCharIterator,
+ decodeSignedVLQ,
+} = require('internal/source_map/vlq');
const kFirstEntry = Symbol('kFirstEntry');
@@ -106,37 +103,6 @@ const kNoSource = -1;
const kUnknownSource = -2;
const kNoName = -1;
-class StringCharIterator {
- /**
- * @param {string} string
- */
- constructor(string) {
- this._string = string;
- this._position = 0;
- }
-
- /**
- * @returns {string}
- */
- next() {
- return StringPrototypeCharAt(this._string, this._position++);
- }
-
- /**
- * @returns {string}
- */
- peek() {
- return StringPrototypeCharAt(this._string, this._position);
- }
-
- /**
- * @returns {boolean}
- */
- hasNext() {
- return this._position < this._string.length;
- }
-}
-
/**
* @class
* Implements Source Map V3 model.
@@ -162,13 +128,6 @@ class SourceMap {
* @param {SourceMapV3} payload
*/
constructor(payload, { lineLengths } = { __proto__: null }) {
- if (!base64Map) {
- const base64Digits =
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
- base64Map = {};
- for (let i = 0; i < base64Digits.length; ++i)
- base64Map[base64Digits[i]] = i;
- }
this.#payload = cloneSourceMapV3(payload);
this.#parseMappingPayload();
if (ArrayIsArray(lineLengths) && lineLengths.length) {
@@ -431,21 +390,21 @@ class SourceMap {
break;
}
- columnNumber += decodeVLQ(stringCharIterator);
+ columnNumber += decodeSignedVLQ(stringCharIterator);
if (!stringCharIterator.hasNext() ||
isSeparator(stringCharIterator.peek())) {
this.#pushEntry(lineNumber, columnNumber, kNoSource, 0, 0, kNoName);
continue;
}
- sourceIndex += decodeVLQ(stringCharIterator);
- sourceLineNumber += decodeVLQ(stringCharIterator);
- sourceColumnNumber += decodeVLQ(stringCharIterator);
+ sourceIndex += decodeSignedVLQ(stringCharIterator);
+ sourceLineNumber += decodeSignedVLQ(stringCharIterator);
+ sourceColumnNumber += decodeSignedVLQ(stringCharIterator);
let entryNameIndex = kNoName;
if (stringCharIterator.hasNext() &&
!isSeparator(stringCharIterator.peek())) {
- nameIndex += decodeVLQ(stringCharIterator);
+ nameIndex += decodeSignedVLQ(stringCharIterator);
if (nameIndex >= 0 && nameIndex < names.length) {
entryNameIndex = nameBase + nameIndex;
}
@@ -488,38 +447,6 @@ function isSeparator(char) {
return char === ',' || char === ';';
}
-/**
- * @param {SourceMap.StringCharIterator} stringCharIterator
- * @returns {number}
- */
-function decodeVLQ(stringCharIterator) {
- // Read unsigned value.
- let result = 0;
- let shift = 0;
- let digit;
- do {
- digit = base64Map[stringCharIterator.next()];
- result += (digit & VLQ_BASE_MASK) << shift;
- shift += VLQ_BASE_SHIFT;
- } while (digit & VLQ_CONTINUATION_MASK);
-
- // Fix the sign.
- const negative = result & 1;
- // Use unsigned right shift, so that the 32nd bit is properly shifted to the
- // 31st, and the 32nd becomes unset.
- result >>>= 1;
- if (!negative) {
- return result;
- }
-
- // We need to OR here to ensure the 32nd bit (the sign bit in an Int32) is
- // always set for negative numbers. If `result` were 1, (meaning `negate` is
- // true and all other bits were zeros), `result` would now be 0. But -0
- // doesn't flip the 32nd bit as intended. All other numbers will successfully
- // set the 32nd bit without issue, so doing this is a noop for them.
- return -result | (1 << 31);
-}
-
/**
* @param {SourceMapV3} payload
* @returns {SourceMapV3}
diff --git a/lib/internal/source_map/vlq.js b/lib/internal/source_map/vlq.js
new file mode 100644
index 00000000000..467733b2631
--- /dev/null
+++ b/lib/internal/source_map/vlq.js
@@ -0,0 +1,122 @@
+'use strict';
+
+const {
+ Int8Array,
+ StringPrototypeCharAt,
+ StringPrototypeCharCodeAt,
+ TypedArrayPrototypeFill,
+} = primordials;
+
+// The base64 digit value of each char code below 128, or -1.
+const kDigitValues = TypedArrayPrototypeFill(new Int8Array(128), -1);
+{
+ const digits =
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
+ for (let i = 0; i < digits.length; ++i) {
+ kDigitValues[StringPrototypeCharCodeAt(digits, i)] = i;
+ }
+}
+
+class StringCharIterator {
+ /**
+ * @param {string} string
+ */
+ constructor(string) {
+ this._string = string;
+ this._position = 0;
+ }
+
+ /**
+ * @returns {string}
+ */
+ next() {
+ return StringPrototypeCharAt(this._string, this._position++);
+ }
+
+ /**
+ * @returns {string}
+ */
+ peek() {
+ return StringPrototypeCharAt(this._string, this._position);
+ }
+
+ /**
+ * @returns {boolean}
+ */
+ hasNext() {
+ return this._position < this._string.length;
+ }
+}
+
+/**
+ * VLQUnsignedValue ( ): an non-negative integer
+ * @see https://tc39.es/ecma426/#sec-VLQUnsignedValue
+ * @param {StringCharIterator} iterator positioned at a Vlq, and moved past it
+ * @returns {number} the VLQUnsignedValue of the Vlq, or -1 if the iterator
+ * isn't at a Vlq or the value is out of range
+ */
+function decodeUnsignedVLQ(iterator) {
+ const string = iterator._string;
+ let position = iterator._position;
+ let result = 0;
+ let shift = 0;
+ let digit;
+ do {
+ // Past the end of the string, the char code is NaN.
+ const code = StringPrototypeCharCodeAt(string, position++);
+ digit = code < 128 ? kDigitValues[code] : -1;
+ if (digit === -1) {
+ iterator._position = position;
+ return -1;
+ }
+ const bits = digit & 0x1f;
+ // Vlq :: VlqDigitList
+ // 2. If _value_ is >= 2<sup>32</sup>, throw an error.
+ if (shift >= 30 && (shift > 30 || bits > 3)) {
+ iterator._position = position;
+ return -1;
+ }
+ // VlqDigitList :: ContinuationDigit VlqDigitList
+ // 3. Return _left_ + _right_ * 2<sup>5</sup>.
+ result |= bits << shift;
+ shift += 5;
+ } while (digit & 0x20);
+ iterator._position = position;
+ return result >>> 0;
+}
+
+/**
+ * VLQSignedValue ( ): an integer
+ * @see https://tc39.es/ecma426/#sec-VLQSignedValue
+ * @param {number} unsigned the VLQUnsignedValue of the VlqDigitList
+ * @returns {number}
+ */
+function toSigned(unsigned) {
+ // 2. If _unsigned_ modulo 2 = 1, let _sign_ be -1.
+ // 3. Else, let _sign_ be 1.
+ // 4. Let _value_ be floor(_unsigned_ / 2).
+ const value = unsigned >>> 1;
+ if ((unsigned & 1) === 0) return value;
+ // 5. If _value_ is 0 and _sign_ is -1, return -2<sup>31</sup>.
+ // 7. Return _sign_ * _value_.
+ return value === 0 ? -2147483648 : -value;
+}
+
+/**
+ * VLQSignedValue ( ): an integer
+ * @see https://tc39.es/ecma426/#sec-VLQSignedValue
+ * @param {StringCharIterator} iterator positioned at a Vlq, and moved past it
+ * @returns {number} the VLQSignedValue of the Vlq, or 0 if the iterator isn't
+ * at a Vlq or the value is out of range
+ */
+function decodeSignedVLQ(iterator) {
+ const unsigned = decodeUnsignedVLQ(iterator);
+ return unsigned === -1 ? 0 : toSigned(unsigned);
+}
+
+module.exports = {
+ StringCharIterator,
+ decodeSignedVLQ,
+ decodeUnsignedVLQ,
+ toSigned,
+};