2016-06-17 15:10:12 -04:00
|
|
|
var Thread = require('./thread');
|
|
|
|
|
2016-06-09 14:23:34 -04:00
|
|
|
/**
|
|
|
|
* If set, block calls, args, and return values will be logged to the console.
|
|
|
|
* @const {boolean}
|
|
|
|
*/
|
|
|
|
var DEBUG_BLOCK_CALLS = true;
|
|
|
|
|
2016-06-17 15:10:12 -04:00
|
|
|
/**
|
|
|
|
* Execute a block.
|
|
|
|
* @param {!Sequencer} sequencer Which sequencer is executing.
|
|
|
|
* @param {!Thread} thread Thread which to read and execute.
|
|
|
|
*/
|
2016-06-17 15:53:58 -04:00
|
|
|
var execute = function (sequencer, thread) {
|
2016-06-09 13:27:30 -04:00
|
|
|
var runtime = sequencer.runtime;
|
|
|
|
|
2016-06-09 17:08:30 -04:00
|
|
|
// Current block to execute is the one on the top of the stack.
|
|
|
|
var currentBlockId = thread.peekStack();
|
|
|
|
var currentStackFrame = thread.peekStackFrame();
|
|
|
|
|
|
|
|
var opcode = runtime.blocks.getOpcode(currentBlockId);
|
2016-06-09 13:27:30 -04:00
|
|
|
|
2016-06-17 17:18:44 -04:00
|
|
|
if (!opcode) {
|
|
|
|
console.warn('Could not get opcode for block: ' + currentBlockId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockFunction = runtime.getOpcodeFunction(opcode);
|
|
|
|
if (!blockFunction) {
|
|
|
|
console.warn('Could not get implementation for opcode: ' + opcode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-09 13:28:50 -04:00
|
|
|
// Generate values for arguments (inputs).
|
2016-06-09 13:27:30 -04:00
|
|
|
var argValues = {};
|
|
|
|
|
2016-06-09 13:28:50 -04:00
|
|
|
// Add all fields on this block to the argValues.
|
2016-06-09 17:08:30 -04:00
|
|
|
var fields = runtime.blocks.getFields(currentBlockId);
|
2016-06-09 13:28:50 -04:00
|
|
|
for (var fieldName in fields) {
|
2016-06-09 14:27:11 -04:00
|
|
|
argValues[fieldName] = fields[fieldName].value;
|
2016-06-09 13:28:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively evaluate input blocks.
|
2016-06-09 17:08:30 -04:00
|
|
|
var inputs = runtime.blocks.getInputs(currentBlockId);
|
2016-06-09 13:28:50 -04:00
|
|
|
for (var inputName in inputs) {
|
|
|
|
var input = inputs[inputName];
|
|
|
|
var inputBlockId = input.block;
|
2016-06-20 14:24:00 -04:00
|
|
|
// Is there no value for this input waiting in the stack frame?
|
2016-06-20 14:11:21 -04:00
|
|
|
if (typeof currentStackFrame.reported[inputName] === 'undefined') {
|
2016-06-20 14:24:00 -04:00
|
|
|
// If there's not, we need to evaluate the block.
|
2016-06-17 15:10:12 -04:00
|
|
|
// Push to the stack to evaluate this input.
|
|
|
|
thread.pushStack(inputBlockId);
|
|
|
|
if (DEBUG_BLOCK_CALLS) {
|
2016-06-20 14:24:00 -04:00
|
|
|
console.time('Reporter evaluation');
|
2016-06-17 15:10:12 -04:00
|
|
|
}
|
2016-06-17 15:53:58 -04:00
|
|
|
runtime.glowBlock(inputBlockId, true);
|
2016-06-20 14:24:00 -04:00
|
|
|
// Save name of input for `Thread.pushReportedValue`.
|
2016-06-17 17:18:44 -04:00
|
|
|
currentStackFrame.waitingReporter = inputName;
|
2016-06-20 14:11:21 -04:00
|
|
|
execute(sequencer, thread);
|
2016-06-17 15:10:12 -04:00
|
|
|
if (thread.status === Thread.STATUS_YIELD) {
|
|
|
|
// Reporter yielded; don't pop stack and wait for it to unyield.
|
|
|
|
// The value will be populated once the reporter unyields,
|
|
|
|
// and passed up to the currentStackFrame on next execution.
|
|
|
|
return;
|
2016-06-20 14:28:12 -04:00
|
|
|
} else {
|
|
|
|
// Reporter finished right away; pop the stack.
|
|
|
|
runtime.glowBlock(inputBlockId, false);
|
|
|
|
thread.popStack();
|
2016-06-17 15:10:12 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-17 17:18:44 -04:00
|
|
|
argValues[inputName] = currentStackFrame.reported[inputName];
|
2016-06-09 13:27:30 -04:00
|
|
|
}
|
|
|
|
|
2016-06-20 14:29:47 -04:00
|
|
|
// If we've gotten this far, all of the input blocks are evaluated,
|
|
|
|
// and `argValues` is fully populated. So, execute the block primitive.
|
|
|
|
// First, clear `currentStackFrame.reported`, so any subsequent execution
|
|
|
|
// (e.g., on return from a substack) gets fresh inputs.
|
|
|
|
currentStackFrame.reported = {};
|
|
|
|
|
2016-06-09 14:23:34 -04:00
|
|
|
if (DEBUG_BLOCK_CALLS) {
|
2016-06-09 13:27:30 -04:00
|
|
|
console.groupCollapsed('Executing: ' + opcode);
|
|
|
|
console.log('with arguments: ', argValues);
|
|
|
|
console.log('and stack frame: ', currentStackFrame);
|
|
|
|
}
|
2016-06-20 14:31:48 -04:00
|
|
|
var primitiveReportedValue = null;
|
|
|
|
primitiveReportedValue = blockFunction(argValues, {
|
2016-06-13 11:23:39 -04:00
|
|
|
yield: thread.yield.bind(thread),
|
|
|
|
done: function() {
|
|
|
|
sequencer.proceedThread(thread);
|
|
|
|
},
|
2016-06-17 15:10:12 -04:00
|
|
|
report: function(reportedValue) {
|
|
|
|
if (DEBUG_BLOCK_CALLS) {
|
2016-06-17 15:53:58 -04:00
|
|
|
console.log('Reported: ', reportedValue);
|
2016-06-17 15:10:12 -04:00
|
|
|
console.timeEnd('Yielding reporter evaluation');
|
|
|
|
}
|
2016-06-17 15:53:58 -04:00
|
|
|
thread.pushReportedValue(reportedValue);
|
2016-06-17 15:10:12 -04:00
|
|
|
sequencer.proceedThread(thread);
|
|
|
|
},
|
2016-06-17 14:36:36 -04:00
|
|
|
timeout: thread.addTimeout.bind(thread),
|
2016-06-17 15:10:12 -04:00
|
|
|
stackFrame: currentStackFrame.executionContext,
|
2016-06-13 11:23:39 -04:00
|
|
|
startSubstack: function (substackNum) {
|
|
|
|
sequencer.stepToSubstack(thread, substackNum);
|
2016-06-09 13:27:30 -04:00
|
|
|
}
|
2016-06-13 11:23:39 -04:00
|
|
|
});
|
2016-06-20 14:11:21 -04:00
|
|
|
if (thread.status === Thread.STATUS_RUNNING) {
|
|
|
|
if (DEBUG_BLOCK_CALLS) {
|
2016-06-20 14:31:48 -04:00
|
|
|
console.log('reporting value: ', primitiveReportedValue);
|
2016-06-20 14:11:21 -04:00
|
|
|
}
|
2016-06-20 14:31:48 -04:00
|
|
|
thread.pushReportedValue(primitiveReportedValue);
|
2016-06-20 14:11:21 -04:00
|
|
|
}
|
2016-06-13 11:23:39 -04:00
|
|
|
if (DEBUG_BLOCK_CALLS) {
|
|
|
|
console.log('ending stack frame: ', currentStackFrame);
|
|
|
|
console.groupEnd();
|
2016-06-09 13:27:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = execute;
|