lua-users home
lua-l archive

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



I was just thinking about tidying up some code and want to do the following.

cache a pointer returned from lua_touserdata() on the C side (I am already doing this anyway)

at some point later on, push that userdata onto the stack to be used as a table look up key, I can guarantee that the userdata will not have been GCd at this point. At the moment I am using a unique integer as a table key, but it would be simpler to use the userdata instead.

however there is no lua_pushuserdata()

Is it safe just to use a modified version of lua_newuserdata() that doesnt allocate any new userdata but takes a previously allocated pointer and pushes it on the stack?

EG  some untested 5.0 code :)

LUA_API void lua_pushuserdata (lua_State *L, void *data) {
 lua_lock(L);
 luaC_checkGC(L);
 setuvalue(L->top, ((Udata *)(data))-1);
 api_incr_top(L);
 lua_unlock(L);
}

or is there some more magic surrounding userdata I should be aware of.

--

Kriss

http://XIXs.com -><- http://www.WetGenes.com