Branch data Line data Source code
1 : 96 : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
2 : : // SPDX-FileCopyrightText: 2013 Red Hat, Inc.
3 : : // SPDX-FileCopyrightText: 2020 Evan Welsh <contact@evanwelsh.com>
4 : :
5 : 96 : (function (exports) {
6 : : 'use strict';
7 : :
8 : : const {
9 : : print,
10 : : printerr,
11 : : log: nativeLog,
12 : : logError: nativeLogError,
13 : : setPrettyPrintFunction,
14 : 48 : } = imports._print;
15 : :
16 : 3 : function log(...args) {
17 [ - + ]: 6 : return nativeLog(args.map(arg => typeof arg === 'string' ? arg : prettyPrint(arg)).join(' '));
18 : : }
19 : :
20 : 69 : function logError(e, ...args) {
21 [ + + ]: 69 : if (args.length === 0)
22 : 52 : return nativeLogError(e);
23 [ - + ]: 34 : return nativeLogError(e, args.map(arg => typeof arg === 'string' ? arg : prettyPrint(arg)).join(' '));
24 : : }
25 : :
26 : : // compare against the %TypedArray% intrinsic object all typed array constructors inherit from
27 : 132 : function _isTypedArray(value) {
28 : 132 : return value instanceof Object.getPrototypeOf(Uint8Array);
29 : : }
30 : :
31 : 135 : function _hasStandardToString(value) {
32 [ + + ]: 135 : return value.toString === Object.prototype.toString ||
33 [ + + ]: 73 : value.toString === Array.prototype.toString ||
34 : : // although TypedArrays have a standard Array.prototype.toString, we currently enforce an override to warn
35 : : // for legacy behaviour, making the toString non-standard for
36 : : // "any Uint8Array instances created in situations where previously a ByteArray would have been created"
37 [ + + ]: 53 : _isTypedArray(value) ||
38 : 52 : value.toString === Date.prototype.toString;
39 : : }
40 : :
41 : 108 : function prettyPrint(value, extra) {
42 [ + + ][ + + ]: 108 : switch (typeof value) {
[ - + ][ + + ]
[ + + ]
43 : : case 'object':
44 [ + + ]: 42 : if (value === null)
45 : 1 : return 'null';
46 : :
47 [ + + ]: 41 : if (_hasStandardToString(value))
48 : 37 : return formatObject(value, extra);
49 : :
50 [ + + ]: 4 : if (!value.toString) {
51 : 2 : let str = formatObject(value, extra);
52 : : // object has null prototype either from Object.create(null) or cases like the module namespace object
53 [ + + ]: 2 : if (Object.getPrototypeOf(value) === null)
54 : 1 : str = `[Object: null prototype] ${str}`;
55 : :
56 : 2 : return str;
57 : : }
58 : :
59 : : // Prefer the non-standard toString
60 : 2 : return value.toString();
61 : : case 'function':
62 [ - + ]: 1 : if (value.toString === Function.prototype.toString)
63 : 1 : return formatFunction(value);
64 : 0 : return value.toString();
65 : : case 'string':
66 : 0 : return JSON.stringify(value);
67 : : case 'symbol':
68 : 3 : return formatSymbol(value);
69 : : case 'undefined':
70 : 1 : return 'undefined';
71 : : default:
72 : 61 : return value.toString();
73 : : }
74 : 2 : }
75 : :
76 : 1595 : function formatPropertyKey(key) {
77 [ + + ]: 1595 : if (typeof key === 'symbol')
78 : 3 : return `[${formatSymbol(key)}]`;
79 : 1592 : return `${key}`;
80 : : }
81 : :
82 [ + + ]: 65 : function formatObject(obj, properties, printedObjects = new WeakSet()) {
83 : 65 : printedObjects.add(obj);
84 [ + + ][ + + ]: 65 : if (Array.isArray(obj) || _isTypedArray(obj))
85 : 8 : return formatArray(obj, properties, printedObjects).toString();
86 : :
87 [ + + ]: 57 : if (obj instanceof Date)
88 : 2 : return formatDate(obj);
89 : :
90 : 55 : const formattedObject = [];
91 : 1603 : let keys = Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj)).map(k => [k, obj[k]]);
92 : : // properties is only passed to us in the debugger
93 [ + - ][ + - ]: 55 : if (properties?.cur)
94 : 0 : keys = keys.concat(properties.cur);
95 : :
96 [ + + ][ - + ]: 3067 : for (const [propertyKey, value] of keys) {
[ - + ][ - + ]
[ # # ][ # # ]
[ # # ][ # # ]
97 : 3004 : const key = formatPropertyKey(propertyKey);
98 [ + + ][ + + ]: 3004 : switch (typeof value) {
[ + + ][ - + ]
99 : : case 'object':
100 [ + + ]: 95 : if (printedObjects.has(value))
101 : 2 : formattedObject.push(`${key}: [Circular]`);
102 [ + + ]: 93 : else if (value === null)
103 : 2 : formattedObject.push(`${key}: null`);
104 [ + + ]: 93 : else if (_hasStandardToString(value))
105 [ + - ]: 48 : formattedObject.push(`${key}: ${formatObject(value, properties?.children[propertyKey], printedObjects)}`);
106 : : else
107 : 46 : formattedObject.push(`${key}: ${value.toString()}`);
108 : : break;
109 : : case 'function':
110 : 164 : formattedObject.push(`${key}: ${formatFunction(value)}`);
111 : : break;
112 : : case 'string':
113 : 4 : formattedObject.push(`${key}: "${value}"`);
114 : : break;
115 : : case 'symbol':
116 : 3 : formattedObject.push(`${key}: ${formatSymbol(value)}`);
117 : : break;
118 : : default:
119 : 2743 : formattedObject.push(`${key}: ${value}`);
120 : : break;
121 : : }
122 : : }
123 [ + - ]: 55 : return Object.keys(formattedObject).length === 0 ? '{}'
124 : 55 : : `{ ${formattedObject.join(', ')} }`;
125 : 65 : }
126 : :
127 : 21 : function formatArray(arr, properties, printedObjects) {
128 : 21 : const formattedArray = [];
129 [ + + ][ - + ]: 86 : for (const [key, value] of arr.entries()) {
[ - + ][ - + ]
[ # # ][ # # ]
[ # # ][ # # ]
130 [ + + ]: 65 : if (printedObjects.has(value))
131 : 2 : formattedArray[key] = '[Circular]';
132 : : else
133 [ + - ]: 63 : formattedArray[key] = prettyPrint(value, properties?.children[key]);
134 : : }
135 : 21 : return `[${formattedArray.join(', ')}]`;
136 : 21 : }
137 : :
138 : 2 : function formatDate(date) {
139 : 2 : return date.toISOString();
140 : : }
141 : :
142 : 165 : function formatFunction(func) {
143 : 165 : let funcOutput = `[ Function: ${func.name} ]`;
144 : 165 : return funcOutput;
145 : 165 : }
146 : :
147 : 9 : function formatSymbol(sym) {
148 : : // Try to format Symbols in the same way that they would be constructed.
149 : :
150 : : // First check if this is a global registered symbol
151 : 9 : const globalKey = Symbol.keyFor(sym);
152 [ + + ]: 9 : if (globalKey !== undefined)
153 : 3 : return `Symbol.for("${globalKey}")`;
154 : :
155 : 6 : const descr = sym.description;
156 : : // Special-case the 'well-known' (built-in) Symbols
157 [ + + ]: 6 : if (descr.startsWith('Symbol.'))
158 : 3 : return descr;
159 : :
160 : : // Otherwise, it's just a regular symbol
161 : 3 : return `Symbol("${descr}")`;
162 : 9 : }
163 : :
164 : 96 : Object.defineProperties(exports, {
165 : 48 : ARGV: {
166 : 48 : configurable: false,
167 : 48 : enumerable: true,
168 : 48 : get() {
169 : : // Wait until after bootstrap or programArgs won't be set.
170 : 3 : return imports.system.programArgs;
171 : : },
172 : : },
173 : 48 : print: {
174 : 48 : configurable: false,
175 : 48 : enumerable: true,
176 : 48 : writable: true,
177 : 48 : value: print,
178 : : },
179 : 48 : printerr: {
180 : 48 : configurable: false,
181 : 48 : enumerable: true,
182 : 48 : writable: true,
183 : 48 : value: printerr,
184 : : },
185 : 48 : log: {
186 : 48 : configurable: false,
187 : 48 : enumerable: true,
188 : 48 : writable: true,
189 : 48 : value: log,
190 : : },
191 : 48 : logError: {
192 : 48 : configurable: false,
193 : 48 : enumerable: true,
194 : 48 : writable: true,
195 : 48 : value: logError,
196 : : },
197 : : });
198 : 48 : setPrettyPrintFunction(exports, prettyPrint);
199 : 48 : })(globalThis);
|