mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-27 06:29:25 -04:00
Report block result to stackFrame.justReported
- Add stackFrame.justReported Report the block result to a known key `justReported` instead of a dynamic key on `reported`. Assuming blocks with a promised value are relatively infrequent the most common recursive input flow immediately reads the value "just" reported. In the assumed uncommon case of a promised thread status, empty the already argValues assigned values onto the currentStackFrame's reported member. In the next execute call on this stackFrame, values assigned to reported are read back off onto argValues, and execute will returned to the assumed common case. This is a safe assumption since a thread in the promise state will not exit that state until the next frame when javascript has a chance to call the resolve handle, setting the thread's state back to another executable state. Using direct assignment to `justReported` saves building an object dynamically. Instead of always building `reported` and `argValues` only `argValues` is built until a promised state is reached. This also gives a known time when `reported` is used, allowing cleanup of a stackFrame's reported to only happen when it was used to persist already reported values.
This commit is contained in:
parent
2ea4c0a21a
commit
6b7582f1c7
3 changed files with 33 additions and 13 deletions
src/engine
|
@ -204,6 +204,16 @@ const execute = function (sequencer, thread) {
|
|||
// Actually execute the block.
|
||||
execute(sequencer, thread);
|
||||
if (thread.status === Thread.STATUS_PROMISE_WAIT) {
|
||||
for (const _inputName in inputs) {
|
||||
if (_inputName === inputName) break;
|
||||
if (!inputs.hasOwnProperty(_inputName)) continue;
|
||||
if (_inputName === 'custom_block') continue;
|
||||
if (_inputName === 'BROADCAST_INPUT') {
|
||||
currentStackFrame.reported[_inputName] = argValues[_inputName].name;
|
||||
} else {
|
||||
currentStackFrame.reported[_inputName] = argValues[_inputName];
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -212,7 +222,23 @@ const execute = function (sequencer, thread) {
|
|||
currentStackFrame.waitingReporter = null;
|
||||
thread.popStack();
|
||||
}
|
||||
const inputValue = currentStackFrame.reported[inputName];
|
||||
let inputValue;
|
||||
if (
|
||||
currentStackFrame.waitingReporter === null
|
||||
) {
|
||||
inputValue = currentStackFrame.justReported;
|
||||
} else if (currentStackFrame.waitingReporter === inputName) {
|
||||
inputValue = currentStackFrame.justReported;
|
||||
currentStackFrame.waitingReporter = null;
|
||||
// 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 (e.g., on return from a branch) gets fresh
|
||||
// inputs.
|
||||
currentStackFrame.reported = {};
|
||||
} else if (typeof currentStackFrame.reported[inputName] !== 'undefined') {
|
||||
inputValue = currentStackFrame.reported[inputName];
|
||||
}
|
||||
if (inputName === 'BROADCAST_INPUT') {
|
||||
const broadcastInput = inputs[inputName];
|
||||
// Check if something is plugged into the broadcast block, or
|
||||
|
@ -244,12 +270,6 @@ const execute = function (sequencer, thread) {
|
|||
argValues.mutation = mutation;
|
||||
}
|
||||
|
||||
// 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
|
||||
// (e.g., on return from a branch) gets fresh inputs.
|
||||
currentStackFrame.reported = {};
|
||||
|
||||
let primitiveReportedValue = null;
|
||||
blockUtility.sequencer = sequencer;
|
||||
blockUtility.thread = thread;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue