assertTrue is the professional blog of Luke Bayes and Ali Mills

AS3 Bug When Nesting 'if' Within 'switch'

Posted by Ali Mills Tue, 29 Aug 2006 05:33:00 GMT

Luke and I ran into an ActionScript 3.0 bug today when nesting an if statement within a switch. It looks like the compiler has problems referencing object properties when this happens. The following code demonstrates what we found:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package {
    import flash.display.Sprite;

    public class SwitchBug extends Sprite {
        public function SwitchBug() {
            var num:Number = 1;
            var firstNumber:Number = num;
            var secondNumber:Number = num;
            tryNumSwitch("go", firstNumber, secondNumber);

            var now:Date = new Date();
            var firstDate:Date = now;
            var secondDate:Date = now;
            tryDateSwitch("go", firstDate, secondDate);            

            // this method causes a crash
            tryCrashingDateSwitch("go", firstDate, secondDate);
        }

        private function tryNumSwitch(val:String, firstNumber:Number, secondNumber:Number):void {
            switch(val) {
                case "go":
                    if(firstNumber == secondNumber) {
                        trace(">> tryNumSwitch successful !!");
                    }
            }
        }

        private function tryDateSwitch(val:String, firstDate:Date, secondDate:Date):void {
            switch(val) {
                case "go":
                    if(firstDate == secondDate) {
                        trace(">> tryDateSwitch successful !!");
                    }
            }
        }

        private function tryCrashingDateSwitch(val:String, firstDate:Date, secondDate:Date):void {
            switch(val) {
                case "go":
                    // the compiler crashes on DATE.milliseconds call
                    if(firstDate.milliseconds == secondDate.milliseconds) {
                        trace(">> tryCrashingDateSwitch successful !!");
                    }
                    // uncommenting the trace below fixes the situation
//                    trace("Fixed");
            }
        }
    }
}

Oddly, adding a trace (or any line of code) after the if statement fixes the issue.

Tags ,  | 2 comments

Comments

  1. Robert Penner replied: Avatar I passed this on to the AS3 team and they've filed a bug.
    Posted: 2 days later.
  2. alimills commented: Avatar Thanks Robert! I just heard from Chris Peyer on the Flash Player QA team and it's been filed. I appreciate you passing it along.
    Posted: 3 days later.

Your Reply

Comment Form.

Fields denoted with an "*" are required.