Reflect
The Reflect API is a way to manipulate library values dynamically through an
abstract interface in an untyped manner. Use with care!
Lib.Reflect.hasField(o, field) |
Tells if structure o has a field named field. If o or field are nil, the result is unspecified. |
o - object field - field name |
Lib.Reflect.field(o, field) |
Returns the value of the field named field on object o. |
o - object field - field name |
Lib.Reflect.setField(o, field, value) |
Sets the field named field of object o to value value. |
o - object field - field name value - field value |
Lib.Reflect.getProperty(o, field) |
Returns the value of the field named field on object o, taking property getter functions into account. |
o - object field - property field name |
Lib.Reflect.setProperty(o, field, value) |
Sets the field named field of object o to value value, taking property setter functions into account. |
o - object field - property field name value - field value |
Lib.Reflect.callMethod(o, funcName, args) |
Call a method funcName with the given object o and arguments args. |
o - object funcName - method name, string value args - array of arguments |
Lib.Reflect.callMethodSafe(o, funcName, args, errFunc) |
Safe call for a Lib method with funcName with the given object o and arguments args. This call performs in safe try/catch block. errFunc will be triggered in case of Lib error. WARNING: callMethodSafe works only with Lib methods. REMOVED FROM LIB (use pcall) |
o - object funcName - method name, string value args - array of arguments errFunc - on error function |
Lib.Reflect.fields(o) |
Returns the fields of structure o. |
o - object |
Lib.Reflect.isFunction(f) |
Returns true if f is a function, false otherwise. |
f - function or any other object |
Lib.Reflect.isObject(v) |
Tells if v is an object. |
v - any type object, enum value |
Lib.Reflect.isEnumValue(v) |
Tells if v is an enum value. |
v - any type object, enum value |
Lib.Reflect.deleteField(o, field) |
Removes the field named field from structure o. |
o - object field - field name |
Lib.Reflect.copy(o) |
Copies the fields of structure o. |
o - object |
Examples:
Lib.Sys.trace(Lib.Reflect.fields(Lib))
Lib.Reflect.callMethod(Lib.Sys, Lib.Sys.trace, {'message'})
local obj = {a = 1, b= 'a'}
local clone = Lib.Reflect.copy(obj)
Lib.Sys.trace(clone)
Lib.Reflect.callMethodSafe(Lib.Sys, "throw", {"My Error"}, function(err) print("ERROR: "..err) end)
--WARNING: current implementation of callMethodSafe will not work on Lib methods assigned to non Lib Lua object:
a = {} -- non Lib Lua object
a.throw = Lib.Sys.throw -- throw assigned to Lua object
Lib.Reflect.callMethodSafe(a, "throw", {"err1"}, function(err) print("ERROR: "..err) end) -- will not capture error
PointToSys = Lib.Sys
Lib.Reflect.callMethodSafe(PointToSys, "throw", {"err2",2}, function(err) print("ERROR: "..err) end) -- will capture error, method "throw" still part of Lib object
Created with the Personal Edition of HelpNDoc: Easy EPub and documentation editor