1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-05 18:41:05 +00:00

Implement helpful __tostring for all userdata-based classes

This commit is contained in:
Lars Mueller 2025-04-27 17:27:55 +02:00 committed by Lars Müller
parent 9ad23e4384
commit 747857bffa
22 changed files with 103 additions and 66 deletions

View file

@ -29,7 +29,22 @@ protected:
virtual void handleToTable(lua_State *L, IMetadata *meta);
virtual bool handleFromTable(lua_State *L, int table, IMetadata *meta);
static void registerMetadataClass(lua_State *L, const char *name, const luaL_Reg *methods);
template<class T>
static void registerMetadataClass(lua_State *L, const luaL_Reg *methods)
{
const luaL_Reg metamethods[] = {
{"__eq", l_equals},
{"__gc", gc_object},
{0, 0}
};
registerClass<T>(L, methods, metamethods);
// Set metadata_class in the metatable for MetaDataRef::checkAnyMetadata.
luaL_getmetatable(L, T::className);
lua_pushstring(L, T::className);
lua_setfield(L, -2, "metadata_class");
lua_pop(L, 1);
}
// Exported functions