AngelScript

Reflection

While the script language doesn't offer built-in functionality for reflection, the application interface do provide all the necessary methods to enumerate everything within the scripts.

The article on debugging shows a little of these interfaces with the focus on debugging rather than reflection. The article on dynamic compilations shows another aspect where scripts may be partially modified at runtime.

The following will do an overview of the available methods for enumerating the various entities in a script.

# Enumerating variables and properties

Global variables in a script module are enumerated with the interface asIScriptModule. Specifically the methods GetGlobalVarCount and GetGlobalVar. GetGlobalVarIndexByName and GetGlobalVarIndexByDecl can be used if the name and/or declaration of the desired variable is known before hand. To inspect or even modify the value of the global variable the method GetAddressOfGlobalVar should be used.

The engine interface asIScriptEngine has a similar set of methods for enumerating application registered global properties, i.e. GetGlobalPropertyCount, GetGlobalPropertyByIndex, GetGlobalPropertyIndexByName, and GetGlobalPropertyIndexByDecl.

Member properties of classes are accessed through the asIScriptObject interface for live object instances, and asIObjectType interface for inspecting the class declarations without any live object instance.

Local variables within functions can also be enumerated as long as the script has been compiled with debug information. These are then enumerated through the asIScriptFunction interface for inspecting the declarations, and directly through asIScriptContext for inspecting and/or modifying them on the stack.

# Enumerating functions and methods

Global functions in a script are enumerated with the interface asIScriptModule, using the methods GetFunctionCount, GetFunctionByIndex, GetFunctionByName, and GetFunctionByDecl.

The engine interface asIScriptEngine also exposes methods for enumerating application registered functions in a similar manner.

To enumerate methods of classes the interface asIObjectType should be used.

# Enumerating types

asIScriptModule is of course used to enumerate the types declared in the scripts too. The methods GetObjectTypeCount, GetObjectTypeByIndex, and GetObjectTypeByName are for enumerating classes and interfaces. The methods GetEnumCount, GetEnumByIndex, GetEnumValueCount, and GetEnumValueByIndex are for enumerating enums and their values.

The asIScriptEngine interface has near identical methods for enumerating the application registered types.

Many of the above methods return values called type ids that describes the type of the respective variable, property, or function argument. In many cases the type id can be directly inspected as a bitfield to get necessary information on the what the type is. The lower bits are just a sequence number where the first 12 numbers represents the built-in primitives, and anything higher represents either application registered types or script declared types. The higher bits indicate if the type represents a primitive, object, or handle. Use the flags in asETypeIdFlags to do the necessary verifications on the type id.

For type ids that represent object types it may be necessary to obtain the asIObjectType instance to get further information on what the type is. The method GetObjectTypeById is used to do this translate from type id to asIObjectType.