You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With sol2, you can query the metatable of tables and table proxys using my_table[sol::metatable_key]. Is there any way to query the metatable of a sol::protected_function directly using the sol API? It doesn't implement operator[] and I found no way to convert it to a table or proxy type. A generic function like sol::getmetatable would be cool.
My use case is that I want to attach metadata to my C++ functions like so:
Now I want to query this metadata in a context (function body of foobar), where I don't have g["bar"], but rather a sol::protected_function. I managed to retrieve the metatable with sol::state::script and one line of LUA code, but it would be nice to have something in the C++ API (which might boost performance as well?):
template <typename Table, typename Key>
autoget_metadata(sol::state_view& lua, Table&& t, Key&& key)
{
// This works, but I was hoping for a cleaner solution
lua["_tmp"] = t;
lua.script(R"( _tmp = getmetatable(_tmp))");
sol::object ret = lua["_tmp"][std::forward<Key>(key)];
return ret;
}
voidfoobar(sol::state_view& lua, sol::protected_function const& f)
{
// in this context, I don't have access to f as a proxy or table
std::cout << get_metadata(lua, f, "foo").as<std::string>() << "\n";
}
Does that actually work, can functions have metatables? I just checked source for Lua 5.1, 5.4 and LuaJit and I don't see any fields where functions could store a metatable. Tables on the other hand have clearly commented metatable fields.
When I try to do that with
functionClassA:MyFunc()
setmetatable(self.MyFunc, { data="meow!" })
end
I get an error bad argument #1 to 'setmetatable' (table expected, got function)
or does this only work with specific Lua versions? (I'm using LuaJit 2.1)
Or does the TableProxy + C++-template work in some wacky magic way when it comes to attaching/getting a metatable to/from functions?
That being said, being able to attach metadata to functions would be an awesome Lua feature.
Thanks a lot for this amazing library!
With sol2, you can query the metatable of tables and table proxys using
my_table[sol::metatable_key]
. Is there any way to query the metatable of asol::protected_function
directly using the sol API? It doesn't implementoperator[]
and I found no way to convert it to a table or proxy type. A generic function likesol::getmetatable
would be cool.My use case is that I want to attach metadata to my C++ functions like so:
Now I want to query this metadata in a context (function body of
foobar
), where I don't haveg["bar"]
, but rather asol::protected_function
. I managed to retrieve the metatable withsol::state::script
and one line of LUA code, but it would be nice to have something in the C++ API (which might boost performance as well?):Run this on godbolt
The text was updated successfully, but these errors were encountered: