Simplify execution by removing nextBlock

Everything is managed by the stack, including what the execute() function does.
This commit is contained in:
Tim Mickel 2016-06-09 17:08:30 -04:00
parent 2cd6bf93bb
commit 526a260101
4 changed files with 70 additions and 57 deletions

View file

@ -6,40 +6,43 @@ var YieldTimers = require('../util/yieldtimers.js');
*/ */
var DEBUG_BLOCK_CALLS = true; var DEBUG_BLOCK_CALLS = true;
var execute = function (sequencer, thread, blockId) { var execute = function (sequencer, thread) {
var runtime = sequencer.runtime; var runtime = sequencer.runtime;
// Current block to execute is the one on the top of the stack.
var currentBlockId = thread.peekStack();
var currentStackFrame = thread.peekStackFrame();
// Save the yield timer ID, in case a primitive makes a new one // 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 // @todo hack - perhaps patch this to allow more than one timer per
// primitive, for example... // primitive, for example...
var oldYieldTimerId = YieldTimers.timerId; var oldYieldTimerId = YieldTimers.timerId;
var opcode = runtime.blocks.getOpcode(blockId); var opcode = runtime.blocks.getOpcode(currentBlockId);
// Push the current block to the stack
thread.pushStack(blockId);
var currentStackFrame = thread.getLastStackFrame();
// Generate values for arguments (inputs). // Generate values for arguments (inputs).
var argValues = {}; var argValues = {};
// Add all fields on this block to the argValues. // Add all fields on this block to the argValues.
var fields = runtime.blocks.getFields(blockId); var fields = runtime.blocks.getFields(currentBlockId);
for (var fieldName in fields) { for (var fieldName in fields) {
argValues[fieldName] = fields[fieldName].value; argValues[fieldName] = fields[fieldName].value;
} }
// Recursively evaluate input blocks. // Recursively evaluate input blocks.
var inputs = runtime.blocks.getInputs(blockId); var inputs = runtime.blocks.getInputs(currentBlockId);
for (var inputName in inputs) { for (var inputName in inputs) {
var input = inputs[inputName]; var input = inputs[inputName];
var inputBlockId = input.block; var inputBlockId = input.block;
var result = execute(sequencer, thread, inputBlockId); // Push to the stack to evaluate this input.
thread.pushStack(inputBlockId);
var result = execute(sequencer, thread);
thread.popStack();
argValues[input.name] = result; argValues[input.name] = result;
} }
if (!opcode) { if (!opcode) {
console.warn('Could not get opcode for block: ' + blockId); console.warn('Could not get opcode for block: ' + currentBlockId);
console.groupEnd(); console.groupEnd();
return; return;
} }
@ -62,12 +65,12 @@ var execute = function (sequencer, thread, blockId) {
primitiveReturnValue = blockFunction(argValues, { primitiveReturnValue = blockFunction(argValues, {
yield: thread.yield.bind(thread), yield: thread.yield.bind(thread),
done: function() { done: function() {
sequencer.proceedThread(thread, blockId); sequencer.proceedThread(thread);
}, },
timeout: YieldTimers.timeout, timeout: YieldTimers.timeout,
stackFrame: currentStackFrame, stackFrame: currentStackFrame,
startSubstack: function () { startSubstack: function () {
sequencer.stepToSubstack(thread, blockId); sequencer.stepToSubstack(thread);
} }
}); });
} }
@ -86,8 +89,6 @@ var execute = function (sequencer, thread, blockId) {
console.log('returned: ', primitiveReturnValue); console.log('returned: ', primitiveReturnValue);
console.groupEnd(); console.groupEnd();
} }
// Pop the stack and stack frame
thread.popStack();
return primitiveReturnValue; return primitiveReturnValue;
} }
}; };

View file

@ -122,6 +122,7 @@ Runtime.prototype.getOpcodeFunction = function (opcode) {
Runtime.prototype._pushThread = function (id) { Runtime.prototype._pushThread = function (id) {
this.emit(Runtime.STACK_GLOW_ON, id); this.emit(Runtime.STACK_GLOW_ON, id);
var thread = new Thread(id); var thread = new Thread(id);
thread.pushStack(id);
this.threads.push(thread); this.threads.push(thread);
}; };

View file

@ -49,17 +49,18 @@ Sequencer.prototype.stepThreads = function (threads) {
var activeThread = threads[i]; var activeThread = threads[i];
if (activeThread.status === Thread.STATUS_RUNNING) { if (activeThread.status === Thread.STATUS_RUNNING) {
// Normal-mode thread: step. // Normal-mode thread: step.
this.stepThread(activeThread); this.startThread(activeThread);
} else if (activeThread.status === Thread.STATUS_YIELD) { } else if (activeThread.status === Thread.STATUS_YIELD) {
// Yield-mode thread: check if the time has passed. // Yield-mode thread: check if the time has passed.
YieldTimers.resolve(activeThread.yieldTimerId); if (!YieldTimers.resolve(activeThread.yieldTimerId)) {
numYieldingThreads++; numYieldingThreads++;
}
} else if (activeThread.status === Thread.STATUS_DONE) { } else if (activeThread.status === Thread.STATUS_DONE) {
// Moved to a done state - finish up // Moved to a done state - finish up
activeThread.status = Thread.STATUS_RUNNING; activeThread.status = Thread.STATUS_RUNNING;
// @todo Deal with the return value // @todo Deal with the return value
} }
if (activeThread.nextBlock === null && if (activeThread.stack.length === 0 &&
activeThread.status === Thread.STATUS_DONE) { activeThread.status === Thread.STATUS_DONE) {
// Finished with this thread - tell runtime to clean it up. // Finished with this thread - tell runtime to clean it up.
inactiveThreads.push(activeThread); inactiveThreads.push(activeThread);
@ -78,21 +79,27 @@ Sequencer.prototype.stepThreads = function (threads) {
* Step the requested thread * Step the requested thread
* @param {!Thread} thread Thread object to step * @param {!Thread} thread Thread object to step
*/ */
Sequencer.prototype.stepThread = function (thread) { Sequencer.prototype.startThread = function (thread) {
// Save the current block and set the nextBlock. var currentBlockId = thread.peekStack();
// If the primitive would like to do control flow,
// it can overwrite nextBlock.
var currentBlockId = thread.nextBlock;
if (!currentBlockId || !this.runtime.blocks.getBlock(currentBlockId)) { if (!currentBlockId || !this.runtime.blocks.getBlock(currentBlockId)) {
thread.status = Thread.STATUS_DONE; thread.status = Thread.STATUS_DONE;
return; return;
} }
// Start showing run feedback in the editor. // Start showing run feedback in the editor.
this.runtime.glowBlock(currentBlockId, true); this.runtime.glowBlock(currentBlockId, true);
// Execute the block
execute(this, thread, currentBlockId, false); // Push the current block to the stack, if executing for the first time.
// If the block executed without yielding, move to done. if (thread.peekStack() != currentBlockId) {
if (thread.status === Thread.STATUS_RUNNING && !thread.switchedStack) { thread.pushStack(currentBlockId);
}
// 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) {
this.proceedThread(thread, currentBlockId); this.proceedThread(thread, currentBlockId);
} }
}; };
@ -102,15 +109,16 @@ Sequencer.prototype.stepThread = function (thread) {
* @param {!Thread} thread Thread object to step to substack. * @param {!Thread} thread Thread object to step to substack.
* @param {string} currentBlockId Block which owns a substack to step to. * @param {string} currentBlockId Block which owns a substack to step to.
*/ */
Sequencer.prototype.stepToSubstack = function (thread, currentBlockId) { Sequencer.prototype.stepToSubstack = function (thread) {
// Set nextBlock to the start of the substack var currentBlockId = thread.peekStack();
var substack = this.runtime.blocks.getSubstack(currentBlockId); var substackId = this.runtime.blocks.getSubstack(currentBlockId);
if (substack && substack.value) { if (substackId) {
thread.nextBlock = substack.value; // Push substack ID to the thread's stack.
thread.pushStack(substackId);
} else { } else {
thread.nextBlock = null; // Push null, so we come back to the current block.
thread.pushStack(null);
} }
thread.switchedStack = true;
}; };
/** /**
@ -118,17 +126,21 @@ Sequencer.prototype.stepToSubstack = function (thread, currentBlockId) {
* @param {!Thread} thread Thread object to proceed. * @param {!Thread} thread Thread object to proceed.
* @param {string} currentBlockId Block we are finished with. * @param {string} currentBlockId Block we are finished with.
*/ */
Sequencer.prototype.proceedThread = function (thread, currentBlockId) { Sequencer.prototype.proceedThread = function (thread) {
// Stop showing run feedback in the editor. var currentBlockId = thread.peekStack();
// Mark the status as done and proceed to the next block.
this.runtime.glowBlock(currentBlockId, false); this.runtime.glowBlock(currentBlockId, false);
// Mark the thread as done and proceed to the next block.
thread.status = Thread.STATUS_DONE; thread.status = Thread.STATUS_DONE;
// Refresh nextBlock in case it has changed during a yield. // Pop from the stack - finished this level of execution.
thread.nextBlock = this.runtime.blocks.getNextBlock(currentBlockId); thread.popStack();
// If none is available, attempt to pop from the stack. // Push next connected block, if there is one.
// First attempt to pop from the stack var nextBlockId = this.runtime.blocks.getNextBlock(currentBlockId);
if (!thread.nextBlock && thread.stack.length > 0) { if (nextBlockId) {
thread.nextBlock = thread.popStack(); thread.pushStack(nextBlockId);
}
// Pop from the stack until we have a next block.
while (thread.peekStack() === null && thread.stack.length > 0) {
thread.popStack();
} }
}; };

View file

@ -9,11 +9,7 @@ function Thread (firstBlock) {
* @type {!string} * @type {!string}
*/ */
this.topBlock = firstBlock; this.topBlock = firstBlock;
/**
* ID of next block that the thread will execute, or null if none.
* @type {?string}
*/
this.nextBlock = firstBlock;
/** /**
* Stack for the thread. When the sequencer enters a control structure, * Stack for the thread. When the sequencer enters a control structure,
* the block is pushed onto the stack so we know where to exit. * the block is pushed onto the stack so we know where to exit.
@ -38,12 +34,6 @@ function Thread (firstBlock) {
* @type {number} * @type {number}
*/ */
this.yieldTimerId = -1; this.yieldTimerId = -1;
/**
* Whether the thread has switched stack in the course of execution.
* @type {boolean}
*/
this.switchedStack = false;
} }
/** /**
@ -83,7 +73,7 @@ Thread.prototype.pushStack = function (blockId) {
/** /**
* Pop last block on the stack and its stack frame. * Pop last block on the stack and its stack frame.
* @returns {string} Block ID popped from the stack. * @return {string} Block ID popped from the stack.
*/ */
Thread.prototype.popStack = function () { Thread.prototype.popStack = function () {
this.stackFrames.pop(); this.stackFrames.pop();
@ -91,10 +81,19 @@ Thread.prototype.popStack = function () {
}; };
/** /**
* Get last stack frame. * Get top stack item.
* @return {?string} Block ID on top of stack.
*/
Thread.prototype.peekStack = function () {
return this.stack[this.stack.length - 1];
};
/**
* Get top stack frame.
* @return {?Object} Last stack frame stored on this thread. * @return {?Object} Last stack frame stored on this thread.
*/ */
Thread.prototype.getLastStackFrame = function () { Thread.prototype.peekStackFrame = function () {
return this.stackFrames[this.stackFrames.length - 1]; return this.stackFrames[this.stackFrames.length - 1];
}; };