lua-users home
lua-l archive

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

Javier Guerra Giraldez wrote:
> >> in the FFI reference, the lightuserdata->(void *) conversion is
> >> automatic; but i don't see how to do the converse.
> >
> > Well, nobody asked for it. :-)
> i can haz?

Only if I find a good place for it in the API. Maybe a special
type for ffi.cast.

> >> am i missing some downside of a (void *)->lightuserdata conversion?
> >
> > Lightuserdata is generally only needed for the classic Lua/C API,
> > so it would be a bit backwards to suggest its use for new stuff.
> in my eyes, a lightuserdata value is a useful opaque value; just like
> pointers without arithmetic.  while a number can do the same, the
> implied semantics are different.

If you're already using the FFI, then a void pointer is exactly
what you want. You can't dereference a void pointer or do pointer
arithmetic with it.

> also, FFI does cast the lightuserdata back to the same pointer.  how
> could i do it with a number?  i would have to store the boxed pointer
> somewhere, right?  (update: i see there's a conversion between C
> number and pointer, i'll play a little with the uintptr_t cast)

See the tutorial. And use intptr_t, the conversion is faster.

> finally, wouldn't it be easier for the JIT optimizer to recognize that
> a lightuserdata value have a lot less possible codepaths? since it can
> only be passed around...