Skip to content

Commit 96283db

Browse files
committed
prettyPrint: handle more cases in nested objects
GJS logs warnings if we pretty print an object containing null as a value. This is because a WeakSet cannot take in null values but null has typeof object. GJS also does not print [GjsFileImporter root] objects if it is nested within another object, such as for imports.gi. This is because the logic on whether to call toString differs between prettyPrint and formatObject. Guard explicitly against null and apply the same logic to formatObject as were in prettyPrint.
1 parent 1c5d500 commit 96283db

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

installed-tests/js/testPrint.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,13 @@ describe('prettyPrint', function () {
202202
it('null', function () {
203203
expect(prettyPrint(null)).toEqual('null');
204204
});
205+
206+
it('nested null', function () {
207+
expect(prettyPrint({'foo': null})).toEqual('{ foo: null }');
208+
});
209+
210+
it('imports root in object', function () {
211+
expect(prettyPrint({'foo': imports}))
212+
.toEqual('{ foo: [GjsFileImporter root] }');
213+
});
205214
});

modules/script/_bootstrap/default.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@
2323
return nativeLogError(e, args.map(arg => typeof arg === 'string' ? arg : prettyPrint(arg)).join(' '));
2424
}
2525

26+
function _hasStandardToString(value) {
27+
return value.toString === Object.prototype.toString ||
28+
value.toString === Array.prototype.toString ||
29+
value.toString === Date.prototype.toString;
30+
}
31+
2632
function prettyPrint(value) {
2733
switch (typeof value) {
2834
case 'object':
2935
if (value === null)
3036
return 'null';
3137

32-
if (value.toString === Object.prototype.toString ||
33-
value.toString === Array.prototype.toString ||
34-
value.toString === Date.prototype.toString) {
38+
if (_hasStandardToString(value)) {
3539
const printedObjects = new WeakSet();
3640
return formatObject(value, printedObjects);
3741
}
@@ -66,9 +70,6 @@
6670
if (obj instanceof Date)
6771
return formatDate(obj);
6872

69-
if (obj[Symbol.toStringTag] === 'GIRepositoryNamespace' || obj[Symbol.toStringTag] === 'GObject_ParamSpec')
70-
return obj.toString();
71-
7273
const formattedObject = [];
7374
const keys = Object.getOwnPropertyNames(obj).concat(Object.getOwnPropertySymbols(obj));
7475
for (const propertyKey of keys) {
@@ -78,8 +79,12 @@
7879
case 'object':
7980
if (printedObjects.has(value))
8081
formattedObject.push(`${key}: [Circular]`);
81-
else
82+
else if (value === null)
83+
formattedObject.push(`${key}: null`);
84+
else if (_hasStandardToString(value))
8285
formattedObject.push(`${key}: ${formatObject(value, printedObjects)}`);
86+
else
87+
formattedObject.push(`${key}: ${value.toString()}`);
8388
break;
8489
case 'function':
8590
formattedObject.push(`${key}: ${formatFunction(value)}`);

0 commit comments

Comments
 (0)