lua-users home
lua-l archive

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


I'm trying to pass a struct by value to a varargs API call over the LuaJIT FFI, like so:

ffi.cdef [[
    typedef void *id;
    typedef void *SEL;
    id objc_msgSend(id theReceiver, SEL theSelector, ...);
    typedef struct _NSRect { double x, y, width, height; } NSRect;
local r, s, a, b, rect = ...,"NSRect", {0,0,32,32}})
ffi.C.objc_msgSend(r, s, rect, a, b)

What happens is the arguments seem to get garbled, and as best I can tell, the reason is that the implementation of objc_msgSend is expecting to pull the NSRect struct off the stack, but LuaJIT is passing a pointer, because it "infers" that objc_msgSend is expecting a pointer based on the fact that I'm passing a struct.  In fact, LuaJIT seems to explicitly replace struct types with corresponding pointer types in ccall_ctid_vararg.

Is there some way to pass the struct by value?  Is there a good explanation for the current behavior?  I'm trying to avoid writing any C stubs or wrappers at all when calling C APIs. :)