]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-lua: Add flag manipulation functions
authorAki Tuomi <aki.tuomi@dovecot.fi>
Mon, 30 Jul 2018 10:12:47 +0000 (13:12 +0300)
committerVille Savolainen <ville.savolainen@dovecot.fi>
Wed, 14 Nov 2018 12:11:00 +0000 (14:11 +0200)
src/lib-lua/dlua-dovecot.c
src/lib-lua/test-lua.c

index ac5d7e0f5e0f883f044cafc1e997e672915b7ad1..d6d8b49d88f4274981e0e0df2c25bac784fbf0ec 100644 (file)
@@ -500,12 +500,47 @@ static int dlua_i_error(lua_State *L)
        return 0;
 }
 
+static int dlua_has_flag(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       /* we rather deal with unsigned value here */
+       lua_Integer value = luaL_checkinteger(script->L, 1);
+       lua_Integer flag = luaL_checkinteger(script->L, 2);
+
+       lua_pushboolean(script->L, (value & flag) == flag);
+       return 1;
+}
+
+static int dlua_set_flag(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       lua_Integer value = luaL_checkinteger(script->L, 1);
+       lua_Integer flag = luaL_checkinteger(script->L, 2);
+
+       lua_pushinteger(script->L, value | flag);
+       return 1;
+}
+
+static int dlua_clear_flag(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       lua_Integer value = luaL_checkinteger(script->L, 1);
+       lua_Integer flag = luaL_checkinteger(script->L, 2);
+
+       lua_pushinteger(script->L, value & (~flag));
+       return 1;
+}
+
+
 static luaL_Reg lua_dovecot_methods[] = {
        { "i_debug", dlua_i_debug },
        { "i_info", dlua_i_info },
        { "i_warning", dlua_i_warning },
        { "i_error", dlua_i_error },
        { "event", dlua_event_new },
+       { "has_flag", dlua_has_flag },
+       { "set_flag", dlua_set_flag },
+       { "clear_flag", dlua_clear_flag },
        { NULL, NULL }
 };
 
index 67c34c9fd4c7057df0303ffa4ad5732c483768a3..7de3eaaae939587562aeda9209ec3bfbf804d186 100644 (file)
@@ -3,6 +3,21 @@
 #include "test-lib.h"
 #include "dlua-script-private.h"
 
+static int dlua_test_assert(lua_State *L)
+{
+       struct dlua_script *script = dlua_script_from_state(L);
+       const char *what = luaL_checkstring(script->L, 1);
+       bool cond = lua_toboolean(script->L, 2);
+
+       if (!cond) {
+               lua_Debug ar;
+               i_assert(lua_getinfo(L, ">Sl", &ar) == 0);
+               test_assert_failed(what, ar.source, ar.currentline);
+       }
+
+       return 0;
+}
+
 static void test_lua(void)
 {
        static const char *luascript =
@@ -13,7 +28,19 @@ static void test_lua(void)
 "  return 0\n"
 "end\n"
 "function lua_function()\n"
+"end\n"
+"function lua_test_flags()\n"
+"  local flag = 0\n"
+"  flag = dovecot.set_flag(flag, 2)\n"
+"  flag = dovecot.set_flag(flag, 4)\n"
+"  flag = dovecot.set_flag(flag, 16)\n"
+"  test_assert(\"has_flag(flag, 8) == false\", dovecot.has_flag(flag, 8) == false)\n"
+"  test_assert(\"has_flag(flag, 4) == true\", dovecot.has_flag(flag, 4) == true)\n"
+"  flag = dovecot.clear_flag(flag, 4)\n"
+"  test_assert(\"has_flag(flag, 4) == false\", dovecot.has_flag(flag, 4) == false)\n"
+"  test_assert(\"has_flag(flag, 16) == true\", dovecot.has_flag(flag, 16) == true)\n"
 "end\n";
+
        const char *error = NULL;
        struct dlua_script *script = NULL;
 
@@ -21,9 +48,16 @@ static void test_lua(void)
 
        test_assert(dlua_script_create_string(luascript, &script, NULL, &error) == 0);
        dlua_dovecot_register(script);
+
+       lua_pushcfunction(script->L, dlua_test_assert);
+       lua_setglobal(script->L, "test_assert");
+
        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);
+
        dlua_script_unref(&script);
 
        test_end();