mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
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:
parent
3c50297e9e
commit
890760b8e5
@ -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
|
||||
|
30
test/parallel/test-console-with-frozen-intrinsics.js
Normal file
30
test/parallel/test-console-with-frozen-intrinsics.js
Normal 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' }]);
|
Loading…
Reference in New Issue
Block a user