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 }
};
#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 =
" 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;
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();