diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-15 14:00:33 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-03-15 14:00:33 +0000 |
commit | 83f4454b042f8c2b62e52ce447748823c7e52365 (patch) | |
tree | f1424bf2661356ea2675e35ad56379f7ad3d9c1d /src/lua/lua_common.c | |
parent | 7a580591aabfda1fc1474c8baba45b8b9a679195 (diff) | |
download | rspamd-83f4454b042f8c2b62e52ce447748823c7e52365.tar.gz rspamd-83f4454b042f8c2b62e52ce447748823c7e52365.zip |
[Minor] Allow to require some Lua function in C
Diffstat (limited to 'src/lua/lua_common.c')
-rw-r--r-- | src/lua/lua_common.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index b44931682..f484c02b9 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -1897,3 +1897,49 @@ rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool, rspamd_mempool_add_destructor (pool, rspamd_lua_ref_dtor, cbdata); } } + +gboolean +rspamd_lua_require_function (lua_State *L, const gchar *modname, + const gchar *funcname) +{ + gint table_pos; + + lua_getglobal (L, "require"); + + if (lua_isnil (L, -1)) { + lua_pop (L, 1); + + return FALSE; + } + + lua_pushstring (L, modname); + + /* Now try to call */ + if (lua_pcall (L, 1, 1, 0) != 0) { + lua_pop (L, 1); + + return FALSE; + } + + /* Now we should have a table with results */ + if (!lua_istable (L, -1)) { + lua_pop (L, 1); + + return FALSE; + } + + table_pos = lua_gettop (L); + lua_pushstring (L, funcname); + lua_gettable (L, -2); + + if (lua_type (L, -1) == LUA_TFUNCTION) { + /* Remove table, preserve just a function */ + lua_remove (L, table_pos); + + return TRUE; + } + + lua_pop (L, 2); + + return FALSE; +} |