From 9757b47c4463647cf466607132f2ecaa0f4f7afa Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 21 Oct 2020 11:34:41 +0200 Subject: [PATCH] console: use more primordials PR-URL: https://github.com/nodejs/node/pull/35734 Reviewed-By: Rich Trott --- lib/internal/console/constructor.js | 66 ++++++++++++++++++----------- lib/internal/console/global.js | 3 +- 2 files changed, 43 insertions(+), 26 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 8684bea5ad4966..dc2817ad2a54ec 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -6,21 +6,31 @@ const { ArrayFrom, ArrayIsArray, + ArrayPrototypePush, + ArrayPrototypeUnshift, Boolean, ErrorCaptureStackTrace, - Map, + FunctionPrototypeBind, MathFloor, Number, + NumberPrototypeToFixed, ObjectDefineProperties, ObjectDefineProperty, ObjectKeys, ObjectPrototypeHasOwnProperty, ObjectValues, ReflectOwnKeys, + SafeMap, + SafeWeakMap, + StringPrototypeIncludes, + StringPrototypePadStart, + StringPrototypeRepeat, + StringPrototypeReplace, + StringPrototypeSlice, + StringPrototypeSplit, Symbol, SymbolHasInstance, SymbolToStringTag, - WeakMap, } = primordials; const { trace } = internalBinding('trace_events'); @@ -80,7 +90,7 @@ const kBindStreamsLazy = Symbol('kBindStreamsLazy'); const kUseStdout = Symbol('kUseStdout'); const kUseStderr = Symbol('kUseStderr'); -const optionsMap = new WeakMap(); +const optionsMap = new SafeWeakMap(); function Console(options /* or: stdout, stderr, ignoreErrors = true */) { // We have to test new.target here to see if this function is called @@ -142,7 +152,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) { // We have to bind the methods grabbed from the instance instead of from // the prototype so that users extending the Console can override them // from the prototype chain of the subclass. - this[key] = this[key].bind(this); + this[key] = FunctionPrototypeBind(this[key], this); ObjectDefineProperty(this[key], 'name', { value: key }); @@ -224,9 +234,9 @@ ObjectDefineProperties(Console.prototype, { ...consolePropAttributes, value: Boolean(ignoreErrors) }, - '_times': { ...consolePropAttributes, value: new Map() }, + '_times': { ...consolePropAttributes, value: new SafeMap() }, // Corresponds to https://console.spec.whatwg.org/#count-map - [kCounts]: { ...consolePropAttributes, value: new Map() }, + [kCounts]: { ...consolePropAttributes, value: new SafeMap() }, [kColorMode]: { ...consolePropAttributes, value: colorMode }, [kIsConsole]: { ...consolePropAttributes, value: true }, [kGroupIndent]: { ...consolePropAttributes, value: '' }, @@ -255,8 +265,8 @@ ObjectDefineProperties(Console.prototype, { this._stdoutErrorHandler : this._stderrErrorHandler; if (groupIndent.length !== 0) { - if (string.includes('\n')) { - string = string.replace(/\n/g, `\n${groupIndent}`); + if (StringPrototypeIncludes(string, '\n')) { + string = StringPrototypeReplace(string, /\n/g, `\n${groupIndent}`); } string = groupIndent + string; } @@ -450,13 +460,16 @@ const consoleMethods = { if (data.length > 0) { this.log(...data); } - this[kGroupIndent] += ' '.repeat(this[kGroupIndentationWidth]); + this[kGroupIndent] += + StringPrototypeRepeat(' ', this[kGroupIndentationWidth]); }, groupEnd() { - this[kGroupIndent] = - this[kGroupIndent].slice(0, this[kGroupIndent].length - - this[kGroupIndentationWidth]); + this[kGroupIndent] = StringPrototypeSlice( + this[kGroupIndent], + 0, + this[kGroupIndent].length - this[kGroupIndentationWidth] + ); }, // https://console.spec.whatwg.org/#table @@ -501,14 +514,14 @@ const consoleMethods = { let length = 0; if (mapIter) { for (; i < tabularData.length / 2; ++i) { - keys.push(_inspect(tabularData[i * 2])); - values.push(_inspect(tabularData[i * 2 + 1])); + ArrayPrototypePush(keys, _inspect(tabularData[i * 2])); + ArrayPrototypePush(values, _inspect(tabularData[i * 2 + 1])); length++; } } else { for (const [k, v] of tabularData) { - keys.push(_inspect(k)); - values.push(_inspect(v)); + ArrayPrototypePush(keys, _inspect(k)); + ArrayPrototypePush(values, _inspect(v)); length++; } } @@ -530,7 +543,7 @@ const consoleMethods = { const values = []; let length = 0; for (const v of tabularData) { - values.push(_inspect(v)); + ArrayPrototypePush(values, _inspect(v)); length++; } return final([iterKey, valuesKey], [getIndexArray(length), values]); @@ -565,11 +578,11 @@ const consoleMethods = { const keys = ObjectKeys(map); const values = ObjectValues(map); if (hasPrimitives) { - keys.push(valuesKey); - values.push(valuesKeyArray); + ArrayPrototypePush(keys, valuesKey); + ArrayPrototypePush(values, valuesKeyArray); } - keys.unshift(indexKey); - values.unshift(indexKeyArray); + ArrayPrototypeUnshift(keys, indexKey); + ArrayPrototypeUnshift(values, indexKeyArray); return final(keys, values); }, @@ -596,7 +609,7 @@ function timeLogImpl(self, name, label, data) { } function pad(value) { - return `${value}`.padStart(2, '0'); + return StringPrototypePadStart(`${value}`, 2, '0'); } function formatTime(ms) { @@ -617,16 +630,19 @@ function formatTime(ms) { } if (hours !== 0 || minutes !== 0) { - [seconds, ms] = seconds.toFixed(3).split('.'); + [seconds, ms] = StringPrototypeSplit( + NumberPrototypeToFixed(seconds, 3), + '.' + ); const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes; return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`; } if (seconds !== 0) { - return `${seconds.toFixed(3)}s`; + return `${NumberPrototypeToFixed(seconds, 3)}s`; } - return `${Number(ms.toFixed(3))}ms`; + return `${Number(NumberPrototypeToFixed(ms, 3))}ms`; } const keyKey = 'Key'; diff --git a/lib/internal/console/global.js b/lib/internal/console/global.js index 1c615c78451510..d6c0c24d529dcc 100644 --- a/lib/internal/console/global.js +++ b/lib/internal/console/global.js @@ -13,6 +13,7 @@ // in the global console prototype chain anymore. const { + FunctionPrototypeBind, ObjectCreate, ReflectDefineProperty, ReflectGetOwnPropertyDescriptor, @@ -37,7 +38,7 @@ for (const prop of ReflectOwnKeys(Console.prototype)) { const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop); if (typeof desc.value === 'function') { // fix the receiver const name = desc.value.name; - desc.value = desc.value.bind(globalConsole); + desc.value = FunctionPrototypeBind(desc.value, globalConsole); ReflectDefineProperty(desc.value, 'name', { value: name }); } ReflectDefineProperty(globalConsole, prop, desc);