[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: [LUAJit] ffi and pointer math
- From: Mike Pall <mikelu-1105@...>
- Date: Wed, 11 May 2011 13:01:44 +0200
Robert G. Jakabosky wrote:
> On Tuesday 10, Kaj Eijlers wrote:
> > local verts = ffi.new("PTC")
> > local vertOffs = verts
> I think 'vertOffs' is the same value as 'verts', so no copy.
Well, of course it is. In dynamic languages, values have types,
but variables do not. Variables hold references to values and only
give them a name. An assignment 'y = x' is just giving the same
value another name (i.e. pointless in the above example).
> You might be able to do what you really need with just this:
Yes, this creates a reference to a struct. Which of course readily
converts to a pointer, e.g. for a function argument.
> But if you really need a pointer to the field of a struct, then something
> like this might work:
> -- get byte pointer to first array element
> local ptr = ffi.cast("unsigned char*", verts)
> -- get field's byte offset.
> local field_offset = ffi.offsetof("PTC", "TexCoord");
> -- cast pointer to field back to correct type.
> local tex = ffi.cast("TexCoords *", ptr + field_offset);
Replace "TexCoords *" with "TexCoords &" and that's exactly what
verts.TexCoord does. So don't bother with the pointer artistry.
BTW: *sigh* Lua is a word, JIT is an acronym. Thus LuaJIT is
spelled 'LuaJIT' only. My next project will be in all lowercase. :-/