bandaro
bandaro

Reputation: 165

Is it possible to take the name of a variable and turn it into a string in ActionScript 3.0?

I am making a simple debugger window in ActionScript for myself where I can add and remove variables I want to track. I was to be able to add variables to the list by just doing something like

DebuggerMonitor.trackVar(variable).

My question is, is there any way I can turn "variable" itself (the name, not the value) into a String to be added into a text field?

Upvotes: 3

Views: 91

Answers (3)

user797257
user797257

Reputation:

There is a confusion caused by the keyword 'var' because it is used to create several types of bindings.

  • Lexical bindings (the keyword 'var' was used inside a function).

  • Dynamic bindings (the keyword 'var' was used to declare a class' field).

Lexical bindings are interpreted by the compiler at compile time as addresses of the registers of the registers space occupied by the function. The names given to lexical bindings perish at this time and it is not possible to restore them at runtime - therefore you can't get the "name" of the variable.

Dynamic bindings are a kind of "public API" of the objects that declare them, they may be accessed from the code that was not compiled together with the code that created them, this is why, for the purpose of reflection the names of these bindings are stored in compiled code. However, ActionScript has no way of referencing LHS values, so you cannot, even if you know the name of the variable and the object declaring it, pass it to another function. But you can look it up in the debugger or by calling describeType on the object declaring the variable. Note that describeType will not show information on private variables even if you are calling it from the scope of the object in question.

Upvotes: 0

weltraumpirat
weltraumpirat

Reputation: 22604

Depending on how "intelligent" your debugger should be, you could just pass the name along:

DebuggerMonitor.trackVar( variable, "variable" );  

since obviously, when used in a context like this, the name should be known at the time you are writing the program.

You can also do some reflection magic to get instance variable names, but it won't work for temp variables (their names are dropped at compilation time):

public function getVariableName( instance:*, match:* ):String {
    var typeDescription:XML = describeType( instance );
    var variables:XMLList = typeDescription..variable;
    var accessors:XMLList = typeDescription..accessor;
    for each(var variable:XML in variables) 
        if(matchesXMLName( instance, variable, match )) 
            return variable.@name;
    for each(var accessor:XML in accessors) 
        if(matchesXMLName( instance, accessor, match )) 
            return accessor.@name;
    return "No name found.";
}

private function matchesXMLName( instance:*, xml:XML, match:* ):Boolean {
    return match == instance[[email protected]()];
}

var varName:String = getVariableName ( myObject, variable );

Using reflections like this will also be quite costly, if used often - you will have to think of a way to cache the type descriptions.

I recommend you check out the as3commons reflections package - there is a lot of useful functionality in there...

Upvotes: 2

Jeames Bone
Jeames Bone

Reputation: 564

Short answer - No :(

You can access the type name but not individual instance names, as these are lost at run-time.

Upvotes: 0

Related Questions