From 9566defa93499118bcc4f27f7ea69f4e5b937c37 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 30 Sep 2016 11:02:09 +0200 Subject: [PATCH] C: Add a .data table to all C objects This table is "just a normal Lua table". Lua code can use it in whatever way it wants to store things related to a given C object. An object (userdata) already references a "plain Lua table" via lua_setuservalue() / lua_setfenv(). This is used to keep a reference to signal functions that are connected to an object. The signal code only uses lightuserdata keys in this table. This commit adds an entry with key "data" to this table which just references another table. This is the table that is made available as .data. Via this .data property, Lua code can add own properties to C objects without having to use, for example, weak tables. The weak tables have the downside that they produce a leak if the value references the key. The new .data property does not have any such problem (no weak references are involved). This new data property is not documented, because I'd have to touch lots of files and I'm lazy. Signed-off-by: Uli Schlachter --- common/luaclass.c | 11 +++++++++++ common/luaobject.h | 2 ++ 2 files changed, 13 insertions(+) diff --git a/common/luaclass.c b/common/luaclass.c index 2f8f2b9c..debfa369 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -451,6 +451,17 @@ luaA_class_index(lua_State *L) lua_class_property_t *prop = luaA_class_property_get(L, class, 2); + /* Is this the special 'data' property? This is available on all objects and + * thus not implemented as a lua_class_property_t. + */ + if (A_STREQ(attr, "data")) + { + luaA_checkudata(L, 1, class); + luaA_getuservalue(L, 1); + lua_getfield(L, -1, "data"); + return 1; + } + /* Property does exist and has an index callback */ if(prop) { diff --git a/common/luaobject.h b/common/luaobject.h index b57ec43a..ed6ecb5c 100644 --- a/common/luaobject.h +++ b/common/luaobject.h @@ -173,6 +173,8 @@ int luaA_object_emit_signal_simple(lua_State *); lua_newtable(L); \ lua_newtable(L); \ lua_setmetatable(L, -2); \ + lua_newtable(L); \ + lua_setfield(L, -2, "data"); \ luaA_setuservalue(L, -2); \ lua_pushvalue(L, -1); \ luaA_class_emit_signal(L, &(lua_class), "new", 1); \