hello,
there seems to be a problem when static properties are being accessed from
inside static methods of the same class, as the following example demonstrates.
the class BugDemo has a static property _objects:Object. tag:String is a local
variable containing an id for local obj:Object. if i try to set
BugDemo._objects[ tag ], it sets _objects.tag ( == _objects[ "tag" ]) instead:
BugDemo._objects[ tag ] = obj; // sets _objects[ "tag" ]
if i write it like the following, it works as expected:
(BugDemo._objects)[ tag ] = obj;
in the attached example, BugDemo has two static methods, "test" and "test2".
they are identical, except for the two lines setting and accessing a property
of BugDemo._objects. in "test" there are no brackets around it, in "test2"
there are.
yes, i know i could simply access it as _objects[ tag ], without the BugDemo in
front of it. if i do, it works as expected (i.e. no bug).
frame 1 contains a TextField, "output", and the following script:
import BugDemo;
var foo = {};
var bar = {};
BugDemo.test( foo );
output.text += "-----\n";
BugDemo.test2( bar );
it's output is:
tag: undefined
-----
id1: object
tag: object
BudDemo.as contains the following:
class BugDemo {
static private var _cntr:Number = 0;
static private var _objects:Object = {};
static public function test ( obj:Object ) {
var tag:String = obj.tag;
if( !tag.length ) {
tag = obj.tag = "id" + BugDemo._cntr++;
BugDemo._objects[ tag ] = obj; // <-- behaves like "_objects.tag"
}
for( var i in BugDemo._objects )
_root.output.text += ( i + ": " + typeof BugDemo._objects[ i ] + "\n" );
}
static public function test2 ( obj:Object ) {
var tag:String = obj.tag;
if( !tag.length ) {
tag = obj.tag = "id" + BugDemo._cntr++;
(BugDemo._objects)[ tag ] = obj; // <-- note the (...)
}
for( var i in BugDemo._objects )
_root.output.text += ( i + ": " + typeof (BugDemo._objects)[ i ] + "\n" );
}
}
decompiled with flasm:
in function test,
lines 93 - 98:
push 'BugDemo'
getVariable
push '_objects'
getMember
push 'tag', r:obj // <-- 'tag' should be a reference to obj
setMember
in function test2,
lines 167 - 172:
push 'BugDemo'
getVariable
push '_objects'
getMember
push r:3, r:obj // <-- that's correct
setMember
the problem does not occur with the mtasc compiler.
i'm using Flash MX 2004, version 7.2.
hth,
mark