From: Vsevolod Stakhov Date: Wed, 16 Mar 2016 17:55:07 +0000 (+0000) Subject: [Feature] Allow to extract functions from lua tables X-Git-Tag: 1.2.0~27 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=095e129405958c6d1313b479d6ecd9d6c7b4a76d;p=rspamd.git [Feature] Allow to extract functions from lua tables --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index d2e64ad6a..992184ee2 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -689,6 +689,34 @@ rspamd_lua_parse_table_arguments (lua_State *L, gint pos, lua_pop (L, 1); break; + case 'F': + if (lua_type (L, -1) == LUA_TFUNCTION) { + *(va_arg (ap, gint *)) = luaL_ref (L, LUA_REGISTRYINDEX); + } + else if (lua_type (L, -1) == LUA_TNIL) { + failed = TRUE; + *(va_arg (ap, gint *)) = -1; + lua_pop (L, 1); + } + else { + g_set_error (err, + lua_error_quark (), + 1, + "bad type for key:" + " %.*s: '%s', '%s' is expected", + (gint) keylen, + key, + lua_typename (L, lua_type (L, -1)), + "function"); + va_end (ap); + lua_pop (L, 1); + + return FALSE; + } + + /* luaL_ref pops argument from the stack */ + break; + case 'B': if (lua_type (L, -1) == LUA_TBOOLEAN) { *(va_arg (ap, gboolean *)) = lua_toboolean (L, -1); diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 5356c3821..39f578ea6 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -307,6 +307,7 @@ struct rspamd_config * lua_check_config (lua_State * L, gint pos); * - B - boolean * - V - size_t + const char * * - U{classname} - userdata of the following class (stored in gpointer) + * - F - function * * If any of format string is prefixed with `*` then it is treated as required argument * @param L lua state