Prototype implementation of yielding reporters

This commit is contained in:
Tim Mickel 2016-06-17 15:10:12 -04:00
parent 34659c9b7b
commit 97f7571c6f
3 changed files with 61 additions and 12 deletions

View file

@ -1,10 +1,19 @@
var Thread = require('./thread');
/** /**
* If set, block calls, args, and return values will be logged to the console. * If set, block calls, args, and return values will be logged to the console.
* @const {boolean} * @const {boolean}
*/ */
var DEBUG_BLOCK_CALLS = true; var DEBUG_BLOCK_CALLS = true;
var execute = function (sequencer, thread) { /**
* Execute a block.
* @param {!Sequencer} sequencer Which sequencer is executing.
* @param {!Thread} thread Thread which to read and execute.
* @param {string=} opt_waitingInputName If evaluating an input, its name.
* @return {?Any} Reported value, if available immediately.
*/
var execute = function (sequencer, thread, opt_waitingInputName) {
var runtime = sequencer.runtime; var runtime = sequencer.runtime;
// Current block to execute is the one on the top of the stack. // Current block to execute is the one on the top of the stack.
@ -27,11 +36,29 @@ var execute = function (sequencer, thread) {
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;
// Push to the stack to evaluate this input. // Is there a value for this input waiting in the stack frame?
thread.pushStack(inputBlockId); if (currentStackFrame.reported &&
var result = execute(sequencer, thread); currentStackFrame.reported[inputName]) {
thread.popStack(); // Use that value.
argValues[input.name] = result; argValues[inputName] = currentStackFrame.reported[inputName];
} else {
// Otherwise, we need to evaluate the block.
// Push to the stack to evaluate this input.
thread.pushStack(inputBlockId);
if (DEBUG_BLOCK_CALLS) {
console.time('Yielding reporter evaluation');
}
var result = execute(sequencer, thread, inputName);
// Did the reporter yield?
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;
}
thread.popStack();
argValues[inputName] = result;
}
} }
if (!opcode) { if (!opcode) {
@ -51,21 +78,29 @@ var execute = function (sequencer, thread) {
console.log('and stack frame: ', currentStackFrame); console.log('and stack frame: ', currentStackFrame);
} }
var primitiveReturnValue = null; var primitiveReturnValue = null;
// @todo deal with the return value
primitiveReturnValue = blockFunction(argValues, { primitiveReturnValue = blockFunction(argValues, {
yield: thread.yield.bind(thread), yield: thread.yield.bind(thread),
done: function() { done: function() {
sequencer.proceedThread(thread); sequencer.proceedThread(thread);
}, },
report: function(reportedValue) {
thread.pushReportedValue(opt_waitingInputName, reportedValue);
if (DEBUG_BLOCK_CALLS) {
console.log('Reported: ', reportedValue,
' for ', opt_waitingInputName);
console.timeEnd('Yielding reporter evaluation');
}
sequencer.proceedThread(thread);
},
timeout: thread.addTimeout.bind(thread), timeout: thread.addTimeout.bind(thread),
stackFrame: currentStackFrame, stackFrame: currentStackFrame.executionContext,
startSubstack: function (substackNum) { startSubstack: function (substackNum) {
sequencer.stepToSubstack(thread, substackNum); sequencer.stepToSubstack(thread, substackNum);
} }
}); });
if (DEBUG_BLOCK_CALLS) { if (DEBUG_BLOCK_CALLS) {
console.log('ending stack frame: ', currentStackFrame); console.log('ending stack frame: ', currentStackFrame);
console.log('returned: ', primitiveReturnValue); console.log('returned immediately: ', primitiveReturnValue);
console.groupEnd(); console.groupEnd();
} }
return primitiveReturnValue; return primitiveReturnValue;

View file

@ -1,6 +1,5 @@
var Timer = require('../util/timer'); var Timer = require('../util/timer');
var Thread = require('./thread'); var Thread = require('./thread');
var YieldTimers = require('../util/yieldtimers.js');
var execute = require('./execute.js'); var execute = require('./execute.js');
function Sequencer (runtime) { function Sequencer (runtime) {
@ -101,7 +100,7 @@ Sequencer.prototype.startThread = function (thread) {
// move to done. // move to done.
if (thread.status === Thread.STATUS_RUNNING && if (thread.status === Thread.STATUS_RUNNING &&
thread.peekStack() === currentBlockId) { thread.peekStack() === currentBlockId) {
this.proceedThread(thread, currentBlockId); this.proceedThread(thread);
} }
}; };

View file

@ -69,7 +69,10 @@ Thread.prototype.pushStack = function (blockId) {
// Push an empty stack frame, if we need one. // Push an empty stack frame, if we need one.
// Might not, if we just popped the stack. // Might not, if we just popped the stack.
if (this.stack.length > this.stackFrames.length) { if (this.stack.length > this.stackFrames.length) {
this.stackFrames.push({}); this.stackFrames.push({
reported: {}, // Collects reported input values.
executionContext: {} // A context passed to block implementations.
});
} }
}; };
@ -99,6 +102,18 @@ Thread.prototype.peekStackFrame = function () {
return this.stackFrames[this.stackFrames.length - 1]; return this.stackFrames[this.stackFrames.length - 1];
}; };
/**
* Push a reported value to the parent of the current stack frame.
* @param {!string} inputName Name of input reported.
* @param {!Any} value Reported value to push.
*/
Thread.prototype.pushReportedValue = function (inputName, value) {
var parentStackFrame = this.stackFrames[this.stackFrames.length - 2];
if (parentStackFrame) {
parentStackFrame.reported[inputName] = value;
}
};
/** /**
* Yields the thread. * Yields the thread.
*/ */