@scratch/scratch-vm 11.0.0-UEPR-176
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +12 -0
- package/README.md +120 -0
- package/dist/node/extension-worker.js +2 -0
- package/dist/node/extension-worker.js.map +1 -0
- package/dist/node/scratch-vm.js +3 -0
- package/dist/node/scratch-vm.js.LICENSE.txt +205 -0
- package/dist/node/scratch-vm.js.map +1 -0
- package/dist/web/extension-worker.js +2 -0
- package/dist/web/extension-worker.js.map +1 -0
- package/dist/web/scratch-vm.js +3 -0
- package/dist/web/scratch-vm.js.LICENSE.txt +42 -0
- package/dist/web/scratch-vm.js.map +1 -0
- package/package.json +108 -0
- package/src/.eslintrc.js +7 -0
- package/src/blocks/scratch3_control.js +206 -0
- package/src/blocks/scratch3_core_example.js +69 -0
- package/src/blocks/scratch3_data.js +257 -0
- package/src/blocks/scratch3_event.js +137 -0
- package/src/blocks/scratch3_looks.js +612 -0
- package/src/blocks/scratch3_motion.js +288 -0
- package/src/blocks/scratch3_operators.js +154 -0
- package/src/blocks/scratch3_procedures.js +79 -0
- package/src/blocks/scratch3_sensing.js +336 -0
- package/src/blocks/scratch3_sound.js +350 -0
- package/src/dispatch/central-dispatch.js +143 -0
- package/src/dispatch/shared-dispatch.js +235 -0
- package/src/dispatch/worker-dispatch.js +110 -0
- package/src/engine/adapter.js +176 -0
- package/src/engine/block-utility.js +242 -0
- package/src/engine/blocks-execute-cache.js +19 -0
- package/src/engine/blocks-runtime-cache.js +78 -0
- package/src/engine/blocks.js +1299 -0
- package/src/engine/comment.js +56 -0
- package/src/engine/execute.js +580 -0
- package/src/engine/monitor-record.js +23 -0
- package/src/engine/mutation-adapter.js +48 -0
- package/src/engine/profiler.js +390 -0
- package/src/engine/runtime.js +2686 -0
- package/src/engine/scratch-blocks-constants.js +27 -0
- package/src/engine/sequencer.js +361 -0
- package/src/engine/stage-layering.js +29 -0
- package/src/engine/target.js +799 -0
- package/src/engine/thread.js +404 -0
- package/src/engine/variable.js +70 -0
- package/src/extension-support/argument-type.js +47 -0
- package/src/extension-support/block-type.js +50 -0
- package/src/extension-support/define-messages.js +18 -0
- package/src/extension-support/extension-manager.js +440 -0
- package/src/extension-support/extension-metadata.js +64 -0
- package/src/extension-support/extension-worker.js +59 -0
- package/src/extension-support/reporter-scope.js +18 -0
- package/src/extension-support/target-type.js +17 -0
- package/src/extensions/scratch3_boost/index.js +2113 -0
- package/src/extensions/scratch3_ev3/index.js +1355 -0
- package/src/extensions/scratch3_gdx_for/index.js +981 -0
- package/src/extensions/scratch3_gdx_for/scratch-link-device-adapter.js +44 -0
- package/src/extensions/scratch3_makeymakey/index.js +396 -0
- package/src/extensions/scratch3_microbit/index.js +984 -0
- package/src/extensions/scratch3_music/assets/drums/1-snare.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/10-wood-block.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/11-cowbell.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/12-triangle.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/13-bongo.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/14-conga.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/15-cabasa.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/16-guiro.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/17-vibraslap.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/18-cuica.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/2-bass-drum.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/3-side-stick.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/4-crash-cymbal.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/5-open-hi-hat.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/6-closed-hi-hat.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/7-tambourine.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/8-hand-clap.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/drums/9-claves.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/108.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/24.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/84.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/1-piano/96.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/10-clarinet/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/10-clarinet/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/11-saxophone/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/11-saxophone/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/11-saxophone/84.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/12-flute/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/12-flute/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/13-wooden-flute/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/13-wooden-flute/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/14-bassoon/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/14-bassoon/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/14-bassoon/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/15-choir/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/15-choir/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/15-choir/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/16-vibraphone/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/16-vibraphone/72.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/17-music-box/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/18-steel-drum/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/19-marimba/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/2-electric-piano/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/20-synth-lead/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/21-synth-pad/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/3-organ/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/4-guitar/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/5-electric-guitar/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/6-bass/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/6-bass/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/7-pizzicato/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/8-cello/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/8-cello/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/8-cello/60.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/9-trombone/36.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/9-trombone/48.mp3 +0 -0
- package/src/extensions/scratch3_music/assets/instruments/9-trombone/60.mp3 +0 -0
- package/src/extensions/scratch3_music/index.js +1333 -0
- package/src/extensions/scratch3_music/manifest.js +63 -0
- package/src/extensions/scratch3_pen/index.js +770 -0
- package/src/extensions/scratch3_speech2text/index.js +700 -0
- package/src/extensions/scratch3_text2speech/index.js +766 -0
- package/src/extensions/scratch3_translate/index.js +286 -0
- package/src/extensions/scratch3_video_sensing/debug.js +13 -0
- package/src/extensions/scratch3_video_sensing/index.js +594 -0
- package/src/extensions/scratch3_video_sensing/library.js +384 -0
- package/src/extensions/scratch3_video_sensing/math.js +76 -0
- package/src/extensions/scratch3_video_sensing/view.js +509 -0
- package/src/extensions/scratch3_wedo2/index.js +1616 -0
- package/src/import/load-costume.js +416 -0
- package/src/import/load-sound.js +116 -0
- package/src/index.js +10 -0
- package/src/io/ble.js +256 -0
- package/src/io/bt.js +202 -0
- package/src/io/clock.js +39 -0
- package/src/io/cloud.js +169 -0
- package/src/io/keyboard.js +146 -0
- package/src/io/mouse.js +145 -0
- package/src/io/mouseWheel.js +28 -0
- package/src/io/userData.js +24 -0
- package/src/io/video.js +210 -0
- package/src/playground/benchmark.css +83 -0
- package/src/playground/benchmark.js +753 -0
- package/src/playground/index.html +111 -0
- package/src/playground/suite.css +78 -0
- package/src/playground/suite.html +25 -0
- package/src/playground/suite.js +544 -0
- package/src/playground/video-sensing.html +18 -0
- package/src/playground/video-sensing.js +133 -0
- package/src/serialization/deserialize-assets.js +178 -0
- package/src/serialization/sb2.js +1295 -0
- package/src/serialization/sb2_specmap.js +1818 -0
- package/src/serialization/sb3.js +1320 -0
- package/src/serialization/serialize-assets.js +60 -0
- package/src/sprites/rendered-target.js +1115 -0
- package/src/sprites/sprite.js +177 -0
- package/src/util/base64-util.js +48 -0
- package/src/util/cast.js +218 -0
- package/src/util/clone.js +17 -0
- package/src/util/color.js +204 -0
- package/src/util/fetch-with-timeout.js +53 -0
- package/src/util/get-monitor-id.js +33 -0
- package/src/util/jsonrpc.js +114 -0
- package/src/util/log.js +4 -0
- package/src/util/math-util.js +122 -0
- package/src/util/maybe-format-message.js +18 -0
- package/src/util/new-block-ids.js +33 -0
- package/src/util/rateLimiter.js +73 -0
- package/src/util/scratch-link-websocket.js +133 -0
- package/src/util/string-util.js +94 -0
- package/src/util/task-queue.js +203 -0
- package/src/util/timer.js +103 -0
- package/src/util/uid.js +29 -0
- package/src/util/variable-util.js +48 -0
- package/src/util/xml-escape.js +33 -0
- package/src/virtual-machine.js +1586 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview
|
|
3
|
+
* Object representing a Scratch Comment (block or workspace).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const uid = require('../util/uid');
|
|
7
|
+
const xmlEscape = require('../util/xml-escape');
|
|
8
|
+
|
|
9
|
+
class Comment {
|
|
10
|
+
/**
|
|
11
|
+
* @param {string} id Id of the comment.
|
|
12
|
+
* @param {string} text Text content of the comment.
|
|
13
|
+
* @param {number} x X position of the comment on the workspace.
|
|
14
|
+
* @param {number} y Y position of the comment on the workspace.
|
|
15
|
+
* @param {number} width The width of the comment when it is full size.
|
|
16
|
+
* @param {number} height The height of the comment when it is full size.
|
|
17
|
+
* @param {boolean} minimized Whether the comment is minimized.
|
|
18
|
+
* @constructor
|
|
19
|
+
*/
|
|
20
|
+
constructor (id, text, x, y, width, height, minimized) {
|
|
21
|
+
this.id = id || uid();
|
|
22
|
+
this.text = text;
|
|
23
|
+
this.x = x;
|
|
24
|
+
this.y = y;
|
|
25
|
+
this.width = Math.max(Number(width), Comment.MIN_WIDTH);
|
|
26
|
+
this.height = Math.max(Number(height), Comment.MIN_HEIGHT);
|
|
27
|
+
this.minimized = minimized || false;
|
|
28
|
+
this.blockId = null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
toXML () {
|
|
32
|
+
return `<comment id="${this.id}" x="${this.x}" y="${
|
|
33
|
+
this.y}" w="${this.width}" h="${this.height}" pinned="${
|
|
34
|
+
this.blockId !== null}" minimized="${this.minimized}">${xmlEscape(this.text)}</comment>`;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// TODO choose min and defaults for width and height
|
|
38
|
+
static get MIN_WIDTH () {
|
|
39
|
+
return 20;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static get MIN_HEIGHT () {
|
|
43
|
+
return 20;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static get DEFAULT_WIDTH () {
|
|
47
|
+
return 100;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static get DEFAULT_HEIGHT () {
|
|
51
|
+
return 100;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = Comment;
|
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
const BlockUtility = require('./block-utility');
|
|
2
|
+
const BlocksExecuteCache = require('./blocks-execute-cache');
|
|
3
|
+
const log = require('../util/log');
|
|
4
|
+
const Thread = require('./thread');
|
|
5
|
+
const {Map} = require('immutable');
|
|
6
|
+
const cast = require('../util/cast');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Single BlockUtility instance reused by execute for every pritimive ran.
|
|
10
|
+
* @const
|
|
11
|
+
*/
|
|
12
|
+
const blockUtility = new BlockUtility();
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Profiler frame name for block functions.
|
|
16
|
+
* @const {string}
|
|
17
|
+
*/
|
|
18
|
+
const blockFunctionProfilerFrame = 'blockFunction';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Profiler frame ID for 'blockFunction'.
|
|
22
|
+
* @type {number}
|
|
23
|
+
*/
|
|
24
|
+
let blockFunctionProfilerId = -1;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Utility function to determine if a value is a Promise.
|
|
28
|
+
* @param {*} value Value to check for a Promise.
|
|
29
|
+
* @return {boolean} True if the value appears to be a Promise.
|
|
30
|
+
*/
|
|
31
|
+
const isPromise = function (value) {
|
|
32
|
+
return (
|
|
33
|
+
value !== null &&
|
|
34
|
+
typeof value === 'object' &&
|
|
35
|
+
typeof value.then === 'function'
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Handle any reported value from the primitive, either directly returned
|
|
41
|
+
* or after a promise resolves.
|
|
42
|
+
* @param {*} resolvedValue Value eventually returned from the primitive.
|
|
43
|
+
* @param {!Sequencer} sequencer Sequencer stepping the thread for the ran
|
|
44
|
+
* primitive.
|
|
45
|
+
* @param {!Thread} thread Thread containing the primitive.
|
|
46
|
+
* @param {!string} currentBlockId Id of the block in its thread for value from
|
|
47
|
+
* the primitive.
|
|
48
|
+
* @param {!string} opcode opcode used to identify a block function primitive.
|
|
49
|
+
* @param {!boolean} isHat Is the current block a hat?
|
|
50
|
+
*/
|
|
51
|
+
// @todo move this to callback attached to the thread when we have performance
|
|
52
|
+
// metrics (dd)
|
|
53
|
+
const handleReport = function (resolvedValue, sequencer, thread, blockCached, lastOperation) {
|
|
54
|
+
const currentBlockId = blockCached.id;
|
|
55
|
+
const opcode = blockCached.opcode;
|
|
56
|
+
const isHat = blockCached._isHat;
|
|
57
|
+
|
|
58
|
+
thread.pushReportedValue(resolvedValue);
|
|
59
|
+
if (isHat) {
|
|
60
|
+
// Hat predicate was evaluated.
|
|
61
|
+
if (sequencer.runtime.getIsEdgeActivatedHat(opcode)) {
|
|
62
|
+
// If this is an edge-activated hat, only proceed if the value is
|
|
63
|
+
// true and used to be false, or the stack was activated explicitly
|
|
64
|
+
// via stack click
|
|
65
|
+
if (!thread.stackClick) {
|
|
66
|
+
const hasOldEdgeValue = thread.target.hasEdgeActivatedValue(currentBlockId);
|
|
67
|
+
const oldEdgeValue = thread.target.updateEdgeActivatedValue(
|
|
68
|
+
currentBlockId,
|
|
69
|
+
resolvedValue
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const edgeWasActivated = hasOldEdgeValue ? (!oldEdgeValue && resolvedValue) : resolvedValue;
|
|
73
|
+
if (!edgeWasActivated) {
|
|
74
|
+
sequencer.retireThread(thread);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
} else if (!resolvedValue) {
|
|
78
|
+
// Not an edge-activated hat: retire the thread
|
|
79
|
+
// if predicate was false.
|
|
80
|
+
sequencer.retireThread(thread);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
// In a non-hat, report the value visually if necessary if
|
|
84
|
+
// at the top of the thread stack.
|
|
85
|
+
if (lastOperation && typeof resolvedValue !== 'undefined' && thread.atStackTop()) {
|
|
86
|
+
if (thread.stackClick) {
|
|
87
|
+
sequencer.runtime.visualReport(currentBlockId, resolvedValue);
|
|
88
|
+
}
|
|
89
|
+
if (thread.updateMonitor) {
|
|
90
|
+
const targetId = sequencer.runtime.monitorBlocks.getBlock(currentBlockId).targetId;
|
|
91
|
+
if (targetId && !sequencer.runtime.getTargetById(targetId)) {
|
|
92
|
+
// Target no longer exists
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
sequencer.runtime.requestUpdateMonitor(Map({
|
|
96
|
+
id: currentBlockId,
|
|
97
|
+
spriteName: targetId ? sequencer.runtime.getTargetById(targetId).getName() : null,
|
|
98
|
+
value: resolvedValue
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// Finished any yields.
|
|
103
|
+
thread.status = Thread.STATUS_RUNNING;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const handlePromise = (primitiveReportedValue, sequencer, thread, blockCached, lastOperation) => {
|
|
108
|
+
if (thread.status === Thread.STATUS_RUNNING) {
|
|
109
|
+
// Primitive returned a promise; automatically yield thread.
|
|
110
|
+
thread.status = Thread.STATUS_PROMISE_WAIT;
|
|
111
|
+
}
|
|
112
|
+
// Promise handlers
|
|
113
|
+
primitiveReportedValue.then(resolvedValue => {
|
|
114
|
+
handleReport(resolvedValue, sequencer, thread, blockCached, lastOperation);
|
|
115
|
+
// If it's a command block or a top level reporter in a stackClick.
|
|
116
|
+
if (lastOperation) {
|
|
117
|
+
let stackFrame;
|
|
118
|
+
let nextBlockId;
|
|
119
|
+
do {
|
|
120
|
+
// In the case that the promise is the last block in the current thread stack
|
|
121
|
+
// We need to pop out repeatedly until we find the next block.
|
|
122
|
+
const popped = thread.popStack();
|
|
123
|
+
if (popped === null) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
nextBlockId = thread.target.blocks.getNextBlock(popped);
|
|
127
|
+
if (nextBlockId !== null) {
|
|
128
|
+
// A next block exists so break out this loop
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
// Investigate the next block and if not in a loop,
|
|
132
|
+
// then repeat and pop the next item off the stack frame
|
|
133
|
+
stackFrame = thread.peekStackFrame();
|
|
134
|
+
} while (stackFrame !== null && !stackFrame.isLoop);
|
|
135
|
+
|
|
136
|
+
thread.pushStack(nextBlockId);
|
|
137
|
+
}
|
|
138
|
+
}, rejectionReason => {
|
|
139
|
+
// Promise rejected: the primitive had some error.
|
|
140
|
+
// Log it and proceed.
|
|
141
|
+
log.warn('Primitive rejected promise: ', rejectionReason);
|
|
142
|
+
thread.status = Thread.STATUS_RUNNING;
|
|
143
|
+
thread.popStack();
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A execute.js internal representation of a block to reduce the time spent in
|
|
149
|
+
* execute as the same blocks are called the most.
|
|
150
|
+
*
|
|
151
|
+
* With the help of the Blocks class create a mutable copy of block
|
|
152
|
+
* information. The members of BlockCached derived values of block information
|
|
153
|
+
* that does not need to be reevaluated until a change in Blocks. Since Blocks
|
|
154
|
+
* handles where the cache instance is stored, it drops all cache versions of a
|
|
155
|
+
* block when any change happens to it. This way we can quickly execute blocks
|
|
156
|
+
* and keep perform the right action according to the current block information
|
|
157
|
+
* in the editor.
|
|
158
|
+
*
|
|
159
|
+
* @param {Blocks} blockContainer the related Blocks instance
|
|
160
|
+
* @param {object} cached default set of cached values
|
|
161
|
+
*/
|
|
162
|
+
class BlockCached {
|
|
163
|
+
constructor (blockContainer, cached) {
|
|
164
|
+
/**
|
|
165
|
+
* Block id in its parent set of blocks.
|
|
166
|
+
* @type {string}
|
|
167
|
+
*/
|
|
168
|
+
this.id = cached.id;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Block operation code for this block.
|
|
172
|
+
* @type {string}
|
|
173
|
+
*/
|
|
174
|
+
this.opcode = cached.opcode;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Original block object containing argument values for static fields.
|
|
178
|
+
* @type {object}
|
|
179
|
+
*/
|
|
180
|
+
this.fields = cached.fields;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Original block object containing argument values for executable inputs.
|
|
184
|
+
* @type {object}
|
|
185
|
+
*/
|
|
186
|
+
this.inputs = cached.inputs;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Procedure mutation.
|
|
190
|
+
* @type {?object}
|
|
191
|
+
*/
|
|
192
|
+
this.mutation = cached.mutation;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* The profiler the block is configured with.
|
|
196
|
+
* @type {?Profiler}
|
|
197
|
+
*/
|
|
198
|
+
this._profiler = null;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Profiler information frame.
|
|
202
|
+
* @type {?ProfilerFrame}
|
|
203
|
+
*/
|
|
204
|
+
this._profilerFrame = null;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Is the opcode a hat (event responder) block.
|
|
208
|
+
* @type {boolean}
|
|
209
|
+
*/
|
|
210
|
+
this._isHat = false;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* The block opcode's implementation function.
|
|
214
|
+
* @type {?function}
|
|
215
|
+
*/
|
|
216
|
+
this._blockFunction = null;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Is the block function defined for this opcode?
|
|
220
|
+
* @type {boolean}
|
|
221
|
+
*/
|
|
222
|
+
this._definedBlockFunction = false;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Is this block a block with no function but a static value to return.
|
|
226
|
+
* @type {boolean}
|
|
227
|
+
*/
|
|
228
|
+
this._isShadowBlock = false;
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The static value of this block if it is a shadow block.
|
|
232
|
+
* @type {?any}
|
|
233
|
+
*/
|
|
234
|
+
this._shadowValue = null;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* A copy of the block's fields that may be modified.
|
|
238
|
+
* @type {object}
|
|
239
|
+
*/
|
|
240
|
+
this._fields = Object.assign({}, this.fields);
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* A copy of the block's inputs that may be modified.
|
|
244
|
+
* @type {object}
|
|
245
|
+
*/
|
|
246
|
+
this._inputs = Object.assign({}, this.inputs);
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* An arguments object for block implementations. All executions of this
|
|
250
|
+
* specific block will use this objecct.
|
|
251
|
+
* @type {object}
|
|
252
|
+
*/
|
|
253
|
+
this._argValues = {
|
|
254
|
+
mutation: this.mutation
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* The inputs key the parent refers to this BlockCached by.
|
|
259
|
+
* @type {string}
|
|
260
|
+
*/
|
|
261
|
+
this._parentKey = null;
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* The target object where the parent wants the resulting value stored
|
|
265
|
+
* with _parentKey as the key.
|
|
266
|
+
* @type {object}
|
|
267
|
+
*/
|
|
268
|
+
this._parentValues = null;
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* A sequence of non-shadow operations that can must be performed. This
|
|
272
|
+
* list recreates the order this block and its children are executed.
|
|
273
|
+
* Since the order is always the same we can safely store that order
|
|
274
|
+
* and iterate over the operations instead of dynamically walking the
|
|
275
|
+
* tree every time.
|
|
276
|
+
* @type {Array<BlockCached>}
|
|
277
|
+
*/
|
|
278
|
+
this._ops = [];
|
|
279
|
+
|
|
280
|
+
const {runtime} = blockUtility.sequencer;
|
|
281
|
+
|
|
282
|
+
const {opcode, fields, inputs} = this;
|
|
283
|
+
|
|
284
|
+
// Assign opcode isHat and blockFunction data to avoid dynamic lookups.
|
|
285
|
+
this._isHat = runtime.getIsHat(opcode);
|
|
286
|
+
this._blockFunction = runtime.getOpcodeFunction(opcode);
|
|
287
|
+
this._definedBlockFunction = typeof this._blockFunction !== 'undefined';
|
|
288
|
+
|
|
289
|
+
// Store the current shadow value if there is a shadow value.
|
|
290
|
+
const fieldKeys = Object.keys(fields);
|
|
291
|
+
this._isShadowBlock = (
|
|
292
|
+
!this._definedBlockFunction &&
|
|
293
|
+
fieldKeys.length === 1 &&
|
|
294
|
+
Object.keys(inputs).length === 0
|
|
295
|
+
);
|
|
296
|
+
this._shadowValue = this._isShadowBlock && fields[fieldKeys[0]].value;
|
|
297
|
+
|
|
298
|
+
// Store the static fields onto _argValues.
|
|
299
|
+
for (const fieldName in fields) {
|
|
300
|
+
if (
|
|
301
|
+
fieldName === 'VARIABLE' ||
|
|
302
|
+
fieldName === 'LIST' ||
|
|
303
|
+
fieldName === 'BROADCAST_OPTION'
|
|
304
|
+
) {
|
|
305
|
+
this._argValues[fieldName] = {
|
|
306
|
+
id: fields[fieldName].id,
|
|
307
|
+
name: fields[fieldName].value
|
|
308
|
+
};
|
|
309
|
+
} else {
|
|
310
|
+
this._argValues[fieldName] = fields[fieldName].value;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Remove custom_block. It is not part of block execution.
|
|
315
|
+
delete this._inputs.custom_block;
|
|
316
|
+
|
|
317
|
+
if ('BROADCAST_INPUT' in this._inputs) {
|
|
318
|
+
// BROADCAST_INPUT is called BROADCAST_OPTION in the args and is an
|
|
319
|
+
// object with an unchanging shape.
|
|
320
|
+
this._argValues.BROADCAST_OPTION = {
|
|
321
|
+
id: null,
|
|
322
|
+
name: null
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// We can go ahead and compute BROADCAST_INPUT if it is a shadow
|
|
326
|
+
// value.
|
|
327
|
+
const broadcastInput = this._inputs.BROADCAST_INPUT;
|
|
328
|
+
if (broadcastInput.block === broadcastInput.shadow) {
|
|
329
|
+
// Shadow dropdown menu is being used.
|
|
330
|
+
// Get the appropriate information out of it.
|
|
331
|
+
const shadow = blockContainer.getBlock(broadcastInput.shadow);
|
|
332
|
+
const broadcastField = shadow.fields.BROADCAST_OPTION;
|
|
333
|
+
this._argValues.BROADCAST_OPTION.id = broadcastField.id;
|
|
334
|
+
this._argValues.BROADCAST_OPTION.name = broadcastField.value;
|
|
335
|
+
|
|
336
|
+
// Evaluating BROADCAST_INPUT here we do not need to do so
|
|
337
|
+
// later.
|
|
338
|
+
delete this._inputs.BROADCAST_INPUT;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Cache all input children blocks in the operation lists. The
|
|
343
|
+
// operations can later be run in the order they appear in correctly
|
|
344
|
+
// executing the operations quickly in a flat loop instead of needing to
|
|
345
|
+
// recursivly iterate them.
|
|
346
|
+
for (const inputName in this._inputs) {
|
|
347
|
+
const input = this._inputs[inputName];
|
|
348
|
+
if (input.block) {
|
|
349
|
+
const inputCached = BlocksExecuteCache.getCached(blockContainer, input.block, BlockCached);
|
|
350
|
+
|
|
351
|
+
if (inputCached._isHat) {
|
|
352
|
+
continue;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
this._ops.push(...inputCached._ops);
|
|
356
|
+
inputCached._parentKey = inputName;
|
|
357
|
+
inputCached._parentValues = this._argValues;
|
|
358
|
+
|
|
359
|
+
// Shadow values are static and do not change, go ahead and
|
|
360
|
+
// store their value on args.
|
|
361
|
+
if (inputCached._isShadowBlock) {
|
|
362
|
+
this._argValues[inputName] = inputCached._shadowValue;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// The final operation is this block itself. At the top most block is a
|
|
368
|
+
// command block or a block that is being run as a monitor.
|
|
369
|
+
if (this._definedBlockFunction) {
|
|
370
|
+
this._ops.push(this);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Initialize a BlockCached instance so its command/hat
|
|
377
|
+
* block and reporters can be profiled during execution.
|
|
378
|
+
* @param {Profiler} profiler - The profiler that is currently enabled.
|
|
379
|
+
* @param {BlockCached} blockCached - The blockCached instance to profile.
|
|
380
|
+
*/
|
|
381
|
+
const _prepareBlockProfiling = function (profiler, blockCached) {
|
|
382
|
+
blockCached._profiler = profiler;
|
|
383
|
+
|
|
384
|
+
if (blockFunctionProfilerId === -1) {
|
|
385
|
+
blockFunctionProfilerId = profiler.idByName(blockFunctionProfilerFrame);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const ops = blockCached._ops;
|
|
389
|
+
for (let i = 0; i < ops.length; i++) {
|
|
390
|
+
ops[i]._profilerFrame = profiler.frame(blockFunctionProfilerId, ops[i].opcode);
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Execute a block.
|
|
396
|
+
* @param {!Sequencer} sequencer Which sequencer is executing.
|
|
397
|
+
* @param {!Thread} thread Thread which to read and execute.
|
|
398
|
+
*/
|
|
399
|
+
const execute = function (sequencer, thread) {
|
|
400
|
+
const runtime = sequencer.runtime;
|
|
401
|
+
|
|
402
|
+
// store sequencer and thread so block functions can access them through
|
|
403
|
+
// convenience methods.
|
|
404
|
+
blockUtility.sequencer = sequencer;
|
|
405
|
+
blockUtility.thread = thread;
|
|
406
|
+
|
|
407
|
+
// Current block to execute is the one on the top of the stack.
|
|
408
|
+
const currentBlockId = thread.peekStack();
|
|
409
|
+
const currentStackFrame = thread.peekStackFrame();
|
|
410
|
+
|
|
411
|
+
let blockContainer = thread.blockContainer;
|
|
412
|
+
let blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached);
|
|
413
|
+
if (blockCached === null) {
|
|
414
|
+
blockContainer = runtime.flyoutBlocks;
|
|
415
|
+
blockCached = BlocksExecuteCache.getCached(blockContainer, currentBlockId, BlockCached);
|
|
416
|
+
// Stop if block or target no longer exists.
|
|
417
|
+
if (blockCached === null) {
|
|
418
|
+
// No block found: stop the thread; script no longer exists.
|
|
419
|
+
sequencer.retireThread(thread);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
const ops = blockCached._ops;
|
|
425
|
+
const length = ops.length;
|
|
426
|
+
let i = 0;
|
|
427
|
+
|
|
428
|
+
if (currentStackFrame.reported !== null) {
|
|
429
|
+
const reported = currentStackFrame.reported;
|
|
430
|
+
// Reinstate all the previous values.
|
|
431
|
+
for (; i < reported.length; i++) {
|
|
432
|
+
const {opCached: oldOpCached, inputValue} = reported[i];
|
|
433
|
+
|
|
434
|
+
const opCached = ops.find(op => op.id === oldOpCached);
|
|
435
|
+
|
|
436
|
+
if (opCached) {
|
|
437
|
+
const inputName = opCached._parentKey;
|
|
438
|
+
const argValues = opCached._parentValues;
|
|
439
|
+
|
|
440
|
+
if (inputName === 'BROADCAST_INPUT') {
|
|
441
|
+
// Something is plugged into the broadcast input.
|
|
442
|
+
// Cast it to a string. We don't need an id here.
|
|
443
|
+
argValues.BROADCAST_OPTION.id = null;
|
|
444
|
+
argValues.BROADCAST_OPTION.name = cast.toString(inputValue);
|
|
445
|
+
} else {
|
|
446
|
+
argValues[inputName] = inputValue;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Find the last reported block that is still in the set of operations.
|
|
452
|
+
// This way if the last operation was removed, we'll find the next
|
|
453
|
+
// candidate. If an earlier block that was performed was removed then
|
|
454
|
+
// we'll find the index where the last operation is now.
|
|
455
|
+
if (reported.length > 0) {
|
|
456
|
+
const lastExisting = reported.reverse().find(report => ops.find(op => op.id === report.opCached));
|
|
457
|
+
if (lastExisting) {
|
|
458
|
+
i = ops.findIndex(opCached => opCached.id === lastExisting.opCached) + 1;
|
|
459
|
+
} else {
|
|
460
|
+
i = 0;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// The reporting block must exist and must be the next one in the sequence of operations.
|
|
465
|
+
if (thread.justReported !== null && ops[i] && ops[i].id === currentStackFrame.reporting) {
|
|
466
|
+
const opCached = ops[i];
|
|
467
|
+
const inputValue = thread.justReported;
|
|
468
|
+
|
|
469
|
+
thread.justReported = null;
|
|
470
|
+
|
|
471
|
+
const inputName = opCached._parentKey;
|
|
472
|
+
const argValues = opCached._parentValues;
|
|
473
|
+
|
|
474
|
+
if (inputName === 'BROADCAST_INPUT') {
|
|
475
|
+
// Something is plugged into the broadcast input.
|
|
476
|
+
// Cast it to a string. We don't need an id here.
|
|
477
|
+
argValues.BROADCAST_OPTION.id = null;
|
|
478
|
+
argValues.BROADCAST_OPTION.name = cast.toString(inputValue);
|
|
479
|
+
} else {
|
|
480
|
+
argValues[inputName] = inputValue;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
i += 1;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
currentStackFrame.reporting = null;
|
|
487
|
+
currentStackFrame.reported = null;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const start = i;
|
|
491
|
+
|
|
492
|
+
for (; i < length; i++) {
|
|
493
|
+
const lastOperation = i === length - 1;
|
|
494
|
+
const opCached = ops[i];
|
|
495
|
+
|
|
496
|
+
const blockFunction = opCached._blockFunction;
|
|
497
|
+
|
|
498
|
+
// Update values for arguments (inputs).
|
|
499
|
+
const argValues = opCached._argValues;
|
|
500
|
+
|
|
501
|
+
// Fields are set during opCached initialization.
|
|
502
|
+
|
|
503
|
+
// Blocks should glow when a script is starting,
|
|
504
|
+
// not after it has finished (see #1404).
|
|
505
|
+
// Only blocks in blockContainers that don't forceNoGlow
|
|
506
|
+
// should request a glow.
|
|
507
|
+
if (!blockContainer.forceNoGlow) {
|
|
508
|
+
thread.requestScriptGlowInFrame = true;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
// Inputs are set during previous steps in the loop.
|
|
512
|
+
|
|
513
|
+
const primitiveReportedValue = blockFunction(argValues, blockUtility);
|
|
514
|
+
|
|
515
|
+
// If it's a promise, wait until promise resolves.
|
|
516
|
+
if (isPromise(primitiveReportedValue)) {
|
|
517
|
+
handlePromise(primitiveReportedValue, sequencer, thread, opCached, lastOperation);
|
|
518
|
+
|
|
519
|
+
// Store the already reported values. They will be thawed into the
|
|
520
|
+
// future versions of the same operations by block id. The reporting
|
|
521
|
+
// operation if it is promise waiting will set its parent value at
|
|
522
|
+
// that time.
|
|
523
|
+
thread.justReported = null;
|
|
524
|
+
currentStackFrame.reporting = ops[i].id;
|
|
525
|
+
currentStackFrame.reported = ops.slice(0, i).map(reportedCached => {
|
|
526
|
+
const inputName = reportedCached._parentKey;
|
|
527
|
+
const reportedValues = reportedCached._parentValues;
|
|
528
|
+
|
|
529
|
+
if (inputName === 'BROADCAST_INPUT') {
|
|
530
|
+
return {
|
|
531
|
+
opCached: reportedCached.id,
|
|
532
|
+
inputValue: reportedValues[inputName].BROADCAST_OPTION.name
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
return {
|
|
536
|
+
opCached: reportedCached.id,
|
|
537
|
+
inputValue: reportedValues[inputName]
|
|
538
|
+
};
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
// We are waiting for a promise. Stop running this set of operations
|
|
542
|
+
// and continue them later after thawing the reported values.
|
|
543
|
+
break;
|
|
544
|
+
} else if (thread.status === Thread.STATUS_RUNNING) {
|
|
545
|
+
if (lastOperation) {
|
|
546
|
+
handleReport(primitiveReportedValue, sequencer, thread, opCached, lastOperation);
|
|
547
|
+
} else {
|
|
548
|
+
// By definition a block that is not last in the list has a
|
|
549
|
+
// parent.
|
|
550
|
+
const inputName = opCached._parentKey;
|
|
551
|
+
const parentValues = opCached._parentValues;
|
|
552
|
+
|
|
553
|
+
if (inputName === 'BROADCAST_INPUT') {
|
|
554
|
+
// Something is plugged into the broadcast input.
|
|
555
|
+
// Cast it to a string. We don't need an id here.
|
|
556
|
+
parentValues.BROADCAST_OPTION.id = null;
|
|
557
|
+
parentValues.BROADCAST_OPTION.name = cast.toString(primitiveReportedValue);
|
|
558
|
+
} else {
|
|
559
|
+
parentValues[inputName] = primitiveReportedValue;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
if (runtime.profiler !== null) {
|
|
566
|
+
if (blockCached._profiler !== runtime.profiler) {
|
|
567
|
+
_prepareBlockProfiling(runtime.profiler, blockCached);
|
|
568
|
+
}
|
|
569
|
+
// Determine the index that is after the last executed block. `i` is
|
|
570
|
+
// currently the block that was just executed. `i + 1` will be the block
|
|
571
|
+
// after that. `length` with the min call makes sure we don't try to
|
|
572
|
+
// reference an operation outside of the set of operations.
|
|
573
|
+
const end = Math.min(i + 1, length);
|
|
574
|
+
for (let p = start; p < end; p++) {
|
|
575
|
+
ops[p]._profilerFrame.count += 1;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
|
|
580
|
+
module.exports = execute;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const {Record} = require('immutable');
|
|
2
|
+
|
|
3
|
+
const MonitorRecord = Record({
|
|
4
|
+
id: null, // Block Id
|
|
5
|
+
/** Present only if the monitor is sprite-specific, such as x position */
|
|
6
|
+
spriteName: null,
|
|
7
|
+
/** Present only if the monitor is sprite-specific, such as x position */
|
|
8
|
+
targetId: null,
|
|
9
|
+
opcode: null,
|
|
10
|
+
value: null,
|
|
11
|
+
params: null,
|
|
12
|
+
mode: 'default',
|
|
13
|
+
sliderMin: 0,
|
|
14
|
+
sliderMax: 100,
|
|
15
|
+
isDiscrete: true,
|
|
16
|
+
x: null, // (x: null, y: null) Indicates that the monitor should be auto-positioned
|
|
17
|
+
y: null,
|
|
18
|
+
width: 0,
|
|
19
|
+
height: 0,
|
|
20
|
+
visible: true
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
module.exports = MonitorRecord;
|