2

I tried to execute the following actionscript3 program and I am surprised of the result of the call to f() function. I was expecting that the result of f() was "1" or at least "undefined" but the "0" value does not makes any sense for me.

I will be pleased to have a good explanation of this behavior if you have one, or to know if you consider this behavior as "normal". I want to precise that I'm studying the behavior of Action Script programs in order to understand how the AVM2 really works and therefore I am not asking equivalent code to do the same thing. As a consequence if you have other tricky examples I am also interested.

package {
import flash.display.Sprite;

  public class S2 extends Sprite {
      public function f():* {
           return x;
       }      
       public static function fs():*{
           return x;
       }      
  }
}
var x:int = 1 ;
var a:S2 = new S2();
var g:Function = a.f;
var gs:Function = S2.fs;
trace("tracing(g)...:"+g()); //tracing(g)...:0
trace("tracing(gs)...:"+gs()); //tracing(gs)...:1

Note: I compiled this program with the following command line:

mxmlc -debug -static-link-runtime-shared-libraries=true -output S2.swf -- S2.as
cdlane
  • 40,441
  • 5
  • 32
  • 81
Vincent M.
  • 23
  • 2

1 Answers1

1

Your x variable exists in a different scope than the x you are returning from S2.f()

S2 extends Sprite, which in turn extends DisplayObject, which already has a x property.
This is what you are returning.

If you change the variable name to something like myX you will get an error a expected.

Doing this will change what is returned:

var a:S2 = new S2();
a.x = 10;
trace(a.f()); // will trace 10
grapefrukt
  • 27,016
  • 6
  • 49
  • 73
  • That's right ! thanks. It was the unique answer in order to comply with the language specification but I just forgot there are already a lot of superclasses for a so simple program. – Vincent M. Nov 07 '11 at 13:50
  • Just another thing: the same program compiled without "-debug" option leads to a "stack underflow" error (with flex45 sdk on linux). The bytecode generated does really weird things (including a pop on an empty operand stack...) in this case, have you already met such problems on compilation ? – Vincent M. Nov 07 '11 at 13:56
  • This is a rather nonstandard way of writing actionscript code, I'm actually surprised it even works at all with code after the class declaration like that. If you add another class and put that code in it's constructor you should have better luck. – grapefrukt Nov 07 '11 at 22:17
  • I understand this is not a standard way for writing code, anyway there is always code executed at this level (script level in bytecode). Without any code after the class declaration, there is at least code for creation of the class that is defined as a property of the script (more precisely global object of the script). Since my final goal is to analyze bytecode, I have to consider this part of code and the compiler does not seem to forbid to write code at this level. The weird thing is that the compiler generates invalid code without complaining and only when debug flag is disabled – Vincent M. Nov 08 '11 at 10:28
  • Typical use for putting code there would be an internal class, but I agree that allowing other code *and* generating invalid bytecode is very strange. I'd say you've found a compiler bug! – grapefrukt Nov 08 '11 at 10:34