summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Chimento <philip.chimento@gmail.com>2022-03-24 04:09:05 +0000
committerPhilip Chimento <philip.chimento@gmail.com>2022-03-24 04:09:05 +0000
commit46c761e9196fc01fd6622196297a6446a5a999e3 (patch)
tree0c6a998e52f75f9255e757cdfd88a102f7d119a6
parent39ff973b89c6d4d30bfc8e34f7eddce532acbb3f (diff)
parentfbd0bb6b8d417520faacb15ee9ffde6f6d176923 (diff)
downloadgjs-46c761e9196fc01fd6622196297a6446a5a999e3.tar.gz
Merge branch 'handle-reference-cycles-in-pretty-print-function' into 'master'
Handle reference cycles in pretty print function Closes #469 See merge request GNOME/gjs!739
-rw-r--r--installed-tests/js/testPrint.js35
-rw-r--r--modules/script/_bootstrap/default.js24
2 files changed, 52 insertions, 7 deletions
diff --git a/installed-tests/js/testPrint.js b/installed-tests/js/testPrint.js
index 50a4ebc2..f6d27405 100644
--- a/installed-tests/js/testPrint.js
+++ b/installed-tests/js/testPrint.js
@@ -48,12 +48,29 @@ describe('prettyPrint', function () {
log({greeting: 'hi'});
});
+ it('property value is object reference', function () {
+ GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+ 'JS LOG: { a: 5, b: [Circular] }');
+ let obj = {a: 5};
+ obj.b = obj;
+ log(obj);
+ });
+
it('more than one property', function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
'JS LOG: { a: 1, b: 2, c: 3 }');
log({a: 1, b: 2, c: 3});
});
+ it('add property value after property value object reference', function () {
+ GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+ 'JS LOG: { a: 5, b: [Circular], c: 4 }');
+ let obj = {a: 5};
+ obj.b = obj;
+ obj.c = 4;
+ log(obj);
+ });
+
it('array', function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
'JS LOG: [1, 2, 3, 4, 5]');
@@ -66,6 +83,24 @@ describe('prettyPrint', function () {
log({arr: [1, 2, 3, 4, 5]});
});
+ it('array reference is the only array element', function () {
+ GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+ 'JS LOG: [[Circular]]');
+ let arr = [];
+ arr.push(arr);
+ log(arr);
+ });
+
+ it('array reference is one of multiple array elements', function () {
+ GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
+ 'JS LOG: [4, [Circular], 5]');
+ let arr = [];
+ arr.push(4);
+ arr.push(arr);
+ arr.push(5);
+ log(arr);
+ });
+
it('nested array', function () {
GLib.test_expect_message('Gjs', GLib.LogLevelFlags.LEVEL_MESSAGE,
'JS LOG: [1, 2, [3, 4], 5]');
diff --git a/modules/script/_bootstrap/default.js b/modules/script/_bootstrap/default.js
index e5953bf3..8c211a5a 100644
--- a/modules/script/_bootstrap/default.js
+++ b/modules/script/_bootstrap/default.js
@@ -24,9 +24,10 @@
}
function prettyPrint(value) {
+ const printedObjects = new WeakSet();
switch (typeof value) {
case 'object':
- return formatObject(value);
+ return formatObject(value, printedObjects);
case 'function':
return formatFunction(value);
default:
@@ -34,9 +35,11 @@
}
}
- function formatObject(obj) {
+ function formatObject(obj, printedObjects) {
+ printedObjects.add(obj);
if (Array.isArray(obj))
- return formatArray(obj).toString();
+ return formatArray(obj, printedObjects).toString();
+
if (obj instanceof Date)
return formatDate(obj);
@@ -44,7 +47,10 @@
for (const [key, value] of Object.entries(obj)) {
switch (typeof value) {
case 'object':
- formattedObject.push(`${key}: ${formatObject(value)}`);
+ if (printedObjects.has(value))
+ formattedObject.push(`${key}: [Circular]`);
+ else
+ formattedObject.push(`${key}: ${formatObject(value, printedObjects)}`);
break;
case 'function':
formattedObject.push(`${key}: ${formatFunction(value)}`);
@@ -60,10 +66,14 @@
return `{ ${formattedObject.join(', ')} }`;
}
- function formatArray(arr) {
+ function formatArray(arr, printedObjects) {
const formattedArray = [];
- for (const [key, value] of arr.entries())
- formattedArray[key] = prettyPrint(value);
+ for (const [key, value] of arr.entries()) {
+ if (printedObjects.has(value))
+ formattedArray[key] = '[Circular]';
+ else
+ formattedArray[key] = prettyPrint(value);
+ }
return `[${formattedArray.join(', ')}]`;
}