lua: otable can now be index by object values

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-08-17 07:52:04 +02:00
parent 17d1e6b741
commit 065d7f8a86
3 changed files with 28 additions and 21 deletions

View File

@ -998,6 +998,7 @@ luaA_client_tags(lua_State *L)
tag_array_t *tags; tag_array_t *tags;
tag_t **tag; tag_t **tag;
client_t **c = luaA_checkudata(L, 1, "client"); client_t **c = luaA_checkudata(L, 1, "client");
int j = 0;
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {
@ -1012,19 +1013,17 @@ luaA_client_tags(lua_State *L)
tag_client(*c, *tag); tag_client(*c, *tag);
lua_pop(L, 1); lua_pop(L, 1);
} }
lua_pop(L, 1);
} }
else
{ tags = &globalconf.screens[(*c)->screen].tags;
tags = &globalconf.screens[(*c)->screen].tags; luaA_otable_new(L);
luaA_otable_new(L); for(int i = 0; i < tags->len; i++)
for(int i = 0; i < tags->len; i++) if(is_client_tagged(*c, tags->tab[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_rawseti(L, -2, ++j);
luaA_tag_userdata_new(L, tags->tab[i]); }
lua_rawset(L, -3);
}
}
return 1; return 1;
} }

12
lua.c
View File

@ -410,9 +410,19 @@ luaA_otable_index(lua_State *L)
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, 1)) 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)) if((v = lua_touserdata(L, -2))
&& *v == *obj) && *v == *obj)
/* return value */
return 1; 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 */ /* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1); lua_pop(L, 1);
} }

14
tag.c
View File

@ -290,6 +290,7 @@ static int
luaA_tag_clients(lua_State *L) luaA_tag_clients(lua_State *L)
{ {
tag_t **tag = luaA_checkudata(L, 1, "tag"); tag_t **tag = luaA_checkudata(L, 1, "tag");
client_array_t *clients = &(*tag)->clients;
int i; int i;
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
@ -307,15 +308,12 @@ luaA_tag_clients(lua_State *L)
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
else
luaA_otable_new(L);
for(i = 0; i < clients->len; i++)
{ {
client_array_t *clients = &(*tag)->clients; luaA_client_userdata_new(L, clients->tab[i]);
lua_newtable(L); lua_rawseti(L, -2, i + 1);
for(i = 0; i < clients->len; i++)
{
luaA_client_userdata_new(L, clients->tab[i]);
lua_rawseti(L, -2, i + 1);
}
} }
return 1; return 1;