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: refactor to avoid unsafe array iteration #36753

Merged
merged 1 commit into from Jan 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 17 additions & 11 deletions lib/internal/console/constructor.js
Expand Up @@ -6,6 +6,7 @@
const {
ArrayFrom,
ArrayIsArray,
ArrayPrototypeForEach,
ArrayPrototypePush,
ArrayPrototypeUnshift,
Boolean,
Expand All @@ -19,7 +20,10 @@ const {
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ObjectValues,
ReflectApply,
ReflectConstruct,
ReflectOwnKeys,
SafeArrayIterator,
SafeMap,
SafeWeakMap,
StringPrototypeIncludes,
Expand Down Expand Up @@ -97,7 +101,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
// with new, because we need to define a custom instanceof to accommodate
// the global console.
if (!new.target) {
return new Console(...arguments);
return ReflectConstruct(Console, arguments);
}

if (!options || typeof options.write === 'function') {
Expand Down Expand Up @@ -147,16 +151,15 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
}

// Bind the prototype functions to this Console instance
const keys = ObjectKeys(Console.prototype);
for (const key of keys) {
ArrayPrototypeForEach(ObjectKeys(Console.prototype), (key) => {
// 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] = FunctionPrototypeBind(this[key], this);
ObjectDefineProperty(this[key], 'name', {
value: key
});
}
});

this[kBindStreamsEager](stdout, stderr);
this[kBindProperties](ignoreErrors, colorMode, groupIndentation);
Expand Down Expand Up @@ -320,14 +323,16 @@ ObjectDefineProperties(Console.prototype, {
...consolePropAttributes,
value: function(args) {
const opts = this[kGetInspectOptions](this._stdout);
return formatWithOptions(opts, ...args);
ArrayPrototypeUnshift(args, opts);
return ReflectApply(formatWithOptions, null, args);
}
},
[kFormatForStderr]: {
...consolePropAttributes,
value: function(args) {
const opts = this[kGetInspectOptions](this._stderr);
return formatWithOptions(opts, ...args);
ArrayPrototypeUnshift(args, opts);
return ReflectApply(formatWithOptions, null, args);
}
},
});
Expand Down Expand Up @@ -412,7 +417,8 @@ const consoleMethods = {
assert(expression, ...args) {
if (!expression) {
args[0] = `Assertion failed${args.length === 0 ? '' : `: ${args[0]}`}`;
this.warn(...args); // The arguments will be formatted in warn() again
// The arguments will be formatted in warn() again
ReflectApply(this.warn, this, args);
}
},

Expand Down Expand Up @@ -458,7 +464,7 @@ const consoleMethods = {

group(...data) {
if (data.length > 0) {
this.log(...data);
ReflectApply(this.log, this, data);
}
this[kGroupIndent] +=
StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
Expand Down Expand Up @@ -603,7 +609,7 @@ function timeLogImpl(self, name, label, data) {
if (data === undefined) {
self.log('%s: %s', label, formatted);
} else {
self.log('%s: %s', label, formatted, ...data);
self.log('%s: %s', label, formatted, ...new SafeArrayIterator(data));
}
return true;
}
Expand All @@ -630,10 +636,10 @@ function formatTime(ms) {
}

if (hours !== 0 || minutes !== 0) {
[seconds, ms] = StringPrototypeSplit(
({ 0: seconds, 1: 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)`;
}
Expand Down