]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Make lua methods and functions use the passed in lua_State * directly
authorJosef 'Jeff' Sipek <jeff.sipek@open-xchange.com>
Tue, 12 Jan 2021 18:53:19 +0000 (13:53 -0500)
committerJosef 'Jeff' Sipek <jeff.sipek@open-xchange.com>
Tue, 12 Jan 2021 18:53:19 +0000 (13:53 -0500)
src/lib-storage/mail-user-lua.c
src/lib-storage/mailbox-lua.c

index 0986d04a99e6a70bf68f328607470041c6b93595..55577f1db12375a6b923b759ba7d76d29521e655 100644 (file)
@@ -173,8 +173,7 @@ static int lua_storage_mail_user_mailbox_alloc(lua_State *L)
 
 static int lua_storage_mail_user_unref(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
-       struct mail_user **ptr = lua_touserdata(script->L, 1);
+       struct mail_user **ptr = lua_touserdata(L, 1);
        if (*ptr != NULL)
                mail_user_unref(ptr);
        *ptr = NULL;
index 681b074870d7f0efddc8f94b8d88ed7a3a22e9ba..a16107f7dfb01af3696451fd76d238bed215ff77 100644 (file)
@@ -66,52 +66,47 @@ static int lua_storage_mailbox_tostring(lua_State *L)
    are really equal */
 static int lua_storage_mailbox_eq(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 2);
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
        struct mailbox *mbox2 = lua_check_storage_mailbox(L, 2);
-       lua_pushboolean(script->L, DLUA_MAILBOX_EQUALS(mbox, mbox2));
+       lua_pushboolean(L, DLUA_MAILBOX_EQUALS(mbox, mbox2));
        return 1;
 }
 
 /* these compare based to mailbox vname */
 static int lua_storage_mailbox_lt(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 2);
        bool res = lua_storage_cmp(L) <= 0;
-       lua_pushboolean(script->L, res);
+       lua_pushboolean(L, res);
        return 1;
 }
 
 static int lua_storage_mailbox_le(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 2);
        bool res = lua_storage_cmp(L) < 0;
-       lua_pushboolean(script->L, res);
+       lua_pushboolean(L, res);
        return 1;
 }
 
 static int lua_storage_mailbox_unref(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 1);
        /* fetch item from table */
-       lua_pushliteral(script->L, "item");
-       lua_rawget(script->L, 1);
-       struct mailbox **mbox = lua_touserdata(script->L, -1);
+       lua_pushliteral(L, "item");
+       lua_rawget(L, 1);
+       struct mailbox **mbox = lua_touserdata(L, -1);
        if (*mbox != NULL)
                mailbox_free(mbox);
        *mbox = NULL;
-       lua_pop(script->L, 1);
+       lua_pop(L, 1);
        return 0;
 }
 
 static int lua_storage_mailbox_gc(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
-       struct mailbox **mbox = lua_touserdata(script->L, 1);
+       struct mailbox **mbox = lua_touserdata(L, 1);
 
        if (*mbox != NULL)
                mailbox_free(mbox);
@@ -121,13 +116,12 @@ static int lua_storage_mailbox_gc(lua_State *L)
 
 static int lua_storage_mailbox_open(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 1);
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
 
        /* try to open the box */
        if (mailbox_open(mbox) < 0) {
-               return luaL_error(script->L, "mailbox_open(%s) failed: %s",
+               return luaL_error(L, "mailbox_open(%s) failed: %s",
                                  mailbox_get_vname(mbox),
                                  mailbox_get_last_error(mbox, NULL));
        }
@@ -147,17 +141,16 @@ static int lua_storage_mailbox_close(lua_State *L)
 
 static int lua_storage_mailbox_sync(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS_IN(L, 1, 2);
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
        enum mailbox_sync_flags flags = 0;
 
-       if (lua_gettop(script->L) >= 2)
-               flags = luaL_checkinteger(script->L, 2);
+       if (lua_gettop(L) >= 2)
+               flags = luaL_checkinteger(L, 2);
 
        if (mailbox_sync(mbox, flags) < 0) {
                const char *error = mailbox_get_last_error(mbox, NULL);
-               return luaL_error(script->L, "mailbox_sync(%s) failed: %s",
+               return luaL_error(L, "mailbox_sync(%s) failed: %s",
                                  mailbox_get_vname(mbox), error);
        }
 
@@ -168,36 +161,35 @@ static int lua_storage_mailbox_status(lua_State *L)
 {
        struct mailbox_status status;
        const char *const *keyword;
-       struct dlua_script *script = dlua_script_from_state(L);
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
        /* get items as list of parameters */
        enum mailbox_status_items items = 0;
 
-       if (lua_gettop(script->L) < 2)
-               return luaL_error(script->L, "expecting at least 1 parameter");
-       for(int i = 2; i <= lua_gettop(script->L); i++)
-               items |= (unsigned int)luaL_checkinteger(script->L, i);
+       if (lua_gettop(L) < 2)
+               return luaL_error(L, "expecting at least 1 parameter");
+       for(int i = 2; i <= lua_gettop(L); i++)
+               items |= (unsigned int)luaL_checkinteger(L, i);
 
        i_zero(&status);
        if (mailbox_get_status(mbox, items, &status) < 0) {
                const char *error = mailbox_get_last_error(mbox, NULL);
-               return luaL_error(script->L, "mailbox_get_status(%s, %u) failed: %s",
+               return luaL_error(L, "mailbox_get_status(%s, %u) failed: %s",
                                  mbox, items, error);
        }
        /* returns a table */
-       lua_createtable(script->L, 0, 20);
+       lua_createtable(L, 0, 20);
 
-       lua_pushstring(script->L, mailbox_get_vname(mbox));
-       lua_setfield(script->L, -2, "mailbox");
+       lua_pushstring(L, mailbox_get_vname(mbox));
+       lua_setfield(L, -2, "mailbox");
 
 #undef LUA_TABLE_SETNUMBER
 #define LUA_TABLE_SETNUMBER(field) \
-       lua_pushnumber(script->L, status.field); \
-       lua_setfield(script->L, -2, #field);
+       lua_pushnumber(L, status.field); \
+       lua_setfield(L, -2, #field);
 #undef LUA_TABLE_SETBOOL
 #define LUA_TABLE_SETBOOL(field) \
-       lua_pushboolean(script->L, status.field); \
-       lua_setfield(script->L, -2, #field);
+       lua_pushboolean(L, status.field); \
+       lua_setfield(L, -2, #field);
 
        LUA_TABLE_SETNUMBER(messages);
        LUA_TABLE_SETNUMBER(recent);
@@ -222,12 +214,12 @@ static int lua_storage_mailbox_status(lua_State *L)
 
        if (status.keywords != NULL && array_is_created(status.keywords)) {
                int i = 1;
-               lua_createtable(script->L, array_count(status.keywords), 0);
+               lua_createtable(L, array_count(status.keywords), 0);
                array_foreach(status.keywords, keyword) {
-                       lua_pushstring(script->L, *keyword);
-                       lua_rawseti(script->L, -2, i++);
+                       lua_pushstring(L, *keyword);
+                       lua_rawseti(L, -2, i++);
                }
-               lua_setfield(script->L, -2, "keywords");
+               lua_setfield(L, -2, "keywords");
        }
 
        return 1;
@@ -235,17 +227,16 @@ static int lua_storage_mailbox_status(lua_State *L)
 
 static int lua_storage_mailbox_metadata_get(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
-       if (lua_gettop(script->L) < 2)
-               return luaL_error(script->L, "expecting at least 1 parameter");
+       if (lua_gettop(L) < 2)
+               return luaL_error(L, "expecting at least 1 parameter");
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
        const char *value, *error;
        size_t value_len;
-       int ret, i, top = lua_gettop(script->L);
+       int ret, i, top = lua_gettop(L);
 
        ret = 0;
        for(i = 2; i <= top; i++) {
-               const char *key = lua_tostring(script->L, i);
+               const char *key = lua_tostring(L, i);
                if (key == NULL) {
                        ret = -1;
                        error = t_strdup_printf("expected string at #%d", i);
@@ -256,14 +247,14 @@ static int lua_storage_mailbox_metadata_get(lua_State *L)
                                                             &value_len, &error)) < 0) {
                        break;
                } else if (ret == 0) {
-                       lua_pushnil(script->L);
+                       lua_pushnil(L);
                } else {
-                       lua_pushlstring(script->L, value, value_len);
+                       lua_pushlstring(L, value, value_len);
                }
        }
 
        if (ret < 0)
-               return luaL_error(script->L, "%s", error);
+               return luaL_error(L, "%s", error);
 
        /* return number of pushed items */
        i_assert(i>=2);
@@ -272,17 +263,16 @@ static int lua_storage_mailbox_metadata_get(lua_State *L)
 
 static int lua_storage_mailbox_metadata_set(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 3);
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
-       const char *key = luaL_checkstring(script->L, 2);
+       const char *key = luaL_checkstring(L, 2);
        const char *value, *error;
        size_t value_len;
 
-       value = lua_tolstring(script->L, 3, &value_len);
+       value = lua_tolstring(L, 3, &value_len);
 
        if (lua_storage_mailbox_attribute_set(mbox, key, value, value_len, &error) < 0)
-               return luaL_error(script->L,
+               return luaL_error(L,
                                  t_strdup_printf("Cannot set attribute: %s", error));
 
        return 0;
@@ -290,14 +280,13 @@ static int lua_storage_mailbox_metadata_set(lua_State *L)
 
 static int lua_storage_mailbox_metadata_unset(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
        DLUA_REQUIRE_ARGS(L, 2);
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
-       const char *key = luaL_checkstring(script->L, 2);
+       const char *key = luaL_checkstring(L, 2);
        const char *error;
 
        if (lua_storage_mailbox_attribute_set(mbox, key, NULL, 0,  &error) < 0)
-               return luaL_error(script->L,
+               return luaL_error(L,
                                  t_strdup_printf("Cannot unset attribute: %s", error));
 
        return 0;
@@ -305,9 +294,8 @@ static int lua_storage_mailbox_metadata_unset(lua_State *L)
 
 static int lua_storage_mailbox_metadata_list(lua_State *L)
 {
-       struct dlua_script *script = dlua_script_from_state(L);
-       if (lua_gettop(script->L) < 2)
-               return luaL_error(script->L, "expecting at least 1 parameter");
+       if (lua_gettop(L) < 2)
+               return luaL_error(L, "expecting at least 1 parameter");
        struct mailbox *mbox = lua_check_storage_mailbox(L, 1);
        const struct lua_storage_keyvalue *item;
        const char *error;
@@ -318,8 +306,8 @@ static int lua_storage_mailbox_metadata_list(lua_State *L)
                t_array_init(&items, 1);
 
                ret = 0;
-               for(i = 2; i <= lua_gettop(script->L); i++) {
-                       const char *key = lua_tostring(script->L, i);
+               for(i = 2; i <= lua_gettop(L); i++) {
+                       const char *key = lua_tostring(L, i);
 
                        if (key == NULL) {
                                ret = -1;
@@ -335,19 +323,19 @@ static int lua_storage_mailbox_metadata_list(lua_State *L)
                }
 
                if (ret == 0) {
-                       lua_createtable(script->L, 0, array_count(&items));
+                       lua_createtable(L, 0, array_count(&items));
                        array_foreach(&items, item) {
                                /* push value */
-                               lua_pushlstring(script->L, item->value,
+                               lua_pushlstring(L, item->value,
                                                item->value_len);
                                /* set field */
-                               lua_setfield(script->L, -2, item->key);
+                               lua_setfield(L, -2, item->key);
                        }
                }
        } T_END;
 
        if (ret == -1)
-               return luaL_error(script->L, "%s", error);
+               return luaL_error(L, "%s", error);
 
        /* stack should have table with items */
        return 1;