2016-04-26 16:50:49 -04:00
|
|
|
/**
|
|
|
|
* A thread is a running stack context and all the metadata needed.
|
|
|
|
* @param {?string} firstBlock First block to execute in the thread.
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function Thread (firstBlock) {
|
2016-04-29 17:31:04 -04:00
|
|
|
/**
|
2016-05-02 13:05:48 -04:00
|
|
|
* ID of top block of the thread
|
|
|
|
* @type {!string}
|
2016-04-29 17:31:04 -04:00
|
|
|
*/
|
|
|
|
this.topBlock = firstBlock;
|
2016-06-09 17:08:30 -04:00
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
/**
|
|
|
|
* Stack for the thread. When the sequencer enters a control structure,
|
|
|
|
* the block is pushed onto the stack so we know where to exit.
|
|
|
|
* @type {Array.<string>}
|
|
|
|
*/
|
|
|
|
this.stack = [];
|
2016-05-02 18:09:02 -04:00
|
|
|
|
2016-05-03 13:28:24 -04:00
|
|
|
/**
|
|
|
|
* Stack frames for the thread. Store metadata for the executing blocks.
|
|
|
|
* @type {Array.<Object>}
|
|
|
|
*/
|
|
|
|
this.stackFrames = [];
|
|
|
|
|
2016-05-02 18:09:02 -04:00
|
|
|
/**
|
|
|
|
* Status of the thread, one of three states (below)
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.status = 0; /* Thread.STATUS_RUNNING */
|
2016-09-08 09:40:53 -04:00
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
|
|
|
* Target of this thread.
|
|
|
|
* @type {?Target}
|
|
|
|
*/
|
|
|
|
this.target = null;
|
|
|
|
|
2016-09-08 09:40:53 -04:00
|
|
|
/**
|
|
|
|
* Whether the thread requests its script to glow during this frame.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.requestScriptGlowInFrame = false;
|
2016-10-17 23:23:16 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Which block ID should glow during this frame, if any.
|
|
|
|
* @type {?string}
|
|
|
|
*/
|
|
|
|
this.blockGlowInFrame = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A timer for when the thread enters warp mode.
|
|
|
|
* Substitutes the sequencer's count toward WORK_TIME on a per-thread basis.
|
|
|
|
* @type {?Timer}
|
|
|
|
*/
|
|
|
|
this.warpTimer = null;
|
2016-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:09:02 -04:00
|
|
|
/**
|
|
|
|
* Thread status for initialized or running thread.
|
2016-06-30 17:12:16 -04:00
|
|
|
* This is the default state for a thread - execution should run normally,
|
|
|
|
* stepping from block to block.
|
2016-05-02 18:09:02 -04:00
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Thread.STATUS_RUNNING = 0;
|
|
|
|
|
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Threads are in this state when a primitive is waiting on a promise;
|
|
|
|
* execution is paused until the promise changes thread status.
|
2016-05-02 18:09:02 -04:00
|
|
|
* @const
|
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Thread.STATUS_PROMISE_WAIT = 1;
|
2016-05-02 18:09:02 -04:00
|
|
|
|
2016-07-01 10:44:43 -04:00
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Thread status for yield.
|
2016-07-01 10:44:43 -04:00
|
|
|
* @const
|
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Thread.STATUS_YIELD = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread status for a single-tick yield. This will be cleared when the
|
|
|
|
* thread is resumed.
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Thread.STATUS_YIELD_TICK = 3;
|
2016-07-01 10:44:43 -04:00
|
|
|
|
2016-05-02 18:09:02 -04:00
|
|
|
/**
|
|
|
|
* Thread status for a finished/done thread.
|
2016-06-30 17:12:16 -04:00
|
|
|
* Thread is in this state when there are no more blocks to execute.
|
2016-05-02 18:09:02 -04:00
|
|
|
* @const
|
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Thread.STATUS_DONE = 4;
|
2016-05-02 18:09:02 -04:00
|
|
|
|
2016-06-09 14:22:58 -04:00
|
|
|
/**
|
|
|
|
* 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) {
|
2016-10-17 23:23:16 -04:00
|
|
|
// Copy warp mode from any higher level.
|
|
|
|
var warpMode = false;
|
|
|
|
if (this.stackFrames[this.stackFrames.length - 1]) {
|
|
|
|
warpMode = this.stackFrames[this.stackFrames.length - 1].warpMode;
|
|
|
|
}
|
2016-06-17 15:10:12 -04:00
|
|
|
this.stackFrames.push({
|
2016-10-17 23:23:16 -04:00
|
|
|
isLoop: false, // Whether this level of the stack is a loop.
|
|
|
|
warpMode: warpMode, // Whether this level is in warp mode.
|
2016-06-17 15:10:12 -04:00
|
|
|
reported: {}, // Collects reported input values.
|
2016-06-17 15:53:58 -04:00
|
|
|
waitingReporter: null, // Name of waiting reporter.
|
2016-10-13 13:11:26 -04:00
|
|
|
params: {}, // Procedure parameters.
|
2016-06-17 15:10:12 -04:00
|
|
|
executionContext: {} // A context passed to block implementations.
|
|
|
|
});
|
2016-06-09 14:22:58 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pop last block on the stack and its stack frame.
|
2016-06-09 17:08:30 -04:00
|
|
|
* @return {string} Block ID popped from the stack.
|
2016-06-09 14:22:58 -04:00
|
|
|
*/
|
|
|
|
Thread.prototype.popStack = function () {
|
|
|
|
this.stackFrames.pop();
|
|
|
|
return this.stack.pop();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-06-09 17:08:30 -04:00
|
|
|
* Get top stack item.
|
|
|
|
* @return {?string} Block ID on top of stack.
|
|
|
|
*/
|
|
|
|
Thread.prototype.peekStack = function () {
|
|
|
|
return this.stack[this.stack.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get top stack frame.
|
2016-06-09 14:22:58 -04:00
|
|
|
* @return {?Object} Last stack frame stored on this thread.
|
|
|
|
*/
|
2016-06-09 17:08:30 -04:00
|
|
|
Thread.prototype.peekStackFrame = function () {
|
2016-06-09 14:22:58 -04:00
|
|
|
return this.stackFrames[this.stackFrames.length - 1];
|
|
|
|
};
|
|
|
|
|
2016-06-20 14:10:19 -04:00
|
|
|
/**
|
|
|
|
* Get stack frame above the current top.
|
|
|
|
* @return {?Object} Second to last stack frame stored on this thread.
|
|
|
|
*/
|
|
|
|
Thread.prototype.peekParentStackFrame = function () {
|
|
|
|
return this.stackFrames[this.stackFrames.length - 2];
|
|
|
|
};
|
|
|
|
|
2016-06-17 15:10:12 -04:00
|
|
|
/**
|
|
|
|
* Push a reported value to the parent of the current stack frame.
|
2016-10-17 23:23:16 -04:00
|
|
|
* @param {*} value Reported value to push.
|
2016-06-17 15:10:12 -04:00
|
|
|
*/
|
2016-06-17 15:53:58 -04:00
|
|
|
Thread.prototype.pushReportedValue = function (value) {
|
2016-06-20 14:10:19 -04:00
|
|
|
var parentStackFrame = this.peekParentStackFrame();
|
2016-06-17 15:10:12 -04:00
|
|
|
if (parentStackFrame) {
|
2016-06-17 15:53:58 -04:00
|
|
|
var waitingReporter = parentStackFrame.waitingReporter;
|
|
|
|
parentStackFrame.reported[waitingReporter] = value;
|
2016-06-17 15:10:12 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Add a parameter to the stack frame.
|
|
|
|
* Use when calling a procedure with parameter values.
|
|
|
|
* @param {!string} paramName Name of parameter.
|
|
|
|
* @param {*} value Value to set for parameter.
|
|
|
|
*/
|
2016-10-13 13:11:26 -04:00
|
|
|
Thread.prototype.pushParam = function (paramName, value) {
|
|
|
|
var stackFrame = this.peekStackFrame();
|
|
|
|
stackFrame.params[paramName] = value;
|
|
|
|
};
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
/**
|
|
|
|
* Get a parameter at the lowest possible level of the stack.
|
|
|
|
* @param {!string} paramName Name of parameter.
|
|
|
|
* @return {*} value Value for parameter.
|
|
|
|
*/
|
2016-10-13 13:11:26 -04:00
|
|
|
Thread.prototype.getParam = function (paramName) {
|
|
|
|
for (var i = this.stackFrames.length - 1; i >= 0; i--) {
|
|
|
|
var frame = this.stackFrames[i];
|
|
|
|
if (frame.params.hasOwnProperty(paramName)) {
|
|
|
|
return frame.params[paramName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2016-08-23 18:12:19 -04:00
|
|
|
/**
|
|
|
|
* Whether the current execution of a thread is at the top of the stack.
|
|
|
|
* @return {Boolean} True if execution is at top of the stack.
|
|
|
|
*/
|
|
|
|
Thread.prototype.atStackTop = function () {
|
|
|
|
return this.peekStack() === this.topBlock;
|
|
|
|
};
|
|
|
|
|
2016-06-09 14:22:58 -04:00
|
|
|
|
2016-09-15 19:37:12 -04:00
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Switch the thread to the next block at the current level of the stack.
|
|
|
|
* For example, this is used in a standard sequence of blocks,
|
|
|
|
* where execution proceeds from one block to the next.
|
2016-09-15 19:37:12 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Thread.prototype.goToNextBlock = function () {
|
|
|
|
var nextBlockId = this.target.blocks.getNextBlock(this.peekStack());
|
|
|
|
// Copy warp mode to next block.
|
|
|
|
var warpMode = this.peekStackFrame().warpMode;
|
|
|
|
// The current block is on the stack - pop it and push the next.
|
|
|
|
// Note that this could push `null` - that is handled by the sequencer.
|
|
|
|
this.popStack();
|
|
|
|
this.pushStack(nextBlockId);
|
|
|
|
if (this.peekStackFrame()) {
|
|
|
|
this.peekStackFrame().warpMode = warpMode;
|
|
|
|
}
|
2016-09-15 19:37:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Attempt to determine whether a procedure call is recursive,
|
|
|
|
* by examining the stack.
|
|
|
|
* @param {!string} procedureCode Procedure code of procedure being called.
|
|
|
|
* @return {boolean} True if the call appears recursive.
|
2016-09-15 19:37:12 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Thread.prototype.isRecursiveCall = function (procedureCode) {
|
|
|
|
var callCount = 5; // Max number of enclosing procedure calls to examine.
|
|
|
|
var sp = this.stack.length - 1;
|
|
|
|
for (var i = sp - 1; i >= 0; i--) {
|
|
|
|
var block = this.target.blocks.getBlock(this.stack[i]);
|
|
|
|
if (block.opcode == 'procedures_callnoreturn' &&
|
2016-10-23 12:41:45 -04:00
|
|
|
block.mutation.proccode == procedureCode) {
|
2016-10-17 23:23:16 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (--callCount < 0) return false;
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-15 19:37:12 -04:00
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Thread;
|