Hello cffi'ers, 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? :)
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
James Bielman <jamesjb <at> jamesjb.com> writes:
Lars Rune Nøstdal <larsnostdal <at> 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 , <at> 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...)
Hm, you're right, the read-from-string stuff doesn't seem to be needed (can't remember what I was thinking). Anyway, I guess this will have to wait until someone finds a workaround that works for all, or all (or maybe most of?) the Lisps support non-toplevel callbacks. :)