From ab66b873778df61ccd2ef27364cb24ea541e9182 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Mon, 11 Aug 2008 23:33:35 +0200 Subject: [PATCH] lua: fix otable __newindex Signed-off-by: Julien Danjou --- lib/awful.lua.in | 2 +- lua.c | 39 +++++++++++++++++++++++++++++++++++++++ lua.h | 9 +-------- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/awful.lua.in b/lib/awful.lua.in index 15acc9003..1c6a02d3b 100644 --- a/lib/awful.lua.in +++ b/lib/awful.lua.in @@ -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 diff --git a/lua.c b/lua.c index 396bd52cb..1a265c8f6 100644 --- a/lua.c +++ b/lua.c @@ -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[] = diff --git a/lua.h b/lua.h index bb672b8aa..ab5ee7ebf 100644 --- a/lua.h +++ b/lua.h @@ -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);