2017-11-03 11:46:57 -04:00
|
|
|
const BlockUtility = require('./block-utility');
|
2018-04-10 16:29:51 -04:00
|
|
|
const BlocksExecuteCache = require('./blocks-execute-cache');
|
2017-04-17 15:10:04 -04:00
|
|
|
const log = require('../util/log');
|
|
|
|
const Thread = require('./thread');
|
2017-05-19 17:28:00 -04:00
|
|
|
const {Map} = require('immutable');
|
2017-12-08 12:19:11 -05:00
|
|
|
const cast = require('../util/cast');
|
2016-06-17 15:10:12 -04:00
|
|
|
|
2017-11-03 11:46:57 -04:00
|
|
|
/**
|
|
|
|
* Single BlockUtility instance reused by execute for every pritimive ran.
|
|
|
|
* @const
|
|
|
|
*/
|
|
|
|
const blockUtility = new BlockUtility();
|
|
|
|
|
2017-11-09 17:27:49 -05:00
|
|
|
/**
|
|
|
|
* Profiler frame name for block functions.
|
|
|
|
* @const {string}
|
|
|
|
*/
|
|
|
|
const blockFunctionProfilerFrame = 'blockFunction';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Profiler frame ID for 'blockFunction'.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
let blockFunctionProfilerId = -1;
|
|
|
|
|
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) {
|
2017-11-03 11:40:20 -04:00
|
|
|
return (
|
|
|
|
value !== null &&
|
|
|
|
typeof value === 'object' &&
|
|
|
|
typeof value.then === 'function'
|
|
|
|
);
|
2016-08-23 18:12:32 -04:00
|
|
|
};
|
|
|
|
|
2017-11-03 11:43:19 -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.
|
|
|
|
* @param {!Sequencer} sequencer Sequencer stepping the thread for the ran
|
|
|
|
* primitive.
|
|
|
|
* @param {!Thread} thread Thread containing the primitive.
|
|
|
|
* @param {!string} currentBlockId Id of the block in its thread for value from
|
|
|
|
* the primitive.
|
2017-11-03 11:46:57 -04:00
|
|
|
* @param {!string} opcode opcode used to identify a block function primitive.
|
2017-11-03 11:43:19 -04:00
|
|
|
* @param {!boolean} isHat Is the current block a hat?
|
|
|
|
*/
|
|
|
|
// @todo move this to callback attached to the thread when we have performance
|
|
|
|
// metrics (dd)
|
2018-07-13 10:23:42 -04:00
|
|
|
const handleReport = function (resolvedValue, sequencer, thread, blockCached, lastOperation) {
|
2018-06-05 23:20:42 -04:00
|
|
|
const currentBlockId = blockCached.id;
|
|
|
|
const opcode = blockCached.opcode;
|
|
|
|
const isHat = blockCached._isHat;
|
|
|
|
|
2017-11-03 11:43:19 -04:00
|
|
|
thread.pushReportedValue(resolvedValue);
|
|
|
|
if (isHat) {
|
|
|
|
// Hat predicate was evaluated.
|
|
|
|
if (sequencer.runtime.getIsEdgeActivatedHat(opcode)) {
|
|
|
|
// If this is an edge-activated hat, only proceed if the value is
|
|
|
|
// true and used to be false, or the stack was activated explicitly
|
|
|
|
// via stack click
|
|
|
|
if (!thread.stackClick) {
|
|
|
|
const oldEdgeValue = sequencer.runtime.updateEdgeActivatedValue(
|
|
|
|
currentBlockId,
|
|
|
|
resolvedValue
|
|
|
|
);
|
|
|
|
const edgeWasActivated = !oldEdgeValue && resolvedValue;
|
|
|
|
if (!edgeWasActivated) {
|
|
|
|
sequencer.retireThread(thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (!resolvedValue) {
|
|
|
|
// Not an edge-activated hat: retire the thread
|
|
|
|
// if predicate was false.
|
|
|
|
sequencer.retireThread(thread);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In a non-hat, report the value visually if necessary if
|
|
|
|
// at the top of the thread stack.
|
2018-07-13 10:23:42 -04:00
|
|
|
if (lastOperation && typeof resolvedValue !== 'undefined' && thread.atStackTop()) {
|
2017-11-03 11:43:19 -04:00
|
|
|
if (thread.stackClick) {
|
|
|
|
sequencer.runtime.visualReport(currentBlockId, resolvedValue);
|
|
|
|
}
|
|
|
|
if (thread.updateMonitor) {
|
2017-11-13 16:28:50 -05:00
|
|
|
const targetId = sequencer.runtime.monitorBlocks.getBlock(currentBlockId).targetId;
|
2017-11-14 18:25:54 -05:00
|
|
|
if (targetId && !sequencer.runtime.getTargetById(targetId)) {
|
|
|
|
// Target no longer exists
|
|
|
|
return;
|
|
|
|
}
|
2017-11-03 11:43:19 -04:00
|
|
|
sequencer.runtime.requestUpdateMonitor(Map({
|
|
|
|
id: currentBlockId,
|
2017-11-13 16:28:50 -05:00
|
|
|
spriteName: targetId ? sequencer.runtime.getTargetById(targetId).getName() : null,
|
2018-05-09 11:39:14 -04:00
|
|
|
value: resolvedValue
|
2017-11-03 11:43:19 -04:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Finished any yields.
|
|
|
|
thread.status = Thread.STATUS_RUNNING;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-12 18:19:05 -04:00
|
|
|
const handlePromise = (primitiveReportedValue, sequencer, thread, blockCached, lastOperation) => {
|
2018-06-05 23:20:42 -04:00
|
|
|
if (thread.status === Thread.STATUS_RUNNING) {
|
|
|
|
// Primitive returned a promise; automatically yield thread.
|
|
|
|
thread.status = Thread.STATUS_PROMISE_WAIT;
|
|
|
|
}
|
|
|
|
// Promise handlers
|
|
|
|
primitiveReportedValue.then(resolvedValue => {
|
2018-07-13 10:23:42 -04:00
|
|
|
handleReport(resolvedValue, sequencer, thread, blockCached, lastOperation);
|
2018-07-12 18:19:05 -04:00
|
|
|
// If its a command block.
|
|
|
|
if (lastOperation && typeof resolvedValue === 'undefined') {
|
2018-06-05 23:20:42 -04:00
|
|
|
let stackFrame;
|
|
|
|
let nextBlockId;
|
|
|
|
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.
|
|
|
|
const popped = thread.popStack();
|
|
|
|
if (popped === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nextBlockId = thread.target.blocks.getNextBlock(popped);
|
|
|
|
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
|
|
|
|
stackFrame = thread.peekStackFrame();
|
|
|
|
} while (stackFrame !== null && !stackFrame.isLoop);
|
|
|
|
|
|
|
|
thread.pushStack(nextBlockId);
|
|
|
|
}
|
|
|
|
}, rejectionReason => {
|
|
|
|
// Promise rejected: the primitive had some error.
|
|
|
|
// Log it and proceed.
|
|
|
|
log.warn('Primitive rejected promise: ', rejectionReason);
|
|
|
|
thread.status = Thread.STATUS_RUNNING;
|
|
|
|
thread.popStack();
|
|
|
|
});
|
|
|
|
};
|
2018-04-10 16:29:51 -04:00
|
|
|
|
2018-04-16 17:35:56 -04:00
|
|
|
/**
|
2018-05-07 11:09:45 -04:00
|
|
|
* A execute.js internal representation of a block to reduce the time spent in
|
|
|
|
* execute as the same blocks are called the most.
|
|
|
|
*
|
|
|
|
* With the help of the Blocks class create a mutable copy of block
|
|
|
|
* information. The members of BlockCached derived values of block information
|
|
|
|
* that does not need to be reevaluated until a change in Blocks. Since Blocks
|
|
|
|
* handles where the cache instance is stored, it drops all cache versions of a
|
|
|
|
* block when any change happens to it. This way we can quickly execute blocks
|
|
|
|
* and keep perform the right action according to the current block information
|
|
|
|
* in the editor.
|
|
|
|
*
|
|
|
|
* @param {Blocks} blockContainer the related Blocks instance
|
|
|
|
* @param {object} cached default set of cached values
|
2018-04-16 17:35:56 -04:00
|
|
|
*/
|
2018-05-07 11:09:45 -04:00
|
|
|
class BlockCached {
|
|
|
|
constructor (blockContainer, cached) {
|
2018-06-05 23:20:42 -04:00
|
|
|
/**
|
|
|
|
* Block id in its parent set of blocks.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.id = cached.id;
|
|
|
|
|
2018-05-07 11:09:45 -04:00
|
|
|
/**
|
|
|
|
* Block operation code for this block.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this.opcode = cached.opcode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Original block object containing argument values for static fields.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this.fields = cached.fields;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Original block object containing argument values for executable inputs.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this.inputs = cached.inputs;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Procedure mutation.
|
|
|
|
* @type {?object}
|
|
|
|
*/
|
|
|
|
this.mutation = cached.mutation;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the opcode a hat (event responder) block.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this._isHat = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The block opcode's implementation function.
|
|
|
|
* @type {?function}
|
|
|
|
*/
|
|
|
|
this._blockFunction = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is the block function defined for this opcode?
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this._definedBlockFunction = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this block a block with no function but a static value to return.
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this._isShadowBlock = false;
|
2018-05-07 10:31:28 -04:00
|
|
|
|
2018-05-07 11:09:45 -04:00
|
|
|
/**
|
|
|
|
* The static value of this block if it is a shadow block.
|
|
|
|
* @type {?any}
|
|
|
|
*/
|
|
|
|
this._shadowValue = null;
|
2018-05-07 10:31:28 -04:00
|
|
|
|
2018-05-07 11:09:45 -04:00
|
|
|
/**
|
|
|
|
* A copy of the block's fields that may be modified.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this._fields = Object.assign({}, this.fields);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A copy of the block's inputs that may be modified.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this._inputs = Object.assign({}, this.inputs);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An arguments object for block implementations. All executions of this
|
|
|
|
* specific block will use this objecct.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this._argValues = {
|
|
|
|
mutation: this.mutation
|
2018-05-07 10:31:28 -04:00
|
|
|
};
|
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
/**
|
|
|
|
* The inputs key the parent refers to this BlockCached by.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
this._parentKey = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The target object where the parent wants the resulting value stored
|
|
|
|
* with _parentKey as the key.
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
this._parentValues = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A sequence of shadow value operations that can be performed in any
|
|
|
|
* order and are easier to perform given that they are static.
|
|
|
|
* @type {Array<BlockCached>}
|
|
|
|
*/
|
|
|
|
this._shadowOps = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A sequence of non-shadow operations that can must be performed. This
|
|
|
|
* list recreates the order this block and its children are executed.
|
|
|
|
* Since the order is always the same we can safely store that order
|
|
|
|
* and iterate over the operations instead of dynamically walking the
|
|
|
|
* tree every time.
|
|
|
|
* @type {Array<BlockCached>}
|
|
|
|
*/
|
|
|
|
this._ops = [];
|
|
|
|
|
2018-05-07 11:09:45 -04:00
|
|
|
const {runtime} = blockUtility.sequencer;
|
|
|
|
|
|
|
|
const {opcode, fields, inputs} = this;
|
|
|
|
|
|
|
|
// Assign opcode isHat and blockFunction data to avoid dynamic lookups.
|
|
|
|
this._isHat = runtime.getIsHat(opcode);
|
|
|
|
this._blockFunction = runtime.getOpcodeFunction(opcode);
|
|
|
|
this._definedBlockFunction = typeof this._blockFunction !== 'undefined';
|
2018-05-07 10:31:28 -04:00
|
|
|
|
2018-05-07 11:09:45 -04:00
|
|
|
// Store the current shadow value if there is a shadow value.
|
|
|
|
const fieldKeys = Object.keys(fields);
|
|
|
|
this._isShadowBlock = (
|
|
|
|
!this._definedBlockFunction &&
|
|
|
|
fieldKeys.length === 1 &&
|
|
|
|
Object.keys(inputs).length === 0
|
|
|
|
);
|
|
|
|
this._shadowValue = this._isShadowBlock && fields[fieldKeys[0]].value;
|
|
|
|
|
|
|
|
// Store the static fields onto _argValues.
|
|
|
|
for (const fieldName in fields) {
|
|
|
|
if (
|
|
|
|
fieldName === 'VARIABLE' ||
|
|
|
|
fieldName === 'LIST' ||
|
|
|
|
fieldName === 'BROADCAST_OPTION'
|
|
|
|
) {
|
|
|
|
this._argValues[fieldName] = {
|
|
|
|
id: fields[fieldName].id,
|
|
|
|
name: fields[fieldName].value
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
this._argValues[fieldName] = fields[fieldName].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove custom_block. It is not part of block execution.
|
|
|
|
delete this._inputs.custom_block;
|
|
|
|
|
|
|
|
if ('BROADCAST_INPUT' in this._inputs) {
|
|
|
|
// BROADCAST_INPUT is called BROADCAST_OPTION in the args and is an
|
|
|
|
// object with an unchanging shape.
|
|
|
|
this._argValues.BROADCAST_OPTION = {
|
|
|
|
id: null,
|
|
|
|
name: null
|
|
|
|
};
|
|
|
|
|
|
|
|
// We can go ahead and compute BROADCAST_INPUT if it is a shadow
|
|
|
|
// value.
|
|
|
|
const broadcastInput = this._inputs.BROADCAST_INPUT;
|
|
|
|
if (broadcastInput.block === broadcastInput.shadow) {
|
|
|
|
// Shadow dropdown menu is being used.
|
|
|
|
// Get the appropriate information out of it.
|
|
|
|
const shadow = blockContainer.getBlock(broadcastInput.shadow);
|
|
|
|
const broadcastField = shadow.fields.BROADCAST_OPTION;
|
|
|
|
this._argValues.BROADCAST_OPTION.id = broadcastField.id;
|
|
|
|
this._argValues.BROADCAST_OPTION.name = broadcastField.value;
|
|
|
|
|
|
|
|
// Evaluating BROADCAST_INPUT here we do not need to do so
|
|
|
|
// later.
|
|
|
|
delete this._inputs.BROADCAST_INPUT;
|
|
|
|
}
|
2018-05-07 10:31:28 -04:00
|
|
|
}
|
2018-06-05 23:20:42 -04:00
|
|
|
|
|
|
|
// Cache all input children blocks in the operation lists. The
|
|
|
|
// operations can later be run in the order they appear in correctly
|
|
|
|
// executing the operations quickly in a flat loop instead of needing to
|
|
|
|
// recursivly iterate them.
|
|
|
|
for (const inputName in this._inputs) {
|
|
|
|
const input = this._inputs[inputName];
|
|
|
|
if (input.block) {
|
|
|
|
const inputCached = BlocksExecuteCache.getCached(blockContainer, input.block, BlockCached);
|
|
|
|
|
|
|
|
if (inputCached._isHat) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._shadowOps.push(...inputCached._shadowOps);
|
|
|
|
this._ops.push(...inputCached._ops);
|
|
|
|
inputCached._parentKey = inputName;
|
|
|
|
inputCached._parentValues = this._argValues;
|
|
|
|
|
|
|
|
// Shadow values are static and do not change, go ahead and
|
|
|
|
// store their value on args.
|
|
|
|
if (inputCached._isShadowBlock) {
|
|
|
|
this._argValues[inputName] = inputCached._shadowValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The final operation is this block itself. At the top most block is a
|
|
|
|
// command block or a block that is being run as a monitor.
|
|
|
|
if (!this._isHat && this._isShadowBlock) {
|
|
|
|
this._shadowOps.push(this);
|
|
|
|
} else if (this._definedBlockFunction) {
|
|
|
|
this._ops.push(this);
|
|
|
|
}
|
2018-05-07 10:31:28 -04:00
|
|
|
}
|
2018-05-07 11:09:45 -04:00
|
|
|
}
|
2018-04-16 17:35:56 -04:00
|
|
|
|
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.
|
|
|
|
*/
|
2018-06-05 23:20:42 -04:00
|
|
|
const execute = function (sequencer, thread) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const runtime = sequencer.runtime;
|
2016-10-13 17:15:49 -04:00
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
// store sequencer and thread so block functions can access them through
|
|
|
|
// convenience methods.
|
|
|
|
blockUtility.sequencer = sequencer;
|
|
|
|
blockUtility.thread = thread;
|
2018-05-07 10:31:28 -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
|
|
|
|
2018-04-10 16:29:51 -04:00
|
|
|
let blockContainer = thread.blockContainer;
|
2018-05-07 10:31:28 -04:00
|
|
|
let blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached);
|
2018-05-04 12:33:42 -04:00
|
|
|
if (blockCached === null) {
|
2016-10-13 17:15:49 -04:00
|
|
|
blockContainer = runtime.flyoutBlocks;
|
2018-05-04 12:33:42 -04:00
|
|
|
blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached);
|
2017-02-11 09:26:23 -05:00
|
|
|
// Stop if block or target no longer exists.
|
2018-05-04 12:33:42 -04:00
|
|
|
if (blockCached === null) {
|
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
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
const ops = blockCached._ops;
|
|
|
|
const length = ops.length;
|
|
|
|
let i = 0;
|
2017-02-11 09:26:23 -05:00
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
if (currentStackFrame.reported !== null) {
|
|
|
|
const reported = currentStackFrame.reported;
|
|
|
|
// Reinstate all the previous values.
|
|
|
|
for (; i < reported.length; i++) {
|
2018-07-12 18:19:05 -04:00
|
|
|
const {opCached: oldOpCached, inputValue} = reported[i];
|
|
|
|
|
|
|
|
const opCached = ops.find(op => op.id === oldOpCached);
|
|
|
|
|
|
|
|
if (opCached) {
|
|
|
|
const inputName = opCached._parentKey;
|
|
|
|
const argValues = opCached._parentValues;
|
|
|
|
|
|
|
|
if (inputName === 'BROADCAST_INPUT') {
|
|
|
|
// Something is plugged into the broadcast input.
|
|
|
|
// Cast it to a string. We don't need an id here.
|
|
|
|
argValues.BROADCAST_OPTION.id = null;
|
|
|
|
argValues.BROADCAST_OPTION.name = cast.toString(inputValue);
|
|
|
|
} else {
|
|
|
|
argValues[inputName] = inputValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the last reported block that is still in the set of operations.
|
|
|
|
// This way if the last operation was removed, we'll find the next
|
|
|
|
// candidate. If an earlier block that was performed was removed then
|
|
|
|
// we'll find the index where the last operation is now.
|
|
|
|
if (reported.length > 0) {
|
|
|
|
const lastExisting = reported.reverse().find(report => ops.find(op => op.id === report.opCached));
|
|
|
|
if (lastExisting) {
|
|
|
|
i = ops.findIndex(opCached => opCached.id === lastExisting.opCached) + 1;
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The reporting block must exist and must be the next one in the sequence of operations.
|
|
|
|
if (thread.justReported !== null && ops[i] && ops[i].id === currentStackFrame.reporting) {
|
|
|
|
const opCached = ops[i];
|
|
|
|
const inputValue = thread.justReported;
|
|
|
|
|
|
|
|
thread.justReported = null;
|
2018-04-18 12:24:39 -04:00
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
const inputName = opCached._parentKey;
|
|
|
|
const argValues = opCached._parentValues;
|
2018-05-07 10:31:28 -04:00
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
if (inputName === 'BROADCAST_INPUT') {
|
2017-12-08 12:19:11 -05:00
|
|
|
// Something is plugged into the broadcast input.
|
|
|
|
// Cast it to a string. We don't need an id here.
|
2018-05-07 10:31:28 -04:00
|
|
|
argValues.BROADCAST_OPTION.id = null;
|
|
|
|
argValues.BROADCAST_OPTION.name = cast.toString(inputValue);
|
2018-06-05 23:20:42 -04:00
|
|
|
} else {
|
|
|
|
argValues[inputName] = inputValue;
|
2017-12-08 12:19:11 -05:00
|
|
|
}
|
2016-06-09 13:27:30 -04:00
|
|
|
|
2018-07-12 18:19:05 -04:00
|
|
|
i += 1;
|
2017-11-09 17:27:49 -05:00
|
|
|
}
|
2016-06-24 11:14:22 -04:00
|
|
|
|
2018-07-12 18:19:05 -04:00
|
|
|
currentStackFrame.reporting = null;
|
2018-06-05 23:20:42 -04:00
|
|
|
currentStackFrame.reported = null;
|
2016-09-08 09:40:53 -04:00
|
|
|
}
|
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
for (; i < length; i++) {
|
2018-07-13 10:23:42 -04:00
|
|
|
const lastOperation = i === length - 1;
|
2018-06-05 23:20:42 -04:00
|
|
|
const opCached = ops[i];
|
|
|
|
|
|
|
|
const blockFunction = opCached._blockFunction;
|
|
|
|
|
|
|
|
// Update values for arguments (inputs).
|
|
|
|
const argValues = opCached._argValues;
|
|
|
|
|
|
|
|
// Fields are set during opCached initialization.
|
|
|
|
|
2018-08-22 14:44:06 -04:00
|
|
|
// Blocks should glow when a script is starting,
|
|
|
|
// not after it has finished (see #1404).
|
|
|
|
// Only blocks in blockContainers that don't forceNoGlow
|
|
|
|
// should request a glow.
|
|
|
|
if (!blockContainer.forceNoGlow) {
|
|
|
|
thread.requestScriptGlowInFrame = true;
|
|
|
|
}
|
|
|
|
|
2018-06-05 23:20:42 -04:00
|
|
|
// Inputs are set during previous steps in the loop.
|
|
|
|
|
|
|
|
let primitiveReportedValue = null;
|
|
|
|
if (runtime.profiler === null) {
|
|
|
|
primitiveReportedValue = blockFunction(argValues, blockUtility);
|
|
|
|
} else {
|
|
|
|
const opcode = opCached.opcode;
|
|
|
|
if (blockFunctionProfilerId === -1) {
|
|
|
|
blockFunctionProfilerId = runtime.profiler.idByName(blockFunctionProfilerFrame);
|
|
|
|
}
|
|
|
|
// The method commented below has its code inlined
|
|
|
|
// underneath to reduce the bias recorded for the profiler's
|
|
|
|
// calls in this time sensitive execute function.
|
|
|
|
//
|
|
|
|
// runtime.profiler.start(blockFunctionProfilerId, opcode);
|
|
|
|
runtime.profiler.records.push(
|
|
|
|
runtime.profiler.START, blockFunctionProfilerId, opcode, 0);
|
|
|
|
|
|
|
|
primitiveReportedValue = blockFunction(argValues, blockUtility);
|
|
|
|
|
|
|
|
// runtime.profiler.stop(blockFunctionProfilerId);
|
|
|
|
runtime.profiler.records.push(runtime.profiler.STOP, 0);
|
2016-06-28 13:39:44 -04:00
|
|
|
}
|
2018-06-05 23:20:42 -04:00
|
|
|
|
|
|
|
// If it's a promise, wait until promise resolves.
|
|
|
|
if (isPromise(primitiveReportedValue)) {
|
2018-07-13 10:23:42 -04:00
|
|
|
handlePromise(primitiveReportedValue, sequencer, thread, opCached, lastOperation);
|
2018-07-12 18:19:05 -04:00
|
|
|
|
|
|
|
// Store the already reported values. They will be thawed into the
|
|
|
|
// future versions of the same operations by block id. The reporting
|
|
|
|
// operation if it is promise waiting will set its parent value at
|
|
|
|
// that time.
|
|
|
|
thread.justReported = null;
|
|
|
|
currentStackFrame.reporting = ops[i].id;
|
2018-06-05 23:20:42 -04:00
|
|
|
currentStackFrame.reported = ops.slice(0, i).map(reportedCached => {
|
|
|
|
const inputName = reportedCached._parentKey;
|
|
|
|
const reportedValues = reportedCached._parentValues;
|
|
|
|
|
|
|
|
if (inputName === 'BROADCAST_INPUT') {
|
|
|
|
return {
|
2018-07-12 18:19:05 -04:00
|
|
|
opCached: reportedCached.id,
|
2018-06-05 23:20:42 -04:00
|
|
|
inputValue: reportedValues[inputName].BROADCAST_OPTION.name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return {
|
2018-07-12 18:19:05 -04:00
|
|
|
opCached: reportedCached.id,
|
2018-06-05 23:20:42 -04:00
|
|
|
inputValue: reportedValues[inputName]
|
|
|
|
};
|
|
|
|
});
|
2018-07-12 18:19:05 -04:00
|
|
|
|
|
|
|
// We are waiting for a promise. Stop running this set of operations
|
|
|
|
// and continue them later after thawing the reported values.
|
|
|
|
break;
|
2018-06-05 23:20:42 -04:00
|
|
|
} else if (thread.status === Thread.STATUS_RUNNING) {
|
2018-07-13 10:23:42 -04:00
|
|
|
if (lastOperation) {
|
|
|
|
handleReport(primitiveReportedValue, sequencer, thread, opCached, lastOperation);
|
2016-10-23 17:55:31 -04:00
|
|
|
} else {
|
2018-06-05 23:20:42 -04:00
|
|
|
// By definition a block that is not last in the list has a
|
|
|
|
// parent.
|
|
|
|
const inputName = opCached._parentKey;
|
|
|
|
const parentValues = opCached._parentValues;
|
|
|
|
|
|
|
|
if (inputName === 'BROADCAST_INPUT') {
|
|
|
|
// Something is plugged into the broadcast input.
|
|
|
|
// Cast it to a string. We don't need an id here.
|
|
|
|
parentValues.BROADCAST_OPTION.id = null;
|
|
|
|
parentValues.BROADCAST_OPTION.name = cast.toString(primitiveReportedValue);
|
|
|
|
} else {
|
|
|
|
parentValues[inputName] = primitiveReportedValue;
|
|
|
|
}
|
2016-10-17 23:23:16 -04:00
|
|
|
}
|
2018-04-10 16:29:51 -04:00
|
|
|
}
|
2016-06-20 14:11:21 -04:00
|
|
|
}
|
2016-06-09 13:27:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = execute;
|