2017-04-17 15:10:04 -04:00
|
|
|
const log = require('../util/log');
|
|
|
|
const Thread = require('./thread');
|
2016-06-17 15:10:12 -04:00
|
|
|
|
2016-08-23 18:12:32 -04:00
|
|
|
/**
|
|
|
|
* Utility function to determine if a value is a Promise.
|
|
|
|
* @param {*} value Value to check for a Promise.
|
2017-02-01 15:59:50 -05:00
|
|
|
* @return {boolean} True if the value appears to be a Promise.
|
2016-08-23 18:12:32 -04:00
|
|
|
*/
|
2017-04-17 15:10:04 -04:00
|
|
|
const isPromise = function (value) {
|
2016-08-23 18:12:32 -04:00
|
|
|
return value && value.then && typeof value.then === 'function';
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*/
|
2017-04-17 17:15:19 -04:00
|
|
|
const execute = function (sequencer, thread) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const runtime = sequencer.runtime;
|
|
|
|
const target = thread.target;
|
2016-06-09 13:27:30 -04:00
|
|
|
|
2016-10-13 17:15:49 -04:00
|
|
|
// Stop if block or target no longer exists.
|
2017-02-16 05:54:17 -05:00
|
|
|
if (target === null) {
|
2016-09-15 13:51:40 -04:00
|
|
|
// No block found: stop the thread; script no longer exists.
|
|
|
|
sequencer.retireThread(thread);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
|
2017-02-11 09:26:23 -05:00
|
|
|
// Current block to execute is the one on the top of the stack.
|
2017-04-17 15:10:04 -04:00
|
|
|
const currentBlockId = thread.peekStack();
|
|
|
|
const currentStackFrame = thread.peekStackFrame();
|
2017-02-11 09:26:23 -05:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
let blockContainer = target.blocks;
|
|
|
|
let block = blockContainer.getBlock(currentBlockId);
|
2017-02-16 05:54:17 -05:00
|
|
|
if (typeof block === 'undefined') {
|
2016-10-13 17:15:49 -04:00
|
|
|
blockContainer = runtime.flyoutBlocks;
|
2017-02-11 09:26:23 -05:00
|
|
|
block = blockContainer.getBlock(currentBlockId);
|
|
|
|
// Stop if block or target no longer exists.
|
2017-02-16 05:54:17 -05:00
|
|
|
if (typeof block === 'undefined') {
|
2017-02-11 09:26:23 -05:00
|
|
|
// No block found: stop the thread; script no longer exists.
|
|
|
|
sequencer.retireThread(thread);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 17:15:49 -04:00
|
|
|
}
|
2017-02-11 09:26:23 -05:00
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
const opcode = blockContainer.getOpcode(block);
|
|
|
|
const fields = blockContainer.getFields(block);
|
|
|
|
const inputs = blockContainer.getInputs(block);
|
|
|
|
const blockFunction = runtime.getOpcodeFunction(opcode);
|
|
|
|
const isHat = runtime.getIsHat(opcode);
|
2016-10-13 17:15:49 -04:00
|
|
|
|
2016-06-09 13:27:30 -04:00
|
|
|
|
2016-06-17 17:18:44 -04:00
|
|
|
if (!opcode) {
|
2017-04-17 15:10:04 -04:00
|
|
|
log.warn(`Could not get opcode for block: ${currentBlockId}`);
|
2016-06-17 17:18:44 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-12 13:14:16 -04:00
|
|
|
/**
|
|
|
|
* Handle any reported value from the primitive, either directly returned
|
|
|
|
* or after a promise resolves.
|
|
|
|
* @param {*} resolvedValue Value eventually returned from the primitive.
|
|
|
|
*/
|
2017-05-09 17:34:13 -04:00
|
|
|
// @todo move this to callback attached to the thread when we have performance
|
|
|
|
// metrics (dd)
|
2017-04-17 15:10:04 -04:00
|
|
|
const handleReport = function (resolvedValue) {
|
2016-09-12 13:14:16 -04:00
|
|
|
thread.pushReportedValue(resolvedValue);
|
|
|
|
if (isHat) {
|
|
|
|
// Hat predicate was evaluated.
|
|
|
|
if (runtime.getIsEdgeActivatedHat(opcode)) {
|
|
|
|
// If this is an edge-activated hat, only proceed if
|
|
|
|
// the value is true and used to be false.
|
2017-04-17 15:10:04 -04:00
|
|
|
const oldEdgeValue = runtime.updateEdgeActivatedValue(
|
2016-09-12 13:14:16 -04:00
|
|
|
currentBlockId,
|
|
|
|
resolvedValue
|
|
|
|
);
|
2017-04-17 15:10:04 -04:00
|
|
|
const edgeWasActivated = !oldEdgeValue && resolvedValue;
|
2016-09-12 13:14:16 -04:00
|
|
|
if (!edgeWasActivated) {
|
|
|
|
sequencer.retireThread(thread);
|
|
|
|
}
|
2016-10-24 10:28:31 -04:00
|
|
|
} else {
|
2016-09-12 13:14:16 -04:00
|
|
|
// Not an edge-activated hat: retire the thread
|
|
|
|
// if predicate was false.
|
2016-10-24 10:28:31 -04:00
|
|
|
if (!resolvedValue) {
|
|
|
|
sequencer.retireThread(thread);
|
|
|
|
}
|
2016-09-12 13:14:16 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In a non-hat, report the value visually if necessary if
|
|
|
|
// at the top of the thread stack.
|
2017-05-09 17:34:13 -04:00
|
|
|
|
|
|
|
if (typeof resolvedValue !== 'undefined' && thread.atStackTop()) {
|
|
|
|
if (thread.showVisualReport) {
|
|
|
|
runtime.visualReport(currentBlockId, resolvedValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thread.updateMonitor) {
|
|
|
|
runtime.updateMonitors([{
|
|
|
|
id: currentBlockId, // @todo(dd) this will collide if multiple sprites use same block
|
|
|
|
category: 'data',
|
|
|
|
label: blockContainer.getOpcode(blockContainer.getBlock(currentBlockId)), // @todo(dd) how to handle translation here?
|
|
|
|
value: String(resolvedValue),
|
|
|
|
x: 0, // @todo(dd) place below the last monitor instead
|
|
|
|
y: 0
|
|
|
|
}]);
|
|
|
|
}
|
2016-09-12 13:14:16 -04:00
|
|
|
}
|
|
|
|
// Finished any yields.
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.status = Thread.STATUS_RUNNING;
|
2016-09-12 13:14:16 -04:00
|
|
|
}
|
|
|
|
};
|
2016-08-23 18:12:32 -04:00
|
|
|
|
2016-09-12 13:14:16 -04:00
|
|
|
// Hats and single-field shadows are implemented slightly differently
|
|
|
|
// from regular blocks.
|
|
|
|
// For hats: if they have an associated block function,
|
|
|
|
// it's treated as a predicate; if not, execution will proceed as a no-op.
|
|
|
|
// For single-field shadows: If the block has a single field, and no inputs,
|
|
|
|
// immediately return the value of the field.
|
2017-02-16 05:54:17 -05:00
|
|
|
if (typeof blockFunction === 'undefined') {
|
2016-09-12 13:14:16 -04:00
|
|
|
if (isHat) {
|
|
|
|
// Skip through the block (hat with no predicate).
|
|
|
|
return;
|
2017-02-11 09:26:23 -05:00
|
|
|
}
|
2017-04-17 15:10:04 -04:00
|
|
|
const keys = Object.keys(fields);
|
2017-02-11 09:26:23 -05:00
|
|
|
if (keys.length === 1 && Object.keys(inputs).length === 0) {
|
|
|
|
// One field and no inputs - treat as arg.
|
|
|
|
handleReport(fields[keys[0]].value);
|
2016-09-12 13:14:16 -04:00
|
|
|
} else {
|
2017-04-17 15:10:04 -04:00
|
|
|
log.warn(`Could not get implementation for opcode: ${opcode}`);
|
2016-08-23 18:12:32 -04:00
|
|
|
}
|
2017-02-11 09:26:23 -05:00
|
|
|
thread.requestScriptGlowInFrame = true;
|
|
|
|
return;
|
2016-06-17 17:18:44 -04:00
|
|
|
}
|
|
|
|
|
2016-06-09 13:28:50 -04:00
|
|
|
// Generate values for arguments (inputs).
|
2017-04-17 15:10:04 -04:00
|
|
|
const argValues = {};
|
2016-06-09 13:27:30 -04:00
|
|
|
|
2016-06-09 13:28:50 -04:00
|
|
|
// Add all fields on this block to the argValues.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (const fieldName in fields) {
|
2017-02-09 21:12:47 -05:00
|
|
|
if (!fields.hasOwnProperty(fieldName)) continue;
|
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.
|
2017-04-17 15:10:04 -04:00
|
|
|
for (const inputName in inputs) {
|
2017-02-09 21:12:47 -05:00
|
|
|
if (!inputs.hasOwnProperty(inputName)) continue;
|
2017-04-17 15:10:04 -04:00
|
|
|
const input = inputs[inputName];
|
|
|
|
const inputBlockId = input.block;
|
2016-06-20 14:24:00 -04:00
|
|
|
// Is there no value for this input waiting in the stack frame?
|
2017-02-16 05:54:17 -05:00
|
|
|
if (inputBlockId !== null && typeof currentStackFrame.reported[inputName] === 'undefined') {
|
2016-06-20 14:24:00 -04:00
|
|
|
// If there's not, we need to evaluate the block.
|
2016-10-17 23:23:16 -04:00
|
|
|
// Push to the stack to evaluate the reporter block.
|
|
|
|
thread.pushStack(inputBlockId);
|
|
|
|
// Save name of input for `Thread.pushReportedValue`.
|
|
|
|
currentStackFrame.waitingReporter = inputName;
|
|
|
|
// Actually execute the block.
|
|
|
|
execute(sequencer, thread);
|
|
|
|
if (thread.status === Thread.STATUS_PROMISE_WAIT) {
|
2016-06-17 15:10:12 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-02-11 09:26:23 -05:00
|
|
|
|
|
|
|
// Execution returned immediately,
|
|
|
|
// and presumably a value was reported, so pop the stack.
|
|
|
|
currentStackFrame.waitingReporter = null;
|
|
|
|
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-10-03 17:43:24 -04:00
|
|
|
// Add any mutation to args (e.g., for procedures).
|
2017-04-17 15:10:04 -04:00
|
|
|
const mutation = blockContainer.getMutation(block);
|
2017-02-11 09:26:23 -05:00
|
|
|
if (mutation !== null) {
|
2016-10-03 17:43:24 -04:00
|
|
|
argValues.mutation = mutation;
|
|
|
|
}
|
|
|
|
|
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
|
2016-08-10 11:27:21 -04:00
|
|
|
// (e.g., on return from a branch) gets fresh inputs.
|
2016-06-20 14:29:47 -04:00
|
|
|
currentStackFrame.reported = {};
|
|
|
|
|
2017-04-17 15:10:04 -04:00
|
|
|
let primitiveReportedValue = null;
|
2016-06-20 14:31:48 -04:00
|
|
|
primitiveReportedValue = blockFunction(argValues, {
|
2016-08-23 18:12:32 -04:00
|
|
|
stackFrame: currentStackFrame.executionContext,
|
|
|
|
target: target,
|
2016-10-23 12:41:45 -04:00
|
|
|
yield: function () {
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.status = Thread.STATUS_YIELD;
|
2016-07-01 10:44:43 -04:00
|
|
|
},
|
2016-10-17 23:23:16 -04:00
|
|
|
startBranch: function (branchNum, isLoop) {
|
|
|
|
sequencer.stepToBranch(thread, branchNum, isLoop);
|
2016-06-29 20:56:55 -04:00
|
|
|
},
|
2016-10-13 23:00:46 -04:00
|
|
|
stopAll: function () {
|
|
|
|
runtime.stopAll();
|
|
|
|
},
|
2016-10-23 12:41:45 -04:00
|
|
|
stopOtherTargetThreads: function () {
|
2016-10-13 23:00:46 -04:00
|
|
|
runtime.stopForTarget(target, thread);
|
|
|
|
},
|
2017-02-08 04:44:10 -05:00
|
|
|
stopThisScript: function () {
|
|
|
|
thread.stopThisScript();
|
2016-10-13 23:00:46 -04:00
|
|
|
},
|
2016-10-17 23:23:16 -04:00
|
|
|
startProcedure: function (procedureCode) {
|
|
|
|
sequencer.stepToProcedure(thread, procedureCode);
|
2016-10-03 17:43:24 -04:00
|
|
|
},
|
2016-10-17 23:23:16 -04:00
|
|
|
getProcedureParamNames: function (procedureCode) {
|
|
|
|
return blockContainer.getProcedureParamNames(procedureCode);
|
2016-10-13 13:11:26 -04:00
|
|
|
},
|
|
|
|
pushParam: function (paramName, paramValue) {
|
|
|
|
thread.pushParam(paramName, paramValue);
|
|
|
|
},
|
|
|
|
getParam: function (paramName) {
|
|
|
|
return thread.getParam(paramName);
|
|
|
|
},
|
2016-10-23 17:55:31 -04:00
|
|
|
startHats: function (requestedHat, optMatchFields, optTarget) {
|
2016-08-23 18:12:32 -04:00
|
|
|
return (
|
2016-10-23 17:55:31 -04:00
|
|
|
runtime.startHats(requestedHat, optMatchFields, optTarget)
|
2016-08-23 18:12:32 -04:00
|
|
|
);
|
|
|
|
},
|
2016-08-15 21:37:36 -04:00
|
|
|
ioQuery: function (device, func, args) {
|
|
|
|
// Find the I/O device and execute the query/function call.
|
|
|
|
if (runtime.ioDevices[device] && runtime.ioDevices[device][func]) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const devObject = runtime.ioDevices[device];
|
2016-10-24 13:01:57 -04:00
|
|
|
// @todo Figure out why eslint complains about no-useless-call
|
|
|
|
// no-useless-call can't tell if the call is useless for dynamic
|
|
|
|
// expressions... or something. Not exactly sure why it
|
|
|
|
// complains here.
|
|
|
|
// eslint-disable-next-line no-useless-call
|
2016-10-24 14:07:38 -04:00
|
|
|
return devObject[func].call(devObject, args);
|
2016-08-15 21:37:36 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-13 11:23:39 -04:00
|
|
|
});
|
2016-06-24 11:14:22 -04:00
|
|
|
|
2016-09-08 09:40:53 -04:00
|
|
|
if (typeof primitiveReportedValue === 'undefined') {
|
|
|
|
// No value reported - potentially a command block.
|
|
|
|
// Edge-activated hats don't request a glow; all commands do.
|
|
|
|
thread.requestScriptGlowInFrame = true;
|
|
|
|
}
|
|
|
|
|
2016-06-24 11:14:22 -04:00
|
|
|
// If it's a promise, wait until promise resolves.
|
2016-08-23 18:12:32 -04:00
|
|
|
if (isPromise(primitiveReportedValue)) {
|
2016-06-28 13:39:44 -04:00
|
|
|
if (thread.status === Thread.STATUS_RUNNING) {
|
|
|
|
// Primitive returned a promise; automatically yield thread.
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.status = Thread.STATUS_PROMISE_WAIT;
|
2016-06-28 13:39:44 -04:00
|
|
|
}
|
|
|
|
// Promise handlers
|
2017-04-17 15:10:04 -04:00
|
|
|
primitiveReportedValue.then(resolvedValue => {
|
2016-08-23 18:12:32 -04:00
|
|
|
handleReport(resolvedValue);
|
2016-10-23 17:55:31 -04:00
|
|
|
if (typeof resolvedValue === 'undefined') {
|
2017-04-17 17:15:19 -04:00
|
|
|
let stackFrame;
|
|
|
|
let nextBlockId;
|
2017-02-20 03:20:21 -05:00
|
|
|
do {
|
|
|
|
// In the case that the promise is the last block in the current thread stack
|
|
|
|
// We need to pop out repeatedly until we find the next block.
|
2017-04-17 15:10:04 -04:00
|
|
|
const popped = thread.popStack();
|
2017-02-20 03:20:21 -05:00
|
|
|
if (popped === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-17 17:15:19 -04:00
|
|
|
nextBlockId = thread.target.blocks.getNextBlock(popped);
|
2017-02-20 04:40:32 -05:00
|
|
|
if (nextBlockId !== null) {
|
|
|
|
// A next block exists so break out this loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Investigate the next block and if not in a loop,
|
|
|
|
// then repeat and pop the next item off the stack frame
|
2017-04-17 17:15:19 -04:00
|
|
|
stackFrame = thread.peekStackFrame();
|
2017-02-20 04:40:32 -05:00
|
|
|
} while (stackFrame !== null && !stackFrame.isLoop);
|
|
|
|
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.pushStack(nextBlockId);
|
2016-10-23 17:55:31 -04:00
|
|
|
} else {
|
|
|
|
thread.popStack();
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2017-04-17 15:10:04 -04:00
|
|
|
}, rejectionReason => {
|
2016-06-24 11:19:38 -04:00
|
|
|
// Promise rejected: the primitive had some error.
|
|
|
|
// Log it and proceed.
|
2016-10-23 18:01:11 -04:00
|
|
|
log.warn('Primitive rejected promise: ', rejectionReason);
|
2016-10-17 23:23:16 -04:00
|
|
|
thread.status = Thread.STATUS_RUNNING;
|
|
|
|
thread.popStack();
|
2016-06-28 13:39:44 -04:00
|
|
|
});
|
2016-06-24 11:14:22 -04:00
|
|
|
} else if (thread.status === Thread.STATUS_RUNNING) {
|
2016-08-23 18:12:32 -04:00
|
|
|
handleReport(primitiveReportedValue);
|
2016-06-20 14:11:21 -04:00
|
|
|
}
|
2016-06-09 13:27:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = execute;
|