|
|
const BlockUtility = require('./block-utility'); |
|
|
const BlocksExecuteCache = require('./blocks-execute-cache'); |
|
|
const log = require('../util/log'); |
|
|
const Thread = require('./thread'); |
|
|
const {Map} = require('immutable'); |
|
|
const cast = require('../util/cast'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const blockUtility = new BlockUtility(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const blockFunctionProfilerFrame = 'blockFunction'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let blockFunctionProfilerId = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const isPromise = function (value) { |
|
|
return ( |
|
|
value !== null && |
|
|
typeof value === 'object' && |
|
|
typeof value.then === 'function' |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleReport = function (resolvedValue, sequencer, thread, blockCached, lastOperation) { |
|
|
const currentBlockId = blockCached.id; |
|
|
const opcode = blockCached.opcode; |
|
|
const isHat = blockCached._isHat; |
|
|
|
|
|
thread.pushReportedValue(resolvedValue); |
|
|
if (isHat) { |
|
|
|
|
|
if (thread.stackClick) { |
|
|
thread.status = Thread.STATUS_RUNNING; |
|
|
} else if (sequencer.runtime.getIsEdgeActivatedHat(opcode)) { |
|
|
|
|
|
|
|
|
|
|
|
const hasOldEdgeValue = thread.target.hasEdgeActivatedValue(currentBlockId); |
|
|
const oldEdgeValue = thread.target.updateEdgeActivatedValue( |
|
|
currentBlockId, |
|
|
resolvedValue |
|
|
); |
|
|
|
|
|
const edgeWasActivated = hasOldEdgeValue ? (!oldEdgeValue && resolvedValue) : resolvedValue; |
|
|
if (edgeWasActivated) { |
|
|
thread.status = Thread.STATUS_RUNNING; |
|
|
} else { |
|
|
sequencer.retireThread(thread); |
|
|
} |
|
|
} else if (resolvedValue) { |
|
|
|
|
|
thread.status = Thread.STATUS_RUNNING; |
|
|
} else { |
|
|
|
|
|
sequencer.retireThread(thread); |
|
|
} |
|
|
} else { |
|
|
|
|
|
|
|
|
if (lastOperation && typeof resolvedValue !== 'undefined' && thread.atStackTop()) { |
|
|
if (thread.stackClick) { |
|
|
sequencer.runtime.visualReport(currentBlockId, resolvedValue); |
|
|
} |
|
|
if (thread.updateMonitor) { |
|
|
const targetId = sequencer.runtime.monitorBlocks.getBlock(currentBlockId).targetId; |
|
|
if (targetId && !sequencer.runtime.getTargetById(targetId)) { |
|
|
|
|
|
return; |
|
|
} |
|
|
sequencer.runtime.requestUpdateMonitor(Map({ |
|
|
id: currentBlockId, |
|
|
spriteName: targetId ? sequencer.runtime.getTargetById(targetId).getName() : null, |
|
|
value: resolvedValue |
|
|
})); |
|
|
} |
|
|
} |
|
|
|
|
|
thread.status = Thread.STATUS_RUNNING; |
|
|
} |
|
|
}; |
|
|
|
|
|
const handlePromise = (primitiveReportedValue, sequencer, thread, blockCached, lastOperation) => { |
|
|
if (thread.status === Thread.STATUS_RUNNING) { |
|
|
|
|
|
thread.status = Thread.STATUS_PROMISE_WAIT; |
|
|
} |
|
|
|
|
|
primitiveReportedValue.then(resolvedValue => { |
|
|
handleReport(resolvedValue, sequencer, thread, blockCached, lastOperation); |
|
|
|
|
|
|
|
|
|
|
|
if (lastOperation && (!blockCached._isHat || thread.stackClick)) { |
|
|
let stackFrame; |
|
|
let nextBlockId; |
|
|
do { |
|
|
|
|
|
|
|
|
const popped = thread.popStack(); |
|
|
if (popped === null) { |
|
|
return; |
|
|
} |
|
|
nextBlockId = thread.target.blocks.getNextBlock(popped); |
|
|
if (nextBlockId !== null) { |
|
|
|
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
stackFrame = thread.peekStackFrame(); |
|
|
} while (stackFrame !== null && !stackFrame.isLoop); |
|
|
|
|
|
thread.pushStack(nextBlockId); |
|
|
} |
|
|
}, rejectionReason => { |
|
|
|
|
|
|
|
|
log.warn('Primitive rejected promise: ', rejectionReason); |
|
|
thread.status = Thread.STATUS_RUNNING; |
|
|
thread.popStack(); |
|
|
}); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BlockCached { |
|
|
constructor (blockContainer, cached) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.id = cached.id; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.opcode = cached.opcode; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.fields = cached.fields; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.inputs = cached.inputs; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.mutation = cached.mutation; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._profiler = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._profilerFrame = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._isHat = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._blockFunction = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._definedBlockFunction = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._isShadowBlock = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._shadowValue = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._fields = Object.assign({}, this.fields); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._inputs = Object.assign({}, this.inputs); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._argValues = { |
|
|
mutation: this.mutation |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._parentKey = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._parentValues = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this._ops = []; |
|
|
|
|
|
const {runtime} = blockUtility.sequencer; |
|
|
|
|
|
const {opcode, fields, inputs} = this; |
|
|
|
|
|
|
|
|
this._isHat = runtime.getIsHat(opcode); |
|
|
this._blockFunction = runtime.getOpcodeFunction(opcode); |
|
|
this._definedBlockFunction = typeof this._blockFunction !== 'undefined'; |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
for (const fieldName in fields) { |
|
|
const field = fields[fieldName]; |
|
|
if (typeof field.variableType === 'string') { |
|
|
this._argValues[fieldName] = { |
|
|
id: field.id, |
|
|
name: field.value |
|
|
}; |
|
|
} else { |
|
|
this._argValues[fieldName] = field.value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
delete this._inputs.custom_block; |
|
|
|
|
|
if ('BROADCAST_INPUT' in this._inputs) { |
|
|
|
|
|
|
|
|
this._argValues.BROADCAST_OPTION = { |
|
|
id: null, |
|
|
name: null |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const broadcastInput = this._inputs.BROADCAST_INPUT; |
|
|
if (broadcastInput.block === broadcastInput.shadow) { |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
delete this._inputs.BROADCAST_INPUT; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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._ops.push(...inputCached._ops); |
|
|
inputCached._parentKey = inputName; |
|
|
inputCached._parentValues = this._argValues; |
|
|
|
|
|
|
|
|
|
|
|
if (inputCached._isShadowBlock) { |
|
|
this._argValues[inputName] = inputCached._shadowValue; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (this._definedBlockFunction) { |
|
|
this._ops.push(this); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const _prepareBlockProfiling = function (profiler, blockCached) { |
|
|
blockCached._profiler = profiler; |
|
|
|
|
|
if (blockFunctionProfilerId === -1) { |
|
|
blockFunctionProfilerId = profiler.idByName(blockFunctionProfilerFrame); |
|
|
} |
|
|
|
|
|
const ops = blockCached._ops; |
|
|
for (let i = 0; i < ops.length; i++) { |
|
|
ops[i]._profilerFrame = profiler.frame(blockFunctionProfilerId, ops[i].opcode); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const execute = function (sequencer, thread) { |
|
|
const runtime = sequencer.runtime; |
|
|
|
|
|
|
|
|
|
|
|
blockUtility.sequencer = sequencer; |
|
|
blockUtility.thread = thread; |
|
|
|
|
|
|
|
|
const currentBlockId = thread.peekStack(); |
|
|
const currentStackFrame = thread.peekStackFrame(); |
|
|
|
|
|
let blockContainer = thread.blockContainer; |
|
|
let blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached); |
|
|
if (blockCached === null) { |
|
|
blockContainer = runtime.flyoutBlocks; |
|
|
blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached); |
|
|
|
|
|
if (blockCached === null) { |
|
|
|
|
|
sequencer.retireThread(thread); |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
const ops = blockCached._ops; |
|
|
const length = ops.length; |
|
|
let i = 0; |
|
|
|
|
|
if (currentStackFrame.reported !== null) { |
|
|
const reported = currentStackFrame.reported; |
|
|
|
|
|
for (; i < reported.length; i++) { |
|
|
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') { |
|
|
|
|
|
|
|
|
argValues.BROADCAST_OPTION.id = null; |
|
|
argValues.BROADCAST_OPTION.name = cast.toString(inputValue); |
|
|
} else { |
|
|
argValues[inputName] = inputValue; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (thread.justReported !== null && ops[i] && ops[i].id === currentStackFrame.reporting) { |
|
|
const opCached = ops[i]; |
|
|
const inputValue = thread.justReported; |
|
|
|
|
|
thread.justReported = null; |
|
|
|
|
|
const inputName = opCached._parentKey; |
|
|
const argValues = opCached._parentValues; |
|
|
|
|
|
if (inputName === 'BROADCAST_INPUT') { |
|
|
|
|
|
|
|
|
argValues.BROADCAST_OPTION.id = null; |
|
|
argValues.BROADCAST_OPTION.name = cast.toString(inputValue); |
|
|
} else { |
|
|
argValues[inputName] = inputValue; |
|
|
} |
|
|
|
|
|
i += 1; |
|
|
} |
|
|
|
|
|
currentStackFrame.reporting = null; |
|
|
currentStackFrame.reported = null; |
|
|
} |
|
|
|
|
|
const start = i; |
|
|
|
|
|
for (; i < length; i++) { |
|
|
const lastOperation = i === length - 1; |
|
|
const opCached = ops[i]; |
|
|
|
|
|
const blockFunction = opCached._blockFunction; |
|
|
|
|
|
|
|
|
const argValues = opCached._argValues; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!blockContainer.forceNoGlow) { |
|
|
thread.requestScriptGlowInFrame = true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const primitiveReportedValue = blockFunction(argValues, blockUtility); |
|
|
|
|
|
|
|
|
if (isPromise(primitiveReportedValue)) { |
|
|
handlePromise(primitiveReportedValue, sequencer, thread, opCached, lastOperation); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread.justReported = null; |
|
|
currentStackFrame.reporting = ops[i].id; |
|
|
currentStackFrame.reported = ops.slice(0, i).map(reportedCached => { |
|
|
const inputName = reportedCached._parentKey; |
|
|
const reportedValues = reportedCached._parentValues; |
|
|
|
|
|
if (inputName === 'BROADCAST_INPUT') { |
|
|
return { |
|
|
opCached: reportedCached.id, |
|
|
inputValue: reportedValues[inputName].BROADCAST_OPTION.name |
|
|
}; |
|
|
} |
|
|
return { |
|
|
opCached: reportedCached.id, |
|
|
inputValue: reportedValues[inputName] |
|
|
}; |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
} else if (thread.status === Thread.STATUS_RUNNING) { |
|
|
if (lastOperation) { |
|
|
handleReport(primitiveReportedValue, sequencer, thread, opCached, lastOperation); |
|
|
} else { |
|
|
|
|
|
|
|
|
const inputName = opCached._parentKey; |
|
|
const parentValues = opCached._parentValues; |
|
|
|
|
|
if (inputName === 'BROADCAST_INPUT') { |
|
|
|
|
|
|
|
|
parentValues.BROADCAST_OPTION.id = null; |
|
|
parentValues.BROADCAST_OPTION.name = cast.toString(primitiveReportedValue); |
|
|
} else { |
|
|
parentValues[inputName] = primitiveReportedValue; |
|
|
} |
|
|
} |
|
|
} else if (thread.status === Thread.STATUS_DONE) { |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if (runtime.profiler !== null) { |
|
|
if (blockCached._profiler !== runtime.profiler) { |
|
|
_prepareBlockProfiling(runtime.profiler, blockCached); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const end = Math.min(i + 1, length); |
|
|
for (let p = start; p < end; p++) { |
|
|
ops[p]._profilerFrame.count += 1; |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
module.exports = execute; |
|
|
|