Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

console: use more primordials #35734

Merged
merged 1 commit into from
Nov 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
@@ -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';
3 changes: 2 additions & 1 deletion lib/internal/console/global.js
Original file line number Diff line number Diff line change
@@ -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);