Spaces:
Running
Running
File size: 7,062 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 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
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
* oh yea you cant access util in the runner anymore
* im not adding it because im done with implementing eval in PM since it was done like 3 times
* @constructor
*/
class jgJavascript {
constructor(runtime) {
/**
* The runtime instantiating this block package.
* @type {runtime}
*/
this.runtime = runtime;
this.runningEditorUnsandboxed = false;
}
/**
* @returns {object} metadata for this extension and its blocks.
*/
getInfo() {
return {
id: 'jgJavascript',
name: 'JavaScript',
isDynamic: true,
// color1: '#EFC900', look like doo doo
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, // this block seems to cause strange behavior because of how sandboxed eval is done
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.vm.emitWorkspaceUpdate();
}
sandbox() {
this.runningEditorUnsandboxed = false;
this.runtime.vm.emitWorkspaceUpdate();
}
// util
evaluateCode(code, args, util, realBlockInfo) {
// used for packager
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;
}
// we are not packaged
return new Promise((resolve) => {
SandboxRunner.execute(code).then(result => {
// result is { value: any, success: boolean }
// in PM, we always ignore errors
return resolve(result.value);
})
})
}
// blocks
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); // this is a JavaScript extension, we should use the JavaScript way of determining booleans
})();
}
return Boolean(possiblePromise);
}
javascriptHat(...args) {
if (!this.runtime.extensionRuntimeOptions.javascriptUnsandboxed && !this.runningEditorUnsandboxed) {
return false; // we will cause issues otherwise, edging hats cause weird issues when waiting for promises each frame
}
const possiblePromise = this.javascriptBool(...args);
if (possiblePromise && typeof possiblePromise.then === 'function') {
return false; // we will cause issues otherwise, edging hats cause weird issues when waiting for promises each frame
}
return possiblePromise;
}
}
module.exports = jgJavascript;
|