|
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 = false; |
|
} |
|
|
|
|
|
|
|
|
|
getInfo() { |
|
return { |
|
id: 'jgJavascript', |
|
name: 'JavaScript', |
|
isDynamic: true, |
|
|
|
blocks: [ |
|
{ |
|
opcode: 'unsandbox', |
|
text: 'Run Unsandboxed', |
|
blockType: BlockType.BUTTON, |
|
hideFromPalette: this.runningEditorUnsandboxed |
|
}, |
|
{ |
|
opcode: 'sandbox', |
|
text: 'Run Sandboxed', |
|
blockType: BlockType.BUTTON, |
|
hideFromPalette: !this.runningEditorUnsandboxed |
|
}, |
|
{ |
|
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" |
|
} |
|
} |
|
}, |
|
{ |
|
blockType: BlockType.LABEL, |
|
text: 'You can run unsandboxed', |
|
hideFromPalette: !this.runningEditorUnsandboxed |
|
}, |
|
{ |
|
blockType: BlockType.LABEL, |
|
text: 'when packaging the project.', |
|
hideFromPalette: !this.runningEditorUnsandboxed |
|
}, |
|
{ |
|
blockType: BlockType.LABEL, |
|
text: '⠀', |
|
hideFromPalette: !this.runningEditorUnsandboxed |
|
}, |
|
{ |
|
blockType: BlockType.LABEL, |
|
text: 'Player Options >', |
|
hideFromPalette: !this.runningEditorUnsandboxed |
|
}, |
|
{ |
|
blockType: BlockType.LABEL, |
|
text: 'Remove sandbox on the JavaScript Ext.', |
|
hideFromPalette: !this.runningEditorUnsandboxed |
|
}, |
|
] |
|
}; |
|
} |
|
|
|
async unsandbox() { |
|
const unsandbox = await this.runtime.vm.securityManager.canUnsandbox('JavaScript'); |
|
if (!unsandbox) return; |
|
this.runningEditorUnsandboxed = true; |
|
this.runtime.extensionManager.refreshBlocks("jgJavascript"); |
|
} |
|
sandbox() { |
|
this.runningEditorUnsandboxed = false; |
|
this.runtime.extensionManager.refreshBlocks("jgJavascript"); |
|
} |
|
|
|
|
|
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; |
|
|