File size: 5,503 Bytes
966637e
 
 
 
 
 
 
 
ac1c1f6
966637e
 
 
 
 
 
 
 
 
ac1c1f6
 
 
 
 
 
 
 
 
 
966637e
 
ac1c1f6
 
 
966637e
 
 
 
 
ac1c1f6
966637e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac1c1f6
 
 
 
 
 
 
 
 
 
 
966637e
 
 
ac1c1f6
 
 
05e36be
966637e
ac1c1f6
 
 
966637e
 
ac1c1f6
966637e
 
 
 
 
 
 
 
 
 
 
 
 
ac1c1f6
 
966637e
 
 
 
 
 
ac1c1f6
966637e
 
 
 
ac1c1f6
966637e
 
 
 
 
 
ac1c1f6
966637e
 
 
 
ac1c1f6
966637e
 
ac1c1f6
966637e
 
 
ac1c1f6
966637e
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const SandboxRunner = require('../../util/sandboxed-javascript-runner');
const Cast = require('../../util/cast');

class jgJavascript {
    constructor(runtime) {
        this.runtime = runtime;
        this.runningEditorUnsandboxed = true; // デフォルトでunsandboxedに変更
    }

    getInfo() {
        return {
            id: 'jgJavascript',
            name: 'JavaScript',
            isDynamic: true,
            blocks: [
                {
                    opcode: 'sandboxToggle',
                    text: 'サンドボックスを[STATE]にする',
                    blockType: BlockType.COMMAND,
                    arguments: {
                        STATE: {
                            type: ArgumentType.STRING,
                            menu: 'sandboxStateMenu',
                            defaultValue: 'オフ'
                        }
                    }
                },
                {
                    opcode: 'isSandboxed',
                    text: 'サンドボックスあり?',
                    blockType: BlockType.BOOLEAN
                },
                {
                    opcode: 'javascriptHat',
                    text: 'when javascript [CODE] == true',
                    blockType: BlockType.HAT,
                    hideFromPalette: !this.runningEditorUnsandboxed,
                    arguments: {
                        CODE: {
                            type: ArgumentType.STRING,
                            defaultValue: "Math.round(Math.random()) === 1"
                        }
                    }
                },
                {
                    opcode: 'javascriptStack',
                    text: 'javascript [CODE]',
                    blockType: BlockType.COMMAND,
                    arguments: {
                        CODE: {
                            type: ArgumentType.STRING,
                            defaultValue: "alert('Hello!')"
                        }
                    }
                },
                {
                    opcode: 'javascriptString',
                    text: 'javascript [CODE]',
                    blockType: BlockType.REPORTER,
                    disableMonitor: true,
                    arguments: {
                        CODE: {
                            type: ArgumentType.STRING,
                            defaultValue: "Math.random()"
                        }
                    }
                },
                {
                    opcode: 'javascriptBool',
                    text: 'javascript [CODE]',
                    blockType: BlockType.BOOLEAN,
                    disableMonitor: true,
                    arguments: {
                        CODE: {
                            type: ArgumentType.STRING,
                            defaultValue: "Math.round(Math.random()) === 1"
                        }
                    }
                }
            ],
            menus: {
                sandboxStateMenu: {
                    acceptReporters: true,
                    items: [
                        { text: 'オン', value: 'オン' },
                        { text: 'オフ', value: 'オフ' }
                    ]
                }
            }
        };
    }

    sandboxToggle(args) {
        const state = Cast.toString(args.STATE);
        this.runningEditorUnsandboxed = (state === 'オフ');
        this.runtime.extensionManager.refreshBlocks("jgJavascript");
    }

    isSandboxed() {
        return !this.runningEditorUnsandboxed;
    }

    async evaluateCode(code, args, util, realBlockInfo) {
        if (this.runtime.extensionRuntimeOptions.javascriptUnsandboxed === true || this.runningEditorUnsandboxed) {
            let result;
            try {
                // eslint-disable-next-line no-eval
                result = eval(code);
            } catch (err) {
                result = err;
            }
            return result;
        }
        return new Promise((resolve) => {
            SandboxRunner.execute(code).then(result => {
                return resolve(result.value);
            });
        });
    }

    javascriptStack(args, util, realBlockInfo) {
        const code = Cast.toString(args.CODE);
        return this.evaluateCode(code, args, util, realBlockInfo);
    }

    javascriptString(args, util, realBlockInfo) {
        const code = Cast.toString(args.CODE);
        return this.evaluateCode(code, args, util, realBlockInfo);
    }

    javascriptBool(args, util, realBlockInfo) {
        const code = Cast.toString(args.CODE);
        const possiblePromise = this.evaluateCode(code, args, util, realBlockInfo);
        if (possiblePromise && typeof possiblePromise.then === 'function') {
            return (async () => {
                const value = await possiblePromise;
                return Boolean(value);
            })();
        }
        return Boolean(possiblePromise);
    }

    javascriptHat(...args) {
        if (!this.runtime.extensionRuntimeOptions.javascriptUnsandboxed && !this.runningEditorUnsandboxed) {
            return false;
        }
        const possiblePromise = this.javascriptBool(...args);
        if (possiblePromise && typeof possiblePromise.then === 'function') {
            return false;
        }
        return possiblePromise;
    }
}

module.exports = jgJavascript;