From dbfb3356c691fdabbc0ec64a414a2b2a30261da9 Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Tue, 26 Apr 2016 15:51:14 -0400 Subject: [PATCH] Move threads list to the Runtime --- src/engine/runtime.js | 20 ++++++++++++++++++-- src/engine/sequencer.js | 21 ++++++++------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/engine/runtime.js b/src/engine/runtime.js index 44c1a7f1..dd5376cf 100644 --- a/src/engine/runtime.js +++ b/src/engine/runtime.js @@ -10,11 +10,27 @@ function Runtime () { EventEmitter.call(this); // State for the runtime - /** @type {Object.} */ + /** + * All blocks in the workspace. + * Keys are block IDs, values are metadata about the block. + * @type {Object.} + */ this.blocks = {}; - /** @type {Array.} */ + + /** + * All stacks in the workspace. + * A list of block IDs that represent stacks (first block in stack). + * @type {Array.} + */ this.stacks = []; + /** + * A list of threads that are currently running in the VM. + * Threads are added when execution starts and pruned when execution ends. + * @type {Array.} + */ + this.threads = []; + /** @type {!Sequencer} */ this.sequencer = new Sequencer(); } diff --git a/src/engine/sequencer.js b/src/engine/sequencer.js index 4e783c7a..dd26992a 100644 --- a/src/engine/sequencer.js +++ b/src/engine/sequencer.js @@ -1,13 +1,6 @@ var Timer = require('../util/Timer'); function Sequencer () { - /** - * A list of threads that are currently running in the VM. - * Threads are added when execution starts and pruned when execution ends. - * @type {Array.} - */ - this.threads = []; - /** * A utility timer for timing thread sequencing. * @type {!Timer} @@ -25,34 +18,36 @@ Sequencer.WORK_TIME = 1000 / 60; /** * Step through all threads in `this.threads`, running them in order. + * @return {Array.} New threads after this round of execution. */ -Sequencer.prototype.stepThreads = function () { +Sequencer.prototype.stepThreads = function (threads) { // Start counting toward WORK_TIME this.timer.start(); // While there are still threads to run and we are within WORK_TIME, // continue executing threads. - while (this.threads.length > 0 && + while (threads.length > 0 && this.timer.timeElapsed() < Sequencer.WORK_TIME) { // New threads at the end of the iteration. var newThreads = []; // Attempt to run each thread one time - for (var i = 0; i < this.threads.length; i++) { - var activeThread = this.threads[i]; + for (var i = 0; i < threads.length; i++) { + var activeThread = threads[i]; this.stepThread(activeThread); if (activeThread.nextBlock !== null) { newThreads.push(activeThread); } } // Effectively filters out threads that have stopped. - this.threads = newThreads; + threads = newThreads; } + return threads; }; /** * Step the requested thread * @param {!Thread} thread Thread object to step */ -Sequencer.protoype.stepThread = function (thread) { +Sequencer.prototype.stepThread = function (thread) { // @todo };