@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.
Files changed (178) hide show
  1. package/LICENSE +12 -0
  2. package/README.md +120 -0
  3. package/dist/node/extension-worker.js +2 -0
  4. package/dist/node/extension-worker.js.map +1 -0
  5. package/dist/node/scratch-vm.js +3 -0
  6. package/dist/node/scratch-vm.js.LICENSE.txt +205 -0
  7. package/dist/node/scratch-vm.js.map +1 -0
  8. package/dist/web/extension-worker.js +2 -0
  9. package/dist/web/extension-worker.js.map +1 -0
  10. package/dist/web/scratch-vm.js +3 -0
  11. package/dist/web/scratch-vm.js.LICENSE.txt +42 -0
  12. package/dist/web/scratch-vm.js.map +1 -0
  13. package/package.json +108 -0
  14. package/src/.eslintrc.js +7 -0
  15. package/src/blocks/scratch3_control.js +206 -0
  16. package/src/blocks/scratch3_core_example.js +69 -0
  17. package/src/blocks/scratch3_data.js +257 -0
  18. package/src/blocks/scratch3_event.js +137 -0
  19. package/src/blocks/scratch3_looks.js +612 -0
  20. package/src/blocks/scratch3_motion.js +288 -0
  21. package/src/blocks/scratch3_operators.js +154 -0
  22. package/src/blocks/scratch3_procedures.js +79 -0
  23. package/src/blocks/scratch3_sensing.js +336 -0
  24. package/src/blocks/scratch3_sound.js +350 -0
  25. package/src/dispatch/central-dispatch.js +143 -0
  26. package/src/dispatch/shared-dispatch.js +235 -0
  27. package/src/dispatch/worker-dispatch.js +110 -0
  28. package/src/engine/adapter.js +176 -0
  29. package/src/engine/block-utility.js +242 -0
  30. package/src/engine/blocks-execute-cache.js +19 -0
  31. package/src/engine/blocks-runtime-cache.js +78 -0
  32. package/src/engine/blocks.js +1299 -0
  33. package/src/engine/comment.js +56 -0
  34. package/src/engine/execute.js +580 -0
  35. package/src/engine/monitor-record.js +23 -0
  36. package/src/engine/mutation-adapter.js +48 -0
  37. package/src/engine/profiler.js +390 -0
  38. package/src/engine/runtime.js +2686 -0
  39. package/src/engine/scratch-blocks-constants.js +27 -0
  40. package/src/engine/sequencer.js +361 -0
  41. package/src/engine/stage-layering.js +29 -0
  42. package/src/engine/target.js +799 -0
  43. package/src/engine/thread.js +404 -0
  44. package/src/engine/variable.js +70 -0
  45. package/src/extension-support/argument-type.js +47 -0
  46. package/src/extension-support/block-type.js +50 -0
  47. package/src/extension-support/define-messages.js +18 -0
  48. package/src/extension-support/extension-manager.js +440 -0
  49. package/src/extension-support/extension-metadata.js +64 -0
  50. package/src/extension-support/extension-worker.js +59 -0
  51. package/src/extension-support/reporter-scope.js +18 -0
  52. package/src/extension-support/target-type.js +17 -0
  53. package/src/extensions/scratch3_boost/index.js +2113 -0
  54. package/src/extensions/scratch3_ev3/index.js +1355 -0
  55. package/src/extensions/scratch3_gdx_for/index.js +981 -0
  56. package/src/extensions/scratch3_gdx_for/scratch-link-device-adapter.js +44 -0
  57. package/src/extensions/scratch3_makeymakey/index.js +396 -0
  58. package/src/extensions/scratch3_microbit/index.js +984 -0
  59. package/src/extensions/scratch3_music/assets/drums/1-snare.mp3 +0 -0
  60. package/src/extensions/scratch3_music/assets/drums/10-wood-block.mp3 +0 -0
  61. package/src/extensions/scratch3_music/assets/drums/11-cowbell.mp3 +0 -0
  62. package/src/extensions/scratch3_music/assets/drums/12-triangle.mp3 +0 -0
  63. package/src/extensions/scratch3_music/assets/drums/13-bongo.mp3 +0 -0
  64. package/src/extensions/scratch3_music/assets/drums/14-conga.mp3 +0 -0
  65. package/src/extensions/scratch3_music/assets/drums/15-cabasa.mp3 +0 -0
  66. package/src/extensions/scratch3_music/assets/drums/16-guiro.mp3 +0 -0
  67. package/src/extensions/scratch3_music/assets/drums/17-vibraslap.mp3 +0 -0
  68. package/src/extensions/scratch3_music/assets/drums/18-cuica.mp3 +0 -0
  69. package/src/extensions/scratch3_music/assets/drums/2-bass-drum.mp3 +0 -0
  70. package/src/extensions/scratch3_music/assets/drums/3-side-stick.mp3 +0 -0
  71. package/src/extensions/scratch3_music/assets/drums/4-crash-cymbal.mp3 +0 -0
  72. package/src/extensions/scratch3_music/assets/drums/5-open-hi-hat.mp3 +0 -0
  73. package/src/extensions/scratch3_music/assets/drums/6-closed-hi-hat.mp3 +0 -0
  74. package/src/extensions/scratch3_music/assets/drums/7-tambourine.mp3 +0 -0
  75. package/src/extensions/scratch3_music/assets/drums/8-hand-clap.mp3 +0 -0
  76. package/src/extensions/scratch3_music/assets/drums/9-claves.mp3 +0 -0
  77. package/src/extensions/scratch3_music/assets/instruments/1-piano/108.mp3 +0 -0
  78. package/src/extensions/scratch3_music/assets/instruments/1-piano/24.mp3 +0 -0
  79. package/src/extensions/scratch3_music/assets/instruments/1-piano/36.mp3 +0 -0
  80. package/src/extensions/scratch3_music/assets/instruments/1-piano/48.mp3 +0 -0
  81. package/src/extensions/scratch3_music/assets/instruments/1-piano/60.mp3 +0 -0
  82. package/src/extensions/scratch3_music/assets/instruments/1-piano/72.mp3 +0 -0
  83. package/src/extensions/scratch3_music/assets/instruments/1-piano/84.mp3 +0 -0
  84. package/src/extensions/scratch3_music/assets/instruments/1-piano/96.mp3 +0 -0
  85. package/src/extensions/scratch3_music/assets/instruments/10-clarinet/48.mp3 +0 -0
  86. package/src/extensions/scratch3_music/assets/instruments/10-clarinet/60.mp3 +0 -0
  87. package/src/extensions/scratch3_music/assets/instruments/11-saxophone/36.mp3 +0 -0
  88. package/src/extensions/scratch3_music/assets/instruments/11-saxophone/60.mp3 +0 -0
  89. package/src/extensions/scratch3_music/assets/instruments/11-saxophone/84.mp3 +0 -0
  90. package/src/extensions/scratch3_music/assets/instruments/12-flute/60.mp3 +0 -0
  91. package/src/extensions/scratch3_music/assets/instruments/12-flute/72.mp3 +0 -0
  92. package/src/extensions/scratch3_music/assets/instruments/13-wooden-flute/60.mp3 +0 -0
  93. package/src/extensions/scratch3_music/assets/instruments/13-wooden-flute/72.mp3 +0 -0
  94. package/src/extensions/scratch3_music/assets/instruments/14-bassoon/36.mp3 +0 -0
  95. package/src/extensions/scratch3_music/assets/instruments/14-bassoon/48.mp3 +0 -0
  96. package/src/extensions/scratch3_music/assets/instruments/14-bassoon/60.mp3 +0 -0
  97. package/src/extensions/scratch3_music/assets/instruments/15-choir/48.mp3 +0 -0
  98. package/src/extensions/scratch3_music/assets/instruments/15-choir/60.mp3 +0 -0
  99. package/src/extensions/scratch3_music/assets/instruments/15-choir/72.mp3 +0 -0
  100. package/src/extensions/scratch3_music/assets/instruments/16-vibraphone/60.mp3 +0 -0
  101. package/src/extensions/scratch3_music/assets/instruments/16-vibraphone/72.mp3 +0 -0
  102. package/src/extensions/scratch3_music/assets/instruments/17-music-box/60.mp3 +0 -0
  103. package/src/extensions/scratch3_music/assets/instruments/18-steel-drum/60.mp3 +0 -0
  104. package/src/extensions/scratch3_music/assets/instruments/19-marimba/60.mp3 +0 -0
  105. package/src/extensions/scratch3_music/assets/instruments/2-electric-piano/60.mp3 +0 -0
  106. package/src/extensions/scratch3_music/assets/instruments/20-synth-lead/60.mp3 +0 -0
  107. package/src/extensions/scratch3_music/assets/instruments/21-synth-pad/60.mp3 +0 -0
  108. package/src/extensions/scratch3_music/assets/instruments/3-organ/60.mp3 +0 -0
  109. package/src/extensions/scratch3_music/assets/instruments/4-guitar/60.mp3 +0 -0
  110. package/src/extensions/scratch3_music/assets/instruments/5-electric-guitar/60.mp3 +0 -0
  111. package/src/extensions/scratch3_music/assets/instruments/6-bass/36.mp3 +0 -0
  112. package/src/extensions/scratch3_music/assets/instruments/6-bass/48.mp3 +0 -0
  113. package/src/extensions/scratch3_music/assets/instruments/7-pizzicato/60.mp3 +0 -0
  114. package/src/extensions/scratch3_music/assets/instruments/8-cello/36.mp3 +0 -0
  115. package/src/extensions/scratch3_music/assets/instruments/8-cello/48.mp3 +0 -0
  116. package/src/extensions/scratch3_music/assets/instruments/8-cello/60.mp3 +0 -0
  117. package/src/extensions/scratch3_music/assets/instruments/9-trombone/36.mp3 +0 -0
  118. package/src/extensions/scratch3_music/assets/instruments/9-trombone/48.mp3 +0 -0
  119. package/src/extensions/scratch3_music/assets/instruments/9-trombone/60.mp3 +0 -0
  120. package/src/extensions/scratch3_music/index.js +1333 -0
  121. package/src/extensions/scratch3_music/manifest.js +63 -0
  122. package/src/extensions/scratch3_pen/index.js +770 -0
  123. package/src/extensions/scratch3_speech2text/index.js +700 -0
  124. package/src/extensions/scratch3_text2speech/index.js +766 -0
  125. package/src/extensions/scratch3_translate/index.js +286 -0
  126. package/src/extensions/scratch3_video_sensing/debug.js +13 -0
  127. package/src/extensions/scratch3_video_sensing/index.js +594 -0
  128. package/src/extensions/scratch3_video_sensing/library.js +384 -0
  129. package/src/extensions/scratch3_video_sensing/math.js +76 -0
  130. package/src/extensions/scratch3_video_sensing/view.js +509 -0
  131. package/src/extensions/scratch3_wedo2/index.js +1616 -0
  132. package/src/import/load-costume.js +416 -0
  133. package/src/import/load-sound.js +116 -0
  134. package/src/index.js +10 -0
  135. package/src/io/ble.js +256 -0
  136. package/src/io/bt.js +202 -0
  137. package/src/io/clock.js +39 -0
  138. package/src/io/cloud.js +169 -0
  139. package/src/io/keyboard.js +146 -0
  140. package/src/io/mouse.js +145 -0
  141. package/src/io/mouseWheel.js +28 -0
  142. package/src/io/userData.js +24 -0
  143. package/src/io/video.js +210 -0
  144. package/src/playground/benchmark.css +83 -0
  145. package/src/playground/benchmark.js +753 -0
  146. package/src/playground/index.html +111 -0
  147. package/src/playground/suite.css +78 -0
  148. package/src/playground/suite.html +25 -0
  149. package/src/playground/suite.js +544 -0
  150. package/src/playground/video-sensing.html +18 -0
  151. package/src/playground/video-sensing.js +133 -0
  152. package/src/serialization/deserialize-assets.js +178 -0
  153. package/src/serialization/sb2.js +1295 -0
  154. package/src/serialization/sb2_specmap.js +1818 -0
  155. package/src/serialization/sb3.js +1320 -0
  156. package/src/serialization/serialize-assets.js +60 -0
  157. package/src/sprites/rendered-target.js +1115 -0
  158. package/src/sprites/sprite.js +177 -0
  159. package/src/util/base64-util.js +48 -0
  160. package/src/util/cast.js +218 -0
  161. package/src/util/clone.js +17 -0
  162. package/src/util/color.js +204 -0
  163. package/src/util/fetch-with-timeout.js +53 -0
  164. package/src/util/get-monitor-id.js +33 -0
  165. package/src/util/jsonrpc.js +114 -0
  166. package/src/util/log.js +4 -0
  167. package/src/util/math-util.js +122 -0
  168. package/src/util/maybe-format-message.js +18 -0
  169. package/src/util/new-block-ids.js +33 -0
  170. package/src/util/rateLimiter.js +73 -0
  171. package/src/util/scratch-link-websocket.js +133 -0
  172. package/src/util/string-util.js +94 -0
  173. package/src/util/task-queue.js +203 -0
  174. package/src/util/timer.js +103 -0
  175. package/src/util/uid.js +29 -0
  176. package/src/util/variable-util.js +48 -0
  177. package/src/util/xml-escape.js +33 -0
  178. package/src/virtual-machine.js +1586 -0
@@ -0,0 +1,404 @@
1
+ /**
2
+ * Recycle bin for empty stackFrame objects
3
+ * @type Array<_StackFrame>
4
+ */
5
+ const _stackFrameFreeList = [];
6
+
7
+ /**
8
+ * A frame used for each level of the stack. A general purpose
9
+ * place to store a bunch of execution context and parameters
10
+ * @param {boolean} warpMode Whether this level of the stack is warping
11
+ * @constructor
12
+ * @private
13
+ */
14
+ class _StackFrame {
15
+ constructor (warpMode) {
16
+ /**
17
+ * Whether this level of the stack is a loop.
18
+ * @type {boolean}
19
+ */
20
+ this.isLoop = false;
21
+
22
+ /**
23
+ * Whether this level is in warp mode. Is set by some legacy blocks and
24
+ * "turbo mode"
25
+ * @type {boolean}
26
+ */
27
+ this.warpMode = warpMode;
28
+
29
+ /**
30
+ * Reported value from just executed block.
31
+ * @type {Any}
32
+ */
33
+ this.justReported = null;
34
+
35
+ /**
36
+ * The active block that is waiting on a promise.
37
+ * @type {string}
38
+ */
39
+ this.reporting = '';
40
+
41
+ /**
42
+ * Persists reported inputs during async block.
43
+ * @type {Object}
44
+ */
45
+ this.reported = null;
46
+
47
+ /**
48
+ * Name of waiting reporter.
49
+ * @type {string}
50
+ */
51
+ this.waitingReporter = null;
52
+
53
+ /**
54
+ * Procedure parameters.
55
+ * @type {Object}
56
+ */
57
+ this.params = null;
58
+
59
+ /**
60
+ * A context passed to block implementations.
61
+ * @type {Object}
62
+ */
63
+ this.executionContext = null;
64
+ }
65
+
66
+ /**
67
+ * Reset all properties of the frame to pristine null and false states.
68
+ * Used to recycle.
69
+ * @return {_StackFrame} this
70
+ */
71
+ reset () {
72
+
73
+ this.isLoop = false;
74
+ this.warpMode = false;
75
+ this.justReported = null;
76
+ this.reported = null;
77
+ this.waitingReporter = null;
78
+ this.params = null;
79
+ this.executionContext = null;
80
+
81
+ return this;
82
+ }
83
+
84
+ /**
85
+ * Reuse an active stack frame in the stack.
86
+ * @param {?boolean} warpMode defaults to current warpMode
87
+ * @returns {_StackFrame} this
88
+ */
89
+ reuse (warpMode = this.warpMode) {
90
+ this.reset();
91
+ this.warpMode = Boolean(warpMode);
92
+ return this;
93
+ }
94
+
95
+ /**
96
+ * Create or recycle a stack frame object.
97
+ * @param {boolean} warpMode Enable warpMode on this frame.
98
+ * @returns {_StackFrame} The clean stack frame with correct warpMode setting.
99
+ */
100
+ static create (warpMode) {
101
+ const stackFrame = _stackFrameFreeList.pop();
102
+ if (typeof stackFrame !== 'undefined') {
103
+ stackFrame.warpMode = Boolean(warpMode);
104
+ return stackFrame;
105
+ }
106
+ return new _StackFrame(warpMode);
107
+ }
108
+
109
+ /**
110
+ * Put a stack frame object into the recycle bin for reuse.
111
+ * @param {_StackFrame} stackFrame The frame to reset and recycle.
112
+ */
113
+ static release (stackFrame) {
114
+ if (typeof stackFrame !== 'undefined') {
115
+ _stackFrameFreeList.push(stackFrame.reset());
116
+ }
117
+ }
118
+ }
119
+
120
+ /**
121
+ * A thread is a running stack context and all the metadata needed.
122
+ * @param {?string} firstBlock First block to execute in the thread.
123
+ * @constructor
124
+ */
125
+ class Thread {
126
+ constructor (firstBlock) {
127
+ /**
128
+ * ID of top block of the thread
129
+ * @type {!string}
130
+ */
131
+ this.topBlock = firstBlock;
132
+
133
+ /**
134
+ * Stack for the thread. When the sequencer enters a control structure,
135
+ * the block is pushed onto the stack so we know where to exit.
136
+ * @type {Array.<string>}
137
+ */
138
+ this.stack = [];
139
+
140
+ /**
141
+ * Stack frames for the thread. Store metadata for the executing blocks.
142
+ * @type {Array.<_StackFrame>}
143
+ */
144
+ this.stackFrames = [];
145
+
146
+ /**
147
+ * Status of the thread, one of three states (below)
148
+ * @type {number}
149
+ */
150
+ this.status = 0; /* Thread.STATUS_RUNNING */
151
+
152
+ /**
153
+ * Whether the thread is killed in the middle of execution.
154
+ * @type {boolean}
155
+ */
156
+ this.isKilled = false;
157
+
158
+ /**
159
+ * Target of this thread.
160
+ * @type {?Target}
161
+ */
162
+ this.target = null;
163
+
164
+ /**
165
+ * The Blocks this thread will execute.
166
+ * @type {Blocks}
167
+ */
168
+ this.blockContainer = null;
169
+
170
+ /**
171
+ * Whether the thread requests its script to glow during this frame.
172
+ * @type {boolean}
173
+ */
174
+ this.requestScriptGlowInFrame = false;
175
+
176
+ /**
177
+ * Which block ID should glow during this frame, if any.
178
+ * @type {?string}
179
+ */
180
+ this.blockGlowInFrame = null;
181
+
182
+ /**
183
+ * A timer for when the thread enters warp mode.
184
+ * Substitutes the sequencer's count toward WORK_TIME on a per-thread basis.
185
+ * @type {?Timer}
186
+ */
187
+ this.warpTimer = null;
188
+
189
+ this.justReported = null;
190
+ }
191
+
192
+ /**
193
+ * Thread status for initialized or running thread.
194
+ * This is the default state for a thread - execution should run normally,
195
+ * stepping from block to block.
196
+ * @const
197
+ */
198
+ static get STATUS_RUNNING () {
199
+ return 0;
200
+ }
201
+
202
+ /**
203
+ * Threads are in this state when a primitive is waiting on a promise;
204
+ * execution is paused until the promise changes thread status.
205
+ * @const
206
+ */
207
+ static get STATUS_PROMISE_WAIT () {
208
+ return 1;
209
+ }
210
+
211
+ /**
212
+ * Thread status for yield.
213
+ * @const
214
+ */
215
+ static get STATUS_YIELD () {
216
+ return 2;
217
+ }
218
+
219
+ /**
220
+ * Thread status for a single-tick yield. This will be cleared when the
221
+ * thread is resumed.
222
+ * @const
223
+ */
224
+ static get STATUS_YIELD_TICK () {
225
+ return 3;
226
+ }
227
+
228
+ /**
229
+ * Thread status for a finished/done thread.
230
+ * Thread is in this state when there are no more blocks to execute.
231
+ * @const
232
+ */
233
+ static get STATUS_DONE () {
234
+ return 4;
235
+ }
236
+
237
+ /**
238
+ * Push stack and update stack frames appropriately.
239
+ * @param {string} blockId Block ID to push to stack.
240
+ */
241
+ pushStack (blockId) {
242
+ this.stack.push(blockId);
243
+ // Push an empty stack frame, if we need one.
244
+ // Might not, if we just popped the stack.
245
+ if (this.stack.length > this.stackFrames.length) {
246
+ const parent = this.stackFrames[this.stackFrames.length - 1];
247
+ this.stackFrames.push(_StackFrame.create(typeof parent !== 'undefined' && parent.warpMode));
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Reset the stack frame for use by the next block.
253
+ * (avoids popping and re-pushing a new stack frame - keeps the warpmode the same
254
+ * @param {string} blockId Block ID to push to stack.
255
+ */
256
+ reuseStackForNextBlock (blockId) {
257
+ this.stack[this.stack.length - 1] = blockId;
258
+ this.stackFrames[this.stackFrames.length - 1].reuse();
259
+ }
260
+
261
+ /**
262
+ * Pop last block on the stack and its stack frame.
263
+ * @return {string} Block ID popped from the stack.
264
+ */
265
+ popStack () {
266
+ _StackFrame.release(this.stackFrames.pop());
267
+ return this.stack.pop();
268
+ }
269
+
270
+ /**
271
+ * Pop back down the stack frame until we hit a procedure call or the stack frame is emptied
272
+ */
273
+ stopThisScript () {
274
+ let blockID = this.peekStack();
275
+ while (blockID !== null) {
276
+ const block = this.target.blocks.getBlock(blockID);
277
+ if (typeof block !== 'undefined' && block.opcode === 'procedures_call') {
278
+ break;
279
+ }
280
+ this.popStack();
281
+ blockID = this.peekStack();
282
+ }
283
+
284
+ if (this.stack.length === 0) {
285
+ // Clean up!
286
+ this.requestScriptGlowInFrame = false;
287
+ this.status = Thread.STATUS_DONE;
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Get top stack item.
293
+ * @return {?string} Block ID on top of stack.
294
+ */
295
+ peekStack () {
296
+ return this.stack.length > 0 ? this.stack[this.stack.length - 1] : null;
297
+ }
298
+
299
+
300
+ /**
301
+ * Get top stack frame.
302
+ * @return {?object} Last stack frame stored on this thread.
303
+ */
304
+ peekStackFrame () {
305
+ return this.stackFrames.length > 0 ? this.stackFrames[this.stackFrames.length - 1] : null;
306
+ }
307
+
308
+ /**
309
+ * Get stack frame above the current top.
310
+ * @return {?object} Second to last stack frame stored on this thread.
311
+ */
312
+ peekParentStackFrame () {
313
+ return this.stackFrames.length > 1 ? this.stackFrames[this.stackFrames.length - 2] : null;
314
+ }
315
+
316
+ /**
317
+ * Push a reported value to the parent of the current stack frame.
318
+ * @param {*} value Reported value to push.
319
+ */
320
+ pushReportedValue (value) {
321
+ this.justReported = typeof value === 'undefined' ? null : value;
322
+ }
323
+
324
+ /**
325
+ * Initialize procedure parameters on this stack frame.
326
+ */
327
+ initParams () {
328
+ const stackFrame = this.peekStackFrame();
329
+ if (stackFrame.params === null) {
330
+ stackFrame.params = {};
331
+ }
332
+ }
333
+
334
+ /**
335
+ * Add a parameter to the stack frame.
336
+ * Use when calling a procedure with parameter values.
337
+ * @param {!string} paramName Name of parameter.
338
+ * @param {*} value Value to set for parameter.
339
+ */
340
+ pushParam (paramName, value) {
341
+ const stackFrame = this.peekStackFrame();
342
+ stackFrame.params[paramName] = value;
343
+ }
344
+
345
+ /**
346
+ * Get a parameter at the lowest possible level of the stack.
347
+ * @param {!string} paramName Name of parameter.
348
+ * @return {*} value Value for parameter.
349
+ */
350
+ getParam (paramName) {
351
+ for (let i = this.stackFrames.length - 1; i >= 0; i--) {
352
+ const frame = this.stackFrames[i];
353
+ if (frame.params === null) {
354
+ continue;
355
+ }
356
+ if (Object.prototype.hasOwnProperty.call(frame.params, paramName)) {
357
+ return frame.params[paramName];
358
+ }
359
+ return null;
360
+ }
361
+ return null;
362
+ }
363
+
364
+ /**
365
+ * Whether the current execution of a thread is at the top of the stack.
366
+ * @return {boolean} True if execution is at top of the stack.
367
+ */
368
+ atStackTop () {
369
+ return this.peekStack() === this.topBlock;
370
+ }
371
+
372
+
373
+ /**
374
+ * Switch the thread to the next block at the current level of the stack.
375
+ * For example, this is used in a standard sequence of blocks,
376
+ * where execution proceeds from one block to the next.
377
+ */
378
+ goToNextBlock () {
379
+ const nextBlockId = this.target.blocks.getNextBlock(this.peekStack());
380
+ this.reuseStackForNextBlock(nextBlockId);
381
+ }
382
+
383
+ /**
384
+ * Attempt to determine whether a procedure call is recursive,
385
+ * by examining the stack.
386
+ * @param {!string} procedureCode Procedure code of procedure being called.
387
+ * @return {boolean} True if the call appears recursive.
388
+ */
389
+ isRecursiveCall (procedureCode) {
390
+ let callCount = 5; // Max number of enclosing procedure calls to examine.
391
+ const sp = this.stack.length - 1;
392
+ for (let i = sp - 1; i >= 0; i--) {
393
+ const block = this.target.blocks.getBlock(this.stack[i]);
394
+ if (block.opcode === 'procedures_call' &&
395
+ block.mutation.proccode === procedureCode) {
396
+ return true;
397
+ }
398
+ if (--callCount < 0) return false;
399
+ }
400
+ return false;
401
+ }
402
+ }
403
+
404
+ module.exports = Thread;
@@ -0,0 +1,70 @@
1
+ /**
2
+ * @fileoverview
3
+ * Object representing a Scratch variable.
4
+ */
5
+
6
+ const uid = require('../util/uid');
7
+ const xmlEscape = require('../util/xml-escape');
8
+
9
+ class Variable {
10
+ /**
11
+ * @param {string} id Id of the variable.
12
+ * @param {string} name Name of the variable.
13
+ * @param {string} type Type of the variable, one of '' or 'list'
14
+ * @param {boolean} isCloud Whether the variable is stored in the cloud.
15
+ * @constructor
16
+ */
17
+ constructor (id, name, type, isCloud) {
18
+ this.id = id || uid();
19
+ this.name = name;
20
+ this.type = type;
21
+ this.isCloud = isCloud;
22
+ switch (this.type) {
23
+ case Variable.SCALAR_TYPE:
24
+ this.value = 0;
25
+ break;
26
+ case Variable.LIST_TYPE:
27
+ this.value = [];
28
+ break;
29
+ case Variable.BROADCAST_MESSAGE_TYPE:
30
+ this.value = this.name;
31
+ break;
32
+ default:
33
+ throw new Error(`Invalid variable type: ${this.type}`);
34
+ }
35
+ }
36
+
37
+ toXML (isLocal) {
38
+ isLocal = (isLocal === true);
39
+ return `<variable type="${this.type}" id="${this.id}" islocal="${isLocal
40
+ }" iscloud="${this.isCloud}">${xmlEscape(this.name)}</variable>`;
41
+ }
42
+
43
+ /**
44
+ * Type representation for scalar variables.
45
+ * This is currently represented as ''
46
+ * for compatibility with blockly.
47
+ * @const {string}
48
+ */
49
+ static get SCALAR_TYPE () {
50
+ return '';
51
+ }
52
+
53
+ /**
54
+ * Type representation for list variables.
55
+ * @const {string}
56
+ */
57
+ static get LIST_TYPE () {
58
+ return 'list';
59
+ }
60
+
61
+ /**
62
+ * Type representation for list variables.
63
+ * @const {string}
64
+ */
65
+ static get BROADCAST_MESSAGE_TYPE () {
66
+ return 'broadcast_msg';
67
+ }
68
+ }
69
+
70
+ module.exports = Variable;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Block argument types
3
+ * @enum {string}
4
+ */
5
+ const ArgumentType = {
6
+ /**
7
+ * Numeric value with angle picker
8
+ */
9
+ ANGLE: 'angle',
10
+
11
+ /**
12
+ * Boolean value with hexagonal placeholder
13
+ */
14
+ BOOLEAN: 'Boolean',
15
+
16
+ /**
17
+ * Numeric value with color picker
18
+ */
19
+ COLOR: 'color',
20
+
21
+ /**
22
+ * Numeric value with text field
23
+ */
24
+ NUMBER: 'number',
25
+
26
+ /**
27
+ * String value with text field
28
+ */
29
+ STRING: 'string',
30
+
31
+ /**
32
+ * String value with matrix field
33
+ */
34
+ MATRIX: 'matrix',
35
+
36
+ /**
37
+ * MIDI note number with note picker (piano) field
38
+ */
39
+ NOTE: 'note',
40
+
41
+ /**
42
+ * Inline image on block (as part of the label)
43
+ */
44
+ IMAGE: 'image'
45
+ };
46
+
47
+ module.exports = ArgumentType;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Types of block
3
+ * @enum {string}
4
+ */
5
+ const BlockType = {
6
+ /**
7
+ * Boolean reporter with hexagonal shape
8
+ */
9
+ BOOLEAN: 'Boolean',
10
+
11
+ /**
12
+ * A button (not an actual block) for some special action, like making a variable
13
+ */
14
+ BUTTON: 'button',
15
+
16
+ /**
17
+ * Command block
18
+ */
19
+ COMMAND: 'command',
20
+
21
+ /**
22
+ * Specialized command block which may or may not run a child branch
23
+ * The thread continues with the next block whether or not a child branch ran.
24
+ */
25
+ CONDITIONAL: 'conditional',
26
+
27
+ /**
28
+ * Specialized hat block with no implementation function
29
+ * This stack only runs if the corresponding event is emitted by other code.
30
+ */
31
+ EVENT: 'event',
32
+
33
+ /**
34
+ * Hat block which conditionally starts a block stack
35
+ */
36
+ HAT: 'hat',
37
+
38
+ /**
39
+ * Specialized command block which may or may not run a child branch
40
+ * If a child branch runs, the thread evaluates the loop block again.
41
+ */
42
+ LOOP: 'loop',
43
+
44
+ /**
45
+ * General reporter with numeric or string value
46
+ */
47
+ REPORTER: 'reporter'
48
+ };
49
+
50
+ module.exports = BlockType;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * @typedef {object} MessageDescriptor
3
+ * @property {string} id - the translator-friendly unique ID of this message.
4
+ * @property {string} default - the message text in the default language (English).
5
+ * @property {string} [description] - a description of this message to help translators understand the context.
6
+ */
7
+
8
+ /**
9
+ * This is a hook for extracting messages from extension source files.
10
+ * This function simply returns the message descriptor map object that's passed in.
11
+ * @param {object.<MessageDescriptor>} messages - the messages to be defined
12
+ * @return {object.<MessageDescriptor>} - the input, unprocessed
13
+ */
14
+ const defineMessages = function (messages) {
15
+ return messages;
16
+ };
17
+
18
+ module.exports = defineMessages;