Something went wrong. Try again.
Common Lisp bindings for egui
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899(in-package #:egui)
;;; Lisp closures are passed to the shim as integer ids. The shim calls back;;; through one trampoline with the id and a handle. Rust frames sit between;;; the trampoline and whoever called into the shim, so nothing may unwind out;;; of the trampoline: errors and non-local exits are stopped at its boundary.
(defstruct (callback (:constructor make-callback (function name kind))) (function nil :type function :read-only t) (name "" :read-only t) (kind nil :type (member :ui :context nil) :read-only t))
(defvar *callbacks* (make-hash-table :synchronized t))
(defvar *callback-ids* (make-id-counter))
(defun register-callback (function name kind) (let ((id (next-id *callback-ids*))) (setf (gethash id *callbacks*) (make-callback function name kind)) id))
(defun unregister-callback (id) (remhash id *callbacks*))
(defmacro with-callback ((id function name kind) &body body) `(let ((,id (register-callback ,function ,name ,kind))) (unwind-protect (progn ,@body) (unregister-callback ,id))))
;;; Handles are wrapped in structures so Lisp code sees typed objects. They;;; are only valid during the callback that produced them; the shim reports a;;; stale handle as an error.
(defstruct (ui (:constructor make-ui (handle)) (:copier nil)) (handle 0 :type (unsigned-byte 64) :read-only t))
(defstruct (context (:constructor make-context (handle)) (:copier nil)) (handle 0 :type (unsigned-byte 64) :read-only t))
(defstruct (response (:constructor make-response (handle)) (:copier nil)) (handle 0 :type (unsigned-byte 64) :read-only t))
(defun wrap-handle (kind handle) (ecase kind (:ui (make-ui handle)) (:context (make-context handle)) ((nil) nil)))
(defun report-error (condition name backtrace) (handler-case (funcall *error-hook* condition name backtrace) (serious-condition (hook-condition) (default-error-hook hook-condition "*error-hook*" (capture-backtrace)) (default-error-hook condition name backtrace))))
(defun show-inline-error (ui-handle name condition) (let ((text (format nil "Error in ~A: ~A" name condition))) (with-utf8 ((pointer length) text) (with-foreign-float-traps (ffi:%ui-label ui-handle pointer length (cffi:null-pointer))))))
(defun invoke-callback (callback handle) "Call CALLBACK with HANDLE and return 0, or report the failure and return 1.Never lets a condition or a non-local exit escape." (let ((completed nil) (failure nil)) (block boundary (unwind-protect (handler-bind ((serious-condition (lambda (condition) (setf failure (list condition (capture-backtrace))) (return-from boundary)))) (funcall (callback-function callback) (wrap-handle (callback-kind callback) handle)) (setf completed t)) (unless (or completed failure) ;; A non-local exit is unwinding toward a target outside this ;; callback, across Rust frames. Abandon it by exiting to BOUNDARY. (setf failure (list (make-condition 'non-local-exit-error :message "a non-local exit tried to leave a UI callback; it was stopped") (capture-backtrace))) (return-from boundary)))) (cond (completed 0) (t (destructuring-bind (condition backtrace) failure (report-error condition (callback-name callback) backtrace) (when (eq (callback-kind callback) :ui) (show-inline-error handle (callback-name callback) condition))) 1))))
;; ECL defines callbacks only in natively compiled code. The static builder;; first loads everything with ECL's bytecode compiler, for macros and packages;; only (D-63).#-ecl-bytecmp(cffi:defcallback trampoline :int32 ((id :uint64) (handle :uint64)) (with-lisp-float-traps (let ((callback (gethash id *callbacks*))) (if callback (invoke-callback callback handle) 1))))