From 7632af3c73fc4e4ae3ad4c98c90c39b47c3b8379 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Tue, 4 Oct 2022 08:31:36 -0400 Subject: Consolidate API object code (#12728) Co-authored-by: sfan5 --- src/script/lua_api/l_modchannels.cpp | 43 +++++++----------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) (limited to 'src/script/lua_api/l_modchannels.cpp') diff --git a/src/script/lua_api/l_modchannels.cpp b/src/script/lua_api/l_modchannels.cpp index 931c2749c..019bb2238 100644 --- a/src/script/lua_api/l_modchannels.cpp +++ b/src/script/lua_api/l_modchannels.cpp @@ -57,14 +57,14 @@ ModChannelRef::ModChannelRef(const std::string &modchannel) : int ModChannelRef::l_leave(lua_State *L) { - ModChannelRef *ref = checkobject(L, 1); + ModChannelRef *ref = checkObject(L, 1); getGameDef(L)->leaveModChannel(ref->m_modchannel_name); return 0; } int ModChannelRef::l_send_all(lua_State *L) { - ModChannelRef *ref = checkobject(L, 1); + ModChannelRef *ref = checkObject(L, 1); ModChannel *channel = getobject(L, ref); if (!channel || !channel->canWrite()) return 0; @@ -78,7 +78,7 @@ int ModChannelRef::l_send_all(lua_State *L) int ModChannelRef::l_is_writeable(lua_State *L) { - ModChannelRef *ref = checkobject(L, 1); + ModChannelRef *ref = checkObject(L, 1); ModChannel *channel = getobject(L, ref); if (!channel) return 0; @@ -88,27 +88,11 @@ int ModChannelRef::l_is_writeable(lua_State *L) } void ModChannelRef::Register(lua_State *L) { - lua_newtable(L); - int methodtable = lua_gettop(L); - luaL_newmetatable(L, className); - int metatable = lua_gettop(L); - - lua_pushliteral(L, "__metatable"); - lua_pushvalue(L, methodtable); - lua_settable(L, metatable); // hide metatable from lua getmetatable() - - lua_pushliteral(L, "__index"); - lua_pushvalue(L, methodtable); - lua_settable(L, metatable); - - lua_pushliteral(L, "__gc"); - lua_pushcfunction(L, gc_object); - lua_settable(L, metatable); - - lua_pop(L, 1); // Drop metatable - - luaL_register(L, nullptr, methods); // fill methodtable - lua_pop(L, 1); // Drop methodtable + static const luaL_Reg metamethods[] = { + {"__gc", gc_object}, + {0, 0} + }; + registerClass(L, className, methods, metamethods); } void ModChannelRef::create(lua_State *L, const std::string &channel) @@ -126,17 +110,6 @@ int ModChannelRef::gc_object(lua_State *L) return 0; } -ModChannelRef *ModChannelRef::checkobject(lua_State *L, int narg) -{ - luaL_checktype(L, narg, LUA_TUSERDATA); - - void *ud = luaL_checkudata(L, narg, className); - if (!ud) - luaL_typerror(L, narg, className); - - return *(ModChannelRef **)ud; // unbox pointer -} - ModChannel *ModChannelRef::getobject(lua_State *L, ModChannelRef *ref) { return getGameDef(L)->getModChannel(ref->m_modchannel_name); -- cgit v1.2.3