Browse Source

[Minor] Allow to require some Lua function in C

tags/1.7.1
Vsevolod Stakhov 6 years ago
parent
commit
83f4454b04
2 changed files with 56 additions and 0 deletions
  1. 46
    0
      src/lua/lua_common.c
  2. 10
    0
      src/lua/lua_common.h

+ 46
- 0
src/lua/lua_common.c View File

@@ -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;
}

+ 10
- 0
src/lua/lua_common.h View File

@@ -390,5 +390,15 @@ gboolean rspamd_lua_run_postloads (lua_State *L, struct rspamd_config *cfg,
void rspamd_lua_add_ref_dtor (lua_State *L, rspamd_mempool_t *pool,
gint ref);

/**
* Tries to load some module using `require` and get some method from it
* @param L
* @param modname
* @param funcname
* @return TRUE if function exists in that module, the function is pushed in stack, otherwise stack is unchanged and FALSE is returned
*/
gboolean rspamd_lua_require_function (lua_State *L, const gchar *modname,
const gchar *funcname);

#endif /* WITH_LUA */
#endif /* RSPAMD_LUA_H */

Loading…
Cancel
Save