From e2b954b14960abe44c9f3387c1a601da6343d436 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 31 Dec 2020 13:32:35 +0100 Subject: [PATCH] lib: refactor source_map to use more primordials PR-URL: https://github.com/nodejs/node/pull/36733 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- lib/internal/source_map/source_map.js | 31 +++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/internal/source_map/source_map.js b/lib/internal/source_map/source_map.js index 44edbda04ab4d6..f49de5d8c4deda 100644 --- a/lib/internal/source_map/source_map.js +++ b/lib/internal/source_map/source_map.js @@ -67,7 +67,12 @@ 'use strict'; const { - ArrayIsArray + ArrayIsArray, + ArrayPrototypePush, + ArrayPrototypeSlice, + ArrayPrototypeSort, + ObjectPrototypeHasOwnProperty, + StringPrototypeCharAt, } = primordials; const { @@ -94,14 +99,14 @@ class StringCharIterator { * @return {string} */ next() { - return this._string.charAt(this._position++); + return StringPrototypeCharAt(this._string, this._position++); } /** * @return {string} */ peek() { - return this._string.charAt(this._position); + return StringPrototypeCharAt(this._string, this._position); } /** @@ -158,7 +163,7 @@ class SourceMap { } else { this.#parseMap(this.#payload, 0, 0); } - this.#mappings.sort(compareSourceMapEntry); + ArrayPrototypeSort(this.#mappings, compareSourceMapEntry); } /** @@ -211,7 +216,7 @@ class SourceMap { /** * @override */ - #parseMap = (map, lineNumber, columnNumber) => { + #parseMap(map, lineNumber, columnNumber) { let sourceIndex = 0; let sourceLineNumber = 0; let sourceColumnNumber = 0; @@ -222,7 +227,7 @@ class SourceMap { for (let i = 0; i < map.sources.length; ++i) { const url = map.sources[i]; originalToCanonicalURLMap[url] = url; - sources.push(url); + ArrayPrototypePush(sources, url); this.#sources[url] = true; if (map.sourcesContent && map.sourcesContent[i]) @@ -246,7 +251,7 @@ class SourceMap { columnNumber += decodeVLQ(stringCharIterator); if (isSeparator(stringCharIterator.peek())) { - this.#mappings.push([lineNumber, columnNumber]); + ArrayPrototypePush(this.#mappings, [lineNumber, columnNumber]); continue; } @@ -264,8 +269,11 @@ class SourceMap { name = map.names?.[nameIndex]; } - this.#mappings.push([lineNumber, columnNumber, sourceURL, - sourceLineNumber, sourceColumnNumber, name]); + ArrayPrototypePush( + this.#mappings, + [lineNumber, columnNumber, sourceURL, sourceLineNumber, + sourceColumnNumber, name] + ); } }; } @@ -320,8 +328,9 @@ function cloneSourceMapV3(payload) { } payload = { ...payload }; for (const key in payload) { - if (payload.hasOwnProperty(key) && ArrayIsArray(payload[key])) { - payload[key] = payload[key].slice(0); + if (ObjectPrototypeHasOwnProperty(payload, key) && + ArrayIsArray(payload[key])) { + payload[key] = ArrayPrototypeSlice(payload[key]); } } return payload;