<div dir="ltr">I spent some time hacking around on this from a library perspective when I had to interoperate with a bunch of Objective C on a 64-bit mac as many of the core library functions you need to FFI out to pass around pairs of <font face="courier new, monospace">Int32</font>s as a struct small enough by the x64 ABI to get shoehorned into one register, and as I was programmatically cloning Objective C APIs via template haskell I couldn't use the usual clunky C shims.<div>
<br></div><div>What I was doing was just using <font face="courier new, monospace">libffi</font> with a lot of work to cache the results of <span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="courier new, monospace">ffi_prep_cif</font></span><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif"> for each signature.</font></span></div>
<div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif"><br></font></span></div><div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif">It worked reasonably well for my purposes, but my need for it vanished and I abandoned the code in the middle of refactoring it for grander things.</font></span></div>
<div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif"><br></font></span></div><div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif">So if nothing else, you can at least take this as a vote of confidence that your idea isn't crazy. =)</font></span></div>
<div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif"><br></font></span></div><div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif">I'd also be happy to answer questions if you get stuck or need help.</font></span></div>
<div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif"><br></font></span></div><div><span style="color:rgb(51,51,51);line-height:18px;white-space:pre"><font face="arial, helvetica, sans-serif">-Edward</font></span></div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Mar 14, 2014 at 7:50 AM, Yuras Shumovich <span dir="ltr"><<a href="mailto:shumovichy@gmail.com" target="_blank">shumovichy@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
Hi,<br>
<br>
Right now ghc's FFI doesn't support c/c++ structures.<br>
<br>
Whenever we have foreign function that accepts or returns struct by<br>
value, we have to create wrapper that accepts or returns pointer to<br>
struct. It is inconvenient, but actually not a big deal.<br>
<br>
But there is no easy workaround when you want to export haskell function<br>
to use it with c/c++ API that requires structures to be passed by value<br>
(Usually it is a callback in c/c++ API. You can't change it's signature,<br>
and if it doesn't provide some kind of "void* userdata", then you are<br>
stuck.)<br>
<br>
I'm interested in fixing that. I'm going to start with 'foreign import<br>
"wrapper" ...' stuff.<br>
<br>
Calling conventions for passing c/c++ structures by value are pretty<br>
tricky and platform/compiler specific. So initially I'll use libffi for<br>
that (it will work when USE_LIBFFI_FOR_ADJUSTORS is defined, see<br>
rts/Adjustor.c). It will allow me to explore design space without<br>
bothering about low level implementation details. Later it could be<br>
implemented for native (non-libffi) adjustors.<br>
<br>
Is anybody interested it that? I appreciate any comments/ideas.<br>
<br>
Right now I don't have clear design. It would be nice to support plain<br>
haskell data types that are 1) not recursive, 2) has one constructor and<br>
3) contains only c/c++ types. But it doesn't work with c/c++ unions. Any<br>
ideas are welcome.<br>
<br>
An example how to use libffi with structures:<br>
<a href="http://www.atmark-techno.com/~yashi/libffi.html#Structures" target="_blank">http://www.atmark-techno.com/~yashi/libffi.html#Structures</a><br>
<br>
Thanks,<br>
Yuras<br>
<br>
<br>
_______________________________________________<br>
ghc-devs mailing list<br>
<a href="mailto:ghc-devs@haskell.org">ghc-devs@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/ghc-devs" target="_blank">http://www.haskell.org/mailman/listinfo/ghc-devs</a><br>
</blockquote></div><br></div>