From 065d7f8a861a2293990a976a5287eb3d116aea95 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Sun, 17 Aug 2008 07:52:04 +0200 Subject: [PATCH] lua: otable can now be index by object values Signed-off-by: Julien Danjou --- client.c | 23 +++++++++++------------ lua.c | 12 +++++++++++- tag.c | 14 ++++++-------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/client.c b/client.c index d8fd37b3..831e1c02 100644 --- a/client.c +++ b/client.c @@ -998,6 +998,7 @@ luaA_client_tags(lua_State *L) tag_array_t *tags; tag_t **tag; client_t **c = luaA_checkudata(L, 1, "client"); + int j = 0; if(lua_gettop(L) == 2) { @@ -1012,19 +1013,17 @@ luaA_client_tags(lua_State *L) tag_client(*c, *tag); lua_pop(L, 1); } + lua_pop(L, 1); } - else - { - tags = &globalconf.screens[(*c)->screen].tags; - luaA_otable_new(L); - for(int i = 0; i < tags->len; i++) - if(is_client_tagged(*c, tags->tab[i])) - { - luaA_tag_userdata_new(L, tags->tab[i]); - luaA_tag_userdata_new(L, tags->tab[i]); - lua_rawset(L, -3); - } - } + + tags = &globalconf.screens[(*c)->screen].tags; + luaA_otable_new(L); + for(int i = 0; i < tags->len; i++) + if(is_client_tagged(*c, tags->tab[i])) + { + luaA_tag_userdata_new(L, tags->tab[i]); + lua_rawseti(L, -2, ++j); + } return 1; } diff --git a/lua.c b/lua.c index b03750db..c4532e87 100644 --- a/lua.c +++ b/lua.c @@ -410,9 +410,19 @@ luaA_otable_index(lua_State *L) lua_pushnil(L); while(lua_next(L, 1)) { + /* check the key against the key if the key is a userdata, + * otherwise check it again the value. */ if((v = lua_touserdata(L, -2)) - && *v == *obj) + && *v == *obj) + /* return value */ return 1; + else if((v = lua_touserdata(L, -1)) + && *v == *obj) + { + /* return key */ + lua_pop(L, 1); + return 1; + } /* removes 'value'; keeps 'key' for next iteration */ lua_pop(L, 1); } diff --git a/tag.c b/tag.c index baf2bc17..417257e1 100644 --- a/tag.c +++ b/tag.c @@ -290,6 +290,7 @@ static int luaA_tag_clients(lua_State *L) { tag_t **tag = luaA_checkudata(L, 1, "tag"); + client_array_t *clients = &(*tag)->clients; int i; if(lua_gettop(L) == 2) @@ -307,15 +308,12 @@ luaA_tag_clients(lua_State *L) lua_pop(L, 1); } } - else + + luaA_otable_new(L); + for(i = 0; i < clients->len; i++) { - client_array_t *clients = &(*tag)->clients; - lua_newtable(L); - for(i = 0; i < clients->len; i++) - { - luaA_client_userdata_new(L, clients->tab[i]); - lua_rawseti(L, -2, i + 1); - } + luaA_client_userdata_new(L, clients->tab[i]); + lua_rawseti(L, -2, i + 1); } return 1;