Re: callbacks with pass-by-value struct args

  • From: Mike Pall <mike-1403@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 27 Mar 2014 12:13:10 +0100

Cosmin Apreutesei wrote:
> Do you have any plans to support callbacks with pass-by-value struct
> args in the future?

Not right now, this is very hard. You can send a patch if you're
brave enough to dive into the code. :-)

> This is a show-stopper for bridging Cocoa via ffi because subclassing
> is done via callbacks and a few essential methods have pass-by-value
> struct args.

Most pass-by-value conventions are rather simple and effectively
split the struct up into consecutive registers or stack slots. So
you could just lie to the FFI and declare the callback functions
as receiving the individual struct fields instead of the struct
itself.

There are some complications on POSIX/x64, which uses rather funky
calling conventions. E.g. a struct of two 32 bit quantities is
passed in a single 64 bit register or stack slot. It gets even
more complicated when floating point types come into play. But the
above workaround is fine for most simple cases, e.g. when passing
a struct of two pointers.

--Mike

Other related posts: