Skip to content

Commit fd1b855

Browse files
authored
Merge pull request #301 from TurboWarp/more-unnecessary-casts
Remove more unnecessary casts
2 parents d8c26ad + efb502a commit fd1b855

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

src/compiler/iroptimizer.js

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,36 @@ class IROptimizer {
158158
case InputOpcode.ADDON_CALL:
159159
break;
160160

161+
case InputOpcode.CAST_BOOLEAN: {
162+
const innerType = inputs.target.type;
163+
if (innerType & InputType.BOOLEAN) return innerType;
164+
return InputType.BOOLEAN;
165+
}
166+
161167
case InputOpcode.CAST_NUMBER: {
162168
const innerType = inputs.target.type;
163169
if (innerType & InputType.NUMBER) return innerType;
164170
return InputType.NUMBER;
165-
} case InputOpcode.CAST_NUMBER_OR_NAN: {
171+
}
172+
173+
case InputOpcode.CAST_NUMBER_INDEX: {
174+
const innerType = inputs.target.type;
175+
if (innerType & InputType.NUMBER_INDEX) return innerType;
176+
return InputType.NUMBER_INDEX;
177+
}
178+
179+
case InputOpcode.CAST_NUMBER_OR_NAN: {
166180
const innerType = inputs.target.type;
167181
if (innerType & InputType.NUMBER_OR_NAN) return innerType;
168182
return InputType.NUMBER_OR_NAN;
169183
}
170184

185+
case InputOpcode.CAST_STRING: {
186+
const innerType = inputs.target.type;
187+
if (innerType & InputType.STRING) return innerType;
188+
return InputType.STRING;
189+
}
190+
171191
case InputOpcode.OP_ADD: {
172192
const leftType = inputs.left.type;
173193
const rightType = inputs.right.type;
@@ -703,19 +723,45 @@ class IROptimizer {
703723
}
704724

705725
switch (input.opcode) {
726+
case InputOpcode.CAST_BOOLEAN: {
727+
const targetType = input.inputs.target.type;
728+
if ((targetType & InputType.BOOLEAN) === targetType) {
729+
return input.inputs.target;
730+
}
731+
return input;
732+
}
733+
706734
case InputOpcode.CAST_NUMBER: {
707735
const targetType = input.inputs.target.type;
708736
if ((targetType & InputType.NUMBER) === targetType) {
709737
return input.inputs.target;
710738
}
711739
return input;
712-
} case InputOpcode.CAST_NUMBER_OR_NAN: {
740+
}
741+
742+
case InputOpcode.CAST_NUMBER_INDEX: {
743+
const targetType = input.inputs.target.type;
744+
if ((targetType & InputType.NUMBER_INDEX) === targetType) {
745+
return input.inputs.target;
746+
}
747+
return input;
748+
}
749+
750+
case InputOpcode.CAST_NUMBER_OR_NAN: {
713751
const targetType = input.inputs.target.type;
714752
if ((targetType & InputType.NUMBER_OR_NAN) === targetType) {
715753
return input.inputs.target;
716754
}
717755
return input;
718756
}
757+
758+
case InputOpcode.CAST_STRING: {
759+
const targetType = input.inputs.target.type;
760+
if ((targetType & InputType.STRING) === targetType) {
761+
return input.inputs.target;
762+
}
763+
return input;
764+
}
719765
}
720766

721767
return input;

test/snapshot/__snapshots__/tw-sensing-of.sb3.tw-snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (compareEqual((b4 ? b4.value : 0), 0)) {
5252
yield* executeInCompatibilityLayer({"MESSAGE":"pass non existent variable",}, b0, false, false, ")nnN?*l+E)dC(fT5(_@q", null);
5353
}
5454
b5.value = (("" + randomInt(1, 9)) + ("" + randomInt(1, 9)));
55-
if (compareEqual(runtime.ext_scratch3_sensing.getAttributeOf({OBJECT: ("" + b5.value), PROPERTY: "backdrop #" }), 0)) {
55+
if (compareEqual(runtime.ext_scratch3_sensing.getAttributeOf({OBJECT: b5.value, PROPERTY: "backdrop #" }), 0)) {
5656
yield* executeInCompatibilityLayer({"MESSAGE":"pass NE backdrop #",}, b0, false, false, "UFr{[email protected]_paq:r]F", null);
5757
}
5858
if (compareEqual(runtime.ext_scratch3_sensing.getAttributeOf({OBJECT: ("" + b5.value), PROPERTY: "backdrop name" }), 0)) {

test/snapshot/__snapshots__/tw-simple-string-operations.sb3.tw-snapshot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ return function funXYZ_a () {
2424
b0.value = "ababa";
2525
b1.value = "";
2626
b2.value = 1;
27-
for (var a0 = ("" + b0.value).length; a0 > 0; a0--) {
28-
if ((((("" + b0.value))[(b2.value | 0) - 1] || "").toLowerCase() === "a".toLowerCase())) {
29-
b1.value = (("" + b1.value) + "b");
27+
for (var a0 = b0.value.length; a0 > 0; a0--) {
28+
if ((((b0.value)[b2.value - 1] || "").toLowerCase() === "a".toLowerCase())) {
29+
b1.value = (b1.value + "b");
3030
} else {
31-
b1.value = (("" + b1.value) + "a");
31+
b1.value = (b1.value + "a");
3232
}
3333
b2.value = (b2.value + 1);
3434
}

test/snapshot/__snapshots__/warp-timer/tw-sensing-of.sb3.tw-snapshot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (compareEqual((b4 ? b4.value : 0), 0)) {
5252
yield* executeInCompatibilityLayer({"MESSAGE":"pass non existent variable",}, b0, false, false, ")nnN?*l+E)dC(fT5(_@q", null);
5353
}
5454
b5.value = (("" + randomInt(1, 9)) + ("" + randomInt(1, 9)));
55-
if (compareEqual(runtime.ext_scratch3_sensing.getAttributeOf({OBJECT: ("" + b5.value), PROPERTY: "backdrop #" }), 0)) {
55+
if (compareEqual(runtime.ext_scratch3_sensing.getAttributeOf({OBJECT: b5.value, PROPERTY: "backdrop #" }), 0)) {
5656
yield* executeInCompatibilityLayer({"MESSAGE":"pass NE backdrop #",}, b0, false, false, "UFr{[email protected]_paq:r]F", null);
5757
}
5858
if (compareEqual(runtime.ext_scratch3_sensing.getAttributeOf({OBJECT: ("" + b5.value), PROPERTY: "backdrop name" }), 0)) {

0 commit comments

Comments
 (0)