From 4d661815063e8e33c7b71abe44f5206a5cb00786 Mon Sep 17 00:00:00 2001 From: buffet Date: Wed, 8 Jan 2020 17:33:01 +0000 Subject: [PATCH] Add equality for Lua types --- include/luak/luak.h | 1 + kiwmi/luak/kiwmi_cursor.c | 3 +++ kiwmi/luak/kiwmi_keyboard.c | 3 +++ kiwmi/luak/kiwmi_lua_callback.c | 3 +++ kiwmi/luak/kiwmi_output.c | 3 +++ kiwmi/luak/kiwmi_server.c | 3 +++ kiwmi/luak/kiwmi_view.c | 3 +++ kiwmi/luak/luak.c | 14 ++++++++++++++ 8 files changed, 33 insertions(+) diff --git a/include/luak/luak.h b/include/luak/luak.h index acc3c03..4495387 100644 --- a/include/luak/luak.h +++ b/include/luak/luak.h @@ -21,6 +21,7 @@ struct kiwmi_lua { }; int luaK_callback_register_dispatch(lua_State *L); +int luaK_usertype_ref_equal(lua_State *L); struct kiwmi_lua *luaK_create(struct kiwmi_server *server); bool luaK_dofile(struct kiwmi_lua *lua, const char *config_path); void luaK_destroy(struct kiwmi_lua *lua); diff --git a/kiwmi/luak/kiwmi_cursor.c b/kiwmi/luak/kiwmi_cursor.c index 35242f6..14b5713 100644 --- a/kiwmi/luak/kiwmi_cursor.c +++ b/kiwmi/luak/kiwmi_cursor.c @@ -231,5 +231,8 @@ luaK_kiwmi_cursor_register(lua_State *L) luaL_newlib(L, kiwmi_cursor_events); lua_setfield(L, -2, "__events"); + lua_pushcfunction(L, luaK_usertype_ref_equal); + lua_setfield(L, -2, "__eq"); + return 0; } diff --git a/kiwmi/luak/kiwmi_keyboard.c b/kiwmi/luak/kiwmi_keyboard.c index 2a427b5..45bba68 100644 --- a/kiwmi/luak/kiwmi_keyboard.c +++ b/kiwmi/luak/kiwmi_keyboard.c @@ -191,5 +191,8 @@ luaK_kiwmi_keyboard_register(lua_State *L) luaL_newlib(L, kiwmi_keyboard_events); lua_setfield(L, -2, "__events"); + lua_pushcfunction(L, luaK_usertype_ref_equal); + lua_setfield(L, -2, "__eq"); + return 0; } diff --git a/kiwmi/luak/kiwmi_lua_callback.c b/kiwmi/luak/kiwmi_lua_callback.c index 74fdfc5..6d2517e 100644 --- a/kiwmi/luak/kiwmi_lua_callback.c +++ b/kiwmi/luak/kiwmi_lua_callback.c @@ -76,6 +76,9 @@ luaK_kiwmi_lua_callback_register(lua_State *L) lua_setfield(L, -2, "__index"); luaL_setfuncs(L, kiwmi_lua_callback_methods, 0); + lua_pushcfunction(L, luaK_usertype_ref_equal); + lua_setfield(L, -2, "__eq"); + return 0; } diff --git a/kiwmi/luak/kiwmi_output.c b/kiwmi/luak/kiwmi_output.c index 37926e7..167fc9a 100644 --- a/kiwmi/luak/kiwmi_output.c +++ b/kiwmi/luak/kiwmi_output.c @@ -179,5 +179,8 @@ luaK_kiwmi_output_register(lua_State *L) luaL_newlib(L, kiwmi_output_events); lua_setfield(L, -2, "__events"); + lua_pushcfunction(L, luaK_usertype_ref_equal); + lua_setfield(L, -2, "__eq"); + return 0; } diff --git a/kiwmi/luak/kiwmi_server.c b/kiwmi/luak/kiwmi_server.c index 592cce6..c4ebe48 100644 --- a/kiwmi/luak/kiwmi_server.c +++ b/kiwmi/luak/kiwmi_server.c @@ -273,5 +273,8 @@ luaK_kiwmi_server_register(lua_State *L) luaL_newlib(L, kiwmi_server_events); lua_setfield(L, -2, "__events"); + lua_pushcfunction(L, luaK_usertype_ref_equal); + lua_setfield(L, -2, "__eq"); + return 0; } diff --git a/kiwmi/luak/kiwmi_view.c b/kiwmi/luak/kiwmi_view.c index 28f177d..d257291 100644 --- a/kiwmi/luak/kiwmi_view.c +++ b/kiwmi/luak/kiwmi_view.c @@ -267,5 +267,8 @@ luaK_kiwmi_view_register(lua_State *L) luaL_newlib(L, kiwmi_view_events); lua_setfield(L, -2, "__events"); + lua_pushcfunction(L, luaK_usertype_ref_equal); + lua_setfield(L, -2, "__eq"); + return 0; } diff --git a/kiwmi/luak/luak.c b/kiwmi/luak/luak.c index c16b87e..44621e8 100644 --- a/kiwmi/luak/luak.c +++ b/kiwmi/luak/luak.c @@ -47,6 +47,20 @@ luaK_callback_register_dispatch(lua_State *L) return 1; } +int +luaK_usertype_ref_equal(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TUSERDATA); + luaL_checktype(L, 2, LUA_TUSERDATA); + + void *a = *(void **)lua_touserdata(L, 1); + void *b = *(void **)lua_touserdata(L, 2); + + lua_pushboolean(L, a == b); + + return 1; +} + struct kiwmi_lua * luaK_create(struct kiwmi_server *server) {