From f71bdb30e9ced15782778ce37769111498529a93 Mon Sep 17 00:00:00 2001 From: GarboMuffin Date: Tue, 22 Aug 2023 14:44:20 -0500 Subject: [PATCH] Add LOOP and CONDITION test extension (#921) does not work yet, but will soon - https://github.com/TurboWarp/scratch-vm/pull/141 - https://github.com/TurboWarp/scratch-vm/pull/158 --- website/test/loops.js | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 website/test/loops.js diff --git a/website/test/loops.js b/website/test/loops.js new file mode 100644 index 0000000000..ae1bf2f6c3 --- /dev/null +++ b/website/test/loops.js @@ -0,0 +1,71 @@ +// From https://github.com/TurboWarp/scratch-vm/pull/141 +(function (Scratch) { + "use strict"; + + class LoopsAndThings { + getInfo() { + return { + id: "loopsAndThings", + name: "Loops and things test", + blocks: [ + { + opcode: "conditional", + blockType: Scratch.BlockType.CONDITIONAL, + text: "run branch [BRANCH] of", + arguments: { + BRANCH: { + type: Scratch.ArgumentType.NUMBER, + defaultValue: 1 + } + }, + branchCount: 3 + }, + { + opcode: "loop", + blockType: Scratch.BlockType.LOOP, + text: "my repeat [TIMES]", + arguments: { + TIMES: { + type: Scratch.ArgumentType.NUMBER, + defaultValue: 10 + } + }, + }, + '---', + { + opcode: "testPromise", + blockType: Scratch.BlockType.REPORTER, + text: "return [VALUE] in a Promise", + arguments: { + VALUE: { + type: Scratch.ArgumentType.STRING, + defaultValue: '' + } + } + } + ] + }; + } + + conditional({BRANCH}, util) { + return Scratch.Cast.toNumber(BRANCH); + } + + loop({TIMES}, util) { + const times = Math.round(Scratch.Cast.toNumber(TIMES)); + if (typeof util.stackFrame.loopCounter === "undefined") { + util.stackFrame.loopCounter = times; + } + util.stackFrame.loopCounter--; + if (util.stackFrame.loopCounter >= 0) { + return true; + } + } + + testPromise({VALUE}) { + return Promise.resolve(VALUE); + } + } + + Scratch.extensions.register(new LoopsAndThings()); +})(Scratch); \ No newline at end of file