Skip to content

Commit

Permalink
Optimize counter blocks in compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Oct 8, 2023
1 parent d386082 commit 6ef82be
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/blocks/scratch3_control.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Scratch3ControlBlocks {
* The "counter" block value. For compatibility with 2.0.
* @type {number}
*/
this._counter = 0;
this._counter = 0; // used by compiler

this.runtime.on('RUNTIME_DISPOSED', this.clearCounter.bind(this));
}
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/compat-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// Please keep these lists alphabetical.

const stacked = [
'control_clear_counter',
'control_incr_counter',
'looks_changestretchby',
'looks_hideallsprites',
'looks_say',
Expand Down Expand Up @@ -36,7 +34,6 @@ const stacked = [
];

const inputs = [
'control_get_counter',
'motion_xscroll',
'motion_yscroll',
'sensing_loud',
Expand Down
15 changes: 14 additions & 1 deletion src/compiler/irgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ class ScriptTreeGenerator {
};
}

case 'control_get_counter':
return {
kind: 'counter.get'
};

case 'data_variable':
return {
kind: 'var.get',
Expand Down Expand Up @@ -699,6 +704,10 @@ class ScriptTreeGenerator {
whenTrue: this.descendSubstack(block, 'SUBSTACK'),
whenFalse: []
};
case 'control_clear_counter':
return {
kind: 'counter.clear'
};
case 'control_create_clone_of':
return {
kind: 'control.createClone',
Expand Down Expand Up @@ -741,6 +750,10 @@ class ScriptTreeGenerator {
whenTrue: this.descendSubstack(block, 'SUBSTACK'),
whenFalse: this.descendSubstack(block, 'SUBSTACK2')
};
case 'control_incr_counter':
return {
kind: 'counter.increment'
};
case 'control_repeat':
this.analyzeLoop();
return {
Expand Down Expand Up @@ -1560,7 +1573,7 @@ class ScriptTreeGenerator {
} else {
entryBlock = topBlockId;
}

if (entryBlock) {
this.script.stack = this.walkStack(entryBlock);
}
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/jsgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ class JSGenerator {
case 'constant':
return this.safeConstantInput(node.value);

case 'counter.get':
return new TypedInput('runtime.ext_scratch3_control._counter', TYPE_NUMBER);

case 'keyboard.pressed':
return new TypedInput(`runtime.ioDevices.keyboard.getKeyIsDown(${this.descendInput(node.key).asSafe()})`, TYPE_BOOLEAN);

Expand Down Expand Up @@ -873,6 +876,13 @@ class JSGenerator {
this.source += `}\n`;
break;

case 'counter.clear':
this.source += 'runtime.ext_scratch3_control._counter = 0;\n';
break;
case 'counter.increment':
this.source += 'runtime.ext_scratch3_control._counter++;\n';
break;

case 'hat.edge':
this.isInHat = true;
this.source += '{\n';
Expand Down
Binary file added test/fixtures/execute/tw-counter.sb3
Binary file not shown.
41 changes: 41 additions & 0 deletions test/snapshot/__snapshots__/tw-counter.sb3.tw-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// TW Snapshot
// Input SHA-256: 29dd30c632645f071bfd450530dab86dc85a2c1cf30564bee984394a790eca6a

// 2 script
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
const b0 = runtime.getOpcodeFunction("looks_say");
return function* genXYZ () {
if ((runtime.ext_scratch3_control._counter === 10)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass global",}, b0, false, false, "C", null);
}
yield* executeInCompatibilityLayer({"MESSAGE":"end",}, b0, false, false, "B", null);
retire(); return;
}; })

// 1 script
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
const b0 = runtime.getOpcodeFunction("looks_say");
return function* genXYZ () {
yield* executeInCompatibilityLayer({"MESSAGE":"plan 5",}, b0, false, false, "g", null);
if ((runtime.ext_scratch3_control._counter === 0)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass initial value = 0",}, b0, false, false, "q", null);
}
runtime.ext_scratch3_control._counter++;
if ((runtime.ext_scratch3_control._counter === 1)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass incr 1",}, b0, false, false, "s", null);
}
runtime.ext_scratch3_control._counter++;
if ((runtime.ext_scratch3_control._counter === 2)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass incr 2",}, b0, false, false, "u", null);
}
runtime.ext_scratch3_control._counter = 0;
if ((runtime.ext_scratch3_control._counter === 0)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass clear = 0",}, b0, false, false, "w", null);
}
for (var a0 = 10; a0 >= 0.5; a0--) {
runtime.ext_scratch3_control._counter++;
yield;
}
startHats("event_whenbroadcastreceived", { BROADCAST_OPTION: "check globalness" });
retire(); return;
}; })
41 changes: 41 additions & 0 deletions test/snapshot/__snapshots__/warp-timer/tw-counter.sb3.tw-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// TW Snapshot
// Input SHA-256: 29dd30c632645f071bfd450530dab86dc85a2c1cf30564bee984394a790eca6a

// 2 script
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
const b0 = runtime.getOpcodeFunction("looks_say");
return function* genXYZ () {
if ((runtime.ext_scratch3_control._counter === 10)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass global",}, b0, false, false, "C", null);
}
yield* executeInCompatibilityLayer({"MESSAGE":"end",}, b0, false, false, "B", null);
retire(); return;
}; })

// 1 script
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
const b0 = runtime.getOpcodeFunction("looks_say");
return function* genXYZ () {
yield* executeInCompatibilityLayer({"MESSAGE":"plan 5",}, b0, false, false, "g", null);
if ((runtime.ext_scratch3_control._counter === 0)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass initial value = 0",}, b0, false, false, "q", null);
}
runtime.ext_scratch3_control._counter++;
if ((runtime.ext_scratch3_control._counter === 1)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass incr 1",}, b0, false, false, "s", null);
}
runtime.ext_scratch3_control._counter++;
if ((runtime.ext_scratch3_control._counter === 2)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass incr 2",}, b0, false, false, "u", null);
}
runtime.ext_scratch3_control._counter = 0;
if ((runtime.ext_scratch3_control._counter === 0)) {
yield* executeInCompatibilityLayer({"MESSAGE":"pass clear = 0",}, b0, false, false, "w", null);
}
for (var a0 = 10; a0 >= 0.5; a0--) {
runtime.ext_scratch3_control._counter++;
yield;
}
startHats("event_whenbroadcastreceived", { BROADCAST_OPTION: "check globalness" });
retire(); return;
}; })

0 comments on commit 6ef82be

Please sign in to comment.