Replies: 7 comments
-
Should be possible by accessing class metatable and using the internal keys luabridge use. Will try to provide some working snippets as soon as i'm back to the laptop. |
Beta Was this translation helpful? Give feedback.
-
Do you need to access them from lua or c++ ? |
Beta Was this translation helpful? Give feedback.
-
luabridge::setHideMetatables(false);
luabridge::getGlobalNamespace(L)
.addFunction("getMetatableClass", [](const luabridge::LuaRef& ref) -> luabridge::LuaRef
{
if (!ref.isTable())
return luabridge::LuaRef(ref.state());
ref.push();
luabridge::lua_rawgetp(ref.state(), -1, luabridge::detail::getClassKey());
auto mt = luabridge::LuaRef::fromStack(ref.state(), 1);
for (const auto& p : luabridge::pairs(mt))
std::cout << p.first << " " << p.second << std::endl;
return mt;
})
.beginClass<ExampleStringifiableClass>("ExampleStringifiableClass")
.addConstructor<void(*) ()>()
.addFunction("nonConstMethod", [](ExampleStringifiableClass*) {})
.addFunction("constMethod", [](const ExampleStringifiableClass*) {})
.endClass();
runLua(R"(
local t = ExampleStringifiableClass()
local mt = getmetatable(t)
getMetatableClass(mt)
)"); this will print:
Will provide some facilities to extract this kind of information in a more straight forward way |
Beta Was this translation helpful? Give feedback.
-
I need to access them from Lua. It is a powerful tool for verifying the correctness of the LuaBridge implementation, as well as presenting the class framework to users as a learning aid. |
Beta Was this translation helpful? Give feedback.
-
We can design a specific help table for that. So registering a method could push an entry in the table with the description of the method and argument type as seen from c++ (but not name) registered in the class |
Beta Was this translation helpful? Give feedback.
-
Pre-2019, instead of hard-coded usersdata values the tables above had string keys, I am currently doing exactly what you suggested and adding helper tables as I register the classes. I use |
Beta Was this translation helpful? Give feedback.
-
Indeed, reflection support on properties and methods would be great, but we can do it without needing refl-cpp as when registering custom callables we know their type and signature at compile time. |
Beta Was this translation helpful? Give feedback.
-
Before 2019 or so, it was possible to get the
_class
,_propget
, and_propset
tables for a class and for a namespace. This allowed Lua scripts to reflect classes and instances, which I used for diagnostic and regression testing. (Also for a class browser to facility easier programming.) However, this access was closed down due (apparently) to security concerns. I now build in extra tables that allow for reflection when I register the classes, but this complicates my registration process when I want to add in a new C++ framework that has challenges (such as overloaded functions) that make it difficult or impossible to include in my standard registration process.I can imagine that in many contexts (I am thinking games) the Lua environment needs to be relatively locked down. But I am working in a non-hostile environment where the user has to opt in to the Lua environment by installing the Lua plugin, and even then there is little motivation to cheat the system. So I am wondering if LuaBridge3 has an option to expose the class tables and if not, would you consider adding it?
Beta Was this translation helpful? Give feedback.
All reactions