From e9b39a1ef74f9a3b15de54896ae612637ddc3956 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Mon, 4 Jan 2016 22:59:59 +0100 Subject: degesch: Lua: allow arbitrary userdata properties --- degesch.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/degesch.c b/degesch.c index b66114b..5d1186c 100644 --- a/degesch.c +++ b/degesch.c @@ -8173,8 +8173,38 @@ lua_plugin_property_get (lua_State *L) return 1; } - return luaL_error (L, "%s: %s", - "no such method or property", property_name); + // Or we look for a property set by the user (__gc cannot be overriden) + if (lua_getuservalue (L, 1) != LUA_TTABLE) + lua_pushnil (L); + else + lua_getfield (L, -1, property_name); + return 1; +} + +static int +lua_plugin_property_set (lua_State *L) +{ + luaL_checktype (L, 1, LUA_TUSERDATA); + const char *property_name = luaL_checkstring (L, 2); + luaL_checkany (L, 3); + + // We use the associated value to store user-defined properties + int type = lua_getuservalue (L, 1); + if (type == LUA_TNIL) + { + lua_pop (L, 1); + lua_newtable (L); + lua_pushvalue (L, -1); + lua_setuservalue (L, 1); + } + else if (type != LUA_TTABLE) + return luaL_error (L, "associated value is not a table"); + + // Beware that we do not check for conflicts here; + // if Lua code writes a conflicting field, it is effectively ignored + lua_pushvalue (L, 3); + lua_setfield (L, -2, property_name); + return 0; } static void @@ -8186,6 +8216,9 @@ lua_plugin_create_meta (lua_State *L, const char *name, luaL_Reg *fns) // Emulate properties for convenience lua_pushcfunction (L, lua_plugin_property_get); lua_setfield (L, -2, "__index"); + lua_pushcfunction (L, lua_plugin_property_set); + lua_setfield (L, -2, "__newindex"); + lua_pop (L, 1); } -- cgit v1.2.3-70-g09d2