From 26e95e7607241d8695b31870da45d18631bea4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Sat, 27 Jul 2024 13:08:32 -0300 Subject: [PATCH 1/2] console: fix issues with frozen intrinsics --- lib/internal/console/constructor.js | 39 +++++++++---------- .../test-console-with-frozen-intrinsics.js | 32 +++++++++++++++ 2 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 test/parallel/test-console-with-frozen-intrinsics.js diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index c3488d40593acf..6c34a9ededcc60 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 00000000000000..33353525c071e4 --- /dev/null +++ b/test/parallel/test-console-with-frozen-intrinsics.js @@ -0,0 +1,32 @@ +// 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' }]); + +console.timeStamp('label'); From b6fa4af6d91c9f37d55b58990a09e57147afbdcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vinicius=20Louren=C3=A7o?= <12551007+H4ad@users.noreply.github.com> Date: Sun, 28 Jul 2024 09:48:34 -0300 Subject: [PATCH 2/2] remove method that does not exist --- test/parallel/test-console-with-frozen-intrinsics.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/parallel/test-console-with-frozen-intrinsics.js b/test/parallel/test-console-with-frozen-intrinsics.js index 33353525c071e4..1da2a6a5fb9eaa 100644 --- a/test/parallel/test-console-with-frozen-intrinsics.js +++ b/test/parallel/test-console-with-frozen-intrinsics.js @@ -28,5 +28,3 @@ console.groupCollapsed('label'); console.groupEnd(); console.table([{ a: 1, b: 2 }, { a: 'foo', b: 'bar' }]); - -console.timeStamp('label');