From: Vsevolod Stakhov Date: Wed, 4 Mar 2020 12:47:45 +0000 (+0000) Subject: [Minor] Add helper to alter metatables X-Git-Tag: 2.5~99 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c5836a23f9bb91872f33375914baa02bca4001ca;p=rspamd.git [Minor] Add helper to alter metatables --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index 2087fe592..ce5fff6c5 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -197,6 +197,24 @@ rspamd_lua_setclass (lua_State * L, const gchar *classname, gint objidx) lua_setmetatable (L, objidx); } +void +rspamd_lua_add_metamethod (lua_State *L, const gchar *classname, + luaL_Reg *meth) +{ + khiter_t k; + + k = kh_get (lua_class_set, lua_classes, classname); + + g_assert (k != kh_end (lua_classes)); + /* get metatable identified by pointer */ + lua_rawgetp (L, LUA_REGISTRYINDEX, + RSPAMD_LIGHTUSERDATA_MASK (kh_key (lua_classes, k))); + + lua_pushcfunction (L, meth->func); + lua_setfield (L, -2, meth->name); + lua_pop (L, 1); /* remove metatable */ +} + /* assume that table is at the top */ void rspamd_lua_table_set (lua_State * L, const gchar *index, const gchar *value) diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 53e925832..aea6ebf26 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -161,6 +161,15 @@ void rspamd_lua_new_class (lua_State *L, */ void rspamd_lua_setclass (lua_State *L, const gchar *classname, gint objidx); +/** + * Adds a new field to the class (metatable) identified by `classname` + * @param L + * @param classname + * @param meth + */ +void rspamd_lua_add_metamethod (lua_State *L, const gchar *classname, + luaL_Reg *meth); + /** * Set index of table to value (like t['index'] = value) */ diff --git a/src/lua/lua_worker.c b/src/lua/lua_worker.c index f7f4b3c68..a286069cb 100644 --- a/src/lua/lua_worker.c +++ b/src/lua/lua_worker.c @@ -47,6 +47,7 @@ LUA_FUNCTION_DEF (worker, add_control_handler); const luaL_reg worker_reg[] = { LUA_INTERFACE_DEF (worker, get_name), + {"get_type", lua_worker_get_name}, LUA_INTERFACE_DEF (worker, get_stat), LUA_INTERFACE_DEF (worker, get_index), LUA_INTERFACE_DEF (worker, get_count),