|
|
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; |
|
|
} |
|
|
|
|
|
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 { |
|
|
|
|
|
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; |
|
|
|