@@ -2,6 +2,17 @@ import OpcodeLabels from './opcode-labels.js';
22
33const isUndefined = a => typeof a === 'undefined' ;
44
5+ const circularReplacer = ( ) => {
6+ const stack = new Set ( ) ;
7+ return ( _ , value ) => {
8+ if ( typeof value === 'object' && value !== null ) {
9+ if ( stack . has ( value ) ) return Array . isArray ( value ) ? '[...]' : '{...}' ;
10+ stack . add ( value ) ;
11+ }
12+ return value ;
13+ } ;
14+ } ;
15+
516/**
617 * Convert monitors from VM format to what the GUI needs to render.
718 * - Convert opcode to a label and a category
@@ -32,18 +43,26 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
3243 value = Number ( value . toFixed ( 6 ) ) ;
3344 }
3445
35- // Turn the value to a string, for handle boolean values
46+ // Turn the value to a string, to handle boolean values
3647 if ( typeof value === 'boolean' ) {
3748 value = value . toString ( ) ;
3849 }
3950
40- // Lists can contain booleans, which should also be turned to strings
51+ // Turn the value to a string, to handle JSON values
52+ // do not convert arrays as it will be confused for lists
53+ if ( typeof value === 'object' && ! Array . isArray ( value ) ) {
54+ value = JSON . stringify ( value , circularReplacer ( ) ) ;
55+ }
56+
57+ // Lists can contain booleans or Objects, which should also be turned to strings
4158 if ( Array . isArray ( value ) ) {
4259 value = value . slice ( ) ;
4360 for ( let i = 0 ; i < value . length ; i ++ ) {
4461 const item = value [ i ] ;
4562 if ( typeof item === 'boolean' ) {
4663 value [ i ] = item . toString ( ) ;
64+ } else if ( typeof value [ i ] === 'object' && ! Array . isArray ( value [ i ] ) ) {
65+ value [ i ] = JSON . stringify ( item , circularReplacer ( ) ) ;
4766 }
4867 }
4968 }
0 commit comments