mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
console: use more primordials
PR-URL: https://github.com/nodejs/node/pull/35734 Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
c9e38489e7
commit
1f6ca18c3b
2 files changed
+43
-26
No files matched your search
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user