lua-users home
lua-l archive

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


Excerpts from Thomas Jericke's message of 2014-04-29 15:01:42 +0200:
> Sounds interesting, just out of interest, what happens if the userdata 
> has been collected in the meantime and a new userdata with the same pointer
> has been allocated as well. Will nil be pushed or the new userdata 
> (which may be of a different kind).

The idea is that programmer knows what he is doing when using pudata.

Which generally means, he manages the lifetime of userdata somewhere
(say, some other pointer translation table). While the nil case will
probably work semi-reliably, it should _not_ be relied upon. Only as
means of assert(!lua_isnil(L, -1)). Because this sanity is only specific
to this polyfill - real code does something akin to:

LUA_API void lua_pushuserdata_unsafe(lua_State *L, void *p) {
  lua_lock(L);
  setuvalue(L->top, p);
  api_incr_top(L);
  lua_unlock(L);
}
#define lua_pushuserdata lua_pushuserdata_unsafe

(For lua 5.2, 5.1 or LuaJIT will have something slightly different.)

I'm using macros so pudata.h can check for existence of native
implemenetation.

This will do evil stuff if *p is a dangling pointer (ie not already
anchored) somewhere in Lua.