Skip to content

Commit 37c6fbe

Browse files
authored
Merge pull request #284 from TurboWarp/fix-281
Discard old state after calling yielding procedure
2 parents d7a5f1c + 2244b26 commit 37c6fbe

9 files changed

+170
-4
lines changed

src/compiler/iroptimizer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ class TypeState {
9999
});
100100
}
101101

102+
/**
103+
* @param {TypeState} other
104+
* @returns {boolean}
105+
*/
106+
overwrite (other) {
107+
return this.mutate(other, varId => other.variables[varId] ?? InputType.ANY);
108+
}
109+
102110
/**
103111
* @param {*} variable A variable codegen object.
104112
* @param {InputType} type The type to set this variable to
@@ -490,6 +498,8 @@ class IROptimizer {
490498

491499
if (!script || !script.cachedAnalysisEndState) {
492500
modified = state.clear() || modified;
501+
} else if (script.yields) {
502+
modified = state.overwrite(script.cachedAnalysisEndState) || modified;
493503
} else {
494504
modified = state.after(script.cachedAnalysisEndState) || modified;
495505
}
@@ -578,6 +588,8 @@ class IROptimizer {
578588

579589
if (!script || !script.cachedAnalysisEndState) {
580590
modified = state.clear() || modified;
591+
} else if (script.yields) {
592+
modified = state.overwrite(script.cachedAnalysisEndState) || modified;
581593
} else {
582594
modified = state.after(script.cachedAnalysisEndState) || modified;
583595
}
Binary file not shown.
Binary file not shown.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// TW Snapshot
2+
// Input SHA-256: 848a4efc16b174b53f0a4b581e6b6d1091ae9eaa916e040c963e360ce3883509
3+
4+
// Sprite1 script
5+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
6+
const b0 = runtime.getOpcodeFunction("looks_say");
7+
const b1 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
8+
const b2 = stage.variables["FpLI$ida6)qR,q~y`1|*"];
9+
return function* genXYZ () {
10+
yield* executeInCompatibilityLayer({"MESSAGE":"plan 1",}, b0, false, false, "j", null);
11+
b1.value = (1 + 2);
12+
yield* thread.procedures["Znon-warp recursion %s"](2);
13+
if ((("" + listGet(b2.value, b1.value)).toLowerCase() === "the only thing".toLowerCase())) {
14+
yield* executeInCompatibilityLayer({"MESSAGE":"pass",}, b0, false, false, "t", null);
15+
}
16+
yield* executeInCompatibilityLayer({"MESSAGE":"end",}, b0, false, false, "s", null);
17+
retire(); return;
18+
}; })
19+
20+
// Sprite1 Znon-warp recursion %s
21+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
22+
return function* genXYZ_non_warp_recursion_ (p0) {
23+
if (compareGreaterThan(p0, 0)) {
24+
yield;
25+
yield* thread.procedures["Znon-warp recursion %s"](((+p0 || 0) - 1));
26+
}
27+
return "";
28+
}; })
29+
30+
// Sprite1 script
31+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
32+
const b0 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
33+
return function* genXYZ () {
34+
for (var a0 = 1; a0 >= 0.5; a0--) {
35+
yield;
36+
}
37+
b0.value = "random";
38+
retire(); return;
39+
}; })
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// TW Snapshot
2+
// Input SHA-256: 4715c75fe8effcdcdea3ad810949e33a7b6beb3fbd253c1edc2ed4f2bd093df3
3+
4+
// Sprite1 script
5+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
6+
const b0 = runtime.getOpcodeFunction("looks_say");
7+
const b1 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
8+
const b2 = stage.variables["FpLI$ida6)qR,q~y`1|*"];
9+
return function* genXYZ () {
10+
yield* executeInCompatibilityLayer({"MESSAGE":"plan 1",}, b0, false, false, "e", null);
11+
b1.value = (1 + 2);
12+
yield* thread.procedures["Zsomething that yields"]();
13+
if ((("" + listGet(b2.value, b1.value)).toLowerCase() === "the only thing".toLowerCase())) {
14+
yield* executeInCompatibilityLayer({"MESSAGE":"pass",}, b0, false, false, "o", null);
15+
}
16+
yield* executeInCompatibilityLayer({"MESSAGE":"end",}, b0, false, false, "n", null);
17+
retire(); return;
18+
}; })
19+
20+
// Sprite1 Zsomething that yields
21+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
22+
return function* genXYZ_something_that_yield () {
23+
for (var a0 = 2; a0 >= 0.5; a0--) {
24+
yield;
25+
}
26+
return "";
27+
}; })
28+
29+
// Sprite1 script
30+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
31+
const b0 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
32+
return function* genXYZ () {
33+
for (var a0 = 1; a0 >= 0.5; a0--) {
34+
yield;
35+
}
36+
b0.value = "random";
37+
retire(); return;
38+
}; })

test/snapshot/__snapshots__/tw-procedure-return-recursion.sb3.tw-snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ return function* genXYZ () {
1010
yield* executeInCompatibilityLayer({"MESSAGE":"plan 18",}, b0, false, false, "G", null);
1111
b1.value = 0;
1212
b2.value = (yield* thread.procedures["Znon warp recursion should yield %s"](8));
13-
if ((b1.value === 4)) {
13+
if (((+b1.value || 0) === 4)) {
1414
yield* executeInCompatibilityLayer({"MESSAGE":"pass non warp recursion yields",}, b0, false, false, "ao", null);
1515
}
1616
b1.value = 0;
@@ -20,7 +20,7 @@ yield* executeInCompatibilityLayer({"MESSAGE":"pass warp recursion does not yiel
2020
}
2121
b1.value = 0;
2222
b2.value = (yield* thread.procedures["Zfib %s"](7));
23-
if ((b1.value === 20)) {
23+
if (((+b1.value || 0) === 20)) {
2424
yield* executeInCompatibilityLayer({"MESSAGE":"pass non warp fib yielded",}, b0, false, false, "au", null);
2525
}
2626
yield* thread.procedures["Zrecursing yields between each %s"]("initial");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// TW Snapshot
2+
// Input SHA-256: 848a4efc16b174b53f0a4b581e6b6d1091ae9eaa916e040c963e360ce3883509
3+
4+
// Sprite1 script
5+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
6+
const b0 = runtime.getOpcodeFunction("looks_say");
7+
const b1 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
8+
const b2 = stage.variables["FpLI$ida6)qR,q~y`1|*"];
9+
return function* genXYZ () {
10+
yield* executeInCompatibilityLayer({"MESSAGE":"plan 1",}, b0, false, false, "j", null);
11+
b1.value = (1 + 2);
12+
yield* thread.procedures["Znon-warp recursion %s"](2);
13+
if ((("" + listGet(b2.value, b1.value)).toLowerCase() === "the only thing".toLowerCase())) {
14+
yield* executeInCompatibilityLayer({"MESSAGE":"pass",}, b0, false, false, "t", null);
15+
}
16+
yield* executeInCompatibilityLayer({"MESSAGE":"end",}, b0, false, false, "s", null);
17+
retire(); return;
18+
}; })
19+
20+
// Sprite1 Znon-warp recursion %s
21+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
22+
return function* genXYZ_non_warp_recursion_ (p0) {
23+
if (compareGreaterThan(p0, 0)) {
24+
yield;
25+
yield* thread.procedures["Znon-warp recursion %s"](((+p0 || 0) - 1));
26+
}
27+
return "";
28+
}; })
29+
30+
// Sprite1 script
31+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
32+
const b0 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
33+
return function* genXYZ () {
34+
for (var a0 = 1; a0 >= 0.5; a0--) {
35+
yield;
36+
}
37+
b0.value = "random";
38+
retire(); return;
39+
}; })
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// TW Snapshot
2+
// Input SHA-256: 4715c75fe8effcdcdea3ad810949e33a7b6beb3fbd253c1edc2ed4f2bd093df3
3+
4+
// Sprite1 script
5+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
6+
const b0 = runtime.getOpcodeFunction("looks_say");
7+
const b1 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
8+
const b2 = stage.variables["FpLI$ida6)qR,q~y`1|*"];
9+
return function* genXYZ () {
10+
yield* executeInCompatibilityLayer({"MESSAGE":"plan 1",}, b0, false, false, "e", null);
11+
b1.value = (1 + 2);
12+
yield* thread.procedures["Zsomething that yields"]();
13+
if ((("" + listGet(b2.value, b1.value)).toLowerCase() === "the only thing".toLowerCase())) {
14+
yield* executeInCompatibilityLayer({"MESSAGE":"pass",}, b0, false, false, "o", null);
15+
}
16+
yield* executeInCompatibilityLayer({"MESSAGE":"end",}, b0, false, false, "n", null);
17+
retire(); return;
18+
}; })
19+
20+
// Sprite1 Zsomething that yields
21+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
22+
return function* genXYZ_something_that_yield () {
23+
for (var a0 = 2; a0 >= 0.5; a0--) {
24+
yield;
25+
}
26+
return "";
27+
}; })
28+
29+
// Sprite1 script
30+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
31+
const b0 = stage.variables["`jEk@4|i[#Fk?(8x)AV.-my variable"];
32+
return function* genXYZ () {
33+
for (var a0 = 1; a0 >= 0.5; a0--) {
34+
yield;
35+
}
36+
b0.value = "random";
37+
retire(); return;
38+
}; })

test/snapshot/__snapshots__/warp-timer/tw-procedure-return-recursion.sb3.tw-snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ return function* genXYZ () {
1010
yield* executeInCompatibilityLayer({"MESSAGE":"plan 18",}, b0, false, false, "G", null);
1111
b1.value = 0;
1212
b2.value = (yield* thread.procedures["Znon warp recursion should yield %s"](8));
13-
if ((b1.value === 4)) {
13+
if (((+b1.value || 0) === 4)) {
1414
yield* executeInCompatibilityLayer({"MESSAGE":"pass non warp recursion yields",}, b0, false, false, "ao", null);
1515
}
1616
b1.value = 0;
@@ -20,7 +20,7 @@ yield* executeInCompatibilityLayer({"MESSAGE":"pass warp recursion does not yiel
2020
}
2121
b1.value = 0;
2222
b2.value = (yield* thread.procedures["Zfib %s"](7));
23-
if ((b1.value === 20)) {
23+
if (((+b1.value || 0) === 20)) {
2424
yield* executeInCompatibilityLayer({"MESSAGE":"pass non warp fib yielded",}, b0, false, false, "au", null);
2525
}
2626
yield* thread.procedures["Zrecursing yields between each %s"]("initial");

0 commit comments

Comments
 (0)