-;;;; Callback mechanism
-
-(defun register-callback-function (function)
- (check-type function (or null symbol function))
- ; We treat callbacks just as ordinary user data
- (register-user-data function))
-
-(defun callback-trampoline (callback-id nargs arg-array)
- (declare (fixnum callback-id nargs))
- (let* ((return-arg (unless (null-pointer-p arg-array)
- (arg-array-ref arg-array nargs)))
- (return-type (if return-arg
- (type-from-number (arg-type return-arg))
- nil))
- (args nil)
- (callback-function (find-user-data callback-id)))
-
- (dotimes (n nargs)
- (push (arg-value (arg-array-ref arg-array (- nargs n 1))) args))
-
- (labels ((invoke-callback ()
- (restart-case
- (unwind-protect
- (let ((return-value (apply callback-function args)))
- (when return-type
- (setf (return-arg-value return-arg) return-value))))
-
- (continue nil :report "Return from callback function"
- (when return-type
- (format
- *query-io*
- "Enter return value of type ~S: "
- return-type)
- (force-output *query-io*)
- (setf
- (return-arg-value return-arg)
- (eval (read *query-io*)))))
- (re-invoke nil :report "Re-invoke callback function"
- (invoke-callback)))))
- (invoke-callback))))
-
-(defvar *callback-marshal* (system:foreign-symbol-address "callback_marshal"))
-(setq *destroy-marshal* (system:foreign-symbol-address "destroy_marshal"))
-
-(defun after-gc-hook ()
- (setf
- (extern-alien "callback_trampoline" system-area-pointer)
- (make-pointer (kernel:get-lisp-obj-address #'callback-trampoline))
- (extern-alien "destroy_user_data" system-area-pointer)
- (make-pointer (kernel:get-lisp-obj-address #'destroy-user-data))))
-
-(pushnew 'after-gc-hook ext:*after-gc-hooks*)
-(after-gc-hook)
-
-