11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci// The Console constructor is not actually used to construct the global 41cb0ef41Sopenharmony_ci// console. It's exported for backwards compatibility. 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst { 71cb0ef41Sopenharmony_ci ArrayFrom, 81cb0ef41Sopenharmony_ci ArrayIsArray, 91cb0ef41Sopenharmony_ci ArrayPrototypeForEach, 101cb0ef41Sopenharmony_ci ArrayPrototypePush, 111cb0ef41Sopenharmony_ci ArrayPrototypeUnshift, 121cb0ef41Sopenharmony_ci Boolean, 131cb0ef41Sopenharmony_ci ErrorCaptureStackTrace, 141cb0ef41Sopenharmony_ci FunctionPrototypeBind, 151cb0ef41Sopenharmony_ci MathFloor, 161cb0ef41Sopenharmony_ci Number, 171cb0ef41Sopenharmony_ci NumberPrototypeToFixed, 181cb0ef41Sopenharmony_ci ObjectCreate, 191cb0ef41Sopenharmony_ci ObjectDefineProperties, 201cb0ef41Sopenharmony_ci ObjectDefineProperty, 211cb0ef41Sopenharmony_ci ObjectKeys, 221cb0ef41Sopenharmony_ci ObjectPrototypeHasOwnProperty, 231cb0ef41Sopenharmony_ci ObjectValues, 241cb0ef41Sopenharmony_ci ReflectApply, 251cb0ef41Sopenharmony_ci ReflectConstruct, 261cb0ef41Sopenharmony_ci ReflectOwnKeys, 271cb0ef41Sopenharmony_ci RegExpPrototypeSymbolReplace, 281cb0ef41Sopenharmony_ci SafeArrayIterator, 291cb0ef41Sopenharmony_ci SafeMap, 301cb0ef41Sopenharmony_ci SafeWeakMap, 311cb0ef41Sopenharmony_ci SafeSet, 321cb0ef41Sopenharmony_ci StringPrototypeIncludes, 331cb0ef41Sopenharmony_ci StringPrototypePadStart, 341cb0ef41Sopenharmony_ci StringPrototypeRepeat, 351cb0ef41Sopenharmony_ci StringPrototypeSlice, 361cb0ef41Sopenharmony_ci StringPrototypeSplit, 371cb0ef41Sopenharmony_ci Symbol, 381cb0ef41Sopenharmony_ci SymbolHasInstance, 391cb0ef41Sopenharmony_ci SymbolToStringTag, 401cb0ef41Sopenharmony_ci} = primordials; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciconst { trace } = internalBinding('trace_events'); 431cb0ef41Sopenharmony_ciconst { 441cb0ef41Sopenharmony_ci isStackOverflowError, 451cb0ef41Sopenharmony_ci codes: { 461cb0ef41Sopenharmony_ci ERR_CONSOLE_WRITABLE_STREAM, 471cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 481cb0ef41Sopenharmony_ci ERR_INCOMPATIBLE_OPTION_PAIR, 491cb0ef41Sopenharmony_ci }, 501cb0ef41Sopenharmony_ci} = require('internal/errors'); 511cb0ef41Sopenharmony_ciconst { 521cb0ef41Sopenharmony_ci validateArray, 531cb0ef41Sopenharmony_ci validateInteger, 541cb0ef41Sopenharmony_ci validateObject, 551cb0ef41Sopenharmony_ci} = require('internal/validators'); 561cb0ef41Sopenharmony_ciconst { previewEntries } = internalBinding('util'); 571cb0ef41Sopenharmony_ciconst { Buffer: { isBuffer } } = require('buffer'); 581cb0ef41Sopenharmony_ciconst { 591cb0ef41Sopenharmony_ci inspect, 601cb0ef41Sopenharmony_ci formatWithOptions, 611cb0ef41Sopenharmony_ci} = require('internal/util/inspect'); 621cb0ef41Sopenharmony_ciconst { 631cb0ef41Sopenharmony_ci isTypedArray, isSet, isMap, isSetIterator, isMapIterator, 641cb0ef41Sopenharmony_ci} = require('internal/util/types'); 651cb0ef41Sopenharmony_ciconst { 661cb0ef41Sopenharmony_ci CHAR_LOWERCASE_B: kTraceBegin, 671cb0ef41Sopenharmony_ci CHAR_LOWERCASE_E: kTraceEnd, 681cb0ef41Sopenharmony_ci CHAR_LOWERCASE_N: kTraceInstant, 691cb0ef41Sopenharmony_ci CHAR_UPPERCASE_C: kTraceCount, 701cb0ef41Sopenharmony_ci} = require('internal/constants'); 711cb0ef41Sopenharmony_ciconst kCounts = Symbol('counts'); 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ciconst kTraceConsoleCategory = 'node,node.console'; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_ciconst kSecond = 1000; 761cb0ef41Sopenharmony_ciconst kMinute = 60 * kSecond; 771cb0ef41Sopenharmony_ciconst kHour = 60 * kMinute; 781cb0ef41Sopenharmony_ciconst kMaxGroupIndentation = 1000; 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci// Lazy loaded for startup performance. 811cb0ef41Sopenharmony_cilet cliTable; 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_cilet utilColors; 841cb0ef41Sopenharmony_cifunction lazyUtilColors() { 851cb0ef41Sopenharmony_ci utilColors ??= require('internal/util/colors'); 861cb0ef41Sopenharmony_ci return utilColors; 871cb0ef41Sopenharmony_ci} 881cb0ef41Sopenharmony_ci 891cb0ef41Sopenharmony_ci// Track amount of indentation required via `console.group()`. 901cb0ef41Sopenharmony_ciconst kGroupIndent = Symbol('kGroupIndent'); 911cb0ef41Sopenharmony_ciconst kGroupIndentationWidth = Symbol('kGroupIndentWidth'); 921cb0ef41Sopenharmony_ciconst kFormatForStderr = Symbol('kFormatForStderr'); 931cb0ef41Sopenharmony_ciconst kFormatForStdout = Symbol('kFormatForStdout'); 941cb0ef41Sopenharmony_ciconst kGetInspectOptions = Symbol('kGetInspectOptions'); 951cb0ef41Sopenharmony_ciconst kColorMode = Symbol('kColorMode'); 961cb0ef41Sopenharmony_ciconst kIsConsole = Symbol('kIsConsole'); 971cb0ef41Sopenharmony_ciconst kWriteToConsole = Symbol('kWriteToConsole'); 981cb0ef41Sopenharmony_ciconst kBindProperties = Symbol('kBindProperties'); 991cb0ef41Sopenharmony_ciconst kBindStreamsEager = Symbol('kBindStreamsEager'); 1001cb0ef41Sopenharmony_ciconst kBindStreamsLazy = Symbol('kBindStreamsLazy'); 1011cb0ef41Sopenharmony_ciconst kUseStdout = Symbol('kUseStdout'); 1021cb0ef41Sopenharmony_ciconst kUseStderr = Symbol('kUseStderr'); 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_ciconst optionsMap = new SafeWeakMap(); 1051cb0ef41Sopenharmony_cifunction Console(options /* or: stdout, stderr, ignoreErrors = true */) { 1061cb0ef41Sopenharmony_ci // We have to test new.target here to see if this function is called 1071cb0ef41Sopenharmony_ci // with new, because we need to define a custom instanceof to accommodate 1081cb0ef41Sopenharmony_ci // the global console. 1091cb0ef41Sopenharmony_ci if (new.target === undefined) { 1101cb0ef41Sopenharmony_ci return ReflectConstruct(Console, arguments); 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci if (!options || typeof options.write === 'function') { 1141cb0ef41Sopenharmony_ci options = { 1151cb0ef41Sopenharmony_ci stdout: options, 1161cb0ef41Sopenharmony_ci stderr: arguments[1], 1171cb0ef41Sopenharmony_ci ignoreErrors: arguments[2], 1181cb0ef41Sopenharmony_ci }; 1191cb0ef41Sopenharmony_ci } 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci const { 1221cb0ef41Sopenharmony_ci stdout, 1231cb0ef41Sopenharmony_ci stderr = stdout, 1241cb0ef41Sopenharmony_ci ignoreErrors = true, 1251cb0ef41Sopenharmony_ci colorMode = 'auto', 1261cb0ef41Sopenharmony_ci inspectOptions, 1271cb0ef41Sopenharmony_ci groupIndentation, 1281cb0ef41Sopenharmony_ci } = options; 1291cb0ef41Sopenharmony_ci 1301cb0ef41Sopenharmony_ci if (!stdout || typeof stdout.write !== 'function') { 1311cb0ef41Sopenharmony_ci throw new ERR_CONSOLE_WRITABLE_STREAM('stdout'); 1321cb0ef41Sopenharmony_ci } 1331cb0ef41Sopenharmony_ci if (!stderr || typeof stderr.write !== 'function') { 1341cb0ef41Sopenharmony_ci throw new ERR_CONSOLE_WRITABLE_STREAM('stderr'); 1351cb0ef41Sopenharmony_ci } 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci if (typeof colorMode !== 'boolean' && colorMode !== 'auto') 1381cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE('colorMode', colorMode); 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci if (groupIndentation !== undefined) { 1411cb0ef41Sopenharmony_ci validateInteger(groupIndentation, 'groupIndentation', 1421cb0ef41Sopenharmony_ci 0, kMaxGroupIndentation); 1431cb0ef41Sopenharmony_ci } 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ci if (inspectOptions !== undefined) { 1461cb0ef41Sopenharmony_ci validateObject(inspectOptions, 'options.inspectOptions'); 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci if (inspectOptions.colors !== undefined && 1491cb0ef41Sopenharmony_ci options.colorMode !== undefined) { 1501cb0ef41Sopenharmony_ci throw new ERR_INCOMPATIBLE_OPTION_PAIR( 1511cb0ef41Sopenharmony_ci 'options.inspectOptions.color', 'colorMode'); 1521cb0ef41Sopenharmony_ci } 1531cb0ef41Sopenharmony_ci optionsMap.set(this, inspectOptions); 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci // Bind the prototype functions to this Console instance 1571cb0ef41Sopenharmony_ci ArrayPrototypeForEach(ObjectKeys(Console.prototype), (key) => { 1581cb0ef41Sopenharmony_ci // We have to bind the methods grabbed from the instance instead of from 1591cb0ef41Sopenharmony_ci // the prototype so that users extending the Console can override them 1601cb0ef41Sopenharmony_ci // from the prototype chain of the subclass. 1611cb0ef41Sopenharmony_ci this[key] = FunctionPrototypeBind(this[key], this); 1621cb0ef41Sopenharmony_ci ObjectDefineProperty(this[key], 'name', { 1631cb0ef41Sopenharmony_ci __proto__: null, 1641cb0ef41Sopenharmony_ci value: key, 1651cb0ef41Sopenharmony_ci }); 1661cb0ef41Sopenharmony_ci }); 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci this[kBindStreamsEager](stdout, stderr); 1691cb0ef41Sopenharmony_ci this[kBindProperties](ignoreErrors, colorMode, groupIndentation); 1701cb0ef41Sopenharmony_ci} 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ciconst consolePropAttributes = { 1731cb0ef41Sopenharmony_ci writable: true, 1741cb0ef41Sopenharmony_ci enumerable: false, 1751cb0ef41Sopenharmony_ci configurable: true, 1761cb0ef41Sopenharmony_ci}; 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci// Fixup global.console instanceof global.console.Console 1791cb0ef41Sopenharmony_ciObjectDefineProperty(Console, SymbolHasInstance, { 1801cb0ef41Sopenharmony_ci __proto__: null, 1811cb0ef41Sopenharmony_ci value(instance) { 1821cb0ef41Sopenharmony_ci return instance[kIsConsole]; 1831cb0ef41Sopenharmony_ci }, 1841cb0ef41Sopenharmony_ci}); 1851cb0ef41Sopenharmony_ci 1861cb0ef41Sopenharmony_ciconst kColorInspectOptions = { colors: true }; 1871cb0ef41Sopenharmony_ciconst kNoColorInspectOptions = {}; 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ciObjectDefineProperties(Console.prototype, { 1901cb0ef41Sopenharmony_ci [kBindStreamsEager]: { 1911cb0ef41Sopenharmony_ci __proto__: null, 1921cb0ef41Sopenharmony_ci ...consolePropAttributes, 1931cb0ef41Sopenharmony_ci // Eager version for the Console constructor 1941cb0ef41Sopenharmony_ci value: function(stdout, stderr) { 1951cb0ef41Sopenharmony_ci ObjectDefineProperties(this, { 1961cb0ef41Sopenharmony_ci '_stdout': { __proto__: null, ...consolePropAttributes, value: stdout }, 1971cb0ef41Sopenharmony_ci '_stderr': { __proto__: null, ...consolePropAttributes, value: stderr }, 1981cb0ef41Sopenharmony_ci }); 1991cb0ef41Sopenharmony_ci }, 2001cb0ef41Sopenharmony_ci }, 2011cb0ef41Sopenharmony_ci [kBindStreamsLazy]: { 2021cb0ef41Sopenharmony_ci __proto__: null, 2031cb0ef41Sopenharmony_ci ...consolePropAttributes, 2041cb0ef41Sopenharmony_ci // Lazily load the stdout and stderr from an object so we don't 2051cb0ef41Sopenharmony_ci // create the stdio streams when they are not even accessed 2061cb0ef41Sopenharmony_ci value: function(object) { 2071cb0ef41Sopenharmony_ci let stdout; 2081cb0ef41Sopenharmony_ci let stderr; 2091cb0ef41Sopenharmony_ci ObjectDefineProperties(this, { 2101cb0ef41Sopenharmony_ci '_stdout': { 2111cb0ef41Sopenharmony_ci __proto__: null, 2121cb0ef41Sopenharmony_ci enumerable: false, 2131cb0ef41Sopenharmony_ci configurable: true, 2141cb0ef41Sopenharmony_ci get() { 2151cb0ef41Sopenharmony_ci if (!stdout) stdout = object.stdout; 2161cb0ef41Sopenharmony_ci return stdout; 2171cb0ef41Sopenharmony_ci }, 2181cb0ef41Sopenharmony_ci set(value) { stdout = value; }, 2191cb0ef41Sopenharmony_ci }, 2201cb0ef41Sopenharmony_ci '_stderr': { 2211cb0ef41Sopenharmony_ci __proto__: null, 2221cb0ef41Sopenharmony_ci enumerable: false, 2231cb0ef41Sopenharmony_ci configurable: true, 2241cb0ef41Sopenharmony_ci get() { 2251cb0ef41Sopenharmony_ci if (!stderr) { stderr = object.stderr; } 2261cb0ef41Sopenharmony_ci return stderr; 2271cb0ef41Sopenharmony_ci }, 2281cb0ef41Sopenharmony_ci set(value) { stderr = value; }, 2291cb0ef41Sopenharmony_ci }, 2301cb0ef41Sopenharmony_ci }); 2311cb0ef41Sopenharmony_ci }, 2321cb0ef41Sopenharmony_ci }, 2331cb0ef41Sopenharmony_ci [kBindProperties]: { 2341cb0ef41Sopenharmony_ci __proto__: null, 2351cb0ef41Sopenharmony_ci ...consolePropAttributes, 2361cb0ef41Sopenharmony_ci value: function(ignoreErrors, colorMode, groupIndentation = 2) { 2371cb0ef41Sopenharmony_ci ObjectDefineProperties(this, { 2381cb0ef41Sopenharmony_ci '_stdoutErrorHandler': { 2391cb0ef41Sopenharmony_ci __proto__: null, 2401cb0ef41Sopenharmony_ci ...consolePropAttributes, 2411cb0ef41Sopenharmony_ci value: createWriteErrorHandler(this, kUseStdout), 2421cb0ef41Sopenharmony_ci }, 2431cb0ef41Sopenharmony_ci '_stderrErrorHandler': { 2441cb0ef41Sopenharmony_ci ...consolePropAttributes, 2451cb0ef41Sopenharmony_ci __proto__: null, 2461cb0ef41Sopenharmony_ci value: createWriteErrorHandler(this, kUseStderr), 2471cb0ef41Sopenharmony_ci }, 2481cb0ef41Sopenharmony_ci '_ignoreErrors': { 2491cb0ef41Sopenharmony_ci __proto__: null, 2501cb0ef41Sopenharmony_ci ...consolePropAttributes, 2511cb0ef41Sopenharmony_ci value: Boolean(ignoreErrors), 2521cb0ef41Sopenharmony_ci }, 2531cb0ef41Sopenharmony_ci '_times': { __proto__: null, ...consolePropAttributes, value: new SafeMap() }, 2541cb0ef41Sopenharmony_ci // Corresponds to https://console.spec.whatwg.org/#count-map 2551cb0ef41Sopenharmony_ci [kCounts]: { __proto__: null, ...consolePropAttributes, value: new SafeMap() }, 2561cb0ef41Sopenharmony_ci [kColorMode]: { __proto__: null, ...consolePropAttributes, value: colorMode }, 2571cb0ef41Sopenharmony_ci [kIsConsole]: { __proto__: null, ...consolePropAttributes, value: true }, 2581cb0ef41Sopenharmony_ci [kGroupIndent]: { __proto__: null, ...consolePropAttributes, value: '' }, 2591cb0ef41Sopenharmony_ci [kGroupIndentationWidth]: { 2601cb0ef41Sopenharmony_ci __proto__: null, 2611cb0ef41Sopenharmony_ci ...consolePropAttributes, 2621cb0ef41Sopenharmony_ci value: groupIndentation, 2631cb0ef41Sopenharmony_ci }, 2641cb0ef41Sopenharmony_ci [SymbolToStringTag]: { 2651cb0ef41Sopenharmony_ci __proto__: null, 2661cb0ef41Sopenharmony_ci writable: false, 2671cb0ef41Sopenharmony_ci enumerable: false, 2681cb0ef41Sopenharmony_ci configurable: true, 2691cb0ef41Sopenharmony_ci value: 'console', 2701cb0ef41Sopenharmony_ci }, 2711cb0ef41Sopenharmony_ci }); 2721cb0ef41Sopenharmony_ci }, 2731cb0ef41Sopenharmony_ci }, 2741cb0ef41Sopenharmony_ci [kWriteToConsole]: { 2751cb0ef41Sopenharmony_ci __proto__: null, 2761cb0ef41Sopenharmony_ci ...consolePropAttributes, 2771cb0ef41Sopenharmony_ci value: function(streamSymbol, string) { 2781cb0ef41Sopenharmony_ci const ignoreErrors = this._ignoreErrors; 2791cb0ef41Sopenharmony_ci const groupIndent = this[kGroupIndent]; 2801cb0ef41Sopenharmony_ci 2811cb0ef41Sopenharmony_ci const useStdout = streamSymbol === kUseStdout; 2821cb0ef41Sopenharmony_ci const stream = useStdout ? this._stdout : this._stderr; 2831cb0ef41Sopenharmony_ci const errorHandler = useStdout ? 2841cb0ef41Sopenharmony_ci this._stdoutErrorHandler : this._stderrErrorHandler; 2851cb0ef41Sopenharmony_ci 2861cb0ef41Sopenharmony_ci if (groupIndent.length !== 0) { 2871cb0ef41Sopenharmony_ci if (StringPrototypeIncludes(string, '\n')) { 2881cb0ef41Sopenharmony_ci string = RegExpPrototypeSymbolReplace(/\n/g, string, `\n${groupIndent}`); 2891cb0ef41Sopenharmony_ci } 2901cb0ef41Sopenharmony_ci string = groupIndent + string; 2911cb0ef41Sopenharmony_ci } 2921cb0ef41Sopenharmony_ci string += '\n'; 2931cb0ef41Sopenharmony_ci 2941cb0ef41Sopenharmony_ci if (ignoreErrors === false) return stream.write(string); 2951cb0ef41Sopenharmony_ci 2961cb0ef41Sopenharmony_ci // There may be an error occurring synchronously (e.g. for files or TTYs 2971cb0ef41Sopenharmony_ci // on POSIX systems) or asynchronously (e.g. pipes on POSIX systems), so 2981cb0ef41Sopenharmony_ci // handle both situations. 2991cb0ef41Sopenharmony_ci try { 3001cb0ef41Sopenharmony_ci // Add and later remove a noop error handler to catch synchronous 3011cb0ef41Sopenharmony_ci // errors. 3021cb0ef41Sopenharmony_ci if (stream.listenerCount('error') === 0) 3031cb0ef41Sopenharmony_ci stream.once('error', noop); 3041cb0ef41Sopenharmony_ci 3051cb0ef41Sopenharmony_ci stream.write(string, errorHandler); 3061cb0ef41Sopenharmony_ci } catch (e) { 3071cb0ef41Sopenharmony_ci // Console is a debugging utility, so it swallowing errors is not 3081cb0ef41Sopenharmony_ci // desirable even in edge cases such as low stack space. 3091cb0ef41Sopenharmony_ci if (isStackOverflowError(e)) 3101cb0ef41Sopenharmony_ci throw e; 3111cb0ef41Sopenharmony_ci // Sorry, there's no proper way to pass along the error here. 3121cb0ef41Sopenharmony_ci } finally { 3131cb0ef41Sopenharmony_ci stream.removeListener('error', noop); 3141cb0ef41Sopenharmony_ci } 3151cb0ef41Sopenharmony_ci }, 3161cb0ef41Sopenharmony_ci }, 3171cb0ef41Sopenharmony_ci [kGetInspectOptions]: { 3181cb0ef41Sopenharmony_ci __proto__: null, 3191cb0ef41Sopenharmony_ci ...consolePropAttributes, 3201cb0ef41Sopenharmony_ci value: function(stream) { 3211cb0ef41Sopenharmony_ci let color = this[kColorMode]; 3221cb0ef41Sopenharmony_ci if (color === 'auto') { 3231cb0ef41Sopenharmony_ci color = lazyUtilColors().shouldColorize(stream); 3241cb0ef41Sopenharmony_ci } 3251cb0ef41Sopenharmony_ci 3261cb0ef41Sopenharmony_ci const options = optionsMap.get(this); 3271cb0ef41Sopenharmony_ci if (options) { 3281cb0ef41Sopenharmony_ci if (options.colors === undefined) { 3291cb0ef41Sopenharmony_ci options.colors = color; 3301cb0ef41Sopenharmony_ci } 3311cb0ef41Sopenharmony_ci return options; 3321cb0ef41Sopenharmony_ci } 3331cb0ef41Sopenharmony_ci 3341cb0ef41Sopenharmony_ci return color ? kColorInspectOptions : kNoColorInspectOptions; 3351cb0ef41Sopenharmony_ci }, 3361cb0ef41Sopenharmony_ci }, 3371cb0ef41Sopenharmony_ci [kFormatForStdout]: { 3381cb0ef41Sopenharmony_ci __proto__: null, 3391cb0ef41Sopenharmony_ci ...consolePropAttributes, 3401cb0ef41Sopenharmony_ci value: function(args) { 3411cb0ef41Sopenharmony_ci const opts = this[kGetInspectOptions](this._stdout); 3421cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(args, opts); 3431cb0ef41Sopenharmony_ci return ReflectApply(formatWithOptions, null, args); 3441cb0ef41Sopenharmony_ci }, 3451cb0ef41Sopenharmony_ci }, 3461cb0ef41Sopenharmony_ci [kFormatForStderr]: { 3471cb0ef41Sopenharmony_ci __proto__: null, 3481cb0ef41Sopenharmony_ci ...consolePropAttributes, 3491cb0ef41Sopenharmony_ci value: function(args) { 3501cb0ef41Sopenharmony_ci const opts = this[kGetInspectOptions](this._stderr); 3511cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(args, opts); 3521cb0ef41Sopenharmony_ci return ReflectApply(formatWithOptions, null, args); 3531cb0ef41Sopenharmony_ci }, 3541cb0ef41Sopenharmony_ci }, 3551cb0ef41Sopenharmony_ci}); 3561cb0ef41Sopenharmony_ci 3571cb0ef41Sopenharmony_ci// Make a function that can serve as the callback passed to `stream.write()`. 3581cb0ef41Sopenharmony_cifunction createWriteErrorHandler(instance, streamSymbol) { 3591cb0ef41Sopenharmony_ci return (err) => { 3601cb0ef41Sopenharmony_ci // This conditional evaluates to true if and only if there was an error 3611cb0ef41Sopenharmony_ci // that was not already emitted (which happens when the _write callback 3621cb0ef41Sopenharmony_ci // is invoked asynchronously). 3631cb0ef41Sopenharmony_ci const stream = streamSymbol === kUseStdout ? 3641cb0ef41Sopenharmony_ci instance._stdout : instance._stderr; 3651cb0ef41Sopenharmony_ci if (err !== null && !stream._writableState.errorEmitted) { 3661cb0ef41Sopenharmony_ci // If there was an error, it will be emitted on `stream` as 3671cb0ef41Sopenharmony_ci // an `error` event. Adding a `once` listener will keep that error 3681cb0ef41Sopenharmony_ci // from becoming an uncaught exception, but since the handler is 3691cb0ef41Sopenharmony_ci // removed after the event, non-console.* writes won't be affected. 3701cb0ef41Sopenharmony_ci // we are only adding noop if there is no one else listening for 'error' 3711cb0ef41Sopenharmony_ci if (stream.listenerCount('error') === 0) { 3721cb0ef41Sopenharmony_ci stream.once('error', noop); 3731cb0ef41Sopenharmony_ci } 3741cb0ef41Sopenharmony_ci } 3751cb0ef41Sopenharmony_ci }; 3761cb0ef41Sopenharmony_ci} 3771cb0ef41Sopenharmony_ci 3781cb0ef41Sopenharmony_ciconst consoleMethods = { 3791cb0ef41Sopenharmony_ci log(...args) { 3801cb0ef41Sopenharmony_ci this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); 3811cb0ef41Sopenharmony_ci }, 3821cb0ef41Sopenharmony_ci 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci warn(...args) { 3851cb0ef41Sopenharmony_ci this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args)); 3861cb0ef41Sopenharmony_ci }, 3871cb0ef41Sopenharmony_ci 3881cb0ef41Sopenharmony_ci 3891cb0ef41Sopenharmony_ci dir(object, options) { 3901cb0ef41Sopenharmony_ci this[kWriteToConsole](kUseStdout, inspect(object, { 3911cb0ef41Sopenharmony_ci customInspect: false, 3921cb0ef41Sopenharmony_ci ...this[kGetInspectOptions](this._stdout), 3931cb0ef41Sopenharmony_ci ...options, 3941cb0ef41Sopenharmony_ci })); 3951cb0ef41Sopenharmony_ci }, 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ci time(label = 'default') { 3981cb0ef41Sopenharmony_ci // Coerces everything other than Symbol to a string 3991cb0ef41Sopenharmony_ci label = `${label}`; 4001cb0ef41Sopenharmony_ci if (this._times.has(label)) { 4011cb0ef41Sopenharmony_ci process.emitWarning(`Label '${label}' already exists for console.time()`); 4021cb0ef41Sopenharmony_ci return; 4031cb0ef41Sopenharmony_ci } 4041cb0ef41Sopenharmony_ci trace(kTraceBegin, kTraceConsoleCategory, `time::${label}`, 0); 4051cb0ef41Sopenharmony_ci this._times.set(label, process.hrtime()); 4061cb0ef41Sopenharmony_ci }, 4071cb0ef41Sopenharmony_ci 4081cb0ef41Sopenharmony_ci timeEnd(label = 'default') { 4091cb0ef41Sopenharmony_ci // Coerces everything other than Symbol to a string 4101cb0ef41Sopenharmony_ci label = `${label}`; 4111cb0ef41Sopenharmony_ci const found = timeLogImpl(this, 'timeEnd', label); 4121cb0ef41Sopenharmony_ci trace(kTraceEnd, kTraceConsoleCategory, `time::${label}`, 0); 4131cb0ef41Sopenharmony_ci if (found) { 4141cb0ef41Sopenharmony_ci this._times.delete(label); 4151cb0ef41Sopenharmony_ci } 4161cb0ef41Sopenharmony_ci }, 4171cb0ef41Sopenharmony_ci 4181cb0ef41Sopenharmony_ci timeLog(label = 'default', ...data) { 4191cb0ef41Sopenharmony_ci // Coerces everything other than Symbol to a string 4201cb0ef41Sopenharmony_ci label = `${label}`; 4211cb0ef41Sopenharmony_ci timeLogImpl(this, 'timeLog', label, data); 4221cb0ef41Sopenharmony_ci trace(kTraceInstant, kTraceConsoleCategory, `time::${label}`, 0); 4231cb0ef41Sopenharmony_ci }, 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_ci trace: function trace(...args) { 4261cb0ef41Sopenharmony_ci const err = { 4271cb0ef41Sopenharmony_ci name: 'Trace', 4281cb0ef41Sopenharmony_ci message: this[kFormatForStderr](args), 4291cb0ef41Sopenharmony_ci }; 4301cb0ef41Sopenharmony_ci ErrorCaptureStackTrace(err, trace); 4311cb0ef41Sopenharmony_ci this.error(err.stack); 4321cb0ef41Sopenharmony_ci }, 4331cb0ef41Sopenharmony_ci 4341cb0ef41Sopenharmony_ci assert(expression, ...args) { 4351cb0ef41Sopenharmony_ci if (!expression) { 4361cb0ef41Sopenharmony_ci args[0] = `Assertion failed${args.length === 0 ? '' : `: ${args[0]}`}`; 4371cb0ef41Sopenharmony_ci // The arguments will be formatted in warn() again 4381cb0ef41Sopenharmony_ci ReflectApply(this.warn, this, args); 4391cb0ef41Sopenharmony_ci } 4401cb0ef41Sopenharmony_ci }, 4411cb0ef41Sopenharmony_ci 4421cb0ef41Sopenharmony_ci // Defined by: https://console.spec.whatwg.org/#clear 4431cb0ef41Sopenharmony_ci clear() { 4441cb0ef41Sopenharmony_ci // It only makes sense to clear if _stdout is a TTY. 4451cb0ef41Sopenharmony_ci // Otherwise, do nothing. 4461cb0ef41Sopenharmony_ci if (this._stdout.isTTY && process.env.TERM !== 'dumb') { 4471cb0ef41Sopenharmony_ci // The require is here intentionally to avoid readline being 4481cb0ef41Sopenharmony_ci // required too early when console is first loaded. 4491cb0ef41Sopenharmony_ci const { 4501cb0ef41Sopenharmony_ci cursorTo, 4511cb0ef41Sopenharmony_ci clearScreenDown, 4521cb0ef41Sopenharmony_ci } = require('internal/readline/callbacks'); 4531cb0ef41Sopenharmony_ci cursorTo(this._stdout, 0, 0); 4541cb0ef41Sopenharmony_ci clearScreenDown(this._stdout); 4551cb0ef41Sopenharmony_ci } 4561cb0ef41Sopenharmony_ci }, 4571cb0ef41Sopenharmony_ci 4581cb0ef41Sopenharmony_ci // Defined by: https://console.spec.whatwg.org/#count 4591cb0ef41Sopenharmony_ci count(label = 'default') { 4601cb0ef41Sopenharmony_ci // Ensures that label is a string, and only things that can be 4611cb0ef41Sopenharmony_ci // coerced to strings. e.g. Symbol is not allowed 4621cb0ef41Sopenharmony_ci label = `${label}`; 4631cb0ef41Sopenharmony_ci const counts = this[kCounts]; 4641cb0ef41Sopenharmony_ci let count = counts.get(label); 4651cb0ef41Sopenharmony_ci if (count === undefined) 4661cb0ef41Sopenharmony_ci count = 1; 4671cb0ef41Sopenharmony_ci else 4681cb0ef41Sopenharmony_ci count++; 4691cb0ef41Sopenharmony_ci counts.set(label, count); 4701cb0ef41Sopenharmony_ci trace(kTraceCount, kTraceConsoleCategory, `count::${label}`, 0, count); 4711cb0ef41Sopenharmony_ci this.log(`${label}: ${count}`); 4721cb0ef41Sopenharmony_ci }, 4731cb0ef41Sopenharmony_ci 4741cb0ef41Sopenharmony_ci // Defined by: https://console.spec.whatwg.org/#countreset 4751cb0ef41Sopenharmony_ci countReset(label = 'default') { 4761cb0ef41Sopenharmony_ci const counts = this[kCounts]; 4771cb0ef41Sopenharmony_ci if (!counts.has(label)) { 4781cb0ef41Sopenharmony_ci process.emitWarning(`Count for '${label}' does not exist`); 4791cb0ef41Sopenharmony_ci return; 4801cb0ef41Sopenharmony_ci } 4811cb0ef41Sopenharmony_ci trace(kTraceCount, kTraceConsoleCategory, `count::${label}`, 0, 0); 4821cb0ef41Sopenharmony_ci counts.delete(`${label}`); 4831cb0ef41Sopenharmony_ci }, 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ci group(...data) { 4861cb0ef41Sopenharmony_ci if (data.length > 0) { 4871cb0ef41Sopenharmony_ci ReflectApply(this.log, this, data); 4881cb0ef41Sopenharmony_ci } 4891cb0ef41Sopenharmony_ci this[kGroupIndent] += 4901cb0ef41Sopenharmony_ci StringPrototypeRepeat(' ', this[kGroupIndentationWidth]); 4911cb0ef41Sopenharmony_ci }, 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci groupEnd() { 4941cb0ef41Sopenharmony_ci this[kGroupIndent] = StringPrototypeSlice( 4951cb0ef41Sopenharmony_ci this[kGroupIndent], 4961cb0ef41Sopenharmony_ci 0, 4971cb0ef41Sopenharmony_ci this[kGroupIndent].length - this[kGroupIndentationWidth], 4981cb0ef41Sopenharmony_ci ); 4991cb0ef41Sopenharmony_ci }, 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci // https://console.spec.whatwg.org/#table 5021cb0ef41Sopenharmony_ci table(tabularData, properties) { 5031cb0ef41Sopenharmony_ci if (properties !== undefined) 5041cb0ef41Sopenharmony_ci validateArray(properties, 'properties'); 5051cb0ef41Sopenharmony_ci 5061cb0ef41Sopenharmony_ci if (tabularData === null || typeof tabularData !== 'object') 5071cb0ef41Sopenharmony_ci return this.log(tabularData); 5081cb0ef41Sopenharmony_ci 5091cb0ef41Sopenharmony_ci cliTable ??= require('internal/cli_table'); 5101cb0ef41Sopenharmony_ci const final = (k, v) => this.log(cliTable(k, v)); 5111cb0ef41Sopenharmony_ci 5121cb0ef41Sopenharmony_ci const _inspect = (v) => { 5131cb0ef41Sopenharmony_ci const depth = v !== null && 5141cb0ef41Sopenharmony_ci typeof v === 'object' && 5151cb0ef41Sopenharmony_ci !isArray(v) && 5161cb0ef41Sopenharmony_ci ObjectKeys(v).length > 2 ? -1 : 0; 5171cb0ef41Sopenharmony_ci const opt = { 5181cb0ef41Sopenharmony_ci depth, 5191cb0ef41Sopenharmony_ci maxArrayLength: 3, 5201cb0ef41Sopenharmony_ci breakLength: Infinity, 5211cb0ef41Sopenharmony_ci ...this[kGetInspectOptions](this._stdout), 5221cb0ef41Sopenharmony_ci }; 5231cb0ef41Sopenharmony_ci return inspect(v, opt); 5241cb0ef41Sopenharmony_ci }; 5251cb0ef41Sopenharmony_ci const getIndexArray = (length) => ArrayFrom( 5261cb0ef41Sopenharmony_ci { length }, (_, i) => _inspect(i)); 5271cb0ef41Sopenharmony_ci 5281cb0ef41Sopenharmony_ci const mapIter = isMapIterator(tabularData); 5291cb0ef41Sopenharmony_ci let isKeyValue = false; 5301cb0ef41Sopenharmony_ci let i = 0; 5311cb0ef41Sopenharmony_ci if (mapIter) { 5321cb0ef41Sopenharmony_ci const res = previewEntries(tabularData, true); 5331cb0ef41Sopenharmony_ci tabularData = res[0]; 5341cb0ef41Sopenharmony_ci isKeyValue = res[1]; 5351cb0ef41Sopenharmony_ci } 5361cb0ef41Sopenharmony_ci 5371cb0ef41Sopenharmony_ci if (isKeyValue || isMap(tabularData)) { 5381cb0ef41Sopenharmony_ci const keys = []; 5391cb0ef41Sopenharmony_ci const values = []; 5401cb0ef41Sopenharmony_ci let length = 0; 5411cb0ef41Sopenharmony_ci if (mapIter) { 5421cb0ef41Sopenharmony_ci for (; i < tabularData.length / 2; ++i) { 5431cb0ef41Sopenharmony_ci ArrayPrototypePush(keys, _inspect(tabularData[i * 2])); 5441cb0ef41Sopenharmony_ci ArrayPrototypePush(values, _inspect(tabularData[i * 2 + 1])); 5451cb0ef41Sopenharmony_ci length++; 5461cb0ef41Sopenharmony_ci } 5471cb0ef41Sopenharmony_ci } else { 5481cb0ef41Sopenharmony_ci for (const { 0: k, 1: v } of tabularData) { 5491cb0ef41Sopenharmony_ci ArrayPrototypePush(keys, _inspect(k)); 5501cb0ef41Sopenharmony_ci ArrayPrototypePush(values, _inspect(v)); 5511cb0ef41Sopenharmony_ci length++; 5521cb0ef41Sopenharmony_ci } 5531cb0ef41Sopenharmony_ci } 5541cb0ef41Sopenharmony_ci return final([ 5551cb0ef41Sopenharmony_ci iterKey, keyKey, valuesKey, 5561cb0ef41Sopenharmony_ci ], [ 5571cb0ef41Sopenharmony_ci getIndexArray(length), 5581cb0ef41Sopenharmony_ci keys, 5591cb0ef41Sopenharmony_ci values, 5601cb0ef41Sopenharmony_ci ]); 5611cb0ef41Sopenharmony_ci } 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci const setIter = isSetIterator(tabularData); 5641cb0ef41Sopenharmony_ci if (setIter) 5651cb0ef41Sopenharmony_ci tabularData = previewEntries(tabularData); 5661cb0ef41Sopenharmony_ci 5671cb0ef41Sopenharmony_ci const setlike = setIter || mapIter || isSet(tabularData); 5681cb0ef41Sopenharmony_ci if (setlike) { 5691cb0ef41Sopenharmony_ci const values = []; 5701cb0ef41Sopenharmony_ci let length = 0; 5711cb0ef41Sopenharmony_ci for (const v of tabularData) { 5721cb0ef41Sopenharmony_ci ArrayPrototypePush(values, _inspect(v)); 5731cb0ef41Sopenharmony_ci length++; 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci return final([iterKey, valuesKey], [getIndexArray(length), values]); 5761cb0ef41Sopenharmony_ci } 5771cb0ef41Sopenharmony_ci 5781cb0ef41Sopenharmony_ci const map = ObjectCreate(null); 5791cb0ef41Sopenharmony_ci let hasPrimitives = false; 5801cb0ef41Sopenharmony_ci const valuesKeyArray = []; 5811cb0ef41Sopenharmony_ci const indexKeyArray = ObjectKeys(tabularData); 5821cb0ef41Sopenharmony_ci 5831cb0ef41Sopenharmony_ci for (; i < indexKeyArray.length; i++) { 5841cb0ef41Sopenharmony_ci const item = tabularData[indexKeyArray[i]]; 5851cb0ef41Sopenharmony_ci const primitive = item === null || 5861cb0ef41Sopenharmony_ci (typeof item !== 'function' && typeof item !== 'object'); 5871cb0ef41Sopenharmony_ci if (properties === undefined && primitive) { 5881cb0ef41Sopenharmony_ci hasPrimitives = true; 5891cb0ef41Sopenharmony_ci valuesKeyArray[i] = _inspect(item); 5901cb0ef41Sopenharmony_ci } else { 5911cb0ef41Sopenharmony_ci const keys = properties || ObjectKeys(item); 5921cb0ef41Sopenharmony_ci for (const key of keys) { 5931cb0ef41Sopenharmony_ci map[key] ??= []; 5941cb0ef41Sopenharmony_ci if ((primitive && properties) || 5951cb0ef41Sopenharmony_ci !ObjectPrototypeHasOwnProperty(item, key)) 5961cb0ef41Sopenharmony_ci map[key][i] = ''; 5971cb0ef41Sopenharmony_ci else 5981cb0ef41Sopenharmony_ci map[key][i] = _inspect(item[key]); 5991cb0ef41Sopenharmony_ci } 6001cb0ef41Sopenharmony_ci } 6011cb0ef41Sopenharmony_ci } 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci const keys = ObjectKeys(map); 6041cb0ef41Sopenharmony_ci const values = ObjectValues(map); 6051cb0ef41Sopenharmony_ci if (hasPrimitives) { 6061cb0ef41Sopenharmony_ci ArrayPrototypePush(keys, valuesKey); 6071cb0ef41Sopenharmony_ci ArrayPrototypePush(values, valuesKeyArray); 6081cb0ef41Sopenharmony_ci } 6091cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(keys, indexKey); 6101cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(values, indexKeyArray); 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci return final(keys, values); 6131cb0ef41Sopenharmony_ci }, 6141cb0ef41Sopenharmony_ci}; 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci// Returns true if label was found 6171cb0ef41Sopenharmony_cifunction timeLogImpl(self, name, label, data) { 6181cb0ef41Sopenharmony_ci const time = self._times.get(label); 6191cb0ef41Sopenharmony_ci if (time === undefined) { 6201cb0ef41Sopenharmony_ci process.emitWarning(`No such label '${label}' for console.${name}()`); 6211cb0ef41Sopenharmony_ci return false; 6221cb0ef41Sopenharmony_ci } 6231cb0ef41Sopenharmony_ci const duration = process.hrtime(time); 6241cb0ef41Sopenharmony_ci const ms = duration[0] * 1000 + duration[1] / 1e6; 6251cb0ef41Sopenharmony_ci 6261cb0ef41Sopenharmony_ci const formatted = formatTime(ms); 6271cb0ef41Sopenharmony_ci 6281cb0ef41Sopenharmony_ci if (data === undefined) { 6291cb0ef41Sopenharmony_ci self.log('%s: %s', label, formatted); 6301cb0ef41Sopenharmony_ci } else { 6311cb0ef41Sopenharmony_ci self.log('%s: %s', label, formatted, ...new SafeArrayIterator(data)); 6321cb0ef41Sopenharmony_ci } 6331cb0ef41Sopenharmony_ci return true; 6341cb0ef41Sopenharmony_ci} 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_cifunction pad(value) { 6371cb0ef41Sopenharmony_ci return StringPrototypePadStart(`${value}`, 2, '0'); 6381cb0ef41Sopenharmony_ci} 6391cb0ef41Sopenharmony_ci 6401cb0ef41Sopenharmony_cifunction formatTime(ms) { 6411cb0ef41Sopenharmony_ci let hours = 0; 6421cb0ef41Sopenharmony_ci let minutes = 0; 6431cb0ef41Sopenharmony_ci let seconds = 0; 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci if (ms >= kSecond) { 6461cb0ef41Sopenharmony_ci if (ms >= kMinute) { 6471cb0ef41Sopenharmony_ci if (ms >= kHour) { 6481cb0ef41Sopenharmony_ci hours = MathFloor(ms / kHour); 6491cb0ef41Sopenharmony_ci ms = ms % kHour; 6501cb0ef41Sopenharmony_ci } 6511cb0ef41Sopenharmony_ci minutes = MathFloor(ms / kMinute); 6521cb0ef41Sopenharmony_ci ms = ms % kMinute; 6531cb0ef41Sopenharmony_ci } 6541cb0ef41Sopenharmony_ci seconds = ms / kSecond; 6551cb0ef41Sopenharmony_ci } 6561cb0ef41Sopenharmony_ci 6571cb0ef41Sopenharmony_ci if (hours !== 0 || minutes !== 0) { 6581cb0ef41Sopenharmony_ci ({ 0: seconds, 1: ms } = StringPrototypeSplit( 6591cb0ef41Sopenharmony_ci NumberPrototypeToFixed(seconds, 3), 6601cb0ef41Sopenharmony_ci '.', 6611cb0ef41Sopenharmony_ci )); 6621cb0ef41Sopenharmony_ci const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes; 6631cb0ef41Sopenharmony_ci return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`; 6641cb0ef41Sopenharmony_ci } 6651cb0ef41Sopenharmony_ci 6661cb0ef41Sopenharmony_ci if (seconds !== 0) { 6671cb0ef41Sopenharmony_ci return `${NumberPrototypeToFixed(seconds, 3)}s`; 6681cb0ef41Sopenharmony_ci } 6691cb0ef41Sopenharmony_ci 6701cb0ef41Sopenharmony_ci return `${Number(NumberPrototypeToFixed(ms, 3))}ms`; 6711cb0ef41Sopenharmony_ci} 6721cb0ef41Sopenharmony_ci 6731cb0ef41Sopenharmony_ciconst keyKey = 'Key'; 6741cb0ef41Sopenharmony_ciconst valuesKey = 'Values'; 6751cb0ef41Sopenharmony_ciconst indexKey = '(index)'; 6761cb0ef41Sopenharmony_ciconst iterKey = '(iteration index)'; 6771cb0ef41Sopenharmony_ci 6781cb0ef41Sopenharmony_ciconst isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v); 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_cifunction noop() {} 6811cb0ef41Sopenharmony_ci 6821cb0ef41Sopenharmony_cifor (const method of ReflectOwnKeys(consoleMethods)) 6831cb0ef41Sopenharmony_ci Console.prototype[method] = consoleMethods[method]; 6841cb0ef41Sopenharmony_ci 6851cb0ef41Sopenharmony_ciConsole.prototype.debug = Console.prototype.log; 6861cb0ef41Sopenharmony_ciConsole.prototype.info = Console.prototype.log; 6871cb0ef41Sopenharmony_ciConsole.prototype.dirxml = Console.prototype.log; 6881cb0ef41Sopenharmony_ciConsole.prototype.error = Console.prototype.warn; 6891cb0ef41Sopenharmony_ciConsole.prototype.groupCollapsed = Console.prototype.group; 6901cb0ef41Sopenharmony_ci 6911cb0ef41Sopenharmony_cifunction initializeGlobalConsole(globalConsole) { 6921cb0ef41Sopenharmony_ci globalConsole[kBindStreamsLazy](process); 6931cb0ef41Sopenharmony_ci globalConsole[kBindProperties](true, 'auto'); 6941cb0ef41Sopenharmony_ci 6951cb0ef41Sopenharmony_ci const { 6961cb0ef41Sopenharmony_ci namespace: { 6971cb0ef41Sopenharmony_ci addSerializeCallback, 6981cb0ef41Sopenharmony_ci isBuildingSnapshot, 6991cb0ef41Sopenharmony_ci }, 7001cb0ef41Sopenharmony_ci } = require('internal/v8/startup_snapshot'); 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ci if (!internalBinding('config').hasInspector || !isBuildingSnapshot()) { 7031cb0ef41Sopenharmony_ci return; 7041cb0ef41Sopenharmony_ci } 7051cb0ef41Sopenharmony_ci const { console: consoleFromVM } = internalBinding('inspector'); 7061cb0ef41Sopenharmony_ci const nodeConsoleKeys = ObjectKeys(Console.prototype); 7071cb0ef41Sopenharmony_ci const vmConsoleKeys = ObjectKeys(consoleFromVM); 7081cb0ef41Sopenharmony_ci const originalKeys = new SafeSet(vmConsoleKeys.concat(nodeConsoleKeys)); 7091cb0ef41Sopenharmony_ci const inspectorConsoleKeys = new SafeSet(); 7101cb0ef41Sopenharmony_ci for (const key of ObjectKeys(globalConsole)) { 7111cb0ef41Sopenharmony_ci if (!originalKeys.has(key)) { 7121cb0ef41Sopenharmony_ci inspectorConsoleKeys.add(key); 7131cb0ef41Sopenharmony_ci } 7141cb0ef41Sopenharmony_ci } 7151cb0ef41Sopenharmony_ci // During deserialization these should be reinstalled to console by 7161cb0ef41Sopenharmony_ci // V8 when the inspector client is created. 7171cb0ef41Sopenharmony_ci addSerializeCallback(() => { 7181cb0ef41Sopenharmony_ci for (const key of inspectorConsoleKeys) { 7191cb0ef41Sopenharmony_ci globalConsole[key] = undefined; 7201cb0ef41Sopenharmony_ci } 7211cb0ef41Sopenharmony_ci }); 7221cb0ef41Sopenharmony_ci} 7231cb0ef41Sopenharmony_ci 7241cb0ef41Sopenharmony_cimodule.exports = { 7251cb0ef41Sopenharmony_ci Console, 7261cb0ef41Sopenharmony_ci kBindStreamsLazy, 7271cb0ef41Sopenharmony_ci kBindProperties, 7281cb0ef41Sopenharmony_ci initializeGlobalConsole, 7291cb0ef41Sopenharmony_ci formatTime, // exported for tests 7301cb0ef41Sopenharmony_ci}; 731