From: Guo Rui Date: Tue, 21 Jul 2009 16:27:55 +0000 (+0800) Subject: Push broker to lua script, instead of the pointer itself. X-Git-Url: https://repo.or.cz/w/screen-lua.git/commitdiff_plain/7375dfca86133b400588cca235cd3175181b56b3 Push broker to lua script, instead of the pointer itself. --- diff --git a/src/lua.c b/src/lua.c index 81197c9..43c412f 100644 --- a/src/lua.c +++ b/src/lua.c @@ -146,12 +146,16 @@ LuaFreeHandler(lua_State *L, struct lua_handler **lh) static type * \ check_##name(lua_State *L, int index) \ { \ - type **var; \ + struct broker **broker; \ + type *var; \ luaL_checktype(L, index, LUA_TUSERDATA); \ - var = (type **) luaL_checkudata(L, index, #name); \ - if (!var || !*var) \ + broker = (struct broker **) luaL_checkudata(L, index, #name); \ + if (broker) { \ + var = (type *)get_broker_obj(broker); \ + } \ + if (!broker || !*broker) \ luaL_typerror(L, index, #name); \ - return *var; \ + return var; \ } #define PUSH_TYPE(name, type) \ @@ -162,9 +166,9 @@ push_##name(lua_State *L, type **t) \ lua_pushnil(L); \ else \ { \ - type **r; \ - r = (type **)lua_newuserdata(L, sizeof(type *)); \ - *r = *t; \ + struct broker **r; \ + r = (struct broker **)lua_newuserdata(L, sizeof(struct broker *)); \ + *r = get_obj_broker(*t); \ luaL_getmetatable(L, #name); \ lua_setmetatable(L,-2); \ } \ @@ -238,11 +242,13 @@ static int Xet_call (lua_State *L) /* for get: stack has userdata, index, lightuserdata */ /* for set: stack has userdata, index, value, lightuserdata */ const struct Xet_reg *m = (const struct Xet_reg *)lua_touserdata(L, -1); /* member info */ + void *obj; lua_pop(L, 1); /* drop lightuserdata */ luaL_checktype(L, 1, LUA_TUSERDATA); if (m->absolute) return m->absolute(L); - return m->func(L, *(char**)lua_touserdata(L, 1) + m->offset); + obj = get_broker_obj((struct broker **)lua_touserdata(L, 1)); + return m->func(L, (char *)obj + m->offset); } static int index_handler (lua_State *L) @@ -319,7 +325,21 @@ struct_register(lua_State *L, const char *name, const luaL_reg fn_methods[], con /** }}} */ /** Callback {{{ */ -PUSH_TYPE(callback, struct listener) +static void +push_callback(lua_State *L, struct listener **t) +{ + if (!t || !*t) + lua_pushnil(L); + else + { + struct listener **r; + r = (struct listener **)lua_newuserdata(L, sizeof(struct listener *)); + *r = *t; + luaL_getmetatable(L, "callback"); + lua_setmetatable(L,-2); + } +} + static int internal_unhook(lua_State *L, int warn) @@ -1157,8 +1177,8 @@ LuaShowErr(lua_State *L) struct display *d = display; unsigned int len; const char *message = luaL_checklstring(L, -1, &len); - LMsg(0, "%s", message ? message : "Unknown error"); lua_pop(L, 1); + LMsg(0, "%s", message ? message : "Unknown error"); display = d; }