I create a userobject in C, and as part of this I create a metatable to override methods like __tostring
, __index
, etc. I noticed, though, that it's possible to retrieve the object's metatable through getmetatable()
. It seems that it's possible to make getmetatable()
return any arbitrary table, if my metatable has a __metatable
key. I'd like getmetatable()
to return nil
, so my first thought was to add a __metatable
key with nil
, but that obviously doesn't work as it doesn't actually add the __metatable
key to the metatable:
struct my_obj* obj = (struct my_obj*)lua_newuserdata(L, sizeof(struct my_obj));
if (luaL_newmetatable(L, "mymetatable") != 0) {
lua_pushliteral(L, "__metatable");
lua_pushnil(L);
lua_rawset(L, -3); /* will not work, can not add a nil... */
}
lua_setmetatable(L, -2);
Can this be done somehow, or do I have to stick with stuffing an empty table into __metatable
?