File size: 3,301 Bytes
6bcb42f
 
 
 
f3442a0
 
 
 
 
 
 
 
 
 
 
6bcb42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f3442a0
6bcb42f
 
 
 
 
 
 
 
f3442a0
 
 
 
 
 
 
6bcb42f
 
 
 
 
f3442a0
 
 
 
 
 
 
 
 
 
6bcb42f
 
 
f3442a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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 Array.isArray(value) ? "[...]" : "{...}";
            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
 * @param {string} block.id - The id of the monitor block
 * @param {string} block.spriteName - Present only if the monitor applies only to the sprite
 *     with given target ID. The name of the target sprite when the monitor was created
 * @param {string} block.opcode - The opcode of the monitor
 * @param {object} block.params - Extra params to the monitor block
 * @param {string|number|Array} block.value - The monitor value
 * @param {VirtualMachine} block.vm - the VM instance which owns the block
 * @return {object} The adapted monitor with label and category
 */
export default function ({id, spriteName, opcode, params, value, vm}) {
    // Extension monitors get their labels from the Runtime through `getLabelForOpcode`.
    // Other monitors' labels are hard-coded in `OpcodeLabels`.
    let {label, category, labelFn} = (vm && vm.runtime.getLabelForOpcode(opcode)) || OpcodeLabels.getLabel(opcode);

    // Use labelFn if provided for dynamic labelling (e.g. variables)
    if (!isUndefined(labelFn)) label = labelFn(params);

    // Append sprite name for sprite-specific monitors
    if (spriteName) {
        label = `${spriteName}: ${label}`;
    }

    // If value is a number, round it to six decimal places
    if (typeof value === 'number') {
        value = Number(value.toFixed(6));
    }

    // Turn the value to a string, for handle boolean values
    if (typeof value === 'boolean') {
        value = value.toString();
    }
    
    // Lists can contain booleans, 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();
            }
            if (typeof item === 'object') {
                // check if this is a pure object or custom display
                if (typeof (item.toListItem || value.toMonitorContent || item.toReporterContent) === 'function') {
                    value[i].isHTML = true;
                } else {
                    value[i] = JSON.stringify(item, circularReplacer());
                }
            }
        }
    }

    let isHTML = false;
    if (typeof value === 'object') {
        // check if this is a pure object or custom display
        if (typeof (value.toMonitorContent || value.toReporterContent) === 'function') {
            value = value.toMonitorContent
              ? value.toMonitorContent() : value.toReporterContent();
            isHTML = true;
        } else if (!Array.isArray(value)) {
            // only applies to objects
            value = JSON.stringify(value, circularReplacer());
        }
    }

    return {id, label, category, value, isHTML};
}