diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index c3488d40593..6c34a9ededc 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -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 diff --git a/test/parallel/test-console-with-frozen-intrinsics.js b/test/parallel/test-console-with-frozen-intrinsics.js new file mode 100644 index 00000000000..1da2a6a5fb9 --- /dev/null +++ b/test/parallel/test-console-with-frozen-intrinsics.js @@ -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' }]);