File size: 1,393 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
const rotationStyleOptions = [
    'left-right',
    'don\'t rotate',
    'all around'
];

class execute {
    constructor (funcText, target, runtime) {
        const thisData = this.getThisFuncs(target, runtime);
        thisData.mySelf = new Function(funcText);
    }
    getThisFuncs (target, runtime) {
        // all of the functions accessed by `this`
        // must have there internal vars defined externaly
        // so that the code can not access nor edit them
        const thisFuncs = {
            goto: target.setXY,
            set xPosition (newPos) {
                target.setXY(newPos, target.y);
            },
            set yPosition (newPos) {
                target.setXY(target.x, newPos);
            },
            get xPosition () {
                return target.x;
            },
            get yPosition () {
                return target.y;
            },
            set direction (newDir) {
                target.setDirection(newDir);
            },
            get direction () {
                return target.direction;
            },
            set rotationStyle (style) {
                if (!rotationStyleOptions.includes(style)) {
                    throw new Error(`invalid style: ${style}`);
                }
                target.setRotationStyle(style);
            }
        };
        return thisFuncs;
    }
}

module.exports = execute;