From 2ff719f2bae4f13b38d869f6a1b6dac5a3f27862 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Wed, 23 Nov 2016 15:43:05 -0500 Subject: [PATCH 1/4] Fix consistency of existing emits --- playground/playground.js | 4 ++-- src/engine/runtime.js | 14 +++++++------- src/index.js | 25 +++++++++++++------------ 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/playground/playground.js b/playground/playground.js index 75027dc33..cd9f8dff0 100644 --- a/playground/playground.js +++ b/playground/playground.js @@ -137,10 +137,10 @@ window.onload = function() { }; // Feedback for stacks and blocks running. - vm.on('STACK_GLOW_ON', function(data) { + vm.on('SCRIPT_GLOW_ON', function(data) { workspace.glowStack(data.id, true); }); - vm.on('STACK_GLOW_OFF', function(data) { + vm.on('SCRIPT_GLOW_OFF', function(data) { workspace.glowStack(data.id, false); }); vm.on('BLOCK_GLOW_ON', function(data) { diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 36722a458..a8bd98c52 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -159,13 +159,13 @@ Runtime.STAGE_HEIGHT = 360; * Event name for glowing a script. * @const {string} */ -Runtime.SCRIPT_GLOW_ON = 'STACK_GLOW_ON'; +Runtime.SCRIPT_GLOW_ON = 'SCRIPT_GLOW_ON'; /** * Event name for unglowing a script. * @const {string} */ -Runtime.SCRIPT_GLOW_OFF = 'STACK_GLOW_OFF'; +Runtime.SCRIPT_GLOW_OFF = 'SCRIPT_GLOW_OFF'; /** * Event name for glowing a block. @@ -659,9 +659,9 @@ Runtime.prototype.quietGlow = function (scriptBlockId) { */ Runtime.prototype.glowBlock = function (blockId, isGlowing) { if (isGlowing) { - this.emit(Runtime.BLOCK_GLOW_ON, blockId); + this.emit(Runtime.BLOCK_GLOW_ON, {id: blockId}); } else { - this.emit(Runtime.BLOCK_GLOW_OFF, blockId); + this.emit(Runtime.BLOCK_GLOW_OFF, {id: blockId}); } }; @@ -672,9 +672,9 @@ Runtime.prototype.glowBlock = function (blockId, isGlowing) { */ Runtime.prototype.glowScript = function (topBlockId, isGlowing) { if (isGlowing) { - this.emit(Runtime.SCRIPT_GLOW_ON, topBlockId); + this.emit(Runtime.SCRIPT_GLOW_ON, {id: topBlockId}); } else { - this.emit(Runtime.SCRIPT_GLOW_OFF, topBlockId); + this.emit(Runtime.SCRIPT_GLOW_OFF, {id: topBlockId}); } }; @@ -684,7 +684,7 @@ Runtime.prototype.glowScript = function (topBlockId, isGlowing) { * @param {string} value Value to show associated with the block. */ Runtime.prototype.visualReport = function (blockId, value) { - this.emit(Runtime.VISUAL_REPORT, blockId, String(value)); + this.emit(Runtime.VISUAL_REPORT, {id: blockId, value: String(value)}); }; /** diff --git a/src/index.js b/src/index.js index a275cfacc..8b390f351 100644 --- a/src/index.js +++ b/src/index.js @@ -25,23 +25,24 @@ var VirtualMachine = function () { */ instance.editingTarget = null; // Runtime emits are passed along as VM emits. - instance.runtime.on(Runtime.SCRIPT_GLOW_ON, function (id) { - instance.emit(Runtime.SCRIPT_GLOW_ON, {id: id}); + instance.runtime.on(Runtime.SCRIPT_GLOW_ON, function (glowData) { + instance.emit(Runtime.SCRIPT_GLOW_ON, glowData); }); - instance.runtime.on(Runtime.SCRIPT_GLOW_OFF, function (id) { - instance.emit(Runtime.SCRIPT_GLOW_OFF, {id: id}); + instance.runtime.on(Runtime.SCRIPT_GLOW_OFF, function (glowData) { + instance.emit(Runtime.SCRIPT_GLOW_OFF, glowData); }); - instance.runtime.on(Runtime.BLOCK_GLOW_ON, function (id) { - instance.emit(Runtime.BLOCK_GLOW_ON, {id: id}); + instance.runtime.on(Runtime.BLOCK_GLOW_ON, function (glowData) { + instance.emit(Runtime.BLOCK_GLOW_ON, glowData); }); - instance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (id) { - instance.emit(Runtime.BLOCK_GLOW_OFF, {id: id}); + instance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (glowData) { + instance.emit(Runtime.BLOCK_GLOW_OFF, glowData); }); - instance.runtime.on(Runtime.VISUAL_REPORT, function (id, value) { - instance.emit(Runtime.VISUAL_REPORT, {id: id, value: value}); }); - instance.runtime.on(Runtime.SPRITE_INFO_REPORT, function (data) { - instance.emit(Runtime.SPRITE_INFO_REPORT, data); + instance.runtime.on(Runtime.VISUAL_REPORT, function (visualReport) { + instance.emit(Runtime.VISUAL_REPORT, visualReport); + }); + instance.runtime.on(Runtime.SPRITE_INFO_REPORT, function (spriteInfo) { + instance.emit(Runtime.SPRITE_INFO_REPORT, spriteInfo); }); this.blockListener = this.blockListener.bind(this); From 55ac7e269cd3a49262e05883dfecf0f31f8d19ce Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Wed, 23 Nov 2016 15:47:49 -0500 Subject: [PATCH 2/4] Add PROJECT_RUN_START/STOP events These events signal when any threads are running or when all threads have stopped running. This maps to whether the green flag or the stop button should be illuminated in the GUI. --- src/engine/runtime.js | 33 +++++++++++++++++++++++++++++++++ src/index.js | 5 +++++ 2 files changed, 38 insertions(+) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index a8bd98c52..6e4c0c87c 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -83,6 +83,12 @@ var Runtime = function () { */ this._scriptGlowsPreviousFrame = []; + /** + * Number of threads running during the previous frame + * @type {number} + */ + this._threadCount = 0; + /** * Currently known number of clones, used to enforce clone limit. * @type {number} @@ -179,6 +185,18 @@ Runtime.BLOCK_GLOW_ON = 'BLOCK_GLOW_ON'; */ Runtime.BLOCK_GLOW_OFF = 'BLOCK_GLOW_OFF'; +/** + * Event name for glowing the green flag + * @const {string} + */ +Runtime.PROJECT_RUN_START = 'PROJECT_RUN_START' + +/** + * Event name for unglowing the green flag + * @const {string} + */ +Runtime.PROJECT_RUN_STOP = 'PROJECT_RUN_STOP' + /** * Event name for visual value report. * @const {string} @@ -552,6 +570,7 @@ Runtime.prototype._step = function () { this.redrawRequested = false; var inactiveThreads = this.sequencer.stepThreads(); this._updateGlows(inactiveThreads); + this._setThreadCount(this.threads.length); if (this.renderer) { // @todo: Only render when this.redrawRequested or clones rendered. this.renderer.draw(); @@ -639,6 +658,20 @@ Runtime.prototype._updateGlows = function (optExtraThreads) { this._scriptGlowsPreviousFrame = finalScriptGlows; }; +/** + * Emit run start/stop after each tick. Emits when `this.threads.length` goes + * between non-zero and zero + */ +Runtime.prototype._setThreadCount = function (threadCount) { + if (this._threadCount === 0 && threadCount > 0) { + this.emit(Runtime.PROJECT_RUN_START); + } + if (this._threadCount > 0 && threadCount === 0) { + this.emit(Runtime.PROJECT_RUN_STOP); + } + this._threadCount = threadCount; +}; + /** * "Quiet" a script's glow: stop the VM from generating glow/unglow events * about that script. Use when a script has just been deleted, but we may diff --git a/src/index.js b/src/index.js index 8b390f351..4ed724f39 100644 --- a/src/index.js +++ b/src/index.js @@ -37,6 +37,11 @@ var VirtualMachine = function () { instance.runtime.on(Runtime.BLOCK_GLOW_OFF, function (glowData) { instance.emit(Runtime.BLOCK_GLOW_OFF, glowData); }); + instance.runtime.on(Runtime.PROJECT_RUN_START, function () { + instance.emit(Runtime.PROJECT_RUN_START); + }); + instance.runtime.on(Runtime.PROJECT_RUN_STOP, function () { + instance.emit(Runtime.PROJECT_RUN_STOP); }); instance.runtime.on(Runtime.VISUAL_REPORT, function (visualReport) { instance.emit(Runtime.VISUAL_REPORT, visualReport); From 0003b294cb825d551e5d1ffb585bbd21c0847c60 Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Thu, 24 Nov 2016 10:36:30 -0500 Subject: [PATCH 3/4] Lint --- src/engine/runtime.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 6e4c0c87c..2244d1ed3 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -189,13 +189,13 @@ Runtime.BLOCK_GLOW_OFF = 'BLOCK_GLOW_OFF'; * Event name for glowing the green flag * @const {string} */ -Runtime.PROJECT_RUN_START = 'PROJECT_RUN_START' +Runtime.PROJECT_RUN_START = 'PROJECT_RUN_START'; /** * Event name for unglowing the green flag * @const {string} */ -Runtime.PROJECT_RUN_STOP = 'PROJECT_RUN_STOP' +Runtime.PROJECT_RUN_STOP = 'PROJECT_RUN_STOP'; /** * Event name for visual value report. @@ -661,6 +661,8 @@ Runtime.prototype._updateGlows = function (optExtraThreads) { /** * Emit run start/stop after each tick. Emits when `this.threads.length` goes * between non-zero and zero + * + * @param {number} threadCount The new threadCount */ Runtime.prototype._setThreadCount = function (threadCount) { if (this._threadCount === 0 && threadCount > 0) { From d00cdd6b2c4a274d916270872371b85e63ad342f Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Mon, 28 Nov 2016 10:49:05 -0500 Subject: [PATCH 4/4] Account for done threads on threadCount update Also rename inactiveThreads -> doneThreads, since this describes them better. They are the threads that have completed in the step. --- src/engine/runtime.js | 6 +++--- src/engine/sequencer.js | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 2244d1ed3..5c7b788c4 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -568,9 +568,9 @@ Runtime.prototype._step = function () { } } this.redrawRequested = false; - var inactiveThreads = this.sequencer.stepThreads(); - this._updateGlows(inactiveThreads); - this._setThreadCount(this.threads.length); + var doneThreads = this.sequencer.stepThreads(); + this._updateGlows(doneThreads); + this._setThreadCount(this.threads.length + doneThreads.length); if (this.renderer) { // @todo: Only render when this.redrawRequested or clones rendered. this.renderer.draw(); diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index 8c732ca9c..16e07110e 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -35,7 +35,7 @@ Sequencer.prototype.stepThreads = function () { var numActiveThreads = Infinity; // Whether `stepThreads` has run through a full single tick. var ranFirstTick = false; - var inactiveThreads = []; + var doneThreads = []; // Conditions for continuing to stepping threads: // 1. We must have threads in the list, and some must be active. // 2. Time elapsed must be less than WORK_TIME. @@ -51,8 +51,8 @@ Sequencer.prototype.stepThreads = function () { if (activeThread.stack.length === 0 || activeThread.status === Thread.STATUS_DONE) { // Finished with this thread. - if (inactiveThreads.indexOf(activeThread) < 0) { - inactiveThreads.push(activeThread); + if (doneThreads.indexOf(activeThread) < 0) { + doneThreads.push(activeThread); } continue; } @@ -77,12 +77,12 @@ Sequencer.prototype.stepThreads = function () { } // Filter inactive threads from `this.runtime.threads`. this.runtime.threads = this.runtime.threads.filter(function (thread) { - if (inactiveThreads.indexOf(thread) > -1) { + if (doneThreads.indexOf(thread) > -1) { return false; } return true; }); - return inactiveThreads; + return doneThreads; }; /**