mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-03-13 17:04:39 -04:00
Merge pull request #1032 from towerofnix/while-vm
Implement "while" block
This commit is contained in:
commit
b794e19c31
3 changed files with 45 additions and 1 deletions
|
@ -17,6 +17,7 @@ class Scratch3ControlBlocks {
|
|||
return {
|
||||
control_repeat: this.repeat,
|
||||
control_repeat_until: this.repeatUntil,
|
||||
control_while: this.repeatWhile,
|
||||
control_for_each: this.forEach,
|
||||
control_forever: this.forever,
|
||||
control_wait: this.wait,
|
||||
|
@ -56,12 +57,20 @@ class Scratch3ControlBlocks {
|
|||
|
||||
repeatUntil (args, util) {
|
||||
const condition = Cast.toBoolean(args.CONDITION);
|
||||
// If the condition is true, start the branch.
|
||||
// If the condition is false (repeat UNTIL), start the branch.
|
||||
if (!condition) {
|
||||
util.startBranch(1, true);
|
||||
}
|
||||
}
|
||||
|
||||
repeatWhile (args, util) {
|
||||
const condition = Cast.toBoolean(args.CONDITION);
|
||||
// If the condition is true (repeat WHILE), start the branch.
|
||||
if (condition) {
|
||||
util.startBranch(1, true);
|
||||
}
|
||||
}
|
||||
|
||||
forEach (args, util) {
|
||||
const variable = util.target.lookupOrCreateVariable(
|
||||
args.VARIABLE.id, args.VARIABLE.name);
|
||||
|
|
|
@ -799,6 +799,19 @@ const specMap = {
|
|||
}
|
||||
]
|
||||
},
|
||||
'doWhile': {
|
||||
opcode: 'control_while',
|
||||
argMap: [
|
||||
{
|
||||
type: 'input',
|
||||
inputName: 'CONDITION'
|
||||
},
|
||||
{
|
||||
type: 'input',
|
||||
inputName: 'SUBSTACK'
|
||||
}
|
||||
]
|
||||
},
|
||||
'doForLoop': {
|
||||
opcode: 'control_for_each',
|
||||
argMap: [
|
||||
|
|
|
@ -52,6 +52,28 @@ test('repeatUntil', t => {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('repeatWhile', t => {
|
||||
const rt = new Runtime();
|
||||
const c = new Control(rt);
|
||||
|
||||
// Test harness (mocks `util`)
|
||||
let i = 0;
|
||||
const repeat = 10;
|
||||
const util = {
|
||||
stackFrame: Object.create(null),
|
||||
startBranch: function () {
|
||||
i++;
|
||||
// Note !== instead of ===
|
||||
c.repeatWhile({CONDITION: (i !== repeat)}, util);
|
||||
}
|
||||
};
|
||||
|
||||
// Execute test
|
||||
c.repeatWhile({CONDITION: (i !== repeat)}, util);
|
||||
t.strictEqual(i, repeat);
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('forEach', t => {
|
||||
const rt = new Runtime();
|
||||
const c = new Control(rt);
|
||||
|
|
Loading…
Reference in a new issue