2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-07-06 06:03:12 -04:00
|
|
|
|
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, internals, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
isAnyArrayBuffer,
|
|
|
|
isArgumentsObject,
|
|
|
|
isArrayBuffer,
|
|
|
|
isAsyncFunction,
|
|
|
|
isBigIntObject,
|
|
|
|
isBooleanObject,
|
|
|
|
isBoxedPrimitive,
|
|
|
|
isDataView,
|
|
|
|
isDate,
|
|
|
|
isGeneratorFunction,
|
|
|
|
isMap,
|
|
|
|
isMapIterator,
|
|
|
|
isModuleNamespaceObject,
|
|
|
|
isNativeError,
|
|
|
|
isNumberObject,
|
|
|
|
isPromise,
|
|
|
|
isRegExp,
|
|
|
|
isSet,
|
|
|
|
isSetIterator,
|
|
|
|
isStringObject,
|
|
|
|
isTypedArray,
|
|
|
|
isWeakMap,
|
|
|
|
isWeakSet,
|
|
|
|
} = core;
|
|
|
|
const {
|
|
|
|
op_get_constructor_name,
|
|
|
|
op_get_non_index_property_names,
|
|
|
|
op_preview_entries,
|
|
|
|
} = core.ensureFastOps(true);
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
Array,
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayBufferPrototypeGetByteLength,
|
|
|
|
ArrayIsArray,
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeFill,
|
|
|
|
ArrayPrototypeFilter,
|
|
|
|
ArrayPrototypeFind,
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeForEach,
|
|
|
|
ArrayPrototypeIncludes,
|
|
|
|
ArrayPrototypeJoin,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
ArrayPrototypePop,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
ArrayPrototypePushApply,
|
|
|
|
ArrayPrototypeReduce,
|
|
|
|
ArrayPrototypeShift,
|
|
|
|
ArrayPrototypeSlice,
|
|
|
|
ArrayPrototypeSort,
|
|
|
|
ArrayPrototypeSplice,
|
|
|
|
ArrayPrototypeUnshift,
|
|
|
|
BigIntPrototypeValueOf,
|
|
|
|
Boolean,
|
|
|
|
BooleanPrototypeValueOf,
|
|
|
|
DateNow,
|
|
|
|
DatePrototypeGetTime,
|
|
|
|
DatePrototypeToISOString,
|
|
|
|
Error,
|
2024-01-03 23:12:38 -05:00
|
|
|
ErrorCaptureStackTrace,
|
2024-01-10 17:37:25 -05:00
|
|
|
ErrorPrototype,
|
2023-06-05 15:57:01 -04:00
|
|
|
ErrorPrototypeToString,
|
2023-02-07 14:22:46 -05:00
|
|
|
FunctionPrototypeBind,
|
2023-05-01 09:30:02 -04:00
|
|
|
FunctionPrototypeCall,
|
|
|
|
FunctionPrototypeToString,
|
2023-02-07 14:22:46 -05:00
|
|
|
MapPrototypeDelete,
|
2023-05-01 09:30:02 -04:00
|
|
|
MapPrototypeEntries,
|
2023-02-07 14:22:46 -05:00
|
|
|
MapPrototypeForEach,
|
2023-05-01 09:30:02 -04:00
|
|
|
MapPrototypeGet,
|
2023-02-07 14:22:46 -05:00
|
|
|
MapPrototypeGetSize,
|
2023-05-01 09:30:02 -04:00
|
|
|
MapPrototypeHas,
|
|
|
|
MapPrototypeSet,
|
2023-02-07 14:22:46 -05:00
|
|
|
MathAbs,
|
2023-05-01 09:30:02 -04:00
|
|
|
MathFloor,
|
2023-02-07 14:22:46 -05:00
|
|
|
MathMax,
|
|
|
|
MathMin,
|
|
|
|
MathRound,
|
2023-05-01 09:30:02 -04:00
|
|
|
MathSqrt,
|
2023-02-07 14:22:46 -05:00
|
|
|
Number,
|
2023-05-01 09:30:02 -04:00
|
|
|
NumberIsInteger,
|
2024-01-10 17:37:25 -05:00
|
|
|
NumberIsNaN,
|
2023-05-01 09:30:02 -04:00
|
|
|
NumberParseInt,
|
2023-02-07 14:22:46 -05:00
|
|
|
NumberPrototypeToString,
|
2023-05-01 09:30:02 -04:00
|
|
|
NumberPrototypeValueOf,
|
|
|
|
ObjectAssign,
|
|
|
|
ObjectCreate,
|
|
|
|
ObjectDefineProperty,
|
|
|
|
ObjectFreeze,
|
|
|
|
ObjectFromEntries,
|
|
|
|
ObjectGetOwnPropertyDescriptor,
|
|
|
|
ObjectGetOwnPropertyNames,
|
|
|
|
ObjectGetOwnPropertySymbols,
|
|
|
|
ObjectGetPrototypeOf,
|
2023-05-02 06:15:45 -04:00
|
|
|
ObjectHasOwn,
|
2023-05-01 09:30:02 -04:00
|
|
|
ObjectIs,
|
|
|
|
ObjectKeys,
|
|
|
|
ObjectPrototype,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
ObjectPrototypePropertyIsEnumerable,
|
|
|
|
ObjectSetPrototypeOf,
|
|
|
|
ObjectValues,
|
2023-02-07 14:22:46 -05:00
|
|
|
Proxy,
|
|
|
|
ReflectGet,
|
|
|
|
ReflectGetOwnPropertyDescriptor,
|
|
|
|
ReflectGetPrototypeOf,
|
|
|
|
ReflectHas,
|
2023-05-01 09:30:02 -04:00
|
|
|
ReflectOwnKeys,
|
|
|
|
RegExpPrototypeExec,
|
|
|
|
RegExpPrototypeSymbolReplace,
|
|
|
|
RegExpPrototypeTest,
|
|
|
|
RegExpPrototypeToString,
|
|
|
|
SafeArrayIterator,
|
|
|
|
SafeMap,
|
|
|
|
SafeMapIterator,
|
|
|
|
SafeRegExp,
|
2023-04-30 03:24:13 -04:00
|
|
|
SafeSet,
|
|
|
|
SafeSetIterator,
|
2023-05-01 09:30:02 -04:00
|
|
|
SafeStringIterator,
|
|
|
|
SetPrototypeAdd,
|
|
|
|
SetPrototypeGetSize,
|
2024-01-10 17:37:25 -05:00
|
|
|
SetPrototypeHas,
|
2023-05-01 09:30:02 -04:00
|
|
|
SetPrototypeValues,
|
|
|
|
String,
|
|
|
|
StringPrototypeCharCodeAt,
|
|
|
|
StringPrototypeCodePointAt,
|
|
|
|
StringPrototypeEndsWith,
|
|
|
|
StringPrototypeIncludes,
|
|
|
|
StringPrototypeIndexOf,
|
|
|
|
StringPrototypeLastIndexOf,
|
|
|
|
StringPrototypeMatch,
|
|
|
|
StringPrototypeNormalize,
|
|
|
|
StringPrototypePadEnd,
|
|
|
|
StringPrototypePadStart,
|
|
|
|
StringPrototypeRepeat,
|
|
|
|
StringPrototypeReplace,
|
|
|
|
StringPrototypeReplaceAll,
|
|
|
|
StringPrototypeSlice,
|
|
|
|
StringPrototypeSplit,
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
StringPrototypeToLowerCase,
|
|
|
|
StringPrototypeTrim,
|
|
|
|
StringPrototypeValueOf,
|
|
|
|
Symbol,
|
|
|
|
SymbolFor,
|
|
|
|
SymbolHasInstance,
|
|
|
|
SymbolIterator,
|
|
|
|
SymbolPrototypeGetDescription,
|
|
|
|
SymbolPrototypeToString,
|
|
|
|
SymbolPrototypeValueOf,
|
|
|
|
SymbolToStringTag,
|
2023-04-30 03:24:13 -04:00
|
|
|
TypedArrayPrototypeGetByteLength,
|
2023-05-01 09:30:02 -04:00
|
|
|
TypedArrayPrototypeGetLength,
|
|
|
|
Uint8Array,
|
2023-02-07 14:22:46 -05:00
|
|
|
} = primordials;
|
2023-11-28 10:07:48 -05:00
|
|
|
|
2023-11-12 23:52:59 -05:00
|
|
|
let noColor = () => false;
|
2023-04-30 05:11:37 -04:00
|
|
|
|
2023-11-12 23:52:59 -05:00
|
|
|
function setNoColorFn(fn) {
|
|
|
|
noColor = fn;
|
2023-04-30 05:11:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function getNoColor() {
|
2023-11-12 23:52:59 -05:00
|
|
|
return noColor();
|
2023-04-30 05:11:37 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
|
2023-06-28 11:29:16 -04:00
|
|
|
function assert(cond, msg = "Assertion failed.") {
|
|
|
|
if (!cond) {
|
|
|
|
throw new AssertionError(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// Don't use 'blue' not visible on cmd.exe
|
|
|
|
const styles = {
|
|
|
|
special: "cyan",
|
|
|
|
number: "yellow",
|
|
|
|
bigint: "yellow",
|
|
|
|
boolean: "yellow",
|
|
|
|
undefined: "grey",
|
|
|
|
null: "bold",
|
|
|
|
string: "green",
|
|
|
|
symbol: "green",
|
|
|
|
date: "magenta",
|
|
|
|
// "name": intentionally not styling
|
|
|
|
// TODO(BridgeAR): Highlight regular expressions properly.
|
|
|
|
regexp: "red",
|
|
|
|
module: "underline",
|
2023-05-11 09:08:17 -04:00
|
|
|
internalError: "red",
|
2024-01-14 19:26:57 -05:00
|
|
|
temporal: "magenta",
|
2023-04-30 03:24:13 -04:00
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const defaultFG = 39;
|
|
|
|
const defaultBG = 49;
|
|
|
|
|
|
|
|
// Set Graphics Rendition https://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
|
|
|
// Each color consists of an array with the color code as first entry and the
|
|
|
|
// reset code as second entry.
|
|
|
|
const colors = {
|
|
|
|
reset: [0, 0],
|
|
|
|
bold: [1, 22],
|
|
|
|
dim: [2, 22], // Alias: faint
|
|
|
|
italic: [3, 23],
|
|
|
|
underline: [4, 24],
|
|
|
|
blink: [5, 25],
|
|
|
|
// Swap foreground and background colors
|
|
|
|
inverse: [7, 27], // Alias: swapcolors, swapColors
|
|
|
|
hidden: [8, 28], // Alias: conceal
|
|
|
|
strikethrough: [9, 29], // Alias: strikeThrough, crossedout, crossedOut
|
|
|
|
doubleunderline: [21, 24], // Alias: doubleUnderline
|
|
|
|
black: [30, defaultFG],
|
|
|
|
red: [31, defaultFG],
|
|
|
|
green: [32, defaultFG],
|
|
|
|
yellow: [33, defaultFG],
|
|
|
|
blue: [34, defaultFG],
|
|
|
|
magenta: [35, defaultFG],
|
|
|
|
cyan: [36, defaultFG],
|
|
|
|
white: [37, defaultFG],
|
|
|
|
bgBlack: [40, defaultBG],
|
|
|
|
bgRed: [41, defaultBG],
|
|
|
|
bgGreen: [42, defaultBG],
|
|
|
|
bgYellow: [43, defaultBG],
|
|
|
|
bgBlue: [44, defaultBG],
|
|
|
|
bgMagenta: [45, defaultBG],
|
|
|
|
bgCyan: [46, defaultBG],
|
|
|
|
bgWhite: [47, defaultBG],
|
|
|
|
framed: [51, 54],
|
|
|
|
overlined: [53, 55],
|
|
|
|
gray: [90, defaultFG], // Alias: grey, blackBright
|
|
|
|
redBright: [91, defaultFG],
|
|
|
|
greenBright: [92, defaultFG],
|
|
|
|
yellowBright: [93, defaultFG],
|
|
|
|
blueBright: [94, defaultFG],
|
|
|
|
magentaBright: [95, defaultFG],
|
|
|
|
cyanBright: [96, defaultFG],
|
|
|
|
whiteBright: [97, defaultFG],
|
|
|
|
bgGray: [100, defaultBG], // Alias: bgGrey, bgBlackBright
|
|
|
|
bgRedBright: [101, defaultBG],
|
|
|
|
bgGreenBright: [102, defaultBG],
|
|
|
|
bgYellowBright: [103, defaultBG],
|
|
|
|
bgBlueBright: [104, defaultBG],
|
|
|
|
bgMagentaBright: [105, defaultBG],
|
|
|
|
bgCyanBright: [106, defaultBG],
|
|
|
|
bgWhiteBright: [107, defaultBG],
|
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function defineColorAlias(target, alias) {
|
|
|
|
ObjectDefineProperty(colors, alias, {
|
|
|
|
get() {
|
|
|
|
return this[target];
|
|
|
|
},
|
|
|
|
set(value) {
|
|
|
|
this[target] = value;
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
});
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-02-10 06:52:54 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
defineColorAlias("gray", "grey");
|
|
|
|
defineColorAlias("gray", "blackBright");
|
|
|
|
defineColorAlias("bgGray", "bgGrey");
|
|
|
|
defineColorAlias("bgGray", "bgBlackBright");
|
|
|
|
defineColorAlias("dim", "faint");
|
|
|
|
defineColorAlias("strikethrough", "crossedout");
|
|
|
|
defineColorAlias("strikethrough", "strikeThrough");
|
|
|
|
defineColorAlias("strikethrough", "crossedOut");
|
|
|
|
defineColorAlias("hidden", "conceal");
|
|
|
|
defineColorAlias("inverse", "swapColors");
|
|
|
|
defineColorAlias("inverse", "swapcolors");
|
|
|
|
defineColorAlias("doubleunderline", "doubleUnderline");
|
2020-09-18 09:20:55 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// https://tc39.es/ecma262/#sec-get-sharedarraybuffer.prototype.bytelength
|
|
|
|
let _getSharedArrayBufferByteLength;
|
|
|
|
|
2023-06-06 05:06:00 -04:00
|
|
|
function getSharedArrayBufferByteLength(value) {
|
|
|
|
// TODO(kt3k): add SharedArrayBuffer to primordials
|
|
|
|
_getSharedArrayBufferByteLength ??= ObjectGetOwnPropertyDescriptor(
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
SharedArrayBuffer.prototype,
|
|
|
|
"byteLength",
|
|
|
|
).get;
|
|
|
|
|
|
|
|
return FunctionPrototypeCall(_getSharedArrayBufferByteLength, value);
|
|
|
|
}
|
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
// The name property is used to allow cross realms to make a determination
|
|
|
|
// This is the same as WHATWG's structuredClone algorithm
|
|
|
|
// https://github.com/whatwg/html/pull/5150
|
|
|
|
function isAggregateError(value) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return (
|
2024-01-03 23:12:38 -05:00
|
|
|
isNativeError(value) &&
|
|
|
|
value.name === "AggregateError" &&
|
|
|
|
ArrayIsArray(value.errors)
|
2023-04-30 03:24:13 -04:00
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const kObjectType = 0;
|
|
|
|
const kArrayType = 1;
|
|
|
|
const kArrayExtrasType = 2;
|
|
|
|
|
|
|
|
const kMinLineLength = 16;
|
|
|
|
|
|
|
|
// Constants to map the iterator state.
|
|
|
|
const kWeak = 0;
|
|
|
|
const kIterator = 1;
|
|
|
|
const kMapEntries = 2;
|
|
|
|
|
|
|
|
// Escaped control characters (plus the single quote and the backslash). Use
|
|
|
|
// empty strings to fill up unused entries.
|
|
|
|
// deno-fmt-ignore
|
|
|
|
const meta = [
|
|
|
|
'\\x00', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\x07', // x07
|
|
|
|
'\\b', '\\t', '\\n', '\\x0B', '\\f', '\\r', '\\x0E', '\\x0F', // x0F
|
|
|
|
'\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17', // x17
|
|
|
|
'\\x18', '\\x19', '\\x1A', '\\x1B', '\\x1C', '\\x1D', '\\x1E', '\\x1F', // x1F
|
|
|
|
'', '', '', '', '', '', '', "\\'", '', '', '', '', '', '', '', '', // x2F
|
|
|
|
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x3F
|
|
|
|
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x4F
|
|
|
|
'', '', '', '', '', '', '', '', '', '', '', '', '\\\\', '', '', '', // x5F
|
|
|
|
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x6F
|
|
|
|
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\\x7F', // x7F
|
|
|
|
'\\x80', '\\x81', '\\x82', '\\x83', '\\x84', '\\x85', '\\x86', '\\x87', // x87
|
|
|
|
'\\x88', '\\x89', '\\x8A', '\\x8B', '\\x8C', '\\x8D', '\\x8E', '\\x8F', // x8F
|
|
|
|
'\\x90', '\\x91', '\\x92', '\\x93', '\\x94', '\\x95', '\\x96', '\\x97', // x97
|
|
|
|
'\\x98', '\\x99', '\\x9A', '\\x9B', '\\x9C', '\\x9D', '\\x9E', '\\x9F', // x9F
|
|
|
|
];
|
|
|
|
|
|
|
|
// https://tc39.es/ecma262/#sec-IsHTMLDDA-internal-slot
|
|
|
|
const isUndetectableObject = (v) => typeof v === "undefined" && v !== undefined;
|
|
|
|
|
|
|
|
const strEscapeSequencesReplacer = new SafeRegExp(
|
|
|
|
"[\x00-\x1f\x27\x5c\x7f-\x9f]",
|
|
|
|
"g",
|
|
|
|
);
|
|
|
|
|
|
|
|
const keyStrRegExp = new SafeRegExp("^[a-zA-Z_][a-zA-Z_0-9]*$");
|
|
|
|
const numberRegExp = new SafeRegExp("^(0|[1-9][0-9]*)$");
|
|
|
|
|
|
|
|
// TODO(wafuwafu13): Figure out
|
2023-05-01 09:30:02 -04:00
|
|
|
const escapeFn = (str) => meta[StringPrototypeCharCodeAt(str, 0)];
|
2023-04-30 03:24:13 -04:00
|
|
|
|
|
|
|
function stylizeNoColor(str) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// node custom inspect symbol
|
|
|
|
const nodeCustomInspectSymbol = SymbolFor("nodejs.util.inspect.custom");
|
|
|
|
|
|
|
|
// This non-unique symbol is used to support op_crates, ie.
|
|
|
|
// in extensions/web we don't want to depend on public
|
|
|
|
// Symbol.for("Deno.customInspect") symbol defined in the public API.
|
|
|
|
// Internal only, shouldn't be used by users.
|
|
|
|
const privateCustomInspect = SymbolFor("Deno.privateCustomInspect");
|
|
|
|
|
|
|
|
function getUserOptions(ctx, isCrossContext) {
|
|
|
|
const ret = {
|
|
|
|
stylize: ctx.stylize,
|
|
|
|
showHidden: ctx.showHidden,
|
|
|
|
depth: ctx.depth,
|
|
|
|
colors: ctx.colors,
|
|
|
|
customInspect: ctx.customInspect,
|
|
|
|
showProxy: ctx.showProxy,
|
|
|
|
maxArrayLength: ctx.maxArrayLength,
|
|
|
|
maxStringLength: ctx.maxStringLength,
|
|
|
|
breakLength: ctx.breakLength,
|
|
|
|
compact: ctx.compact,
|
|
|
|
sorted: ctx.sorted,
|
|
|
|
getters: ctx.getters,
|
|
|
|
numericSeparator: ctx.numericSeparator,
|
|
|
|
...ctx.userOptions,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Typically, the target value will be an instance of `Object`. If that is
|
|
|
|
// *not* the case, the object may come from another vm.Context, and we want
|
|
|
|
// to avoid passing it objects from this Context in that case, so we remove
|
|
|
|
// the prototype from the returned object itself + the `stylize()` function,
|
|
|
|
// and remove all other non-primitives, including non-primitive user options.
|
|
|
|
if (isCrossContext) {
|
|
|
|
ObjectSetPrototypeOf(ret, null);
|
|
|
|
for (const key of new SafeArrayIterator(ObjectKeys(ret))) {
|
|
|
|
if (
|
|
|
|
(typeof ret[key] === "object" || typeof ret[key] === "function") &&
|
|
|
|
ret[key] !== null
|
|
|
|
) {
|
|
|
|
delete ret[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.stylize = ObjectSetPrototypeOf((value, flavour) => {
|
|
|
|
let stylized;
|
|
|
|
try {
|
|
|
|
stylized = `${ctx.stylize(value, flavour)}`;
|
|
|
|
} catch {
|
|
|
|
// Continue regardless of error.
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
|
|
|
|
if (typeof stylized !== "string") return value;
|
|
|
|
// `stylized` is a string as it should be, which is safe to pass along.
|
|
|
|
return stylized;
|
|
|
|
}, null);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-02-10 06:52:54 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: using `formatValue` directly requires the indentation level to be
|
|
|
|
// corrected by setting `ctx.indentationLvL += diff` and then to decrease the
|
|
|
|
// value afterwards again.
|
|
|
|
function formatValue(
|
|
|
|
ctx,
|
|
|
|
value,
|
|
|
|
recurseTimes,
|
|
|
|
typedArray,
|
|
|
|
) {
|
|
|
|
// Primitive types cannot have properties.
|
|
|
|
if (
|
|
|
|
typeof value !== "object" &&
|
|
|
|
typeof value !== "function" &&
|
|
|
|
!isUndetectableObject(value)
|
|
|
|
) {
|
|
|
|
return formatPrimitive(ctx.stylize, value, ctx);
|
|
|
|
}
|
|
|
|
if (value === null) {
|
|
|
|
return ctx.stylize("null", "null");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Memorize the context for custom inspection on proxies.
|
|
|
|
const context = value;
|
|
|
|
// Always check for proxies to prevent side effects and to prevent triggering
|
|
|
|
// any proxy handlers.
|
|
|
|
// TODO(wafuwafu13): Set Proxy
|
|
|
|
const proxyDetails = core.getProxyDetails(value);
|
|
|
|
// const proxy = getProxyDetails(value, !!ctx.showProxy);
|
|
|
|
// if (proxy !== undefined) {
|
|
|
|
// if (ctx.showProxy) {
|
|
|
|
// return formatProxy(ctx, proxy, recurseTimes);
|
|
|
|
// }
|
|
|
|
// value = proxy;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Provide a hook for user-specified inspect functions.
|
|
|
|
// Check that value is an object with an inspect function on it.
|
|
|
|
if (ctx.customInspect) {
|
|
|
|
if (
|
|
|
|
ReflectHas(value, customInspect) &&
|
|
|
|
typeof value[customInspect] === "function"
|
2021-02-10 06:52:54 -05:00
|
|
|
) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return String(value[customInspect](inspect, ctx));
|
|
|
|
} else if (
|
|
|
|
ReflectHas(value, privateCustomInspect) &&
|
|
|
|
typeof value[privateCustomInspect] === "function"
|
|
|
|
) {
|
|
|
|
// TODO(nayeemrmn): `inspect` is passed as an argument because custom
|
|
|
|
// inspect implementations in `extensions` need it, but may not have access
|
|
|
|
// to the `Deno` namespace in web workers. Remove when the `Deno`
|
|
|
|
// namespace is always enabled.
|
|
|
|
return String(value[privateCustomInspect](inspect, ctx));
|
|
|
|
} else if (ReflectHas(value, nodeCustomInspectSymbol)) {
|
|
|
|
const maybeCustom = value[nodeCustomInspectSymbol];
|
|
|
|
if (
|
|
|
|
typeof maybeCustom === "function" &&
|
|
|
|
// Filter out the util module, its inspect function is special.
|
|
|
|
maybeCustom !== ctx.inspect &&
|
|
|
|
// Also filter out any prototype objects using the circular check.
|
|
|
|
!(value.constructor && value.constructor.prototype === value)
|
|
|
|
) {
|
|
|
|
// This makes sure the recurseTimes are reported as before while using
|
|
|
|
// a counter internally.
|
|
|
|
const depth = ctx.depth === null ? null : ctx.depth - recurseTimes;
|
|
|
|
// TODO(@crowlKats): proxy handling
|
|
|
|
const isCrossContext = !ObjectPrototypeIsPrototypeOf(
|
|
|
|
ObjectPrototype,
|
|
|
|
context,
|
|
|
|
);
|
|
|
|
const ret = FunctionPrototypeCall(
|
|
|
|
maybeCustom,
|
|
|
|
context,
|
|
|
|
depth,
|
|
|
|
getUserOptions(ctx, isCrossContext),
|
|
|
|
ctx.inspect,
|
|
|
|
);
|
|
|
|
// If the custom inspection method returned `this`, don't go into
|
|
|
|
// infinite recursion.
|
|
|
|
if (ret !== context) {
|
|
|
|
if (typeof ret !== "string") {
|
|
|
|
return formatValue(ctx, ret, recurseTimes);
|
|
|
|
}
|
|
|
|
return StringPrototypeReplaceAll(
|
|
|
|
ret,
|
|
|
|
"\n",
|
|
|
|
`\n${StringPrototypeRepeat(" ", ctx.indentationLvl)}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// Using an array here is actually better for the average case than using
|
|
|
|
// a Set. `seen` will only check for the depth and will never grow too large.
|
2023-05-01 09:30:02 -04:00
|
|
|
if (ArrayPrototypeIncludes(ctx.seen, value)) {
|
2023-04-30 03:24:13 -04:00
|
|
|
let index = 1;
|
|
|
|
if (ctx.circular === undefined) {
|
|
|
|
ctx.circular = new SafeMap();
|
2023-05-01 09:30:02 -04:00
|
|
|
MapPrototypeSet(ctx.circular, value, index);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else {
|
|
|
|
index = ctx.circular.get(value);
|
|
|
|
if (index === undefined) {
|
|
|
|
index = ctx.circular.size + 1;
|
2023-05-01 09:30:02 -04:00
|
|
|
MapPrototypeSet(ctx.circular, value, index);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
return ctx.stylize(`[Circular *${index}]`, "special");
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatRaw(ctx, value, recurseTimes, typedArray, proxyDetails);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClassBase(value, constructor, tag) {
|
2023-05-02 06:15:45 -04:00
|
|
|
const hasName = ObjectHasOwn(value, "name");
|
2023-04-30 03:24:13 -04:00
|
|
|
const name = (hasName && value.name) || "(anonymous)";
|
|
|
|
let base = `class ${name}`;
|
|
|
|
if (constructor !== "Function" && constructor !== null) {
|
|
|
|
base += ` [${constructor}]`;
|
|
|
|
}
|
|
|
|
if (tag !== "" && constructor !== tag) {
|
|
|
|
base += ` [${tag}]`;
|
|
|
|
}
|
|
|
|
if (constructor !== null) {
|
|
|
|
const superName = ObjectGetPrototypeOf(value).name;
|
|
|
|
if (superName) {
|
|
|
|
base += ` extends ${superName}`;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-04-30 03:24:13 -04:00
|
|
|
base += " extends [null prototype]";
|
|
|
|
}
|
|
|
|
return `[${base}]`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stripCommentsRegExp = new SafeRegExp(
|
|
|
|
"(\\/\\/.*?\\n)|(\\/\\*(.|\\n)*?\\*\\/)",
|
|
|
|
"g",
|
|
|
|
);
|
|
|
|
const classRegExp = new SafeRegExp("^(\\s+[^(]*?)\\s*{");
|
|
|
|
|
|
|
|
function getFunctionBase(value, constructor, tag) {
|
|
|
|
const stringified = FunctionPrototypeToString(value);
|
|
|
|
if (
|
|
|
|
StringPrototypeStartsWith(stringified, "class") &&
|
|
|
|
StringPrototypeEndsWith(stringified, "}")
|
|
|
|
) {
|
|
|
|
const slice = StringPrototypeSlice(stringified, 5, -1);
|
|
|
|
const bracketIndex = StringPrototypeIndexOf(slice, "{");
|
|
|
|
if (
|
|
|
|
bracketIndex !== -1 &&
|
|
|
|
(!StringPrototypeIncludes(
|
|
|
|
StringPrototypeSlice(slice, 0, bracketIndex),
|
|
|
|
"(",
|
|
|
|
) ||
|
|
|
|
// Slow path to guarantee that it's indeed a class.
|
|
|
|
RegExpPrototypeExec(
|
|
|
|
classRegExp,
|
|
|
|
RegExpPrototypeSymbolReplace(stripCommentsRegExp, slice),
|
|
|
|
) !== null)
|
|
|
|
) {
|
|
|
|
return getClassBase(value, constructor, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let type = "Function";
|
|
|
|
if (isGeneratorFunction(value)) {
|
|
|
|
type = `Generator${type}`;
|
|
|
|
}
|
|
|
|
if (isAsyncFunction(value)) {
|
|
|
|
type = `Async${type}`;
|
|
|
|
}
|
|
|
|
let base = `[${type}`;
|
|
|
|
if (constructor === null) {
|
|
|
|
base += " (null prototype)";
|
|
|
|
}
|
|
|
|
if (value.name === "") {
|
|
|
|
base += " (anonymous)";
|
|
|
|
} else {
|
|
|
|
base += `: ${value.name}`;
|
|
|
|
}
|
|
|
|
base += "]";
|
|
|
|
if (constructor !== type && constructor !== null) {
|
|
|
|
base += ` ${constructor}`;
|
|
|
|
}
|
|
|
|
if (tag !== "" && constructor !== tag) {
|
|
|
|
base += ` [${tag}]`;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatRaw(ctx, value, recurseTimes, typedArray, proxyDetails) {
|
|
|
|
let keys;
|
|
|
|
let protoProps;
|
|
|
|
if (ctx.showHidden && (recurseTimes <= ctx.depth || ctx.depth === null)) {
|
|
|
|
protoProps = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const constructor = getConstructorName(value, ctx, recurseTimes, protoProps);
|
|
|
|
// Reset the variable to check for this later on.
|
|
|
|
if (protoProps !== undefined && protoProps.length === 0) {
|
|
|
|
protoProps = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let tag = value[SymbolToStringTag];
|
|
|
|
// Only list the tag in case it's non-enumerable / not an own property.
|
|
|
|
// Otherwise we'd print this twice.
|
|
|
|
if (
|
|
|
|
typeof tag !== "string"
|
|
|
|
// TODO(wafuwafu13): Implement
|
|
|
|
// (tag !== "" &&
|
|
|
|
// (ctx.showHidden
|
|
|
|
// ? Object.prototype.hasOwnProperty
|
|
|
|
// : Object.prototype.propertyIsEnumerable)(
|
|
|
|
// value,
|
|
|
|
// Symbol.toStringTag,
|
|
|
|
// ))
|
|
|
|
) {
|
|
|
|
tag = "";
|
|
|
|
}
|
|
|
|
let base = "";
|
|
|
|
let formatter = () => [];
|
|
|
|
let braces;
|
|
|
|
let noIterator = true;
|
|
|
|
let i = 0;
|
|
|
|
const filter = ctx.showHidden ? 0 : 2;
|
|
|
|
|
|
|
|
let extrasType = kObjectType;
|
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
if (proxyDetails !== null && ctx.showProxy) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return `Proxy ` + formatValue(ctx, proxyDetails, recurseTimes);
|
|
|
|
} else {
|
|
|
|
// Iterators and the rest are split to reduce checks.
|
|
|
|
// We have to check all values in case the constructor is set to null.
|
|
|
|
// Otherwise it would not possible to identify all types properly.
|
|
|
|
if (ReflectHas(value, SymbolIterator) || constructor === null) {
|
|
|
|
noIterator = false;
|
|
|
|
if (ArrayIsArray(value)) {
|
|
|
|
// Only set the constructor for non ordinary ("Array [...]") arrays.
|
|
|
|
const prefix = (constructor !== "Array" || tag !== "")
|
|
|
|
? getPrefix(constructor, tag, "Array", `(${value.length})`)
|
|
|
|
: "";
|
2024-01-03 23:12:38 -05:00
|
|
|
keys = op_get_non_index_property_names(value, filter);
|
2023-04-30 03:24:13 -04:00
|
|
|
braces = [`${prefix}[`, "]"];
|
|
|
|
if (
|
|
|
|
value.length === 0 && keys.length === 0 && protoProps === undefined
|
|
|
|
) {
|
|
|
|
return `${braces[0]}]`;
|
2021-07-26 07:46:48 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
extrasType = kArrayExtrasType;
|
|
|
|
formatter = formatArray;
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isSet(value)) ||
|
|
|
|
(proxyDetails !== null && isSet(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const set = proxyDetails?.[0] ?? value;
|
|
|
|
const size = SetPrototypeGetSize(set);
|
2023-04-30 03:24:13 -04:00
|
|
|
const prefix = getPrefix(constructor, tag, "Set", `(${size})`);
|
2024-01-03 23:12:38 -05:00
|
|
|
keys = getKeys(set, ctx.showHidden);
|
2023-04-30 03:24:13 -04:00
|
|
|
formatter = constructor !== null
|
2024-01-03 23:12:38 -05:00
|
|
|
? FunctionPrototypeBind(formatSet, null, set)
|
|
|
|
: FunctionPrototypeBind(formatSet, null, SetPrototypeValues(set));
|
2023-04-30 03:24:13 -04:00
|
|
|
if (size === 0 && keys.length === 0 && protoProps === undefined) {
|
|
|
|
return `${prefix}{}`;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
braces = [`${prefix}{`, "}"];
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isMap(value)) ||
|
|
|
|
(proxyDetails !== null && isMap(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const map = proxyDetails?.[0] ?? value;
|
|
|
|
const size = MapPrototypeGetSize(map);
|
2023-04-30 03:24:13 -04:00
|
|
|
const prefix = getPrefix(constructor, tag, "Map", `(${size})`);
|
2024-01-03 23:12:38 -05:00
|
|
|
keys = getKeys(map, ctx.showHidden);
|
2023-04-30 03:24:13 -04:00
|
|
|
formatter = constructor !== null
|
2024-01-03 23:12:38 -05:00
|
|
|
? FunctionPrototypeBind(formatMap, null, map)
|
|
|
|
: FunctionPrototypeBind(formatMap, null, MapPrototypeEntries(map));
|
2023-04-30 03:24:13 -04:00
|
|
|
if (size === 0 && keys.length === 0 && protoProps === undefined) {
|
|
|
|
return `${prefix}{}`;
|
|
|
|
}
|
|
|
|
braces = [`${prefix}{`, "}"];
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isTypedArray(value)) ||
|
|
|
|
(proxyDetails !== null && isTypedArray(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const typedArray = proxyDetails?.[0] ?? value;
|
|
|
|
keys = op_get_non_index_property_names(typedArray, filter);
|
|
|
|
const bound = typedArray;
|
2023-04-30 03:24:13 -04:00
|
|
|
const fallback = "";
|
|
|
|
if (constructor === null) {
|
|
|
|
// TODO(wafuwafu13): Implement
|
|
|
|
// fallback = TypedArrayPrototypeGetSymbolToStringTag(value);
|
|
|
|
// // Reconstruct the array information.
|
|
|
|
// bound = new primordials[fallback](value);
|
|
|
|
}
|
2024-01-03 23:12:38 -05:00
|
|
|
const size = TypedArrayPrototypeGetLength(typedArray);
|
2023-04-30 03:24:13 -04:00
|
|
|
const prefix = getPrefix(constructor, tag, fallback, `(${size})`);
|
|
|
|
braces = [`${prefix}[`, "]"];
|
2024-01-03 23:12:38 -05:00
|
|
|
if (typedArray.length === 0 && keys.length === 0 && !ctx.showHidden) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return `${braces[0]}]`;
|
|
|
|
}
|
|
|
|
// Special handle the value. The original value is required below. The
|
|
|
|
// bound function is required to reconstruct missing information.
|
|
|
|
formatter = FunctionPrototypeBind(formatTypedArray, null, bound, size);
|
|
|
|
extrasType = kArrayExtrasType;
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isMapIterator(value)) ||
|
|
|
|
(proxyDetails !== null && isMapIterator(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const mapIterator = proxyDetails?.[0] ?? value;
|
|
|
|
keys = getKeys(mapIterator, ctx.showHidden);
|
2023-04-30 03:24:13 -04:00
|
|
|
braces = getIteratorBraces("Map", tag);
|
|
|
|
// Add braces to the formatter parameters.
|
|
|
|
formatter = FunctionPrototypeBind(formatIterator, null, braces);
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isSetIterator(value)) ||
|
|
|
|
(proxyDetails !== null && isSetIterator(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const setIterator = proxyDetails?.[0] ?? value;
|
|
|
|
keys = getKeys(setIterator, ctx.showHidden);
|
2023-04-30 03:24:13 -04:00
|
|
|
braces = getIteratorBraces("Set", tag);
|
|
|
|
// Add braces to the formatter parameters.
|
|
|
|
formatter = FunctionPrototypeBind(formatIterator, null, braces);
|
|
|
|
} else {
|
|
|
|
noIterator = true;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
if (noIterator) {
|
|
|
|
keys = getKeys(value, ctx.showHidden);
|
|
|
|
braces = ["{", "}"];
|
|
|
|
if (constructor === "Object") {
|
|
|
|
if (isArgumentsObject(value)) {
|
|
|
|
braces[0] = "[Arguments] {";
|
|
|
|
} else if (tag !== "") {
|
|
|
|
braces[0] = `${getPrefix(constructor, tag, "Object")}{`;
|
|
|
|
}
|
|
|
|
if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
return `${braces[0]}}`;
|
|
|
|
}
|
|
|
|
} else if (typeof value === "function") {
|
|
|
|
base = getFunctionBase(value, constructor, tag);
|
|
|
|
if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
return ctx.stylize(base, "special");
|
|
|
|
}
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isRegExp(value)) ||
|
|
|
|
(proxyDetails !== null && isRegExp(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const regExp = proxyDetails?.[0] ?? value;
|
2023-04-30 03:24:13 -04:00
|
|
|
// Make RegExps say that they are RegExps
|
|
|
|
base = RegExpPrototypeToString(
|
2024-01-03 23:12:38 -05:00
|
|
|
constructor !== null ? regExp : new SafeRegExp(regExp),
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
const prefix = getPrefix(constructor, tag, "RegExp");
|
|
|
|
if (prefix !== "RegExp ") {
|
|
|
|
base = `${prefix}${base}`;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
(keys.length === 0 && protoProps === undefined) ||
|
|
|
|
(recurseTimes > ctx.depth && ctx.depth !== null)
|
|
|
|
) {
|
|
|
|
return ctx.stylize(base, "regexp");
|
|
|
|
}
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null && isDate(value)) ||
|
|
|
|
(proxyDetails !== null && isDate(proxyDetails[0]))
|
|
|
|
) {
|
|
|
|
const date = proxyDetails?.[0] ?? value;
|
2023-11-28 10:07:48 -05:00
|
|
|
if (NumberIsNaN(DatePrototypeGetTime(date))) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return ctx.stylize("Invalid Date", "date");
|
|
|
|
} else {
|
|
|
|
base = DatePrototypeToISOString(date);
|
|
|
|
if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
return ctx.stylize(base, "date");
|
|
|
|
}
|
|
|
|
}
|
2024-01-14 19:26:57 -05:00
|
|
|
} else if (
|
|
|
|
proxyDetails === null &&
|
|
|
|
typeof globalThis.Temporal !== "undefined" &&
|
|
|
|
(
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.Instant.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.ZonedDateTime.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.PlainDate.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.PlainTime.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.PlainDateTime.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.PlainYearMonth.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.PlainMonthDay.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.Duration.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.TimeZone.prototype,
|
|
|
|
value,
|
|
|
|
) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(
|
|
|
|
globalThis.Temporal.Calendar.prototype,
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// Temporal is not available in primordials yet
|
|
|
|
// deno-lint-ignore prefer-primordials
|
|
|
|
return ctx.stylize(value.toString(), "temporal");
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (
|
|
|
|
(proxyDetails === null &&
|
|
|
|
(isNativeError(value) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ErrorPrototype, value))) ||
|
|
|
|
(proxyDetails !== null &&
|
|
|
|
(isNativeError(proxyDetails[0]) ||
|
|
|
|
ObjectPrototypeIsPrototypeOf(ErrorPrototype, proxyDetails[0])))
|
|
|
|
) {
|
|
|
|
const error = proxyDetails?.[0] ?? value;
|
|
|
|
base = inspectError(error, ctx);
|
2023-04-30 03:24:13 -04:00
|
|
|
if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
} else if (isAnyArrayBuffer(value)) {
|
|
|
|
// Fast path for ArrayBuffer and SharedArrayBuffer.
|
|
|
|
// Can't do the same for DataView because it has a non-primitive
|
|
|
|
// .buffer property that we need to recurse for.
|
|
|
|
const arrayType = isArrayBuffer(value)
|
|
|
|
? "ArrayBuffer"
|
|
|
|
: "SharedArrayBuffer";
|
|
|
|
|
|
|
|
const prefix = getPrefix(constructor, tag, arrayType);
|
|
|
|
if (typedArray === undefined) {
|
|
|
|
formatter = formatArrayBuffer;
|
|
|
|
} else if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
return prefix +
|
|
|
|
`{ byteLength: ${
|
|
|
|
formatNumber(ctx.stylize, TypedArrayPrototypeGetByteLength(value))
|
|
|
|
} }`;
|
|
|
|
}
|
|
|
|
braces[0] = `${prefix}{`;
|
|
|
|
ArrayPrototypeUnshift(keys, "byteLength");
|
|
|
|
} else if (isDataView(value)) {
|
|
|
|
braces[0] = `${getPrefix(constructor, tag, "DataView")}{`;
|
|
|
|
// .buffer goes last, it's not a primitive like the others.
|
|
|
|
ArrayPrototypeUnshift(keys, "byteLength", "byteOffset", "buffer");
|
|
|
|
} else if (isPromise(value)) {
|
|
|
|
braces[0] = `${getPrefix(constructor, tag, "Promise")}{`;
|
|
|
|
formatter = formatPromise;
|
|
|
|
} else if (isWeakSet(value)) {
|
|
|
|
braces[0] = `${getPrefix(constructor, tag, "WeakSet")}{`;
|
|
|
|
formatter = ctx.showHidden ? formatWeakSet : formatWeakCollection;
|
|
|
|
} else if (isWeakMap(value)) {
|
|
|
|
braces[0] = `${getPrefix(constructor, tag, "WeakMap")}{`;
|
|
|
|
formatter = ctx.showHidden ? formatWeakMap : formatWeakCollection;
|
|
|
|
} else if (isModuleNamespaceObject(value)) {
|
|
|
|
braces[0] = `${getPrefix(constructor, tag, "Module")}{`;
|
|
|
|
// Special handle keys for namespace objects.
|
2023-05-01 09:30:02 -04:00
|
|
|
formatter = FunctionPrototypeBind(formatNamespaceObject, null, keys);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else if (isBoxedPrimitive(value)) {
|
|
|
|
base = getBoxedBase(value, ctx, keys, constructor, tag);
|
|
|
|
if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
return base;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-04-30 03:24:13 -04:00
|
|
|
if (keys.length === 0 && protoProps === undefined) {
|
|
|
|
// TODO(wafuwafu13): Implement
|
|
|
|
// if (isExternal(value)) {
|
|
|
|
// const address = getExternalValue(value).toString(16);
|
|
|
|
// return ctx.stylize(`[External: ${address}]`, 'special');
|
|
|
|
// }
|
|
|
|
return `${getCtxStyle(value, constructor, tag)}{}`;
|
|
|
|
}
|
|
|
|
braces[0] = `${getCtxStyle(value, constructor, tag)}{`;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
if (recurseTimes > ctx.depth && ctx.depth !== null) {
|
|
|
|
let constructorName = StringPrototypeSlice(
|
|
|
|
getCtxStyle(value, constructor, tag),
|
|
|
|
0,
|
|
|
|
-1,
|
|
|
|
);
|
|
|
|
if (constructor !== null) {
|
|
|
|
constructorName = `[${constructorName}]`;
|
|
|
|
}
|
|
|
|
return ctx.stylize(constructorName, "special");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
recurseTimes += 1;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(ctx.seen, value);
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.currentDepth = recurseTimes;
|
|
|
|
let output;
|
|
|
|
try {
|
|
|
|
output = formatter(ctx, value, recurseTimes);
|
|
|
|
for (i = 0; i < keys.length; i++) {
|
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
formatProperty(ctx, value, recurseTimes, keys[i], extrasType),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (protoProps !== undefined) {
|
|
|
|
ArrayPrototypePushApply(output, protoProps);
|
|
|
|
}
|
2023-05-11 09:08:17 -04:00
|
|
|
} catch (error) {
|
|
|
|
// TODO(wafuwafu13): Implement stack overflow check
|
|
|
|
return ctx.stylize(
|
|
|
|
`[Internal Formatting Error] ${error.stack}`,
|
|
|
|
"internalError",
|
2023-04-30 03:24:13 -04:00
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
if (ctx.circular !== undefined) {
|
|
|
|
const index = ctx.circular.get(value);
|
|
|
|
if (index !== undefined) {
|
|
|
|
const reference = ctx.stylize(`<ref *${index}>`, "special");
|
|
|
|
// Add reference always to the very beginning of the output.
|
|
|
|
if (ctx.compact !== true) {
|
|
|
|
base = base === "" ? reference : `${reference} ${base}`;
|
|
|
|
} else {
|
|
|
|
braces[0] = `${reference} ${braces[0]}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePop(ctx.seen);
|
2023-04-30 03:24:13 -04:00
|
|
|
|
|
|
|
if (ctx.sorted) {
|
|
|
|
const comparator = ctx.sorted === true ? undefined : ctx.sorted;
|
|
|
|
if (extrasType === kObjectType) {
|
|
|
|
output = ArrayPrototypeSort(output, comparator);
|
|
|
|
} else if (keys.length > 1) {
|
2023-05-01 09:30:02 -04:00
|
|
|
const sorted = ArrayPrototypeSort(
|
|
|
|
ArrayPrototypeSlice(output, output.length - keys.length),
|
|
|
|
comparator,
|
|
|
|
);
|
|
|
|
ArrayPrototypeSplice(
|
|
|
|
output,
|
2023-04-30 03:24:13 -04:00
|
|
|
output.length - keys.length,
|
|
|
|
keys.length,
|
|
|
|
...new SafeArrayIterator(sorted),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const res = reduceToSingleString(
|
|
|
|
ctx,
|
|
|
|
output,
|
|
|
|
base,
|
|
|
|
braces,
|
|
|
|
extrasType,
|
|
|
|
recurseTimes,
|
|
|
|
value,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
const budget = ctx.budget[ctx.indentationLvl] || 0;
|
|
|
|
const newLength = budget + res.length;
|
|
|
|
ctx.budget[ctx.indentationLvl] = newLength;
|
|
|
|
// If any indentationLvl exceeds this limit, limit further inspecting to the
|
|
|
|
// minimum. Otherwise the recursive algorithm might continue inspecting the
|
|
|
|
// object even though the maximum string size (~2 ** 28 on 32 bit systems and
|
|
|
|
// ~2 ** 30 on 64 bit systems) exceeded. The actual output is not limited at
|
|
|
|
// exactly 2 ** 27 but a bit higher. This depends on the object shape.
|
|
|
|
// This limit also makes sure that huge objects don't block the event loop
|
|
|
|
// significantly.
|
|
|
|
if (newLength > 2 ** 27) {
|
|
|
|
ctx.depth = -1;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const builtInObjectsRegExp = new SafeRegExp("^[A-Z][a-zA-Z0-9]+$");
|
|
|
|
const builtInObjects = new SafeSet(
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeFilter(
|
|
|
|
ObjectGetOwnPropertyNames(globalThis),
|
|
|
|
(e) => RegExpPrototypeTest(builtInObjectsRegExp, e),
|
2023-04-30 03:24:13 -04:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
function addPrototypeProperties(
|
|
|
|
ctx,
|
|
|
|
main,
|
|
|
|
obj,
|
|
|
|
recurseTimes,
|
|
|
|
output,
|
|
|
|
) {
|
|
|
|
let depth = 0;
|
|
|
|
let keys;
|
|
|
|
let keySet;
|
|
|
|
do {
|
|
|
|
if (depth !== 0 || main === obj) {
|
|
|
|
obj = ObjectGetPrototypeOf(obj);
|
|
|
|
// Stop as soon as a null prototype is encountered.
|
|
|
|
if (obj === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Stop as soon as a built-in object type is detected.
|
|
|
|
const descriptor = ObjectGetOwnPropertyDescriptor(obj, "constructor");
|
|
|
|
if (
|
|
|
|
descriptor !== undefined &&
|
|
|
|
typeof descriptor.value === "function" &&
|
2023-05-01 09:30:02 -04:00
|
|
|
SetPrototypeHas(builtInObjects, descriptor.value.name)
|
2023-04-30 03:24:13 -04:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth === 0) {
|
|
|
|
keySet = new SafeSet();
|
|
|
|
} else {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeForEach(keys, (key) => SetPrototypeAdd(keySet, key));
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
// Get all own property names and symbols.
|
|
|
|
keys = ReflectOwnKeys(obj);
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(ctx.seen, main);
|
2023-04-30 03:24:13 -04:00
|
|
|
for (const key of new SafeArrayIterator(keys)) {
|
|
|
|
// Ignore the `constructor` property and keys that exist on layers above.
|
|
|
|
if (
|
|
|
|
key === "constructor" ||
|
2023-05-02 06:15:45 -04:00
|
|
|
ObjectHasOwn(main, key) ||
|
2023-05-01 09:30:02 -04:00
|
|
|
(depth !== 0 && SetPrototypeHas(keySet, key))
|
2023-04-30 03:24:13 -04:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const desc = ObjectGetOwnPropertyDescriptor(obj, key);
|
|
|
|
if (typeof desc.value === "function") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const value = formatProperty(
|
|
|
|
ctx,
|
|
|
|
obj,
|
|
|
|
recurseTimes,
|
|
|
|
key,
|
|
|
|
kObjectType,
|
|
|
|
desc,
|
|
|
|
main,
|
|
|
|
);
|
|
|
|
if (ctx.colors) {
|
|
|
|
// Faint!
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(output, `\u001b[2m${value}\u001b[22m`);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(output, value);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePop(ctx.seen);
|
2023-04-30 03:24:13 -04:00
|
|
|
// Limit the inspection to up to three prototype layers. Using `recurseTimes`
|
|
|
|
// is not a good choice here, because it's as if the properties are declared
|
|
|
|
// on the current object from the users perspective.
|
|
|
|
} while (++depth !== 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
function isInstanceof(proto, object) {
|
|
|
|
try {
|
|
|
|
return ObjectPrototypeIsPrototypeOf(proto, object);
|
|
|
|
} catch {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConstructorName(obj, ctx, recurseTimes, protoProps) {
|
|
|
|
let firstProto;
|
|
|
|
const tmp = obj;
|
|
|
|
while (obj || isUndetectableObject(obj)) {
|
2023-09-19 14:24:19 -04:00
|
|
|
let descriptor;
|
|
|
|
try {
|
|
|
|
descriptor = ObjectGetOwnPropertyDescriptor(obj, "constructor");
|
|
|
|
} catch {
|
|
|
|
/* this could fail */
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (
|
2023-04-30 03:24:13 -04:00
|
|
|
descriptor !== undefined &&
|
|
|
|
typeof descriptor.value === "function" &&
|
|
|
|
descriptor.value.name !== "" &&
|
|
|
|
isInstanceof(descriptor.value.prototype, tmp)
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
2023-04-30 03:24:13 -04:00
|
|
|
if (
|
|
|
|
protoProps !== undefined &&
|
|
|
|
(firstProto !== obj ||
|
2023-05-01 09:30:02 -04:00
|
|
|
!SetPrototypeHas(builtInObjects, descriptor.value.name))
|
2023-04-30 03:24:13 -04:00
|
|
|
) {
|
|
|
|
addPrototypeProperties(
|
|
|
|
ctx,
|
|
|
|
tmp,
|
|
|
|
firstProto || tmp,
|
|
|
|
recurseTimes,
|
|
|
|
protoProps,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return String(descriptor.value.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = ObjectGetPrototypeOf(obj);
|
|
|
|
if (firstProto === undefined) {
|
|
|
|
firstProto = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (firstProto === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
const res = op_get_constructor_name(tmp);
|
2023-04-30 03:24:13 -04:00
|
|
|
|
|
|
|
if (recurseTimes > ctx.depth && ctx.depth !== null) {
|
|
|
|
return `${res} <Complex prototype>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const protoConstr = getConstructorName(
|
|
|
|
firstProto,
|
|
|
|
ctx,
|
|
|
|
recurseTimes + 1,
|
|
|
|
protoProps,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (protoConstr === null) {
|
|
|
|
return `${res} <${
|
|
|
|
inspect(firstProto, {
|
|
|
|
...ctx,
|
|
|
|
customInspect: false,
|
|
|
|
depth: -1,
|
|
|
|
})
|
|
|
|
}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${res} <${protoConstr}>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatPrimitiveRegExp = new SafeRegExp("(?<=\n)");
|
|
|
|
function formatPrimitive(fn, value, ctx) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
let trailer = "";
|
|
|
|
if (value.length > ctx.maxStringLength) {
|
|
|
|
const remaining = value.length - ctx.maxStringLength;
|
2023-05-01 09:30:02 -04:00
|
|
|
value = StringPrototypeSlice(value, 0, ctx.maxStringLength);
|
2023-04-30 03:24:13 -04:00
|
|
|
trailer = `... ${remaining} more character${remaining > 1 ? "s" : ""}`;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
ctx.compact !== true &&
|
|
|
|
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
|
|
|
|
// function.
|
|
|
|
value.length > kMinLineLength &&
|
|
|
|
value.length > ctx.breakLength - ctx.indentationLvl - 4
|
|
|
|
) {
|
2023-05-01 09:30:02 -04:00
|
|
|
return ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeMap(
|
|
|
|
StringPrototypeSplit(value, formatPrimitiveRegExp),
|
|
|
|
(line) => fn(quoteString(line, ctx), "string"),
|
|
|
|
),
|
|
|
|
` +\n${StringPrototypeRepeat(" ", ctx.indentationLvl + 2)}`,
|
|
|
|
) + trailer;
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
return fn(quoteString(value, ctx), "string") + trailer;
|
|
|
|
}
|
|
|
|
if (typeof value === "number") {
|
|
|
|
return formatNumber(fn, value);
|
|
|
|
}
|
|
|
|
if (typeof value === "bigint") {
|
|
|
|
return formatBigInt(fn, value);
|
|
|
|
}
|
|
|
|
if (typeof value === "boolean") {
|
|
|
|
return fn(`${value}`, "boolean");
|
|
|
|
}
|
|
|
|
if (typeof value === "undefined") {
|
|
|
|
return fn("undefined", "undefined");
|
|
|
|
}
|
|
|
|
// es6 symbol primitive
|
|
|
|
return fn(maybeQuoteSymbol(value, ctx), "symbol");
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPrefix(constructor, tag, fallback, size = "") {
|
|
|
|
if (constructor === null) {
|
|
|
|
if (tag !== "" && fallback !== tag) {
|
|
|
|
return `[${fallback}${size}: null prototype] [${tag}] `;
|
|
|
|
}
|
|
|
|
return `[${fallback}${size}: null prototype] `;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag !== "" && constructor !== tag) {
|
|
|
|
return `${constructor}${size} [${tag}] `;
|
|
|
|
}
|
|
|
|
return `${constructor}${size} `;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatArray(ctx, value, recurseTimes) {
|
|
|
|
const valLen = value.length;
|
|
|
|
const len = MathMin(MathMax(0, ctx.maxArrayLength), valLen);
|
|
|
|
|
|
|
|
const remaining = valLen - len;
|
|
|
|
const output = [];
|
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
|
// Special handle sparse arrays.
|
2023-05-02 06:15:45 -04:00
|
|
|
if (!ObjectHasOwn(value, i)) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return formatSpecialArray(ctx, value, recurseTimes, len, output, i);
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
formatProperty(ctx, value, recurseTimes, i, kArrayType),
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
if (remaining > 0) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
`... ${remaining} more item${remaining > 1 ? "s" : ""}`,
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCtxStyle(value, constructor, tag) {
|
|
|
|
let fallback = "";
|
|
|
|
if (constructor === null) {
|
2024-01-03 23:12:38 -05:00
|
|
|
fallback = op_get_constructor_name(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
if (fallback === tag) {
|
|
|
|
fallback = "Object";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getPrefix(constructor, tag, fallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look up the keys of the object.
|
|
|
|
function getKeys(value, showHidden) {
|
|
|
|
let keys;
|
|
|
|
const symbols = ObjectGetOwnPropertySymbols(value);
|
|
|
|
if (showHidden) {
|
|
|
|
keys = ObjectGetOwnPropertyNames(value);
|
|
|
|
if (symbols.length !== 0) {
|
|
|
|
ArrayPrototypePushApply(keys, symbols);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This might throw if `value` is a Module Namespace Object from an
|
|
|
|
// unevaluated module, but we don't want to perform the actual type
|
|
|
|
// check because it's expensive.
|
|
|
|
// TODO(devsnek): track https://github.com/tc39/ecma262/issues/1209
|
|
|
|
// and modify this logic as needed.
|
|
|
|
try {
|
|
|
|
keys = ObjectKeys(value);
|
|
|
|
} catch (err) {
|
|
|
|
assert(
|
|
|
|
isNativeError(err) && err.name === "ReferenceError" &&
|
|
|
|
isModuleNamespaceObject(value),
|
|
|
|
);
|
|
|
|
keys = ObjectGetOwnPropertyNames(value);
|
|
|
|
}
|
|
|
|
if (symbols.length !== 0) {
|
|
|
|
const filter = (key) => ObjectPrototypePropertyIsEnumerable(value, key);
|
|
|
|
ArrayPrototypePushApply(keys, ArrayPrototypeFilter(symbols, filter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatSet(value, ctx, _ignored, recurseTimes) {
|
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
|
|
|
|
const values = [...new SafeSetIterator(value)];
|
|
|
|
const valLen = SetPrototypeGetSize(value);
|
|
|
|
const len = MathMin(MathMax(0, ctx.iterableLimit), valLen);
|
|
|
|
|
|
|
|
const remaining = valLen - len;
|
|
|
|
const output = [];
|
|
|
|
for (let i = 0; i < len; i++) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(output, formatValue(ctx, values[i], recurseTimes));
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
if (remaining > 0) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
`... ${remaining} more item${remaining > 1 ? "s" : ""}`,
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2023-06-26 09:10:27 -04:00
|
|
|
function formatMap(value, ctx, _ignored, recurseTimes) {
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
|
|
|
|
const values = [...new SafeMapIterator(value)];
|
|
|
|
const valLen = MapPrototypeGetSize(value);
|
|
|
|
const len = MathMin(MathMax(0, ctx.iterableLimit), valLen);
|
|
|
|
|
|
|
|
const remaining = valLen - len;
|
|
|
|
const output = [];
|
|
|
|
for (let i = 0; i < len; i++) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
2023-04-30 03:24:13 -04:00
|
|
|
`${formatValue(ctx, values[i][0], recurseTimes)} => ${
|
|
|
|
formatValue(ctx, values[i][1], recurseTimes)
|
|
|
|
}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (remaining > 0) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
`... ${remaining} more item${remaining > 1 ? "s" : ""}`,
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTypedArray(
|
|
|
|
value,
|
|
|
|
length,
|
|
|
|
ctx,
|
|
|
|
_ignored,
|
|
|
|
recurseTimes,
|
|
|
|
) {
|
|
|
|
const maxLength = MathMin(MathMax(0, ctx.maxArrayLength), length);
|
|
|
|
const remaining = value.length - maxLength;
|
2023-12-19 01:05:49 -05:00
|
|
|
const output = [];
|
2023-04-30 03:24:13 -04:00
|
|
|
const elementFormatter = value.length > 0 && typeof value[0] === "number"
|
|
|
|
? formatNumber
|
|
|
|
: formatBigInt;
|
|
|
|
for (let i = 0; i < maxLength; ++i) {
|
|
|
|
output[i] = elementFormatter(ctx.stylize, value[i]);
|
|
|
|
}
|
|
|
|
if (remaining > 0) {
|
|
|
|
output[maxLength] = `... ${remaining} more item${remaining > 1 ? "s" : ""}`;
|
|
|
|
}
|
|
|
|
if (ctx.showHidden) {
|
|
|
|
// .buffer goes last, it's not a primitive like the others.
|
|
|
|
// All besides `BYTES_PER_ELEMENT` are actually getters.
|
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
for (
|
|
|
|
const key of new SafeArrayIterator([
|
|
|
|
"BYTES_PER_ELEMENT",
|
|
|
|
"length",
|
|
|
|
"byteLength",
|
|
|
|
"byteOffset",
|
|
|
|
"buffer",
|
|
|
|
])
|
|
|
|
) {
|
|
|
|
const str = formatValue(ctx, value[key], recurseTimes, true);
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(output, `[${key}]: ${str}`);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIteratorBraces(type, tag) {
|
|
|
|
if (tag !== `${type} Iterator`) {
|
|
|
|
if (tag !== "") {
|
|
|
|
tag += "] [";
|
|
|
|
}
|
|
|
|
tag += `${type} Iterator`;
|
|
|
|
}
|
|
|
|
return [`[${tag}] {`, "}"];
|
|
|
|
}
|
|
|
|
|
|
|
|
const iteratorRegExp = new SafeRegExp(" Iterator] {$");
|
|
|
|
function formatIterator(braces, ctx, value, recurseTimes) {
|
2024-01-03 23:12:38 -05:00
|
|
|
const { 0: entries, 1: isKeyValue } = op_preview_entries(value, true);
|
2023-04-30 03:24:13 -04:00
|
|
|
if (isKeyValue) {
|
|
|
|
// Mark entry iterators as such.
|
2023-05-01 09:30:02 -04:00
|
|
|
braces[0] = StringPrototypeReplace(
|
|
|
|
braces[0],
|
|
|
|
iteratorRegExp,
|
|
|
|
" Entries] {",
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
return formatMapIterInner(ctx, recurseTimes, entries, kMapEntries);
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatSetIterInner(ctx, recurseTimes, entries, kIterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCircular(value, ctx) {
|
|
|
|
let index = 1;
|
|
|
|
if (ctx.circular === undefined) {
|
|
|
|
ctx.circular = new SafeMap();
|
|
|
|
MapPrototypeSet(ctx.circular, value, index);
|
|
|
|
} else {
|
|
|
|
index = MapPrototypeGet(ctx.circular, value);
|
|
|
|
if (index === undefined) {
|
|
|
|
index = MapPrototypeGetSize(ctx.circular) + 1;
|
|
|
|
MapPrototypeSet(ctx.circular, value, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Circular string is cyan
|
|
|
|
return ctx.stylize(`[Circular *${index}]`, "special");
|
|
|
|
}
|
|
|
|
|
|
|
|
const AGGREGATE_ERROR_HAS_AT_PATTERN = new SafeRegExp(/\s+at/);
|
|
|
|
const AGGREGATE_ERROR_NOT_EMPTY_LINE_PATTERN = new SafeRegExp(/^(?!\s*$)/gm);
|
|
|
|
|
|
|
|
function inspectError(value, ctx) {
|
|
|
|
const causes = [value];
|
|
|
|
|
|
|
|
let err = value;
|
|
|
|
while (err.cause) {
|
|
|
|
if (ArrayPrototypeIncludes(causes, err.cause)) {
|
|
|
|
ArrayPrototypePush(causes, handleCircular(err.cause, ctx));
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ArrayPrototypePush(causes, err.cause);
|
|
|
|
err = err.cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const refMap = new SafeMap();
|
|
|
|
for (let i = 0; i < causes.length; ++i) {
|
|
|
|
const cause = causes[i];
|
|
|
|
if (ctx.circular !== undefined) {
|
|
|
|
const index = MapPrototypeGet(ctx.circular, cause);
|
|
|
|
if (index !== undefined) {
|
|
|
|
MapPrototypeSet(
|
|
|
|
refMap,
|
|
|
|
cause,
|
|
|
|
ctx.stylize(`<ref *${index}> `, "special"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArrayPrototypeShift(causes);
|
|
|
|
|
|
|
|
let finalMessage = MapPrototypeGet(refMap, value) ?? "";
|
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isAggregateError(value)) {
|
2023-04-30 03:24:13 -04:00
|
|
|
const stackLines = StringPrototypeSplit(value.stack, "\n");
|
|
|
|
while (true) {
|
|
|
|
const line = ArrayPrototypeShift(stackLines);
|
|
|
|
if (RegExpPrototypeTest(AGGREGATE_ERROR_HAS_AT_PATTERN, line)) {
|
|
|
|
ArrayPrototypeUnshift(stackLines, line);
|
|
|
|
break;
|
|
|
|
} else if (typeof line === "undefined") {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
finalMessage += line;
|
|
|
|
finalMessage += "\n";
|
|
|
|
}
|
|
|
|
const aggregateMessage = ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeMap(
|
|
|
|
value.errors,
|
|
|
|
(error) =>
|
|
|
|
StringPrototypeReplace(
|
|
|
|
inspectArgs([error]),
|
|
|
|
AGGREGATE_ERROR_NOT_EMPTY_LINE_PATTERN,
|
|
|
|
StringPrototypeRepeat(" ", 4),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
"\n",
|
|
|
|
);
|
|
|
|
finalMessage += aggregateMessage;
|
|
|
|
finalMessage += "\n";
|
|
|
|
finalMessage += ArrayPrototypeJoin(stackLines, "\n");
|
|
|
|
} else {
|
|
|
|
const stack = value.stack;
|
|
|
|
if (stack?.includes("\n at")) {
|
|
|
|
finalMessage += stack;
|
|
|
|
} else {
|
2023-06-05 15:57:01 -04:00
|
|
|
finalMessage += `[${stack || ErrorPrototypeToString(value)}]`;
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finalMessage += ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeMap(
|
|
|
|
causes,
|
|
|
|
(cause) =>
|
|
|
|
"\nCaused by " + (MapPrototypeGet(refMap, cause) ?? "") +
|
|
|
|
(cause?.stack ?? cause),
|
|
|
|
),
|
|
|
|
"",
|
|
|
|
);
|
|
|
|
|
|
|
|
return finalMessage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hexSliceLookupTable = function () {
|
|
|
|
const alphabet = "0123456789abcdef";
|
2023-12-19 01:05:49 -05:00
|
|
|
const table = [];
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let i = 0; i < 16; ++i) {
|
|
|
|
const i16 = i * 16;
|
|
|
|
for (let j = 0; j < 16; ++j) {
|
|
|
|
table[i16 + j] = alphabet[i] + alphabet[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return table;
|
|
|
|
}();
|
|
|
|
|
|
|
|
function hexSlice(buf, start, end) {
|
2023-06-06 05:06:00 -04:00
|
|
|
const len = TypedArrayPrototypeGetLength(buf);
|
2023-04-30 03:24:13 -04:00
|
|
|
if (!start || start < 0) {
|
|
|
|
start = 0;
|
|
|
|
}
|
|
|
|
if (!end || end < 0 || end > len) {
|
|
|
|
end = len;
|
|
|
|
}
|
|
|
|
let out = "";
|
|
|
|
for (let i = start; i < end; ++i) {
|
|
|
|
out += hexSliceLookupTable[buf[i]];
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
const arrayBufferRegExp = new SafeRegExp("(.{2})", "g");
|
|
|
|
function formatArrayBuffer(ctx, value) {
|
2023-06-06 05:06:00 -04:00
|
|
|
let valLen;
|
|
|
|
try {
|
|
|
|
valLen = ArrayBufferPrototypeGetByteLength(value);
|
|
|
|
} catch {
|
|
|
|
valLen = getSharedArrayBufferByteLength(value);
|
|
|
|
}
|
|
|
|
const len = MathMin(MathMax(0, ctx.maxArrayLength), valLen);
|
2023-04-30 03:24:13 -04:00
|
|
|
let buffer;
|
|
|
|
try {
|
2023-06-06 05:06:00 -04:00
|
|
|
buffer = new Uint8Array(value, 0, len);
|
2023-04-30 03:24:13 -04:00
|
|
|
} catch {
|
|
|
|
return [ctx.stylize("(detached)", "special")];
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
let str = StringPrototypeTrim(
|
2023-06-06 05:06:00 -04:00
|
|
|
StringPrototypeReplace(hexSlice(buffer), arrayBufferRegExp, "$1 "),
|
2023-05-01 09:30:02 -04:00
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
|
2023-06-06 05:06:00 -04:00
|
|
|
const remaining = valLen - len;
|
2023-04-30 03:24:13 -04:00
|
|
|
if (remaining > 0) {
|
|
|
|
str += ` ... ${remaining} more byte${remaining > 1 ? "s" : ""}`;
|
|
|
|
}
|
|
|
|
return [`${ctx.stylize("[Uint8Contents]", "special")}: <${str}>`];
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatNumber(fn, value) {
|
|
|
|
// Format -0 as '-0'. Checking `value === -0` won't distinguish 0 from -0.
|
|
|
|
return fn(ObjectIs(value, -0) ? "-0" : `${value}`, "number");
|
|
|
|
}
|
|
|
|
|
|
|
|
const PromiseState = {
|
|
|
|
Pending: 0,
|
|
|
|
Fulfilled: 1,
|
|
|
|
Rejected: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
function formatPromise(ctx, value, recurseTimes) {
|
|
|
|
let output;
|
2023-12-19 01:05:49 -05:00
|
|
|
const { 0: state, 1: result } = core.getPromiseDetails(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
if (state === PromiseState.Pending) {
|
|
|
|
output = [ctx.stylize("<pending>", "special")];
|
|
|
|
} else {
|
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
const str = formatValue(ctx, result, recurseTimes);
|
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
output = [
|
|
|
|
state === PromiseState.Rejected
|
|
|
|
? `${ctx.stylize("<rejected>", "special")} ${str}`
|
|
|
|
: str,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatWeakCollection(ctx) {
|
|
|
|
return [ctx.stylize("<items unknown>", "special")];
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatWeakSet(ctx, value, recurseTimes) {
|
2024-01-03 23:12:38 -05:00
|
|
|
const entries = op_preview_entries(value, false);
|
2023-04-30 03:24:13 -04:00
|
|
|
return formatSetIterInner(ctx, recurseTimes, entries, kWeak);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatWeakMap(ctx, value, recurseTimes) {
|
2024-01-03 23:12:38 -05:00
|
|
|
const entries = op_preview_entries(value, false);
|
2023-04-30 03:24:13 -04:00
|
|
|
return formatMapIterInner(ctx, recurseTimes, entries, kWeak);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatProperty(
|
|
|
|
ctx,
|
|
|
|
value,
|
|
|
|
recurseTimes,
|
|
|
|
key,
|
|
|
|
type,
|
|
|
|
desc,
|
|
|
|
original = value,
|
|
|
|
) {
|
|
|
|
let name, str;
|
|
|
|
let extra = " ";
|
|
|
|
desc = desc || ObjectGetOwnPropertyDescriptor(value, key) ||
|
|
|
|
{ value: value[key], enumerable: true };
|
|
|
|
if (desc.value !== undefined) {
|
|
|
|
const diff = (ctx.compact !== true || type !== kObjectType) ? 2 : 3;
|
|
|
|
ctx.indentationLvl += diff;
|
|
|
|
str = formatValue(ctx, desc.value, recurseTimes);
|
|
|
|
if (diff === 3 && ctx.breakLength < getStringWidth(str, ctx.colors)) {
|
2023-05-01 09:30:02 -04:00
|
|
|
extra = `\n${StringPrototypeRepeat(" ", ctx.indentationLvl)}`;
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
ctx.indentationLvl -= diff;
|
|
|
|
} else if (desc.get !== undefined) {
|
|
|
|
const label = desc.set !== undefined ? "Getter/Setter" : "Getter";
|
|
|
|
const s = ctx.stylize;
|
|
|
|
const sp = "special";
|
|
|
|
if (
|
|
|
|
ctx.getters && (ctx.getters === true ||
|
|
|
|
(ctx.getters === "get" && desc.set === undefined) ||
|
|
|
|
(ctx.getters === "set" && desc.set !== undefined))
|
|
|
|
) {
|
|
|
|
try {
|
2023-05-01 09:30:02 -04:00
|
|
|
const tmp = FunctionPrototypeCall(desc.get, original);
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
if (tmp === null) {
|
|
|
|
str = `${s(`[${label}:`, sp)} ${s("null", "null")}${s("]", sp)}`;
|
|
|
|
} else if (typeof tmp === "object") {
|
|
|
|
str = `${s(`[${label}]`, sp)} ${formatValue(ctx, tmp, recurseTimes)}`;
|
|
|
|
} else {
|
|
|
|
const primitive = formatPrimitive(s, tmp, ctx);
|
|
|
|
str = `${s(`[${label}:`, sp)} ${primitive}${s("]", sp)}`;
|
|
|
|
}
|
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
} catch (err) {
|
|
|
|
const message = `<Inspection threw (${err.message})>`;
|
|
|
|
str = `${s(`[${label}:`, sp)} ${message}${s("]", sp)}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
str = ctx.stylize(`[${label}]`, sp);
|
|
|
|
}
|
|
|
|
} else if (desc.set !== undefined) {
|
|
|
|
str = ctx.stylize("[Setter]", "special");
|
|
|
|
} else {
|
|
|
|
str = ctx.stylize("undefined", "undefined");
|
|
|
|
}
|
|
|
|
if (type === kArrayType) {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
if (typeof key === "symbol") {
|
|
|
|
name = `[${ctx.stylize(maybeQuoteSymbol(key, ctx), "symbol")}]`;
|
|
|
|
} else if (key === "__proto__") {
|
|
|
|
name = "['__proto__']";
|
|
|
|
} else if (desc.enumerable === false) {
|
2023-05-01 09:30:02 -04:00
|
|
|
const tmp = StringPrototypeReplace(
|
|
|
|
key,
|
|
|
|
strEscapeSequencesReplacer,
|
|
|
|
escapeFn,
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
|
|
|
|
name = `[${tmp}]`;
|
|
|
|
} else if (keyStrRegExp.test(key)) {
|
|
|
|
name = ctx.stylize(key, "name");
|
|
|
|
} else {
|
|
|
|
name = ctx.stylize(quoteString(key, ctx), "string");
|
|
|
|
}
|
|
|
|
return `${name}:${extra}${str}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const colorRegExp = new SafeRegExp("\u001b\\[\\d\\d?m", "g");
|
|
|
|
function removeColors(str) {
|
2023-05-01 09:30:02 -04:00
|
|
|
return StringPrototypeReplace(str, colorRegExp, "");
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function isBelowBreakLength(ctx, output, start, base) {
|
|
|
|
// Each entry is separated by at least a comma. Thus, we start with a total
|
|
|
|
// length of at least `output.length`. In addition, some cases have a
|
|
|
|
// whitespace in-between each other that is added to the total as well.
|
|
|
|
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
|
|
|
|
// function. Check the performance overhead and make it an opt-in in case it's
|
|
|
|
// significant.
|
|
|
|
let totalLength = output.length + start;
|
|
|
|
if (totalLength + output.length > ctx.breakLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < output.length; i++) {
|
|
|
|
if (ctx.colors) {
|
|
|
|
totalLength += removeColors(output[i]).length;
|
|
|
|
} else {
|
|
|
|
totalLength += output[i].length;
|
|
|
|
}
|
|
|
|
if (totalLength > ctx.breakLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Do not line up properties on the same line if `base` contains line breaks.
|
|
|
|
return base === "" || !StringPrototypeIncludes(base, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatBigInt(fn, value) {
|
|
|
|
return fn(`${value}n`, "bigint");
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatNamespaceObject(
|
|
|
|
keys,
|
|
|
|
ctx,
|
|
|
|
value,
|
|
|
|
recurseTimes,
|
|
|
|
) {
|
2023-12-19 01:05:49 -05:00
|
|
|
const output = [];
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
try {
|
|
|
|
output[i] = formatProperty(
|
|
|
|
ctx,
|
|
|
|
value,
|
|
|
|
recurseTimes,
|
|
|
|
keys[i],
|
|
|
|
kObjectType,
|
|
|
|
);
|
|
|
|
} catch (_err) {
|
|
|
|
// TODO(wafuwfu13): Implement
|
|
|
|
// assert(isNativeError(err) && err.name === 'ReferenceError');
|
|
|
|
// Use the existing functionality. This makes sure the indentation and
|
|
|
|
// line breaks are always correct. Otherwise it is very difficult to keep
|
|
|
|
// this aligned, even though this is a hacky way of dealing with this.
|
|
|
|
const tmp = { [keys[i]]: "" };
|
|
|
|
output[i] = formatProperty(ctx, tmp, recurseTimes, keys[i], kObjectType);
|
2023-05-01 09:30:02 -04:00
|
|
|
const pos = StringPrototypeLastIndexOf(output[i], " ");
|
2023-04-30 03:24:13 -04:00
|
|
|
// We have to find the last whitespace and have to replace that value as
|
|
|
|
// it will be visualized as a regular string.
|
2023-05-01 09:30:02 -04:00
|
|
|
output[i] = StringPrototypeSlice(output[i], 0, pos + 1) +
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.stylize("<uninitialized>", "special");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Reset the keys to an empty array. This prevents duplicated inspection.
|
|
|
|
keys.length = 0;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The array is sparse and/or has extra keys
|
|
|
|
function formatSpecialArray(
|
|
|
|
ctx,
|
|
|
|
value,
|
|
|
|
recurseTimes,
|
|
|
|
maxLength,
|
|
|
|
output,
|
|
|
|
i,
|
|
|
|
) {
|
|
|
|
const keys = ObjectKeys(value);
|
|
|
|
let index = i;
|
|
|
|
for (; i < keys.length && output.length < maxLength; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
const tmp = +key;
|
|
|
|
// Arrays can only have up to 2^32 - 1 entries
|
|
|
|
if (tmp > 2 ** 32 - 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (`${index}` !== key) {
|
|
|
|
if (!numberRegExp.test(key)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const emptyItems = tmp - index;
|
|
|
|
const ending = emptyItems > 1 ? "s" : "";
|
|
|
|
const message = `<${emptyItems} empty item${ending}>`;
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(output, ctx.stylize(message, "undefined"));
|
2023-04-30 03:24:13 -04:00
|
|
|
index = tmp;
|
|
|
|
if (output.length === maxLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
formatProperty(ctx, value, recurseTimes, key, kArrayType),
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
const remaining = value.length - index;
|
|
|
|
if (output.length !== maxLength) {
|
|
|
|
if (remaining > 0) {
|
|
|
|
const ending = remaining > 1 ? "s" : "";
|
|
|
|
const message = `<${remaining} empty item${ending}>`;
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(output, ctx.stylize(message, "undefined"));
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
} else if (remaining > 0) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
`... ${remaining} more item${remaining > 1 ? "s" : ""}`,
|
|
|
|
);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBoxedBase(
|
|
|
|
value,
|
|
|
|
ctx,
|
|
|
|
keys,
|
|
|
|
constructor,
|
|
|
|
tag,
|
|
|
|
) {
|
2023-05-01 09:30:02 -04:00
|
|
|
let type, primitive;
|
2023-04-30 03:24:13 -04:00
|
|
|
if (isNumberObject(value)) {
|
|
|
|
type = "Number";
|
2023-05-01 09:30:02 -04:00
|
|
|
primitive = NumberPrototypeValueOf(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else if (isStringObject(value)) {
|
|
|
|
type = "String";
|
2023-05-01 09:30:02 -04:00
|
|
|
primitive = StringPrototypeValueOf(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
// For boxed Strings, we have to remove the 0-n indexed entries,
|
|
|
|
// since they just noisy up the output and are redundant
|
|
|
|
// Make boxed primitive Strings look like such
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeSplice(keys, 0, value.length);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else if (isBooleanObject(value)) {
|
|
|
|
type = "Boolean";
|
2023-05-01 09:30:02 -04:00
|
|
|
primitive = BooleanPrototypeValueOf(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else if (isBigIntObject(value)) {
|
|
|
|
type = "BigInt";
|
2023-05-01 09:30:02 -04:00
|
|
|
primitive = BigIntPrototypeValueOf(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
} else {
|
|
|
|
type = "Symbol";
|
2023-05-01 09:30:02 -04:00
|
|
|
primitive = SymbolPrototypeValueOf(value);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
let base = `[${type}`;
|
|
|
|
if (type !== constructor) {
|
|
|
|
if (constructor === null) {
|
|
|
|
base += " (null prototype)";
|
|
|
|
} else {
|
|
|
|
base += ` (${constructor})`;
|
|
|
|
}
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
base += `: ${formatPrimitive(stylizeNoColor, primitive, ctx)}]`;
|
2023-04-30 03:24:13 -04:00
|
|
|
if (tag !== "" && tag !== constructor) {
|
|
|
|
base += ` [${tag}]`;
|
|
|
|
}
|
|
|
|
if (keys.length !== 0 || ctx.stylize === stylizeNoColor) {
|
|
|
|
return base;
|
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
return ctx.stylize(base, StringPrototypeToLowerCase(type));
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function reduceToSingleString(
|
|
|
|
ctx,
|
|
|
|
output,
|
|
|
|
base,
|
|
|
|
braces,
|
|
|
|
extrasType,
|
|
|
|
recurseTimes,
|
|
|
|
value,
|
|
|
|
) {
|
|
|
|
if (ctx.compact !== true) {
|
|
|
|
if (typeof ctx.compact === "number" && ctx.compact >= 1) {
|
|
|
|
// Memorize the original output length. In case the output is grouped,
|
|
|
|
// prevent lining up the entries on a single line.
|
|
|
|
const entries = output.length;
|
|
|
|
// Group array elements together if the array contains at least six
|
|
|
|
// separate entries.
|
|
|
|
if (extrasType === kArrayExtrasType && entries > 6) {
|
|
|
|
output = groupArrayElements(ctx, output, value);
|
|
|
|
}
|
|
|
|
// `ctx.currentDepth` is set to the most inner depth of the currently
|
|
|
|
// inspected object part while `recurseTimes` is the actual current depth
|
|
|
|
// that is inspected.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// const a = { first: [ 1, 2, 3 ], second: { inner: [ 1, 2, 3 ] } }
|
|
|
|
//
|
|
|
|
// The deepest depth of `a` is 2 (a.second.inner) and `a.first` has a max
|
|
|
|
// depth of 1.
|
|
|
|
//
|
|
|
|
// Consolidate all entries of the local most inner depth up to
|
|
|
|
// `ctx.compact`, as long as the properties are smaller than
|
|
|
|
// `ctx.breakLength`.
|
|
|
|
if (
|
|
|
|
ctx.currentDepth - recurseTimes < ctx.compact &&
|
|
|
|
entries === output.length
|
|
|
|
) {
|
|
|
|
// Line up all entries on a single line in case the entries do not
|
|
|
|
// exceed `breakLength`. Add 10 as constant to start next to all other
|
|
|
|
// factors that may reduce `breakLength`.
|
|
|
|
const start = output.length + ctx.indentationLvl +
|
|
|
|
braces[0].length + base.length + 10;
|
|
|
|
if (isBelowBreakLength(ctx, output, start, base)) {
|
|
|
|
const joinedOutput = ArrayPrototypeJoin(output, ", ");
|
|
|
|
if (!StringPrototypeIncludes(joinedOutput, "\n")) {
|
|
|
|
return `${base ? `${base} ` : ""}${braces[0]} ${joinedOutput}` +
|
|
|
|
` ${braces[1]}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
// Line up each entry on an individual line.
|
|
|
|
const indentation = `\n${StringPrototypeRepeat(" ", ctx.indentationLvl)}`;
|
|
|
|
return `${base ? `${base} ` : ""}${braces[0]}${indentation} ` +
|
|
|
|
`${ArrayPrototypeJoin(output, `,${indentation} `)}${
|
|
|
|
ctx.trailingComma ? "," : ""
|
|
|
|
}${indentation}${braces[1]}`;
|
|
|
|
}
|
|
|
|
// Line up all entries on a single line in case the entries do not exceed
|
|
|
|
// `breakLength`.
|
|
|
|
if (isBelowBreakLength(ctx, output, 0, base)) {
|
|
|
|
return `${braces[0]}${base ? ` ${base}` : ""} ${
|
|
|
|
ArrayPrototypeJoin(output, ", ")
|
|
|
|
} ` +
|
|
|
|
braces[1];
|
|
|
|
}
|
|
|
|
const indentation = StringPrototypeRepeat(" ", ctx.indentationLvl);
|
|
|
|
// If the opening "brace" is too large, like in the case of "Set {",
|
|
|
|
// we need to force the first item to be on the next line or the
|
|
|
|
// items will not line up correctly.
|
|
|
|
const ln = base === "" && braces[0].length === 1
|
|
|
|
? " "
|
|
|
|
: `${base ? ` ${base}` : ""}\n${indentation} `;
|
|
|
|
// Line up each entry on an individual line.
|
|
|
|
return `${braces[0]}${ln}${
|
|
|
|
ArrayPrototypeJoin(output, `,\n${indentation} `)
|
|
|
|
} ${braces[1]}`;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function groupArrayElements(ctx, output, value) {
|
2023-02-07 14:22:46 -05:00
|
|
|
let totalLength = 0;
|
|
|
|
let maxLength = 0;
|
2023-04-30 03:24:13 -04:00
|
|
|
let i = 0;
|
|
|
|
let outputLength = output.length;
|
|
|
|
if (ctx.maxArrayLength < output.length) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// This makes sure the "... n more items" part is not taken into account.
|
2023-04-30 03:24:13 -04:00
|
|
|
outputLength--;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
const separatorSpace = 2; // Add 1 for the space and 1 for the separator.
|
2023-12-19 01:05:49 -05:00
|
|
|
const dataLen = [];
|
2023-02-07 14:22:46 -05:00
|
|
|
// Calculate the total length of all output entries and the individual max
|
2023-04-30 03:24:13 -04:00
|
|
|
// entries length of all output entries. We have to remove colors first,
|
|
|
|
// otherwise the length would not be calculated properly.
|
|
|
|
for (; i < outputLength; i++) {
|
|
|
|
const len = getStringWidth(output[i], ctx.colors);
|
2023-02-07 14:22:46 -05:00
|
|
|
dataLen[i] = len;
|
|
|
|
totalLength += len + separatorSpace;
|
2023-04-30 03:24:13 -04:00
|
|
|
if (maxLength < len) {
|
|
|
|
maxLength = len;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
// Add two to `maxLength` as we add a single whitespace character plus a comma
|
|
|
|
// in-between two entries.
|
|
|
|
const actualMax = maxLength + separatorSpace;
|
|
|
|
// Check if at least three entries fit next to each other and prevent grouping
|
|
|
|
// of arrays that contains entries of very different length (i.e., if a single
|
|
|
|
// entry is longer than 1/5 of all other entries combined). Otherwise the
|
|
|
|
// space in-between small entries would be enormous.
|
|
|
|
if (
|
2023-04-30 03:24:13 -04:00
|
|
|
actualMax * 3 + ctx.indentationLvl < ctx.breakLength &&
|
2023-02-07 14:22:46 -05:00
|
|
|
(totalLength / actualMax > 5 || maxLength <= 6)
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const approxCharHeights = 2.5;
|
2023-04-30 03:24:13 -04:00
|
|
|
const averageBias = MathSqrt(actualMax - totalLength / output.length);
|
2023-02-07 14:22:46 -05:00
|
|
|
const biasedMax = MathMax(actualMax - 3 - averageBias, 1);
|
|
|
|
// Dynamically check how many columns seem possible.
|
|
|
|
const columns = MathMin(
|
|
|
|
// Ideally a square should be drawn. We expect a character to be about 2.5
|
|
|
|
// times as high as wide. This is the area formula to calculate a square
|
|
|
|
// which contains n rectangles of size `actualMax * approxCharHeights`.
|
|
|
|
// Divide that by `actualMax` to receive the correct number of columns.
|
|
|
|
// The added bias increases the columns for short entries.
|
|
|
|
MathRound(
|
2023-04-30 03:24:13 -04:00
|
|
|
MathSqrt(
|
|
|
|
approxCharHeights * biasedMax * outputLength,
|
|
|
|
) / biasedMax,
|
2023-02-07 14:22:46 -05:00
|
|
|
),
|
|
|
|
// Do not exceed the breakLength.
|
2023-04-30 03:24:13 -04:00
|
|
|
MathFloor((ctx.breakLength - ctx.indentationLvl) / actualMax),
|
|
|
|
// Limit array grouping for small `compact` modes as the user requested
|
|
|
|
// minimal grouping.
|
|
|
|
ctx.compact * 4,
|
2023-02-07 14:22:46 -05:00
|
|
|
// Limit the columns to a maximum of fifteen.
|
|
|
|
15,
|
|
|
|
);
|
|
|
|
// Return with the original output if no grouping should happen.
|
|
|
|
if (columns <= 1) {
|
2023-04-30 03:24:13 -04:00
|
|
|
return output;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
const tmp = [];
|
|
|
|
const maxLineLength = [];
|
|
|
|
for (let i = 0; i < columns; i++) {
|
|
|
|
let lineMaxLength = 0;
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let j = i; j < output.length; j += columns) {
|
|
|
|
if (dataLen[j] > lineMaxLength) {
|
|
|
|
lineMaxLength = dataLen[j];
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
lineMaxLength += separatorSpace;
|
|
|
|
maxLineLength[i] = lineMaxLength;
|
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
let order = StringPrototypePadStart;
|
2023-02-07 14:22:46 -05:00
|
|
|
if (value !== undefined) {
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let i = 0; i < output.length; i++) {
|
|
|
|
if (typeof value[i] !== "number" && typeof value[i] !== "bigint") {
|
|
|
|
order = StringPrototypePadEnd;
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
// Each iteration creates a single line of grouped entries.
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let i = 0; i < outputLength; i += columns) {
|
2023-02-07 14:22:46 -05:00
|
|
|
// The last lines may contain less entries than columns.
|
2023-04-30 03:24:13 -04:00
|
|
|
const max = MathMin(i + columns, outputLength);
|
2023-02-07 14:22:46 -05:00
|
|
|
let str = "";
|
|
|
|
let j = i;
|
|
|
|
for (; j < max - 1; j++) {
|
2023-04-30 03:24:13 -04:00
|
|
|
// Calculate extra color padding in case it's active. This has to be
|
|
|
|
// done line by line as some lines might contain more colors than
|
|
|
|
// others.
|
|
|
|
const padding = maxLineLength[j - i] + output[j].length - dataLen[j];
|
|
|
|
str += order(`${output[j]}, `, padding, " ");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
if (order === StringPrototypePadStart) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const padding = maxLineLength[j - i] +
|
2023-04-30 03:24:13 -04:00
|
|
|
output[j].length -
|
|
|
|
dataLen[j] -
|
2023-02-07 14:22:46 -05:00
|
|
|
separatorSpace;
|
2023-04-30 03:24:13 -04:00
|
|
|
str += StringPrototypePadStart(output[j], padding, " ");
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-04-30 03:24:13 -04:00
|
|
|
str += output[j];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(tmp, str);
|
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
if (ctx.maxArrayLength < output.length) {
|
|
|
|
ArrayPrototypePush(tmp, output[outputLength]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
output = tmp;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
return output;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function formatMapIterInner(
|
|
|
|
ctx,
|
|
|
|
recurseTimes,
|
|
|
|
entries,
|
|
|
|
state,
|
|
|
|
) {
|
|
|
|
const maxArrayLength = MathMax(ctx.maxArrayLength, 0);
|
|
|
|
// Entries exist as [key1, val1, key2, val2, ...]
|
|
|
|
const len = entries.length / 2;
|
|
|
|
const remaining = len - maxArrayLength;
|
|
|
|
const maxLength = MathMin(maxArrayLength, len);
|
2023-12-19 01:05:49 -05:00
|
|
|
const output = [];
|
2023-04-30 03:24:13 -04:00
|
|
|
let i = 0;
|
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
if (state === kWeak) {
|
|
|
|
for (; i < maxLength; i++) {
|
|
|
|
const pos = i * 2;
|
|
|
|
output[i] = `${formatValue(ctx, entries[pos], recurseTimes)} => ${
|
|
|
|
formatValue(ctx, entries[pos + 1], recurseTimes)
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
// Sort all entries to have a halfway reliable output (if more entries than
|
|
|
|
// retrieved ones exist, we can not reliably return the same output) if the
|
|
|
|
// output is not sorted anyway.
|
|
|
|
if (!ctx.sorted) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeSort(output);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-04-30 03:24:13 -04:00
|
|
|
for (; i < maxLength; i++) {
|
|
|
|
const pos = i * 2;
|
|
|
|
const res = [
|
|
|
|
formatValue(ctx, entries[pos], recurseTimes),
|
|
|
|
formatValue(ctx, entries[pos + 1], recurseTimes),
|
|
|
|
];
|
|
|
|
output[i] = reduceToSingleString(
|
|
|
|
ctx,
|
|
|
|
res,
|
|
|
|
"",
|
|
|
|
["[", "]"],
|
|
|
|
kArrayExtrasType,
|
|
|
|
recurseTimes,
|
|
|
|
);
|
2022-02-04 15:10:47 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
if (remaining > 0) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
|
|
|
output,
|
|
|
|
`... ${remaining} more item${remaining > 1 ? "s" : ""}`,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
return output;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function formatSetIterInner(
|
|
|
|
ctx,
|
|
|
|
recurseTimes,
|
|
|
|
entries,
|
|
|
|
state,
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
2023-04-30 03:24:13 -04:00
|
|
|
const maxArrayLength = MathMax(ctx.maxArrayLength, 0);
|
|
|
|
const maxLength = MathMin(maxArrayLength, entries.length);
|
2023-12-19 01:05:49 -05:00
|
|
|
const output = [];
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.indentationLvl += 2;
|
|
|
|
for (let i = 0; i < maxLength; i++) {
|
|
|
|
output[i] = formatValue(ctx, entries[i], recurseTimes);
|
|
|
|
}
|
|
|
|
ctx.indentationLvl -= 2;
|
|
|
|
if (state === kWeak && !ctx.sorted) {
|
|
|
|
// Sort all entries to have a halfway reliable output (if more entries than
|
|
|
|
// retrieved ones exist, we can not reliably return the same output) if the
|
|
|
|
// output is not sorted anyway.
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeSort(output);
|
2023-04-30 03:24:13 -04:00
|
|
|
}
|
|
|
|
const remaining = entries.length - maxLength;
|
|
|
|
if (remaining > 0) {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypePush(
|
2023-04-30 03:24:13 -04:00
|
|
|
output,
|
|
|
|
`... ${remaining} more item${remaining > 1 ? "s" : ""}`,
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
return output;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// Regex used for ansi escape code splitting
|
|
|
|
// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js
|
|
|
|
// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore
|
|
|
|
// Matches all ansi escape code sequences in a string
|
|
|
|
const ansiPattern = "[\\u001B\\u009B][[\\]()#;?]*" +
|
|
|
|
"(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*" +
|
|
|
|
"|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)" +
|
|
|
|
"|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))";
|
|
|
|
const ansi = new SafeRegExp(ansiPattern, "g");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of columns required to display the given string.
|
2023-02-07 14:22:46 -05:00
|
|
|
*/
|
2023-04-30 03:24:13 -04:00
|
|
|
export function getStringWidth(str, removeControlChars = true) {
|
|
|
|
let width = 0;
|
2020-09-22 11:10:02 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
if (removeControlChars) {
|
|
|
|
str = stripVTControlCharacters(str);
|
2021-04-27 05:54:47 -04:00
|
|
|
}
|
2023-05-01 09:30:02 -04:00
|
|
|
str = StringPrototypeNormalize(str, "NFC");
|
2023-04-30 03:24:13 -04:00
|
|
|
for (const char of new SafeStringIterator(str)) {
|
2023-05-01 09:30:02 -04:00
|
|
|
const code = StringPrototypeCodePointAt(char, 0);
|
2023-04-30 03:24:13 -04:00
|
|
|
if (isFullWidthCodePoint(code)) {
|
|
|
|
width += 2;
|
|
|
|
} else if (!isZeroWidthCodePoint(code)) {
|
|
|
|
width++;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
return width;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const isZeroWidthCodePoint = (code) => {
|
|
|
|
return code <= 0x1F || // C0 control codes
|
|
|
|
(code >= 0x7F && code <= 0x9F) || // C1 control codes
|
|
|
|
(code >= 0x300 && code <= 0x36F) || // Combining Diacritical Marks
|
|
|
|
(code >= 0x200B && code <= 0x200F) || // Modifying Invisible Characters
|
|
|
|
// Combining Diacritical Marks for Symbols
|
|
|
|
(code >= 0x20D0 && code <= 0x20FF) ||
|
|
|
|
(code >= 0xFE00 && code <= 0xFE0F) || // Variation Selectors
|
|
|
|
(code >= 0xFE20 && code <= 0xFE2F) || // Combining Half Marks
|
|
|
|
(code >= 0xE0100 && code <= 0xE01EF); // Variation Selectors
|
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
/**
|
|
|
|
* Remove all VT control characters. Use to estimate displayed string width.
|
|
|
|
*/
|
|
|
|
export function stripVTControlCharacters(str) {
|
2023-05-01 09:30:02 -04:00
|
|
|
return StringPrototypeReplace(str, ansi, "");
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function hasOwnProperty(obj, v) {
|
|
|
|
if (obj == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-02 06:15:45 -04:00
|
|
|
return ObjectHasOwn(obj, v);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors. MIT license.
|
|
|
|
// Forked from Node's lib/internal/cli_table.js
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const tableChars = {
|
|
|
|
middleMiddle: "\u2500",
|
|
|
|
rowMiddle: "\u253c",
|
|
|
|
topRight: "\u2510",
|
|
|
|
topLeft: "\u250c",
|
|
|
|
leftMiddle: "\u251c",
|
|
|
|
topMiddle: "\u252c",
|
|
|
|
bottomRight: "\u2518",
|
|
|
|
bottomLeft: "\u2514",
|
|
|
|
bottomMiddle: "\u2534",
|
|
|
|
rightMiddle: "\u2524",
|
|
|
|
left: "\u2502 ",
|
|
|
|
right: " \u2502",
|
|
|
|
middle: " \u2502 ",
|
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function isFullWidthCodePoint(code) {
|
|
|
|
// Code points are partially derived from:
|
|
|
|
// http://www.unicode.org/Public/UNIDATA/EastAsianWidth.txt
|
|
|
|
return (
|
|
|
|
code >= 0x1100 &&
|
|
|
|
(code <= 0x115f || // Hangul Jamo
|
|
|
|
code === 0x2329 || // LEFT-POINTING ANGLE BRACKET
|
|
|
|
code === 0x232a || // RIGHT-POINTING ANGLE BRACKET
|
|
|
|
// CJK Radicals Supplement .. Enclosed CJK Letters and Months
|
|
|
|
(code >= 0x2e80 && code <= 0x3247 && code !== 0x303f) ||
|
|
|
|
// Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
|
|
|
|
(code >= 0x3250 && code <= 0x4dbf) ||
|
|
|
|
// CJK Unified Ideographs .. Yi Radicals
|
|
|
|
(code >= 0x4e00 && code <= 0xa4c6) ||
|
|
|
|
// Hangul Jamo Extended-A
|
|
|
|
(code >= 0xa960 && code <= 0xa97c) ||
|
|
|
|
// Hangul Syllables
|
|
|
|
(code >= 0xac00 && code <= 0xd7a3) ||
|
|
|
|
// CJK Compatibility Ideographs
|
|
|
|
(code >= 0xf900 && code <= 0xfaff) ||
|
|
|
|
// Vertical Forms
|
|
|
|
(code >= 0xfe10 && code <= 0xfe19) ||
|
|
|
|
// CJK Compatibility Forms .. Small Form Variants
|
|
|
|
(code >= 0xfe30 && code <= 0xfe6b) ||
|
|
|
|
// Halfwidth and Fullwidth Forms
|
|
|
|
(code >= 0xff01 && code <= 0xff60) ||
|
|
|
|
(code >= 0xffe0 && code <= 0xffe6) ||
|
|
|
|
// Kana Supplement
|
|
|
|
(code >= 0x1b000 && code <= 0x1b001) ||
|
|
|
|
// Enclosed Ideographic Supplement
|
|
|
|
(code >= 0x1f200 && code <= 0x1f251) ||
|
|
|
|
// Miscellaneous Symbols and Pictographs 0x1f300 - 0x1f5ff
|
|
|
|
// Emoticons 0x1f600 - 0x1f64f
|
|
|
|
(code >= 0x1f300 && code <= 0x1f64f) ||
|
|
|
|
// CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
|
|
|
|
(code >= 0x20000 && code <= 0x3fffd))
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function renderRow(row, columnWidths, columnRightAlign) {
|
|
|
|
let out = tableChars.left;
|
|
|
|
for (let i = 0; i < row.length; i++) {
|
|
|
|
const cell = row[i];
|
|
|
|
const len = getStringWidth(cell);
|
|
|
|
const padding = StringPrototypeRepeat(" ", columnWidths[i] - len);
|
|
|
|
if (columnRightAlign?.[i]) {
|
|
|
|
out += `${padding}${cell}`;
|
2023-02-07 14:22:46 -05:00
|
|
|
} else {
|
2023-04-30 03:24:13 -04:00
|
|
|
out += `${cell}${padding}`;
|
|
|
|
}
|
|
|
|
if (i !== row.length - 1) {
|
|
|
|
out += tableChars.middle;
|
2022-04-16 10:12:26 -04:00
|
|
|
}
|
2021-10-30 13:25:46 -04:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
out += tableChars.right;
|
|
|
|
return out;
|
|
|
|
}
|
2021-10-30 13:25:46 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function cliTable(head, columns) {
|
|
|
|
const rows = [];
|
|
|
|
const columnWidths = ArrayPrototypeMap(head, (h) => getStringWidth(h));
|
|
|
|
const longestColumn = ArrayPrototypeReduce(
|
|
|
|
columns,
|
|
|
|
(n, a) => MathMax(n, a.length),
|
|
|
|
0,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2023-05-01 09:30:02 -04:00
|
|
|
const columnRightAlign = ArrayPrototypeFill(
|
|
|
|
new Array(columnWidths.length),
|
|
|
|
true,
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let i = 0; i < head.length; i++) {
|
|
|
|
const column = columns[i];
|
|
|
|
for (let j = 0; j < longestColumn; j++) {
|
|
|
|
if (rows[j] === undefined) {
|
|
|
|
rows[j] = [];
|
|
|
|
}
|
|
|
|
const value = (rows[j][i] = hasOwnProperty(column, j) ? column[j] : "");
|
|
|
|
const width = columnWidths[i] || 0;
|
|
|
|
const counted = getStringWidth(value);
|
|
|
|
columnWidths[i] = MathMax(width, counted);
|
|
|
|
columnRightAlign[i] &= NumberIsInteger(+value);
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const divider = ArrayPrototypeMap(
|
|
|
|
columnWidths,
|
|
|
|
(i) => StringPrototypeRepeat(tableChars.middleMiddle, i + 2),
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
let result =
|
|
|
|
`${tableChars.topLeft}${
|
|
|
|
ArrayPrototypeJoin(divider, tableChars.topMiddle)
|
|
|
|
}` +
|
|
|
|
`${tableChars.topRight}\n${renderRow(head, columnWidths)}\n` +
|
|
|
|
`${tableChars.leftMiddle}${
|
|
|
|
ArrayPrototypeJoin(divider, tableChars.rowMiddle)
|
|
|
|
}` +
|
|
|
|
`${tableChars.rightMiddle}\n`;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
for (let i = 0; i < rows.length; ++i) {
|
|
|
|
const row = rows[i];
|
|
|
|
result += `${renderRow(row, columnWidths, columnRightAlign)}\n`;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
result +=
|
|
|
|
`${tableChars.bottomLeft}${
|
|
|
|
ArrayPrototypeJoin(divider, tableChars.bottomMiddle)
|
|
|
|
}` +
|
|
|
|
tableChars.bottomRight;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
return result;
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
/* End of forked part */
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// We can match Node's quoting behavior exactly by swapping the double quote and
|
|
|
|
// single quote in this array. That would give preference to single quotes.
|
|
|
|
// However, we prefer double quotes as the default.
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const denoInspectDefaultOptions = {
|
|
|
|
indentationLvl: 0,
|
|
|
|
currentDepth: 0,
|
|
|
|
stylize: stylizeNoColor,
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
showHidden: false,
|
|
|
|
depth: 4,
|
|
|
|
colors: false,
|
|
|
|
showProxy: false,
|
|
|
|
breakLength: 80,
|
2023-06-05 06:25:47 -04:00
|
|
|
escapeSequences: true,
|
2023-04-30 03:24:13 -04:00
|
|
|
compact: 3,
|
|
|
|
sorted: false,
|
|
|
|
getters: false,
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// node only
|
|
|
|
maxArrayLength: 100,
|
|
|
|
maxStringLength: 100, // deno: strAbbreviateSize: 100
|
|
|
|
customInspect: true,
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// deno only
|
|
|
|
/** You can override the quotes preference in inspectString.
|
|
|
|
* Used by util.inspect() */
|
|
|
|
// TODO(kt3k): Consider using symbol as a key to hide this from the public
|
|
|
|
// API.
|
|
|
|
quotes: ['"', "'", "`"],
|
|
|
|
iterableLimit: 100, // similar to node's maxArrayLength, but doesn't only apply to arrays
|
|
|
|
trailingComma: false,
|
2021-10-13 23:52:08 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
inspect,
|
2021-10-13 23:52:08 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// TODO(@crowlKats): merge into indentationLvl
|
|
|
|
indentLevel: 0,
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function getDefaultInspectOptions() {
|
|
|
|
return {
|
|
|
|
budget: {},
|
|
|
|
seen: [],
|
|
|
|
...denoInspectDefaultOptions,
|
|
|
|
};
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const DEFAULT_INDENT = " "; // Default indent string
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const STR_ABBREVIATE_SIZE = 100;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
class CSI {
|
|
|
|
static kClear = "\x1b[1;1H";
|
|
|
|
static kClearScreenDown = "\x1b[0J";
|
|
|
|
}
|
2020-09-29 22:59:50 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const QUOTE_SYMBOL_REG = new SafeRegExp(/^[a-zA-Z_][a-zA-Z_.0-9]*$/);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function maybeQuoteSymbol(symbol, ctx) {
|
|
|
|
const description = SymbolPrototypeGetDescription(symbol);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
if (description === undefined) {
|
|
|
|
return SymbolPrototypeToString(symbol);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-11 15:52:20 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
if (RegExpPrototypeTest(QUOTE_SYMBOL_REG, description)) {
|
|
|
|
return SymbolPrototypeToString(symbol);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-10-11 15:52:20 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
return `Symbol(${quoteString(description, ctx)})`;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
/** Surround the string in quotes.
|
|
|
|
*
|
|
|
|
* The quote symbol is chosen by taking the first of the `QUOTES` array which
|
|
|
|
* does not occur in the string. If they all occur, settle with `QUOTES[0]`.
|
|
|
|
*
|
|
|
|
* Insert a backslash before any occurrence of the chosen quote symbol and
|
|
|
|
* before any backslash.
|
|
|
|
*/
|
|
|
|
function quoteString(string, ctx) {
|
|
|
|
const quote = ArrayPrototypeFind(
|
|
|
|
ctx.quotes,
|
|
|
|
(c) => !StringPrototypeIncludes(string, c),
|
|
|
|
) ??
|
|
|
|
ctx.quotes[0];
|
|
|
|
const escapePattern = new SafeRegExp(`(?=[${quote}\\\\])`, "g");
|
|
|
|
string = StringPrototypeReplace(string, escapePattern, "\\");
|
2023-06-05 06:25:47 -04:00
|
|
|
if (ctx.escapeSequences) {
|
|
|
|
string = replaceEscapeSequences(string);
|
|
|
|
}
|
2023-04-30 03:24:13 -04:00
|
|
|
return `${quote}${string}${quote}`;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const ESCAPE_PATTERN = new SafeRegExp(/([\b\f\n\r\t\v])/g);
|
|
|
|
const ESCAPE_MAP = ObjectFreeze({
|
|
|
|
"\b": "\\b",
|
|
|
|
"\f": "\\f",
|
|
|
|
"\n": "\\n",
|
|
|
|
"\r": "\\r",
|
|
|
|
"\t": "\\t",
|
|
|
|
"\v": "\\v",
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
const ESCAPE_PATTERN2 = new SafeRegExp("[\x00-\x1f\x7f-\x9f]", "g");
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// Replace escape sequences that can modify output.
|
|
|
|
function replaceEscapeSequences(string) {
|
|
|
|
return StringPrototypeReplace(
|
|
|
|
StringPrototypeReplace(
|
|
|
|
string,
|
|
|
|
ESCAPE_PATTERN,
|
|
|
|
(c) => ESCAPE_MAP[c],
|
|
|
|
),
|
2023-06-28 21:46:30 -04:00
|
|
|
ESCAPE_PATTERN2,
|
2023-04-30 03:24:13 -04:00
|
|
|
(c) =>
|
|
|
|
"\\x" +
|
|
|
|
StringPrototypePadStart(
|
|
|
|
NumberPrototypeToString(StringPrototypeCharCodeAt(c, 0), 16),
|
|
|
|
2,
|
|
|
|
"0",
|
|
|
|
),
|
|
|
|
);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
// Print strings when they are inside of arrays or objects with quotes
|
|
|
|
function inspectValueWithQuotes(
|
|
|
|
value,
|
|
|
|
ctx,
|
|
|
|
) {
|
|
|
|
const abbreviateSize = typeof ctx.strAbbreviateSize === "undefined"
|
|
|
|
? STR_ABBREVIATE_SIZE
|
|
|
|
: ctx.strAbbreviateSize;
|
|
|
|
switch (typeof value) {
|
|
|
|
case "string": {
|
|
|
|
const trunc = value.length > abbreviateSize
|
|
|
|
? StringPrototypeSlice(value, 0, abbreviateSize) + "..."
|
|
|
|
: value;
|
|
|
|
return ctx.stylize(quoteString(trunc, ctx), "string"); // Quoted strings are green
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return formatValue(ctx, value, 0);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:23:28 -04:00
|
|
|
const colorKeywords = new SafeMap([
|
2023-02-07 14:22:46 -05:00
|
|
|
["black", "#000000"],
|
|
|
|
["silver", "#c0c0c0"],
|
|
|
|
["gray", "#808080"],
|
|
|
|
["white", "#ffffff"],
|
|
|
|
["maroon", "#800000"],
|
|
|
|
["red", "#ff0000"],
|
|
|
|
["purple", "#800080"],
|
|
|
|
["fuchsia", "#ff00ff"],
|
|
|
|
["green", "#008000"],
|
|
|
|
["lime", "#00ff00"],
|
|
|
|
["olive", "#808000"],
|
|
|
|
["yellow", "#ffff00"],
|
|
|
|
["navy", "#000080"],
|
|
|
|
["blue", "#0000ff"],
|
|
|
|
["teal", "#008080"],
|
|
|
|
["aqua", "#00ffff"],
|
|
|
|
["orange", "#ffa500"],
|
|
|
|
["aliceblue", "#f0f8ff"],
|
|
|
|
["antiquewhite", "#faebd7"],
|
|
|
|
["aquamarine", "#7fffd4"],
|
|
|
|
["azure", "#f0ffff"],
|
|
|
|
["beige", "#f5f5dc"],
|
|
|
|
["bisque", "#ffe4c4"],
|
|
|
|
["blanchedalmond", "#ffebcd"],
|
|
|
|
["blueviolet", "#8a2be2"],
|
|
|
|
["brown", "#a52a2a"],
|
|
|
|
["burlywood", "#deb887"],
|
|
|
|
["cadetblue", "#5f9ea0"],
|
|
|
|
["chartreuse", "#7fff00"],
|
|
|
|
["chocolate", "#d2691e"],
|
|
|
|
["coral", "#ff7f50"],
|
|
|
|
["cornflowerblue", "#6495ed"],
|
|
|
|
["cornsilk", "#fff8dc"],
|
|
|
|
["crimson", "#dc143c"],
|
|
|
|
["cyan", "#00ffff"],
|
|
|
|
["darkblue", "#00008b"],
|
|
|
|
["darkcyan", "#008b8b"],
|
|
|
|
["darkgoldenrod", "#b8860b"],
|
|
|
|
["darkgray", "#a9a9a9"],
|
|
|
|
["darkgreen", "#006400"],
|
|
|
|
["darkgrey", "#a9a9a9"],
|
|
|
|
["darkkhaki", "#bdb76b"],
|
|
|
|
["darkmagenta", "#8b008b"],
|
|
|
|
["darkolivegreen", "#556b2f"],
|
|
|
|
["darkorange", "#ff8c00"],
|
|
|
|
["darkorchid", "#9932cc"],
|
|
|
|
["darkred", "#8b0000"],
|
|
|
|
["darksalmon", "#e9967a"],
|
|
|
|
["darkseagreen", "#8fbc8f"],
|
|
|
|
["darkslateblue", "#483d8b"],
|
|
|
|
["darkslategray", "#2f4f4f"],
|
|
|
|
["darkslategrey", "#2f4f4f"],
|
|
|
|
["darkturquoise", "#00ced1"],
|
|
|
|
["darkviolet", "#9400d3"],
|
|
|
|
["deeppink", "#ff1493"],
|
|
|
|
["deepskyblue", "#00bfff"],
|
|
|
|
["dimgray", "#696969"],
|
|
|
|
["dimgrey", "#696969"],
|
|
|
|
["dodgerblue", "#1e90ff"],
|
|
|
|
["firebrick", "#b22222"],
|
|
|
|
["floralwhite", "#fffaf0"],
|
|
|
|
["forestgreen", "#228b22"],
|
|
|
|
["gainsboro", "#dcdcdc"],
|
|
|
|
["ghostwhite", "#f8f8ff"],
|
|
|
|
["gold", "#ffd700"],
|
|
|
|
["goldenrod", "#daa520"],
|
|
|
|
["greenyellow", "#adff2f"],
|
|
|
|
["grey", "#808080"],
|
|
|
|
["honeydew", "#f0fff0"],
|
|
|
|
["hotpink", "#ff69b4"],
|
|
|
|
["indianred", "#cd5c5c"],
|
|
|
|
["indigo", "#4b0082"],
|
|
|
|
["ivory", "#fffff0"],
|
|
|
|
["khaki", "#f0e68c"],
|
|
|
|
["lavender", "#e6e6fa"],
|
|
|
|
["lavenderblush", "#fff0f5"],
|
|
|
|
["lawngreen", "#7cfc00"],
|
|
|
|
["lemonchiffon", "#fffacd"],
|
|
|
|
["lightblue", "#add8e6"],
|
|
|
|
["lightcoral", "#f08080"],
|
|
|
|
["lightcyan", "#e0ffff"],
|
|
|
|
["lightgoldenrodyellow", "#fafad2"],
|
|
|
|
["lightgray", "#d3d3d3"],
|
|
|
|
["lightgreen", "#90ee90"],
|
|
|
|
["lightgrey", "#d3d3d3"],
|
|
|
|
["lightpink", "#ffb6c1"],
|
|
|
|
["lightsalmon", "#ffa07a"],
|
|
|
|
["lightseagreen", "#20b2aa"],
|
|
|
|
["lightskyblue", "#87cefa"],
|
|
|
|
["lightslategray", "#778899"],
|
|
|
|
["lightslategrey", "#778899"],
|
|
|
|
["lightsteelblue", "#b0c4de"],
|
|
|
|
["lightyellow", "#ffffe0"],
|
|
|
|
["limegreen", "#32cd32"],
|
|
|
|
["linen", "#faf0e6"],
|
|
|
|
["magenta", "#ff00ff"],
|
|
|
|
["mediumaquamarine", "#66cdaa"],
|
|
|
|
["mediumblue", "#0000cd"],
|
|
|
|
["mediumorchid", "#ba55d3"],
|
|
|
|
["mediumpurple", "#9370db"],
|
|
|
|
["mediumseagreen", "#3cb371"],
|
|
|
|
["mediumslateblue", "#7b68ee"],
|
|
|
|
["mediumspringgreen", "#00fa9a"],
|
|
|
|
["mediumturquoise", "#48d1cc"],
|
|
|
|
["mediumvioletred", "#c71585"],
|
|
|
|
["midnightblue", "#191970"],
|
|
|
|
["mintcream", "#f5fffa"],
|
|
|
|
["mistyrose", "#ffe4e1"],
|
|
|
|
["moccasin", "#ffe4b5"],
|
|
|
|
["navajowhite", "#ffdead"],
|
|
|
|
["oldlace", "#fdf5e6"],
|
|
|
|
["olivedrab", "#6b8e23"],
|
|
|
|
["orangered", "#ff4500"],
|
|
|
|
["orchid", "#da70d6"],
|
|
|
|
["palegoldenrod", "#eee8aa"],
|
|
|
|
["palegreen", "#98fb98"],
|
|
|
|
["paleturquoise", "#afeeee"],
|
|
|
|
["palevioletred", "#db7093"],
|
|
|
|
["papayawhip", "#ffefd5"],
|
|
|
|
["peachpuff", "#ffdab9"],
|
|
|
|
["peru", "#cd853f"],
|
|
|
|
["pink", "#ffc0cb"],
|
|
|
|
["plum", "#dda0dd"],
|
|
|
|
["powderblue", "#b0e0e6"],
|
|
|
|
["rosybrown", "#bc8f8f"],
|
|
|
|
["royalblue", "#4169e1"],
|
|
|
|
["saddlebrown", "#8b4513"],
|
|
|
|
["salmon", "#fa8072"],
|
|
|
|
["sandybrown", "#f4a460"],
|
|
|
|
["seagreen", "#2e8b57"],
|
|
|
|
["seashell", "#fff5ee"],
|
|
|
|
["sienna", "#a0522d"],
|
|
|
|
["skyblue", "#87ceeb"],
|
|
|
|
["slateblue", "#6a5acd"],
|
|
|
|
["slategray", "#708090"],
|
|
|
|
["slategrey", "#708090"],
|
|
|
|
["snow", "#fffafa"],
|
|
|
|
["springgreen", "#00ff7f"],
|
|
|
|
["steelblue", "#4682b4"],
|
|
|
|
["tan", "#d2b48c"],
|
|
|
|
["thistle", "#d8bfd8"],
|
|
|
|
["tomato", "#ff6347"],
|
|
|
|
["turquoise", "#40e0d0"],
|
|
|
|
["violet", "#ee82ee"],
|
|
|
|
["wheat", "#f5deb3"],
|
|
|
|
["whitesmoke", "#f5f5f5"],
|
|
|
|
["yellowgreen", "#9acd32"],
|
|
|
|
["rebeccapurple", "#663399"],
|
|
|
|
]);
|
|
|
|
|
2023-02-28 18:14:16 -05:00
|
|
|
const HASH_PATTERN = new SafeRegExp(
|
|
|
|
/^#([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})([\dA-Fa-f]{2})?$/,
|
|
|
|
);
|
|
|
|
const SMALL_HASH_PATTERN = new SafeRegExp(
|
|
|
|
/^#([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])?$/,
|
|
|
|
);
|
|
|
|
const RGB_PATTERN = new SafeRegExp(
|
|
|
|
/^rgba?\(\s*([+\-]?\d*\.?\d+)\s*,\s*([+\-]?\d*\.?\d+)\s*,\s*([+\-]?\d*\.?\d+)\s*(,\s*([+\-]?\d*\.?\d+)\s*)?\)$/,
|
|
|
|
);
|
|
|
|
const HSL_PATTERN = new SafeRegExp(
|
|
|
|
/^hsla?\(\s*([+\-]?\d*\.?\d+)\s*,\s*([+\-]?\d*\.?\d+)%\s*,\s*([+\-]?\d*\.?\d+)%\s*(,\s*([+\-]?\d*\.?\d+)\s*)?\)$/,
|
|
|
|
);
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function parseCssColor(colorString) {
|
2023-11-30 13:33:44 -05:00
|
|
|
if (colorKeywords.has(colorString)) {
|
|
|
|
colorString = colorKeywords.get(colorString);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
// deno-fmt-ignore
|
2023-02-28 18:14:16 -05:00
|
|
|
const hashMatch = StringPrototypeMatch(colorString, HASH_PATTERN);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (hashMatch != null) {
|
|
|
|
return [
|
2023-11-30 13:33:44 -05:00
|
|
|
NumberParseInt(hashMatch[1], 16),
|
|
|
|
NumberParseInt(hashMatch[2], 16),
|
|
|
|
NumberParseInt(hashMatch[3], 16),
|
2023-02-07 14:22:46 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
// deno-fmt-ignore
|
2023-02-28 18:14:16 -05:00
|
|
|
const smallHashMatch = StringPrototypeMatch(colorString, SMALL_HASH_PATTERN);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (smallHashMatch != null) {
|
|
|
|
return [
|
2023-11-30 13:33:44 -05:00
|
|
|
NumberParseInt(`${smallHashMatch[1]}${smallHashMatch[1]}`, 16),
|
|
|
|
NumberParseInt(`${smallHashMatch[2]}${smallHashMatch[2]}`, 16),
|
|
|
|
NumberParseInt(`${smallHashMatch[3]}${smallHashMatch[3]}`, 16),
|
2023-02-07 14:22:46 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
// deno-fmt-ignore
|
2023-02-28 18:14:16 -05:00
|
|
|
const rgbMatch = StringPrototypeMatch(colorString, RGB_PATTERN);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (rgbMatch != null) {
|
|
|
|
return [
|
2023-11-30 13:33:44 -05:00
|
|
|
MathRound(MathMax(0, MathMin(255, rgbMatch[1]))),
|
|
|
|
MathRound(MathMax(0, MathMin(255, rgbMatch[2]))),
|
|
|
|
MathRound(MathMax(0, MathMin(255, rgbMatch[3]))),
|
2023-02-07 14:22:46 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
// deno-fmt-ignore
|
2023-02-28 18:14:16 -05:00
|
|
|
const hslMatch = StringPrototypeMatch(colorString, HSL_PATTERN);
|
2023-02-07 14:22:46 -05:00
|
|
|
if (hslMatch != null) {
|
|
|
|
// https://www.rapidtables.com/convert/color/hsl-to-rgb.html
|
|
|
|
let h = Number(hslMatch[1]) % 360;
|
|
|
|
if (h < 0) {
|
|
|
|
h += 360;
|
|
|
|
}
|
2023-11-30 13:33:44 -05:00
|
|
|
const s = MathMax(0, MathMin(100, hslMatch[2])) / 100;
|
|
|
|
const l = MathMax(0, MathMin(100, hslMatch[3])) / 100;
|
2023-02-07 14:22:46 -05:00
|
|
|
const c = (1 - MathAbs(2 * l - 1)) * s;
|
|
|
|
const x = c * (1 - MathAbs((h / 60) % 2 - 1));
|
|
|
|
const m = l - c / 2;
|
|
|
|
let r_;
|
|
|
|
let g_;
|
|
|
|
let b_;
|
|
|
|
if (h < 60) {
|
|
|
|
({ 0: r_, 1: g_, 2: b_ } = [c, x, 0]);
|
|
|
|
} else if (h < 120) {
|
|
|
|
({ 0: r_, 1: g_, 2: b_ } = [x, c, 0]);
|
|
|
|
} else if (h < 180) {
|
|
|
|
({ 0: r_, 1: g_, 2: b_ } = [0, c, x]);
|
|
|
|
} else if (h < 240) {
|
|
|
|
({ 0: r_, 1: g_, 2: b_ } = [0, x, c]);
|
|
|
|
} else if (h < 300) {
|
|
|
|
({ 0: r_, 1: g_, 2: b_ } = [x, 0, c]);
|
2020-07-19 13:49:44 -04:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
({ 0: r_, 1: g_, 2: b_ } = [c, 0, x]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return [
|
|
|
|
MathRound((r_ + m) * 255),
|
|
|
|
MathRound((g_ + m) * 255),
|
|
|
|
MathRound((b_ + m) * 255),
|
|
|
|
];
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultCss() {
|
|
|
|
return {
|
|
|
|
backgroundColor: null,
|
|
|
|
color: null,
|
|
|
|
fontWeight: null,
|
|
|
|
fontStyle: null,
|
|
|
|
textDecorationColor: null,
|
|
|
|
textDecorationLine: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-28 18:14:16 -05:00
|
|
|
const SPACE_PATTERN = new SafeRegExp(/\s+/g);
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function parseCss(cssString) {
|
|
|
|
const css = getDefaultCss();
|
|
|
|
|
|
|
|
const rawEntries = [];
|
|
|
|
let inValue = false;
|
|
|
|
let currentKey = null;
|
|
|
|
let parenthesesDepth = 0;
|
|
|
|
let currentPart = "";
|
|
|
|
for (let i = 0; i < cssString.length; i++) {
|
|
|
|
const c = cssString[i];
|
|
|
|
if (c == "(") {
|
|
|
|
parenthesesDepth++;
|
|
|
|
} else if (parenthesesDepth > 0) {
|
|
|
|
if (c == ")") {
|
|
|
|
parenthesesDepth--;
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (inValue) {
|
|
|
|
if (c == ";") {
|
|
|
|
const value = StringPrototypeTrim(currentPart);
|
|
|
|
if (value != "") {
|
|
|
|
ArrayPrototypePush(rawEntries, [currentKey, value]);
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
currentKey = null;
|
2020-09-10 06:49:47 -04:00
|
|
|
currentPart = "";
|
2023-02-07 14:22:46 -05:00
|
|
|
inValue = false;
|
2020-09-10 06:49:47 -04:00
|
|
|
continue;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (c == ":") {
|
|
|
|
currentKey = StringPrototypeTrim(currentPart);
|
2020-09-10 06:49:47 -04:00
|
|
|
currentPart = "";
|
2023-02-07 14:22:46 -05:00
|
|
|
inValue = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
currentPart += c;
|
|
|
|
}
|
|
|
|
if (inValue && parenthesesDepth == 0) {
|
|
|
|
const value = StringPrototypeTrim(currentPart);
|
|
|
|
if (value != "") {
|
|
|
|
ArrayPrototypePush(rawEntries, [currentKey, value]);
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
currentKey = null;
|
|
|
|
currentPart = "";
|
|
|
|
}
|
2020-09-10 06:49:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < rawEntries.length; ++i) {
|
|
|
|
const { 0: key, 1: value } = rawEntries[i];
|
|
|
|
if (key == "background-color") {
|
|
|
|
if (value != null) {
|
|
|
|
css.backgroundColor = value;
|
|
|
|
}
|
|
|
|
} else if (key == "color") {
|
|
|
|
if (value != null) {
|
|
|
|
css.color = value;
|
|
|
|
}
|
|
|
|
} else if (key == "font-weight") {
|
|
|
|
if (value == "bold") {
|
|
|
|
css.fontWeight = value;
|
|
|
|
}
|
|
|
|
} else if (key == "font-style") {
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(["italic", "oblique", "oblique 14deg"], value)
|
|
|
|
) {
|
|
|
|
css.fontStyle = "italic";
|
|
|
|
}
|
|
|
|
} else if (key == "text-decoration-line") {
|
|
|
|
css.textDecorationLine = [];
|
2023-02-28 18:14:16 -05:00
|
|
|
const lineTypes = StringPrototypeSplit(value, SPACE_PATTERN);
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < lineTypes.length; ++i) {
|
|
|
|
const lineType = lineTypes[i];
|
2021-07-06 06:03:12 -04:00
|
|
|
if (
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
["line-through", "overline", "underline"],
|
|
|
|
lineType,
|
|
|
|
)
|
2021-07-06 06:03:12 -04:00
|
|
|
) {
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(css.textDecorationLine, lineType);
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
} else if (key == "text-decoration-color") {
|
|
|
|
const color = parseCssColor(value);
|
|
|
|
if (color != null) {
|
|
|
|
css.textDecorationColor = color;
|
|
|
|
}
|
|
|
|
} else if (key == "text-decoration") {
|
|
|
|
css.textDecorationColor = null;
|
|
|
|
css.textDecorationLine = [];
|
2023-02-28 18:14:16 -05:00
|
|
|
const args = StringPrototypeSplit(value, SPACE_PATTERN);
|
2023-02-07 14:22:46 -05:00
|
|
|
for (let i = 0; i < args.length; ++i) {
|
|
|
|
const arg = args[i];
|
|
|
|
const maybeColor = parseCssColor(arg);
|
|
|
|
if (maybeColor != null) {
|
|
|
|
css.textDecorationColor = maybeColor;
|
|
|
|
} else if (
|
|
|
|
ArrayPrototypeIncludes(
|
|
|
|
["line-through", "overline", "underline"],
|
|
|
|
arg,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
ArrayPrototypePush(css.textDecorationLine, arg);
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-09-10 06:49:47 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return css;
|
|
|
|
}
|
|
|
|
|
|
|
|
function colorEquals(color1, color2) {
|
|
|
|
return color1?.[0] == color2?.[0] && color1?.[1] == color2?.[1] &&
|
|
|
|
color1?.[2] == color2?.[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
function cssToAnsi(css, prevCss = null) {
|
|
|
|
prevCss = prevCss ?? getDefaultCss();
|
|
|
|
let ansi = "";
|
|
|
|
if (!colorEquals(css.backgroundColor, prevCss.backgroundColor)) {
|
|
|
|
if (css.backgroundColor == null) {
|
|
|
|
ansi += "\x1b[49m";
|
|
|
|
} else if (css.backgroundColor == "black") {
|
|
|
|
ansi += `\x1b[40m`;
|
|
|
|
} else if (css.backgroundColor == "red") {
|
|
|
|
ansi += `\x1b[41m`;
|
|
|
|
} else if (css.backgroundColor == "green") {
|
|
|
|
ansi += `\x1b[42m`;
|
|
|
|
} else if (css.backgroundColor == "yellow") {
|
|
|
|
ansi += `\x1b[43m`;
|
|
|
|
} else if (css.backgroundColor == "blue") {
|
|
|
|
ansi += `\x1b[44m`;
|
|
|
|
} else if (css.backgroundColor == "magenta") {
|
|
|
|
ansi += `\x1b[45m`;
|
|
|
|
} else if (css.backgroundColor == "cyan") {
|
|
|
|
ansi += `\x1b[46m`;
|
|
|
|
} else if (css.backgroundColor == "white") {
|
|
|
|
ansi += `\x1b[47m`;
|
|
|
|
} else {
|
|
|
|
if (ArrayIsArray(css.backgroundColor)) {
|
|
|
|
const { 0: r, 1: g, 2: b } = css.backgroundColor;
|
|
|
|
ansi += `\x1b[48;2;${r};${g};${b}m`;
|
2021-12-23 07:06:24 -05:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
const parsed = parseCssColor(css.backgroundColor);
|
|
|
|
if (parsed !== null) {
|
|
|
|
const { 0: r, 1: g, 2: b } = parsed;
|
2022-02-06 05:00:06 -05:00
|
|
|
ansi += `\x1b[48;2;${r};${g};${b}m`;
|
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
ansi += "\x1b[49m";
|
2022-02-06 05:00:06 -05:00
|
|
|
}
|
2020-10-05 05:50:49 -04:00
|
|
|
}
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (!colorEquals(css.color, prevCss.color)) {
|
|
|
|
if (css.color == null) {
|
|
|
|
ansi += "\x1b[39m";
|
|
|
|
} else if (css.color == "black") {
|
|
|
|
ansi += `\x1b[30m`;
|
|
|
|
} else if (css.color == "red") {
|
|
|
|
ansi += `\x1b[31m`;
|
|
|
|
} else if (css.color == "green") {
|
|
|
|
ansi += `\x1b[32m`;
|
|
|
|
} else if (css.color == "yellow") {
|
|
|
|
ansi += `\x1b[33m`;
|
|
|
|
} else if (css.color == "blue") {
|
|
|
|
ansi += `\x1b[34m`;
|
|
|
|
} else if (css.color == "magenta") {
|
|
|
|
ansi += `\x1b[35m`;
|
|
|
|
} else if (css.color == "cyan") {
|
|
|
|
ansi += `\x1b[36m`;
|
|
|
|
} else if (css.color == "white") {
|
|
|
|
ansi += `\x1b[37m`;
|
|
|
|
} else {
|
|
|
|
if (ArrayIsArray(css.color)) {
|
|
|
|
const { 0: r, 1: g, 2: b } = css.color;
|
|
|
|
ansi += `\x1b[38;2;${r};${g};${b}m`;
|
2021-12-23 07:06:24 -05:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
const parsed = parseCssColor(css.color);
|
|
|
|
if (parsed !== null) {
|
|
|
|
const { 0: r, 1: g, 2: b } = parsed;
|
2022-02-06 05:00:06 -05:00
|
|
|
ansi += `\x1b[38;2;${r};${g};${b}m`;
|
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
ansi += "\x1b[39m";
|
2022-02-06 05:00:06 -05:00
|
|
|
}
|
2020-10-05 05:50:49 -04:00
|
|
|
}
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (css.fontWeight != prevCss.fontWeight) {
|
|
|
|
if (css.fontWeight == "bold") {
|
|
|
|
ansi += `\x1b[1m`;
|
|
|
|
} else {
|
|
|
|
ansi += "\x1b[22m";
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (css.fontStyle != prevCss.fontStyle) {
|
|
|
|
if (css.fontStyle == "italic") {
|
|
|
|
ansi += `\x1b[3m`;
|
|
|
|
} else {
|
|
|
|
ansi += "\x1b[23m";
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (!colorEquals(css.textDecorationColor, prevCss.textDecorationColor)) {
|
|
|
|
if (css.textDecorationColor != null) {
|
|
|
|
const { 0: r, 1: g, 2: b } = css.textDecorationColor;
|
|
|
|
ansi += `\x1b[58;2;${r};${g};${b}m`;
|
|
|
|
} else {
|
|
|
|
ansi += "\x1b[59m";
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(css.textDecorationLine, "line-through") !=
|
|
|
|
ArrayPrototypeIncludes(prevCss.textDecorationLine, "line-through")
|
|
|
|
) {
|
|
|
|
if (ArrayPrototypeIncludes(css.textDecorationLine, "line-through")) {
|
|
|
|
ansi += "\x1b[9m";
|
|
|
|
} else {
|
|
|
|
ansi += "\x1b[29m";
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(css.textDecorationLine, "overline") !=
|
|
|
|
ArrayPrototypeIncludes(prevCss.textDecorationLine, "overline")
|
|
|
|
) {
|
|
|
|
if (ArrayPrototypeIncludes(css.textDecorationLine, "overline")) {
|
|
|
|
ansi += "\x1b[53m";
|
|
|
|
} else {
|
|
|
|
ansi += "\x1b[55m";
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
ArrayPrototypeIncludes(css.textDecorationLine, "underline") !=
|
|
|
|
ArrayPrototypeIncludes(prevCss.textDecorationLine, "underline")
|
|
|
|
) {
|
|
|
|
if (ArrayPrototypeIncludes(css.textDecorationLine, "underline")) {
|
|
|
|
ansi += "\x1b[4m";
|
|
|
|
} else {
|
|
|
|
ansi += "\x1b[24m";
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
return ansi;
|
|
|
|
}
|
|
|
|
|
|
|
|
function inspectArgs(args, inspectOptions = {}) {
|
2023-04-30 03:24:13 -04:00
|
|
|
const ctx = {
|
|
|
|
...getDefaultInspectOptions(),
|
|
|
|
...inspectOptions,
|
|
|
|
};
|
|
|
|
if (inspectOptions.iterableLimit !== undefined) {
|
|
|
|
ctx.maxArrayLength = inspectOptions.iterableLimit;
|
|
|
|
}
|
|
|
|
if (inspectOptions.strAbbreviateSize !== undefined) {
|
|
|
|
ctx.maxStringLength = inspectOptions.strAbbreviateSize;
|
|
|
|
}
|
|
|
|
if (ctx.colors) ctx.stylize = createStylizeWithColor(styles, colors);
|
|
|
|
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
|
|
|
|
if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2023-04-30 05:11:37 -04:00
|
|
|
const noColor = getNoColor();
|
2023-02-07 14:22:46 -05:00
|
|
|
const first = args[0];
|
|
|
|
let a = 0;
|
|
|
|
let string = "";
|
|
|
|
|
|
|
|
if (typeof first == "string" && args.length > 1) {
|
|
|
|
a++;
|
|
|
|
// Index of the first not-yet-appended character. Use this so we only
|
|
|
|
// have to append to `string` when a substitution occurs / at the end.
|
|
|
|
let appendedChars = 0;
|
|
|
|
let usedStyle = false;
|
|
|
|
let prevCss = null;
|
|
|
|
for (let i = 0; i < first.length - 1; i++) {
|
|
|
|
if (first[i] == "%") {
|
|
|
|
const char = first[++i];
|
|
|
|
if (a < args.length) {
|
|
|
|
let formattedArg = null;
|
|
|
|
if (char == "s") {
|
|
|
|
// Format as a string.
|
|
|
|
formattedArg = String(args[a++]);
|
|
|
|
} else if (ArrayPrototypeIncludes(["d", "i"], char)) {
|
|
|
|
// Format as an integer.
|
|
|
|
const value = args[a++];
|
|
|
|
if (typeof value == "bigint") {
|
|
|
|
formattedArg = `${value}n`;
|
|
|
|
} else if (typeof value == "number") {
|
|
|
|
formattedArg = `${NumberParseInt(String(value))}`;
|
|
|
|
} else {
|
|
|
|
formattedArg = "NaN";
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (char == "f") {
|
|
|
|
// Format as a floating point value.
|
|
|
|
const value = args[a++];
|
|
|
|
if (typeof value == "number") {
|
|
|
|
formattedArg = `${value}`;
|
|
|
|
} else {
|
|
|
|
formattedArg = "NaN";
|
|
|
|
}
|
|
|
|
} else if (ArrayPrototypeIncludes(["O", "o"], char)) {
|
|
|
|
// Format as an object.
|
2023-04-30 03:24:13 -04:00
|
|
|
formattedArg = formatValue(ctx, args[a++], 0);
|
2023-02-07 14:22:46 -05:00
|
|
|
} else if (char == "c") {
|
|
|
|
const value = args[a++];
|
|
|
|
if (!noColor) {
|
|
|
|
const css = parseCss(value);
|
|
|
|
formattedArg = cssToAnsi(css, prevCss);
|
|
|
|
if (formattedArg != "") {
|
|
|
|
usedStyle = true;
|
|
|
|
prevCss = css;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
formattedArg = "";
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2020-09-10 06:49:47 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
if (formattedArg != null) {
|
|
|
|
string += StringPrototypeSlice(first, appendedChars, i - 1) +
|
|
|
|
formattedArg;
|
2020-09-10 06:49:47 -04:00
|
|
|
appendedChars = i + 1;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (char == "%") {
|
|
|
|
string += StringPrototypeSlice(first, appendedChars, i - 1) + "%";
|
|
|
|
appendedChars = i + 1;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
string += StringPrototypeSlice(first, appendedChars);
|
|
|
|
if (usedStyle) {
|
|
|
|
string += "\x1b[0m";
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
for (; a < args.length; a++) {
|
|
|
|
if (a > 0) {
|
|
|
|
string += " ";
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
if (typeof args[a] == "string") {
|
|
|
|
string += args[a];
|
|
|
|
} else {
|
|
|
|
// Use default maximum depth for null or undefined arguments.
|
2023-04-30 03:24:13 -04:00
|
|
|
string += formatValue(ctx, args[a], 0);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
if (ctx.indentLevel > 0) {
|
2023-02-07 14:22:46 -05:00
|
|
|
const groupIndent = StringPrototypeRepeat(
|
|
|
|
DEFAULT_INDENT,
|
2023-04-30 03:24:13 -04:00
|
|
|
ctx.indentLevel,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
string = groupIndent +
|
|
|
|
StringPrototypeReplaceAll(string, "\n", `\n${groupIndent}`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return string;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-04-30 03:24:13 -04:00
|
|
|
function createStylizeWithColor(styles, colors) {
|
|
|
|
return function stylizeWithColor(str, styleType) {
|
|
|
|
const style = styles[styleType];
|
|
|
|
if (style !== undefined) {
|
|
|
|
const color = colors[style];
|
|
|
|
if (color !== undefined) {
|
|
|
|
return `\u001b[${color[0]}m${str}\u001b[${color[1]}m`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-14 16:23:28 -04:00
|
|
|
const countMap = new SafeMap();
|
|
|
|
const timerMap = new SafeMap();
|
2023-02-07 14:22:46 -05:00
|
|
|
const isConsoleInstance = Symbol("isConsoleInstance");
|
2020-09-29 22:59:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getConsoleInspectOptions() {
|
2023-04-30 05:11:37 -04:00
|
|
|
const color = !getNoColor();
|
2023-02-07 14:22:46 -05:00
|
|
|
return {
|
2023-04-30 03:24:13 -04:00
|
|
|
...getDefaultInspectOptions(),
|
|
|
|
colors: color,
|
|
|
|
stylize: color ? createStylizeWithColor(styles, colors) : stylizeNoColor,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Console {
|
|
|
|
#printFunc = null;
|
|
|
|
[isConsoleInstance] = false;
|
|
|
|
|
|
|
|
constructor(printFunc) {
|
|
|
|
this.#printFunc = printFunc;
|
|
|
|
this.indentLevel = 0;
|
|
|
|
this[isConsoleInstance] = true;
|
|
|
|
|
|
|
|
// ref https://console.spec.whatwg.org/#console-namespace
|
|
|
|
// For historical web-compatibility reasons, the namespace object for
|
|
|
|
// console must have as its [[Prototype]] an empty object, created as if
|
|
|
|
// by ObjectCreate(%ObjectPrototype%), instead of %ObjectPrototype%.
|
|
|
|
const console = ObjectCreate({}, {
|
|
|
|
[SymbolToStringTag]: {
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
configurable: true,
|
|
|
|
value: "console",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
ObjectAssign(console, this);
|
|
|
|
return console;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
log = (...args) => {
|
|
|
|
this.#printFunc(
|
|
|
|
inspectArgs(args, {
|
|
|
|
...getConsoleInspectOptions(),
|
|
|
|
indentLevel: this.indentLevel,
|
|
|
|
}) + "\n",
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
debug = (...args) => {
|
|
|
|
this.#printFunc(
|
|
|
|
inspectArgs(args, {
|
|
|
|
...getConsoleInspectOptions(),
|
|
|
|
indentLevel: this.indentLevel,
|
|
|
|
}) + "\n",
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
};
|
2021-03-18 14:25:25 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
info = (...args) => {
|
|
|
|
this.#printFunc(
|
|
|
|
inspectArgs(args, {
|
|
|
|
...getConsoleInspectOptions(),
|
|
|
|
indentLevel: this.indentLevel,
|
|
|
|
}) + "\n",
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
dir = (obj = undefined, options = {}) => {
|
|
|
|
this.#printFunc(
|
|
|
|
inspectArgs([obj], { ...getConsoleInspectOptions(), ...options }) +
|
|
|
|
"\n",
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
dirxml = this.dir;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
warn = (...args) => {
|
|
|
|
this.#printFunc(
|
|
|
|
inspectArgs(args, {
|
|
|
|
...getConsoleInspectOptions(),
|
|
|
|
indentLevel: this.indentLevel,
|
|
|
|
}) + "\n",
|
|
|
|
2,
|
|
|
|
);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
error = (...args) => {
|
|
|
|
this.#printFunc(
|
|
|
|
inspectArgs(args, {
|
|
|
|
...getConsoleInspectOptions(),
|
|
|
|
indentLevel: this.indentLevel,
|
|
|
|
}) + "\n",
|
|
|
|
3,
|
|
|
|
);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
assert = (condition = false, ...args) => {
|
|
|
|
if (condition) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (args.length === 0) {
|
|
|
|
this.error("Assertion failed");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const [first, ...rest] = new SafeArrayIterator(args);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (typeof first === "string") {
|
|
|
|
this.error(
|
|
|
|
`Assertion failed: ${first}`,
|
|
|
|
...new SafeArrayIterator(rest),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.error(`Assertion failed:`, ...new SafeArrayIterator(args));
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
count = (label = "default") => {
|
|
|
|
label = String(label);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (MapPrototypeHas(countMap, label)) {
|
|
|
|
const current = MapPrototypeGet(countMap, label) || 0;
|
|
|
|
MapPrototypeSet(countMap, label, current + 1);
|
|
|
|
} else {
|
|
|
|
MapPrototypeSet(countMap, label, 1);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.info(`${label}: ${MapPrototypeGet(countMap, label)}`);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
countReset = (label = "default") => {
|
|
|
|
label = String(label);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (MapPrototypeHas(countMap, label)) {
|
|
|
|
MapPrototypeSet(countMap, label, 0);
|
|
|
|
} else {
|
|
|
|
this.warn(`Count for '${label}' does not exist`);
|
|
|
|
}
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
table = (data = undefined, properties = undefined) => {
|
|
|
|
if (properties !== undefined && !ArrayIsArray(properties)) {
|
|
|
|
throw new Error(
|
|
|
|
"The 'properties' argument must be of type Array. " +
|
2023-06-28 21:46:30 -04:00
|
|
|
"Received type " + typeof properties,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (data === null || typeof data !== "object") {
|
|
|
|
return this.log(data);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const stringifyValue = (value) =>
|
|
|
|
inspectValueWithQuotes(value, {
|
2023-04-30 03:24:13 -04:00
|
|
|
...getDefaultInspectOptions(),
|
2023-02-07 14:22:46 -05:00
|
|
|
depth: 1,
|
2023-04-30 03:24:13 -04:00
|
|
|
compact: true,
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
const toTable = (header, body) => this.log(cliTable(header, body));
|
|
|
|
|
|
|
|
let resultData;
|
2024-01-03 23:12:38 -05:00
|
|
|
const isSetObject = isSet(data);
|
|
|
|
const isMapObject = isMap(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
const valuesKey = "Values";
|
2024-01-03 23:12:38 -05:00
|
|
|
const indexKey = isSetObject || isMapObject ? "(iter idx)" : "(idx)";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-03 23:12:38 -05:00
|
|
|
if (isSetObject) {
|
2023-04-25 18:36:22 -04:00
|
|
|
resultData = [...new SafeSetIterator(data)];
|
2024-01-03 23:12:38 -05:00
|
|
|
} else if (isMapObject) {
|
2023-02-07 14:22:46 -05:00
|
|
|
let idx = 0;
|
|
|
|
resultData = {};
|
|
|
|
|
|
|
|
MapPrototypeForEach(data, (v, k) => {
|
|
|
|
resultData[idx] = { Key: k, Values: v };
|
|
|
|
idx++;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resultData = data;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const keys = ObjectKeys(resultData);
|
|
|
|
const numRows = keys.length;
|
2021-04-11 08:19:50 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const objectValues = properties
|
|
|
|
? ObjectFromEntries(
|
|
|
|
ArrayPrototypeMap(
|
|
|
|
properties,
|
|
|
|
(name) => [name, ArrayPrototypeFill(new Array(numRows), "")],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: {};
|
|
|
|
const indexKeys = [];
|
|
|
|
const values = [];
|
|
|
|
|
|
|
|
let hasPrimitives = false;
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeForEach(keys, (k, idx) => {
|
2023-02-07 14:22:46 -05:00
|
|
|
const value = resultData[k];
|
|
|
|
const primitive = value === null ||
|
|
|
|
(typeof value !== "function" && typeof value !== "object");
|
|
|
|
if (properties === undefined && primitive) {
|
|
|
|
hasPrimitives = true;
|
|
|
|
ArrayPrototypePush(values, stringifyValue(value));
|
|
|
|
} else {
|
|
|
|
const valueObj = value || {};
|
|
|
|
const keys = properties || ObjectKeys(valueObj);
|
|
|
|
for (let i = 0; i < keys.length; ++i) {
|
|
|
|
const k = keys[i];
|
|
|
|
if (!primitive && ReflectHas(valueObj, k)) {
|
|
|
|
if (!(ReflectHas(objectValues, k))) {
|
|
|
|
objectValues[k] = ArrayPrototypeFill(new Array(numRows), "");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
objectValues[k][idx] = stringifyValue(valueObj[k]);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(values, "");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
ArrayPrototypePush(indexKeys, k);
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const headerKeys = ObjectKeys(objectValues);
|
|
|
|
const bodyValues = ObjectValues(objectValues);
|
|
|
|
const headerProps = properties ||
|
|
|
|
[
|
|
|
|
...new SafeArrayIterator(headerKeys),
|
2024-01-03 23:12:38 -05:00
|
|
|
!isMapObject && hasPrimitives && valuesKey,
|
2023-02-07 14:22:46 -05:00
|
|
|
];
|
|
|
|
const header = ArrayPrototypeFilter([
|
|
|
|
indexKey,
|
|
|
|
...new SafeArrayIterator(headerProps),
|
|
|
|
], Boolean);
|
|
|
|
const body = [indexKeys, ...new SafeArrayIterator(bodyValues), values];
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
toTable(header, body);
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
time = (label = "default") => {
|
|
|
|
label = String(label);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (MapPrototypeHas(timerMap, label)) {
|
|
|
|
this.warn(`Timer '${label}' already exists`);
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
MapPrototypeSet(timerMap, label, DateNow());
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
timeLog = (label = "default", ...args) => {
|
|
|
|
label = String(label);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!MapPrototypeHas(timerMap, label)) {
|
2023-06-28 21:46:30 -04:00
|
|
|
this.warn(`Timer '${label}' does not exist`);
|
2023-02-07 14:22:46 -05:00
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const startTime = MapPrototypeGet(timerMap, label);
|
|
|
|
const duration = DateNow() - startTime;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.info(`${label}: ${duration}ms`, ...new SafeArrayIterator(args));
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
timeEnd = (label = "default") => {
|
|
|
|
label = String(label);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
if (!MapPrototypeHas(timerMap, label)) {
|
|
|
|
this.warn(`Timer '${label}' does not exist`);
|
|
|
|
return;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
const startTime = MapPrototypeGet(timerMap, label);
|
|
|
|
MapPrototypeDelete(timerMap, label);
|
|
|
|
const duration = DateNow() - startTime;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.info(`${label}: ${duration}ms`);
|
|
|
|
};
|
2022-11-21 20:17:14 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
group = (...label) => {
|
|
|
|
if (label.length > 0) {
|
|
|
|
this.log(...new SafeArrayIterator(label));
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
this.indentLevel += 2;
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
groupCollapsed = this.group;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
groupEnd = () => {
|
|
|
|
if (this.indentLevel > 0) {
|
|
|
|
this.indentLevel -= 2;
|
|
|
|
}
|
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
clear = () => {
|
|
|
|
this.indentLevel = 0;
|
|
|
|
this.#printFunc(CSI.kClear, 1);
|
|
|
|
this.#printFunc(CSI.kClearScreenDown, 1);
|
|
|
|
};
|
2021-07-08 09:43:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
trace = (...args) => {
|
|
|
|
const message = inspectArgs(
|
|
|
|
args,
|
|
|
|
{ ...getConsoleInspectOptions(), indentLevel: 0 },
|
|
|
|
);
|
|
|
|
const err = {
|
|
|
|
name: "Trace",
|
|
|
|
message,
|
|
|
|
};
|
|
|
|
ErrorCaptureStackTrace(err, this.trace);
|
|
|
|
this.error(err.stack);
|
|
|
|
};
|
2021-07-08 09:43:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// These methods are noops, but when the inspector is connected, they
|
|
|
|
// call into V8.
|
|
|
|
profile = (_label) => {};
|
|
|
|
profileEnd = (_label) => {};
|
|
|
|
timeStamp = (_label) => {};
|
2021-07-08 09:43:36 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
static [SymbolHasInstance](instance) {
|
|
|
|
return instance[isConsoleInstance];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const customInspect = SymbolFor("Deno.customInspect");
|
|
|
|
|
|
|
|
function inspect(
|
|
|
|
value,
|
|
|
|
inspectOptions = {},
|
|
|
|
) {
|
2023-04-30 03:24:13 -04:00
|
|
|
// Default options
|
|
|
|
const ctx = {
|
|
|
|
...getDefaultInspectOptions(),
|
2023-02-07 14:22:46 -05:00
|
|
|
...inspectOptions,
|
2023-04-30 03:24:13 -04:00
|
|
|
};
|
|
|
|
if (inspectOptions.iterableLimit !== undefined) {
|
|
|
|
ctx.maxArrayLength = inspectOptions.iterableLimit;
|
|
|
|
}
|
|
|
|
if (inspectOptions.strAbbreviateSize !== undefined) {
|
|
|
|
ctx.maxStringLength = inspectOptions.strAbbreviateSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.colors) ctx.stylize = createStylizeWithColor(styles, colors);
|
|
|
|
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
|
|
|
|
if (ctx.maxStringLength === null) ctx.maxStringLength = Infinity;
|
|
|
|
return formatValue(ctx, value, 0);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Creates a proxy that represents a subset of the properties
|
|
|
|
* of the original object optionally without evaluating the properties
|
|
|
|
* in order to get the values. */
|
|
|
|
function createFilteredInspectProxy({ object, keys, evaluate }) {
|
2023-04-30 03:24:13 -04:00
|
|
|
const obj = class {};
|
|
|
|
if (object.constructor?.name) {
|
|
|
|
ObjectDefineProperty(obj, "name", { value: object.constructor.name });
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Proxy(new obj(), {
|
2023-02-07 14:22:46 -05:00
|
|
|
get(_target, key) {
|
|
|
|
if (key === SymbolToStringTag) {
|
|
|
|
return object.constructor?.name;
|
|
|
|
} else if (ArrayPrototypeIncludes(keys, key)) {
|
|
|
|
return ReflectGet(object, key);
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getOwnPropertyDescriptor(_target, key) {
|
|
|
|
if (!ArrayPrototypeIncludes(keys, key)) {
|
|
|
|
return undefined;
|
|
|
|
} else if (evaluate) {
|
|
|
|
return getEvaluatedDescriptor(object, key);
|
2022-11-21 20:17:14 -05:00
|
|
|
} else {
|
2023-02-07 14:22:46 -05:00
|
|
|
return getDescendantPropertyDescriptor(object, key) ??
|
|
|
|
getEvaluatedDescriptor(object, key);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
has(_target, key) {
|
|
|
|
return ArrayPrototypeIncludes(keys, key);
|
|
|
|
},
|
|
|
|
ownKeys() {
|
|
|
|
return keys;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function getDescendantPropertyDescriptor(object, key) {
|
|
|
|
let propertyDescriptor = ReflectGetOwnPropertyDescriptor(object, key);
|
|
|
|
if (!propertyDescriptor) {
|
|
|
|
const prototype = ReflectGetPrototypeOf(object);
|
|
|
|
if (prototype) {
|
|
|
|
propertyDescriptor = getDescendantPropertyDescriptor(prototype, key);
|
2021-06-26 20:27:50 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
return propertyDescriptor;
|
2021-06-26 20:27:50 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
function getEvaluatedDescriptor(object, key) {
|
|
|
|
return {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: object[key],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expose these fields to internalObject for tests.
|
|
|
|
internals.Console = Console;
|
|
|
|
internals.cssToAnsi = cssToAnsi;
|
|
|
|
internals.inspectArgs = inspectArgs;
|
|
|
|
internals.parseCss = parseCss;
|
|
|
|
internals.parseCssColor = parseCssColor;
|
|
|
|
|
|
|
|
export {
|
2023-04-30 03:24:13 -04:00
|
|
|
colors,
|
2023-02-07 14:22:46 -05:00
|
|
|
Console,
|
|
|
|
createFilteredInspectProxy,
|
2023-04-30 03:24:13 -04:00
|
|
|
createStylizeWithColor,
|
2023-02-07 14:22:46 -05:00
|
|
|
CSI,
|
|
|
|
customInspect,
|
2023-04-30 03:24:13 -04:00
|
|
|
formatBigInt,
|
|
|
|
formatNumber,
|
|
|
|
formatValue,
|
|
|
|
getDefaultInspectOptions,
|
2023-04-30 05:11:37 -04:00
|
|
|
getNoColor,
|
2023-02-07 14:22:46 -05:00
|
|
|
inspect,
|
|
|
|
inspectArgs,
|
|
|
|
quoteString,
|
2023-11-12 23:52:59 -05:00
|
|
|
setNoColorFn,
|
2023-04-30 03:24:13 -04:00
|
|
|
styles,
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|