]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-lua: Move init/deinit function invocation into helper functions
authorJosef 'Jeff' Sipek <jeff.sipek@open-xchange.com>
Fri, 11 Dec 2020 19:43:17 +0000 (14:43 -0500)
committerjeff.sipek <jeff.sipek@open-xchange.com>
Mon, 11 Jan 2021 16:36:23 +0000 (16:36 +0000)
This moves them out of the way and keeps them close to each other.

src/lib-lua/dlua-script.c

index d0899f0f50817cd0f2373a03968d51ce5d8a63be..b4736918195b2e066b25a5a48ac2d13277c47478 100644 (file)
@@ -109,21 +109,18 @@ struct dlua_script *dlua_script_from_state(lua_State *L)
        return script;
 }
 
-int dlua_script_init(struct dlua_script *script, const char **error_r)
+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;
 
-       if (script->init)
-               return 0;
-       script->init = TRUE;
-
-       /* see if there is a symbol for init */
-       lua_getglobal(script->L, LUA_SCRIPT_INIT_FN);
+       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("LUA_SCRIPT_INIT_FN") failed: %s",
+                       *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)) {
@@ -131,7 +128,7 @@ int dlua_script_init(struct dlua_script *script, const char **error_r)
                        if (ret != 0)
                                *error_r = "Script init failed";
                } else {
-                       *error_r = t_strdup_printf(LUA_SCRIPT_INIT_FN "() returned non-number");
+                       *error_r = t_strdup_printf("%s() returned non-number", func_name);
                        ret = -1;
                }
        }
@@ -140,6 +137,34 @@ int dlua_script_init(struct dlua_script *script, const char **error_r)
        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_warning("lua_pcall(%s) failed: %s", func_name,
+                                 lua_tostring(script->L, -1));
+                       lua_pop(script->L, 1);
+               }
+       } else {
+               lua_pop(script->L, 1);
+       }
+}
+
+int dlua_script_init(struct dlua_script *script, const char **error_r)
+{
+       if (script->init)
+               return 0;
+       script->init = TRUE;
+
+       return dlua_call_init_function(script, error_r);
+}
+
 static int dlua_atpanic(lua_State *L)
 {
        struct dlua_script *script = dlua_script_from_state(L);
@@ -277,20 +302,8 @@ int dlua_script_create_stream(struct istream *is, struct dlua_script **script_r,
 
 static void dlua_script_destroy(struct dlua_script *script)
 {
-       /* courtesy call */
-       int ret;
-       /* see if there is a symbol for deinit */
-       lua_getglobal(script->L, LUA_SCRIPT_DEINIT_FN);
-       if (lua_isfunction(script->L, -1)) {
-               ret = lua_pcall(script->L, 0, 0, 0);
-               if (ret != 0) {
-                       i_warning("lua_pcall("LUA_SCRIPT_DEINIT_FN") failed: %s",
-                                 lua_tostring(script->L, -1));
-                       lua_pop(script->L, 1);
-               }
-       } else {
-               lua_pop(script->L, 1);
-       }
+       dlua_call_deinit_function(script);
+
        lua_close(script->L);
        /* remove from list */
        DLLIST_REMOVE(&dlua_scripts, script);