11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci Array, 51cb0ef41Sopenharmony_ci ArrayIsArray, 61cb0ef41Sopenharmony_ci ArrayPrototypeFilter, 71cb0ef41Sopenharmony_ci ArrayPrototypeForEach, 81cb0ef41Sopenharmony_ci ArrayPrototypeIncludes, 91cb0ef41Sopenharmony_ci ArrayPrototypeIndexOf, 101cb0ef41Sopenharmony_ci ArrayPrototypeJoin, 111cb0ef41Sopenharmony_ci ArrayPrototypeMap, 121cb0ef41Sopenharmony_ci ArrayPrototypePop, 131cb0ef41Sopenharmony_ci ArrayPrototypePush, 141cb0ef41Sopenharmony_ci ArrayPrototypePushApply, 151cb0ef41Sopenharmony_ci ArrayPrototypeSlice, 161cb0ef41Sopenharmony_ci ArrayPrototypeSplice, 171cb0ef41Sopenharmony_ci ArrayPrototypeSort, 181cb0ef41Sopenharmony_ci ArrayPrototypeUnshift, 191cb0ef41Sopenharmony_ci BigIntPrototypeValueOf, 201cb0ef41Sopenharmony_ci BooleanPrototypeValueOf, 211cb0ef41Sopenharmony_ci DatePrototypeGetTime, 221cb0ef41Sopenharmony_ci DatePrototypeToISOString, 231cb0ef41Sopenharmony_ci DatePrototypeToString, 241cb0ef41Sopenharmony_ci ErrorPrototypeToString, 251cb0ef41Sopenharmony_ci FunctionPrototypeBind, 261cb0ef41Sopenharmony_ci FunctionPrototypeCall, 271cb0ef41Sopenharmony_ci FunctionPrototypeToString, 281cb0ef41Sopenharmony_ci JSONStringify, 291cb0ef41Sopenharmony_ci MapPrototypeGetSize, 301cb0ef41Sopenharmony_ci MapPrototypeEntries, 311cb0ef41Sopenharmony_ci MathFloor, 321cb0ef41Sopenharmony_ci MathMax, 331cb0ef41Sopenharmony_ci MathMin, 341cb0ef41Sopenharmony_ci MathRound, 351cb0ef41Sopenharmony_ci MathSqrt, 361cb0ef41Sopenharmony_ci MathTrunc, 371cb0ef41Sopenharmony_ci Number, 381cb0ef41Sopenharmony_ci NumberIsFinite, 391cb0ef41Sopenharmony_ci NumberIsNaN, 401cb0ef41Sopenharmony_ci NumberParseFloat, 411cb0ef41Sopenharmony_ci NumberParseInt, 421cb0ef41Sopenharmony_ci NumberPrototypeToString, 431cb0ef41Sopenharmony_ci NumberPrototypeValueOf, 441cb0ef41Sopenharmony_ci Object, 451cb0ef41Sopenharmony_ci ObjectAssign, 461cb0ef41Sopenharmony_ci ObjectCreate, 471cb0ef41Sopenharmony_ci ObjectDefineProperty, 481cb0ef41Sopenharmony_ci ObjectGetOwnPropertyDescriptor, 491cb0ef41Sopenharmony_ci ObjectGetOwnPropertyNames, 501cb0ef41Sopenharmony_ci ObjectGetOwnPropertySymbols, 511cb0ef41Sopenharmony_ci ObjectGetPrototypeOf, 521cb0ef41Sopenharmony_ci ObjectIs, 531cb0ef41Sopenharmony_ci ObjectKeys, 541cb0ef41Sopenharmony_ci ObjectPrototypeHasOwnProperty, 551cb0ef41Sopenharmony_ci ObjectPrototypePropertyIsEnumerable, 561cb0ef41Sopenharmony_ci ObjectSeal, 571cb0ef41Sopenharmony_ci ObjectSetPrototypeOf, 581cb0ef41Sopenharmony_ci ReflectApply, 591cb0ef41Sopenharmony_ci ReflectOwnKeys, 601cb0ef41Sopenharmony_ci RegExp, 611cb0ef41Sopenharmony_ci RegExpPrototypeExec, 621cb0ef41Sopenharmony_ci RegExpPrototypeSymbolReplace, 631cb0ef41Sopenharmony_ci RegExpPrototypeSymbolSplit, 641cb0ef41Sopenharmony_ci RegExpPrototypeToString, 651cb0ef41Sopenharmony_ci SafeStringIterator, 661cb0ef41Sopenharmony_ci SafeMap, 671cb0ef41Sopenharmony_ci SafeSet, 681cb0ef41Sopenharmony_ci SetPrototypeGetSize, 691cb0ef41Sopenharmony_ci SetPrototypeValues, 701cb0ef41Sopenharmony_ci String, 711cb0ef41Sopenharmony_ci StringPrototypeCharCodeAt, 721cb0ef41Sopenharmony_ci StringPrototypeCodePointAt, 731cb0ef41Sopenharmony_ci StringPrototypeIncludes, 741cb0ef41Sopenharmony_ci StringPrototypeIndexOf, 751cb0ef41Sopenharmony_ci StringPrototypeLastIndexOf, 761cb0ef41Sopenharmony_ci StringPrototypeNormalize, 771cb0ef41Sopenharmony_ci StringPrototypePadEnd, 781cb0ef41Sopenharmony_ci StringPrototypePadStart, 791cb0ef41Sopenharmony_ci StringPrototypeRepeat, 801cb0ef41Sopenharmony_ci StringPrototypeReplaceAll, 811cb0ef41Sopenharmony_ci StringPrototypeSlice, 821cb0ef41Sopenharmony_ci StringPrototypeSplit, 831cb0ef41Sopenharmony_ci StringPrototypeEndsWith, 841cb0ef41Sopenharmony_ci StringPrototypeStartsWith, 851cb0ef41Sopenharmony_ci StringPrototypeToLowerCase, 861cb0ef41Sopenharmony_ci StringPrototypeTrim, 871cb0ef41Sopenharmony_ci StringPrototypeValueOf, 881cb0ef41Sopenharmony_ci SymbolPrototypeToString, 891cb0ef41Sopenharmony_ci SymbolPrototypeValueOf, 901cb0ef41Sopenharmony_ci SymbolIterator, 911cb0ef41Sopenharmony_ci SymbolToStringTag, 921cb0ef41Sopenharmony_ci TypedArrayPrototypeGetLength, 931cb0ef41Sopenharmony_ci TypedArrayPrototypeGetSymbolToStringTag, 941cb0ef41Sopenharmony_ci Uint8Array, 951cb0ef41Sopenharmony_ci globalThis, 961cb0ef41Sopenharmony_ci uncurryThis, 971cb0ef41Sopenharmony_ci} = primordials; 981cb0ef41Sopenharmony_ci 991cb0ef41Sopenharmony_ciconst { 1001cb0ef41Sopenharmony_ci constants: { 1011cb0ef41Sopenharmony_ci ALL_PROPERTIES, 1021cb0ef41Sopenharmony_ci ONLY_ENUMERABLE, 1031cb0ef41Sopenharmony_ci kPending, 1041cb0ef41Sopenharmony_ci kRejected, 1051cb0ef41Sopenharmony_ci }, 1061cb0ef41Sopenharmony_ci getOwnNonIndexProperties, 1071cb0ef41Sopenharmony_ci getPromiseDetails, 1081cb0ef41Sopenharmony_ci getProxyDetails, 1091cb0ef41Sopenharmony_ci previewEntries, 1101cb0ef41Sopenharmony_ci getConstructorName: internalGetConstructorName, 1111cb0ef41Sopenharmony_ci getExternalValue, 1121cb0ef41Sopenharmony_ci} = internalBinding('util'); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ciconst { 1151cb0ef41Sopenharmony_ci customInspectSymbol, 1161cb0ef41Sopenharmony_ci isError, 1171cb0ef41Sopenharmony_ci join, 1181cb0ef41Sopenharmony_ci removeColors, 1191cb0ef41Sopenharmony_ci} = require('internal/util'); 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ciconst { 1221cb0ef41Sopenharmony_ci isStackOverflowError, 1231cb0ef41Sopenharmony_ci} = require('internal/errors'); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ciconst { 1261cb0ef41Sopenharmony_ci isAsyncFunction, 1271cb0ef41Sopenharmony_ci isGeneratorFunction, 1281cb0ef41Sopenharmony_ci isAnyArrayBuffer, 1291cb0ef41Sopenharmony_ci isArrayBuffer, 1301cb0ef41Sopenharmony_ci isArgumentsObject, 1311cb0ef41Sopenharmony_ci isBoxedPrimitive, 1321cb0ef41Sopenharmony_ci isDataView, 1331cb0ef41Sopenharmony_ci isExternal, 1341cb0ef41Sopenharmony_ci isMap, 1351cb0ef41Sopenharmony_ci isMapIterator, 1361cb0ef41Sopenharmony_ci isModuleNamespaceObject, 1371cb0ef41Sopenharmony_ci isNativeError, 1381cb0ef41Sopenharmony_ci isPromise, 1391cb0ef41Sopenharmony_ci isSet, 1401cb0ef41Sopenharmony_ci isSetIterator, 1411cb0ef41Sopenharmony_ci isWeakMap, 1421cb0ef41Sopenharmony_ci isWeakSet, 1431cb0ef41Sopenharmony_ci isRegExp, 1441cb0ef41Sopenharmony_ci isDate, 1451cb0ef41Sopenharmony_ci isTypedArray, 1461cb0ef41Sopenharmony_ci isStringObject, 1471cb0ef41Sopenharmony_ci isNumberObject, 1481cb0ef41Sopenharmony_ci isBooleanObject, 1491cb0ef41Sopenharmony_ci isBigIntObject, 1501cb0ef41Sopenharmony_ci} = require('internal/util/types'); 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ciconst assert = require('internal/assert'); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ciconst { BuiltinModule } = require('internal/bootstrap/realm'); 1551cb0ef41Sopenharmony_ciconst { 1561cb0ef41Sopenharmony_ci validateObject, 1571cb0ef41Sopenharmony_ci validateString, 1581cb0ef41Sopenharmony_ci} = require('internal/validators'); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_cilet hexSlice; 1611cb0ef41Sopenharmony_cilet internalUrl; 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_cifunction pathToFileUrlHref(filepath) { 1641cb0ef41Sopenharmony_ci internalUrl ??= require('internal/url'); 1651cb0ef41Sopenharmony_ci return internalUrl.pathToFileURL(filepath).href; 1661cb0ef41Sopenharmony_ci} 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ciconst builtInObjects = new SafeSet( 1691cb0ef41Sopenharmony_ci ArrayPrototypeFilter( 1701cb0ef41Sopenharmony_ci ObjectGetOwnPropertyNames(globalThis), 1711cb0ef41Sopenharmony_ci (e) => RegExpPrototypeExec(/^[A-Z][a-zA-Z0-9]+$/, e) !== null, 1721cb0ef41Sopenharmony_ci ), 1731cb0ef41Sopenharmony_ci); 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci// https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot 1761cb0ef41Sopenharmony_ciconst isUndetectableObject = (v) => typeof v === 'undefined' && v !== undefined; 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci// These options must stay in sync with `getUserOptions`. So if any option will 1791cb0ef41Sopenharmony_ci// be added or removed, `getUserOptions` must also be updated accordingly. 1801cb0ef41Sopenharmony_ciconst inspectDefaultOptions = ObjectSeal({ 1811cb0ef41Sopenharmony_ci showHidden: false, 1821cb0ef41Sopenharmony_ci depth: 2, 1831cb0ef41Sopenharmony_ci colors: false, 1841cb0ef41Sopenharmony_ci customInspect: true, 1851cb0ef41Sopenharmony_ci showProxy: false, 1861cb0ef41Sopenharmony_ci maxArrayLength: 100, 1871cb0ef41Sopenharmony_ci maxStringLength: 10000, 1881cb0ef41Sopenharmony_ci breakLength: 80, 1891cb0ef41Sopenharmony_ci compact: 3, 1901cb0ef41Sopenharmony_ci sorted: false, 1911cb0ef41Sopenharmony_ci getters: false, 1921cb0ef41Sopenharmony_ci numericSeparator: false, 1931cb0ef41Sopenharmony_ci}); 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ciconst kObjectType = 0; 1961cb0ef41Sopenharmony_ciconst kArrayType = 1; 1971cb0ef41Sopenharmony_ciconst kArrayExtrasType = 2; 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci/* eslint-disable no-control-regex */ 2001cb0ef41Sopenharmony_ciconst strEscapeSequencesRegExp = /[\x00-\x1f\x27\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/; 2011cb0ef41Sopenharmony_ciconst strEscapeSequencesReplacer = /[\x00-\x1f\x27\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g; 2021cb0ef41Sopenharmony_ciconst strEscapeSequencesRegExpSingle = /[\x00-\x1f\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/; 2031cb0ef41Sopenharmony_ciconst strEscapeSequencesReplacerSingle = /[\x00-\x1f\x5c\x7f-\x9f]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g; 2041cb0ef41Sopenharmony_ci/* eslint-enable no-control-regex */ 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ciconst keyStrRegExp = /^[a-zA-Z_][a-zA-Z_0-9]*$/; 2071cb0ef41Sopenharmony_ciconst numberRegExp = /^(0|[1-9][0-9]*)$/; 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ciconst coreModuleRegExp = /^ {4}at (?:[^/\\(]+ \(|)node:(.+):\d+:\d+\)?$/; 2101cb0ef41Sopenharmony_ciconst nodeModulesRegExp = /[/\\]node_modules[/\\](.+?)(?=[/\\])/g; 2111cb0ef41Sopenharmony_ci 2121cb0ef41Sopenharmony_ciconst classRegExp = /^(\s+[^(]*?)\s*{/; 2131cb0ef41Sopenharmony_ci// eslint-disable-next-line node-core/no-unescaped-regexp-dot 2141cb0ef41Sopenharmony_ciconst stripCommentsRegExp = /(\/\/.*?\n)|(\/\*(.|\n)*?\*\/)/g; 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ciconst kMinLineLength = 16; 2171cb0ef41Sopenharmony_ci 2181cb0ef41Sopenharmony_ci// Constants to map the iterator state. 2191cb0ef41Sopenharmony_ciconst kWeak = 0; 2201cb0ef41Sopenharmony_ciconst kIterator = 1; 2211cb0ef41Sopenharmony_ciconst kMapEntries = 2; 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci// Escaped control characters (plus the single quote and the backslash). Use 2241cb0ef41Sopenharmony_ci// empty strings to fill up unused entries. 2251cb0ef41Sopenharmony_ciconst meta = [ 2261cb0ef41Sopenharmony_ci '\\x00', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\x07', // x07 2271cb0ef41Sopenharmony_ci '\\b', '\\t', '\\n', '\\x0B', '\\f', '\\r', '\\x0E', '\\x0F', // x0F 2281cb0ef41Sopenharmony_ci '\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17', // x17 2291cb0ef41Sopenharmony_ci '\\x18', '\\x19', '\\x1A', '\\x1B', '\\x1C', '\\x1D', '\\x1E', '\\x1F', // x1F 2301cb0ef41Sopenharmony_ci '', '', '', '', '', '', '', "\\'", '', '', '', '', '', '', '', '', // x2F 2311cb0ef41Sopenharmony_ci '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x3F 2321cb0ef41Sopenharmony_ci '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x4F 2331cb0ef41Sopenharmony_ci '', '', '', '', '', '', '', '', '', '', '', '', '\\\\', '', '', '', // x5F 2341cb0ef41Sopenharmony_ci '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x6F 2351cb0ef41Sopenharmony_ci '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\\x7F', // x7F 2361cb0ef41Sopenharmony_ci '\\x80', '\\x81', '\\x82', '\\x83', '\\x84', '\\x85', '\\x86', '\\x87', // x87 2371cb0ef41Sopenharmony_ci '\\x88', '\\x89', '\\x8A', '\\x8B', '\\x8C', '\\x8D', '\\x8E', '\\x8F', // x8F 2381cb0ef41Sopenharmony_ci '\\x90', '\\x91', '\\x92', '\\x93', '\\x94', '\\x95', '\\x96', '\\x97', // x97 2391cb0ef41Sopenharmony_ci '\\x98', '\\x99', '\\x9A', '\\x9B', '\\x9C', '\\x9D', '\\x9E', '\\x9F', // x9F 2401cb0ef41Sopenharmony_ci]; 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci// Regex used for ansi escape code splitting 2431cb0ef41Sopenharmony_ci// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js 2441cb0ef41Sopenharmony_ci// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore 2451cb0ef41Sopenharmony_ci// Matches all ansi escape code sequences in a string 2461cb0ef41Sopenharmony_ciconst ansiPattern = '[\\u001B\\u009B][[\\]()#;?]*' + 2471cb0ef41Sopenharmony_ci '(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*' + 2481cb0ef41Sopenharmony_ci '|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' + 2491cb0ef41Sopenharmony_ci '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'; 2501cb0ef41Sopenharmony_ciconst ansi = new RegExp(ansiPattern, 'g'); 2511cb0ef41Sopenharmony_ci 2521cb0ef41Sopenharmony_cilet getStringWidth; 2531cb0ef41Sopenharmony_ci 2541cb0ef41Sopenharmony_cifunction getUserOptions(ctx, isCrossContext) { 2551cb0ef41Sopenharmony_ci const ret = { 2561cb0ef41Sopenharmony_ci stylize: ctx.stylize, 2571cb0ef41Sopenharmony_ci showHidden: ctx.showHidden, 2581cb0ef41Sopenharmony_ci depth: ctx.depth, 2591cb0ef41Sopenharmony_ci colors: ctx.colors, 2601cb0ef41Sopenharmony_ci customInspect: ctx.customInspect, 2611cb0ef41Sopenharmony_ci showProxy: ctx.showProxy, 2621cb0ef41Sopenharmony_ci maxArrayLength: ctx.maxArrayLength, 2631cb0ef41Sopenharmony_ci maxStringLength: ctx.maxStringLength, 2641cb0ef41Sopenharmony_ci breakLength: ctx.breakLength, 2651cb0ef41Sopenharmony_ci compact: ctx.compact, 2661cb0ef41Sopenharmony_ci sorted: ctx.sorted, 2671cb0ef41Sopenharmony_ci getters: ctx.getters, 2681cb0ef41Sopenharmony_ci numericSeparator: ctx.numericSeparator, 2691cb0ef41Sopenharmony_ci ...ctx.userOptions, 2701cb0ef41Sopenharmony_ci }; 2711cb0ef41Sopenharmony_ci 2721cb0ef41Sopenharmony_ci // Typically, the target value will be an instance of `Object`. If that is 2731cb0ef41Sopenharmony_ci // *not* the case, the object may come from another vm.Context, and we want 2741cb0ef41Sopenharmony_ci // to avoid passing it objects from this Context in that case, so we remove 2751cb0ef41Sopenharmony_ci // the prototype from the returned object itself + the `stylize()` function, 2761cb0ef41Sopenharmony_ci // and remove all other non-primitives, including non-primitive user options. 2771cb0ef41Sopenharmony_ci if (isCrossContext) { 2781cb0ef41Sopenharmony_ci ObjectSetPrototypeOf(ret, null); 2791cb0ef41Sopenharmony_ci for (const key of ObjectKeys(ret)) { 2801cb0ef41Sopenharmony_ci if ((typeof ret[key] === 'object' || typeof ret[key] === 'function') && 2811cb0ef41Sopenharmony_ci ret[key] !== null) { 2821cb0ef41Sopenharmony_ci delete ret[key]; 2831cb0ef41Sopenharmony_ci } 2841cb0ef41Sopenharmony_ci } 2851cb0ef41Sopenharmony_ci ret.stylize = ObjectSetPrototypeOf((value, flavour) => { 2861cb0ef41Sopenharmony_ci let stylized; 2871cb0ef41Sopenharmony_ci try { 2881cb0ef41Sopenharmony_ci stylized = `${ctx.stylize(value, flavour)}`; 2891cb0ef41Sopenharmony_ci } catch { 2901cb0ef41Sopenharmony_ci // Continue regardless of error. 2911cb0ef41Sopenharmony_ci } 2921cb0ef41Sopenharmony_ci 2931cb0ef41Sopenharmony_ci if (typeof stylized !== 'string') return value; 2941cb0ef41Sopenharmony_ci // `stylized` is a string as it should be, which is safe to pass along. 2951cb0ef41Sopenharmony_ci return stylized; 2961cb0ef41Sopenharmony_ci }, null); 2971cb0ef41Sopenharmony_ci } 2981cb0ef41Sopenharmony_ci 2991cb0ef41Sopenharmony_ci return ret; 3001cb0ef41Sopenharmony_ci} 3011cb0ef41Sopenharmony_ci 3021cb0ef41Sopenharmony_ci/** 3031cb0ef41Sopenharmony_ci * Echos the value of any input. Tries to print the value out 3041cb0ef41Sopenharmony_ci * in the best way possible given the different types. 3051cb0ef41Sopenharmony_ci * @param {any} value The value to print out. 3061cb0ef41Sopenharmony_ci * @param {object} opts Optional options object that alters the output. 3071cb0ef41Sopenharmony_ci */ 3081cb0ef41Sopenharmony_ci/* Legacy: value, showHidden, depth, colors */ 3091cb0ef41Sopenharmony_cifunction inspect(value, opts) { 3101cb0ef41Sopenharmony_ci // Default options 3111cb0ef41Sopenharmony_ci const ctx = { 3121cb0ef41Sopenharmony_ci budget: {}, 3131cb0ef41Sopenharmony_ci indentationLvl: 0, 3141cb0ef41Sopenharmony_ci seen: [], 3151cb0ef41Sopenharmony_ci currentDepth: 0, 3161cb0ef41Sopenharmony_ci stylize: stylizeNoColor, 3171cb0ef41Sopenharmony_ci showHidden: inspectDefaultOptions.showHidden, 3181cb0ef41Sopenharmony_ci depth: inspectDefaultOptions.depth, 3191cb0ef41Sopenharmony_ci colors: inspectDefaultOptions.colors, 3201cb0ef41Sopenharmony_ci customInspect: inspectDefaultOptions.customInspect, 3211cb0ef41Sopenharmony_ci showProxy: inspectDefaultOptions.showProxy, 3221cb0ef41Sopenharmony_ci maxArrayLength: inspectDefaultOptions.maxArrayLength, 3231cb0ef41Sopenharmony_ci maxStringLength: inspectDefaultOptions.maxStringLength, 3241cb0ef41Sopenharmony_ci breakLength: inspectDefaultOptions.breakLength, 3251cb0ef41Sopenharmony_ci compact: inspectDefaultOptions.compact, 3261cb0ef41Sopenharmony_ci sorted: inspectDefaultOptions.sorted, 3271cb0ef41Sopenharmony_ci getters: inspectDefaultOptions.getters, 3281cb0ef41Sopenharmony_ci numericSeparator: inspectDefaultOptions.numericSeparator, 3291cb0ef41Sopenharmony_ci }; 3301cb0ef41Sopenharmony_ci if (arguments.length > 1) { 3311cb0ef41Sopenharmony_ci // Legacy... 3321cb0ef41Sopenharmony_ci if (arguments.length > 2) { 3331cb0ef41Sopenharmony_ci if (arguments[2] !== undefined) { 3341cb0ef41Sopenharmony_ci ctx.depth = arguments[2]; 3351cb0ef41Sopenharmony_ci } 3361cb0ef41Sopenharmony_ci if (arguments.length > 3 && arguments[3] !== undefined) { 3371cb0ef41Sopenharmony_ci ctx.colors = arguments[3]; 3381cb0ef41Sopenharmony_ci } 3391cb0ef41Sopenharmony_ci } 3401cb0ef41Sopenharmony_ci // Set user-specified options 3411cb0ef41Sopenharmony_ci if (typeof opts === 'boolean') { 3421cb0ef41Sopenharmony_ci ctx.showHidden = opts; 3431cb0ef41Sopenharmony_ci } else if (opts) { 3441cb0ef41Sopenharmony_ci const optKeys = ObjectKeys(opts); 3451cb0ef41Sopenharmony_ci for (let i = 0; i < optKeys.length; ++i) { 3461cb0ef41Sopenharmony_ci const key = optKeys[i]; 3471cb0ef41Sopenharmony_ci // TODO(BridgeAR): Find a solution what to do about stylize. Either make 3481cb0ef41Sopenharmony_ci // this function public or add a new API with a similar or better 3491cb0ef41Sopenharmony_ci // functionality. 3501cb0ef41Sopenharmony_ci if ( 3511cb0ef41Sopenharmony_ci ObjectPrototypeHasOwnProperty(inspectDefaultOptions, key) || 3521cb0ef41Sopenharmony_ci key === 'stylize') { 3531cb0ef41Sopenharmony_ci ctx[key] = opts[key]; 3541cb0ef41Sopenharmony_ci } else if (ctx.userOptions === undefined) { 3551cb0ef41Sopenharmony_ci // This is required to pass through the actual user input. 3561cb0ef41Sopenharmony_ci ctx.userOptions = opts; 3571cb0ef41Sopenharmony_ci } 3581cb0ef41Sopenharmony_ci } 3591cb0ef41Sopenharmony_ci } 3601cb0ef41Sopenharmony_ci } 3611cb0ef41Sopenharmony_ci if (ctx.colors) ctx.stylize = stylizeWithColor; 3621cb0ef41Sopenharmony_ci if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity; 3631cb0ef41Sopenharmony_ci if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity; 3641cb0ef41Sopenharmony_ci return formatValue(ctx, value, 0); 3651cb0ef41Sopenharmony_ci} 3661cb0ef41Sopenharmony_ciinspect.custom = customInspectSymbol; 3671cb0ef41Sopenharmony_ci 3681cb0ef41Sopenharmony_ciObjectDefineProperty(inspect, 'defaultOptions', { 3691cb0ef41Sopenharmony_ci __proto__: null, 3701cb0ef41Sopenharmony_ci get() { 3711cb0ef41Sopenharmony_ci return inspectDefaultOptions; 3721cb0ef41Sopenharmony_ci }, 3731cb0ef41Sopenharmony_ci set(options) { 3741cb0ef41Sopenharmony_ci validateObject(options, 'options'); 3751cb0ef41Sopenharmony_ci return ObjectAssign(inspectDefaultOptions, options); 3761cb0ef41Sopenharmony_ci }, 3771cb0ef41Sopenharmony_ci}); 3781cb0ef41Sopenharmony_ci 3791cb0ef41Sopenharmony_ci// Set Graphics Rendition https://en.wikipedia.org/wiki/ANSI_escape_code#graphics 3801cb0ef41Sopenharmony_ci// Each color consists of an array with the color code as first entry and the 3811cb0ef41Sopenharmony_ci// reset code as second entry. 3821cb0ef41Sopenharmony_ciconst defaultFG = 39; 3831cb0ef41Sopenharmony_ciconst defaultBG = 49; 3841cb0ef41Sopenharmony_ciinspect.colors = { 3851cb0ef41Sopenharmony_ci __proto__: null, 3861cb0ef41Sopenharmony_ci reset: [0, 0], 3871cb0ef41Sopenharmony_ci bold: [1, 22], 3881cb0ef41Sopenharmony_ci dim: [2, 22], // Alias: faint 3891cb0ef41Sopenharmony_ci italic: [3, 23], 3901cb0ef41Sopenharmony_ci underline: [4, 24], 3911cb0ef41Sopenharmony_ci blink: [5, 25], 3921cb0ef41Sopenharmony_ci // Swap foreground and background colors 3931cb0ef41Sopenharmony_ci inverse: [7, 27], // Alias: swapcolors, swapColors 3941cb0ef41Sopenharmony_ci hidden: [8, 28], // Alias: conceal 3951cb0ef41Sopenharmony_ci strikethrough: [9, 29], // Alias: strikeThrough, crossedout, crossedOut 3961cb0ef41Sopenharmony_ci doubleunderline: [21, 24], // Alias: doubleUnderline 3971cb0ef41Sopenharmony_ci black: [30, defaultFG], 3981cb0ef41Sopenharmony_ci red: [31, defaultFG], 3991cb0ef41Sopenharmony_ci green: [32, defaultFG], 4001cb0ef41Sopenharmony_ci yellow: [33, defaultFG], 4011cb0ef41Sopenharmony_ci blue: [34, defaultFG], 4021cb0ef41Sopenharmony_ci magenta: [35, defaultFG], 4031cb0ef41Sopenharmony_ci cyan: [36, defaultFG], 4041cb0ef41Sopenharmony_ci white: [37, defaultFG], 4051cb0ef41Sopenharmony_ci bgBlack: [40, defaultBG], 4061cb0ef41Sopenharmony_ci bgRed: [41, defaultBG], 4071cb0ef41Sopenharmony_ci bgGreen: [42, defaultBG], 4081cb0ef41Sopenharmony_ci bgYellow: [43, defaultBG], 4091cb0ef41Sopenharmony_ci bgBlue: [44, defaultBG], 4101cb0ef41Sopenharmony_ci bgMagenta: [45, defaultBG], 4111cb0ef41Sopenharmony_ci bgCyan: [46, defaultBG], 4121cb0ef41Sopenharmony_ci bgWhite: [47, defaultBG], 4131cb0ef41Sopenharmony_ci framed: [51, 54], 4141cb0ef41Sopenharmony_ci overlined: [53, 55], 4151cb0ef41Sopenharmony_ci gray: [90, defaultFG], // Alias: grey, blackBright 4161cb0ef41Sopenharmony_ci redBright: [91, defaultFG], 4171cb0ef41Sopenharmony_ci greenBright: [92, defaultFG], 4181cb0ef41Sopenharmony_ci yellowBright: [93, defaultFG], 4191cb0ef41Sopenharmony_ci blueBright: [94, defaultFG], 4201cb0ef41Sopenharmony_ci magentaBright: [95, defaultFG], 4211cb0ef41Sopenharmony_ci cyanBright: [96, defaultFG], 4221cb0ef41Sopenharmony_ci whiteBright: [97, defaultFG], 4231cb0ef41Sopenharmony_ci bgGray: [100, defaultBG], // Alias: bgGrey, bgBlackBright 4241cb0ef41Sopenharmony_ci bgRedBright: [101, defaultBG], 4251cb0ef41Sopenharmony_ci bgGreenBright: [102, defaultBG], 4261cb0ef41Sopenharmony_ci bgYellowBright: [103, defaultBG], 4271cb0ef41Sopenharmony_ci bgBlueBright: [104, defaultBG], 4281cb0ef41Sopenharmony_ci bgMagentaBright: [105, defaultBG], 4291cb0ef41Sopenharmony_ci bgCyanBright: [106, defaultBG], 4301cb0ef41Sopenharmony_ci bgWhiteBright: [107, defaultBG], 4311cb0ef41Sopenharmony_ci}; 4321cb0ef41Sopenharmony_ci 4331cb0ef41Sopenharmony_cifunction defineColorAlias(target, alias) { 4341cb0ef41Sopenharmony_ci ObjectDefineProperty(inspect.colors, alias, { 4351cb0ef41Sopenharmony_ci __proto__: null, 4361cb0ef41Sopenharmony_ci get() { 4371cb0ef41Sopenharmony_ci return this[target]; 4381cb0ef41Sopenharmony_ci }, 4391cb0ef41Sopenharmony_ci set(value) { 4401cb0ef41Sopenharmony_ci this[target] = value; 4411cb0ef41Sopenharmony_ci }, 4421cb0ef41Sopenharmony_ci configurable: true, 4431cb0ef41Sopenharmony_ci enumerable: false, 4441cb0ef41Sopenharmony_ci }); 4451cb0ef41Sopenharmony_ci} 4461cb0ef41Sopenharmony_ci 4471cb0ef41Sopenharmony_cidefineColorAlias('gray', 'grey'); 4481cb0ef41Sopenharmony_cidefineColorAlias('gray', 'blackBright'); 4491cb0ef41Sopenharmony_cidefineColorAlias('bgGray', 'bgGrey'); 4501cb0ef41Sopenharmony_cidefineColorAlias('bgGray', 'bgBlackBright'); 4511cb0ef41Sopenharmony_cidefineColorAlias('dim', 'faint'); 4521cb0ef41Sopenharmony_cidefineColorAlias('strikethrough', 'crossedout'); 4531cb0ef41Sopenharmony_cidefineColorAlias('strikethrough', 'strikeThrough'); 4541cb0ef41Sopenharmony_cidefineColorAlias('strikethrough', 'crossedOut'); 4551cb0ef41Sopenharmony_cidefineColorAlias('hidden', 'conceal'); 4561cb0ef41Sopenharmony_cidefineColorAlias('inverse', 'swapColors'); 4571cb0ef41Sopenharmony_cidefineColorAlias('inverse', 'swapcolors'); 4581cb0ef41Sopenharmony_cidefineColorAlias('doubleunderline', 'doubleUnderline'); 4591cb0ef41Sopenharmony_ci 4601cb0ef41Sopenharmony_ci// TODO(BridgeAR): Add function style support for more complex styles. 4611cb0ef41Sopenharmony_ci// Don't use 'blue' not visible on cmd.exe 4621cb0ef41Sopenharmony_ciinspect.styles = ObjectAssign(ObjectCreate(null), { 4631cb0ef41Sopenharmony_ci special: 'cyan', 4641cb0ef41Sopenharmony_ci number: 'yellow', 4651cb0ef41Sopenharmony_ci bigint: 'yellow', 4661cb0ef41Sopenharmony_ci boolean: 'yellow', 4671cb0ef41Sopenharmony_ci undefined: 'grey', 4681cb0ef41Sopenharmony_ci null: 'bold', 4691cb0ef41Sopenharmony_ci string: 'green', 4701cb0ef41Sopenharmony_ci symbol: 'green', 4711cb0ef41Sopenharmony_ci date: 'magenta', 4721cb0ef41Sopenharmony_ci // "name": intentionally not styling 4731cb0ef41Sopenharmony_ci // TODO(BridgeAR): Highlight regular expressions properly. 4741cb0ef41Sopenharmony_ci regexp: 'red', 4751cb0ef41Sopenharmony_ci module: 'underline', 4761cb0ef41Sopenharmony_ci}); 4771cb0ef41Sopenharmony_ci 4781cb0ef41Sopenharmony_cifunction addQuotes(str, quotes) { 4791cb0ef41Sopenharmony_ci if (quotes === -1) { 4801cb0ef41Sopenharmony_ci return `"${str}"`; 4811cb0ef41Sopenharmony_ci } 4821cb0ef41Sopenharmony_ci if (quotes === -2) { 4831cb0ef41Sopenharmony_ci return `\`${str}\``; 4841cb0ef41Sopenharmony_ci } 4851cb0ef41Sopenharmony_ci return `'${str}'`; 4861cb0ef41Sopenharmony_ci} 4871cb0ef41Sopenharmony_ci 4881cb0ef41Sopenharmony_cifunction escapeFn(str) { 4891cb0ef41Sopenharmony_ci const charCode = StringPrototypeCharCodeAt(str); 4901cb0ef41Sopenharmony_ci return meta.length > charCode ? meta[charCode] : `\\u${NumberPrototypeToString(charCode, 16)}`; 4911cb0ef41Sopenharmony_ci} 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci// Escape control characters, single quotes and the backslash. 4941cb0ef41Sopenharmony_ci// This is similar to JSON stringify escaping. 4951cb0ef41Sopenharmony_cifunction strEscape(str) { 4961cb0ef41Sopenharmony_ci let escapeTest = strEscapeSequencesRegExp; 4971cb0ef41Sopenharmony_ci let escapeReplace = strEscapeSequencesReplacer; 4981cb0ef41Sopenharmony_ci let singleQuote = 39; 4991cb0ef41Sopenharmony_ci 5001cb0ef41Sopenharmony_ci // Check for double quotes. If not present, do not escape single quotes and 5011cb0ef41Sopenharmony_ci // instead wrap the text in double quotes. If double quotes exist, check for 5021cb0ef41Sopenharmony_ci // backticks. If they do not exist, use those as fallback instead of the 5031cb0ef41Sopenharmony_ci // double quotes. 5041cb0ef41Sopenharmony_ci if (StringPrototypeIncludes(str, "'")) { 5051cb0ef41Sopenharmony_ci // This invalidates the charCode and therefore can not be matched for 5061cb0ef41Sopenharmony_ci // anymore. 5071cb0ef41Sopenharmony_ci if (!StringPrototypeIncludes(str, '"')) { 5081cb0ef41Sopenharmony_ci singleQuote = -1; 5091cb0ef41Sopenharmony_ci } else if (!StringPrototypeIncludes(str, '`') && 5101cb0ef41Sopenharmony_ci !StringPrototypeIncludes(str, '${')) { 5111cb0ef41Sopenharmony_ci singleQuote = -2; 5121cb0ef41Sopenharmony_ci } 5131cb0ef41Sopenharmony_ci if (singleQuote !== 39) { 5141cb0ef41Sopenharmony_ci escapeTest = strEscapeSequencesRegExpSingle; 5151cb0ef41Sopenharmony_ci escapeReplace = strEscapeSequencesReplacerSingle; 5161cb0ef41Sopenharmony_ci } 5171cb0ef41Sopenharmony_ci } 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci // Some magic numbers that worked out fine while benchmarking with v8 6.0 5201cb0ef41Sopenharmony_ci if (str.length < 5000 && RegExpPrototypeExec(escapeTest, str) === null) 5211cb0ef41Sopenharmony_ci return addQuotes(str, singleQuote); 5221cb0ef41Sopenharmony_ci if (str.length > 100) { 5231cb0ef41Sopenharmony_ci str = RegExpPrototypeSymbolReplace(escapeReplace, str, escapeFn); 5241cb0ef41Sopenharmony_ci return addQuotes(str, singleQuote); 5251cb0ef41Sopenharmony_ci } 5261cb0ef41Sopenharmony_ci 5271cb0ef41Sopenharmony_ci let result = ''; 5281cb0ef41Sopenharmony_ci let last = 0; 5291cb0ef41Sopenharmony_ci for (let i = 0; i < str.length; i++) { 5301cb0ef41Sopenharmony_ci const point = StringPrototypeCharCodeAt(str, i); 5311cb0ef41Sopenharmony_ci if (point === singleQuote || 5321cb0ef41Sopenharmony_ci point === 92 || 5331cb0ef41Sopenharmony_ci point < 32 || 5341cb0ef41Sopenharmony_ci (point > 126 && point < 160)) { 5351cb0ef41Sopenharmony_ci if (last === i) { 5361cb0ef41Sopenharmony_ci result += meta[point]; 5371cb0ef41Sopenharmony_ci } else { 5381cb0ef41Sopenharmony_ci result += `${StringPrototypeSlice(str, last, i)}${meta[point]}`; 5391cb0ef41Sopenharmony_ci } 5401cb0ef41Sopenharmony_ci last = i + 1; 5411cb0ef41Sopenharmony_ci } else if (point >= 0xd800 && point <= 0xdfff) { 5421cb0ef41Sopenharmony_ci if (point <= 0xdbff && i + 1 < str.length) { 5431cb0ef41Sopenharmony_ci const point = StringPrototypeCharCodeAt(str, i + 1); 5441cb0ef41Sopenharmony_ci if (point >= 0xdc00 && point <= 0xdfff) { 5451cb0ef41Sopenharmony_ci i++; 5461cb0ef41Sopenharmony_ci continue; 5471cb0ef41Sopenharmony_ci } 5481cb0ef41Sopenharmony_ci } 5491cb0ef41Sopenharmony_ci result += `${StringPrototypeSlice(str, last, i)}\\u${NumberPrototypeToString(point, 16)}`; 5501cb0ef41Sopenharmony_ci last = i + 1; 5511cb0ef41Sopenharmony_ci } 5521cb0ef41Sopenharmony_ci } 5531cb0ef41Sopenharmony_ci 5541cb0ef41Sopenharmony_ci if (last !== str.length) { 5551cb0ef41Sopenharmony_ci result += StringPrototypeSlice(str, last); 5561cb0ef41Sopenharmony_ci } 5571cb0ef41Sopenharmony_ci return addQuotes(result, singleQuote); 5581cb0ef41Sopenharmony_ci} 5591cb0ef41Sopenharmony_ci 5601cb0ef41Sopenharmony_cifunction stylizeWithColor(str, styleType) { 5611cb0ef41Sopenharmony_ci const style = inspect.styles[styleType]; 5621cb0ef41Sopenharmony_ci if (style !== undefined) { 5631cb0ef41Sopenharmony_ci const color = inspect.colors[style]; 5641cb0ef41Sopenharmony_ci if (color !== undefined) 5651cb0ef41Sopenharmony_ci return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`; 5661cb0ef41Sopenharmony_ci } 5671cb0ef41Sopenharmony_ci return str; 5681cb0ef41Sopenharmony_ci} 5691cb0ef41Sopenharmony_ci 5701cb0ef41Sopenharmony_cifunction stylizeNoColor(str) { 5711cb0ef41Sopenharmony_ci return str; 5721cb0ef41Sopenharmony_ci} 5731cb0ef41Sopenharmony_ci 5741cb0ef41Sopenharmony_ci// Return a new empty array to push in the results of the default formatter. 5751cb0ef41Sopenharmony_cifunction getEmptyFormatArray() { 5761cb0ef41Sopenharmony_ci return []; 5771cb0ef41Sopenharmony_ci} 5781cb0ef41Sopenharmony_ci 5791cb0ef41Sopenharmony_cifunction isInstanceof(object, proto) { 5801cb0ef41Sopenharmony_ci try { 5811cb0ef41Sopenharmony_ci return object instanceof proto; 5821cb0ef41Sopenharmony_ci } catch { 5831cb0ef41Sopenharmony_ci return false; 5841cb0ef41Sopenharmony_ci } 5851cb0ef41Sopenharmony_ci} 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_cifunction getConstructorName(obj, ctx, recurseTimes, protoProps) { 5881cb0ef41Sopenharmony_ci let firstProto; 5891cb0ef41Sopenharmony_ci const tmp = obj; 5901cb0ef41Sopenharmony_ci while (obj || isUndetectableObject(obj)) { 5911cb0ef41Sopenharmony_ci const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor'); 5921cb0ef41Sopenharmony_ci if (descriptor !== undefined && 5931cb0ef41Sopenharmony_ci typeof descriptor.value === 'function' && 5941cb0ef41Sopenharmony_ci descriptor.value.name !== '' && 5951cb0ef41Sopenharmony_ci isInstanceof(tmp, descriptor.value)) { 5961cb0ef41Sopenharmony_ci if (protoProps !== undefined && 5971cb0ef41Sopenharmony_ci (firstProto !== obj || 5981cb0ef41Sopenharmony_ci !builtInObjects.has(descriptor.value.name))) { 5991cb0ef41Sopenharmony_ci addPrototypeProperties( 6001cb0ef41Sopenharmony_ci ctx, tmp, firstProto || tmp, recurseTimes, protoProps); 6011cb0ef41Sopenharmony_ci } 6021cb0ef41Sopenharmony_ci return String(descriptor.value.name); 6031cb0ef41Sopenharmony_ci } 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ci obj = ObjectGetPrototypeOf(obj); 6061cb0ef41Sopenharmony_ci if (firstProto === undefined) { 6071cb0ef41Sopenharmony_ci firstProto = obj; 6081cb0ef41Sopenharmony_ci } 6091cb0ef41Sopenharmony_ci } 6101cb0ef41Sopenharmony_ci 6111cb0ef41Sopenharmony_ci if (firstProto === null) { 6121cb0ef41Sopenharmony_ci return null; 6131cb0ef41Sopenharmony_ci } 6141cb0ef41Sopenharmony_ci 6151cb0ef41Sopenharmony_ci const res = internalGetConstructorName(tmp); 6161cb0ef41Sopenharmony_ci 6171cb0ef41Sopenharmony_ci if (recurseTimes > ctx.depth && ctx.depth !== null) { 6181cb0ef41Sopenharmony_ci return `${res} <Complex prototype>`; 6191cb0ef41Sopenharmony_ci } 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ci const protoConstr = getConstructorName( 6221cb0ef41Sopenharmony_ci firstProto, ctx, recurseTimes + 1, protoProps); 6231cb0ef41Sopenharmony_ci 6241cb0ef41Sopenharmony_ci if (protoConstr === null) { 6251cb0ef41Sopenharmony_ci return `${res} <${inspect(firstProto, { 6261cb0ef41Sopenharmony_ci ...ctx, 6271cb0ef41Sopenharmony_ci customInspect: false, 6281cb0ef41Sopenharmony_ci depth: -1, 6291cb0ef41Sopenharmony_ci })}>`; 6301cb0ef41Sopenharmony_ci } 6311cb0ef41Sopenharmony_ci 6321cb0ef41Sopenharmony_ci return `${res} <${protoConstr}>`; 6331cb0ef41Sopenharmony_ci} 6341cb0ef41Sopenharmony_ci 6351cb0ef41Sopenharmony_ci// This function has the side effect of adding prototype properties to the 6361cb0ef41Sopenharmony_ci// `output` argument (which is an array). This is intended to highlight user 6371cb0ef41Sopenharmony_ci// defined prototype properties. 6381cb0ef41Sopenharmony_cifunction addPrototypeProperties(ctx, main, obj, recurseTimes, output) { 6391cb0ef41Sopenharmony_ci let depth = 0; 6401cb0ef41Sopenharmony_ci let keys; 6411cb0ef41Sopenharmony_ci let keySet; 6421cb0ef41Sopenharmony_ci do { 6431cb0ef41Sopenharmony_ci if (depth !== 0 || main === obj) { 6441cb0ef41Sopenharmony_ci obj = ObjectGetPrototypeOf(obj); 6451cb0ef41Sopenharmony_ci // Stop as soon as a null prototype is encountered. 6461cb0ef41Sopenharmony_ci if (obj === null) { 6471cb0ef41Sopenharmony_ci return; 6481cb0ef41Sopenharmony_ci } 6491cb0ef41Sopenharmony_ci // Stop as soon as a built-in object type is detected. 6501cb0ef41Sopenharmony_ci const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor'); 6511cb0ef41Sopenharmony_ci if (descriptor !== undefined && 6521cb0ef41Sopenharmony_ci typeof descriptor.value === 'function' && 6531cb0ef41Sopenharmony_ci builtInObjects.has(descriptor.value.name)) { 6541cb0ef41Sopenharmony_ci return; 6551cb0ef41Sopenharmony_ci } 6561cb0ef41Sopenharmony_ci } 6571cb0ef41Sopenharmony_ci 6581cb0ef41Sopenharmony_ci if (depth === 0) { 6591cb0ef41Sopenharmony_ci keySet = new SafeSet(); 6601cb0ef41Sopenharmony_ci } else { 6611cb0ef41Sopenharmony_ci ArrayPrototypeForEach(keys, (key) => keySet.add(key)); 6621cb0ef41Sopenharmony_ci } 6631cb0ef41Sopenharmony_ci // Get all own property names and symbols. 6641cb0ef41Sopenharmony_ci keys = ReflectOwnKeys(obj); 6651cb0ef41Sopenharmony_ci ArrayPrototypePush(ctx.seen, main); 6661cb0ef41Sopenharmony_ci for (const key of keys) { 6671cb0ef41Sopenharmony_ci // Ignore the `constructor` property and keys that exist on layers above. 6681cb0ef41Sopenharmony_ci if (key === 'constructor' || 6691cb0ef41Sopenharmony_ci ObjectPrototypeHasOwnProperty(main, key) || 6701cb0ef41Sopenharmony_ci (depth !== 0 && keySet.has(key))) { 6711cb0ef41Sopenharmony_ci continue; 6721cb0ef41Sopenharmony_ci } 6731cb0ef41Sopenharmony_ci const desc = ObjectGetOwnPropertyDescriptor(obj, key); 6741cb0ef41Sopenharmony_ci if (typeof desc.value === 'function') { 6751cb0ef41Sopenharmony_ci continue; 6761cb0ef41Sopenharmony_ci } 6771cb0ef41Sopenharmony_ci const value = formatProperty( 6781cb0ef41Sopenharmony_ci ctx, obj, recurseTimes, key, kObjectType, desc, main); 6791cb0ef41Sopenharmony_ci if (ctx.colors) { 6801cb0ef41Sopenharmony_ci // Faint! 6811cb0ef41Sopenharmony_ci ArrayPrototypePush(output, `\u001b[2m${value}\u001b[22m`); 6821cb0ef41Sopenharmony_ci } else { 6831cb0ef41Sopenharmony_ci ArrayPrototypePush(output, value); 6841cb0ef41Sopenharmony_ci } 6851cb0ef41Sopenharmony_ci } 6861cb0ef41Sopenharmony_ci ArrayPrototypePop(ctx.seen); 6871cb0ef41Sopenharmony_ci // Limit the inspection to up to three prototype layers. Using `recurseTimes` 6881cb0ef41Sopenharmony_ci // is not a good choice here, because it's as if the properties are declared 6891cb0ef41Sopenharmony_ci // on the current object from the users perspective. 6901cb0ef41Sopenharmony_ci } while (++depth !== 3); 6911cb0ef41Sopenharmony_ci} 6921cb0ef41Sopenharmony_ci 6931cb0ef41Sopenharmony_cifunction getPrefix(constructor, tag, fallback, size = '') { 6941cb0ef41Sopenharmony_ci if (constructor === null) { 6951cb0ef41Sopenharmony_ci if (tag !== '' && fallback !== tag) { 6961cb0ef41Sopenharmony_ci return `[${fallback}${size}: null prototype] [${tag}] `; 6971cb0ef41Sopenharmony_ci } 6981cb0ef41Sopenharmony_ci return `[${fallback}${size}: null prototype] `; 6991cb0ef41Sopenharmony_ci } 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_ci if (tag !== '' && constructor !== tag) { 7021cb0ef41Sopenharmony_ci return `${constructor}${size} [${tag}] `; 7031cb0ef41Sopenharmony_ci } 7041cb0ef41Sopenharmony_ci return `${constructor}${size} `; 7051cb0ef41Sopenharmony_ci} 7061cb0ef41Sopenharmony_ci 7071cb0ef41Sopenharmony_ci// Look up the keys of the object. 7081cb0ef41Sopenharmony_cifunction getKeys(value, showHidden) { 7091cb0ef41Sopenharmony_ci let keys; 7101cb0ef41Sopenharmony_ci const symbols = ObjectGetOwnPropertySymbols(value); 7111cb0ef41Sopenharmony_ci if (showHidden) { 7121cb0ef41Sopenharmony_ci keys = ObjectGetOwnPropertyNames(value); 7131cb0ef41Sopenharmony_ci if (symbols.length !== 0) 7141cb0ef41Sopenharmony_ci ArrayPrototypePushApply(keys, symbols); 7151cb0ef41Sopenharmony_ci } else { 7161cb0ef41Sopenharmony_ci // This might throw if `value` is a Module Namespace Object from an 7171cb0ef41Sopenharmony_ci // unevaluated module, but we don't want to perform the actual type 7181cb0ef41Sopenharmony_ci // check because it's expensive. 7191cb0ef41Sopenharmony_ci // TODO(devsnek): track https://github.com/tc39/ecma262/issues/1209 7201cb0ef41Sopenharmony_ci // and modify this logic as needed. 7211cb0ef41Sopenharmony_ci try { 7221cb0ef41Sopenharmony_ci keys = ObjectKeys(value); 7231cb0ef41Sopenharmony_ci } catch (err) { 7241cb0ef41Sopenharmony_ci assert(isNativeError(err) && err.name === 'ReferenceError' && 7251cb0ef41Sopenharmony_ci isModuleNamespaceObject(value)); 7261cb0ef41Sopenharmony_ci keys = ObjectGetOwnPropertyNames(value); 7271cb0ef41Sopenharmony_ci } 7281cb0ef41Sopenharmony_ci if (symbols.length !== 0) { 7291cb0ef41Sopenharmony_ci const filter = (key) => ObjectPrototypePropertyIsEnumerable(value, key); 7301cb0ef41Sopenharmony_ci ArrayPrototypePushApply(keys, ArrayPrototypeFilter(symbols, filter)); 7311cb0ef41Sopenharmony_ci } 7321cb0ef41Sopenharmony_ci } 7331cb0ef41Sopenharmony_ci return keys; 7341cb0ef41Sopenharmony_ci} 7351cb0ef41Sopenharmony_ci 7361cb0ef41Sopenharmony_cifunction getCtxStyle(value, constructor, tag) { 7371cb0ef41Sopenharmony_ci let fallback = ''; 7381cb0ef41Sopenharmony_ci if (constructor === null) { 7391cb0ef41Sopenharmony_ci fallback = internalGetConstructorName(value); 7401cb0ef41Sopenharmony_ci if (fallback === tag) { 7411cb0ef41Sopenharmony_ci fallback = 'Object'; 7421cb0ef41Sopenharmony_ci } 7431cb0ef41Sopenharmony_ci } 7441cb0ef41Sopenharmony_ci return getPrefix(constructor, tag, fallback); 7451cb0ef41Sopenharmony_ci} 7461cb0ef41Sopenharmony_ci 7471cb0ef41Sopenharmony_cifunction formatProxy(ctx, proxy, recurseTimes) { 7481cb0ef41Sopenharmony_ci if (recurseTimes > ctx.depth && ctx.depth !== null) { 7491cb0ef41Sopenharmony_ci return ctx.stylize('Proxy [Array]', 'special'); 7501cb0ef41Sopenharmony_ci } 7511cb0ef41Sopenharmony_ci recurseTimes += 1; 7521cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 7531cb0ef41Sopenharmony_ci const res = [ 7541cb0ef41Sopenharmony_ci formatValue(ctx, proxy[0], recurseTimes), 7551cb0ef41Sopenharmony_ci formatValue(ctx, proxy[1], recurseTimes), 7561cb0ef41Sopenharmony_ci ]; 7571cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 7581cb0ef41Sopenharmony_ci return reduceToSingleString( 7591cb0ef41Sopenharmony_ci ctx, res, '', ['Proxy [', ']'], kArrayExtrasType, recurseTimes); 7601cb0ef41Sopenharmony_ci} 7611cb0ef41Sopenharmony_ci 7621cb0ef41Sopenharmony_ci// Note: using `formatValue` directly requires the indentation level to be 7631cb0ef41Sopenharmony_ci// corrected by setting `ctx.indentationLvL += diff` and then to decrease the 7641cb0ef41Sopenharmony_ci// value afterwards again. 7651cb0ef41Sopenharmony_cifunction formatValue(ctx, value, recurseTimes, typedArray) { 7661cb0ef41Sopenharmony_ci // Primitive types cannot have properties. 7671cb0ef41Sopenharmony_ci if (typeof value !== 'object' && 7681cb0ef41Sopenharmony_ci typeof value !== 'function' && 7691cb0ef41Sopenharmony_ci !isUndetectableObject(value)) { 7701cb0ef41Sopenharmony_ci return formatPrimitive(ctx.stylize, value, ctx); 7711cb0ef41Sopenharmony_ci } 7721cb0ef41Sopenharmony_ci if (value === null) { 7731cb0ef41Sopenharmony_ci return ctx.stylize('null', 'null'); 7741cb0ef41Sopenharmony_ci } 7751cb0ef41Sopenharmony_ci 7761cb0ef41Sopenharmony_ci // Memorize the context for custom inspection on proxies. 7771cb0ef41Sopenharmony_ci const context = value; 7781cb0ef41Sopenharmony_ci // Always check for proxies to prevent side effects and to prevent triggering 7791cb0ef41Sopenharmony_ci // any proxy handlers. 7801cb0ef41Sopenharmony_ci const proxy = getProxyDetails(value, !!ctx.showProxy); 7811cb0ef41Sopenharmony_ci if (proxy !== undefined) { 7821cb0ef41Sopenharmony_ci if (proxy === null || proxy[0] === null) { 7831cb0ef41Sopenharmony_ci return ctx.stylize('<Revoked Proxy>', 'special'); 7841cb0ef41Sopenharmony_ci } 7851cb0ef41Sopenharmony_ci if (ctx.showProxy) { 7861cb0ef41Sopenharmony_ci return formatProxy(ctx, proxy, recurseTimes); 7871cb0ef41Sopenharmony_ci } 7881cb0ef41Sopenharmony_ci value = proxy; 7891cb0ef41Sopenharmony_ci } 7901cb0ef41Sopenharmony_ci 7911cb0ef41Sopenharmony_ci // Provide a hook for user-specified inspect functions. 7921cb0ef41Sopenharmony_ci // Check that value is an object with an inspect function on it. 7931cb0ef41Sopenharmony_ci if (ctx.customInspect) { 7941cb0ef41Sopenharmony_ci const maybeCustom = value[customInspectSymbol]; 7951cb0ef41Sopenharmony_ci if (typeof maybeCustom === 'function' && 7961cb0ef41Sopenharmony_ci // Filter out the util module, its inspect function is special. 7971cb0ef41Sopenharmony_ci maybeCustom !== inspect && 7981cb0ef41Sopenharmony_ci // Also filter out any prototype objects using the circular check. 7991cb0ef41Sopenharmony_ci !(value.constructor && value.constructor.prototype === value)) { 8001cb0ef41Sopenharmony_ci // This makes sure the recurseTimes are reported as before while using 8011cb0ef41Sopenharmony_ci // a counter internally. 8021cb0ef41Sopenharmony_ci const depth = ctx.depth === null ? null : ctx.depth - recurseTimes; 8031cb0ef41Sopenharmony_ci const isCrossContext = 8041cb0ef41Sopenharmony_ci proxy !== undefined || !(context instanceof Object); 8051cb0ef41Sopenharmony_ci const ret = FunctionPrototypeCall( 8061cb0ef41Sopenharmony_ci maybeCustom, 8071cb0ef41Sopenharmony_ci context, 8081cb0ef41Sopenharmony_ci depth, 8091cb0ef41Sopenharmony_ci getUserOptions(ctx, isCrossContext), 8101cb0ef41Sopenharmony_ci inspect, 8111cb0ef41Sopenharmony_ci ); 8121cb0ef41Sopenharmony_ci // If the custom inspection method returned `this`, don't go into 8131cb0ef41Sopenharmony_ci // infinite recursion. 8141cb0ef41Sopenharmony_ci if (ret !== context) { 8151cb0ef41Sopenharmony_ci if (typeof ret !== 'string') { 8161cb0ef41Sopenharmony_ci return formatValue(ctx, ret, recurseTimes); 8171cb0ef41Sopenharmony_ci } 8181cb0ef41Sopenharmony_ci return StringPrototypeReplaceAll(ret, '\n', `\n${StringPrototypeRepeat(' ', ctx.indentationLvl)}`); 8191cb0ef41Sopenharmony_ci } 8201cb0ef41Sopenharmony_ci } 8211cb0ef41Sopenharmony_ci } 8221cb0ef41Sopenharmony_ci 8231cb0ef41Sopenharmony_ci // Using an array here is actually better for the average case than using 8241cb0ef41Sopenharmony_ci // a Set. `seen` will only check for the depth and will never grow too large. 8251cb0ef41Sopenharmony_ci if (ctx.seen.includes(value)) { 8261cb0ef41Sopenharmony_ci let index = 1; 8271cb0ef41Sopenharmony_ci if (ctx.circular === undefined) { 8281cb0ef41Sopenharmony_ci ctx.circular = new SafeMap(); 8291cb0ef41Sopenharmony_ci ctx.circular.set(value, index); 8301cb0ef41Sopenharmony_ci } else { 8311cb0ef41Sopenharmony_ci index = ctx.circular.get(value); 8321cb0ef41Sopenharmony_ci if (index === undefined) { 8331cb0ef41Sopenharmony_ci index = ctx.circular.size + 1; 8341cb0ef41Sopenharmony_ci ctx.circular.set(value, index); 8351cb0ef41Sopenharmony_ci } 8361cb0ef41Sopenharmony_ci } 8371cb0ef41Sopenharmony_ci return ctx.stylize(`[Circular *${index}]`, 'special'); 8381cb0ef41Sopenharmony_ci } 8391cb0ef41Sopenharmony_ci 8401cb0ef41Sopenharmony_ci return formatRaw(ctx, value, recurseTimes, typedArray); 8411cb0ef41Sopenharmony_ci} 8421cb0ef41Sopenharmony_ci 8431cb0ef41Sopenharmony_cifunction formatRaw(ctx, value, recurseTimes, typedArray) { 8441cb0ef41Sopenharmony_ci let keys; 8451cb0ef41Sopenharmony_ci let protoProps; 8461cb0ef41Sopenharmony_ci if (ctx.showHidden && (recurseTimes <= ctx.depth || ctx.depth === null)) { 8471cb0ef41Sopenharmony_ci protoProps = []; 8481cb0ef41Sopenharmony_ci } 8491cb0ef41Sopenharmony_ci 8501cb0ef41Sopenharmony_ci const constructor = getConstructorName(value, ctx, recurseTimes, protoProps); 8511cb0ef41Sopenharmony_ci // Reset the variable to check for this later on. 8521cb0ef41Sopenharmony_ci if (protoProps !== undefined && protoProps.length === 0) { 8531cb0ef41Sopenharmony_ci protoProps = undefined; 8541cb0ef41Sopenharmony_ci } 8551cb0ef41Sopenharmony_ci 8561cb0ef41Sopenharmony_ci let tag = value[SymbolToStringTag]; 8571cb0ef41Sopenharmony_ci // Only list the tag in case it's non-enumerable / not an own property. 8581cb0ef41Sopenharmony_ci // Otherwise we'd print this twice. 8591cb0ef41Sopenharmony_ci if (typeof tag !== 'string' || 8601cb0ef41Sopenharmony_ci (tag !== '' && 8611cb0ef41Sopenharmony_ci (ctx.showHidden ? 8621cb0ef41Sopenharmony_ci ObjectPrototypeHasOwnProperty : 8631cb0ef41Sopenharmony_ci ObjectPrototypePropertyIsEnumerable)( 8641cb0ef41Sopenharmony_ci value, SymbolToStringTag, 8651cb0ef41Sopenharmony_ci ))) { 8661cb0ef41Sopenharmony_ci tag = ''; 8671cb0ef41Sopenharmony_ci } 8681cb0ef41Sopenharmony_ci let base = ''; 8691cb0ef41Sopenharmony_ci let formatter = getEmptyFormatArray; 8701cb0ef41Sopenharmony_ci let braces; 8711cb0ef41Sopenharmony_ci let noIterator = true; 8721cb0ef41Sopenharmony_ci let i = 0; 8731cb0ef41Sopenharmony_ci const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE; 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ci let extrasType = kObjectType; 8761cb0ef41Sopenharmony_ci 8771cb0ef41Sopenharmony_ci // Iterators and the rest are split to reduce checks. 8781cb0ef41Sopenharmony_ci // We have to check all values in case the constructor is set to null. 8791cb0ef41Sopenharmony_ci // Otherwise it would not possible to identify all types properly. 8801cb0ef41Sopenharmony_ci if (SymbolIterator in value || constructor === null) { 8811cb0ef41Sopenharmony_ci noIterator = false; 8821cb0ef41Sopenharmony_ci if (ArrayIsArray(value)) { 8831cb0ef41Sopenharmony_ci // Only set the constructor for non ordinary ("Array [...]") arrays. 8841cb0ef41Sopenharmony_ci const prefix = (constructor !== 'Array' || tag !== '') ? 8851cb0ef41Sopenharmony_ci getPrefix(constructor, tag, 'Array', `(${value.length})`) : 8861cb0ef41Sopenharmony_ci ''; 8871cb0ef41Sopenharmony_ci keys = getOwnNonIndexProperties(value, filter); 8881cb0ef41Sopenharmony_ci braces = [`${prefix}[`, ']']; 8891cb0ef41Sopenharmony_ci if (value.length === 0 && keys.length === 0 && protoProps === undefined) 8901cb0ef41Sopenharmony_ci return `${braces[0]}]`; 8911cb0ef41Sopenharmony_ci extrasType = kArrayExtrasType; 8921cb0ef41Sopenharmony_ci formatter = formatArray; 8931cb0ef41Sopenharmony_ci } else if (isSet(value)) { 8941cb0ef41Sopenharmony_ci const size = SetPrototypeGetSize(value); 8951cb0ef41Sopenharmony_ci const prefix = getPrefix(constructor, tag, 'Set', `(${size})`); 8961cb0ef41Sopenharmony_ci keys = getKeys(value, ctx.showHidden); 8971cb0ef41Sopenharmony_ci formatter = constructor !== null ? 8981cb0ef41Sopenharmony_ci FunctionPrototypeBind(formatSet, null, value) : 8991cb0ef41Sopenharmony_ci FunctionPrototypeBind(formatSet, null, SetPrototypeValues(value)); 9001cb0ef41Sopenharmony_ci if (size === 0 && keys.length === 0 && protoProps === undefined) 9011cb0ef41Sopenharmony_ci return `${prefix}{}`; 9021cb0ef41Sopenharmony_ci braces = [`${prefix}{`, '}']; 9031cb0ef41Sopenharmony_ci } else if (isMap(value)) { 9041cb0ef41Sopenharmony_ci const size = MapPrototypeGetSize(value); 9051cb0ef41Sopenharmony_ci const prefix = getPrefix(constructor, tag, 'Map', `(${size})`); 9061cb0ef41Sopenharmony_ci keys = getKeys(value, ctx.showHidden); 9071cb0ef41Sopenharmony_ci formatter = constructor !== null ? 9081cb0ef41Sopenharmony_ci FunctionPrototypeBind(formatMap, null, value) : 9091cb0ef41Sopenharmony_ci FunctionPrototypeBind(formatMap, null, MapPrototypeEntries(value)); 9101cb0ef41Sopenharmony_ci if (size === 0 && keys.length === 0 && protoProps === undefined) 9111cb0ef41Sopenharmony_ci return `${prefix}{}`; 9121cb0ef41Sopenharmony_ci braces = [`${prefix}{`, '}']; 9131cb0ef41Sopenharmony_ci } else if (isTypedArray(value)) { 9141cb0ef41Sopenharmony_ci keys = getOwnNonIndexProperties(value, filter); 9151cb0ef41Sopenharmony_ci let bound = value; 9161cb0ef41Sopenharmony_ci let fallback = ''; 9171cb0ef41Sopenharmony_ci if (constructor === null) { 9181cb0ef41Sopenharmony_ci fallback = TypedArrayPrototypeGetSymbolToStringTag(value); 9191cb0ef41Sopenharmony_ci // Reconstruct the array information. 9201cb0ef41Sopenharmony_ci bound = new primordials[fallback](value); 9211cb0ef41Sopenharmony_ci } 9221cb0ef41Sopenharmony_ci const size = TypedArrayPrototypeGetLength(value); 9231cb0ef41Sopenharmony_ci const prefix = getPrefix(constructor, tag, fallback, `(${size})`); 9241cb0ef41Sopenharmony_ci braces = [`${prefix}[`, ']']; 9251cb0ef41Sopenharmony_ci if (value.length === 0 && keys.length === 0 && !ctx.showHidden) 9261cb0ef41Sopenharmony_ci return `${braces[0]}]`; 9271cb0ef41Sopenharmony_ci // Special handle the value. The original value is required below. The 9281cb0ef41Sopenharmony_ci // bound function is required to reconstruct missing information. 9291cb0ef41Sopenharmony_ci formatter = FunctionPrototypeBind(formatTypedArray, null, bound, size); 9301cb0ef41Sopenharmony_ci extrasType = kArrayExtrasType; 9311cb0ef41Sopenharmony_ci } else if (isMapIterator(value)) { 9321cb0ef41Sopenharmony_ci keys = getKeys(value, ctx.showHidden); 9331cb0ef41Sopenharmony_ci braces = getIteratorBraces('Map', tag); 9341cb0ef41Sopenharmony_ci // Add braces to the formatter parameters. 9351cb0ef41Sopenharmony_ci formatter = FunctionPrototypeBind(formatIterator, null, braces); 9361cb0ef41Sopenharmony_ci } else if (isSetIterator(value)) { 9371cb0ef41Sopenharmony_ci keys = getKeys(value, ctx.showHidden); 9381cb0ef41Sopenharmony_ci braces = getIteratorBraces('Set', tag); 9391cb0ef41Sopenharmony_ci // Add braces to the formatter parameters. 9401cb0ef41Sopenharmony_ci formatter = FunctionPrototypeBind(formatIterator, null, braces); 9411cb0ef41Sopenharmony_ci } else { 9421cb0ef41Sopenharmony_ci noIterator = true; 9431cb0ef41Sopenharmony_ci } 9441cb0ef41Sopenharmony_ci } 9451cb0ef41Sopenharmony_ci if (noIterator) { 9461cb0ef41Sopenharmony_ci keys = getKeys(value, ctx.showHidden); 9471cb0ef41Sopenharmony_ci braces = ['{', '}']; 9481cb0ef41Sopenharmony_ci if (constructor === 'Object') { 9491cb0ef41Sopenharmony_ci if (isArgumentsObject(value)) { 9501cb0ef41Sopenharmony_ci braces[0] = '[Arguments] {'; 9511cb0ef41Sopenharmony_ci } else if (tag !== '') { 9521cb0ef41Sopenharmony_ci braces[0] = `${getPrefix(constructor, tag, 'Object')}{`; 9531cb0ef41Sopenharmony_ci } 9541cb0ef41Sopenharmony_ci if (keys.length === 0 && protoProps === undefined) { 9551cb0ef41Sopenharmony_ci return `${braces[0]}}`; 9561cb0ef41Sopenharmony_ci } 9571cb0ef41Sopenharmony_ci } else if (typeof value === 'function') { 9581cb0ef41Sopenharmony_ci base = getFunctionBase(value, constructor, tag); 9591cb0ef41Sopenharmony_ci if (keys.length === 0 && protoProps === undefined) 9601cb0ef41Sopenharmony_ci return ctx.stylize(base, 'special'); 9611cb0ef41Sopenharmony_ci } else if (isRegExp(value)) { 9621cb0ef41Sopenharmony_ci // Make RegExps say that they are RegExps 9631cb0ef41Sopenharmony_ci base = RegExpPrototypeToString( 9641cb0ef41Sopenharmony_ci constructor !== null ? value : new RegExp(value), 9651cb0ef41Sopenharmony_ci ); 9661cb0ef41Sopenharmony_ci const prefix = getPrefix(constructor, tag, 'RegExp'); 9671cb0ef41Sopenharmony_ci if (prefix !== 'RegExp ') 9681cb0ef41Sopenharmony_ci base = `${prefix}${base}`; 9691cb0ef41Sopenharmony_ci if ((keys.length === 0 && protoProps === undefined) || 9701cb0ef41Sopenharmony_ci (recurseTimes > ctx.depth && ctx.depth !== null)) { 9711cb0ef41Sopenharmony_ci return ctx.stylize(base, 'regexp'); 9721cb0ef41Sopenharmony_ci } 9731cb0ef41Sopenharmony_ci } else if (isDate(value)) { 9741cb0ef41Sopenharmony_ci // Make dates with properties first say the date 9751cb0ef41Sopenharmony_ci base = NumberIsNaN(DatePrototypeGetTime(value)) ? 9761cb0ef41Sopenharmony_ci DatePrototypeToString(value) : 9771cb0ef41Sopenharmony_ci DatePrototypeToISOString(value); 9781cb0ef41Sopenharmony_ci const prefix = getPrefix(constructor, tag, 'Date'); 9791cb0ef41Sopenharmony_ci if (prefix !== 'Date ') 9801cb0ef41Sopenharmony_ci base = `${prefix}${base}`; 9811cb0ef41Sopenharmony_ci if (keys.length === 0 && protoProps === undefined) { 9821cb0ef41Sopenharmony_ci return ctx.stylize(base, 'date'); 9831cb0ef41Sopenharmony_ci } 9841cb0ef41Sopenharmony_ci } else if (isError(value)) { 9851cb0ef41Sopenharmony_ci base = formatError(value, constructor, tag, ctx, keys); 9861cb0ef41Sopenharmony_ci if (keys.length === 0 && protoProps === undefined) 9871cb0ef41Sopenharmony_ci return base; 9881cb0ef41Sopenharmony_ci } else if (isAnyArrayBuffer(value)) { 9891cb0ef41Sopenharmony_ci // Fast path for ArrayBuffer and SharedArrayBuffer. 9901cb0ef41Sopenharmony_ci // Can't do the same for DataView because it has a non-primitive 9911cb0ef41Sopenharmony_ci // .buffer property that we need to recurse for. 9921cb0ef41Sopenharmony_ci const arrayType = isArrayBuffer(value) ? 'ArrayBuffer' : 9931cb0ef41Sopenharmony_ci 'SharedArrayBuffer'; 9941cb0ef41Sopenharmony_ci const prefix = getPrefix(constructor, tag, arrayType); 9951cb0ef41Sopenharmony_ci if (typedArray === undefined) { 9961cb0ef41Sopenharmony_ci formatter = formatArrayBuffer; 9971cb0ef41Sopenharmony_ci } else if (keys.length === 0 && protoProps === undefined) { 9981cb0ef41Sopenharmony_ci return prefix + 9991cb0ef41Sopenharmony_ci `{ byteLength: ${formatNumber(ctx.stylize, value.byteLength, false)} }`; 10001cb0ef41Sopenharmony_ci } 10011cb0ef41Sopenharmony_ci braces[0] = `${prefix}{`; 10021cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(keys, 'byteLength'); 10031cb0ef41Sopenharmony_ci } else if (isDataView(value)) { 10041cb0ef41Sopenharmony_ci braces[0] = `${getPrefix(constructor, tag, 'DataView')}{`; 10051cb0ef41Sopenharmony_ci // .buffer goes last, it's not a primitive like the others. 10061cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(keys, 'byteLength', 'byteOffset', 'buffer'); 10071cb0ef41Sopenharmony_ci } else if (isPromise(value)) { 10081cb0ef41Sopenharmony_ci braces[0] = `${getPrefix(constructor, tag, 'Promise')}{`; 10091cb0ef41Sopenharmony_ci formatter = formatPromise; 10101cb0ef41Sopenharmony_ci } else if (isWeakSet(value)) { 10111cb0ef41Sopenharmony_ci braces[0] = `${getPrefix(constructor, tag, 'WeakSet')}{`; 10121cb0ef41Sopenharmony_ci formatter = ctx.showHidden ? formatWeakSet : formatWeakCollection; 10131cb0ef41Sopenharmony_ci } else if (isWeakMap(value)) { 10141cb0ef41Sopenharmony_ci braces[0] = `${getPrefix(constructor, tag, 'WeakMap')}{`; 10151cb0ef41Sopenharmony_ci formatter = ctx.showHidden ? formatWeakMap : formatWeakCollection; 10161cb0ef41Sopenharmony_ci } else if (isModuleNamespaceObject(value)) { 10171cb0ef41Sopenharmony_ci braces[0] = `${getPrefix(constructor, tag, 'Module')}{`; 10181cb0ef41Sopenharmony_ci // Special handle keys for namespace objects. 10191cb0ef41Sopenharmony_ci formatter = FunctionPrototypeBind(formatNamespaceObject, null, keys); 10201cb0ef41Sopenharmony_ci } else if (isBoxedPrimitive(value)) { 10211cb0ef41Sopenharmony_ci base = getBoxedBase(value, ctx, keys, constructor, tag); 10221cb0ef41Sopenharmony_ci if (keys.length === 0 && protoProps === undefined) { 10231cb0ef41Sopenharmony_ci return base; 10241cb0ef41Sopenharmony_ci } 10251cb0ef41Sopenharmony_ci } else { 10261cb0ef41Sopenharmony_ci if (keys.length === 0 && protoProps === undefined) { 10271cb0ef41Sopenharmony_ci if (isExternal(value)) { 10281cb0ef41Sopenharmony_ci const address = getExternalValue(value).toString(16); 10291cb0ef41Sopenharmony_ci return ctx.stylize(`[External: ${address}]`, 'special'); 10301cb0ef41Sopenharmony_ci } 10311cb0ef41Sopenharmony_ci return `${getCtxStyle(value, constructor, tag)}{}`; 10321cb0ef41Sopenharmony_ci } 10331cb0ef41Sopenharmony_ci braces[0] = `${getCtxStyle(value, constructor, tag)}{`; 10341cb0ef41Sopenharmony_ci } 10351cb0ef41Sopenharmony_ci } 10361cb0ef41Sopenharmony_ci 10371cb0ef41Sopenharmony_ci if (recurseTimes > ctx.depth && ctx.depth !== null) { 10381cb0ef41Sopenharmony_ci let constructorName = StringPrototypeSlice(getCtxStyle(value, constructor, tag), 0, -1); 10391cb0ef41Sopenharmony_ci if (constructor !== null) 10401cb0ef41Sopenharmony_ci constructorName = `[${constructorName}]`; 10411cb0ef41Sopenharmony_ci return ctx.stylize(constructorName, 'special'); 10421cb0ef41Sopenharmony_ci } 10431cb0ef41Sopenharmony_ci recurseTimes += 1; 10441cb0ef41Sopenharmony_ci 10451cb0ef41Sopenharmony_ci ArrayPrototypePush(ctx.seen, value); 10461cb0ef41Sopenharmony_ci ctx.currentDepth = recurseTimes; 10471cb0ef41Sopenharmony_ci let output; 10481cb0ef41Sopenharmony_ci const indentationLvl = ctx.indentationLvl; 10491cb0ef41Sopenharmony_ci try { 10501cb0ef41Sopenharmony_ci output = formatter(ctx, value, recurseTimes); 10511cb0ef41Sopenharmony_ci for (i = 0; i < keys.length; i++) { 10521cb0ef41Sopenharmony_ci ArrayPrototypePush( 10531cb0ef41Sopenharmony_ci output, 10541cb0ef41Sopenharmony_ci formatProperty(ctx, value, recurseTimes, keys[i], extrasType), 10551cb0ef41Sopenharmony_ci ); 10561cb0ef41Sopenharmony_ci } 10571cb0ef41Sopenharmony_ci if (protoProps !== undefined) { 10581cb0ef41Sopenharmony_ci ArrayPrototypePushApply(output, protoProps); 10591cb0ef41Sopenharmony_ci } 10601cb0ef41Sopenharmony_ci } catch (err) { 10611cb0ef41Sopenharmony_ci const constructorName = StringPrototypeSlice(getCtxStyle(value, constructor, tag), 0, -1); 10621cb0ef41Sopenharmony_ci return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl); 10631cb0ef41Sopenharmony_ci } 10641cb0ef41Sopenharmony_ci if (ctx.circular !== undefined) { 10651cb0ef41Sopenharmony_ci const index = ctx.circular.get(value); 10661cb0ef41Sopenharmony_ci if (index !== undefined) { 10671cb0ef41Sopenharmony_ci const reference = ctx.stylize(`<ref *${index}>`, 'special'); 10681cb0ef41Sopenharmony_ci // Add reference always to the very beginning of the output. 10691cb0ef41Sopenharmony_ci if (ctx.compact !== true) { 10701cb0ef41Sopenharmony_ci base = base === '' ? reference : `${reference} ${base}`; 10711cb0ef41Sopenharmony_ci } else { 10721cb0ef41Sopenharmony_ci braces[0] = `${reference} ${braces[0]}`; 10731cb0ef41Sopenharmony_ci } 10741cb0ef41Sopenharmony_ci } 10751cb0ef41Sopenharmony_ci } 10761cb0ef41Sopenharmony_ci ArrayPrototypePop(ctx.seen); 10771cb0ef41Sopenharmony_ci 10781cb0ef41Sopenharmony_ci if (ctx.sorted) { 10791cb0ef41Sopenharmony_ci const comparator = ctx.sorted === true ? undefined : ctx.sorted; 10801cb0ef41Sopenharmony_ci if (extrasType === kObjectType) { 10811cb0ef41Sopenharmony_ci ArrayPrototypeSort(output, comparator); 10821cb0ef41Sopenharmony_ci } else if (keys.length > 1) { 10831cb0ef41Sopenharmony_ci const sorted = ArrayPrototypeSort(ArrayPrototypeSlice(output, output.length - keys.length), comparator); 10841cb0ef41Sopenharmony_ci ArrayPrototypeUnshift(sorted, output, output.length - keys.length, keys.length); 10851cb0ef41Sopenharmony_ci ReflectApply(ArrayPrototypeSplice, null, sorted); 10861cb0ef41Sopenharmony_ci } 10871cb0ef41Sopenharmony_ci } 10881cb0ef41Sopenharmony_ci 10891cb0ef41Sopenharmony_ci const res = reduceToSingleString( 10901cb0ef41Sopenharmony_ci ctx, output, base, braces, extrasType, recurseTimes, value); 10911cb0ef41Sopenharmony_ci const budget = ctx.budget[ctx.indentationLvl] || 0; 10921cb0ef41Sopenharmony_ci const newLength = budget + res.length; 10931cb0ef41Sopenharmony_ci ctx.budget[ctx.indentationLvl] = newLength; 10941cb0ef41Sopenharmony_ci // If any indentationLvl exceeds this limit, limit further inspecting to the 10951cb0ef41Sopenharmony_ci // minimum. Otherwise the recursive algorithm might continue inspecting the 10961cb0ef41Sopenharmony_ci // object even though the maximum string size (~2 ** 28 on 32 bit systems and 10971cb0ef41Sopenharmony_ci // ~2 ** 30 on 64 bit systems) exceeded. The actual output is not limited at 10981cb0ef41Sopenharmony_ci // exactly 2 ** 27 but a bit higher. This depends on the object shape. 10991cb0ef41Sopenharmony_ci // This limit also makes sure that huge objects don't block the event loop 11001cb0ef41Sopenharmony_ci // significantly. 11011cb0ef41Sopenharmony_ci if (newLength > 2 ** 27) { 11021cb0ef41Sopenharmony_ci ctx.depth = -1; 11031cb0ef41Sopenharmony_ci } 11041cb0ef41Sopenharmony_ci return res; 11051cb0ef41Sopenharmony_ci} 11061cb0ef41Sopenharmony_ci 11071cb0ef41Sopenharmony_cifunction getIteratorBraces(type, tag) { 11081cb0ef41Sopenharmony_ci if (tag !== `${type} Iterator`) { 11091cb0ef41Sopenharmony_ci if (tag !== '') 11101cb0ef41Sopenharmony_ci tag += '] ['; 11111cb0ef41Sopenharmony_ci tag += `${type} Iterator`; 11121cb0ef41Sopenharmony_ci } 11131cb0ef41Sopenharmony_ci return [`[${tag}] {`, '}']; 11141cb0ef41Sopenharmony_ci} 11151cb0ef41Sopenharmony_ci 11161cb0ef41Sopenharmony_cifunction getBoxedBase(value, ctx, keys, constructor, tag) { 11171cb0ef41Sopenharmony_ci let fn; 11181cb0ef41Sopenharmony_ci let type; 11191cb0ef41Sopenharmony_ci if (isNumberObject(value)) { 11201cb0ef41Sopenharmony_ci fn = NumberPrototypeValueOf; 11211cb0ef41Sopenharmony_ci type = 'Number'; 11221cb0ef41Sopenharmony_ci } else if (isStringObject(value)) { 11231cb0ef41Sopenharmony_ci fn = StringPrototypeValueOf; 11241cb0ef41Sopenharmony_ci type = 'String'; 11251cb0ef41Sopenharmony_ci // For boxed Strings, we have to remove the 0-n indexed entries, 11261cb0ef41Sopenharmony_ci // since they just noisy up the output and are redundant 11271cb0ef41Sopenharmony_ci // Make boxed primitive Strings look like such 11281cb0ef41Sopenharmony_ci keys.splice(0, value.length); 11291cb0ef41Sopenharmony_ci } else if (isBooleanObject(value)) { 11301cb0ef41Sopenharmony_ci fn = BooleanPrototypeValueOf; 11311cb0ef41Sopenharmony_ci type = 'Boolean'; 11321cb0ef41Sopenharmony_ci } else if (isBigIntObject(value)) { 11331cb0ef41Sopenharmony_ci fn = BigIntPrototypeValueOf; 11341cb0ef41Sopenharmony_ci type = 'BigInt'; 11351cb0ef41Sopenharmony_ci } else { 11361cb0ef41Sopenharmony_ci fn = SymbolPrototypeValueOf; 11371cb0ef41Sopenharmony_ci type = 'Symbol'; 11381cb0ef41Sopenharmony_ci } 11391cb0ef41Sopenharmony_ci let base = `[${type}`; 11401cb0ef41Sopenharmony_ci if (type !== constructor) { 11411cb0ef41Sopenharmony_ci if (constructor === null) { 11421cb0ef41Sopenharmony_ci base += ' (null prototype)'; 11431cb0ef41Sopenharmony_ci } else { 11441cb0ef41Sopenharmony_ci base += ` (${constructor})`; 11451cb0ef41Sopenharmony_ci } 11461cb0ef41Sopenharmony_ci } 11471cb0ef41Sopenharmony_ci base += `: ${formatPrimitive(stylizeNoColor, fn(value), ctx)}]`; 11481cb0ef41Sopenharmony_ci if (tag !== '' && tag !== constructor) { 11491cb0ef41Sopenharmony_ci base += ` [${tag}]`; 11501cb0ef41Sopenharmony_ci } 11511cb0ef41Sopenharmony_ci if (keys.length !== 0 || ctx.stylize === stylizeNoColor) 11521cb0ef41Sopenharmony_ci return base; 11531cb0ef41Sopenharmony_ci return ctx.stylize(base, StringPrototypeToLowerCase(type)); 11541cb0ef41Sopenharmony_ci} 11551cb0ef41Sopenharmony_ci 11561cb0ef41Sopenharmony_cifunction getClassBase(value, constructor, tag) { 11571cb0ef41Sopenharmony_ci const hasName = ObjectPrototypeHasOwnProperty(value, 'name'); 11581cb0ef41Sopenharmony_ci const name = (hasName && value.name) || '(anonymous)'; 11591cb0ef41Sopenharmony_ci let base = `class ${name}`; 11601cb0ef41Sopenharmony_ci if (constructor !== 'Function' && constructor !== null) { 11611cb0ef41Sopenharmony_ci base += ` [${constructor}]`; 11621cb0ef41Sopenharmony_ci } 11631cb0ef41Sopenharmony_ci if (tag !== '' && constructor !== tag) { 11641cb0ef41Sopenharmony_ci base += ` [${tag}]`; 11651cb0ef41Sopenharmony_ci } 11661cb0ef41Sopenharmony_ci if (constructor !== null) { 11671cb0ef41Sopenharmony_ci const superName = ObjectGetPrototypeOf(value).name; 11681cb0ef41Sopenharmony_ci if (superName) { 11691cb0ef41Sopenharmony_ci base += ` extends ${superName}`; 11701cb0ef41Sopenharmony_ci } 11711cb0ef41Sopenharmony_ci } else { 11721cb0ef41Sopenharmony_ci base += ' extends [null prototype]'; 11731cb0ef41Sopenharmony_ci } 11741cb0ef41Sopenharmony_ci return `[${base}]`; 11751cb0ef41Sopenharmony_ci} 11761cb0ef41Sopenharmony_ci 11771cb0ef41Sopenharmony_cifunction getFunctionBase(value, constructor, tag) { 11781cb0ef41Sopenharmony_ci const stringified = FunctionPrototypeToString(value); 11791cb0ef41Sopenharmony_ci if (StringPrototypeStartsWith(stringified, 'class') && StringPrototypeEndsWith(stringified, '}')) { 11801cb0ef41Sopenharmony_ci const slice = StringPrototypeSlice(stringified, 5, -1); 11811cb0ef41Sopenharmony_ci const bracketIndex = StringPrototypeIndexOf(slice, '{'); 11821cb0ef41Sopenharmony_ci if (bracketIndex !== -1 && 11831cb0ef41Sopenharmony_ci (!StringPrototypeIncludes(StringPrototypeSlice(slice, 0, bracketIndex), '(') || 11841cb0ef41Sopenharmony_ci // Slow path to guarantee that it's indeed a class. 11851cb0ef41Sopenharmony_ci RegExpPrototypeExec(classRegExp, RegExpPrototypeSymbolReplace(stripCommentsRegExp, slice)) !== null) 11861cb0ef41Sopenharmony_ci ) { 11871cb0ef41Sopenharmony_ci return getClassBase(value, constructor, tag); 11881cb0ef41Sopenharmony_ci } 11891cb0ef41Sopenharmony_ci } 11901cb0ef41Sopenharmony_ci let type = 'Function'; 11911cb0ef41Sopenharmony_ci if (isGeneratorFunction(value)) { 11921cb0ef41Sopenharmony_ci type = `Generator${type}`; 11931cb0ef41Sopenharmony_ci } 11941cb0ef41Sopenharmony_ci if (isAsyncFunction(value)) { 11951cb0ef41Sopenharmony_ci type = `Async${type}`; 11961cb0ef41Sopenharmony_ci } 11971cb0ef41Sopenharmony_ci let base = `[${type}`; 11981cb0ef41Sopenharmony_ci if (constructor === null) { 11991cb0ef41Sopenharmony_ci base += ' (null prototype)'; 12001cb0ef41Sopenharmony_ci } 12011cb0ef41Sopenharmony_ci if (value.name === '') { 12021cb0ef41Sopenharmony_ci base += ' (anonymous)'; 12031cb0ef41Sopenharmony_ci } else { 12041cb0ef41Sopenharmony_ci base += `: ${value.name}`; 12051cb0ef41Sopenharmony_ci } 12061cb0ef41Sopenharmony_ci base += ']'; 12071cb0ef41Sopenharmony_ci if (constructor !== type && constructor !== null) { 12081cb0ef41Sopenharmony_ci base += ` ${constructor}`; 12091cb0ef41Sopenharmony_ci } 12101cb0ef41Sopenharmony_ci if (tag !== '' && constructor !== tag) { 12111cb0ef41Sopenharmony_ci base += ` [${tag}]`; 12121cb0ef41Sopenharmony_ci } 12131cb0ef41Sopenharmony_ci return base; 12141cb0ef41Sopenharmony_ci} 12151cb0ef41Sopenharmony_ci 12161cb0ef41Sopenharmony_cifunction identicalSequenceRange(a, b) { 12171cb0ef41Sopenharmony_ci for (let i = 0; i < a.length - 3; i++) { 12181cb0ef41Sopenharmony_ci // Find the first entry of b that matches the current entry of a. 12191cb0ef41Sopenharmony_ci const pos = ArrayPrototypeIndexOf(b, a[i]); 12201cb0ef41Sopenharmony_ci if (pos !== -1) { 12211cb0ef41Sopenharmony_ci const rest = b.length - pos; 12221cb0ef41Sopenharmony_ci if (rest > 3) { 12231cb0ef41Sopenharmony_ci let len = 1; 12241cb0ef41Sopenharmony_ci const maxLen = MathMin(a.length - i, rest); 12251cb0ef41Sopenharmony_ci // Count the number of consecutive entries. 12261cb0ef41Sopenharmony_ci while (maxLen > len && a[i + len] === b[pos + len]) { 12271cb0ef41Sopenharmony_ci len++; 12281cb0ef41Sopenharmony_ci } 12291cb0ef41Sopenharmony_ci if (len > 3) { 12301cb0ef41Sopenharmony_ci return { len, offset: i }; 12311cb0ef41Sopenharmony_ci } 12321cb0ef41Sopenharmony_ci } 12331cb0ef41Sopenharmony_ci } 12341cb0ef41Sopenharmony_ci } 12351cb0ef41Sopenharmony_ci 12361cb0ef41Sopenharmony_ci return { len: 0, offset: 0 }; 12371cb0ef41Sopenharmony_ci} 12381cb0ef41Sopenharmony_ci 12391cb0ef41Sopenharmony_cifunction getStackString(error) { 12401cb0ef41Sopenharmony_ci return error.stack ? String(error.stack) : ErrorPrototypeToString(error); 12411cb0ef41Sopenharmony_ci} 12421cb0ef41Sopenharmony_ci 12431cb0ef41Sopenharmony_cifunction getStackFrames(ctx, err, stack) { 12441cb0ef41Sopenharmony_ci const frames = StringPrototypeSplit(stack, '\n'); 12451cb0ef41Sopenharmony_ci 12461cb0ef41Sopenharmony_ci let cause; 12471cb0ef41Sopenharmony_ci try { 12481cb0ef41Sopenharmony_ci ({ cause } = err); 12491cb0ef41Sopenharmony_ci } catch { 12501cb0ef41Sopenharmony_ci // If 'cause' is a getter that throws, ignore it. 12511cb0ef41Sopenharmony_ci } 12521cb0ef41Sopenharmony_ci 12531cb0ef41Sopenharmony_ci // Remove stack frames identical to frames in cause. 12541cb0ef41Sopenharmony_ci if (cause != null && isError(cause)) { 12551cb0ef41Sopenharmony_ci const causeStack = getStackString(cause); 12561cb0ef41Sopenharmony_ci const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at'); 12571cb0ef41Sopenharmony_ci if (causeStackStart !== -1) { 12581cb0ef41Sopenharmony_ci const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n'); 12591cb0ef41Sopenharmony_ci const { len, offset } = identicalSequenceRange(frames, causeFrames); 12601cb0ef41Sopenharmony_ci if (len > 0) { 12611cb0ef41Sopenharmony_ci const skipped = len - 2; 12621cb0ef41Sopenharmony_ci const msg = ` ... ${skipped} lines matching cause stack trace ...`; 12631cb0ef41Sopenharmony_ci frames.splice(offset + 1, skipped, ctx.stylize(msg, 'undefined')); 12641cb0ef41Sopenharmony_ci } 12651cb0ef41Sopenharmony_ci } 12661cb0ef41Sopenharmony_ci } 12671cb0ef41Sopenharmony_ci return frames; 12681cb0ef41Sopenharmony_ci} 12691cb0ef41Sopenharmony_ci 12701cb0ef41Sopenharmony_cifunction improveStack(stack, constructor, name, tag) { 12711cb0ef41Sopenharmony_ci // A stack trace may contain arbitrary data. Only manipulate the output 12721cb0ef41Sopenharmony_ci // for "regular errors" (errors that "look normal") for now. 12731cb0ef41Sopenharmony_ci let len = name.length; 12741cb0ef41Sopenharmony_ci 12751cb0ef41Sopenharmony_ci if (constructor === null || 12761cb0ef41Sopenharmony_ci (StringPrototypeEndsWith(name, 'Error') && 12771cb0ef41Sopenharmony_ci StringPrototypeStartsWith(stack, name) && 12781cb0ef41Sopenharmony_ci (stack.length === len || stack[len] === ':' || stack[len] === '\n'))) { 12791cb0ef41Sopenharmony_ci let fallback = 'Error'; 12801cb0ef41Sopenharmony_ci if (constructor === null) { 12811cb0ef41Sopenharmony_ci const start = RegExpPrototypeExec(/^([A-Z][a-z_ A-Z0-9[\]()-]+)(?::|\n {4}at)/, stack) || 12821cb0ef41Sopenharmony_ci RegExpPrototypeExec(/^([a-z_A-Z0-9-]*Error)$/, stack); 12831cb0ef41Sopenharmony_ci fallback = (start && start[1]) || ''; 12841cb0ef41Sopenharmony_ci len = fallback.length; 12851cb0ef41Sopenharmony_ci fallback = fallback || 'Error'; 12861cb0ef41Sopenharmony_ci } 12871cb0ef41Sopenharmony_ci const prefix = StringPrototypeSlice(getPrefix(constructor, tag, fallback), 0, -1); 12881cb0ef41Sopenharmony_ci if (name !== prefix) { 12891cb0ef41Sopenharmony_ci if (StringPrototypeIncludes(prefix, name)) { 12901cb0ef41Sopenharmony_ci if (len === 0) { 12911cb0ef41Sopenharmony_ci stack = `${prefix}: ${stack}`; 12921cb0ef41Sopenharmony_ci } else { 12931cb0ef41Sopenharmony_ci stack = `${prefix}${StringPrototypeSlice(stack, len)}`; 12941cb0ef41Sopenharmony_ci } 12951cb0ef41Sopenharmony_ci } else { 12961cb0ef41Sopenharmony_ci stack = `${prefix} [${name}]${StringPrototypeSlice(stack, len)}`; 12971cb0ef41Sopenharmony_ci } 12981cb0ef41Sopenharmony_ci } 12991cb0ef41Sopenharmony_ci } 13001cb0ef41Sopenharmony_ci return stack; 13011cb0ef41Sopenharmony_ci} 13021cb0ef41Sopenharmony_ci 13031cb0ef41Sopenharmony_cifunction removeDuplicateErrorKeys(ctx, keys, err, stack) { 13041cb0ef41Sopenharmony_ci if (!ctx.showHidden && keys.length !== 0) { 13051cb0ef41Sopenharmony_ci for (const name of ['name', 'message', 'stack']) { 13061cb0ef41Sopenharmony_ci const index = ArrayPrototypeIndexOf(keys, name); 13071cb0ef41Sopenharmony_ci // Only hide the property in case it's part of the original stack 13081cb0ef41Sopenharmony_ci if (index !== -1 && StringPrototypeIncludes(stack, err[name])) { 13091cb0ef41Sopenharmony_ci ArrayPrototypeSplice(keys, index, 1); 13101cb0ef41Sopenharmony_ci } 13111cb0ef41Sopenharmony_ci } 13121cb0ef41Sopenharmony_ci } 13131cb0ef41Sopenharmony_ci} 13141cb0ef41Sopenharmony_ci 13151cb0ef41Sopenharmony_cifunction markNodeModules(ctx, line) { 13161cb0ef41Sopenharmony_ci let tempLine = ''; 13171cb0ef41Sopenharmony_ci let nodeModule; 13181cb0ef41Sopenharmony_ci let pos = 0; 13191cb0ef41Sopenharmony_ci while ((nodeModule = nodeModulesRegExp.exec(line)) !== null) { 13201cb0ef41Sopenharmony_ci // '/node_modules/'.length === 14 13211cb0ef41Sopenharmony_ci tempLine += StringPrototypeSlice(line, pos, nodeModule.index + 14); 13221cb0ef41Sopenharmony_ci tempLine += ctx.stylize(nodeModule[1], 'module'); 13231cb0ef41Sopenharmony_ci pos = nodeModule.index + nodeModule[0].length; 13241cb0ef41Sopenharmony_ci } 13251cb0ef41Sopenharmony_ci if (pos !== 0) { 13261cb0ef41Sopenharmony_ci line = tempLine + StringPrototypeSlice(line, pos); 13271cb0ef41Sopenharmony_ci } 13281cb0ef41Sopenharmony_ci return line; 13291cb0ef41Sopenharmony_ci} 13301cb0ef41Sopenharmony_ci 13311cb0ef41Sopenharmony_cifunction markCwd(ctx, line, workingDirectory) { 13321cb0ef41Sopenharmony_ci let cwdStartPos = StringPrototypeIndexOf(line, workingDirectory); 13331cb0ef41Sopenharmony_ci let tempLine = ''; 13341cb0ef41Sopenharmony_ci let cwdLength = workingDirectory.length; 13351cb0ef41Sopenharmony_ci if (cwdStartPos !== -1) { 13361cb0ef41Sopenharmony_ci if (StringPrototypeSlice(line, cwdStartPos - 7, cwdStartPos) === 'file://') { 13371cb0ef41Sopenharmony_ci cwdLength += 7; 13381cb0ef41Sopenharmony_ci cwdStartPos -= 7; 13391cb0ef41Sopenharmony_ci } 13401cb0ef41Sopenharmony_ci const start = line[cwdStartPos - 1] === '(' ? cwdStartPos - 1 : cwdStartPos; 13411cb0ef41Sopenharmony_ci const end = start !== cwdStartPos && StringPrototypeEndsWith(line, ')') ? -1 : line.length; 13421cb0ef41Sopenharmony_ci const workingDirectoryEndPos = cwdStartPos + cwdLength + 1; 13431cb0ef41Sopenharmony_ci const cwdSlice = StringPrototypeSlice(line, start, workingDirectoryEndPos); 13441cb0ef41Sopenharmony_ci 13451cb0ef41Sopenharmony_ci tempLine += StringPrototypeSlice(line, 0, start); 13461cb0ef41Sopenharmony_ci tempLine += ctx.stylize(cwdSlice, 'undefined'); 13471cb0ef41Sopenharmony_ci tempLine += StringPrototypeSlice(line, workingDirectoryEndPos, end); 13481cb0ef41Sopenharmony_ci if (end === -1) { 13491cb0ef41Sopenharmony_ci tempLine += ctx.stylize(')', 'undefined'); 13501cb0ef41Sopenharmony_ci } 13511cb0ef41Sopenharmony_ci } else { 13521cb0ef41Sopenharmony_ci tempLine += line; 13531cb0ef41Sopenharmony_ci } 13541cb0ef41Sopenharmony_ci return tempLine; 13551cb0ef41Sopenharmony_ci} 13561cb0ef41Sopenharmony_ci 13571cb0ef41Sopenharmony_cifunction safeGetCWD() { 13581cb0ef41Sopenharmony_ci let workingDirectory; 13591cb0ef41Sopenharmony_ci try { 13601cb0ef41Sopenharmony_ci workingDirectory = process.cwd(); 13611cb0ef41Sopenharmony_ci } catch { 13621cb0ef41Sopenharmony_ci return; 13631cb0ef41Sopenharmony_ci } 13641cb0ef41Sopenharmony_ci return workingDirectory; 13651cb0ef41Sopenharmony_ci} 13661cb0ef41Sopenharmony_ci 13671cb0ef41Sopenharmony_cifunction formatError(err, constructor, tag, ctx, keys) { 13681cb0ef41Sopenharmony_ci const name = err.name != null ? String(err.name) : 'Error'; 13691cb0ef41Sopenharmony_ci let stack = getStackString(err); 13701cb0ef41Sopenharmony_ci 13711cb0ef41Sopenharmony_ci removeDuplicateErrorKeys(ctx, keys, err, stack); 13721cb0ef41Sopenharmony_ci 13731cb0ef41Sopenharmony_ci if ('cause' in err && 13741cb0ef41Sopenharmony_ci (keys.length === 0 || !ArrayPrototypeIncludes(keys, 'cause'))) { 13751cb0ef41Sopenharmony_ci ArrayPrototypePush(keys, 'cause'); 13761cb0ef41Sopenharmony_ci } 13771cb0ef41Sopenharmony_ci 13781cb0ef41Sopenharmony_ci // Print errors aggregated into AggregateError 13791cb0ef41Sopenharmony_ci if (ArrayIsArray(err.errors) && 13801cb0ef41Sopenharmony_ci (keys.length === 0 || !ArrayPrototypeIncludes(keys, 'errors'))) { 13811cb0ef41Sopenharmony_ci ArrayPrototypePush(keys, 'errors'); 13821cb0ef41Sopenharmony_ci } 13831cb0ef41Sopenharmony_ci 13841cb0ef41Sopenharmony_ci stack = improveStack(stack, constructor, name, tag); 13851cb0ef41Sopenharmony_ci 13861cb0ef41Sopenharmony_ci // Ignore the error message if it's contained in the stack. 13871cb0ef41Sopenharmony_ci let pos = (err.message && StringPrototypeIndexOf(stack, err.message)) || -1; 13881cb0ef41Sopenharmony_ci if (pos !== -1) 13891cb0ef41Sopenharmony_ci pos += err.message.length; 13901cb0ef41Sopenharmony_ci // Wrap the error in brackets in case it has no stack trace. 13911cb0ef41Sopenharmony_ci const stackStart = StringPrototypeIndexOf(stack, '\n at', pos); 13921cb0ef41Sopenharmony_ci if (stackStart === -1) { 13931cb0ef41Sopenharmony_ci stack = `[${stack}]`; 13941cb0ef41Sopenharmony_ci } else { 13951cb0ef41Sopenharmony_ci let newStack = StringPrototypeSlice(stack, 0, stackStart); 13961cb0ef41Sopenharmony_ci const stackFramePart = StringPrototypeSlice(stack, stackStart + 1); 13971cb0ef41Sopenharmony_ci const lines = getStackFrames(ctx, err, stackFramePart); 13981cb0ef41Sopenharmony_ci if (ctx.colors) { 13991cb0ef41Sopenharmony_ci // Highlight userland code and node modules. 14001cb0ef41Sopenharmony_ci const workingDirectory = safeGetCWD(); 14011cb0ef41Sopenharmony_ci let esmWorkingDirectory; 14021cb0ef41Sopenharmony_ci for (let line of lines) { 14031cb0ef41Sopenharmony_ci const core = RegExpPrototypeExec(coreModuleRegExp, line); 14041cb0ef41Sopenharmony_ci if (core !== null && BuiltinModule.exists(core[1])) { 14051cb0ef41Sopenharmony_ci newStack += `\n${ctx.stylize(line, 'undefined')}`; 14061cb0ef41Sopenharmony_ci } else { 14071cb0ef41Sopenharmony_ci newStack += '\n'; 14081cb0ef41Sopenharmony_ci 14091cb0ef41Sopenharmony_ci line = markNodeModules(ctx, line); 14101cb0ef41Sopenharmony_ci if (workingDirectory !== undefined) { 14111cb0ef41Sopenharmony_ci let newLine = markCwd(ctx, line, workingDirectory); 14121cb0ef41Sopenharmony_ci if (newLine === line) { 14131cb0ef41Sopenharmony_ci esmWorkingDirectory ??= pathToFileUrlHref(workingDirectory); 14141cb0ef41Sopenharmony_ci newLine = markCwd(ctx, line, esmWorkingDirectory); 14151cb0ef41Sopenharmony_ci } 14161cb0ef41Sopenharmony_ci line = newLine; 14171cb0ef41Sopenharmony_ci } 14181cb0ef41Sopenharmony_ci 14191cb0ef41Sopenharmony_ci newStack += line; 14201cb0ef41Sopenharmony_ci } 14211cb0ef41Sopenharmony_ci } 14221cb0ef41Sopenharmony_ci } else { 14231cb0ef41Sopenharmony_ci newStack += `\n${ArrayPrototypeJoin(lines, '\n')}`; 14241cb0ef41Sopenharmony_ci } 14251cb0ef41Sopenharmony_ci stack = newStack; 14261cb0ef41Sopenharmony_ci } 14271cb0ef41Sopenharmony_ci // The message and the stack have to be indented as well! 14281cb0ef41Sopenharmony_ci if (ctx.indentationLvl !== 0) { 14291cb0ef41Sopenharmony_ci const indentation = StringPrototypeRepeat(' ', ctx.indentationLvl); 14301cb0ef41Sopenharmony_ci stack = StringPrototypeReplaceAll(stack, '\n', `\n${indentation}`); 14311cb0ef41Sopenharmony_ci } 14321cb0ef41Sopenharmony_ci return stack; 14331cb0ef41Sopenharmony_ci} 14341cb0ef41Sopenharmony_ci 14351cb0ef41Sopenharmony_cifunction groupArrayElements(ctx, output, value) { 14361cb0ef41Sopenharmony_ci let totalLength = 0; 14371cb0ef41Sopenharmony_ci let maxLength = 0; 14381cb0ef41Sopenharmony_ci let i = 0; 14391cb0ef41Sopenharmony_ci let outputLength = output.length; 14401cb0ef41Sopenharmony_ci if (ctx.maxArrayLength < output.length) { 14411cb0ef41Sopenharmony_ci // This makes sure the "... n more items" part is not taken into account. 14421cb0ef41Sopenharmony_ci outputLength--; 14431cb0ef41Sopenharmony_ci } 14441cb0ef41Sopenharmony_ci const separatorSpace = 2; // Add 1 for the space and 1 for the separator. 14451cb0ef41Sopenharmony_ci const dataLen = new Array(outputLength); 14461cb0ef41Sopenharmony_ci // Calculate the total length of all output entries and the individual max 14471cb0ef41Sopenharmony_ci // entries length of all output entries. We have to remove colors first, 14481cb0ef41Sopenharmony_ci // otherwise the length would not be calculated properly. 14491cb0ef41Sopenharmony_ci for (; i < outputLength; i++) { 14501cb0ef41Sopenharmony_ci const len = getStringWidth(output[i], ctx.colors); 14511cb0ef41Sopenharmony_ci dataLen[i] = len; 14521cb0ef41Sopenharmony_ci totalLength += len + separatorSpace; 14531cb0ef41Sopenharmony_ci if (maxLength < len) 14541cb0ef41Sopenharmony_ci maxLength = len; 14551cb0ef41Sopenharmony_ci } 14561cb0ef41Sopenharmony_ci // Add two to `maxLength` as we add a single whitespace character plus a comma 14571cb0ef41Sopenharmony_ci // in-between two entries. 14581cb0ef41Sopenharmony_ci const actualMax = maxLength + separatorSpace; 14591cb0ef41Sopenharmony_ci // Check if at least three entries fit next to each other and prevent grouping 14601cb0ef41Sopenharmony_ci // of arrays that contains entries of very different length (i.e., if a single 14611cb0ef41Sopenharmony_ci // entry is longer than 1/5 of all other entries combined). Otherwise the 14621cb0ef41Sopenharmony_ci // space in-between small entries would be enormous. 14631cb0ef41Sopenharmony_ci if (actualMax * 3 + ctx.indentationLvl < ctx.breakLength && 14641cb0ef41Sopenharmony_ci (totalLength / actualMax > 5 || maxLength <= 6)) { 14651cb0ef41Sopenharmony_ci 14661cb0ef41Sopenharmony_ci const approxCharHeights = 2.5; 14671cb0ef41Sopenharmony_ci const averageBias = MathSqrt(actualMax - totalLength / output.length); 14681cb0ef41Sopenharmony_ci const biasedMax = MathMax(actualMax - 3 - averageBias, 1); 14691cb0ef41Sopenharmony_ci // Dynamically check how many columns seem possible. 14701cb0ef41Sopenharmony_ci const columns = MathMin( 14711cb0ef41Sopenharmony_ci // Ideally a square should be drawn. We expect a character to be about 2.5 14721cb0ef41Sopenharmony_ci // times as high as wide. This is the area formula to calculate a square 14731cb0ef41Sopenharmony_ci // which contains n rectangles of size `actualMax * approxCharHeights`. 14741cb0ef41Sopenharmony_ci // Divide that by `actualMax` to receive the correct number of columns. 14751cb0ef41Sopenharmony_ci // The added bias increases the columns for short entries. 14761cb0ef41Sopenharmony_ci MathRound( 14771cb0ef41Sopenharmony_ci MathSqrt( 14781cb0ef41Sopenharmony_ci approxCharHeights * biasedMax * outputLength, 14791cb0ef41Sopenharmony_ci ) / biasedMax, 14801cb0ef41Sopenharmony_ci ), 14811cb0ef41Sopenharmony_ci // Do not exceed the breakLength. 14821cb0ef41Sopenharmony_ci MathFloor((ctx.breakLength - ctx.indentationLvl) / actualMax), 14831cb0ef41Sopenharmony_ci // Limit array grouping for small `compact` modes as the user requested 14841cb0ef41Sopenharmony_ci // minimal grouping. 14851cb0ef41Sopenharmony_ci ctx.compact * 4, 14861cb0ef41Sopenharmony_ci // Limit the columns to a maximum of fifteen. 14871cb0ef41Sopenharmony_ci 15, 14881cb0ef41Sopenharmony_ci ); 14891cb0ef41Sopenharmony_ci // Return with the original output if no grouping should happen. 14901cb0ef41Sopenharmony_ci if (columns <= 1) { 14911cb0ef41Sopenharmony_ci return output; 14921cb0ef41Sopenharmony_ci } 14931cb0ef41Sopenharmony_ci const tmp = []; 14941cb0ef41Sopenharmony_ci const maxLineLength = []; 14951cb0ef41Sopenharmony_ci for (let i = 0; i < columns; i++) { 14961cb0ef41Sopenharmony_ci let lineMaxLength = 0; 14971cb0ef41Sopenharmony_ci for (let j = i; j < output.length; j += columns) { 14981cb0ef41Sopenharmony_ci if (dataLen[j] > lineMaxLength) 14991cb0ef41Sopenharmony_ci lineMaxLength = dataLen[j]; 15001cb0ef41Sopenharmony_ci } 15011cb0ef41Sopenharmony_ci lineMaxLength += separatorSpace; 15021cb0ef41Sopenharmony_ci maxLineLength[i] = lineMaxLength; 15031cb0ef41Sopenharmony_ci } 15041cb0ef41Sopenharmony_ci let order = StringPrototypePadStart; 15051cb0ef41Sopenharmony_ci if (value !== undefined) { 15061cb0ef41Sopenharmony_ci for (let i = 0; i < output.length; i++) { 15071cb0ef41Sopenharmony_ci if (typeof value[i] !== 'number' && typeof value[i] !== 'bigint') { 15081cb0ef41Sopenharmony_ci order = StringPrototypePadEnd; 15091cb0ef41Sopenharmony_ci break; 15101cb0ef41Sopenharmony_ci } 15111cb0ef41Sopenharmony_ci } 15121cb0ef41Sopenharmony_ci } 15131cb0ef41Sopenharmony_ci // Each iteration creates a single line of grouped entries. 15141cb0ef41Sopenharmony_ci for (let i = 0; i < outputLength; i += columns) { 15151cb0ef41Sopenharmony_ci // The last lines may contain less entries than columns. 15161cb0ef41Sopenharmony_ci const max = MathMin(i + columns, outputLength); 15171cb0ef41Sopenharmony_ci let str = ''; 15181cb0ef41Sopenharmony_ci let j = i; 15191cb0ef41Sopenharmony_ci for (; j < max - 1; j++) { 15201cb0ef41Sopenharmony_ci // Calculate extra color padding in case it's active. This has to be 15211cb0ef41Sopenharmony_ci // done line by line as some lines might contain more colors than 15221cb0ef41Sopenharmony_ci // others. 15231cb0ef41Sopenharmony_ci const padding = maxLineLength[j - i] + output[j].length - dataLen[j]; 15241cb0ef41Sopenharmony_ci str += order(`${output[j]}, `, padding, ' '); 15251cb0ef41Sopenharmony_ci } 15261cb0ef41Sopenharmony_ci if (order === StringPrototypePadStart) { 15271cb0ef41Sopenharmony_ci const padding = maxLineLength[j - i] + 15281cb0ef41Sopenharmony_ci output[j].length - 15291cb0ef41Sopenharmony_ci dataLen[j] - 15301cb0ef41Sopenharmony_ci separatorSpace; 15311cb0ef41Sopenharmony_ci str += StringPrototypePadStart(output[j], padding, ' '); 15321cb0ef41Sopenharmony_ci } else { 15331cb0ef41Sopenharmony_ci str += output[j]; 15341cb0ef41Sopenharmony_ci } 15351cb0ef41Sopenharmony_ci ArrayPrototypePush(tmp, str); 15361cb0ef41Sopenharmony_ci } 15371cb0ef41Sopenharmony_ci if (ctx.maxArrayLength < output.length) { 15381cb0ef41Sopenharmony_ci ArrayPrototypePush(tmp, output[outputLength]); 15391cb0ef41Sopenharmony_ci } 15401cb0ef41Sopenharmony_ci output = tmp; 15411cb0ef41Sopenharmony_ci } 15421cb0ef41Sopenharmony_ci return output; 15431cb0ef41Sopenharmony_ci} 15441cb0ef41Sopenharmony_ci 15451cb0ef41Sopenharmony_cifunction handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) { 15461cb0ef41Sopenharmony_ci if (isStackOverflowError(err)) { 15471cb0ef41Sopenharmony_ci ArrayPrototypePop(ctx.seen); 15481cb0ef41Sopenharmony_ci ctx.indentationLvl = indentationLvl; 15491cb0ef41Sopenharmony_ci return ctx.stylize( 15501cb0ef41Sopenharmony_ci `[${constructorName}: Inspection interrupted ` + 15511cb0ef41Sopenharmony_ci 'prematurely. Maximum call stack size exceeded.]', 15521cb0ef41Sopenharmony_ci 'special', 15531cb0ef41Sopenharmony_ci ); 15541cb0ef41Sopenharmony_ci } 15551cb0ef41Sopenharmony_ci /* c8 ignore next */ 15561cb0ef41Sopenharmony_ci assert.fail(err.stack); 15571cb0ef41Sopenharmony_ci} 15581cb0ef41Sopenharmony_ci 15591cb0ef41Sopenharmony_cifunction addNumericSeparator(integerString) { 15601cb0ef41Sopenharmony_ci let result = ''; 15611cb0ef41Sopenharmony_ci let i = integerString.length; 15621cb0ef41Sopenharmony_ci const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0; 15631cb0ef41Sopenharmony_ci for (; i >= start + 4; i -= 3) { 15641cb0ef41Sopenharmony_ci result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`; 15651cb0ef41Sopenharmony_ci } 15661cb0ef41Sopenharmony_ci return i === integerString.length ? 15671cb0ef41Sopenharmony_ci integerString : 15681cb0ef41Sopenharmony_ci `${StringPrototypeSlice(integerString, 0, i)}${result}`; 15691cb0ef41Sopenharmony_ci} 15701cb0ef41Sopenharmony_ci 15711cb0ef41Sopenharmony_cifunction addNumericSeparatorEnd(integerString) { 15721cb0ef41Sopenharmony_ci let result = ''; 15731cb0ef41Sopenharmony_ci let i = 0; 15741cb0ef41Sopenharmony_ci for (; i < integerString.length - 3; i += 3) { 15751cb0ef41Sopenharmony_ci result += `${StringPrototypeSlice(integerString, i, i + 3)}_`; 15761cb0ef41Sopenharmony_ci } 15771cb0ef41Sopenharmony_ci return i === 0 ? 15781cb0ef41Sopenharmony_ci integerString : 15791cb0ef41Sopenharmony_ci `${result}${StringPrototypeSlice(integerString, i)}`; 15801cb0ef41Sopenharmony_ci} 15811cb0ef41Sopenharmony_ci 15821cb0ef41Sopenharmony_cifunction formatNumber(fn, number, numericSeparator) { 15831cb0ef41Sopenharmony_ci if (!numericSeparator) { 15841cb0ef41Sopenharmony_ci // Format -0 as '-0'. Checking `number === -0` won't distinguish 0 from -0. 15851cb0ef41Sopenharmony_ci if (ObjectIs(number, -0)) { 15861cb0ef41Sopenharmony_ci return fn('-0', 'number'); 15871cb0ef41Sopenharmony_ci } 15881cb0ef41Sopenharmony_ci return fn(`${number}`, 'number'); 15891cb0ef41Sopenharmony_ci } 15901cb0ef41Sopenharmony_ci const integer = MathTrunc(number); 15911cb0ef41Sopenharmony_ci const string = String(integer); 15921cb0ef41Sopenharmony_ci if (integer === number) { 15931cb0ef41Sopenharmony_ci if (!NumberIsFinite(number) || StringPrototypeIncludes(string, 'e')) { 15941cb0ef41Sopenharmony_ci return fn(string, 'number'); 15951cb0ef41Sopenharmony_ci } 15961cb0ef41Sopenharmony_ci return fn(`${addNumericSeparator(string)}`, 'number'); 15971cb0ef41Sopenharmony_ci } 15981cb0ef41Sopenharmony_ci if (NumberIsNaN(number)) { 15991cb0ef41Sopenharmony_ci return fn(string, 'number'); 16001cb0ef41Sopenharmony_ci } 16011cb0ef41Sopenharmony_ci return fn(`${ 16021cb0ef41Sopenharmony_ci addNumericSeparator(string) 16031cb0ef41Sopenharmony_ci }.${ 16041cb0ef41Sopenharmony_ci addNumericSeparatorEnd( 16051cb0ef41Sopenharmony_ci StringPrototypeSlice(String(number), string.length + 1), 16061cb0ef41Sopenharmony_ci ) 16071cb0ef41Sopenharmony_ci }`, 'number'); 16081cb0ef41Sopenharmony_ci} 16091cb0ef41Sopenharmony_ci 16101cb0ef41Sopenharmony_cifunction formatBigInt(fn, bigint, numericSeparator) { 16111cb0ef41Sopenharmony_ci const string = String(bigint); 16121cb0ef41Sopenharmony_ci if (!numericSeparator) { 16131cb0ef41Sopenharmony_ci return fn(`${string}n`, 'bigint'); 16141cb0ef41Sopenharmony_ci } 16151cb0ef41Sopenharmony_ci return fn(`${addNumericSeparator(string)}n`, 'bigint'); 16161cb0ef41Sopenharmony_ci} 16171cb0ef41Sopenharmony_ci 16181cb0ef41Sopenharmony_cifunction formatPrimitive(fn, value, ctx) { 16191cb0ef41Sopenharmony_ci if (typeof value === 'string') { 16201cb0ef41Sopenharmony_ci let trailer = ''; 16211cb0ef41Sopenharmony_ci if (value.length > ctx.maxStringLength) { 16221cb0ef41Sopenharmony_ci const remaining = value.length - ctx.maxStringLength; 16231cb0ef41Sopenharmony_ci value = StringPrototypeSlice(value, 0, ctx.maxStringLength); 16241cb0ef41Sopenharmony_ci trailer = `... ${remaining} more character${remaining > 1 ? 's' : ''}`; 16251cb0ef41Sopenharmony_ci } 16261cb0ef41Sopenharmony_ci if (ctx.compact !== true && 16271cb0ef41Sopenharmony_ci // We do not support handling unicode characters width with 16281cb0ef41Sopenharmony_ci // the readline getStringWidth function as there are 16291cb0ef41Sopenharmony_ci // performance implications. 16301cb0ef41Sopenharmony_ci value.length > kMinLineLength && 16311cb0ef41Sopenharmony_ci value.length > ctx.breakLength - ctx.indentationLvl - 4) { 16321cb0ef41Sopenharmony_ci return ArrayPrototypeJoin( 16331cb0ef41Sopenharmony_ci ArrayPrototypeMap( 16341cb0ef41Sopenharmony_ci RegExpPrototypeSymbolSplit(/(?<=\n)/, value), 16351cb0ef41Sopenharmony_ci (line) => fn(strEscape(line), 'string'), 16361cb0ef41Sopenharmony_ci ), 16371cb0ef41Sopenharmony_ci ` +\n${StringPrototypeRepeat(' ', ctx.indentationLvl + 2)}`, 16381cb0ef41Sopenharmony_ci ) + trailer; 16391cb0ef41Sopenharmony_ci } 16401cb0ef41Sopenharmony_ci return fn(strEscape(value), 'string') + trailer; 16411cb0ef41Sopenharmony_ci } 16421cb0ef41Sopenharmony_ci if (typeof value === 'number') 16431cb0ef41Sopenharmony_ci return formatNumber(fn, value, ctx.numericSeparator); 16441cb0ef41Sopenharmony_ci if (typeof value === 'bigint') 16451cb0ef41Sopenharmony_ci return formatBigInt(fn, value, ctx.numericSeparator); 16461cb0ef41Sopenharmony_ci if (typeof value === 'boolean') 16471cb0ef41Sopenharmony_ci return fn(`${value}`, 'boolean'); 16481cb0ef41Sopenharmony_ci if (typeof value === 'undefined') 16491cb0ef41Sopenharmony_ci return fn('undefined', 'undefined'); 16501cb0ef41Sopenharmony_ci // es6 symbol primitive 16511cb0ef41Sopenharmony_ci return fn(SymbolPrototypeToString(value), 'symbol'); 16521cb0ef41Sopenharmony_ci} 16531cb0ef41Sopenharmony_ci 16541cb0ef41Sopenharmony_cifunction formatNamespaceObject(keys, ctx, value, recurseTimes) { 16551cb0ef41Sopenharmony_ci const output = new Array(keys.length); 16561cb0ef41Sopenharmony_ci for (let i = 0; i < keys.length; i++) { 16571cb0ef41Sopenharmony_ci try { 16581cb0ef41Sopenharmony_ci output[i] = formatProperty(ctx, value, recurseTimes, keys[i], 16591cb0ef41Sopenharmony_ci kObjectType); 16601cb0ef41Sopenharmony_ci } catch (err) { 16611cb0ef41Sopenharmony_ci assert(isNativeError(err) && err.name === 'ReferenceError'); 16621cb0ef41Sopenharmony_ci // Use the existing functionality. This makes sure the indentation and 16631cb0ef41Sopenharmony_ci // line breaks are always correct. Otherwise it is very difficult to keep 16641cb0ef41Sopenharmony_ci // this aligned, even though this is a hacky way of dealing with this. 16651cb0ef41Sopenharmony_ci const tmp = { [keys[i]]: '' }; 16661cb0ef41Sopenharmony_ci output[i] = formatProperty(ctx, tmp, recurseTimes, keys[i], kObjectType); 16671cb0ef41Sopenharmony_ci const pos = StringPrototypeLastIndexOf(output[i], ' '); 16681cb0ef41Sopenharmony_ci // We have to find the last whitespace and have to replace that value as 16691cb0ef41Sopenharmony_ci // it will be visualized as a regular string. 16701cb0ef41Sopenharmony_ci output[i] = StringPrototypeSlice(output[i], 0, pos + 1) + 16711cb0ef41Sopenharmony_ci ctx.stylize('<uninitialized>', 'special'); 16721cb0ef41Sopenharmony_ci } 16731cb0ef41Sopenharmony_ci } 16741cb0ef41Sopenharmony_ci // Reset the keys to an empty array. This prevents duplicated inspection. 16751cb0ef41Sopenharmony_ci keys.length = 0; 16761cb0ef41Sopenharmony_ci return output; 16771cb0ef41Sopenharmony_ci} 16781cb0ef41Sopenharmony_ci 16791cb0ef41Sopenharmony_ci// The array is sparse and/or has extra keys 16801cb0ef41Sopenharmony_cifunction formatSpecialArray(ctx, value, recurseTimes, maxLength, output, i) { 16811cb0ef41Sopenharmony_ci const keys = ObjectKeys(value); 16821cb0ef41Sopenharmony_ci let index = i; 16831cb0ef41Sopenharmony_ci for (; i < keys.length && output.length < maxLength; i++) { 16841cb0ef41Sopenharmony_ci const key = keys[i]; 16851cb0ef41Sopenharmony_ci const tmp = +key; 16861cb0ef41Sopenharmony_ci // Arrays can only have up to 2^32 - 1 entries 16871cb0ef41Sopenharmony_ci if (tmp > 2 ** 32 - 2) { 16881cb0ef41Sopenharmony_ci break; 16891cb0ef41Sopenharmony_ci } 16901cb0ef41Sopenharmony_ci if (`${index}` !== key) { 16911cb0ef41Sopenharmony_ci if (RegExpPrototypeExec(numberRegExp, key) === null) { 16921cb0ef41Sopenharmony_ci break; 16931cb0ef41Sopenharmony_ci } 16941cb0ef41Sopenharmony_ci const emptyItems = tmp - index; 16951cb0ef41Sopenharmony_ci const ending = emptyItems > 1 ? 's' : ''; 16961cb0ef41Sopenharmony_ci const message = `<${emptyItems} empty item${ending}>`; 16971cb0ef41Sopenharmony_ci ArrayPrototypePush(output, ctx.stylize(message, 'undefined')); 16981cb0ef41Sopenharmony_ci index = tmp; 16991cb0ef41Sopenharmony_ci if (output.length === maxLength) { 17001cb0ef41Sopenharmony_ci break; 17011cb0ef41Sopenharmony_ci } 17021cb0ef41Sopenharmony_ci } 17031cb0ef41Sopenharmony_ci ArrayPrototypePush(output, formatProperty(ctx, value, recurseTimes, key, kArrayType)); 17041cb0ef41Sopenharmony_ci index++; 17051cb0ef41Sopenharmony_ci } 17061cb0ef41Sopenharmony_ci const remaining = value.length - index; 17071cb0ef41Sopenharmony_ci if (output.length !== maxLength) { 17081cb0ef41Sopenharmony_ci if (remaining > 0) { 17091cb0ef41Sopenharmony_ci const ending = remaining > 1 ? 's' : ''; 17101cb0ef41Sopenharmony_ci const message = `<${remaining} empty item${ending}>`; 17111cb0ef41Sopenharmony_ci ArrayPrototypePush(output, ctx.stylize(message, 'undefined')); 17121cb0ef41Sopenharmony_ci } 17131cb0ef41Sopenharmony_ci } else if (remaining > 0) { 17141cb0ef41Sopenharmony_ci ArrayPrototypePush(output, `... ${remaining} more item${remaining > 1 ? 's' : ''}`); 17151cb0ef41Sopenharmony_ci } 17161cb0ef41Sopenharmony_ci return output; 17171cb0ef41Sopenharmony_ci} 17181cb0ef41Sopenharmony_ci 17191cb0ef41Sopenharmony_cifunction formatArrayBuffer(ctx, value) { 17201cb0ef41Sopenharmony_ci let buffer; 17211cb0ef41Sopenharmony_ci try { 17221cb0ef41Sopenharmony_ci buffer = new Uint8Array(value); 17231cb0ef41Sopenharmony_ci } catch { 17241cb0ef41Sopenharmony_ci return [ctx.stylize('(detached)', 'special')]; 17251cb0ef41Sopenharmony_ci } 17261cb0ef41Sopenharmony_ci if (hexSlice === undefined) 17271cb0ef41Sopenharmony_ci hexSlice = uncurryThis(require('buffer').Buffer.prototype.hexSlice); 17281cb0ef41Sopenharmony_ci let str = StringPrototypeTrim(RegExpPrototypeSymbolReplace( 17291cb0ef41Sopenharmony_ci /(.{2})/g, 17301cb0ef41Sopenharmony_ci hexSlice(buffer, 0, MathMin(ctx.maxArrayLength, buffer.length)), 17311cb0ef41Sopenharmony_ci '$1 ', 17321cb0ef41Sopenharmony_ci )); 17331cb0ef41Sopenharmony_ci const remaining = buffer.length - ctx.maxArrayLength; 17341cb0ef41Sopenharmony_ci if (remaining > 0) 17351cb0ef41Sopenharmony_ci str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; 17361cb0ef41Sopenharmony_ci return [`${ctx.stylize('[Uint8Contents]', 'special')}: <${str}>`]; 17371cb0ef41Sopenharmony_ci} 17381cb0ef41Sopenharmony_ci 17391cb0ef41Sopenharmony_cifunction formatArray(ctx, value, recurseTimes) { 17401cb0ef41Sopenharmony_ci const valLen = value.length; 17411cb0ef41Sopenharmony_ci const len = MathMin(MathMax(0, ctx.maxArrayLength), valLen); 17421cb0ef41Sopenharmony_ci 17431cb0ef41Sopenharmony_ci const remaining = valLen - len; 17441cb0ef41Sopenharmony_ci const output = []; 17451cb0ef41Sopenharmony_ci for (let i = 0; i < len; i++) { 17461cb0ef41Sopenharmony_ci // Special handle sparse arrays. 17471cb0ef41Sopenharmony_ci if (!ObjectPrototypeHasOwnProperty(value, i)) { 17481cb0ef41Sopenharmony_ci return formatSpecialArray(ctx, value, recurseTimes, len, output, i); 17491cb0ef41Sopenharmony_ci } 17501cb0ef41Sopenharmony_ci ArrayPrototypePush(output, formatProperty(ctx, value, recurseTimes, i, kArrayType)); 17511cb0ef41Sopenharmony_ci } 17521cb0ef41Sopenharmony_ci if (remaining > 0) { 17531cb0ef41Sopenharmony_ci ArrayPrototypePush(output, `... ${remaining} more item${remaining > 1 ? 's' : ''}`); 17541cb0ef41Sopenharmony_ci } 17551cb0ef41Sopenharmony_ci return output; 17561cb0ef41Sopenharmony_ci} 17571cb0ef41Sopenharmony_ci 17581cb0ef41Sopenharmony_cifunction formatTypedArray(value, length, ctx, ignored, recurseTimes) { 17591cb0ef41Sopenharmony_ci const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length); 17601cb0ef41Sopenharmony_ci const remaining = value.length - maxLength; 17611cb0ef41Sopenharmony_ci const output = new Array(maxLength); 17621cb0ef41Sopenharmony_ci const elementFormatter = value.length > 0 && typeof value[0] === 'number' ? 17631cb0ef41Sopenharmony_ci formatNumber : 17641cb0ef41Sopenharmony_ci formatBigInt; 17651cb0ef41Sopenharmony_ci for (let i = 0; i < maxLength; ++i) { 17661cb0ef41Sopenharmony_ci output[i] = elementFormatter(ctx.stylize, value[i], ctx.numericSeparator); 17671cb0ef41Sopenharmony_ci } 17681cb0ef41Sopenharmony_ci if (remaining > 0) { 17691cb0ef41Sopenharmony_ci output[maxLength] = `... ${remaining} more item${remaining > 1 ? 's' : ''}`; 17701cb0ef41Sopenharmony_ci } 17711cb0ef41Sopenharmony_ci if (ctx.showHidden) { 17721cb0ef41Sopenharmony_ci // .buffer goes last, it's not a primitive like the others. 17731cb0ef41Sopenharmony_ci // All besides `BYTES_PER_ELEMENT` are actually getters. 17741cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 17751cb0ef41Sopenharmony_ci for (const key of [ 17761cb0ef41Sopenharmony_ci 'BYTES_PER_ELEMENT', 17771cb0ef41Sopenharmony_ci 'length', 17781cb0ef41Sopenharmony_ci 'byteLength', 17791cb0ef41Sopenharmony_ci 'byteOffset', 17801cb0ef41Sopenharmony_ci 'buffer', 17811cb0ef41Sopenharmony_ci ]) { 17821cb0ef41Sopenharmony_ci const str = formatValue(ctx, value[key], recurseTimes, true); 17831cb0ef41Sopenharmony_ci ArrayPrototypePush(output, `[${key}]: ${str}`); 17841cb0ef41Sopenharmony_ci } 17851cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 17861cb0ef41Sopenharmony_ci } 17871cb0ef41Sopenharmony_ci return output; 17881cb0ef41Sopenharmony_ci} 17891cb0ef41Sopenharmony_ci 17901cb0ef41Sopenharmony_cifunction formatSet(value, ctx, ignored, recurseTimes) { 17911cb0ef41Sopenharmony_ci const output = []; 17921cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 17931cb0ef41Sopenharmony_ci for (const v of value) { 17941cb0ef41Sopenharmony_ci ArrayPrototypePush(output, formatValue(ctx, v, recurseTimes)); 17951cb0ef41Sopenharmony_ci } 17961cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 17971cb0ef41Sopenharmony_ci return output; 17981cb0ef41Sopenharmony_ci} 17991cb0ef41Sopenharmony_ci 18001cb0ef41Sopenharmony_cifunction formatMap(value, ctx, ignored, recurseTimes) { 18011cb0ef41Sopenharmony_ci const output = []; 18021cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 18031cb0ef41Sopenharmony_ci for (const { 0: k, 1: v } of value) { 18041cb0ef41Sopenharmony_ci ArrayPrototypePush( 18051cb0ef41Sopenharmony_ci output, 18061cb0ef41Sopenharmony_ci `${formatValue(ctx, k, recurseTimes)} => ${formatValue(ctx, v, recurseTimes)}`, 18071cb0ef41Sopenharmony_ci ); 18081cb0ef41Sopenharmony_ci } 18091cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 18101cb0ef41Sopenharmony_ci return output; 18111cb0ef41Sopenharmony_ci} 18121cb0ef41Sopenharmony_ci 18131cb0ef41Sopenharmony_cifunction formatSetIterInner(ctx, recurseTimes, entries, state) { 18141cb0ef41Sopenharmony_ci const maxArrayLength = MathMax(ctx.maxArrayLength, 0); 18151cb0ef41Sopenharmony_ci const maxLength = MathMin(maxArrayLength, entries.length); 18161cb0ef41Sopenharmony_ci const output = new Array(maxLength); 18171cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 18181cb0ef41Sopenharmony_ci for (let i = 0; i < maxLength; i++) { 18191cb0ef41Sopenharmony_ci output[i] = formatValue(ctx, entries[i], recurseTimes); 18201cb0ef41Sopenharmony_ci } 18211cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 18221cb0ef41Sopenharmony_ci if (state === kWeak && !ctx.sorted) { 18231cb0ef41Sopenharmony_ci // Sort all entries to have a halfway reliable output (if more entries than 18241cb0ef41Sopenharmony_ci // retrieved ones exist, we can not reliably return the same output) if the 18251cb0ef41Sopenharmony_ci // output is not sorted anyway. 18261cb0ef41Sopenharmony_ci ArrayPrototypeSort(output); 18271cb0ef41Sopenharmony_ci } 18281cb0ef41Sopenharmony_ci const remaining = entries.length - maxLength; 18291cb0ef41Sopenharmony_ci if (remaining > 0) { 18301cb0ef41Sopenharmony_ci ArrayPrototypePush(output, 18311cb0ef41Sopenharmony_ci `... ${remaining} more item${remaining > 1 ? 's' : ''}`); 18321cb0ef41Sopenharmony_ci } 18331cb0ef41Sopenharmony_ci return output; 18341cb0ef41Sopenharmony_ci} 18351cb0ef41Sopenharmony_ci 18361cb0ef41Sopenharmony_cifunction formatMapIterInner(ctx, recurseTimes, entries, state) { 18371cb0ef41Sopenharmony_ci const maxArrayLength = MathMax(ctx.maxArrayLength, 0); 18381cb0ef41Sopenharmony_ci // Entries exist as [key1, val1, key2, val2, ...] 18391cb0ef41Sopenharmony_ci const len = entries.length / 2; 18401cb0ef41Sopenharmony_ci const remaining = len - maxArrayLength; 18411cb0ef41Sopenharmony_ci const maxLength = MathMin(maxArrayLength, len); 18421cb0ef41Sopenharmony_ci const output = new Array(maxLength); 18431cb0ef41Sopenharmony_ci let i = 0; 18441cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 18451cb0ef41Sopenharmony_ci if (state === kWeak) { 18461cb0ef41Sopenharmony_ci for (; i < maxLength; i++) { 18471cb0ef41Sopenharmony_ci const pos = i * 2; 18481cb0ef41Sopenharmony_ci output[i] = 18491cb0ef41Sopenharmony_ci `${formatValue(ctx, entries[pos], recurseTimes)} => ${formatValue(ctx, entries[pos + 1], recurseTimes)}`; 18501cb0ef41Sopenharmony_ci } 18511cb0ef41Sopenharmony_ci // Sort all entries to have a halfway reliable output (if more entries than 18521cb0ef41Sopenharmony_ci // retrieved ones exist, we can not reliably return the same output) if the 18531cb0ef41Sopenharmony_ci // output is not sorted anyway. 18541cb0ef41Sopenharmony_ci if (!ctx.sorted) 18551cb0ef41Sopenharmony_ci ArrayPrototypeSort(output); 18561cb0ef41Sopenharmony_ci } else { 18571cb0ef41Sopenharmony_ci for (; i < maxLength; i++) { 18581cb0ef41Sopenharmony_ci const pos = i * 2; 18591cb0ef41Sopenharmony_ci const res = [ 18601cb0ef41Sopenharmony_ci formatValue(ctx, entries[pos], recurseTimes), 18611cb0ef41Sopenharmony_ci formatValue(ctx, entries[pos + 1], recurseTimes), 18621cb0ef41Sopenharmony_ci ]; 18631cb0ef41Sopenharmony_ci output[i] = reduceToSingleString( 18641cb0ef41Sopenharmony_ci ctx, res, '', ['[', ']'], kArrayExtrasType, recurseTimes); 18651cb0ef41Sopenharmony_ci } 18661cb0ef41Sopenharmony_ci } 18671cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 18681cb0ef41Sopenharmony_ci if (remaining > 0) { 18691cb0ef41Sopenharmony_ci ArrayPrototypePush(output, `... ${remaining} more item${remaining > 1 ? 's' : ''}`); 18701cb0ef41Sopenharmony_ci } 18711cb0ef41Sopenharmony_ci return output; 18721cb0ef41Sopenharmony_ci} 18731cb0ef41Sopenharmony_ci 18741cb0ef41Sopenharmony_cifunction formatWeakCollection(ctx) { 18751cb0ef41Sopenharmony_ci return [ctx.stylize('<items unknown>', 'special')]; 18761cb0ef41Sopenharmony_ci} 18771cb0ef41Sopenharmony_ci 18781cb0ef41Sopenharmony_cifunction formatWeakSet(ctx, value, recurseTimes) { 18791cb0ef41Sopenharmony_ci const entries = previewEntries(value); 18801cb0ef41Sopenharmony_ci return formatSetIterInner(ctx, recurseTimes, entries, kWeak); 18811cb0ef41Sopenharmony_ci} 18821cb0ef41Sopenharmony_ci 18831cb0ef41Sopenharmony_cifunction formatWeakMap(ctx, value, recurseTimes) { 18841cb0ef41Sopenharmony_ci const entries = previewEntries(value); 18851cb0ef41Sopenharmony_ci return formatMapIterInner(ctx, recurseTimes, entries, kWeak); 18861cb0ef41Sopenharmony_ci} 18871cb0ef41Sopenharmony_ci 18881cb0ef41Sopenharmony_cifunction formatIterator(braces, ctx, value, recurseTimes) { 18891cb0ef41Sopenharmony_ci const { 0: entries, 1: isKeyValue } = previewEntries(value, true); 18901cb0ef41Sopenharmony_ci if (isKeyValue) { 18911cb0ef41Sopenharmony_ci // Mark entry iterators as such. 18921cb0ef41Sopenharmony_ci braces[0] = RegExpPrototypeSymbolReplace(/ Iterator] {$/, braces[0], ' Entries] {'); 18931cb0ef41Sopenharmony_ci return formatMapIterInner(ctx, recurseTimes, entries, kMapEntries); 18941cb0ef41Sopenharmony_ci } 18951cb0ef41Sopenharmony_ci 18961cb0ef41Sopenharmony_ci return formatSetIterInner(ctx, recurseTimes, entries, kIterator); 18971cb0ef41Sopenharmony_ci} 18981cb0ef41Sopenharmony_ci 18991cb0ef41Sopenharmony_cifunction formatPromise(ctx, value, recurseTimes) { 19001cb0ef41Sopenharmony_ci let output; 19011cb0ef41Sopenharmony_ci const { 0: state, 1: result } = getPromiseDetails(value); 19021cb0ef41Sopenharmony_ci if (state === kPending) { 19031cb0ef41Sopenharmony_ci output = [ctx.stylize('<pending>', 'special')]; 19041cb0ef41Sopenharmony_ci } else { 19051cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 19061cb0ef41Sopenharmony_ci const str = formatValue(ctx, result, recurseTimes); 19071cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 19081cb0ef41Sopenharmony_ci output = [ 19091cb0ef41Sopenharmony_ci state === kRejected ? 19101cb0ef41Sopenharmony_ci `${ctx.stylize('<rejected>', 'special')} ${str}` : 19111cb0ef41Sopenharmony_ci str, 19121cb0ef41Sopenharmony_ci ]; 19131cb0ef41Sopenharmony_ci } 19141cb0ef41Sopenharmony_ci return output; 19151cb0ef41Sopenharmony_ci} 19161cb0ef41Sopenharmony_ci 19171cb0ef41Sopenharmony_cifunction formatProperty(ctx, value, recurseTimes, key, type, desc, 19181cb0ef41Sopenharmony_ci original = value) { 19191cb0ef41Sopenharmony_ci let name, str; 19201cb0ef41Sopenharmony_ci let extra = ' '; 19211cb0ef41Sopenharmony_ci desc = desc || ObjectGetOwnPropertyDescriptor(value, key) || 19221cb0ef41Sopenharmony_ci { value: value[key], enumerable: true }; 19231cb0ef41Sopenharmony_ci if (desc.value !== undefined) { 19241cb0ef41Sopenharmony_ci const diff = (ctx.compact !== true || type !== kObjectType) ? 2 : 3; 19251cb0ef41Sopenharmony_ci ctx.indentationLvl += diff; 19261cb0ef41Sopenharmony_ci str = formatValue(ctx, desc.value, recurseTimes); 19271cb0ef41Sopenharmony_ci if (diff === 3 && ctx.breakLength < getStringWidth(str, ctx.colors)) { 19281cb0ef41Sopenharmony_ci extra = `\n${StringPrototypeRepeat(' ', ctx.indentationLvl)}`; 19291cb0ef41Sopenharmony_ci } 19301cb0ef41Sopenharmony_ci ctx.indentationLvl -= diff; 19311cb0ef41Sopenharmony_ci } else if (desc.get !== undefined) { 19321cb0ef41Sopenharmony_ci const label = desc.set !== undefined ? 'Getter/Setter' : 'Getter'; 19331cb0ef41Sopenharmony_ci const s = ctx.stylize; 19341cb0ef41Sopenharmony_ci const sp = 'special'; 19351cb0ef41Sopenharmony_ci if (ctx.getters && (ctx.getters === true || 19361cb0ef41Sopenharmony_ci (ctx.getters === 'get' && desc.set === undefined) || 19371cb0ef41Sopenharmony_ci (ctx.getters === 'set' && desc.set !== undefined))) { 19381cb0ef41Sopenharmony_ci try { 19391cb0ef41Sopenharmony_ci const tmp = FunctionPrototypeCall(desc.get, original); 19401cb0ef41Sopenharmony_ci ctx.indentationLvl += 2; 19411cb0ef41Sopenharmony_ci if (tmp === null) { 19421cb0ef41Sopenharmony_ci str = `${s(`[${label}:`, sp)} ${s('null', 'null')}${s(']', sp)}`; 19431cb0ef41Sopenharmony_ci } else if (typeof tmp === 'object') { 19441cb0ef41Sopenharmony_ci str = `${s(`[${label}]`, sp)} ${formatValue(ctx, tmp, recurseTimes)}`; 19451cb0ef41Sopenharmony_ci } else { 19461cb0ef41Sopenharmony_ci const primitive = formatPrimitive(s, tmp, ctx); 19471cb0ef41Sopenharmony_ci str = `${s(`[${label}:`, sp)} ${primitive}${s(']', sp)}`; 19481cb0ef41Sopenharmony_ci } 19491cb0ef41Sopenharmony_ci ctx.indentationLvl -= 2; 19501cb0ef41Sopenharmony_ci } catch (err) { 19511cb0ef41Sopenharmony_ci const message = `<Inspection threw (${err.message})>`; 19521cb0ef41Sopenharmony_ci str = `${s(`[${label}:`, sp)} ${message}${s(']', sp)}`; 19531cb0ef41Sopenharmony_ci } 19541cb0ef41Sopenharmony_ci } else { 19551cb0ef41Sopenharmony_ci str = ctx.stylize(`[${label}]`, sp); 19561cb0ef41Sopenharmony_ci } 19571cb0ef41Sopenharmony_ci } else if (desc.set !== undefined) { 19581cb0ef41Sopenharmony_ci str = ctx.stylize('[Setter]', 'special'); 19591cb0ef41Sopenharmony_ci } else { 19601cb0ef41Sopenharmony_ci str = ctx.stylize('undefined', 'undefined'); 19611cb0ef41Sopenharmony_ci } 19621cb0ef41Sopenharmony_ci if (type === kArrayType) { 19631cb0ef41Sopenharmony_ci return str; 19641cb0ef41Sopenharmony_ci } 19651cb0ef41Sopenharmony_ci if (typeof key === 'symbol') { 19661cb0ef41Sopenharmony_ci const tmp = RegExpPrototypeSymbolReplace( 19671cb0ef41Sopenharmony_ci strEscapeSequencesReplacer, 19681cb0ef41Sopenharmony_ci SymbolPrototypeToString(key), 19691cb0ef41Sopenharmony_ci escapeFn, 19701cb0ef41Sopenharmony_ci ); 19711cb0ef41Sopenharmony_ci name = `[${ctx.stylize(tmp, 'symbol')}]`; 19721cb0ef41Sopenharmony_ci } else if (key === '__proto__') { 19731cb0ef41Sopenharmony_ci name = "['__proto__']"; 19741cb0ef41Sopenharmony_ci } else if (desc.enumerable === false) { 19751cb0ef41Sopenharmony_ci const tmp = RegExpPrototypeSymbolReplace( 19761cb0ef41Sopenharmony_ci strEscapeSequencesReplacer, 19771cb0ef41Sopenharmony_ci key, 19781cb0ef41Sopenharmony_ci escapeFn, 19791cb0ef41Sopenharmony_ci ); 19801cb0ef41Sopenharmony_ci name = `[${tmp}]`; 19811cb0ef41Sopenharmony_ci } else if (RegExpPrototypeExec(keyStrRegExp, key) !== null) { 19821cb0ef41Sopenharmony_ci name = ctx.stylize(key, 'name'); 19831cb0ef41Sopenharmony_ci } else { 19841cb0ef41Sopenharmony_ci name = ctx.stylize(strEscape(key), 'string'); 19851cb0ef41Sopenharmony_ci } 19861cb0ef41Sopenharmony_ci return `${name}:${extra}${str}`; 19871cb0ef41Sopenharmony_ci} 19881cb0ef41Sopenharmony_ci 19891cb0ef41Sopenharmony_cifunction isBelowBreakLength(ctx, output, start, base) { 19901cb0ef41Sopenharmony_ci // Each entry is separated by at least a comma. Thus, we start with a total 19911cb0ef41Sopenharmony_ci // length of at least `output.length`. In addition, some cases have a 19921cb0ef41Sopenharmony_ci // whitespace in-between each other that is added to the total as well. 19931cb0ef41Sopenharmony_ci // TODO(BridgeAR): Add unicode support. Use the readline getStringWidth 19941cb0ef41Sopenharmony_ci // function. Check the performance overhead and make it an opt-in in case it's 19951cb0ef41Sopenharmony_ci // significant. 19961cb0ef41Sopenharmony_ci let totalLength = output.length + start; 19971cb0ef41Sopenharmony_ci if (totalLength + output.length > ctx.breakLength) 19981cb0ef41Sopenharmony_ci return false; 19991cb0ef41Sopenharmony_ci for (let i = 0; i < output.length; i++) { 20001cb0ef41Sopenharmony_ci if (ctx.colors) { 20011cb0ef41Sopenharmony_ci totalLength += removeColors(output[i]).length; 20021cb0ef41Sopenharmony_ci } else { 20031cb0ef41Sopenharmony_ci totalLength += output[i].length; 20041cb0ef41Sopenharmony_ci } 20051cb0ef41Sopenharmony_ci if (totalLength > ctx.breakLength) { 20061cb0ef41Sopenharmony_ci return false; 20071cb0ef41Sopenharmony_ci } 20081cb0ef41Sopenharmony_ci } 20091cb0ef41Sopenharmony_ci // Do not line up properties on the same line if `base` contains line breaks. 20101cb0ef41Sopenharmony_ci return base === '' || !StringPrototypeIncludes(base, '\n'); 20111cb0ef41Sopenharmony_ci} 20121cb0ef41Sopenharmony_ci 20131cb0ef41Sopenharmony_cifunction reduceToSingleString( 20141cb0ef41Sopenharmony_ci ctx, output, base, braces, extrasType, recurseTimes, value) { 20151cb0ef41Sopenharmony_ci if (ctx.compact !== true) { 20161cb0ef41Sopenharmony_ci if (typeof ctx.compact === 'number' && ctx.compact >= 1) { 20171cb0ef41Sopenharmony_ci // Memorize the original output length. In case the output is grouped, 20181cb0ef41Sopenharmony_ci // prevent lining up the entries on a single line. 20191cb0ef41Sopenharmony_ci const entries = output.length; 20201cb0ef41Sopenharmony_ci // Group array elements together if the array contains at least six 20211cb0ef41Sopenharmony_ci // separate entries. 20221cb0ef41Sopenharmony_ci if (extrasType === kArrayExtrasType && entries > 6) { 20231cb0ef41Sopenharmony_ci output = groupArrayElements(ctx, output, value); 20241cb0ef41Sopenharmony_ci } 20251cb0ef41Sopenharmony_ci // `ctx.currentDepth` is set to the most inner depth of the currently 20261cb0ef41Sopenharmony_ci // inspected object part while `recurseTimes` is the actual current depth 20271cb0ef41Sopenharmony_ci // that is inspected. 20281cb0ef41Sopenharmony_ci // 20291cb0ef41Sopenharmony_ci // Example: 20301cb0ef41Sopenharmony_ci // 20311cb0ef41Sopenharmony_ci // const a = { first: [ 1, 2, 3 ], second: { inner: [ 1, 2, 3 ] } } 20321cb0ef41Sopenharmony_ci // 20331cb0ef41Sopenharmony_ci // The deepest depth of `a` is 2 (a.second.inner) and `a.first` has a max 20341cb0ef41Sopenharmony_ci // depth of 1. 20351cb0ef41Sopenharmony_ci // 20361cb0ef41Sopenharmony_ci // Consolidate all entries of the local most inner depth up to 20371cb0ef41Sopenharmony_ci // `ctx.compact`, as long as the properties are smaller than 20381cb0ef41Sopenharmony_ci // `ctx.breakLength`. 20391cb0ef41Sopenharmony_ci if (ctx.currentDepth - recurseTimes < ctx.compact && 20401cb0ef41Sopenharmony_ci entries === output.length) { 20411cb0ef41Sopenharmony_ci // Line up all entries on a single line in case the entries do not 20421cb0ef41Sopenharmony_ci // exceed `breakLength`. Add 10 as constant to start next to all other 20431cb0ef41Sopenharmony_ci // factors that may reduce `breakLength`. 20441cb0ef41Sopenharmony_ci const start = output.length + ctx.indentationLvl + 20451cb0ef41Sopenharmony_ci braces[0].length + base.length + 10; 20461cb0ef41Sopenharmony_ci if (isBelowBreakLength(ctx, output, start, base)) { 20471cb0ef41Sopenharmony_ci const joinedOutput = join(output, ', '); 20481cb0ef41Sopenharmony_ci if (!StringPrototypeIncludes(joinedOutput, '\n')) { 20491cb0ef41Sopenharmony_ci return `${base ? `${base} ` : ''}${braces[0]} ${joinedOutput}` + 20501cb0ef41Sopenharmony_ci ` ${braces[1]}`; 20511cb0ef41Sopenharmony_ci } 20521cb0ef41Sopenharmony_ci } 20531cb0ef41Sopenharmony_ci } 20541cb0ef41Sopenharmony_ci } 20551cb0ef41Sopenharmony_ci // Line up each entry on an individual line. 20561cb0ef41Sopenharmony_ci const indentation = `\n${StringPrototypeRepeat(' ', ctx.indentationLvl)}`; 20571cb0ef41Sopenharmony_ci return `${base ? `${base} ` : ''}${braces[0]}${indentation} ` + 20581cb0ef41Sopenharmony_ci `${join(output, `,${indentation} `)}${indentation}${braces[1]}`; 20591cb0ef41Sopenharmony_ci } 20601cb0ef41Sopenharmony_ci // Line up all entries on a single line in case the entries do not exceed 20611cb0ef41Sopenharmony_ci // `breakLength`. 20621cb0ef41Sopenharmony_ci if (isBelowBreakLength(ctx, output, 0, base)) { 20631cb0ef41Sopenharmony_ci return `${braces[0]}${base ? ` ${base}` : ''} ${join(output, ', ')} ` + 20641cb0ef41Sopenharmony_ci braces[1]; 20651cb0ef41Sopenharmony_ci } 20661cb0ef41Sopenharmony_ci const indentation = StringPrototypeRepeat(' ', ctx.indentationLvl); 20671cb0ef41Sopenharmony_ci // If the opening "brace" is too large, like in the case of "Set {", 20681cb0ef41Sopenharmony_ci // we need to force the first item to be on the next line or the 20691cb0ef41Sopenharmony_ci // items will not line up correctly. 20701cb0ef41Sopenharmony_ci const ln = base === '' && braces[0].length === 1 ? 20711cb0ef41Sopenharmony_ci ' ' : `${base ? ` ${base}` : ''}\n${indentation} `; 20721cb0ef41Sopenharmony_ci // Line up each entry on an individual line. 20731cb0ef41Sopenharmony_ci return `${braces[0]}${ln}${join(output, `,\n${indentation} `)} ${braces[1]}`; 20741cb0ef41Sopenharmony_ci} 20751cb0ef41Sopenharmony_ci 20761cb0ef41Sopenharmony_cifunction hasBuiltInToString(value) { 20771cb0ef41Sopenharmony_ci // Prevent triggering proxy traps. 20781cb0ef41Sopenharmony_ci const getFullProxy = false; 20791cb0ef41Sopenharmony_ci const proxyTarget = getProxyDetails(value, getFullProxy); 20801cb0ef41Sopenharmony_ci if (proxyTarget !== undefined) { 20811cb0ef41Sopenharmony_ci if (proxyTarget === null) { 20821cb0ef41Sopenharmony_ci return true; 20831cb0ef41Sopenharmony_ci } 20841cb0ef41Sopenharmony_ci value = proxyTarget; 20851cb0ef41Sopenharmony_ci } 20861cb0ef41Sopenharmony_ci 20871cb0ef41Sopenharmony_ci // Count objects that have no `toString` function as built-in. 20881cb0ef41Sopenharmony_ci if (typeof value.toString !== 'function') { 20891cb0ef41Sopenharmony_ci return true; 20901cb0ef41Sopenharmony_ci } 20911cb0ef41Sopenharmony_ci 20921cb0ef41Sopenharmony_ci // The object has a own `toString` property. Thus it's not not a built-in one. 20931cb0ef41Sopenharmony_ci if (ObjectPrototypeHasOwnProperty(value, 'toString')) { 20941cb0ef41Sopenharmony_ci return false; 20951cb0ef41Sopenharmony_ci } 20961cb0ef41Sopenharmony_ci 20971cb0ef41Sopenharmony_ci // Find the object that has the `toString` property as own property in the 20981cb0ef41Sopenharmony_ci // prototype chain. 20991cb0ef41Sopenharmony_ci let pointer = value; 21001cb0ef41Sopenharmony_ci do { 21011cb0ef41Sopenharmony_ci pointer = ObjectGetPrototypeOf(pointer); 21021cb0ef41Sopenharmony_ci } while (!ObjectPrototypeHasOwnProperty(pointer, 'toString')); 21031cb0ef41Sopenharmony_ci 21041cb0ef41Sopenharmony_ci // Check closer if the object is a built-in. 21051cb0ef41Sopenharmony_ci const descriptor = ObjectGetOwnPropertyDescriptor(pointer, 'constructor'); 21061cb0ef41Sopenharmony_ci return descriptor !== undefined && 21071cb0ef41Sopenharmony_ci typeof descriptor.value === 'function' && 21081cb0ef41Sopenharmony_ci builtInObjects.has(descriptor.value.name); 21091cb0ef41Sopenharmony_ci} 21101cb0ef41Sopenharmony_ci 21111cb0ef41Sopenharmony_ciconst firstErrorLine = (error) => StringPrototypeSplit(error.message, '\n', 1)[0]; 21121cb0ef41Sopenharmony_cilet CIRCULAR_ERROR_MESSAGE; 21131cb0ef41Sopenharmony_cifunction tryStringify(arg) { 21141cb0ef41Sopenharmony_ci try { 21151cb0ef41Sopenharmony_ci return JSONStringify(arg); 21161cb0ef41Sopenharmony_ci } catch (err) { 21171cb0ef41Sopenharmony_ci // Populate the circular error message lazily 21181cb0ef41Sopenharmony_ci if (!CIRCULAR_ERROR_MESSAGE) { 21191cb0ef41Sopenharmony_ci try { 21201cb0ef41Sopenharmony_ci const a = {}; 21211cb0ef41Sopenharmony_ci a.a = a; 21221cb0ef41Sopenharmony_ci JSONStringify(a); 21231cb0ef41Sopenharmony_ci } catch (circularError) { 21241cb0ef41Sopenharmony_ci CIRCULAR_ERROR_MESSAGE = firstErrorLine(circularError); 21251cb0ef41Sopenharmony_ci } 21261cb0ef41Sopenharmony_ci } 21271cb0ef41Sopenharmony_ci if (err.name === 'TypeError' && 21281cb0ef41Sopenharmony_ci firstErrorLine(err) === CIRCULAR_ERROR_MESSAGE) { 21291cb0ef41Sopenharmony_ci return '[Circular]'; 21301cb0ef41Sopenharmony_ci } 21311cb0ef41Sopenharmony_ci throw err; 21321cb0ef41Sopenharmony_ci } 21331cb0ef41Sopenharmony_ci} 21341cb0ef41Sopenharmony_ci 21351cb0ef41Sopenharmony_cifunction format(...args) { 21361cb0ef41Sopenharmony_ci return formatWithOptionsInternal(undefined, args); 21371cb0ef41Sopenharmony_ci} 21381cb0ef41Sopenharmony_ci 21391cb0ef41Sopenharmony_cifunction formatWithOptions(inspectOptions, ...args) { 21401cb0ef41Sopenharmony_ci validateObject(inspectOptions, 'inspectOptions', { allowArray: true }); 21411cb0ef41Sopenharmony_ci return formatWithOptionsInternal(inspectOptions, args); 21421cb0ef41Sopenharmony_ci} 21431cb0ef41Sopenharmony_ci 21441cb0ef41Sopenharmony_cifunction formatNumberNoColor(number, options) { 21451cb0ef41Sopenharmony_ci return formatNumber( 21461cb0ef41Sopenharmony_ci stylizeNoColor, 21471cb0ef41Sopenharmony_ci number, 21481cb0ef41Sopenharmony_ci options?.numericSeparator ?? inspectDefaultOptions.numericSeparator, 21491cb0ef41Sopenharmony_ci ); 21501cb0ef41Sopenharmony_ci} 21511cb0ef41Sopenharmony_ci 21521cb0ef41Sopenharmony_cifunction formatBigIntNoColor(bigint, options) { 21531cb0ef41Sopenharmony_ci return formatBigInt( 21541cb0ef41Sopenharmony_ci stylizeNoColor, 21551cb0ef41Sopenharmony_ci bigint, 21561cb0ef41Sopenharmony_ci options?.numericSeparator ?? inspectDefaultOptions.numericSeparator, 21571cb0ef41Sopenharmony_ci ); 21581cb0ef41Sopenharmony_ci} 21591cb0ef41Sopenharmony_ci 21601cb0ef41Sopenharmony_cifunction formatWithOptionsInternal(inspectOptions, args) { 21611cb0ef41Sopenharmony_ci const first = args[0]; 21621cb0ef41Sopenharmony_ci let a = 0; 21631cb0ef41Sopenharmony_ci let str = ''; 21641cb0ef41Sopenharmony_ci let join = ''; 21651cb0ef41Sopenharmony_ci 21661cb0ef41Sopenharmony_ci if (typeof first === 'string') { 21671cb0ef41Sopenharmony_ci if (args.length === 1) { 21681cb0ef41Sopenharmony_ci return first; 21691cb0ef41Sopenharmony_ci } 21701cb0ef41Sopenharmony_ci let tempStr; 21711cb0ef41Sopenharmony_ci let lastPos = 0; 21721cb0ef41Sopenharmony_ci 21731cb0ef41Sopenharmony_ci for (let i = 0; i < first.length - 1; i++) { 21741cb0ef41Sopenharmony_ci if (StringPrototypeCharCodeAt(first, i) === 37) { // '%' 21751cb0ef41Sopenharmony_ci const nextChar = StringPrototypeCharCodeAt(first, ++i); 21761cb0ef41Sopenharmony_ci if (a + 1 !== args.length) { 21771cb0ef41Sopenharmony_ci switch (nextChar) { 21781cb0ef41Sopenharmony_ci case 115: { // 's' 21791cb0ef41Sopenharmony_ci const tempArg = args[++a]; 21801cb0ef41Sopenharmony_ci if (typeof tempArg === 'number') { 21811cb0ef41Sopenharmony_ci tempStr = formatNumberNoColor(tempArg, inspectOptions); 21821cb0ef41Sopenharmony_ci } else if (typeof tempArg === 'bigint') { 21831cb0ef41Sopenharmony_ci tempStr = formatBigIntNoColor(tempArg, inspectOptions); 21841cb0ef41Sopenharmony_ci } else if (typeof tempArg !== 'object' || 21851cb0ef41Sopenharmony_ci tempArg === null || 21861cb0ef41Sopenharmony_ci !hasBuiltInToString(tempArg)) { 21871cb0ef41Sopenharmony_ci tempStr = String(tempArg); 21881cb0ef41Sopenharmony_ci } else { 21891cb0ef41Sopenharmony_ci tempStr = inspect(tempArg, { 21901cb0ef41Sopenharmony_ci ...inspectOptions, 21911cb0ef41Sopenharmony_ci compact: 3, 21921cb0ef41Sopenharmony_ci colors: false, 21931cb0ef41Sopenharmony_ci depth: 0, 21941cb0ef41Sopenharmony_ci }); 21951cb0ef41Sopenharmony_ci } 21961cb0ef41Sopenharmony_ci break; 21971cb0ef41Sopenharmony_ci } 21981cb0ef41Sopenharmony_ci case 106: // 'j' 21991cb0ef41Sopenharmony_ci tempStr = tryStringify(args[++a]); 22001cb0ef41Sopenharmony_ci break; 22011cb0ef41Sopenharmony_ci case 100: { // 'd' 22021cb0ef41Sopenharmony_ci const tempNum = args[++a]; 22031cb0ef41Sopenharmony_ci if (typeof tempNum === 'bigint') { 22041cb0ef41Sopenharmony_ci tempStr = formatBigIntNoColor(tempNum, inspectOptions); 22051cb0ef41Sopenharmony_ci } else if (typeof tempNum === 'symbol') { 22061cb0ef41Sopenharmony_ci tempStr = 'NaN'; 22071cb0ef41Sopenharmony_ci } else { 22081cb0ef41Sopenharmony_ci tempStr = formatNumberNoColor(Number(tempNum), inspectOptions); 22091cb0ef41Sopenharmony_ci } 22101cb0ef41Sopenharmony_ci break; 22111cb0ef41Sopenharmony_ci } 22121cb0ef41Sopenharmony_ci case 79: // 'O' 22131cb0ef41Sopenharmony_ci tempStr = inspect(args[++a], inspectOptions); 22141cb0ef41Sopenharmony_ci break; 22151cb0ef41Sopenharmony_ci case 111: // 'o' 22161cb0ef41Sopenharmony_ci tempStr = inspect(args[++a], { 22171cb0ef41Sopenharmony_ci ...inspectOptions, 22181cb0ef41Sopenharmony_ci showHidden: true, 22191cb0ef41Sopenharmony_ci showProxy: true, 22201cb0ef41Sopenharmony_ci depth: 4, 22211cb0ef41Sopenharmony_ci }); 22221cb0ef41Sopenharmony_ci break; 22231cb0ef41Sopenharmony_ci case 105: { // 'i' 22241cb0ef41Sopenharmony_ci const tempInteger = args[++a]; 22251cb0ef41Sopenharmony_ci if (typeof tempInteger === 'bigint') { 22261cb0ef41Sopenharmony_ci tempStr = formatBigIntNoColor(tempInteger, inspectOptions); 22271cb0ef41Sopenharmony_ci } else if (typeof tempInteger === 'symbol') { 22281cb0ef41Sopenharmony_ci tempStr = 'NaN'; 22291cb0ef41Sopenharmony_ci } else { 22301cb0ef41Sopenharmony_ci tempStr = formatNumberNoColor( 22311cb0ef41Sopenharmony_ci NumberParseInt(tempInteger), inspectOptions); 22321cb0ef41Sopenharmony_ci } 22331cb0ef41Sopenharmony_ci break; 22341cb0ef41Sopenharmony_ci } 22351cb0ef41Sopenharmony_ci case 102: { // 'f' 22361cb0ef41Sopenharmony_ci const tempFloat = args[++a]; 22371cb0ef41Sopenharmony_ci if (typeof tempFloat === 'symbol') { 22381cb0ef41Sopenharmony_ci tempStr = 'NaN'; 22391cb0ef41Sopenharmony_ci } else { 22401cb0ef41Sopenharmony_ci tempStr = formatNumberNoColor( 22411cb0ef41Sopenharmony_ci NumberParseFloat(tempFloat), inspectOptions); 22421cb0ef41Sopenharmony_ci } 22431cb0ef41Sopenharmony_ci break; 22441cb0ef41Sopenharmony_ci } 22451cb0ef41Sopenharmony_ci case 99: // 'c' 22461cb0ef41Sopenharmony_ci a += 1; 22471cb0ef41Sopenharmony_ci tempStr = ''; 22481cb0ef41Sopenharmony_ci break; 22491cb0ef41Sopenharmony_ci case 37: // '%' 22501cb0ef41Sopenharmony_ci str += StringPrototypeSlice(first, lastPos, i); 22511cb0ef41Sopenharmony_ci lastPos = i + 1; 22521cb0ef41Sopenharmony_ci continue; 22531cb0ef41Sopenharmony_ci default: // Any other character is not a correct placeholder 22541cb0ef41Sopenharmony_ci continue; 22551cb0ef41Sopenharmony_ci } 22561cb0ef41Sopenharmony_ci if (lastPos !== i - 1) { 22571cb0ef41Sopenharmony_ci str += StringPrototypeSlice(first, lastPos, i - 1); 22581cb0ef41Sopenharmony_ci } 22591cb0ef41Sopenharmony_ci str += tempStr; 22601cb0ef41Sopenharmony_ci lastPos = i + 1; 22611cb0ef41Sopenharmony_ci } else if (nextChar === 37) { 22621cb0ef41Sopenharmony_ci str += StringPrototypeSlice(first, lastPos, i); 22631cb0ef41Sopenharmony_ci lastPos = i + 1; 22641cb0ef41Sopenharmony_ci } 22651cb0ef41Sopenharmony_ci } 22661cb0ef41Sopenharmony_ci } 22671cb0ef41Sopenharmony_ci if (lastPos !== 0) { 22681cb0ef41Sopenharmony_ci a++; 22691cb0ef41Sopenharmony_ci join = ' '; 22701cb0ef41Sopenharmony_ci if (lastPos < first.length) { 22711cb0ef41Sopenharmony_ci str += StringPrototypeSlice(first, lastPos); 22721cb0ef41Sopenharmony_ci } 22731cb0ef41Sopenharmony_ci } 22741cb0ef41Sopenharmony_ci } 22751cb0ef41Sopenharmony_ci 22761cb0ef41Sopenharmony_ci while (a < args.length) { 22771cb0ef41Sopenharmony_ci const value = args[a]; 22781cb0ef41Sopenharmony_ci str += join; 22791cb0ef41Sopenharmony_ci str += typeof value !== 'string' ? inspect(value, inspectOptions) : value; 22801cb0ef41Sopenharmony_ci join = ' '; 22811cb0ef41Sopenharmony_ci a++; 22821cb0ef41Sopenharmony_ci } 22831cb0ef41Sopenharmony_ci return str; 22841cb0ef41Sopenharmony_ci} 22851cb0ef41Sopenharmony_ci 22861cb0ef41Sopenharmony_cifunction isZeroWidthCodePoint(code) { 22871cb0ef41Sopenharmony_ci return code <= 0x1F || // C0 control codes 22881cb0ef41Sopenharmony_ci (code >= 0x7F && code <= 0x9F) || // C1 control codes 22891cb0ef41Sopenharmony_ci (code >= 0x300 && code <= 0x36F) || // Combining Diacritical Marks 22901cb0ef41Sopenharmony_ci (code >= 0x200B && code <= 0x200F) || // Modifying Invisible Characters 22911cb0ef41Sopenharmony_ci // Combining Diacritical Marks for Symbols 22921cb0ef41Sopenharmony_ci (code >= 0x20D0 && code <= 0x20FF) || 22931cb0ef41Sopenharmony_ci (code >= 0xFE00 && code <= 0xFE0F) || // Variation Selectors 22941cb0ef41Sopenharmony_ci (code >= 0xFE20 && code <= 0xFE2F) || // Combining Half Marks 22951cb0ef41Sopenharmony_ci (code >= 0xE0100 && code <= 0xE01EF); // Variation Selectors 22961cb0ef41Sopenharmony_ci} 22971cb0ef41Sopenharmony_ci 22981cb0ef41Sopenharmony_ciif (internalBinding('config').hasIntl) { 22991cb0ef41Sopenharmony_ci const icu = internalBinding('icu'); 23001cb0ef41Sopenharmony_ci // icu.getStringWidth(string, ambiguousAsFullWidth, expandEmojiSequence) 23011cb0ef41Sopenharmony_ci // Defaults: ambiguousAsFullWidth = false; expandEmojiSequence = true; 23021cb0ef41Sopenharmony_ci // TODO(BridgeAR): Expose the options to the user. That is probably the 23031cb0ef41Sopenharmony_ci // best thing possible at the moment, since it's difficult to know what 23041cb0ef41Sopenharmony_ci // the receiving end supports. 23051cb0ef41Sopenharmony_ci getStringWidth = function getStringWidth(str, removeControlChars = true) { 23061cb0ef41Sopenharmony_ci let width = 0; 23071cb0ef41Sopenharmony_ci 23081cb0ef41Sopenharmony_ci if (removeControlChars) { 23091cb0ef41Sopenharmony_ci str = stripVTControlCharacters(str); 23101cb0ef41Sopenharmony_ci } 23111cb0ef41Sopenharmony_ci for (let i = 0; i < str.length; i++) { 23121cb0ef41Sopenharmony_ci // Try to avoid calling into C++ by first handling the ASCII portion of 23131cb0ef41Sopenharmony_ci // the string. If it is fully ASCII, we skip the C++ part. 23141cb0ef41Sopenharmony_ci const code = StringPrototypeCharCodeAt(str, i); 23151cb0ef41Sopenharmony_ci if (code >= 127) { 23161cb0ef41Sopenharmony_ci width += icu.getStringWidth(StringPrototypeNormalize(StringPrototypeSlice(str, i), 'NFC')); 23171cb0ef41Sopenharmony_ci break; 23181cb0ef41Sopenharmony_ci } 23191cb0ef41Sopenharmony_ci width += code >= 32 ? 1 : 0; 23201cb0ef41Sopenharmony_ci } 23211cb0ef41Sopenharmony_ci return width; 23221cb0ef41Sopenharmony_ci }; 23231cb0ef41Sopenharmony_ci} else { 23241cb0ef41Sopenharmony_ci /** 23251cb0ef41Sopenharmony_ci * Returns the number of columns required to display the given string. 23261cb0ef41Sopenharmony_ci */ 23271cb0ef41Sopenharmony_ci getStringWidth = function getStringWidth(str, removeControlChars = true) { 23281cb0ef41Sopenharmony_ci let width = 0; 23291cb0ef41Sopenharmony_ci 23301cb0ef41Sopenharmony_ci if (removeControlChars) 23311cb0ef41Sopenharmony_ci str = stripVTControlCharacters(str); 23321cb0ef41Sopenharmony_ci str = StringPrototypeNormalize(str, 'NFC'); 23331cb0ef41Sopenharmony_ci for (const char of new SafeStringIterator(str)) { 23341cb0ef41Sopenharmony_ci const code = StringPrototypeCodePointAt(char, 0); 23351cb0ef41Sopenharmony_ci if (isFullWidthCodePoint(code)) { 23361cb0ef41Sopenharmony_ci width += 2; 23371cb0ef41Sopenharmony_ci } else if (!isZeroWidthCodePoint(code)) { 23381cb0ef41Sopenharmony_ci width++; 23391cb0ef41Sopenharmony_ci } 23401cb0ef41Sopenharmony_ci } 23411cb0ef41Sopenharmony_ci 23421cb0ef41Sopenharmony_ci return width; 23431cb0ef41Sopenharmony_ci }; 23441cb0ef41Sopenharmony_ci 23451cb0ef41Sopenharmony_ci /** 23461cb0ef41Sopenharmony_ci * Returns true if the character represented by a given 23471cb0ef41Sopenharmony_ci * Unicode code point is full-width. Otherwise returns false. 23481cb0ef41Sopenharmony_ci */ 23491cb0ef41Sopenharmony_ci const isFullWidthCodePoint = (code) => { 23501cb0ef41Sopenharmony_ci // Code points are partially derived from: 23511cb0ef41Sopenharmony_ci // https://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt 23521cb0ef41Sopenharmony_ci return code >= 0x1100 && ( 23531cb0ef41Sopenharmony_ci code <= 0x115f || // Hangul Jamo 23541cb0ef41Sopenharmony_ci code === 0x2329 || // LEFT-POINTING ANGLE BRACKET 23551cb0ef41Sopenharmony_ci code === 0x232a || // RIGHT-POINTING ANGLE BRACKET 23561cb0ef41Sopenharmony_ci // CJK Radicals Supplement .. Enclosed CJK Letters and Months 23571cb0ef41Sopenharmony_ci (code >= 0x2e80 && code <= 0x3247 && code !== 0x303f) || 23581cb0ef41Sopenharmony_ci // Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A 23591cb0ef41Sopenharmony_ci (code >= 0x3250 && code <= 0x4dbf) || 23601cb0ef41Sopenharmony_ci // CJK Unified Ideographs .. Yi Radicals 23611cb0ef41Sopenharmony_ci (code >= 0x4e00 && code <= 0xa4c6) || 23621cb0ef41Sopenharmony_ci // Hangul Jamo Extended-A 23631cb0ef41Sopenharmony_ci (code >= 0xa960 && code <= 0xa97c) || 23641cb0ef41Sopenharmony_ci // Hangul Syllables 23651cb0ef41Sopenharmony_ci (code >= 0xac00 && code <= 0xd7a3) || 23661cb0ef41Sopenharmony_ci // CJK Compatibility Ideographs 23671cb0ef41Sopenharmony_ci (code >= 0xf900 && code <= 0xfaff) || 23681cb0ef41Sopenharmony_ci // Vertical Forms 23691cb0ef41Sopenharmony_ci (code >= 0xfe10 && code <= 0xfe19) || 23701cb0ef41Sopenharmony_ci // CJK Compatibility Forms .. Small Form Variants 23711cb0ef41Sopenharmony_ci (code >= 0xfe30 && code <= 0xfe6b) || 23721cb0ef41Sopenharmony_ci // Halfwidth and Fullwidth Forms 23731cb0ef41Sopenharmony_ci (code >= 0xff01 && code <= 0xff60) || 23741cb0ef41Sopenharmony_ci (code >= 0xffe0 && code <= 0xffe6) || 23751cb0ef41Sopenharmony_ci // Kana Supplement 23761cb0ef41Sopenharmony_ci (code >= 0x1b000 && code <= 0x1b001) || 23771cb0ef41Sopenharmony_ci // Enclosed Ideographic Supplement 23781cb0ef41Sopenharmony_ci (code >= 0x1f200 && code <= 0x1f251) || 23791cb0ef41Sopenharmony_ci // Miscellaneous Symbols and Pictographs 0x1f300 - 0x1f5ff 23801cb0ef41Sopenharmony_ci // Emoticons 0x1f600 - 0x1f64f 23811cb0ef41Sopenharmony_ci (code >= 0x1f300 && code <= 0x1f64f) || 23821cb0ef41Sopenharmony_ci // CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane 23831cb0ef41Sopenharmony_ci (code >= 0x20000 && code <= 0x3fffd) 23841cb0ef41Sopenharmony_ci ); 23851cb0ef41Sopenharmony_ci }; 23861cb0ef41Sopenharmony_ci 23871cb0ef41Sopenharmony_ci} 23881cb0ef41Sopenharmony_ci 23891cb0ef41Sopenharmony_ci/** 23901cb0ef41Sopenharmony_ci * Remove all VT control characters. Use to estimate displayed string width. 23911cb0ef41Sopenharmony_ci */ 23921cb0ef41Sopenharmony_cifunction stripVTControlCharacters(str) { 23931cb0ef41Sopenharmony_ci validateString(str, 'str'); 23941cb0ef41Sopenharmony_ci 23951cb0ef41Sopenharmony_ci return RegExpPrototypeSymbolReplace(ansi, str, ''); 23961cb0ef41Sopenharmony_ci} 23971cb0ef41Sopenharmony_ci 23981cb0ef41Sopenharmony_cimodule.exports = { 23991cb0ef41Sopenharmony_ci identicalSequenceRange, 24001cb0ef41Sopenharmony_ci inspect, 24011cb0ef41Sopenharmony_ci inspectDefaultOptions, 24021cb0ef41Sopenharmony_ci format, 24031cb0ef41Sopenharmony_ci formatWithOptions, 24041cb0ef41Sopenharmony_ci getStringWidth, 24051cb0ef41Sopenharmony_ci stripVTControlCharacters, 24061cb0ef41Sopenharmony_ci isZeroWidthCodePoint, 24071cb0ef41Sopenharmony_ci}; 2408