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-04-26 16:50:49 -04:00
|
|
|
function Sequencer (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-04-18 17:20:30 -04:00
|
|
|
}
|
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
/**
|
|
|
|
* The sequencer does as much work as it can within WORK_TIME milliseconds,
|
|
|
|
* then yields. This is essentially a rate-limiter for blocks.
|
|
|
|
* In Scratch 2.0, this is set to 75% of the target stage frame-rate (30fps).
|
|
|
|
* @const {!number}
|
|
|
|
*/
|
2016-05-05 13:09:37 -04:00
|
|
|
Sequencer.WORK_TIME = 10;
|
2016-04-26 15:00:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Step through all threads in `this.threads`, running them in order.
|
2016-06-08 16:57:08 -04:00
|
|
|
* @param {Array.<Thread>} threads List of which threads to step.
|
2016-04-26 16:50:49 -04:00
|
|
|
* @return {Array.<Thread>} All threads which have finished in this iteration.
|
2016-04-26 15:00:45 -04:00
|
|
|
*/
|
2016-04-26 15:51:14 -04:00
|
|
|
Sequencer.prototype.stepThreads = function (threads) {
|
2016-04-26 15:00:45 -04:00
|
|
|
// Start counting toward WORK_TIME
|
|
|
|
this.timer.start();
|
2016-04-26 16:50:49 -04:00
|
|
|
// List of threads which have been killed by this step.
|
|
|
|
var inactiveThreads = [];
|
2016-05-02 18:09:02 -04:00
|
|
|
// If all of the threads are yielding, we should yield.
|
|
|
|
var numYieldingThreads = 0;
|
2016-07-01 10:44:43 -04:00
|
|
|
// Clear all yield statuses that were for the previous frame.
|
|
|
|
for (var t = 0; t < threads.length; t++) {
|
|
|
|
if (threads[t].status === Thread.STATUS_YIELD_FRAME) {
|
|
|
|
threads[t].setStatus(Thread.STATUS_RUNNING);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
// While there are still threads to run and we are within WORK_TIME,
|
|
|
|
// continue executing threads.
|
2016-04-26 15:51:14 -04:00
|
|
|
while (threads.length > 0 &&
|
2016-05-02 18:09:02 -04:00
|
|
|
threads.length > numYieldingThreads &&
|
2016-04-26 15:00:45 -04:00
|
|
|
this.timer.timeElapsed() < Sequencer.WORK_TIME) {
|
|
|
|
// New threads at the end of the iteration.
|
|
|
|
var newThreads = [];
|
2016-06-10 13:38:35 -04:00
|
|
|
// Reset yielding thread count.
|
|
|
|
numYieldingThreads = 0;
|
2016-04-26 15:00:45 -04:00
|
|
|
// Attempt to run each thread one time
|
2016-04-26 15:51:14 -04:00
|
|
|
for (var i = 0; i < threads.length; i++) {
|
|
|
|
var activeThread = threads[i];
|
2016-05-02 18:09:02 -04:00
|
|
|
if (activeThread.status === Thread.STATUS_RUNNING) {
|
|
|
|
// Normal-mode thread: step.
|
2016-06-09 17:08:30 -04:00
|
|
|
this.startThread(activeThread);
|
2016-07-01 10:44:43 -04:00
|
|
|
} else if (activeThread.status === Thread.STATUS_YIELD ||
|
|
|
|
activeThread.status === Thread.STATUS_YIELD_FRAME) {
|
2016-06-30 17:06:50 -04:00
|
|
|
// Yielding thread: do nothing for this step.
|
2016-07-01 10:44:43 -04:00
|
|
|
numYieldingThreads++;
|
2016-05-02 18:09:02 -04:00
|
|
|
}
|
2016-06-09 17:08:30 -04:00
|
|
|
if (activeThread.stack.length === 0 &&
|
2016-05-02 18:09:02 -04:00
|
|
|
activeThread.status === Thread.STATUS_DONE) {
|
2016-05-03 13:53:52 -04:00
|
|
|
// Finished with this thread - tell runtime to clean it up.
|
2016-04-26 16:50:49 -04:00
|
|
|
inactiveThreads.push(activeThread);
|
2016-05-02 18:09:02 -04:00
|
|
|
} else {
|
|
|
|
// Keep this thead in the loop.
|
|
|
|
newThreads.push(activeThread);
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Effectively filters out threads that have stopped.
|
2016-04-26 15:51:14 -04:00
|
|
|
threads = newThreads;
|
2016-04-26 15:00:45 -04:00
|
|
|
}
|
2016-04-26 16:50:49 -04:00
|
|
|
return inactiveThreads;
|
2016-04-26 15:00:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Step the requested thread
|
|
|
|
* @param {!Thread} thread Thread object to step
|
|
|
|
*/
|
2016-06-09 17:08:30 -04:00
|
|
|
Sequencer.prototype.startThread = function (thread) {
|
|
|
|
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-07-01 11:24:06 -04:00
|
|
|
// Yield for the frame.
|
2016-06-10 08:47:54 -04:00
|
|
|
thread.popStack();
|
2016-07-01 11:24:06 -04:00
|
|
|
thread.setStatus(Thread.STATUS_YIELD_FRAME);
|
2016-05-03 13:45:22 -04:00
|
|
|
return;
|
|
|
|
}
|
2016-06-09 17:08:30 -04:00
|
|
|
// Execute the current block
|
|
|
|
execute(this, thread);
|
|
|
|
// If the block executed without yielding and without doing control flow,
|
|
|
|
// move to done.
|
|
|
|
if (thread.status === Thread.STATUS_RUNNING &&
|
|
|
|
thread.peekStack() === currentBlockId) {
|
2016-06-17 15:10:12 -04:00
|
|
|
this.proceedThread(thread);
|
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.
|
|
|
|
* @param {Number} branchNum Which branch to step to (i.e., 1, 2).
|
2016-06-09 14:23:34 -04:00
|
|
|
*/
|
2016-08-10 11:27:21 -04:00
|
|
|
Sequencer.prototype.stepToBranch = function (thread, branchNum) {
|
|
|
|
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-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
|
|
|
// Push null, so we come back to the current block.
|
|
|
|
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.
|
|
|
|
* @param {!string} procedureName Name of procedure defined in this target.
|
|
|
|
*/
|
|
|
|
Sequencer.prototype.stepToProcedure = function (thread, procedureName) {
|
|
|
|
var definition = thread.target.blocks.getProcedureDefinition(procedureName);
|
|
|
|
thread.pushStack(definition);
|
|
|
|
};
|
2016-06-09 14:23:34 -04:00
|
|
|
|
2016-06-20 15:04:28 -04:00
|
|
|
/**
|
|
|
|
* Step a thread into an input reporter, and manage its status appropriately.
|
|
|
|
* @param {!Thread} thread Thread object to step to reporter.
|
|
|
|
* @param {!string} blockId ID of reporter block.
|
|
|
|
* @param {!string} inputName Name of input on parent block.
|
|
|
|
* @return {boolean} True if yielded, false if it finished immediately.
|
|
|
|
*/
|
|
|
|
Sequencer.prototype.stepToReporter = function (thread, blockId, inputName) {
|
|
|
|
var currentStackFrame = thread.peekStackFrame();
|
|
|
|
// Push to the stack to evaluate the reporter block.
|
|
|
|
thread.pushStack(blockId);
|
|
|
|
// Save name of input for `Thread.pushReportedValue`.
|
|
|
|
currentStackFrame.waitingReporter = inputName;
|
|
|
|
// Actually execute the block.
|
|
|
|
this.startThread(thread);
|
2016-06-30 17:12:16 -04:00
|
|
|
// If a reporter yielded, caller must wait for it to unyield.
|
|
|
|
// The value will be populated once the reporter unyields,
|
|
|
|
// and passed up to the currentStackFrame on next execution.
|
|
|
|
return thread.status === Thread.STATUS_YIELD;
|
2016-06-20 15:04:28 -04:00
|
|
|
};
|
|
|
|
|
2016-06-09 14:23:34 -04:00
|
|
|
/**
|
|
|
|
* Finish stepping a thread and proceed it to the next block.
|
|
|
|
* @param {!Thread} thread Thread object to proceed.
|
|
|
|
*/
|
2016-06-09 17:08:30 -04:00
|
|
|
Sequencer.prototype.proceedThread = function (thread) {
|
|
|
|
var currentBlockId = thread.peekStack();
|
|
|
|
// Mark the status as done and proceed to the next block.
|
|
|
|
// Pop from the stack - finished this level of execution.
|
|
|
|
thread.popStack();
|
|
|
|
// Push next connected block, if there is one.
|
2016-09-15 19:37:12 -04:00
|
|
|
var nextBlockId = thread.target.blocks.getNextBlock(currentBlockId);
|
2016-06-09 17:08:30 -04:00
|
|
|
if (nextBlockId) {
|
|
|
|
thread.pushStack(nextBlockId);
|
|
|
|
}
|
2016-07-01 11:24:06 -04:00
|
|
|
// If we can't find a next block to run, mark the thread as done.
|
|
|
|
if (!thread.peekStack()) {
|
2016-07-01 10:44:43 -04:00
|
|
|
thread.setStatus(Thread.STATUS_DONE);
|
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-08-23 15:53:34 -04:00
|
|
|
thread.setStatus(Thread.STATUS_DONE);
|
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Sequencer;
|