[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] FFI and callbacks -- in Scheme?!
From: |
Peter Bex |
Subject: |
Re: [Chicken-users] FFI and callbacks -- in Scheme?! |
Date: |
Mon, 13 May 2019 11:37:49 +0200 |
User-agent: |
NeoMutt/20170113 (1.7.2) |
On Mon, May 13, 2019 at 11:07:07AM +0200, Christoph Lange wrote:
> I'm still busy trying to get MQTT (using Mosquitto MQTT) running for me in
> Scheme. I have the following code, and this works fine when compiling.
>
> Note, that I have defined the `on_message` callback as external, so
> basically a C function in the end. This forces me to use this code compiled.
>
> Ideally, in a next step, I'd like to put everything into a Chicken module
> in a way that will also be usable in the interpreter. Therefore my question:
>
> *What would I have to do to be able to use pure Scheme functions as
> callbacks here?*
Hi Christoph,
A relatively straightforward way to do that is to use the void * user
data that is passed to mosquitto_new to store a vector or alist or
something ctonainng callbacks. It would have to be a pointer to a GC
root object that stores this vector, because the vector itself will be
moved around by the GC of course.
See http://wiki.call-cc.org/man/5/Embedding#chicken_new_gc_root
Then the callback can simply be a foreign safe lambda or a define-external
procedure that looks up the real callback in this vector. You could also
store it inside the mosquitto connection record type object, which would
be cleaner because you can more easily change the callback for a given
connection. You could either store the aforementioned vector in the
object, or store the actual handlers in it and stuff the mosquitto object
inside a GC root.
The GC root should be freed whenever the mosquitto object is freed.
Cheers,
Peter
signature.asc
Description: PGP signature