Skip to content

Commit

Permalink
Merge pull request #1033 from towerofnix/counter-vm
Browse files Browse the repository at this point in the history
Implement counter blocks
  • Loading branch information
kchadha committed Apr 9, 2018
2 parents b794e19 + 797d16b commit d177f4c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/blocks/scratch3_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class Scratch3ControlBlocks {
* @type {Runtime}
*/
this.runtime = runtime;

/**
* The "counter" block value. For compatibility with 2.0.
* @type {number}
*/
this._counter = 0;
}

/**
Expand All @@ -26,7 +32,10 @@ class Scratch3ControlBlocks {
control_if_else: this.ifElse,
control_stop: this.stop,
control_create_clone_of: this.createClone,
control_delete_this_clone: this.deleteClone
control_delete_this_clone: this.deleteClone,
control_get_counter: this.getCounter,
control_incr_counter: this.incrCounter,
control_clear_counter: this.clearCounter
};
}

Expand Down Expand Up @@ -155,6 +164,18 @@ class Scratch3ControlBlocks {
this.runtime.disposeTarget(util.target);
this.runtime.stopForTarget(util.target);
}

getCounter () {
return this._counter;
}

clearCounter () {
this._counter = 0;
}

incrCounter () {
this._counter++;
}
}

module.exports = Scratch3ControlBlocks;
15 changes: 15 additions & 0 deletions src/serialization/sb2_specmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,21 @@ const specMap = {
argMap: [
]
},
'COUNT': {
opcode: 'control_get_counter',
argMap: [
]
},
'INCR_COUNT': {
opcode: 'control_incr_counter',
argMap: [
]
},
'CLR_COUNT': {
opcode: 'control_clear_counter',
argMap: [
]
},
'touching:': {
opcode: 'sensing_touchingobject',
argMap: [
Expand Down
17 changes: 17 additions & 0 deletions test/unit/blocks_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,20 @@ test('stop', t => {
t.strictEqual(state.stopThisScript, 1);
t.end();
});

test('counter, incrCounter, clearCounter', t => {
const rt = new Runtime();
const c = new Control(rt);

// Default value
t.strictEqual(c.getCounter(), 0);

c.incrCounter();
c.incrCounter();
t.strictEqual(c.getCounter(), 2);

c.clearCounter();
t.strictEqual(c.getCounter(), 0);

t.end();
});

0 comments on commit d177f4c

Please sign in to comment.