lua: fix otable __newindex

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-08-11 23:33:35 +02:00
parent 1fbe4f0d5e
commit ab66b87377
3 changed files with 41 additions and 9 deletions

View File

@ -502,7 +502,7 @@ function client.toggletag(target, c)
local sel = c or capi.client.focus_get();
-- Check that tag and client screen are identical
if sel and sel.screen == target.screen then
local tags = client.tags
local tags = sel.tags
if tags[target] then
tags[target] = nil
else

39
lua.c
View File

@ -411,6 +411,44 @@ luaA_otable_index(lua_State *L)
return 1;
}
/** Object table.
* This table can use safely object as key.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
static int
luaA_otable_newindex(lua_State *L)
{
void **obj, **v;
if((obj = lua_touserdata(L, 2)))
{
printf("looking for %p\n", *obj);
/* begins at nil */
lua_pushnil(L);
while(lua_next(L, 1))
{
if((v = lua_touserdata(L, -2))
&& *v == *obj)
{
printf("found %p, replace\n", *v);
/* remove value */
lua_pop(L, 1);
/* push new value on top */
lua_pushvalue(L, 3);
/* set in table key = value */
lua_rawset(L, 1);
return 0;
}
/* removes 'value'; keeps 'key' for next iteration */
lua_pop(L, 1);
}
}
lua_rawset(L, 1);
return 0;
}
/** Initialize the Lua VM
*/
void
@ -426,6 +464,7 @@ luaA_init(void)
static const struct luaL_reg otable_meta[] =
{
{ "__index", luaA_otable_index },
{ "__newindex", luaA_otable_newindex },
{ NULL, NULL }
};
static const struct luaL_reg awesome_lib[] =

9
lua.h
View File

@ -232,14 +232,7 @@ luaA_otable_new(lua_State *L)
{
/* Our object */
lua_newtable(L);
/* The meta table */
lua_newtable(L);
lua_pushcfunction(L, luaA_otable_index);
/* Register index into the metatable */
lua_setfield(L, -2, "__index");
/* Set the meta table */
lua_setmetatable(L, -2);
return 1;
return luaA_settype(L, "otable");
}
void luaA_init(void);