From 0c2fe06d88ced58f52681ab73cd2650ebc0fca18 Mon Sep 17 00:00:00 2001 From: SharkPool <139097378+SharkPool-SP@users.noreply.github.com> Date: Fri, 23 May 2025 23:08:58 -0700 Subject: [PATCH 1/5] monitor-adapter.js -- implement JSON support --- src/lib/monitor-adapter.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib/monitor-adapter.js b/src/lib/monitor-adapter.js index 7f470ff2e49..1ac9dff2030 100644 --- a/src/lib/monitor-adapter.js +++ b/src/lib/monitor-adapter.js @@ -32,18 +32,26 @@ export default function ({id, spriteName, opcode, params, value, vm}) { value = Number(value.toFixed(6)); } - // Turn the value to a string, for handle boolean values + // Turn the value to a string, to handle boolean values if (typeof value === 'boolean') { value = value.toString(); } - // Lists can contain booleans, which should also be turned to strings + // Turn the value to a string, to handle Object values + // arrays will be confused for lists if we use 'typeof' + if (value.constructor.name === 'Object') { + value = JSON.stringify(value); + } + + // Lists can contain booleans or Objects, which should also be turned to strings if (Array.isArray(value)) { value = value.slice(); for (let i = 0; i < value.length; i++) { const item = value[i]; if (typeof item === 'boolean') { value[i] = item.toString(); + } else if (value.constructor.name === 'Object') { + value[i] = JSON.stringify(item); } } } From 6bd96399a904b4daf9672b04207ef020a4b820c3 Mon Sep 17 00:00:00 2001 From: SharkPool <139097378+SharkPool-SP@users.noreply.github.com> Date: Sat, 24 May 2025 22:56:02 -0700 Subject: [PATCH 2/5] this is why you dont code at 1 am, yet, I do it again --- src/lib/monitor-adapter.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/monitor-adapter.js b/src/lib/monitor-adapter.js index 1ac9dff2030..0e9ff6c1157 100644 --- a/src/lib/monitor-adapter.js +++ b/src/lib/monitor-adapter.js @@ -2,6 +2,17 @@ import OpcodeLabels from './opcode-labels.js'; const isUndefined = a => typeof a === 'undefined'; +const circularReplacer = () => { + const stack = new Set(); + return function replacer(_, value) { + if (typeof value === "object" && value !== null) { + if (stack.has(value)) return "{...}"; + stack.add(value); + } + return value; + }; +} + /** * Convert monitors from VM format to what the GUI needs to render. * - Convert opcode to a label and a category @@ -40,7 +51,7 @@ export default function ({id, spriteName, opcode, params, value, vm}) { // Turn the value to a string, to handle Object values // arrays will be confused for lists if we use 'typeof' if (value.constructor.name === 'Object') { - value = JSON.stringify(value); + value = JSON.stringify(value, circularReplacer); } // Lists can contain booleans or Objects, which should also be turned to strings @@ -50,8 +61,8 @@ export default function ({id, spriteName, opcode, params, value, vm}) { const item = value[i]; if (typeof item === 'boolean') { value[i] = item.toString(); - } else if (value.constructor.name === 'Object') { - value[i] = JSON.stringify(item); + } else if (value[i].constructor.name === 'Object') { + value[i] = JSON.stringify(item, circularReplacer); } } } From 0d1fff83f2cb079a9c8de02c0b2478b2a65ce21d Mon Sep 17 00:00:00 2001 From: SharkPool <139097378+SharkPool-SP@users.noreply.github.com> Date: Mon, 26 May 2025 20:17:04 -0700 Subject: [PATCH 3/5] Update monitor-adapter.js --- src/lib/monitor-adapter.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/monitor-adapter.js b/src/lib/monitor-adapter.js index 0e9ff6c1157..76d4d98b703 100644 --- a/src/lib/monitor-adapter.js +++ b/src/lib/monitor-adapter.js @@ -4,14 +4,14 @@ const isUndefined = a => typeof a === 'undefined'; const circularReplacer = () => { const stack = new Set(); - return function replacer(_, value) { + return function replacer(key, value) { if (typeof value === "object" && value !== null) { - if (stack.has(value)) return "{...}"; + if (stack.has(value)) return Array.isArray(value) ? "[...]" : "{...}"; stack.add(value); } return value; }; -} +}; /** * Convert monitors from VM format to what the GUI needs to render. @@ -48,9 +48,9 @@ export default function ({id, spriteName, opcode, params, value, vm}) { value = value.toString(); } - // Turn the value to a string, to handle Object values - // arrays will be confused for lists if we use 'typeof' - if (value.constructor.name === 'Object') { + // Turn the value to a string, to handle JSON values + // do not convert arrays as it will be confused for lists + if (typeof value === 'object' && !Array.isArray(value)) { value = JSON.stringify(value, circularReplacer); } @@ -61,7 +61,7 @@ export default function ({id, spriteName, opcode, params, value, vm}) { const item = value[i]; if (typeof item === 'boolean') { value[i] = item.toString(); - } else if (value[i].constructor.name === 'Object') { + } else if (typeof value[i] === 'object' && !Array.isArray(value[i])) { value[i] = JSON.stringify(item, circularReplacer); } } From 80e2b0a309df78e5b51b4d93bc7b4ca236e7924f Mon Sep 17 00:00:00 2001 From: SharkPool <139097378+SharkPool-SP@users.noreply.github.com> Date: Mon, 26 May 2025 20:28:56 -0700 Subject: [PATCH 4/5] oops --- src/lib/monitor-adapter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/monitor-adapter.js b/src/lib/monitor-adapter.js index 76d4d98b703..da90b30cb83 100644 --- a/src/lib/monitor-adapter.js +++ b/src/lib/monitor-adapter.js @@ -4,7 +4,7 @@ const isUndefined = a => typeof a === 'undefined'; const circularReplacer = () => { const stack = new Set(); - return function replacer(key, value) { + return function replacer(_, value) { if (typeof value === "object" && value !== null) { if (stack.has(value)) return Array.isArray(value) ? "[...]" : "{...}"; stack.add(value); From afc0a437e4171eea6a1fb70f9c489224d77a824a Mon Sep 17 00:00:00 2001 From: SharkPool <139097378+SharkPool-SP@users.noreply.github.com> Date: Thu, 29 May 2025 20:40:20 -0700 Subject: [PATCH 5/5] should call circular replacer to actually set --- src/lib/monitor-adapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/monitor-adapter.js b/src/lib/monitor-adapter.js index da90b30cb83..bcef430498d 100644 --- a/src/lib/monitor-adapter.js +++ b/src/lib/monitor-adapter.js @@ -51,7 +51,7 @@ export default function ({id, spriteName, opcode, params, value, vm}) { // Turn the value to a string, to handle JSON values // do not convert arrays as it will be confused for lists if (typeof value === 'object' && !Array.isArray(value)) { - value = JSON.stringify(value, circularReplacer); + value = JSON.stringify(value, circularReplacer()); } // Lists can contain booleans or Objects, which should also be turned to strings @@ -62,7 +62,7 @@ export default function ({id, spriteName, opcode, params, value, vm}) { if (typeof item === 'boolean') { value[i] = item.toString(); } else if (typeof value[i] === 'object' && !Array.isArray(value[i])) { - value[i] = JSON.stringify(item, circularReplacer); + value[i] = JSON.stringify(item, circularReplacer()); } } }