Skip to content

Commit 97ab82e

Browse files
committed
Merge branch 'invalid-search-paths-imports-gi' into 'master'
Fix printing imports.gi Closes #629 See merge request GNOME/gjs!935
2 parents e0e48de + 96283db commit 97ab82e

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

gjs/importer.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
#include <js/ComparisonOperators.h>
2525
#include <js/ErrorReport.h> // for JS_ReportOutOfMemory, JSEXN_ERR
2626
#include <js/Exception.h>
27+
#include <js/GCVector.h> // for StackGCVector
2728
#include <js/GlobalObject.h> // for CurrentGlobalOrNull
28-
#include <js/Id.h> // for PropertyKey
29-
#include <js/Object.h> // for GetClass
29+
#include <js/Id.h> // for PropertyKey
30+
#include <js/Object.h> // for GetClass
3031
#include <js/PropertyAndElement.h>
3132
#include <js/PropertyDescriptor.h>
3233
#include <js/PropertySpec.h>
@@ -671,7 +672,8 @@ static bool importer_new_enumerate(JSContext* context, JS::HandleObject object,
671672
while (true) {
672673
GFileInfo *info;
673674
GFile *file;
674-
if (!g_file_enumerator_iterate(direnum, &info, &file, NULL, NULL))
675+
if (!direnum ||
676+
!g_file_enumerator_iterate(direnum, &info, &file, NULL, NULL))
675677
break;
676678
if (info == NULL || file == NULL)
677679
break;

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)