lua-users home
lua-l archive

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


when I set a metatable on a C closure, it seems to spread to all Lua functions, not only to the specific closure at hand. The code I use is basically this:

    objtype* obj = (objtype*)lua_newuserdata(L, sizeof(objtype))
    luaL_getmetatable(L, "POBJECT")
    lua_setmetatable(L, -2)

    lua_pushcclosure(L, <lua.lua_CFunction>asfunc_call, 1)
    luaL_getmetatable(L, "POBJECT")
    lua_setmetatable(L, -2)

I'm just setting the metatable on both the user data object and the closure so that both behave as expected when handled separately. In particular, I need to supply indexing callbacks for both, but I also use other meta methods like "__tostring".

After that, when I declare a new (regular) function in Lua code and call tostring() on it, it calls into the metatable of the above closure. I would have expected that other functions are not impacted by a metatable that I set on a single C function.

Is that the expected behaviour? Is there a way to define a metatable only for a specific C function?