Skip to content

Commit fad3e74

Browse files
H4adRafaelGSS
authored andcommitted
console: fix issues with frozen intrinsics
PR-URL: #54070 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
1 parent eed0963 commit fad3e74

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

lib/internal/console/constructor.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ function lazyUtilColors() {
7878
}
7979

8080
// Track amount of indentation required via `console.group()`.
81-
const kGroupIndent = Symbol('kGroupIndent');
8281
const kGroupIndentationWidth = Symbol('kGroupIndentWidth');
8382
const kFormatForStderr = Symbol('kFormatForStderr');
8483
const kFormatForStdout = Symbol('kFormatForStdout');
@@ -91,7 +90,6 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
9190
const kBindStreamsLazy = Symbol('kBindStreamsLazy');
9291
const kUseStdout = Symbol('kUseStdout');
9392
const kUseStderr = Symbol('kUseStderr');
94-
const kInternalTimeLogImpl = Symbol('kInternalTimeLogImpl');
9593

9694
const optionsMap = new SafeWeakMap();
9795
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
@@ -178,6 +176,8 @@ ObjectDefineProperty(Console, SymbolHasInstance, {
178176
const kColorInspectOptions = { colors: true };
179177
const kNoColorInspectOptions = {};
180178

179+
const internalIndentationMap = new SafeWeakMap();
180+
181181
ObjectDefineProperties(Console.prototype, {
182182
[kBindStreamsEager]: {
183183
__proto__: null,
@@ -247,7 +247,6 @@ ObjectDefineProperties(Console.prototype, {
247247
[kCounts]: { __proto__: null, ...consolePropAttributes, value: new SafeMap() },
248248
[kColorMode]: { __proto__: null, ...consolePropAttributes, value: colorMode },
249249
[kIsConsole]: { __proto__: null, ...consolePropAttributes, value: true },
250-
[kGroupIndent]: { __proto__: null, ...consolePropAttributes, value: '' },
251250
[kGroupIndentationWidth]: {
252251
__proto__: null,
253252
...consolePropAttributes,
@@ -268,7 +267,7 @@ ObjectDefineProperties(Console.prototype, {
268267
...consolePropAttributes,
269268
value: function(streamSymbol, string, color = '') {
270269
const ignoreErrors = this._ignoreErrors;
271-
const groupIndent = this[kGroupIndent];
270+
const groupIndent = internalIndentationMap.get(this) || '';
272271

273272
const useStdout = streamSymbol === kUseStdout;
274273
const stream = useStdout ? this._stdout : this._stderr;
@@ -372,11 +371,11 @@ function createWriteErrorHandler(instance, streamSymbol) {
372371
};
373372
}
374373

375-
function timeLogImpl(label, formatted, args) {
374+
function timeLogImpl(consoleRef, label, formatted, args) {
376375
if (args === undefined) {
377-
this.log('%s: %s', label, formatted);
376+
consoleRef.log('%s: %s', label, formatted);
378377
} else {
379-
this.log('%s: %s', label, formatted, ...new SafeArrayIterator(args));
378+
consoleRef.log('%s: %s', label, formatted, ...new SafeArrayIterator(args));
380379
}
381380
}
382381

@@ -407,17 +406,11 @@ const consoleMethods = {
407406
},
408407

409408
timeEnd(label = 'default') {
410-
if (this[kInternalTimeLogImpl] === undefined)
411-
this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this);
412-
413-
timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', kNone, this[kInternalTimeLogImpl], label, `time::${label}`);
409+
timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', kNone, (label, formatted, args) => timeLogImpl(this, label, formatted, args), label, `time::${label}`);
414410
},
415411

416412
timeLog(label = 'default', ...data) {
417-
if (this[kInternalTimeLogImpl] === undefined)
418-
this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this);
419-
420-
timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', kNone, this[kInternalTimeLogImpl], label, `time::${label}`, data);
413+
timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', kNone, (label, formatted, args) => timeLogImpl(this, label, formatted, args), label, `time::${label}`, data);
421414
},
422415

423416
trace: function trace(...args) {
@@ -489,16 +482,22 @@ const consoleMethods = {
489482
if (data.length > 0) {
490483
ReflectApply(this.log, this, data);
491484
}
492-
this[kGroupIndent] +=
493-
StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
485+
486+
let currentIndentation = internalIndentationMap.get(this) || '';
487+
currentIndentation += StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
488+
489+
internalIndentationMap.set(this, currentIndentation);
494490
},
495491

496492
groupEnd() {
497-
this[kGroupIndent] = StringPrototypeSlice(
498-
this[kGroupIndent],
493+
const currentIndentation = internalIndentationMap.get(this) || '';
494+
const newIndentation = StringPrototypeSlice(
495+
currentIndentation,
499496
0,
500-
this[kGroupIndent].length - this[kGroupIndentationWidth],
497+
currentIndentation.length - this[kGroupIndentationWidth],
501498
);
499+
500+
internalIndentationMap.set(this, newIndentation);
502501
},
503502

504503
// https://console.spec.whatwg.org/#table
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// flags: --frozen-intrinsics
2+
'use strict';
3+
require('../common');
4+
console.clear();
5+
6+
const consoleMethods = ['log', 'info', 'warn', 'error', 'debug', 'trace'];
7+
8+
for (const method of consoleMethods) {
9+
console[method]('foo');
10+
console[method]('foo', 'bar');
11+
console[method]('%s %s', 'foo', 'bar', 'hop');
12+
}
13+
14+
console.dir({ slashes: '\\\\' });
15+
console.dirxml({ slashes: '\\\\' });
16+
17+
console.time('label');
18+
console.timeLog('label', 'hi');
19+
console.timeEnd('label');
20+
21+
console.assert(true, 'true');
22+
23+
console.count('label');
24+
console.countReset('label');
25+
26+
console.group('label');
27+
console.groupCollapsed('label');
28+
console.groupEnd();
29+
30+
console.table([{ a: 1, b: 2 }, { a: 'foo', b: 'bar' }]);

0 commit comments

Comments
 (0)