Skip to content

Commit 0c11a17

Browse files
aduh95danielleadams
authored andcommittedJan 12, 2021
console: refactor to avoid unsafe array iteration
PR-URL: #36753 Reviewed-By: James M Snell <[email protected]>
1 parent 95219ea commit 0c11a17

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed
 

‎lib/internal/console/constructor.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
const {
77
ArrayFrom,
88
ArrayIsArray,
9+
ArrayPrototypeForEach,
910
ArrayPrototypePush,
1011
ArrayPrototypeUnshift,
1112
Boolean,
@@ -19,7 +20,10 @@ const {
1920
ObjectKeys,
2021
ObjectPrototypeHasOwnProperty,
2122
ObjectValues,
23+
ReflectApply,
24+
ReflectConstruct,
2225
ReflectOwnKeys,
26+
SafeArrayIterator,
2327
SafeMap,
2428
SafeWeakMap,
2529
StringPrototypeIncludes,
@@ -97,7 +101,7 @@ function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
97101
// with new, because we need to define a custom instanceof to accommodate
98102
// the global console.
99103
if (!new.target) {
100-
return new Console(...arguments);
104+
return ReflectConstruct(Console, arguments);
101105
}
102106

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

149153
// Bind the prototype functions to this Console instance
150-
const keys = ObjectKeys(Console.prototype);
151-
for (const key of keys) {
154+
ArrayPrototypeForEach(ObjectKeys(Console.prototype), (key) => {
152155
// We have to bind the methods grabbed from the instance instead of from
153156
// the prototype so that users extending the Console can override them
154157
// from the prototype chain of the subclass.
155158
this[key] = FunctionPrototypeBind(this[key], this);
156159
ObjectDefineProperty(this[key], 'name', {
157160
value: key
158161
});
159-
}
162+
});
160163

161164
this[kBindStreamsEager](stdout, stderr);
162165
this[kBindProperties](ignoreErrors, colorMode, groupIndentation);
@@ -320,14 +323,16 @@ ObjectDefineProperties(Console.prototype, {
320323
...consolePropAttributes,
321324
value: function(args) {
322325
const opts = this[kGetInspectOptions](this._stdout);
323-
return formatWithOptions(opts, ...args);
326+
ArrayPrototypeUnshift(args, opts);
327+
return ReflectApply(formatWithOptions, null, args);
324328
}
325329
},
326330
[kFormatForStderr]: {
327331
...consolePropAttributes,
328332
value: function(args) {
329333
const opts = this[kGetInspectOptions](this._stderr);
330-
return formatWithOptions(opts, ...args);
334+
ArrayPrototypeUnshift(args, opts);
335+
return ReflectApply(formatWithOptions, null, args);
331336
}
332337
},
333338
});
@@ -412,7 +417,8 @@ const consoleMethods = {
412417
assert(expression, ...args) {
413418
if (!expression) {
414419
args[0] = `Assertion failed${args.length === 0 ? '' : `: ${args[0]}`}`;
415-
this.warn(...args); // The arguments will be formatted in warn() again
420+
// The arguments will be formatted in warn() again
421+
ReflectApply(this.warn, this, args);
416422
}
417423
},
418424

@@ -458,7 +464,7 @@ const consoleMethods = {
458464

459465
group(...data) {
460466
if (data.length > 0) {
461-
this.log(...data);
467+
ReflectApply(this.log, this, data);
462468
}
463469
this[kGroupIndent] +=
464470
StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
@@ -603,7 +609,7 @@ function timeLogImpl(self, name, label, data) {
603609
if (data === undefined) {
604610
self.log('%s: %s', label, formatted);
605611
} else {
606-
self.log('%s: %s', label, formatted, ...data);
612+
self.log('%s: %s', label, formatted, ...new SafeArrayIterator(data));
607613
}
608614
return true;
609615
}
@@ -630,10 +636,10 @@ function formatTime(ms) {
630636
}
631637

632638
if (hours !== 0 || minutes !== 0) {
633-
[seconds, ms] = StringPrototypeSplit(
639+
({ 0: seconds, 1: ms } = StringPrototypeSplit(
634640
NumberPrototypeToFixed(seconds, 3),
635641
'.'
636-
);
642+
));
637643
const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes;
638644
return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`;
639645
}

0 commit comments

Comments
 (0)