Skip to content

Commit

Permalink
console: fix issues with frozen intrinsics
Browse files Browse the repository at this point in the history
PR-URL: #54070
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Jake Yuesong Li <[email protected]>
  • Loading branch information
H4ad authored and RafaelGSS committed Aug 5, 2024
1 parent eed0963 commit fad3e74
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 */) {
Expand Down Expand Up @@ -178,6 +176,8 @@ ObjectDefineProperty(Console, SymbolHasInstance, {
const kColorInspectOptions = { colors: true };
const kNoColorInspectOptions = {};

const internalIndentationMap = new SafeWeakMap();

ObjectDefineProperties(Console.prototype, {
[kBindStreamsEager]: {
__proto__: null,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-console-with-frozen-intrinsics.js
Original file line number Diff line number Diff line change
@@ -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' }]);

0 comments on commit fad3e74

Please sign in to comment.