console: fix issues with frozen intrinsics

PR-URL: https://github.com/nodejs/node/pull/54070
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Jake Yuesong Li <jake.yuesong@gmail.com>
This commit is contained in:
Vinicius Lourenço 2024-07-30 01:38:21 -03:00 committed by GitHub
parent 3c50297e9e
commit 890760b8e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 20 deletions

View File

@ -78,7 +78,6 @@ function lazyUtilColors() {
}
// Track amount of indentation required via `console.group()`.
const kGroupIndent = Symbol('kGroupIndent');
const kGroupIndentationWidth = Symbol('kGroupIndentWidth');
const kFormatForStderr = Symbol('kFormatForStderr');
const kFormatForStdout = Symbol('kFormatForStdout');
@ -91,7 +90,6 @@ const kBindStreamsEager = Symbol('kBindStreamsEager');
const kBindStreamsLazy = Symbol('kBindStreamsLazy');
const kUseStdout = Symbol('kUseStdout');
const kUseStderr = Symbol('kUseStderr');
const kInternalTimeLogImpl = Symbol('kInternalTimeLogImpl');
const optionsMap = new SafeWeakMap();
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
@ -178,6 +176,8 @@ ObjectDefineProperty(Console, SymbolHasInstance, {
const kColorInspectOptions = { colors: true };
const kNoColorInspectOptions = {};
const internalIndentationMap = new SafeWeakMap();
ObjectDefineProperties(Console.prototype, {
[kBindStreamsEager]: {
__proto__: null,
@ -247,7 +247,6 @@ ObjectDefineProperties(Console.prototype, {
[kCounts]: { __proto__: null, ...consolePropAttributes, value: new SafeMap() },
[kColorMode]: { __proto__: null, ...consolePropAttributes, value: colorMode },
[kIsConsole]: { __proto__: null, ...consolePropAttributes, value: true },
[kGroupIndent]: { __proto__: null, ...consolePropAttributes, value: '' },
[kGroupIndentationWidth]: {
__proto__: null,
...consolePropAttributes,
@ -268,7 +267,7 @@ ObjectDefineProperties(Console.prototype, {
...consolePropAttributes,
value: function(streamSymbol, string, color = '') {
const ignoreErrors = this._ignoreErrors;
const groupIndent = this[kGroupIndent];
const groupIndent = internalIndentationMap.get(this) || '';
const useStdout = streamSymbol === kUseStdout;
const stream = useStdout ? this._stdout : this._stderr;
@ -372,11 +371,11 @@ function createWriteErrorHandler(instance, streamSymbol) {
};
}
function timeLogImpl(label, formatted, args) {
function timeLogImpl(consoleRef, label, formatted, args) {
if (args === undefined) {
this.log('%s: %s', label, formatted);
consoleRef.log('%s: %s', label, formatted);
} else {
this.log('%s: %s', label, formatted, ...new SafeArrayIterator(args));
consoleRef.log('%s: %s', label, formatted, ...new SafeArrayIterator(args));
}
}
@ -407,17 +406,11 @@ const consoleMethods = {
},
timeEnd(label = 'default') {
if (this[kInternalTimeLogImpl] === undefined)
this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this);
timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', kNone, this[kInternalTimeLogImpl], label, `time::${label}`);
timeEnd(this._times, kTraceConsoleCategory, 'console.timeEnd()', kNone, (label, formatted, args) => timeLogImpl(this, label, formatted, args), label, `time::${label}`);
},
timeLog(label = 'default', ...data) {
if (this[kInternalTimeLogImpl] === undefined)
this[kInternalTimeLogImpl] = FunctionPrototypeBind(timeLogImpl, this);
timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', kNone, this[kInternalTimeLogImpl], label, `time::${label}`, data);
timeLog(this._times, kTraceConsoleCategory, 'console.timeLog()', kNone, (label, formatted, args) => timeLogImpl(this, label, formatted, args), label, `time::${label}`, data);
},
trace: function trace(...args) {
@ -489,16 +482,22 @@ const consoleMethods = {
if (data.length > 0) {
ReflectApply(this.log, this, data);
}
this[kGroupIndent] +=
StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
let currentIndentation = internalIndentationMap.get(this) || '';
currentIndentation += StringPrototypeRepeat(' ', this[kGroupIndentationWidth]);
internalIndentationMap.set(this, currentIndentation);
},
groupEnd() {
this[kGroupIndent] = StringPrototypeSlice(
this[kGroupIndent],
const currentIndentation = internalIndentationMap.get(this) || '';
const newIndentation = StringPrototypeSlice(
currentIndentation,
0,
this[kGroupIndent].length - this[kGroupIndentationWidth],
currentIndentation.length - this[kGroupIndentationWidth],
);
internalIndentationMap.set(this, newIndentation);
},
// https://console.spec.whatwg.org/#table

View File

@ -0,0 +1,30 @@
// flags: --frozen-intrinsics
'use strict';
require('../common');
console.clear();
const consoleMethods = ['log', 'info', 'warn', 'error', 'debug', 'trace'];
for (const method of consoleMethods) {
console[method]('foo');
console[method]('foo', 'bar');
console[method]('%s %s', 'foo', 'bar', 'hop');
}
console.dir({ slashes: '\\\\' });
console.dirxml({ slashes: '\\\\' });
console.time('label');
console.timeLog('label', 'hi');
console.timeEnd('label');
console.assert(true, 'true');
console.count('label');
console.countReset('label');
console.group('label');
console.groupCollapsed('label');
console.groupEnd();
console.table([{ a: 1, b: 2 }, { a: 'foo', b: 'bar' }]);