commit 6d72c9e: [Minor] Lua_expression: Allow simplier invocation of create function

Vsevolod Stakhov vsevolod at highsecure.ru
Fri May 17 17:35:04 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-05-17 17:54:26 +0100
URL: https://github.com/rspamd/rspamd/commit/6d72c9e66f5d8a11258bcf3a84224cfd8d45f24f

[Minor] Lua_expression: Allow simplier invocation of create function

---
 src/lua/lua_expression.c | 82 ++++++++++++++++++++++++++----------------------
 1 file changed, 45 insertions(+), 37 deletions(-)

diff --git a/src/lua/lua_expression.c b/src/lua/lua_expression.c
index d65f8c454..d2c1b4a4b 100644
--- a/src/lua/lua_expression.c
+++ b/src/lua/lua_expression.c
@@ -351,7 +351,8 @@ lua_expr_create (lua_State *L)
 	rspamd_mempool_t *pool;
 
 	/* Check sanity of the arguments */
-	if (lua_type (L, 1) != LUA_TSTRING || lua_type (L, 2) != LUA_TTABLE ||
+	if (lua_type (L, 1) != LUA_TSTRING ||
+			(lua_type (L, 2) != LUA_TTABLE || lua_type (L, 2) != LUA_TFUNCTION) ||
 			rspamd_lua_check_mempool (L, 3) == NULL) {
 		msg_info ("bad arguments to lua_expr_create");
 		lua_pushnil (L);
@@ -361,60 +362,67 @@ lua_expr_create (lua_State *L)
 		line = lua_tolstring (L, 1, &len);
 		pool = rspamd_lua_check_mempool (L, 3);
 
-		/* Check callbacks */
-		lua_pushvalue (L, 2);
-		lua_pushnumber (L, 1);
-		lua_gettable (L, -2);
-
-		if (lua_type (L, -1) != LUA_TFUNCTION) {
-			lua_pop (L, 2);
-			lua_pushnil (L);
-			lua_pushstring (L, "bad parse callback");
-
-			return 2;
-		}
-
-		lua_pop (L, 1);
+		e = rspamd_mempool_alloc (pool, sizeof (*e));
+		e->L = L;
+		e->pool = pool;
 
-		lua_pushnumber (L, 2);
-		lua_gettable (L, -2);
+		/* Check callbacks */
+		if (lua_istable (L, 2)) {
+			lua_pushvalue (L, 2);
+			lua_pushnumber (L, 1);
+			lua_gettable (L, -2);
 
-		if (lua_type (L, -1) != LUA_TFUNCTION) {
-			if (lua_type (L, -1) != LUA_TNIL && lua_type (L, -1) != LUA_TNONE) {
+			if (lua_type (L, -1) != LUA_TFUNCTION) {
 				lua_pop (L, 2);
 				lua_pushnil (L);
-				lua_pushstring (L, "bad process callback");
+				lua_pushstring (L, "bad parse callback");
 
 				return 2;
 			}
-			else {
-				no_process = TRUE;
-			}
-		}
 
-		lua_pop (L, 1);
+			lua_pop (L, 1);
 
-		/* Table is still on the top of stack */
+			lua_pushnumber (L, 2);
+			lua_gettable (L, -2);
 
-		e = rspamd_mempool_alloc (pool, sizeof (*e));
-		e->L = L;
-		e->pool = pool;
+			if (lua_type (L, -1) != LUA_TFUNCTION) {
+				if (lua_type (L, -1) != LUA_TNIL && lua_type (L, -1) != LUA_TNONE) {
+					lua_pop (L, 2);
+					lua_pushnil (L);
+					lua_pushstring (L, "bad process callback");
+
+					return 2;
+				}
+				else {
+					no_process = TRUE;
+				}
+			}
 
-		lua_pushnumber (L, 1);
-		lua_gettable (L, -2);
-		e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+			lua_pop (L, 1);
+			/* Table is still on the top of stack */
 
-		if (!no_process) {
-			lua_pushnumber (L, 2);
+			lua_pushnumber (L, 1);
 			lua_gettable (L, -2);
-			e->process_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+			e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+
+			if (!no_process) {
+				lua_pushnumber (L, 2);
+				lua_gettable (L, -2);
+				e->process_idx = luaL_ref (L, LUA_REGISTRYINDEX);
+			}
+			else {
+				e->process_idx = -1;
+			}
+
+			lua_pop (L, 1); /* Table */
 		}
 		else {
+			/* Process function is just a function, not a table */
+			lua_pushvalue (L, 2);
+			e->parse_idx = luaL_ref (L, LUA_REGISTRYINDEX);
 			e->process_idx = -1;
 		}
 
-		lua_pop (L, 1); /* Table */
-
 		if (!rspamd_parse_expression (line, len, &lua_atom_subr, e, pool, &err,
 				&e->expr)) {
 			lua_pushnil (L);


More information about the Commits mailing list