Skip to content

Commit f9b4775

Browse files
committed
Loop and condition test
TurboWarp/scratch-vm#141
1 parent 317186a commit f9b4775

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

website/test/loops.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
console.log(TIMES, util.stackFrame.loopCounter);
56+
const times = Math.round(Scratch.Cast.toNumber(TIMES));
57+
if (typeof util.stackFrame.loopCounter === "undefined") {
58+
util.stackFrame.loopCounter = times;
59+
}
60+
util.stackFrame.loopCounter--;
61+
if (util.stackFrame.loopCounter >= 0) {
62+
return true;
63+
}
64+
}
65+
66+
testPromise({VALUE}) {
67+
return Promise.resolve(VALUE);
68+
}
69+
}
70+
71+
Scratch.extensions.register(new LoopsAndThings());
72+
})(Scratch);

0 commit comments

Comments
 (0)