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-04-26 15:00:45 -04:00
|
|
|
/**
|
2016-05-02 13:05:48 -04:00
|
|
|
* ID of next block that the thread will execute, or null if none.
|
|
|
|
* @type {?string}
|
2016-04-26 15:00:45 -04:00
|
|
|
*/
|
2016-04-26 16:50:49 -04:00
|
|
|
this.nextBlock = firstBlock;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Status of the thread, one of three states (below)
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.status = 0; /* Thread.STATUS_RUNNING */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Yield timer ID (for checking when the thread should unyield).
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
this.yieldTimerId = -1;
|
2016-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 18:09:02 -04:00
|
|
|
/**
|
|
|
|
* Thread status for initialized or running thread.
|
|
|
|
* Threads are in this state when the primitive is called for the first time.
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Thread.STATUS_RUNNING = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread status for a yielded thread.
|
|
|
|
* Threads are in this state when a primitive has yielded.
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Thread.STATUS_YIELD = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread status for a finished/done thread.
|
|
|
|
* Thread is moved to this state when the interpreter
|
|
|
|
* can proceed with execution.
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
Thread.STATUS_DONE = 2;
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Thread;
|