Skip to content

Commit 7aa8930

Browse files
committed
Fix incorrect types for sensing_of
They can return 0 if the sprite does not exist Fixes #262 Closes #325 (this one uses a different fix and has tests) There are broader issues about compiled sensing_of not handling dynamic sprite create/delete. We should tackle that later. The other PR wouldn't really fix this either.
1 parent 779f0a1 commit 7aa8930

File tree

6 files changed

+33
-5
lines changed

6 files changed

+33
-5
lines changed

src/compiler/irgen.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ class ScriptTreeGenerator {
520520
}
521521

522522
if (object.isConstant('_stage_')) {
523+
// We assume that the stage always exists, so these don't need to be able to return 0.
523524
switch (property) {
524525
case 'background #': // fallthrough for scratch 1.0 compatibility
525526
case 'backdrop #':
@@ -528,6 +529,7 @@ class ScriptTreeGenerator {
528529
return new IntermediateInput(InputOpcode.SENSING_OF_BACKDROP_NAME, InputType.STRING);
529530
}
530531
} else {
532+
// If the target sprite does not exist, these may all return 0, even the costume name one.
531533
switch (property) {
532534
case 'x position':
533535
return new IntermediateInput(InputOpcode.SENSING_OF_POS_X, InputType.NUMBER, {object});
@@ -536,11 +538,11 @@ class ScriptTreeGenerator {
536538
case 'direction':
537539
return new IntermediateInput(InputOpcode.SENSING_OF_DIRECTION, InputType.NUMBER_REAL, {object});
538540
case 'costume #':
539-
return new IntermediateInput(InputOpcode.SENSING_OF_COSTUME_NUMBER, InputType.NUMBER_POS_INT, {object});
541+
return new IntermediateInput(InputOpcode.SENSING_OF_COSTUME_NUMBER, InputType.NUMBER_POS_INT | InputType.NUMBER_ZERO, {object});
540542
case 'costume name':
541-
return new IntermediateInput(InputOpcode.SENSING_OF_COSTUME_NAME, InputType.STRING, {object});
543+
return new IntermediateInput(InputOpcode.SENSING_OF_COSTUME_NAME, InputType.STRING | InputType.NUMBER_ZERO, {object});
542544
case 'size':
543-
return new IntermediateInput(InputOpcode.SENSING_OF_SIZE, InputType.NUMBER_POS, {object});
545+
return new IntermediateInput(InputOpcode.SENSING_OF_SIZE, InputType.NUMBER_POS | InputType.NUMBER_ZERO, {object});
544546
}
545547
}
546548

Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TW Snapshot
2+
// Input SHA-256: c3b1a45369f15f6809142ef2a2c6d1a680b802b70524a499b347af1008730a03
3+
4+
// Sprite1 script
5+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
6+
const b0 = runtime.getSpriteTargetByName("Sprite2");
7+
return function* genXYZ () {
8+
runtime.ext_scratch3_looks._say("plan 0", target);
9+
if ((("" + (b0 ? b0.getCostumes()[b0.currentCostume].name : 0)).toLowerCase() === "a".toLowerCase())) {
10+
}
11+
runtime.ext_scratch3_looks._say("end", target);
12+
retire(); return;
13+
}; })

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ runtime.ext_scratch3_looks._say("pass direction", target);
3535
if (((b1 ? b1.currentCostume + 1 : 0) === 3)) {
3636
runtime.ext_scratch3_looks._say("pass costume #", target);
3737
}
38-
if (((b1 ? b1.getCostumes()[b1.currentCostume].name : 0).toLowerCase() === "Costume name test".toLowerCase())) {
38+
if ((("" + (b1 ? b1.getCostumes()[b1.currentCostume].name : 0)).toLowerCase() === "Costume name test".toLowerCase())) {
3939
runtime.ext_scratch3_looks._say("pass costume name", target);
4040
}
4141
if (((b1 ? b1.size : 0) === 76.01)) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// TW Snapshot
2+
// Input SHA-256: c3b1a45369f15f6809142ef2a2c6d1a680b802b70524a499b347af1008730a03
3+
4+
// Sprite1 script
5+
(function factoryXYZ(thread) { const target = thread.target; const runtime = target.runtime; const stage = runtime.getTargetForStage();
6+
const b0 = runtime.getSpriteTargetByName("Sprite2");
7+
return function* genXYZ () {
8+
runtime.ext_scratch3_looks._say("plan 0", target);
9+
if ((("" + (b0 ? b0.getCostumes()[b0.currentCostume].name : 0)).toLowerCase() === "a".toLowerCase())) {
10+
}
11+
runtime.ext_scratch3_looks._say("end", target);
12+
retire(); return;
13+
}; })

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
@@ -35,7 +35,7 @@ runtime.ext_scratch3_looks._say("pass direction", target);
3535
if (((b1 ? b1.currentCostume + 1 : 0) === 3)) {
3636
runtime.ext_scratch3_looks._say("pass costume #", target);
3737
}
38-
if (((b1 ? b1.getCostumes()[b1.currentCostume].name : 0).toLowerCase() === "Costume name test".toLowerCase())) {
38+
if ((("" + (b1 ? b1.getCostumes()[b1.currentCostume].name : 0)).toLowerCase() === "Costume name test".toLowerCase())) {
3939
runtime.ext_scratch3_looks._say("pass costume name", target);
4040
}
4141
if (((b1 ? b1.size : 0) === 76.01)) {

0 commit comments

Comments
 (0)