I've got a userdata
object I've created. I've defined the object like this:
static const luaL_Reg object_methods[] = {
{"__gc", pbject_gc},
{"__tostring", object_print},
{ NULL, NULL }
};
static int
object_new (lua_State *L)
{
Object *Object = lua_newuserdata(L, sizeof(Object));
luaL_getmetatable(L, "Object");
lua_setmetatable(L, -2);
return 1;
}
int
luaopen_Object (lua_State *L)
{
/* create metatable */
luaL_newmetatable(L, "Object");
/* metatable.__index = metatable */
lua_pushvalue(L, -1);
lua_setfield(L, -1, "__index");
/* register methods */
luaL_setfuncs(L, object_methods, 0);
/* Push a function: Object(...) => new Object */
lua_pushcfunction(L, object_new);
return 1;
}
I want to make a different object_new
, let's say object_s_new
for 'special new'. In object_s_new
, how do inject a method into the object metatable?
I've tried just calling luaL_getmetatable
and editing that table, but, as I expected, that edits the metatable for all objects using it. I want the injected method to be local to just objects created by object_s_new
.
Is there anyway to do this without creating a completely separate metatable? Does that affect identity in any weird way with Lua? For instance, can I have a single struct Script
as two different userdata
types?