Reflection
Module concerned with meta information about classes and functions. Generally used for debugging
Require: require("Starlit/utils/Reflection")
Function
-
Starlit.utils.Reflection.getClassFieldNames(class:
string): (fields:string[]) Returns a table of the names of every field belonging to that class
- Parameters:
class (
string) – Name of the class- Returns:
fields (
string[]) – Field names
-
Starlit.utils.Reflection.getClassName(o:
any): (name:string) Returns the name of the java class or lua type that o is an instance of
- Parameters:
o (
any) – The object- Returns:
name (
string) – Name of the object’s class
-
Starlit.utils.Reflection.getField(object:
any, name:string): (value:any) Returns the value of an object’s field by name. This can retrieve field values even from objects of unexposed classes. It also works for exposed classes however it is less performant than the regular syntax.
- Parameters:
object (
any) – The objectname (
string) – The name of the field
- Returns:
value (
any) – The value of the field.
-
Starlit.utils.Reflection.getLocalName(callframeOffset:
integer, value:any): (name?:string) Returns the name of a local variable by its value.
- Parameters:
callframeOffset (
integer) – How many callframes downwards to search for the local.value (
any) – The value of the local to search for.
- Returns:
name? (
string) – The name of the first local variable containing the value, or nil if no local variable containing the value could be found.
-
Starlit.utils.Reflection.getLocalValue(callframeOffset:
integer, name:string): (value:any) Returns the value of a local variable by its name.
- Parameters:
callframeOffset (
integer) – How many callframes downwards to search for the local.name (
string) – The name of the local variable.
- Returns:
value (
any) – The value of the local variable, or nil if there was no such local. A non-existent local is indistinguishable from a local containing the value nil.
-
Starlit.utils.Reflection.getLocals(callframeOffset:
integer): (locals:table<string,any>) Returns a table containing all of the local variabless in a callframe.
- Parameters:
callframeOffset (
integer) – How many callframes downwards to get locals from.- Returns:
locals (
table<string,any>) – The local variables in the callframe.
-
Starlit.utils.Reflection.hasLocal(callframeOffset:
integer, name:string): (hasLocalVariable:boolean) Returns whether the specified callframe has a local variable by that name.
- Parameters:
callframeOffset (
integer) – How many callframes downwards to search for the local.name (
string) – The name of the local variable.
- Returns:
hasLocalVariable (
boolean) – Whether the callframe had a local variable by that name.
-
Starlit.utils.Reflection.registerClassName(metatable:
metatable, name:string):nil Registers a class’s name for getClassName
- Parameters:
metatable (
metatable) – Metatable of the classname (
string) – Name of the class
Glossary
- callframe
Callframes are the context of a function call. When a function is called a new callframe is created and placed on top of the stack. Remember that all Lua code execution is internally the same as a function call, so a file running for the first time is also a callframe, as well as code ran in the console.
Callframes should not be confused with scopes. A callframe will be able to see locals from all scopes visible to the currently executing line of code in that callframe.
For these functions, offset 0 refers to the current function. Offset 1 refers to the function calling the current function.
Note
In some limited circumstances, locals may be optimised out and will not actually exist at runtime, even if they are defined in the source file.