From 9df470255e387b605710dc8915361d37766c1f7b Mon Sep 17 00:00:00 2001 From: Tim Mickel Date: Thu, 9 Jun 2016 14:22:58 -0400 Subject: [PATCH] Add helpers to thread object --- src/engine/thread.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/engine/thread.js b/src/engine/thread.js index 07ceaca35..4142ba88b 100644 --- a/src/engine/thread.js +++ b/src/engine/thread.js @@ -38,6 +38,12 @@ function Thread (firstBlock) { * @type {number} */ this.yieldTimerId = -1; + + /** + * Whether the thread has switched stack in the course of execution. + * @type {boolean} + */ + this.switchedStack = false; } /** @@ -62,4 +68,41 @@ Thread.STATUS_YIELD = 1; */ Thread.STATUS_DONE = 2; +/** + * Push stack and update stack frames appropriately. + * @param {string} blockId Block ID to push to stack. + */ +Thread.prototype.pushStack = function (blockId) { + this.stack.push(blockId); + // Push an empty stack frame, if we need one. + // Might not, if we just popped the stack. + if (this.stack.length > this.stackFrames.length) { + this.stackFrames.push({}); + } +}; + +/** + * Pop last block on the stack and its stack frame. + * @returns {string} Block ID popped from the stack. + */ +Thread.prototype.popStack = function () { + this.stackFrames.pop(); + return this.stack.pop(); +}; + +/** + * Get last stack frame. + * @return {?Object} Last stack frame stored on this thread. + */ +Thread.prototype.getLastStackFrame = function () { + return this.stackFrames[this.stackFrames.length - 1]; +}; + +/** + * Yields the thread. + */ +Thread.prototype.yield = function () { + this.status = Thread.STATUS_YIELD; +}; + module.exports = Thread;