diff --git a/installed-tests/js/testPrint.js b/installed-tests/js/testPrint.js index f6d2740574d56cf4d8df3822fb97a33ab98d9915..9b582c28c3d32a30b743f055bd853518eee8e004 100644 --- a/installed-tests/js/testPrint.js +++ b/installed-tests/js/testPrint.js @@ -3,6 +3,8 @@ // SPDX-FileCopyrightText: 2022 Nasah Kuma 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 8c211a5aceb151420f91a0e16cfaace33b5f3191..e5543df3a2a712b14a5b5e39c1e92b34bc8e23fe 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) {