Skip to content

Commit

Permalink
console: use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #35734
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
aduh95 authored and BethGriggs committed Dec 15, 2020
1 parent 773685c commit 568e617
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 26 deletions.
66 changes: 41 additions & 25 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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: '' },
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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++;
}
}
Expand All @@ -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]);
Expand Down Expand Up @@ -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);
},
Expand All @@ -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) {
Expand All @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/console/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// in the global console prototype chain anymore.

const {
FunctionPrototypeBind,
ObjectCreate,
ReflectDefineProperty,
ReflectGetOwnPropertyDescriptor,
Expand All @@ -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);
Expand Down

0 comments on commit 568e617

Please sign in to comment.