Re: callbacks with pass-by-value struct args

  • From: Cosmin Apreutesei <cosmin.apreutesei@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 27 Mar 2014 16:02:09 +0200

> Not right now, this is very hard.

I trust you on that :)

> You can send a patch if you're
> brave enough to dive into the code. :-)

I can dive into the code, np. It's the cost/benefit analysis that
bothers me. I foresee a learning curve involving calling conventions,
platform idiosyncrasies[1], empirical experience on speed issues, etc.
That's the cost. The benefit on the other side of that mountain for
me, is being able to put together all the cocoa pieces without C code.
Not the best deal for me at the moment :)

> 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.

That's good to know. It seems that this works for 32bit floats too
(eg. works with a CGRect made of 4 32bit floats). Can I also skip any
number of tail arguments with cdecl and not expect a crash?

> 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.

Looks funky indeed. Seems like structs larger that 16 bytes will get
the stack automatically[2] but I don't see how I can force that with
individual args, and even if I learn now, it doesn't seem like a
valuable skill to acquire :) Can I at least skip tail arguments and
not expect a crash?

[1] wikipedia: x86 programs compiled for different operating system
platforms and/or by different compilers can be incompatible, even if
they both use the "cdecl" convention and do not call out to the
underlying environment
[2] http://people.freebsd.org/~obrien/amd64-elf-abi.pdf, section 3.2.3

Other related posts: