lua-users home
lua-l archive

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

> >> in Lua-- it isn't exactly safe.  For example on my project the Lua 
> >> number type is 32-bit float, and using that type to work 
> with 32-bit 
> >> pointers and integers is dubious.
> >
> >i had thoughts along the same lines... is there any way around this 
> >(not using userdata which - as i understand - actually is a void*)?
> Userdata is the way to send C pointers to Lua (and back). It 
> was designed from the start for this use. Why try to avoid it? --lhf

Because for every user data pushed to Lua, memory is allocated which
needs to later be garbage collected.  One might argue that the userdata
could be created once and lua_ref()'ed, and yes, it could, but it makes
the programming far more painful for simple, changing pointer pass
thrus.  That is why the LuaState distribution provides the LUA_TPOINTER
type (the changes to Lua to achieve a LUA_TPOINTER type are actually
very, very few in number).  A void* pointer can be pushed to Lua without
anything more than a stack entry being created, just like a number.
Although Amped didn't have the LUA_TPOINTER type, it did have a need to
pass a pointer through Lua to a C callback.  Lua didn't need to know
anything about the pointer... That is, it didn't need to be user data.

Userdata is powerful, and it definitely has its uses.  I used userdata
in Amped, but it was used at a far more limited scope than the pointer
passing I needed to be doing.