Update src/extensions/jg_javascript/index.js
Browse files
src/extensions/jg_javascript/index.js
CHANGED
@@ -3,49 +3,40 @@ const ArgumentType = require('../../extension-support/argument-type');
|
|
3 |
const SandboxRunner = require('../../util/sandboxed-javascript-runner');
|
4 |
const Cast = require('../../util/cast');
|
5 |
|
6 |
-
/**
|
7 |
-
* Class
|
8 |
-
* oh yea you cant access util in the runner anymore
|
9 |
-
* im not adding it because im done with implementing eval in PM since it was done like 3 times
|
10 |
-
* @constructor
|
11 |
-
*/
|
12 |
class jgJavascript {
|
13 |
constructor(runtime) {
|
14 |
-
/**
|
15 |
-
* The runtime instantiating this block package.
|
16 |
-
* @type {runtime}
|
17 |
-
*/
|
18 |
this.runtime = runtime;
|
19 |
-
this.runningEditorUnsandboxed =
|
20 |
}
|
21 |
|
22 |
-
/**
|
23 |
-
* @returns {object} metadata for this extension and its blocks.
|
24 |
-
*/
|
25 |
getInfo() {
|
26 |
return {
|
27 |
id: 'jgJavascript',
|
28 |
name: 'JavaScript',
|
29 |
isDynamic: true,
|
30 |
-
// color1: '#EFC900', look like doo doo
|
31 |
blocks: [
|
32 |
{
|
33 |
-
opcode: '
|
34 |
-
text: '
|
35 |
-
blockType: BlockType.
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
},
|
38 |
{
|
39 |
-
opcode: '
|
40 |
-
text: '
|
41 |
-
blockType: BlockType.
|
42 |
-
hideFromPalette: !this.runningEditorUnsandboxed
|
43 |
},
|
44 |
{
|
45 |
opcode: 'javascriptHat',
|
46 |
text: 'when javascript [CODE] == true',
|
47 |
blockType: BlockType.HAT,
|
48 |
-
hideFromPalette: !this.runningEditorUnsandboxed,
|
49 |
arguments: {
|
50 |
CODE: {
|
51 |
type: ArgumentType.STRING,
|
@@ -87,50 +78,31 @@ class jgJavascript {
|
|
87 |
defaultValue: "Math.round(Math.random()) === 1"
|
88 |
}
|
89 |
}
|
90 |
-
}
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
{
|
102 |
-
blockType: BlockType.LABEL,
|
103 |
-
text: '⠀',
|
104 |
-
hideFromPalette: !this.runningEditorUnsandboxed
|
105 |
-
},
|
106 |
-
{
|
107 |
-
blockType: BlockType.LABEL,
|
108 |
-
text: 'Player Options >',
|
109 |
-
hideFromPalette: !this.runningEditorUnsandboxed
|
110 |
-
},
|
111 |
-
{
|
112 |
-
blockType: BlockType.LABEL,
|
113 |
-
text: 'Remove sandbox on the JavaScript Ext.',
|
114 |
-
hideFromPalette: !this.runningEditorUnsandboxed
|
115 |
-
},
|
116 |
-
]
|
117 |
};
|
118 |
}
|
119 |
|
120 |
-
|
121 |
-
const
|
122 |
-
|
123 |
-
this.runningEditorUnsandboxed = true;
|
124 |
this.runtime.vm.emitWorkspaceUpdate();
|
125 |
}
|
126 |
-
|
127 |
-
|
128 |
-
this.
|
129 |
}
|
130 |
|
131 |
-
|
132 |
-
evaluateCode(code, args, util, realBlockInfo) {
|
133 |
-
// used for packager
|
134 |
if (this.runtime.extensionRuntimeOptions.javascriptUnsandboxed === true || this.runningEditorUnsandboxed) {
|
135 |
let result;
|
136 |
try {
|
@@ -141,43 +113,42 @@ class jgJavascript {
|
|
141 |
}
|
142 |
return result;
|
143 |
}
|
144 |
-
// we are not packaged
|
145 |
return new Promise((resolve) => {
|
146 |
SandboxRunner.execute(code).then(result => {
|
147 |
-
// result is { value: any, success: boolean }
|
148 |
-
// in PM, we always ignore errors
|
149 |
return resolve(result.value);
|
150 |
-
})
|
151 |
-
})
|
152 |
}
|
153 |
|
154 |
-
// blocks
|
155 |
javascriptStack(args, util, realBlockInfo) {
|
156 |
const code = Cast.toString(args.CODE);
|
157 |
return this.evaluateCode(code, args, util, realBlockInfo);
|
158 |
}
|
|
|
159 |
javascriptString(args, util, realBlockInfo) {
|
160 |
const code = Cast.toString(args.CODE);
|
161 |
return this.evaluateCode(code, args, util, realBlockInfo);
|
162 |
}
|
|
|
163 |
javascriptBool(args, util, realBlockInfo) {
|
164 |
const code = Cast.toString(args.CODE);
|
165 |
const possiblePromise = this.evaluateCode(code, args, util, realBlockInfo);
|
166 |
if (possiblePromise && typeof possiblePromise.then === 'function') {
|
167 |
return (async () => {
|
168 |
const value = await possiblePromise;
|
169 |
-
return Boolean(value);
|
170 |
})();
|
171 |
}
|
172 |
return Boolean(possiblePromise);
|
173 |
}
|
|
|
174 |
javascriptHat(...args) {
|
175 |
if (!this.runtime.extensionRuntimeOptions.javascriptUnsandboxed && !this.runningEditorUnsandboxed) {
|
176 |
-
return false;
|
177 |
}
|
178 |
const possiblePromise = this.javascriptBool(...args);
|
179 |
if (possiblePromise && typeof possiblePromise.then === 'function') {
|
180 |
-
return false;
|
181 |
}
|
182 |
return possiblePromise;
|
183 |
}
|
|
|
3 |
const SandboxRunner = require('../../util/sandboxed-javascript-runner');
|
4 |
const Cast = require('../../util/cast');
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
class jgJavascript {
|
7 |
constructor(runtime) {
|
|
|
|
|
|
|
|
|
8 |
this.runtime = runtime;
|
9 |
+
this.runningEditorUnsandboxed = true; // デフォルトでunsandboxedに変更
|
10 |
}
|
11 |
|
|
|
|
|
|
|
12 |
getInfo() {
|
13 |
return {
|
14 |
id: 'jgJavascript',
|
15 |
name: 'JavaScript',
|
16 |
isDynamic: true,
|
|
|
17 |
blocks: [
|
18 |
{
|
19 |
+
opcode: 'sandboxToggle',
|
20 |
+
text: 'サンドボックスを[STATE]にする',
|
21 |
+
blockType: BlockType.COMMAND,
|
22 |
+
arguments: {
|
23 |
+
STATE: {
|
24 |
+
type: ArgumentType.STRING,
|
25 |
+
menu: 'sandboxStateMenu',
|
26 |
+
defaultValue: 'オフ'
|
27 |
+
}
|
28 |
+
}
|
29 |
},
|
30 |
{
|
31 |
+
opcode: 'isSandboxed',
|
32 |
+
text: 'サンドボックスあり?',
|
33 |
+
blockType: BlockType.BOOLEAN
|
|
|
34 |
},
|
35 |
{
|
36 |
opcode: 'javascriptHat',
|
37 |
text: 'when javascript [CODE] == true',
|
38 |
blockType: BlockType.HAT,
|
39 |
+
hideFromPalette: !this.runningEditorUnsandboxed,
|
40 |
arguments: {
|
41 |
CODE: {
|
42 |
type: ArgumentType.STRING,
|
|
|
78 |
defaultValue: "Math.round(Math.random()) === 1"
|
79 |
}
|
80 |
}
|
81 |
+
}
|
82 |
+
],
|
83 |
+
menus: {
|
84 |
+
sandboxStateMenu: {
|
85 |
+
acceptReporters: true,
|
86 |
+
items: [
|
87 |
+
{ text: 'オン', value: 'オン' },
|
88 |
+
{ text: 'オフ', value: 'オフ' }
|
89 |
+
]
|
90 |
+
}
|
91 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
};
|
93 |
}
|
94 |
|
95 |
+
sandboxToggle(args) {
|
96 |
+
const state = Cast.toString(args.STATE);
|
97 |
+
this.runningEditorUnsandboxed = (state === 'オフ');
|
|
|
98 |
this.runtime.vm.emitWorkspaceUpdate();
|
99 |
}
|
100 |
+
|
101 |
+
isSandboxed() {
|
102 |
+
return !this.runningEditorUnsandboxed;
|
103 |
}
|
104 |
|
105 |
+
async evaluateCode(code, args, util, realBlockInfo) {
|
|
|
|
|
106 |
if (this.runtime.extensionRuntimeOptions.javascriptUnsandboxed === true || this.runningEditorUnsandboxed) {
|
107 |
let result;
|
108 |
try {
|
|
|
113 |
}
|
114 |
return result;
|
115 |
}
|
|
|
116 |
return new Promise((resolve) => {
|
117 |
SandboxRunner.execute(code).then(result => {
|
|
|
|
|
118 |
return resolve(result.value);
|
119 |
+
});
|
120 |
+
});
|
121 |
}
|
122 |
|
|
|
123 |
javascriptStack(args, util, realBlockInfo) {
|
124 |
const code = Cast.toString(args.CODE);
|
125 |
return this.evaluateCode(code, args, util, realBlockInfo);
|
126 |
}
|
127 |
+
|
128 |
javascriptString(args, util, realBlockInfo) {
|
129 |
const code = Cast.toString(args.CODE);
|
130 |
return this.evaluateCode(code, args, util, realBlockInfo);
|
131 |
}
|
132 |
+
|
133 |
javascriptBool(args, util, realBlockInfo) {
|
134 |
const code = Cast.toString(args.CODE);
|
135 |
const possiblePromise = this.evaluateCode(code, args, util, realBlockInfo);
|
136 |
if (possiblePromise && typeof possiblePromise.then === 'function') {
|
137 |
return (async () => {
|
138 |
const value = await possiblePromise;
|
139 |
+
return Boolean(value);
|
140 |
})();
|
141 |
}
|
142 |
return Boolean(possiblePromise);
|
143 |
}
|
144 |
+
|
145 |
javascriptHat(...args) {
|
146 |
if (!this.runtime.extensionRuntimeOptions.javascriptUnsandboxed && !this.runningEditorUnsandboxed) {
|
147 |
+
return false;
|
148 |
}
|
149 |
const possiblePromise = this.javascriptBool(...args);
|
150 |
if (possiblePromise && typeof possiblePromise.then === 'function') {
|
151 |
+
return false;
|
152 |
}
|
153 |
return possiblePromise;
|
154 |
}
|