Branch data Line data Source code
1 : : // 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 : 94 : (function (exports) {
6 : : 'use strict';
7 : :
8 : : const {
9 : : print,
10 : : printerr,
11 : : log: nativeLog,
12 : : logError: nativeLogError,
13 : : setPrettyPrintFunction,
14 : 47 : } = 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 : 68 : function logError(e, ...args) {
21 [ + + ]: 68 : if (args.length === 0)
22 : 51 : return nativeLogError(e);
23 [ - + ]: 34 : return nativeLogError(e, args.map(arg => typeof arg === 'string' ? arg : prettyPrint(arg)).join(' '));
24 : : }
25 : :
26 : : function prettyPrint(value) {
27 [ + + ][ + + ]: 51 : switch (typeof value) {
[ - + ][ + + ]
[ + + ]
28 : : case 'object':
29 [ + + ]: 24 : if (value === null)
30 : 1 : return 'null';
31 : :
32 [ + + ]: 23 : if (value.toString === Object.prototype.toString ||
33 [ + + ]: 8 : value.toString === Array.prototype.toString ||
34 [ + + ]: 2 : value.toString === Date.prototype.toString) {
35 : 22 : const printedObjects = new WeakSet();
36 : 22 : return formatObject(value, printedObjects);
37 : : }
38 : : // If the object has a nonstandard toString, prefer that
39 : 1 : return value.toString();
40 : : case 'function':
41 [ - + ]: 1 : if (value.toString === Function.prototype.toString)
42 : 1 : return formatFunction(value);
43 : 0 : return value.toString();
44 : : case 'string':
45 : 0 : return JSON.stringify(value);
46 : : case 'symbol':
47 : 3 : return formatSymbol(value);
48 : : case 'undefined':
49 : 1 : return 'undefined';
50 : : default:
51 : 22 : return value.toString();
52 : : }
53 : 22 : }
54 : :
55 : : function formatPropertyKey(key) {
56 [ + + ]: 19 : if (typeof key === 'symbol')
57 : 3 : return `[${formatSymbol(key)}]`;
58 : 16 : return `${key}`;
59 : : }
60 : :
61 : 25 : function formatObject(obj, printedObjects) {
62 : 25 : printedObjects.add(obj);
63 [ + + ]: 25 : if (Array.isArray(obj))
64 : 8 : return formatArray(obj, printedObjects).toString();
65 : :
66 [ + + ]: 17 : if (obj instanceof Date)
67 : 2 : return formatDate(obj);
68 : :
69 [ + + ]: 15 : if (obj[Symbol.toStringTag] === 'GIRepositoryNamespace')
70 : 1 : return obj.toString();
71 : :
72 : 14 : const formattedObject = [];
73 : 14 : const keys = Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj));
74 [ + + ]: 33 : for (const propertyKey of keys) {
75 : 19 : const value = obj[propertyKey];
76 : 19 : const key = formatPropertyKey(propertyKey);
77 [ + + ][ + + ]: 19 : switch (typeof value) {
[ + + ][ + + ]
78 : : case 'object':
79 [ + + ]: 5 : if (printedObjects.has(value))
80 : 2 : formattedObject.push(`${key}: [Circular]`);
81 : : else
82 : 3 : formattedObject.push(`${key}: ${formatObject(value, printedObjects)}`);
83 : : break;
84 : : case 'function':
85 : 1 : formattedObject.push(`${key}: ${formatFunction(value)}`);
86 : : break;
87 : : case 'string':
88 : 4 : formattedObject.push(`${key}: "${value}"`);
89 : : break;
90 : : case 'symbol':
91 : 3 : formattedObject.push(`${key}: ${formatSymbol(value)}`);
92 : : break;
93 : : default:
94 : 6 : formattedObject.push(`${key}: ${value}`);
95 : : break;
96 : : }
97 : : }
98 [ + - ]: 14 : return Object.keys(formattedObject).length === 0 ? '{}'
99 : 14 : : `{ ${formattedObject.join(', ')} }`;
100 : 25 : }
101 : :
102 : 8 : function formatArray(arr, printedObjects) {
103 : 8 : const formattedArray = [];
104 [ + + ][ + - ]: 34 : for (const [key, value] of arr.entries()) {
[ + - ][ + - ]
[ + - ]
105 [ + + ]: 26 : if (printedObjects.has(value))
106 : 2 : formattedArray[key] = '[Circular]';
107 : : else
108 : 24 : formattedArray[key] = prettyPrint(value);
109 : : }
110 : 8 : return `[${formattedArray.join(', ')}]`;
111 : 8 : }
112 : :
113 : : function formatDate(date) {
114 : 2 : return date.toISOString();
115 : : }
116 : :
117 : 2 : function formatFunction(func) {
118 : 2 : let funcOutput = `[ Function: ${func.name} ]`;
119 : 2 : return funcOutput;
120 : 2 : }
121 : :
122 : 9 : function formatSymbol(sym) {
123 : : // Try to format Symbols in the same way that they would be constructed.
124 : :
125 : : // First check if this is a global registered symbol
126 : 9 : const globalKey = Symbol.keyFor(sym);
127 [ + + ]: 9 : if (globalKey !== undefined)
128 : 3 : return `Symbol.for("${globalKey}")`;
129 : :
130 : 6 : const descr = sym.description;
131 : : // Special-case the 'well-known' (built-in) Symbols
132 [ + + ]: 6 : if (descr.startsWith('Symbol.'))
133 : 3 : return descr;
134 : :
135 : : // Otherwise, it's just a regular symbol
136 : 3 : return `Symbol("${descr}")`;
137 : 9 : }
138 : :
139 : 94 : Object.defineProperties(exports, {
140 : 47 : ARGV: {
141 : 47 : configurable: false,
142 : 47 : enumerable: true,
143 : 47 : get() {
144 : : // Wait until after bootstrap or programArgs won't be set.
145 : 3 : return imports.system.programArgs;
146 : : },
147 : : },
148 : 47 : print: {
149 : 47 : configurable: false,
150 : 47 : enumerable: true,
151 : 47 : writable: true,
152 : 47 : value: print,
153 : : },
154 : 47 : printerr: {
155 : 47 : configurable: false,
156 : 47 : enumerable: true,
157 : 47 : writable: true,
158 : 47 : value: printerr,
159 : : },
160 : 47 : log: {
161 : 47 : configurable: false,
162 : 47 : enumerable: true,
163 : 47 : writable: true,
164 : 47 : value: log,
165 : : },
166 : 47 : logError: {
167 : 47 : configurable: false,
168 : 47 : enumerable: true,
169 : 47 : writable: true,
170 : 47 : value: logError,
171 : : },
172 : : });
173 : 47 : setPrettyPrintFunction(exports, prettyPrint);
174 : 47 : })(globalThis);
|