2016-06-09 13:27:30 -04:00
|
|
|
var YieldTimers = require('../util/yieldtimers.js');
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
var execute = function (sequencer, thread, blockId) {
|
2016-06-09 13:27:30 -04:00
|
|
|
var runtime = sequencer.runtime;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
var opcode = runtime.blocks.getOpcode(blockId);
|
|
|
|
|
|
|
|
// Push the current block to the stack
|
2016-06-09 14:23:34 -04:00
|
|
|
thread.pushStack(blockId);
|
|
|
|
var currentStackFrame = thread.getLastStackFrame();
|
2016-06-09 13:27:30 -04:00
|
|
|
|
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.
|
|
|
|
var fields = runtime.blocks.getFields(blockId);
|
|
|
|
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.
|
|
|
|
var inputs = runtime.blocks.getInputs(blockId);
|
|
|
|
for (var inputName in inputs) {
|
|
|
|
var input = inputs[inputName];
|
|
|
|
var inputBlockId = input.block;
|
2016-06-09 14:23:34 -04:00
|
|
|
var result = execute(sequencer, thread, inputBlockId);
|
2016-06-09 13:28:50 -04:00
|
|
|
argValues[input.name] = result;
|
|
|
|
}
|
|
|
|
|
2016-06-09 13:27:30 -04:00
|
|
|
if (!opcode) {
|
|
|
|
console.warn('Could not get opcode for block: ' + blockId);
|
|
|
|
console.groupEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var blockFunction = runtime.getOpcodeFunction(opcode);
|
|
|
|
if (!blockFunction) {
|
|
|
|
console.warn('Could not get implementation for opcode: ' + opcode);
|
|
|
|
console.groupEnd();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-09 14:23:34 -04:00
|
|
|
var primitiveReturnValue = null;
|
2016-06-09 13:27:30 -04:00
|
|
|
try {
|
|
|
|
// @todo deal with the return value
|
2016-06-09 14:23:34 -04:00
|
|
|
primitiveReturnValue = blockFunction(argValues, {
|
|
|
|
yield: thread.yield,
|
|
|
|
done: function() {
|
|
|
|
sequencer.proceedThread(thread, blockId);
|
|
|
|
},
|
2016-06-09 13:27:30 -04:00
|
|
|
timeout: YieldTimers.timeout,
|
|
|
|
stackFrame: currentStackFrame,
|
2016-06-09 14:23:34 -04:00
|
|
|
startSubstack: function () {
|
|
|
|
sequencer.stepToSubstack(thread, blockId);
|
|
|
|
}
|
2016-06-09 13:27:30 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
console.error(
|
|
|
|
'Exception calling block function for opcode: ' +
|
|
|
|
opcode + '\n' + e);
|
|
|
|
} finally {
|
|
|
|
// Update if the thread has set a yield timer ID
|
|
|
|
// @todo hack
|
|
|
|
if (YieldTimers.timerId > oldYieldTimerId) {
|
|
|
|
thread.yieldTimerId = YieldTimers.timerId;
|
|
|
|
}
|
2016-06-09 14:23:34 -04:00
|
|
|
if (DEBUG_BLOCK_CALLS) {
|
2016-06-09 13:27:30 -04:00
|
|
|
console.log('ending stack frame: ', currentStackFrame);
|
2016-06-09 14:23:34 -04:00
|
|
|
console.log('returned: ', primitiveReturnValue);
|
2016-06-09 13:27:30 -04:00
|
|
|
console.groupEnd();
|
|
|
|
}
|
2016-06-09 14:45:54 -04:00
|
|
|
// Pop the stack and stack frame
|
|
|
|
thread.popStack();
|
2016-06-09 14:23:34 -04:00
|
|
|
return primitiveReturnValue;
|
2016-06-09 13:27:30 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = execute;
|