Skip to content

Commit f71bdb3

Browse files
authored
Add LOOP and CONDITION test extension (#921)
does not work yet, but will soon - TurboWarp/scratch-vm#141 - TurboWarp/scratch-vm#158
1 parent eb186fc commit f71bdb3

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

website/test/loops.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// From https://github.com/TurboWarp/scratch-vm/pull/141
2+
(function (Scratch) {
3+
"use strict";
4+
5+
class LoopsAndThings {
6+
getInfo() {
7+
return {
8+
id: "loopsAndThings",
9+
name: "Loops and things test",
10+
blocks: [
11+
{
12+
opcode: "conditional",
13+
blockType: Scratch.BlockType.CONDITIONAL,
14+
text: "run branch [BRANCH] of",
15+
arguments: {
16+
BRANCH: {
17+
type: Scratch.ArgumentType.NUMBER,
18+
defaultValue: 1
19+
}
20+
},
21+
branchCount: 3
22+
},
23+
{
24+
opcode: "loop",
25+
blockType: Scratch.BlockType.LOOP,
26+
text: "my repeat [TIMES]",
27+
arguments: {
28+
TIMES: {
29+
type: Scratch.ArgumentType.NUMBER,
30+
defaultValue: 10
31+
}
32+
},
33+
},
34+
'---',
35+
{
36+
opcode: "testPromise",
37+
blockType: Scratch.BlockType.REPORTER,
38+
text: "return [VALUE] in a Promise",
39+
arguments: {
40+
VALUE: {
41+
type: Scratch.ArgumentType.STRING,
42+
defaultValue: ''
43+
}
44+
}
45+
}
46+
]
47+
};
48+
}
49+
50+
conditional({BRANCH}, util) {
51+
return Scratch.Cast.toNumber(BRANCH);
52+
}
53+
54+
loop({TIMES}, util) {
55+
const times = Math.round(Scratch.Cast.toNumber(TIMES));
56+
if (typeof util.stackFrame.loopCounter === "undefined") {
57+
util.stackFrame.loopCounter = times;
58+
}
59+
util.stackFrame.loopCounter--;
60+
if (util.stackFrame.loopCounter >= 0) {
61+
return true;
62+
}
63+
}
64+
65+
testPromise({VALUE}) {
66+
return Promise.resolve(VALUE);
67+
}
68+
}
69+
70+
Scratch.extensions.register(new LoopsAndThings());
71+
})(Scratch);

0 commit comments

Comments
 (0)