Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash Actionscript 3 Bug? Using an int and negative numbers in a switch/case

Running this code

var myValue1:int = 2;
switch (myValue1)
{
    case -3: trace(myValue1 + " == -3"); break;
    case -2: trace(myValue1 + " == -2"); break;
    case -1: trace(myValue1 + " == -1"); break;
    case 0:  trace(myValue1 + " == 0"); break;
    case 1:  trace(myValue1 + " == 1"); break;
    case 2:  trace(myValue1 + " == 2"); break;
    case 3:  trace(myValue1 + " == 3"); break;
    default: trace(myValue1 + " is unknown"); break;
}

var myValue2:int = -2;
switch (myValue2)
{
    case -3: trace(myValue2 + " == -3"); break;
    case -2: trace(myValue2 + " == -2"); break;
    case -1: trace(myValue2 + " == -1"); break;
    case 0:  trace(myValue2 + " == 0"); break;
    case 1:  trace(myValue2 + " == 1"); break;
    case 2:  trace(myValue2 + " == 2"); break;
    case 3:  trace(myValue2 + " == 3"); break;
    default: trace(myValue2 + " is unknown"); break;
}

gives this output:

2 == 0
-2 is unknown

(Compiled in Flash Builder 4.7.0.349722, running on Flash 11.5.502.149. Running in Windows 7 SP1, Firefox 18.0.2)

The following changes all fix the above problem, giving the correct output:

  • Changing the value-type to Number.
  • Removing the negative-number case statements.
  • Changing the case statements to use int-variables rather than literals... unless those variables are also const, in which case it stays broken!

Changing myValue2 = -1 gives the output -1 == -3, which is equally wtf-ish.


Clearly this is a bug, but... what causes it? Is there some subtle nuance of using int or negative numbers in case-statements that I don't understand? Is my code somehow wrong? Or is this simply an issue with the bytecode-compiler in Flash Builder?

like image 898
BlueRaja - Danny Pflughoeft Avatar asked Feb 07 '13 22:02

BlueRaja - Danny Pflughoeft


3 Answers

I don't think you will find a concrete answer to this question. What I can offer is a confirmation of the AS3 compiler bugginess. I spent quite some time trying to resolve similar 'magical' errors and came up empty. One such example is a For and While loops skipping the first or last item in an Array for no apparent reason while a Foreach looped worked just fine.

To me the most plausible reason for that is an error in memory management. It might also be hardware related since the frequency of such things happening was higher on some machines that I or my colleagues worked on. What I think is that some 'invisible' conditions are met e.g. order of performed operations ( you could try putting some dummy code in between the var declaration and the switch or maybe assigning the var value couple more times, just to 'jog the memory') that combined with the error-prone compiler mess up the memory addressing and your switch thinks its compering two integers while in reality it got a different value from the memory.

like image 183
qiuntus Avatar answered Nov 19 '22 05:11

qiuntus


You can probably force the compiler's hand by converting the original value to a String and making your cases into String values.

switch( String(value) )
{
   case "-3": /*do something*/; break;
   case "-2": /*do something*/; break;
   case "-1": /*do something*/; break;
   case  "0": /*do something*/; break;
   case  "1": /*do something*/; break;
   case  "2": /*do something*/; break;
   case  "3": /*do something*/; break;
   default: break;
}
like image 1
Nathan Avatar answered Nov 19 '22 03:11

Nathan


This is simply an issue with the bytecode-compiler in Flash Builder.

Clearly, your syntax is fine; there are literally hundreds of known issues with the compiler dating back to 2005 that are still open or unresolved.

like image 1
cleverbit Avatar answered Nov 19 '22 05:11

cleverbit