]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-lua: Use dlua_pcall
authorAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 5 Feb 2021 11:34:27 +0000 (13:34 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 22 Mar 2021 10:38:20 +0000 (10:38 +0000)
src/lib-lua/dlua-script.c
src/lib-lua/test-lua.c

index c24022b55fd82deb6589d674722e43c3de46513f..fe41fba7beeac5a2c429c051b39f9b79cb94753f 100644 (file)
@@ -185,51 +185,14 @@ int dlua_pcall(lua_State *L, const char *func_name, int nargs, int nresults,
        return ret;
 }
 
-static int dlua_call_init_function(struct dlua_script *script,
-                                  const char **error_r)
-{
-       const char *func_name = LUA_SCRIPT_INIT_FN;
-       int ret = 0;
-
-       lua_getglobal(script->L, func_name);
-
-       if (lua_isfunction(script->L, -1)) {
-               ret = lua_pcall(script->L, 0, 1, 0);
-               if (ret != 0) {
-                       *error_r = t_strdup_printf("lua_pcall(%s) failed: %s", func_name,
-                                                  lua_tostring(script->L, -1));
-                       ret = -1;
-               } else if (lua_isnumber(script->L, -1)) {
-                       ret = lua_tointeger(script->L, -1);
-                       if (ret != 0)
-                               *error_r = "Script init failed";
-               } else {
-                       *error_r = t_strdup_printf("%s() returned non-number", func_name);
-                       ret = -1;
-               }
-       }
-
-       lua_pop(script->L, 1);
-       return ret;
-}
-
 static void dlua_call_deinit_function(struct dlua_script *script)
 {
-       const char *func_name = LUA_SCRIPT_DEINIT_FN;
-       int ret;
-
-       lua_getglobal(script->L, func_name);
-
-       if (lua_isfunction(script->L, -1)) {
-               ret = lua_pcall(script->L, 0, 0, 0);
-               if (ret != 0) {
-                       i_error("lua_pcall(%s) failed: %s", func_name,
-                               lua_tostring(script->L, -1));
-                       lua_pop(script->L, 1);
-               }
-       } else {
-               lua_pop(script->L, 1);
-       }
+       const char *error;
+       if (!dlua_script_has_function(script, LUA_SCRIPT_DEINIT_FN))
+               return;
+       if (dlua_pcall(script->L, LUA_SCRIPT_DEINIT_FN, 0, 0, &error) < 0)
+               e_error(script->event, LUA_SCRIPT_DEINIT_FN"() failed: %s",
+                       error);
 }
 
 int dlua_script_init(struct dlua_script *script, const char **error_r)
@@ -238,7 +201,28 @@ int dlua_script_init(struct dlua_script *script, const char **error_r)
                return 0;
        script->init = TRUE;
 
-       return dlua_call_init_function(script, error_r);
+       /* lets not fail on missing function... */
+       if (!dlua_script_has_function(script, LUA_SCRIPT_INIT_FN))
+               return 0;
+
+       int ret = 0;
+
+       if (dlua_pcall(script->L, LUA_SCRIPT_INIT_FN, 0, 1, error_r) < 0)
+               return -1;
+
+       if (lua_isinteger(script->L, -1)) {
+               ret = lua_tointeger(script->L, -1);
+               if (ret != 0)
+                       *error_r = "Script init failed";
+       } else {
+               *error_r = LUA_SCRIPT_INIT_FN"() returned non-number";
+               ret = -1;
+       }
+
+       lua_pop(script->L, 1);
+
+       i_assert(lua_gettop(script->L) == 0);
+       return ret;
 }
 
 static int dlua_atpanic(lua_State *L)
index cdce7b1e37ce51edb0e843a4f60c0b05ceb2e803..7a28014d9c11855742c5870eb5187be7a0731e57 100644 (file)
@@ -198,8 +198,7 @@ static void test_lua(void)
        test_assert(dlua_script_init(script, &error) == 0);
        test_assert(dlua_script_has_function(script, "lua_function"));
 
-       lua_getglobal(script->L, "lua_test_flags");
-       test_assert(lua_pcall(script->L, 0, 0, 0) == 0);
+       test_assert(dlua_pcall(script->L, "lua_test_flags", 0, 0, &error) == 0);
 
        lua_getglobal(script->L, "lua_test_get_table");
        test_assert(lua_pcall(script->L, 0, 1, 0) == 0);