Skip to content

Commit

Permalink
console: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #36753
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Sep 1, 2021
1 parent 797f7f8 commit 32aff2f
Showing 1 changed file with 17 additions and 11 deletions.
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

0 comments on commit 32aff2f

Please sign in to comment.