diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 3ff1d9fb..c51ec1cf 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -392,6 +392,11 @@ class Runtime extends EventEmitter { * @type {function} */ this.removeCloudVariable = this._initializeRemoveCloudVariable(newCloudDataManager); + + /** + * Whether the script compiler is enabled. + */ + this.compilerEnabled = true; } /** @@ -483,6 +488,22 @@ class Runtime extends EventEmitter { return 'COMPATIBILITY_MODE_OFF'; } + /** + * Event name for enabling the compiler. + * @const {string} + */ + static get COMPILER_ENABLED () { + return 'COMPILER_ENABLED'; + } + + /** + * Event name for disabling the compiler. + * @const {string} + */ + static get COMPILER_DISABLED () { + return 'COMPILER_DISABLED'; + } + /** * Event name when the project is started (threads may not necessarily be * running). @@ -1614,7 +1635,6 @@ class Runtime extends EventEmitter { * @param {?object} opts optional arguments * @param {?boolean} opts.stackClick true if the script was activated by clicking on the stack * @param {?boolean} opts.updateMonitor true if the script should update a monitor value - * @param {?boolean} opts.enableCompiler true if the compiler may be used * @return {!Thread} The newly created thread. */ _pushThread (id, target, opts) { @@ -1629,7 +1649,7 @@ class Runtime extends EventEmitter { thread.pushStack(id); this.threads.push(thread); - if (opts && opts.enableCompiler) { + if (!(opts && opts.updateMonitor) && this.compilerEnabled) { thread.tryCompile(); } @@ -1661,7 +1681,7 @@ class Runtime extends EventEmitter { newThread.updateMonitor = thread.updateMonitor; newThread.blockContainer = thread.blockContainer; newThread.pushStack(thread.topBlock); - if (thread.triedToCompile) { + if (thread.triedToCompile && this.compilerEnabled) { newThread.tryCompile(); } const i = this.threads.indexOf(thread); @@ -1710,8 +1730,7 @@ class Runtime extends EventEmitter { toggleScript (topBlockId, opts) { opts = Object.assign({ target: this._editingTarget, - stackClick: false, - enableCompiler: true + stackClick: false }, opts); // Remove any existing thread. for (let i = 0; i < this.threads.length; i++) { @@ -1857,9 +1876,7 @@ class Runtime extends EventEmitter { } } // Start the thread with this top block. - newThreads.push(this._pushThread(topBlockId, target, { - enableCompiler: true - })); + newThreads.push(this._pushThread(topBlockId, target)); }, optTarget); // For compatibility with Scratch 2, edge triggered hats need to be processed before // threads are stepped. See ScratchRuntime.as for original implementation @@ -2179,6 +2196,20 @@ class Runtime extends EventEmitter { } } + /** + * Set whether the compiler is enabled. + * This does not affect already running threads. + * @param {boolean} compilerEnabled True iff the compiler is to be enabled. + */ + setCompilerEnabled (compilerEnabled) { + this.compilerEnabled = compilerEnabled; + if (compilerEnabled) { + this.emit(Runtime.COMPILER_ENABLED); + } else { + this.emit(Runtime.COMPILER_DISABLED); + } + } + /** * Emit glows/glow clears for scripts after a single tick. * Looks at `this.threads` and notices which have turned on/off new glows. diff --git a/src/virtual-machine.js b/src/virtual-machine.js index b93467ee..b062b42a 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -157,6 +157,12 @@ class VirtualMachine extends EventEmitter { this.runtime.on(Runtime.COMPATIBILITY_MODE_ON, () => { this.emit(Runtime.COMPATIBILITY_MODE_ON); }); + this.runtime.on(Runtime.COMPILER_ENABLED, () => { + this.emit(Runtime.COMPILER_ENABLED); + }); + this.runtime.on(Runtime.COMPILER_DISABLED, () => { + this.emit(Runtime.COMPILER_DISABLED); + }); this.extensionManager = new ExtensionManager(this.runtime); @@ -208,6 +214,10 @@ class VirtualMachine extends EventEmitter { this.runtime.setCompatibilityMode(!!compatibilityModeOn); } + setCompilerEnabled (compilerEnabled) { + this.runtime.setCompilerEnabled(compilerEnabled); + } + /** * Stop all threads and running activities. */