From 0312e28ecb0fbf418fefbffc24232e8cfc3fe8b6 Mon Sep 17 00:00:00 2001 From: Thomas Weber Date: Mon, 13 Jun 2022 21:12:33 -0500 Subject: [PATCH] Dirty hack to enable warp timer for loops that use timer in their condition --- src/compiler/irgen.js | 23 +++++++++++++++--- src/compiler/jsgen.js | 6 ++++- ...w-warp-repeat-until-timer-greater-than.sb3 | Bin 0 -> 1973 bytes 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/execute/tw-warp-repeat-until-timer-greater-than.sb3 diff --git a/src/compiler/irgen.js b/src/compiler/irgen.js index dd92c768..fc777249 100644 --- a/src/compiler/irgen.js +++ b/src/compiler/irgen.js @@ -80,6 +80,8 @@ class ScriptTreeGenerator { * @private */ this.variableCache = {}; + + this.usesTimer = false; } setProcedureVariant (procedureVariant) { @@ -620,6 +622,7 @@ class ScriptTreeGenerator { object: this.descendInputOfBlock(block, 'OBJECT') }; case 'sensing_timer': + this.usesTimer = true; return { kind: 'timer.get' }; @@ -751,16 +754,26 @@ class ScriptTreeGenerator { times: this.descendInputOfBlock(block, 'TIMES'), do: this.descendSubstack(block, 'SUBSTACK') }; - case 'control_repeat_until': + case 'control_repeat_until': { this.analyzeLoop(); + // Dirty hack: automatically enable warp timer for this block if it uses timer + // This fixes project that do things like "repeat until timer > 0.5" + this.usesTimer = false; + const condition = this.descendInputOfBlock(block, 'CONDITION'); + const needsWarpTimer = this.usesTimer; + if (needsWarpTimer) { + this.script.yields = true; + } return { kind: 'control.while', condition: { kind: 'op.not', - operand: this.descendInputOfBlock(block, 'CONDITION') + operand: condition }, - do: this.descendSubstack(block, 'SUBSTACK') + do: this.descendSubstack(block, 'SUBSTACK'), + warpTimer: needsWarpTimer }; + } case 'control_stop': { const level = block.fields.STOP_OPTION.value; if (level === 'all') { @@ -798,7 +811,9 @@ class ScriptTreeGenerator { return { kind: 'control.while', condition: this.descendInputOfBlock(block, 'CONDITION'), - do: this.descendSubstack(block, 'SUBSTACK') + do: this.descendSubstack(block, 'SUBSTACK'), + // We should consider analyzing this like we do for control_repeat_until + warpTimer: false }; case 'data_addtolist': diff --git a/src/compiler/jsgen.js b/src/compiler/jsgen.js index 93ac816d..a23a662d 100644 --- a/src/compiler/jsgen.js +++ b/src/compiler/jsgen.js @@ -794,7 +794,11 @@ class JSGenerator { this.resetVariableInputs(); this.source += `while (${this.descendInput(node.condition).asBoolean()}) {\n`; this.descendStack(node.do, new Frame(true)); - this.yieldLoop(); + if (node.warpTimer) { + this.yieldStuckOrNotWarp(); + } else { + this.yieldLoop(); + } this.source += `}\n`; break; diff --git a/test/fixtures/execute/tw-warp-repeat-until-timer-greater-than.sb3 b/test/fixtures/execute/tw-warp-repeat-until-timer-greater-than.sb3 new file mode 100644 index 0000000000000000000000000000000000000000..77ef3f288ba13469e10b61fe24c9a8ab91c3457a GIT binary patch literal 1973 zcma)-2T;@57RLXEsu17-QUW4URX{@u5~K)74PXqt8X$>5L^|jy0s$f=(u?#aK{^Nu zDj4bt0$CB*D4_-^s}zyG3NCNw&Fro-Z)WeEGiUCd`M#OoIWy;A&>Wmc007_xj{b*X z<9@=#d4&@I_(TAJ|KQgz!1o%#Gw95`Lx_M zBV^Rpw!vzNQ(T!bCXjBuHWez(W9sl0>$LpRW(#-wy7PxHHNE73TVbO)-AH?=&HxZ!jI|Y5t*~EsA2RBy&145?q_F*(vT_1 zAf7!pdgzBuomc+j_k`!Mohue6^QA5%qA=BJ z*Q!NFvpAC}-GO1vXleSau|hoPm-W0e!K6iN*rG?%(^Ks;>82^qWX)qQUD=RfCXJ|4 z);YbuS@v?~p0`to`8g+UgWnIk8$oenzR%o`qpY+$>OM&kzGcoI;_wUSS!%$smi9IB zYbmows?{TiK2FFM4&@mge?HDS!(CS1rKp%L92MTwVD_+g{MJyYNBU)mRjnUnAx3XF zZ=LXhJwJiiLHRBv_kRDZt@Od^@B=LV02^aO`g3L ziThSBuK?mVz7BCEz!HZu6ACPvE>Z)qmDVB_-FLt)2}|*elV`d7WUGoJr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