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');
|
|
|
|
var YieldTimers = require('../util/yieldtimers.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
|
|
|
|
2016-06-07 21:05:32 -04:00
|
|
|
/**
|
|
|
|
* If set, block calls, args, and return values will be logged to the console.
|
|
|
|
* @const {boolean}
|
|
|
|
*/
|
|
|
|
Sequencer.DEBUG_BLOCK_CALLS = true;
|
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
/**
|
|
|
|
* Step through all threads in `this.threads`, running them in order.
|
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-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 = [];
|
|
|
|
// 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.
|
|
|
|
this.stepThread(activeThread);
|
|
|
|
} else if (activeThread.status === Thread.STATUS_YIELD) {
|
|
|
|
// Yield-mode thread: check if the time has passed.
|
|
|
|
YieldTimers.resolve(activeThread.yieldTimerId);
|
|
|
|
numYieldingThreads++;
|
|
|
|
} else if (activeThread.status === Thread.STATUS_DONE) {
|
|
|
|
// Moved to a done state - finish up
|
|
|
|
activeThread.status = Thread.STATUS_RUNNING;
|
|
|
|
// @todo Deal with the return value
|
|
|
|
}
|
2016-05-03 13:53:52 -04:00
|
|
|
// First attempt to pop from the stack
|
|
|
|
if (activeThread.stack.length > 0 &&
|
|
|
|
activeThread.nextBlock === null &&
|
2016-05-02 18:09:02 -04:00
|
|
|
activeThread.status === Thread.STATUS_DONE) {
|
2016-05-03 13:53:52 -04:00
|
|
|
activeThread.nextBlock = activeThread.stack.pop();
|
|
|
|
// Don't pop stack frame - we need the data.
|
|
|
|
// A new one won't be created when we execute.
|
|
|
|
if (activeThread.nextBlock !== null) {
|
|
|
|
activeThread.status === Thread.STATUS_RUNNING;
|
2016-05-03 13:28:24 -04:00
|
|
|
}
|
2016-05-03 13:53:52 -04:00
|
|
|
}
|
2016-05-02 18:09:02 -04:00
|
|
|
if (activeThread.nextBlock === null &&
|
|
|
|
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-04-26 15:51:14 -04:00
|
|
|
Sequencer.prototype.stepThread = function (thread) {
|
2016-05-02 18:09:02 -04:00
|
|
|
// Save the yield timer ID, in case a primitive makes a new one
|
|
|
|
// @todo hack - perhaps patch this to allow more than one timer per
|
|
|
|
// primitive, for example...
|
|
|
|
var oldYieldTimerId = YieldTimers.timerId;
|
|
|
|
|
2016-05-02 15:35:29 -04:00
|
|
|
// Save the current block and set the nextBlock.
|
|
|
|
// If the primitive would like to do control flow,
|
|
|
|
// it can overwrite nextBlock.
|
|
|
|
var currentBlock = thread.nextBlock;
|
2016-06-06 15:29:34 -04:00
|
|
|
if (!currentBlock || !this.runtime.blocks.getBlock(currentBlock)) {
|
2016-05-03 13:45:22 -04:00
|
|
|
thread.status = Thread.STATUS_DONE;
|
|
|
|
return;
|
|
|
|
}
|
2016-06-06 15:29:34 -04:00
|
|
|
thread.nextBlock = this.runtime.blocks.getNextBlock(currentBlock);
|
2016-05-02 15:35:29 -04:00
|
|
|
|
2016-06-06 15:29:34 -04:00
|
|
|
var opcode = this.runtime.blocks.getOpcode(currentBlock);
|
2016-05-02 12:20:27 -04:00
|
|
|
|
2016-05-03 13:28:24 -04:00
|
|
|
// Push the current block to the stack
|
|
|
|
thread.stack.push(currentBlock);
|
|
|
|
// Push an empty stack frame, if we need one.
|
|
|
|
// Might not, if we just popped the stack.
|
|
|
|
if (thread.stack.length > thread.stackFrames.length) {
|
|
|
|
thread.stackFrames.push({});
|
|
|
|
}
|
|
|
|
var currentStackFrame = thread.stackFrames[thread.stackFrames.length - 1];
|
|
|
|
|
2016-05-02 18:09:02 -04:00
|
|
|
/**
|
|
|
|
* A callback for the primitive to indicate its thread should yield.
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
var threadYieldCallback = function () {
|
|
|
|
thread.status = Thread.STATUS_YIELD;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback for the primitive to indicate its thread is finished
|
|
|
|
* @type {Function}
|
|
|
|
*/
|
|
|
|
var instance = this;
|
|
|
|
var threadDoneCallback = function () {
|
|
|
|
thread.status = Thread.STATUS_DONE;
|
2016-05-03 13:28:24 -04:00
|
|
|
// Refresh nextBlock in case it has changed during a yield.
|
2016-06-06 15:29:34 -04:00
|
|
|
thread.nextBlock = instance.runtime.blocks.getNextBlock(currentBlock);
|
2016-05-03 13:28:24 -04:00
|
|
|
// Pop the stack and stack frame
|
|
|
|
thread.stack.pop();
|
|
|
|
thread.stackFrames.pop();
|
2016-06-07 20:59:34 -04:00
|
|
|
// Stop showing run feedback in the editor.
|
|
|
|
instance.runtime.glowBlock(currentBlock, false);
|
2016-05-02 18:09:02 -04:00
|
|
|
};
|
|
|
|
|
2016-05-03 14:57:10 -04:00
|
|
|
/**
|
|
|
|
* A callback for the primitive to start hats.
|
|
|
|
* @todo very hacked...
|
|
|
|
*/
|
|
|
|
var startHats = function(callback) {
|
2016-06-06 15:29:34 -04:00
|
|
|
var stacks = instance.runtime.blocks.getStacks();
|
|
|
|
for (var i = 0; i < stacks.length; i++) {
|
|
|
|
var stack = stacks[i];
|
|
|
|
var stackBlock = instance.runtime.blocks.getBlock(stack);
|
2016-05-03 14:57:10 -04:00
|
|
|
var result = callback(stackBlock);
|
|
|
|
if (result) {
|
|
|
|
// Check if the stack is already running
|
|
|
|
var stackRunning = false;
|
|
|
|
|
|
|
|
for (var j = 0; j < instance.runtime.threads.length; j++) {
|
|
|
|
if (instance.runtime.threads[j].topBlock == stack) {
|
|
|
|
stackRunning = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!stackRunning) {
|
|
|
|
instance.runtime._pushThread(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-03 11:45:25 -04:00
|
|
|
/**
|
2016-05-03 13:28:24 -04:00
|
|
|
* Record whether we have switched stack,
|
|
|
|
* to avoid proceeding the thread automatically.
|
|
|
|
* @type {boolean}
|
2016-05-03 11:45:25 -04:00
|
|
|
*/
|
2016-05-03 13:28:24 -04:00
|
|
|
var switchedStack = false;
|
2016-05-03 11:45:25 -04:00
|
|
|
/**
|
2016-05-03 13:28:24 -04:00
|
|
|
* A callback for a primitive to start a substack.
|
2016-05-03 11:45:25 -04:00
|
|
|
* @type {Function}
|
|
|
|
*/
|
2016-05-03 13:28:24 -04:00
|
|
|
var threadStartSubstack = function () {
|
|
|
|
// Set nextBlock to the start of the substack
|
2016-06-06 15:29:34 -04:00
|
|
|
var substack = instance.runtime.blocks.getSubstack(currentBlock);
|
2016-05-03 13:45:22 -04:00
|
|
|
if (substack && substack.value) {
|
|
|
|
thread.nextBlock = substack.value;
|
|
|
|
} else {
|
|
|
|
thread.nextBlock = null;
|
|
|
|
}
|
2016-05-03 13:28:24 -04:00
|
|
|
switchedStack = true;
|
2016-05-02 18:09:02 -04:00
|
|
|
};
|
|
|
|
|
2016-05-03 14:25:12 -04:00
|
|
|
// @todo extreme hack to get the single argument value for prototype
|
2016-05-02 18:09:02 -04:00
|
|
|
var argValues = [];
|
2016-06-06 15:29:34 -04:00
|
|
|
var blockInputs = this.runtime.blocks.getBlock(currentBlock).fields;
|
2016-05-03 14:25:12 -04:00
|
|
|
for (var bi in blockInputs) {
|
|
|
|
var outer = blockInputs[bi];
|
|
|
|
for (var b in outer.blocks) {
|
|
|
|
var block = outer.blocks[b];
|
|
|
|
var fields = block.fields;
|
|
|
|
for (var f in fields) {
|
|
|
|
var field = fields[f];
|
|
|
|
argValues.push(field.value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-02 18:09:02 -04:00
|
|
|
|
2016-06-07 20:59:34 -04:00
|
|
|
// Start showing run feedback in the editor.
|
|
|
|
this.runtime.glowBlock(currentBlock, true);
|
|
|
|
|
2016-05-02 14:54:32 -04:00
|
|
|
if (!opcode) {
|
2016-05-02 15:35:29 -04:00
|
|
|
console.warn('Could not get opcode for block: ' + currentBlock);
|
2016-05-02 14:54:32 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
var blockFunction = this.runtime.getOpcodeFunction(opcode);
|
|
|
|
if (!blockFunction) {
|
|
|
|
console.warn('Could not get implementation for opcode: ' + opcode);
|
2016-05-02 12:20:27 -04:00
|
|
|
}
|
|
|
|
else {
|
2016-06-07 21:05:32 -04:00
|
|
|
if (Sequencer.DEBUG_BLOCK_CALLS) {
|
|
|
|
console.groupCollapsed('Executing: ' + opcode);
|
|
|
|
console.log('with arguments: ', argValues);
|
|
|
|
console.log('and stack frame: ', currentStackFrame);
|
|
|
|
}
|
|
|
|
var blockFunctionReturnValue = null;
|
2016-05-02 14:54:32 -04:00
|
|
|
try {
|
2016-05-02 18:09:02 -04:00
|
|
|
// @todo deal with the return value
|
2016-06-07 21:05:32 -04:00
|
|
|
blockFunctionReturnValue = blockFunction(argValues, {
|
2016-05-02 18:09:02 -04:00
|
|
|
yield: threadYieldCallback,
|
|
|
|
done: threadDoneCallback,
|
2016-05-03 11:45:25 -04:00
|
|
|
timeout: YieldTimers.timeout,
|
2016-05-03 13:28:24 -04:00
|
|
|
stackFrame: currentStackFrame,
|
2016-05-03 14:57:10 -04:00
|
|
|
startSubstack: threadStartSubstack,
|
|
|
|
startHats: startHats
|
2016-05-02 18:09:02 -04:00
|
|
|
});
|
2016-05-02 14:07:40 -04:00
|
|
|
}
|
2016-05-02 14:54:32 -04:00
|
|
|
catch(e) {
|
2016-05-03 11:55:55 -04:00
|
|
|
console.error(
|
|
|
|
'Exception calling block function for opcode: ' +
|
|
|
|
opcode + '\n' + e);
|
2016-05-02 18:09:02 -04:00
|
|
|
} finally {
|
|
|
|
// Update if the thread has set a yield timer ID
|
|
|
|
// @todo hack
|
|
|
|
if (YieldTimers.timerId > oldYieldTimerId) {
|
|
|
|
thread.yieldTimerId = YieldTimers.timerId;
|
|
|
|
}
|
2016-05-03 13:28:24 -04:00
|
|
|
if (thread.status === Thread.STATUS_RUNNING && !switchedStack) {
|
2016-05-02 18:09:02 -04:00
|
|
|
// Thread executed without yielding - move to done
|
2016-05-03 13:28:24 -04:00
|
|
|
threadDoneCallback();
|
2016-05-02 18:09:02 -04:00
|
|
|
}
|
2016-06-07 21:05:32 -04:00
|
|
|
if (Sequencer.DEBUG_BLOCK_CALLS) {
|
|
|
|
console.log('ending stack frame: ', currentStackFrame);
|
|
|
|
console.log('returned: ', blockFunctionReturnValue);
|
|
|
|
console.groupEnd();
|
|
|
|
}
|
2016-05-02 14:07:40 -04:00
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
}
|
2016-05-02 14:54:32 -04:00
|
|
|
}
|
2016-05-02 12:20:27 -04:00
|
|
|
|
2016-04-26 15:00:45 -04:00
|
|
|
};
|
|
|
|
|
2016-04-18 17:20:30 -04:00
|
|
|
module.exports = Sequencer;
|