From fe721184731499c0369f11e13612c318f1a57f33 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 5 Dec 2020 11:56:34 +0100 Subject: [PATCH 1/3] v8: refactor to use more primordials --- lib/internal/v8_prof_polyfill.js | 4 ++-- lib/v8.js | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/internal/v8_prof_polyfill.js b/lib/internal/v8_prof_polyfill.js index 5f5922c5386543..f266849628fbe8 100644 --- a/lib/internal/v8_prof_polyfill.js +++ b/lib/internal/v8_prof_polyfill.js @@ -120,8 +120,8 @@ function versionCheck(firstLine, expected) { // "v8-version,$major,$minor,$build,$patch[,$embedder],$candidate" // whereas process.versions.v8 is either "$major.$minor.$build-$embedder" or // "$major.$minor.$build.$patch-$embedder". - firstLine = firstLine.split(','); - const curVer = expected.split(/[.\-]/); + firstLine = StringPrototypeSplit(firstLine, ','); + const curVer = StringPrototypeSplit(expected, /[.\-]/); if (firstLine.length !== 6 && firstLine.length !== 7 || firstLine[0] !== 'v8-version') { return 'Unable to read v8-version from log file.'; diff --git a/lib/v8.js b/lib/v8.js index 2ecb4c06a1931e..4d26ddb85e1c4e 100644 --- a/lib/v8.js +++ b/lib/v8.js @@ -17,6 +17,8 @@ const { Array, ArrayBuffer, + ArrayPrototypeForEach, + ArrayPrototypePush, DataView, Error, Float32Array, @@ -198,13 +200,14 @@ const arrayBufferViewTypeToIndex = new SafeMap(); { const dummy = new ArrayBuffer(); - for (const [i, ctor] of arrayBufferViewTypes.entries()) { + ArrayPrototypeForEach(arrayBufferViewTypes, (ctor, i) => { const tag = ObjectPrototypeToString(new ctor(dummy)); arrayBufferViewTypeToIndex.set(tag, i); - } + }); } -const bufferConstructorIndex = arrayBufferViewTypes.push(FastBuffer) - 1; +const bufferConstructorIndex = + ArrayPrototypePush(arrayBufferViewTypes, FastBuffer) - 1; class DefaultSerializer extends Serializer { constructor() { From b30bb9163cfd26defd6f48c4f6e3c7e1c17526f5 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 15 Dec 2020 22:27:42 +0100 Subject: [PATCH 2/3] fixup! v8: refactor to use more primordials --- lib/internal/v8_prof_polyfill.js | 41 +++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/internal/v8_prof_polyfill.js b/lib/internal/v8_prof_polyfill.js index f266849628fbe8..c468dc60569187 100644 --- a/lib/internal/v8_prof_polyfill.js +++ b/lib/internal/v8_prof_polyfill.js @@ -25,7 +25,18 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -/* eslint-disable no-restricted-globals */ +const { + ArrayPrototypeJoin, + ArrayPrototypeMatch, + Error, + JSONStringify, + RegExpPrototypeTest, + StringPrototypeIndexOf, + StringPrototypeMatch, + StringPrototypeReplace, + StringPrototypeSlice, + StringPrototypeSplit, +} = primordials; module.exports = { versionCheck }; @@ -44,7 +55,7 @@ const os = { const arg = args[args.length - 1]; if (arg === '[vdso]' || arg === '[vsyscall]' || - /^[0-9a-f]+-[0-9a-f]+$/.test(arg)) { + RegExpPrototypeTest(/^[0-9a-f]+-[0-9a-f]+$/, arg)) { return ''; } } @@ -55,7 +66,8 @@ const os = { // exits when Xcode hasn't been properly installed or when its license // hasn't been accepted yet. Basically any mention of xcodebuild in // the output means the nm command is non-functional. - const match = out.match(/(?:^|\n)([^\n]*xcodebuild[^\n]*)(?:\n|$)/); + const match = StringPrototypeMatch( + out,/(?:^|\n)([^\n]*xcodebuild[^\n]*)(?:\n|$)/); if (match) throw new Error(match[1]); out = macCppfiltNm(out); } @@ -94,10 +106,10 @@ function peekline() { function readline() { while (true) { - const lineBreak = line.indexOf('\n'); + const lineBreak = StringPrototypeIndexOf(line, '\n'); if (lineBreak !== -1) { - const res = line.slice(0, lineBreak); - line = line.slice(lineBreak + 1); + const res = StringPrototypeSlice(line, 0, lineBreak); + line = StringPrototypeSlice(line, lineBreak + 1); return res; } const bytes = fs.readSync(fd, buf, 0, buf.length); @@ -108,7 +120,7 @@ function readline() { if (bytes === 0) { process.emitWarning(`Profile file ${logFile} is broken`, { code: 'BROKEN_PROFILE_FILE', - detail: `${JSON.stringify(line)} at the file end is broken` + detail: `${JSONStringify(line)} at the file end is broken` }); return ''; } @@ -136,26 +148,27 @@ function macCppfiltNm(out) { // Re-grouped copy-paste from `tickprocessor.js` const FUNC_RE = /^([0-9a-fA-F]{8,16} [iItT] )(.*)$/gm; const CLEAN_RE = /^[0-9a-fA-F]{8,16} [iItT] /; - let entries = out.match(FUNC_RE); + let entries = StringPrototypeMatch(out, FUNC_RE); if (entries === null) return out; - entries = entries.map((entry) => { - return entry.replace(CLEAN_RE, '') - }); + entries = ArrayPrototypeMatch( + entries, + (entry) => StringPrototypeReplace(entry, CLEAN_RE, '') + ); let filtered; try { filtered = cp.spawnSync('c++filt', [ '-p' , '-i' ], { - input: entries.join('\n') + input: ArrayPrototypeJoin(entries, '\n') }).stdout.toString(); } catch { return out; } let i = 0; - filtered = filtered.split('\n'); - return out.replace(FUNC_RE, (all, prefix, postfix) => { + filtered = StringPrototypeSplit(filtered, '\n'); + return StringPrototypeReplace(out, FUNC_RE, (all, prefix, postfix) => { return prefix + (filtered[i++] || postfix); }); } From 4f708e1d438ed3ac8b4bfd5444080527078633c0 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 16 Dec 2020 00:21:44 +0100 Subject: [PATCH 3/3] fixup! v8: refactor to use more primordials --- lib/internal/v8_prof_polyfill.js | 45 ++++++++++++-------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/internal/v8_prof_polyfill.js b/lib/internal/v8_prof_polyfill.js index c468dc60569187..5f5922c5386543 100644 --- a/lib/internal/v8_prof_polyfill.js +++ b/lib/internal/v8_prof_polyfill.js @@ -25,18 +25,7 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -const { - ArrayPrototypeJoin, - ArrayPrototypeMatch, - Error, - JSONStringify, - RegExpPrototypeTest, - StringPrototypeIndexOf, - StringPrototypeMatch, - StringPrototypeReplace, - StringPrototypeSlice, - StringPrototypeSplit, -} = primordials; +/* eslint-disable no-restricted-globals */ module.exports = { versionCheck }; @@ -55,7 +44,7 @@ const os = { const arg = args[args.length - 1]; if (arg === '[vdso]' || arg === '[vsyscall]' || - RegExpPrototypeTest(/^[0-9a-f]+-[0-9a-f]+$/, arg)) { + /^[0-9a-f]+-[0-9a-f]+$/.test(arg)) { return ''; } } @@ -66,8 +55,7 @@ const os = { // exits when Xcode hasn't been properly installed or when its license // hasn't been accepted yet. Basically any mention of xcodebuild in // the output means the nm command is non-functional. - const match = StringPrototypeMatch( - out,/(?:^|\n)([^\n]*xcodebuild[^\n]*)(?:\n|$)/); + const match = out.match(/(?:^|\n)([^\n]*xcodebuild[^\n]*)(?:\n|$)/); if (match) throw new Error(match[1]); out = macCppfiltNm(out); } @@ -106,10 +94,10 @@ function peekline() { function readline() { while (true) { - const lineBreak = StringPrototypeIndexOf(line, '\n'); + const lineBreak = line.indexOf('\n'); if (lineBreak !== -1) { - const res = StringPrototypeSlice(line, 0, lineBreak); - line = StringPrototypeSlice(line, lineBreak + 1); + const res = line.slice(0, lineBreak); + line = line.slice(lineBreak + 1); return res; } const bytes = fs.readSync(fd, buf, 0, buf.length); @@ -120,7 +108,7 @@ function readline() { if (bytes === 0) { process.emitWarning(`Profile file ${logFile} is broken`, { code: 'BROKEN_PROFILE_FILE', - detail: `${JSONStringify(line)} at the file end is broken` + detail: `${JSON.stringify(line)} at the file end is broken` }); return ''; } @@ -132,8 +120,8 @@ function versionCheck(firstLine, expected) { // "v8-version,$major,$minor,$build,$patch[,$embedder],$candidate" // whereas process.versions.v8 is either "$major.$minor.$build-$embedder" or // "$major.$minor.$build.$patch-$embedder". - firstLine = StringPrototypeSplit(firstLine, ','); - const curVer = StringPrototypeSplit(expected, /[.\-]/); + firstLine = firstLine.split(','); + const curVer = expected.split(/[.\-]/); if (firstLine.length !== 6 && firstLine.length !== 7 || firstLine[0] !== 'v8-version') { return 'Unable to read v8-version from log file.'; @@ -148,27 +136,26 @@ function macCppfiltNm(out) { // Re-grouped copy-paste from `tickprocessor.js` const FUNC_RE = /^([0-9a-fA-F]{8,16} [iItT] )(.*)$/gm; const CLEAN_RE = /^[0-9a-fA-F]{8,16} [iItT] /; - let entries = StringPrototypeMatch(out, FUNC_RE); + let entries = out.match(FUNC_RE); if (entries === null) return out; - entries = ArrayPrototypeMatch( - entries, - (entry) => StringPrototypeReplace(entry, CLEAN_RE, '') - ); + entries = entries.map((entry) => { + return entry.replace(CLEAN_RE, '') + }); let filtered; try { filtered = cp.spawnSync('c++filt', [ '-p' , '-i' ], { - input: ArrayPrototypeJoin(entries, '\n') + input: entries.join('\n') }).stdout.toString(); } catch { return out; } let i = 0; - filtered = StringPrototypeSplit(filtered, '\n'); - return StringPrototypeReplace(out, FUNC_RE, (all, prefix, postfix) => { + filtered = filtered.split('\n'); + return out.replace(FUNC_RE, (all, prefix, postfix) => { return prefix + (filtered[i++] || postfix); }); }