Skip to content

Commit 9757b47

Browse files
aduh95danielleadams
authored andcommitted
console: use more primordials
PR-URL: #35734 Reviewed-By: Rich Trott <[email protected]>
1 parent dc4936b commit 9757b47

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

lib/internal/console/constructor.js

+41-25
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,31 @@
66
const {
77
ArrayFrom,
88
ArrayIsArray,
9+
ArrayPrototypePush,
10+
ArrayPrototypeUnshift,
911
Boolean,
1012
ErrorCaptureStackTrace,
11-
Map,
13+
FunctionPrototypeBind,
1214
MathFloor,
1315
Number,
16+
NumberPrototypeToFixed,
1417
ObjectDefineProperties,
1518
ObjectDefineProperty,
1619
ObjectKeys,
1720
ObjectPrototypeHasOwnProperty,
1821
ObjectValues,
1922
ReflectOwnKeys,
23+
SafeMap,
24+
SafeWeakMap,
25+
StringPrototypeIncludes,
26+
StringPrototypePadStart,
27+
StringPrototypeRepeat,
28+
StringPrototypeReplace,
29+
StringPrototypeSlice,
30+
StringPrototypeSplit,
2031
Symbol,
2132
SymbolHasInstance,
2233
SymbolToStringTag,
23-
WeakMap,
2434
} = primordials;
2535

2636
const { trace } = internalBinding('trace_events');
@@ -80,7 +90,7 @@ const kBindStreamsLazy = Symbol('kBindStreamsLazy');
8090
const kUseStdout = Symbol('kUseStdout');
8191
const kUseStderr = Symbol('kUseStderr');
8292

83-
const optionsMap = new WeakMap();
93+
const optionsMap = new SafeWeakMap();
8494

8595
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
8696
// 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 */) {
142152
// We have to bind the methods grabbed from the instance instead of from
143153
// the prototype so that users extending the Console can override them
144154
// from the prototype chain of the subclass.
145-
this[key] = this[key].bind(this);
155+
this[key] = FunctionPrototypeBind(this[key], this);
146156
ObjectDefineProperty(this[key], 'name', {
147157
value: key
148158
});
@@ -224,9 +234,9 @@ ObjectDefineProperties(Console.prototype, {
224234
...consolePropAttributes,
225235
value: Boolean(ignoreErrors)
226236
},
227-
'_times': { ...consolePropAttributes, value: new Map() },
237+
'_times': { ...consolePropAttributes, value: new SafeMap() },
228238
// Corresponds to https://console.spec.whatwg.org/#count-map
229-
[kCounts]: { ...consolePropAttributes, value: new Map() },
239+
[kCounts]: { ...consolePropAttributes, value: new SafeMap() },
230240
[kColorMode]: { ...consolePropAttributes, value: colorMode },
231241
[kIsConsole]: { ...consolePropAttributes, value: true },
232242
[kGroupIndent]: { ...consolePropAttributes, value: '' },
@@ -255,8 +265,8 @@ ObjectDefineProperties(Console.prototype, {
255265
this._stdoutErrorHandler : this._stderrErrorHandler;
256266

257267
if (groupIndent.length !== 0) {
258-
if (string.includes('\n')) {
259-
string = string.replace(/\n/g, `\n${groupIndent}`);
268+
if (StringPrototypeIncludes(string, '\n')) {
269+
string = StringPrototypeReplace(string, /\n/g, `\n${groupIndent}`);
260270
}
261271
string = groupIndent + string;
262272
}
@@ -450,13 +460,16 @@ const consoleMethods = {
450460
if (data.length > 0) {
451461
this.log(...data);
452462
}
453-
this[kGroupIndent] += ' '.repeat(this[kGroupIndentationWidth]);
463+
this[kGroupIndent] +=
464+
StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
454465
},
455466

456467
groupEnd() {
457-
this[kGroupIndent] =
458-
this[kGroupIndent].slice(0, this[kGroupIndent].length -
459-
this[kGroupIndentationWidth]);
468+
this[kGroupIndent] = StringPrototypeSlice(
469+
this[kGroupIndent],
470+
0,
471+
this[kGroupIndent].length - this[kGroupIndentationWidth]
472+
);
460473
},
461474

462475
// https://console.spec.whatwg.org/#table
@@ -501,14 +514,14 @@ const consoleMethods = {
501514
let length = 0;
502515
if (mapIter) {
503516
for (; i < tabularData.length / 2; ++i) {
504-
keys.push(_inspect(tabularData[i * 2]));
505-
values.push(_inspect(tabularData[i * 2 + 1]));
517+
ArrayPrototypePush(keys, _inspect(tabularData[i * 2]));
518+
ArrayPrototypePush(values, _inspect(tabularData[i * 2 + 1]));
506519
length++;
507520
}
508521
} else {
509522
for (const [k, v] of tabularData) {
510-
keys.push(_inspect(k));
511-
values.push(_inspect(v));
523+
ArrayPrototypePush(keys, _inspect(k));
524+
ArrayPrototypePush(values, _inspect(v));
512525
length++;
513526
}
514527
}
@@ -530,7 +543,7 @@ const consoleMethods = {
530543
const values = [];
531544
let length = 0;
532545
for (const v of tabularData) {
533-
values.push(_inspect(v));
546+
ArrayPrototypePush(values, _inspect(v));
534547
length++;
535548
}
536549
return final([iterKey, valuesKey], [getIndexArray(length), values]);
@@ -565,11 +578,11 @@ const consoleMethods = {
565578
const keys = ObjectKeys(map);
566579
const values = ObjectValues(map);
567580
if (hasPrimitives) {
568-
keys.push(valuesKey);
569-
values.push(valuesKeyArray);
581+
ArrayPrototypePush(keys, valuesKey);
582+
ArrayPrototypePush(values, valuesKeyArray);
570583
}
571-
keys.unshift(indexKey);
572-
values.unshift(indexKeyArray);
584+
ArrayPrototypeUnshift(keys, indexKey);
585+
ArrayPrototypeUnshift(values, indexKeyArray);
573586

574587
return final(keys, values);
575588
},
@@ -596,7 +609,7 @@ function timeLogImpl(self, name, label, data) {
596609
}
597610

598611
function pad(value) {
599-
return `${value}`.padStart(2, '0');
612+
return StringPrototypePadStart(`${value}`, 2, '0');
600613
}
601614

602615
function formatTime(ms) {
@@ -617,16 +630,19 @@ function formatTime(ms) {
617630
}
618631

619632
if (hours !== 0 || minutes !== 0) {
620-
[seconds, ms] = seconds.toFixed(3).split('.');
633+
[seconds, ms] = StringPrototypeSplit(
634+
NumberPrototypeToFixed(seconds, 3),
635+
'.'
636+
);
621637
const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes;
622638
return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`;
623639
}
624640

625641
if (seconds !== 0) {
626-
return `${seconds.toFixed(3)}s`;
642+
return `${NumberPrototypeToFixed(seconds, 3)}s`;
627643
}
628644

629-
return `${Number(ms.toFixed(3))}ms`;
645+
return `${Number(NumberPrototypeToFixed(ms, 3))}ms`;
630646
}
631647

632648
const keyKey = 'Key';

lib/internal/console/global.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// in the global console prototype chain anymore.
1414

1515
const {
16+
FunctionPrototypeBind,
1617
ObjectCreate,
1718
ReflectDefineProperty,
1819
ReflectGetOwnPropertyDescriptor,
@@ -37,7 +38,7 @@ for (const prop of ReflectOwnKeys(Console.prototype)) {
3738
const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
3839
if (typeof desc.value === 'function') { // fix the receiver
3940
const name = desc.value.name;
40-
desc.value = desc.value.bind(globalConsole);
41+
desc.value = FunctionPrototypeBind(desc.value, globalConsole);
4142
ReflectDefineProperty(desc.value, 'name', { value: name });
4243
}
4344
ReflectDefineProperty(globalConsole, prop, desc);

0 commit comments

Comments
 (0)