kapodamy
kapodamy

Reputation: 165

Reuse lua_newuserdata() returned pointer

¿Is posible reuse lua_newuserdata() returned pointer? The idea is not allocate a new userdatum every time for the same object and allow equality check (example) done by lua.

obj1 = c_api__foobar();
obj2 = c_api__foobar();

if obj1 == obj2 then
    print("equal")
else
    print("they are supposed to be equal")
end

Upvotes: 0

Views: 90

Answers (1)

kapodamy
kapodamy

Reputation: 165

well look is not possible, attemping to reuse the pointer will throw something like calling 'c_api__foobar' on bad self (FOOBAR expected, found FOOBAR) error.

static int expose_foobar(lua_State* L) {
   void* existing_userdata = /* previous value returned by lua_newuserdata() */;
   LUA.luaL_getmetatable(L, "FOOBAR")
   LUA.lua_setmetatable(L, -2);
   return 1;
}

tested in lua 5.4

Upvotes: 0

Related Questions