[cffi-devel] Converting foreign structures with CFFI generic functions

Liam Healy lnp at healy.washington.dc.us
Sat Aug 27 19:13:04 UTC 2011


On Sat, Aug 27, 2011 at 1:08 PM, Luís Oliveira <luismbo at gmail.com> wrote:

> On Sat, Aug 27, 2011 at 5:05 PM, Liam Healy <lnp at healy.washington.dc.us>
> wrote:
> > My thought now is that I should define methods for translate-*-foreign
> and
> > free-translated-object dispatching on the foreign-struct-type class.
>
> That makes sense. The type object should have all the necessary info
> you need to construct whatever objects libffi requires. (I'm not
> familiar with the libffi API.) I.e., you shouldn't need to define
> specific translation methods for each struct type definition.
>

I think I do need specific translations; see below.  I think a default would
be appropriate though.  libffi needs a pointer to a foreign structure.


> > The body will call the same method with the structure name, and
> applications can
> > define methods eql-specialized on the name.
>
> I don't think that's necessary. DEFCSTRUCT takes an undocumented class
> argument that allows for specialization based on class rather than
> using EQL-specializers. I'm guessing you're thinking about stuff like
> converting CLOS objects into C structs. That is something that hasn't
> been properly explored in CFFI. See the undocumented
> DEFINE-C-STRUCT-WRAPPER for a half-baked go at that problem.
>

I didn't know about the :class argument defcstruct, that seems right.  I
could define a default method for foreign-struct-type.   I'm not thinking
about CLOS objects, though that's interesting.  However, I don't view
foreign structs as always mapping into Lisp structures; my canonical example
is complex numbers (see FSBV readme http://repo.or.cz/w/fsbv.git) because
the original driver for FSBV was the use of complex numbers in GSLL.   I
convert Lisp complex numbers to and from the foreign structure consisting of
two doubles or floats.



> Does this sound like it will
> work correctly with CFFI as it exists now, so that I can then introduce
the
> libffi interface for the calls to foreign functions?

 What kind of translation do you need these methods to do on
> FOREIGN-STRUCT-TYPE?
>
> They should take the lisp object, allocate the foreign structure, then
define the slots correctly (recursively if necessary), and return the
pointer.  For the return pointer, they should read the contents and generate
the lisp equivalent.  I suppose the calls to libffi functions should invoke
translate-*-foreign just like it now invokes the functions on the properties
fsbv::foreign-object-components and fsbv::setf-foreign-object-components.



> Cheers,
>
> --
> Luís Oliveira
> http://r42.eu/~luis/
>

Does this make sense?

Thanks,

Liam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20110827/43b2ccfa/attachment.html>


More information about the cffi-devel mailing list