File size: 2,032 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const BlockUtility = require('../engine/block-utility');

class CompatibilityLayerBlockUtility extends BlockUtility {
    constructor () {
        super();
        this._startedBranch = null;
    }

    get stackFrame () {
        return this.thread.compatibilityStackFrame;
    }

    startBranch (branchNumber, isLoop, onEnd) {
        if (this._branchInfo && onEnd) this._branchInfo.onEnd.push(onEnd);
        this._startedBranch = [branchNumber, isLoop];
    }

    /**
     * runs any given procedure
     * @param {String} proccode the procedure to start
     * @param {Object} args 
     * @returns the return value of the procedure, returns undefined if statement
     */
    startProcedure (proccode, args) {
        if (!args)
            return this.thread.procedures[proccode]();
        if (!(typeof args === 'object'))
            throw new Error(`procedure arguments can only be of type undefined|object. instead got "${typeof args}"`);
        let evaluate = `this.thread.procedures[proccode](`;
        const inputs = [];
        for (const arg in args) {
            inputs.push(String(args[arg]));
        }
        evaluate += `${inputs.join(',')})`;
        return new Function(`Procedure ${proccode}`, evaluate)();
    }

    /*
    // Parameters are not used by compiled scripts.
    initParams () {
        throw new Error('initParams is not supported by this BlockUtility');
    }
    pushParam () {
        throw new Error('pushParam is not supported by this BlockUtility');
    }
    getParam () {
        throw new Error('getParam is not supported by this BlockUtility');
    }
    */

    init (thread, fakeBlockId, stackFrame, branchInfo) {
        this.thread = thread;
        this.sequencer = thread.target.runtime.sequencer;
        this._startedBranch = null;
        this._branchInfo = branchInfo;
        thread.stack[0] = fakeBlockId;
        thread.compatibilityStackFrame = stackFrame;
    }
}

// Export a single instance to be reused.
module.exports = new CompatibilityLayerBlockUtility();