@@ -2,6 +2,17 @@ import OpcodeLabels from './opcode-labels.js';
2
2
3
3
const isUndefined = a => typeof a === 'undefined' ;
4
4
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
+
5
16
/**
6
17
* Convert monitors from VM format to what the GUI needs to render.
7
18
* - Convert opcode to a label and a category
@@ -32,18 +43,26 @@ export default function ({id, spriteName, opcode, params, value, vm}) {
32
43
value = Number ( value . toFixed ( 6 ) ) ;
33
44
}
34
45
35
- // Turn the value to a string, for handle boolean values
46
+ // Turn the value to a string, to handle boolean values
36
47
if ( typeof value === 'boolean' ) {
37
48
value = value . toString ( ) ;
38
49
}
39
50
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
41
58
if ( Array . isArray ( value ) ) {
42
59
value = value . slice ( ) ;
43
60
for ( let i = 0 ; i < value . length ; i ++ ) {
44
61
const item = value [ i ] ;
45
62
if ( typeof item === 'boolean' ) {
46
63
value [ i ] = item . toString ( ) ;
64
+ } else if ( typeof value [ i ] === 'object' && ! Array . isArray ( value [ i ] ) ) {
65
+ value [ i ] = JSON . stringify ( item , circularReplacer ( ) ) ;
47
66
}
48
67
}
49
68
}
0 commit comments