Allowing dynamic addition / removal of properties collides with LuaBridge
I cannot add other properties to userdata defined with LuaBridge, so I tried to override __newindex and __index:
class Object
{
public:
Object(LuaRef def) : ID(def) // def.isTable() == true
{
}
LuaRef ID;
std::map<std::string, LuaRef> locals;
void newindex(std::string key, LuaRef value)
{
locals.emplace(key, value);
}
LuaRef index(std::string key)
{
auto i = locals.find(key);
if (i != locals.end())
{
return LuaRef(i->second);
}
return ID[key];
}
};
getGlobalNamespace(L)
.beginClass<Object>("Object")
.addProperty("ID", &Object::ID, false)
.addFunction("Remove", &Object::Remove)
.addFunction("__newindex", &Object::newindex)
.addFunction("__index", &Object::index)
.endClass()
However, this collides with LuaBridge's internal metatable - I can add, remove and access newly added properties and properties of the object's base definition table, but properties and functions added with addProperty or addFunction can't be found.
Thus, some way to either support dynamic properties or call LuaBridge's metamethods would be nice.
Indeed, when you register own __index function, you replace the LuaBridge one, and there is no means to access LuaBridge registered functions and data anymore. There is no mechanism for extending a userdata or table object other than through the metatable functions. And the problem is that registered functions are stored directly in a userdata metatable by the LuaBridge design.
How do you expect your code should work in a case where "Remove" is assigned from Lua? Replacing registered methods is insecure, while ignoring such assignments seems to be surprising.
How do you expect your code should work in a case where "Remove" is assigned from Lua?
My code shall store it inside a map and return it, effectively shadowing C++' "Remove".
Indeed, when you register own __index function, you replace the LuaBridge one, and there is no means to access LuaBridge registered functions and data anymore.
I somehow solved this problem by saving my __index function into another metatable field and swapping it with LuaBridge's one upon object creation, allowing me to access both dynamic and LuaBridge's properties.