[gjs: 1/2] Add check for GObject introspected objects to pretty-print




commit 115867d662672997d8e155b99d71056baea2d1be
Author: Nasah Kuma <nasahnash19 gmail com>
Date:   Fri Apr 29 21:15:08 2022 +0100

    Add check for GObject introspected objects to pretty-print

 installed-tests/js/testPrint.js      | 14 ++++++++++++++
 modules/script/_bootstrap/default.js | 21 ++++++++++++++-------
 2 files changed, 28 insertions(+), 7 deletions(-)
---
diff --git a/installed-tests/js/testPrint.js b/installed-tests/js/testPrint.js
index f6d274057..9b582c28c 100644
--- a/installed-tests/js/testPrint.js
+++ b/installed-tests/js/testPrint.js
@@ -3,6 +3,8 @@
 // SPDX-FileCopyrightText: 2022 Nasah Kuma <nasahnash19 gmail com>
 
 const GLib = imports.gi.GLib;
+imports.gi.versions.Gdk = '3.0';
+const Gdk = imports.gi.Gdk;
 
 describe('print', function () {
     it('can be spied upon', function () {
@@ -142,4 +144,16 @@ describe('prettyPrint', function () {
             'JS LOG: { date: 2018-12-24T10:33:30.000Z }');
         log({date: new Date(Date.UTC(2018, 11, 24, 10, 33, 30))});
     });
+
+    it('toString is overridden on object', function () {
+        GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+            'JS LOG: [boxed instance wrapper GIName:*]');
+        log(new Gdk.Rectangle());
+    });
+
+    it('string tag supplied', function () {
+        GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+            'JS LOG: [object GIRepositoryNamespace]');
+        log(Gdk);
+    });
 });
diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js
index 8c211a5ac..e5543df3a 100644
--- a/modules/script/_bootstrap/default.js
+++ b/modules/script/_bootstrap/default.js
@@ -24,13 +24,17 @@
     }
 
     function prettyPrint(value) {
-        const printedObjects = new WeakSet();
-        switch (typeof value) {
-        case 'object':
-            return formatObject(value, printedObjects);
-        case 'function':
-            return formatFunction(value);
-        default:
+        if (value.toString === Object.prototype.toString || value.toString === Array.prototype.toString || 
value.toString === Function.prototype.toString || value.toString === Date.prototype.toString) {
+            const printedObjects = new WeakSet();
+            switch (typeof value) {
+            case 'object':
+                return formatObject(value, printedObjects);
+            case 'function':
+                return formatFunction(value);
+            default:
+                return value.toString();
+            }
+        } else {
             return value.toString();
         }
     }
@@ -43,6 +47,9 @@
         if (obj instanceof Date)
             return formatDate(obj);
 
+        if (obj[Symbol.toStringTag] === 'GIRepositoryNamespace')
+            return obj.toString();
+
         const formattedObject = [];
         for (const [key, value] of Object.entries(obj)) {
             switch (typeof value) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]