mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 06:23:37 -05:00
Pull sequencer-related callbacks out of execute
This commit is contained in:
parent
9df470255e
commit
c21700380f
2 changed files with 69 additions and 125 deletions
|
@ -1,7 +1,12 @@
|
|||
var Thread = require('./thread');
|
||||
var YieldTimers = require('../util/yieldtimers.js');
|
||||
|
||||
var execute = function (sequencer, thread, blockId, isInput) {
|
||||
/**
|
||||
* If set, block calls, args, and return values will be logged to the console.
|
||||
* @const {boolean}
|
||||
*/
|
||||
var DEBUG_BLOCK_CALLS = true;
|
||||
|
||||
var execute = function (sequencer, thread, blockId) {
|
||||
var runtime = sequencer.runtime;
|
||||
|
||||
// Save the yield timer ID, in case a primitive makes a new one
|
||||
|
@ -12,91 +17,8 @@ var execute = function (sequencer, thread, blockId, isInput) {
|
|||
var opcode = runtime.blocks.getOpcode(blockId);
|
||||
|
||||
// Push the current block to the stack
|
||||
thread.stack.push(blockId);
|
||||
// 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];
|
||||
|
||||
/**
|
||||
* 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 threadDoneCallback = function () {
|
||||
// Pop the stack and stack frame
|
||||
thread.stack.pop();
|
||||
thread.stackFrames.pop();
|
||||
// If we're not executing an input sub-block,
|
||||
// mark the thread as done and proceed to the next block.
|
||||
if (!isInput) {
|
||||
thread.status = Thread.STATUS_DONE;
|
||||
// Refresh nextBlock in case it has changed during a yield.
|
||||
thread.nextBlock = runtime.blocks.getNextBlock(blockId);
|
||||
}
|
||||
// Stop showing run feedback in the editor.
|
||||
runtime.glowBlock(blockId, false);
|
||||
};
|
||||
|
||||
/**
|
||||
* A callback for the primitive to start hats.
|
||||
* @todo very hacked...
|
||||
* Provide a callback that is passed in a block and returns true
|
||||
* if it is a hat that should be triggered.
|
||||
* @param {Function} callback Provided callback.
|
||||
*/
|
||||
var startHats = function(callback) {
|
||||
var stacks = runtime.blocks.getStacks();
|
||||
for (var i = 0; i < stacks.length; i++) {
|
||||
var stack = stacks[i];
|
||||
var stackBlock = runtime.blocks.getBlock(stack);
|
||||
var result = callback(stackBlock);
|
||||
if (result) {
|
||||
// Check if the stack is already running
|
||||
var stackRunning = false;
|
||||
|
||||
for (var j = 0; j < runtime.threads.length; j++) {
|
||||
if (runtime.threads[j].topBlock == stack) {
|
||||
stackRunning = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!stackRunning) {
|
||||
runtime._pushThread(stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Record whether we have switched stack,
|
||||
* to avoid proceeding the thread automatically.
|
||||
* @type {boolean}
|
||||
*/
|
||||
var switchedStack = false;
|
||||
/**
|
||||
* A callback for a primitive to start a substack.
|
||||
* @type {Function}
|
||||
*/
|
||||
var threadStartSubstack = function () {
|
||||
// Set nextBlock to the start of the substack
|
||||
var substack = runtime.blocks.getSubstack(blockId);
|
||||
if (substack && substack.value) {
|
||||
thread.nextBlock = substack.value;
|
||||
} else {
|
||||
thread.nextBlock = null;
|
||||
}
|
||||
switchedStack = true;
|
||||
};
|
||||
thread.pushStack(blockId);
|
||||
var currentStackFrame = thread.getLastStackFrame();
|
||||
|
||||
// Generate values for arguments (inputs).
|
||||
var argValues = {};
|
||||
|
@ -112,13 +34,10 @@ var execute = function (sequencer, thread, blockId, isInput) {
|
|||
for (var inputName in inputs) {
|
||||
var input = inputs[inputName];
|
||||
var inputBlockId = input.block;
|
||||
var result = execute(sequencer, thread, inputBlockId, true);
|
||||
var result = execute(sequencer, thread, inputBlockId);
|
||||
argValues[input.name] = result;
|
||||
}
|
||||
|
||||
// Start showing run feedback in the editor.
|
||||
runtime.glowBlock(blockId, true);
|
||||
|
||||
if (!opcode) {
|
||||
console.warn('Could not get opcode for block: ' + blockId);
|
||||
console.groupEnd();
|
||||
|
@ -132,21 +51,24 @@ var execute = function (sequencer, thread, blockId, isInput) {
|
|||
return;
|
||||
}
|
||||
|
||||
if (sequencer.DEBUG_BLOCK_CALLS) {
|
||||
if (DEBUG_BLOCK_CALLS) {
|
||||
console.groupCollapsed('Executing: ' + opcode);
|
||||
console.log('with arguments: ', argValues);
|
||||
console.log('and stack frame: ', currentStackFrame);
|
||||
}
|
||||
var blockFunctionReturnValue = null;
|
||||
var primitiveReturnValue = null;
|
||||
try {
|
||||
// @todo deal with the return value
|
||||
blockFunctionReturnValue = blockFunction(argValues, {
|
||||
yield: threadYieldCallback,
|
||||
done: threadDoneCallback,
|
||||
primitiveReturnValue = blockFunction(argValues, {
|
||||
yield: thread.yield,
|
||||
done: function() {
|
||||
sequencer.proceedThread(thread, blockId);
|
||||
},
|
||||
timeout: YieldTimers.timeout,
|
||||
stackFrame: currentStackFrame,
|
||||
startSubstack: threadStartSubstack,
|
||||
startHats: startHats
|
||||
startSubstack: function () {
|
||||
sequencer.stepToSubstack(thread, blockId);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch(e) {
|
||||
|
@ -159,15 +81,12 @@ var execute = function (sequencer, thread, blockId, isInput) {
|
|||
if (YieldTimers.timerId > oldYieldTimerId) {
|
||||
thread.yieldTimerId = YieldTimers.timerId;
|
||||
}
|
||||
if (thread.status === Thread.STATUS_RUNNING && !switchedStack) {
|
||||
// Thread executed without yielding - move to done
|
||||
threadDoneCallback();
|
||||
}
|
||||
if (sequencer.DEBUG_BLOCK_CALLS) {
|
||||
if (DEBUG_BLOCK_CALLS) {
|
||||
console.log('ending stack frame: ', currentStackFrame);
|
||||
console.log('returned: ', blockFunctionReturnValue);
|
||||
console.log('returned: ', primitiveReturnValue);
|
||||
console.groupEnd();
|
||||
}
|
||||
return primitiveReturnValue;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -25,12 +25,6 @@ function Sequencer (runtime) {
|
|||
*/
|
||||
Sequencer.WORK_TIME = 10;
|
||||
|
||||
/**
|
||||
* If set, block calls, args, and return values will be logged to the console.
|
||||
* @const {boolean}
|
||||
*/
|
||||
Sequencer.DEBUG_BLOCK_CALLS = true;
|
||||
|
||||
/**
|
||||
* Step through all threads in `this.threads`, running them in order.
|
||||
* @param {Array.<Thread>} threads List of which threads to step.
|
||||
|
@ -65,17 +59,6 @@ Sequencer.prototype.stepThreads = function (threads) {
|
|||
activeThread.status = Thread.STATUS_RUNNING;
|
||||
// @todo Deal with the return value
|
||||
}
|
||||
// First attempt to pop from the stack
|
||||
if (activeThread.stack.length > 0 &&
|
||||
activeThread.nextBlock === null &&
|
||||
activeThread.status === Thread.STATUS_DONE) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (activeThread.nextBlock === null &&
|
||||
activeThread.status === Thread.STATUS_DONE) {
|
||||
// Finished with this thread - tell runtime to clean it up.
|
||||
|
@ -99,14 +82,56 @@ Sequencer.prototype.stepThread = function (thread) {
|
|||
// 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;
|
||||
if (!currentBlock || !this.runtime.blocks.getBlock(currentBlock)) {
|
||||
var currentBlockId = thread.nextBlock;
|
||||
if (!currentBlockId || !this.runtime.blocks.getBlock(currentBlockId)) {
|
||||
thread.status = Thread.STATUS_DONE;
|
||||
return;
|
||||
}
|
||||
thread.nextBlock = this.runtime.blocks.getNextBlock(currentBlock);
|
||||
// Start showing run feedback in the editor.
|
||||
this.runtime.glowBlock(currentBlockId, true);
|
||||
// Execute the block
|
||||
execute(this, thread, currentBlockId, false);
|
||||
// If the block executed without yielding, move to done.
|
||||
if (thread.status === Thread.STATUS_RUNNING && !thread.switchedStack) {
|
||||
this.proceedThread(thread, currentBlockId);
|
||||
}
|
||||
};
|
||||
|
||||
execute(this, thread, currentBlock, false);
|
||||
/**
|
||||
* Step a thread into a block's substack.
|
||||
* @param {!Thread} thread Thread object to step to substack.
|
||||
* @param {string} currentBlockId Block which owns a substack to step to.
|
||||
*/
|
||||
Sequencer.prototype.stepToSubstack = function (thread, currentBlockId) {
|
||||
// Set nextBlock to the start of the substack
|
||||
var substack = this.runtime.blocks.getSubstack(currentBlockId);
|
||||
if (substack && substack.value) {
|
||||
thread.nextBlock = substack.value;
|
||||
} else {
|
||||
thread.nextBlock = null;
|
||||
}
|
||||
thread.switchedStack = true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Finish stepping a thread and proceed it to the next block.
|
||||
* @param {!Thread} thread Thread object to proceed.
|
||||
* @param {string} currentBlockId Block we are finished with.
|
||||
*/
|
||||
Sequencer.prototype.proceedThread = function (thread, currentBlockId) {
|
||||
// Stop showing run feedback in the editor.
|
||||
this.runtime.glowBlock(currentBlockId, false);
|
||||
// Pop the stack and stack frame
|
||||
thread.popStack();
|
||||
// Mark the thread as done and proceed to the next block.
|
||||
thread.status = Thread.STATUS_DONE;
|
||||
// Refresh nextBlock in case it has changed during a yield.
|
||||
thread.nextBlock = this.runtime.blocks.getNextBlock(currentBlockId);
|
||||
// If none is available, attempt to pop from the stack.
|
||||
// First attempt to pop from the stack
|
||||
if (!thread.nextBlock && thread.stack.length > 0) {
|
||||
thread.nextBlock = thread.popStack();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Sequencer;
|
||||
|
|
Loading…
Reference in a new issue