lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

I have a question/suggestion regarding inheritance.

Given a metatable defined in C with attached methods as in:

luaL_newmetatable(L, "useful");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
luaL_setfuncs(L, stuff, 0);

One can use this metatable by direct access to the registry and set it as the metatable for a new userdata instance using luaL_setmetatable ( or luaL_getmetatable, lua_setmetatable). This way, one can check/test for compliance using the luaL_checkudata function.

void **pinstance = (void **)lua_newuserdata(L, sizeof(void *));
luaL_setmetatable(L, "useful");
...
luaL_checkudata(L, -1, "useful"); // OK -- passes test

However, one can also defined a new table and set as metatable one of the already available metatables using luaL_setmetatable. But, if one sets this new table as the metatable for a specific instance of userdata using lua_setmetatable, one cannot use luaL_checkudata function anymore since in the definition of that C function, it is not possible to check the metatable of the metatable.

lua_newtable(L);
luaL_setmetatable(L, "useful");
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
...
luaL_checkudata(L, -1, "useful"); // FAILS -- diferrent metatable
// and
lua_getmetatable(L, -1);
luaL_checkudata(L, -1, "useful"); // FAILS -- not a userdata
// HOW should I check for this?

My point is that in Lua, inheritance is performed by means of tables (metatables), but in C the function checks not only for inheritance based in tables but also in the type of the actual instance, in my example a userdata. However, given that one has a userdata in first place, and that check has been successful, inheritance check should be performed next based only on tables (metatables).

Thanks,

// Carlos
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org