0

¿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
kapodamy
  • 65
  • 8
  • 1
    Well, `c_api__foobar()` is presumably under your control. So it can do whatever it wants, including storing the value returned from `lua_newuserdata` the first time and returning it on a second call. How you go about doing that is up to you. – Nicol Bolas Sep 12 '22 at 15:29

1 Answers1

0

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

kapodamy
  • 65
  • 8