[Cffi-devel] Callbacks & Lambdas

Luís Oliveira luismbo at gmail.com
Wed Nov 5 09:16:07 UTC 2014


On Wed, Nov 5, 2014 at 7:53 AM, FAU <fau at riseup.net> wrote:
> Is there a reliable/portable way to have lambdas called as a callback
> from a foreign C function?  I assume there's not though.

SBCL has an ALIEN-LAMBDA but, last time I checked, most Lisps don't
have a similar feature.


> I could imagine it could be simulated (by a dispatcher callback or
> something) so I'm wondering if somebody has done something like that
> before and may share insight into her/his design.

I know one of the GTK bindings did something like this, but it used
some GTK-specific tricks. IIRC, callbacks were somehow associated
with specific GTK objects, and so you could dispatch on those.

If you know the callback will be invoked within your foreign function
call, a special variable might do the trick, but you'd have to define
callbacks a priori for each and every combination of argument types.

I think you might be able to do this using libffi's closure API,
though. (See cffi-libffi for inspiration on how to use libffi.) Do you
feel like giving that a go?

-- 
Luís Oliveira
http://kerno.org/~luis/




More information about the cffi-devel mailing list