Lars Rune Nøstdal larsnostdal@gmail.com writes:
I've been using this lately:
(defvar *lambda-callbacks* nil "TODO: Clean up later somehow?")
(defmacro lambda-callback (return-type args &body body) (let ((name (read-from-string (symbol-name (gensym))))) `(progn (defcallback ,name ,return-type ,args ,@body) (push ',name *lambda-callbacks*) (callback ,name))))
..maybe something like this could be included in cffi? :)
Using CFFI's DEFCALLBACK in any non-toplevel context is probably a recipe for disaster on at least some of the Lisps we support, so I don't think this can work as a portable solution. (I don't understand the purpose of *LAMBDA-CALLBACKS* or the READ-FROM-STRING business either...)
I implemented a proof-of-concept CALLBACK-LAMBDA for SBCL and CLISP awhile back---the patch should be in the list archives somewhere. I'd like to see it cleaned up and integrated into CFFI proper, but currently don't have the time to finish it; the main things missing are an interface for freeing the callback functions, and probably some sort of CALLBACK-FLET type macro that can be used when the callback function has dynamic extent.
James