2016-04-26 17:06:24 -04:00
|
|
|
var Timer = require('../util/timer');
|
2016-05-02 18:09:02 -04:00
|
|
|
var Thread = require('./thread');
|
2016-06-09 13:27:30 -04:00
|
|
|
var execute = require('./execute.js');
|
2016-04-26 15:00:45 -04:00
|
|
|
|
2016-10-23 17:55:31 -04:00
|
|
|
var Sequencer = function (runtime) {
|
2016-04-26 15:00:45 -04:00
|
|
|
/**
|
|
|
|
* A utility timer for timing thread sequencing.
|
|
|
|
* @type {!Timer}
|
|
|
|
*/
|
|
|
|
this.timer = new Timer();
|
2016-04-26 16:50:49 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the runtime owning this sequencer.
|
|
|
|
* @type {!Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
2016-10-23 17:55:31 -04:00
|
|
|
};
|
2016-04-18 17:20:30 -04:00
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Time to run a warp-mode thread, in ms.
|
|
|
|
* @type {number}
|
2016-04-26 15:00:45 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Sequencer.WARP_TIME = 500;
|
2016-04-26 15:00:45 -04:00
|
|
|
|
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Step through all threads in `this.runtime.threads`, running them in order.
|
|
|
|
* @return {Array.<!Thread>} List of inactive threads after stepping.
|
2016-04-26 15:00:45 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Sequencer.prototype.stepThreads = function () {
|
|
|
|
// Work time is 75% of the thread stepping interval.
|
|
|
|
var WORK_TIME = 0.75 * this.runtime.currentStepTime;
|
|
|
|
// Start counting toward WORK_TIME.
|
2016-04-26 15:00:45 -04:00
|
|
|
this.timer.start();
|
2016-10-17 23:23:16 -04:00
|
|
|
// Count of active threads.
|
|
|
|
var numActiveThreads = Infinity;
|
|
|
|
// Whether `stepThreads` has run through a full single tick.
|
|
|
|
var ranFirstTick = false;
|
2016-11-28 10:49:05 -05:00
|
|
|
var doneThreads = [];
|
2016-10-17 23:23:16 -04:00
|
|
|
// 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.
|
|
|
|
// 3. Either turbo mode, or no redraw has been requested by a primitive.
|
|
|
|
while (this.runtime.threads.length > 0 &&
|
|
|
|
numActiveThreads > 0 &&
|
|
|
|
this.timer.timeElapsed() < WORK_TIME &&
|
|
|
|
(this.runtime.turboMode || !this.runtime.redrawRequested)) {
|
|
|
|
numActiveThreads = 0;
|
|
|
|
// Attempt to run each thread one time.
|
2016-11-10 15:05:49 -05:00
|
|
|
for (var i = 0; i < this.runtime.threads.length; i++) {
|
|
|
|
var activeThread = this.runtime.threads[i];
|
2016-10-17 23:23:16 -04:00
|
|
|
if (activeThread.stack.length === 0 ||
|
|
|
|
activeThread.status === Thread.STATUS_DONE) {
|
|
|
|
// Finished with this thread.
|
2016-11-28 10:49:05 -05:00
|
|
|
if (doneThreads.indexOf(activeThread) < 0) {
|
|
|
|
doneThreads.push(activeThread);
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (activeThread.status === Thread.STATUS_YIELD_TICK &&
|
|
|
|
!ranFirstTick) {
|
|
|
|
// Clear single-tick yield from the last call of `stepThreads`.
|
|
|
|
activeThread.status = Thread.STATUS_RUNNING;
|
|
|
|
}
|
|
|
|
if (activeThread.status === Thread.STATUS_RUNNING ||
|
|
|
|
activeThread.status === Thread.STATUS_YIELD) {
|
2016-05-02 18:09:02 -04:00
|
|
|
// Normal-mode thread: step.
|
2016-10-17 23:23:16 -04:00
|
|
|
this.stepThread(activeThread);
|
|
|
|
activeThread.warpTimer = null;
|
2016-05-02 18:09:02 -04:00
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
if (activeThread.status === Thread.STATUS_RUNNING) {
|
|
|
|
numActiveThreads++;
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
// We successfully ticked once. Prevents running STATUS_YIELD_TICK
|
|
|
|
// threads on the next tick.
|
|
|
|
ranFirstTick = true;
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
// Filter inactive threads from `this.runtime.threads`.
|
2016-10-23 12:41:45 -04:00
|
|
|
this.runtime.threads = this.runtime.threads.filter(function (thread) {
|
2016-11-28 10:49:05 -05:00
|
|
|
if (doneThreads.indexOf(thread) > -1) {
|
2016-10-17 23:23:16 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2016-11-28 10:49:05 -05:00
|
|
|
return doneThreads;
|
2016-04-26 15:00:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-10-17 23:23:16 -04:00
|
|
|
* Step the requested thread for as long as necessary.
|
|
|
|
* @param {!Thread} thread Thread object to step.
|
2016-04-26 15:00:45 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Sequencer.prototype.stepThread = function (thread) {
|
2016-06-09 17:08:30 -04:00
|
|
|
var currentBlockId = thread.peekStack();
|
2016-06-10 08:47:54 -04:00
|
|
|
if (!currentBlockId) {
|
2016-08-10 11:27:21 -04:00
|
|
|
// A "null block" - empty branch.
|
2016-06-10 08:47:54 -04:00
|
|
|
thread.popStack();
|
2016-05-03 13:45:22 -04:00
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
while (thread.peekStack()) {
|
|
|
|
var isWarpMode = thread.peekStackFrame().warpMode;
|
|
|
|
if (isWarpMode && !thread.warpTimer) {
|
|
|
|
// Initialize warp-mode timer if it hasn't been already.
|
|
|
|
// This will start counting the thread toward `Sequencer.WARP_TIME`.
|
|
|
|
thread.warpTimer = new Timer();
|
|
|
|
thread.warpTimer.start();
|
|
|
|
}
|
|
|
|
// Execute the current block.
|
|
|
|
// Save the current block ID to notice if we did control flow.
|
|
|
|
currentBlockId = thread.peekStack();
|
|
|
|
execute(this, thread);
|
|
|
|
thread.blockGlowInFrame = currentBlockId;
|
|
|
|
// If the thread has yielded or is waiting, yield to other threads.
|
|
|
|
if (thread.status === Thread.STATUS_YIELD) {
|
|
|
|
// Mark as running for next iteration.
|
|
|
|
thread.status = Thread.STATUS_RUNNING;
|
|
|
|
// In warp mode, yielded blocks are re-executed immediately.
|
|
|
|
if (isWarpMode &&
|
|
|
|
thread.warpTimer.timeElapsed() <= Sequencer.WARP_TIME) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (thread.status === Thread.STATUS_PROMISE_WAIT) {
|
|
|
|
// A promise was returned by the primitive. Yield the thread
|
|
|
|
// until the promise resolves. Promise resolution should reset
|
|
|
|
// thread.status to Thread.STATUS_RUNNING.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If no control flow has happened, switch to next block.
|
|
|
|
if (thread.peekStack() === currentBlockId) {
|
|
|
|
thread.goToNextBlock();
|
|
|
|
}
|
|
|
|
// If no next block has been found at this point, look on the stack.
|
|
|
|
while (!thread.peekStack()) {
|
|
|
|
thread.popStack();
|
2017-01-26 06:29:58 -05:00
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
if (thread.stack.length === 0) {
|
|
|
|
// No more stack to run!
|
|
|
|
thread.status = Thread.STATUS_DONE;
|
|
|
|
return;
|
|
|
|
}
|
2017-01-28 09:11:48 -05:00
|
|
|
|
|
|
|
var stackFrame = thread.peekStackFrame();
|
|
|
|
isWarpMode = stackFrame.warpMode;
|
2017-01-26 06:29:58 -05:00
|
|
|
|
|
|
|
if (stackFrame.isLoop) {
|
2016-10-17 23:23:16 -04:00
|
|
|
// The current level of the stack is marked as a loop.
|
|
|
|
// Return to yield for the frame/tick in general.
|
|
|
|
// Unless we're in warp mode - then only return if the
|
|
|
|
// warp timer is up.
|
|
|
|
if (!isWarpMode ||
|
|
|
|
thread.warpTimer.timeElapsed() > Sequencer.WARP_TIME) {
|
|
|
|
// Don't do anything to the stack, since loops need
|
|
|
|
// to be re-executed.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Don't go to the next block for this level of the stack,
|
|
|
|
// since loops need to be re-executed.
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-26 06:29:58 -05:00
|
|
|
} else if (stackFrame.waitingReporter) {
|
2016-10-17 23:23:16 -04:00
|
|
|
// This level of the stack was waiting for a value.
|
|
|
|
// This means a reporter has just returned - so don't go
|
|
|
|
// to the next block for this level of the stack.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get next block of existing block on the stack.
|
|
|
|
thread.goToNextBlock();
|
|
|
|
}
|
2016-06-09 14:23:34 -04:00
|
|
|
}
|
|
|
|
};
|
2016-05-02 15:35:29 -04:00
|
|
|
|
2016-06-09 14:23:34 -04:00
|
|
|
/**
|
2016-08-10 11:27:21 -04:00
|
|
|
* Step a thread into a block's branch.
|
|
|
|
* @param {!Thread} thread Thread object to step to branch.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @param {number} branchNum Which branch to step to (i.e., 1, 2).
|
|
|
|
* @param {boolean} isLoop Whether this block is a loop.
|
2016-06-09 14:23:34 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Sequencer.prototype.stepToBranch = function (thread, branchNum, isLoop) {
|
2016-08-10 11:27:21 -04:00
|
|
|
if (!branchNum) {
|
|
|
|
branchNum = 1;
|
2016-06-10 10:40:15 -04:00
|
|
|
}
|
2016-06-09 17:08:30 -04:00
|
|
|
var currentBlockId = thread.peekStack();
|
2016-09-15 19:37:12 -04:00
|
|
|
var branchId = thread.target.blocks.getBranch(
|
2016-06-10 10:40:15 -04:00
|
|
|
currentBlockId,
|
2016-08-10 11:27:21 -04:00
|
|
|
branchNum
|
2016-06-10 10:40:15 -04:00
|
|
|
);
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.peekStackFrame().isLoop = isLoop;
|
2016-08-10 11:27:21 -04:00
|
|
|
if (branchId) {
|
|
|
|
// Push branch ID to the thread's stack.
|
|
|
|
thread.pushStack(branchId);
|
2016-06-09 14:23:34 -04:00
|
|
|
} else {
|
2016-06-09 17:08:30 -04:00
|
|
|
thread.pushStack(null);
|
2016-06-09 14:23:34 -04:00
|
|
|
}
|
|
|
|
};
|
2016-10-03 17:43:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Step a procedure.
|
|
|
|
* @param {!Thread} thread Thread object to step to procedure.
|
2016-10-17 23:23:16 -04:00
|
|
|
* @param {!string} procedureCode Procedure code of procedure to step to.
|
2016-10-03 17:43:24 -04:00
|
|
|
*/
|
2016-10-17 23:23:16 -04:00
|
|
|
Sequencer.prototype.stepToProcedure = function (thread, procedureCode) {
|
|
|
|
var definition = thread.target.blocks.getProcedureDefinition(procedureCode);
|
|
|
|
if (!definition) {
|
|
|
|
return;
|
2016-06-09 17:08:30 -04:00
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
// Check if the call is recursive.
|
|
|
|
// If so, set the thread to yield after pushing.
|
|
|
|
var isRecursive = thread.isRecursiveCall(procedureCode);
|
|
|
|
// To step to a procedure, we put its definition on the stack.
|
|
|
|
// Execution for the thread will proceed through the definition hat
|
|
|
|
// and on to the main definition of the procedure.
|
|
|
|
// When that set of blocks finishes executing, it will be popped
|
|
|
|
// from the stack by the sequencer, returning control to the caller.
|
|
|
|
thread.pushStack(definition);
|
|
|
|
// In known warp-mode threads, only yield when time is up.
|
|
|
|
if (thread.peekStackFrame().warpMode &&
|
|
|
|
thread.warpTimer.timeElapsed() > Sequencer.WARP_TIME) {
|
|
|
|
thread.status = Thread.STATUS_YIELD;
|
|
|
|
} else {
|
|
|
|
// Look for warp-mode flag on definition, and set the thread
|
|
|
|
// to warp-mode if needed.
|
|
|
|
var definitionBlock = thread.target.blocks.getBlock(definition);
|
|
|
|
var doWarp = definitionBlock.mutation.warp;
|
|
|
|
if (doWarp) {
|
|
|
|
thread.peekStackFrame().warpMode = true;
|
2016-10-24 13:01:41 -04:00
|
|
|
} else {
|
2016-10-17 23:23:16 -04:00
|
|
|
// In normal-mode threads, yield any time we have a recursive call.
|
2016-10-24 13:01:41 -04:00
|
|
|
if (isRecursive) {
|
|
|
|
thread.status = Thread.STATUS_YIELD;
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2016-06-30 17:12:16 -04:00
|
|
|
}
|
2016-04-26 15:00:45 -04:00
|
|
|
};
|
|
|
|
|
2016-08-23 15:53:34 -04:00
|
|
|
/**
|
|
|
|
* Retire a thread in the middle, without considering further blocks.
|
|
|
|
* @param {!Thread} thread Thread object to retire.
|
|
|
|
*/
|
|
|
|
Sequencer.prototype.retireThread = function (thread) {
|
|
|
|
thread.stack = [];
|
|
|
|
thread.stackFrame = [];
|
2016-09-15 13:51:40 -04:00
|
|
|
thread.requestScriptGlowInFrame = false;
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.status = Thread.STATUS_DONE;
|
2016-08-23 15:53:34 -04:00
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Sequencer;
|