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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-09-30 11:02:09 +02:00
parent 502b4139ba
commit 9566defa93
2 changed files with 13 additions and 0 deletions

View File

@ -451,6 +451,17 @@ luaA_class_index(lua_State *L)
lua_class_property_t *prop = luaA_class_property_get(L, class, 2); 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 */ /* Property does exist and has an index callback */
if(prop) if(prop)
{ {

View File

@ -173,6 +173,8 @@ int luaA_object_emit_signal_simple(lua_State *);
lua_newtable(L); \ lua_newtable(L); \
lua_newtable(L); \ lua_newtable(L); \
lua_setmetatable(L, -2); \ lua_setmetatable(L, -2); \
lua_newtable(L); \
lua_setfield(L, -2, "data"); \
luaA_setuservalue(L, -2); \ luaA_setuservalue(L, -2); \
lua_pushvalue(L, -1); \ lua_pushvalue(L, -1); \
luaA_class_emit_signal(L, &(lua_class), "new", 1); \ luaA_class_emit_signal(L, &(lua_class), "new", 1); \