ffi: foreign-callable — receive callbacks from C
jolt could call C (foreign-fn -> foreign-procedure) but C could not call back into jolt, which GTK signals (and any callback-taking C API) require. Add the inverse: jolt.ffi/foreign-callable wraps a jolt fn as a C-callable function pointer, mirroring the foreign-fn pipeline. A new jolt.ffi/__ccallable special form carries the fn as a child expression (analyzed + walked by the passes; ir.clj gains an :ffi-callable arm in both child walks) plus literal arg/ret type keywords. The back end lowers it to a locked Chez foreign-callable and returns its entry-point address as a jolt pointer; host/chez/ffi.ss registers the code object so the collector keeps it, and free-callable unlocks it. :collect-safe emits the convention that reactivates the thread on entry, for callbacks fired while it is parked in a :blocking call (a GTK main loop). Test: ffi-binding-test.ss sorts an int array through libc qsort with a jolt comparator (C -> jolt -> C). Re-minted seed.
This commit is contained in:
parent
91eed2b622
commit
c91b6092bc
7 changed files with 178 additions and 70 deletions
|
|
@ -287,6 +287,19 @@
|
|||
" (" (str/join " " (map ffi-type->chez (:argtypes node))) ") "
|
||||
(ffi-type->chez (:rettype node)) ")"))
|
||||
|
||||
;; jolt.ffi/__ccallable -> a Chez foreign-callable wrapping the emitted jolt fn,
|
||||
;; locked + registered (jolt-ffi-register-callable!, host/chez/ffi.ss) so the
|
||||
;; collector neither moves nor reclaims it while C may still call through it. The
|
||||
;; expression evaluates to the entry-point address — a jolt pointer the caller
|
||||
;; hands to C. :collect-safe emits the convention that reactivates the thread on
|
||||
;; entry, for callbacks invoked while it is parked in a :blocking foreign call.
|
||||
(defn- emit-ffi-callable [node]
|
||||
(str "(jolt-ffi-register-callable! (foreign-callable "
|
||||
(when (:collect-safe node) "__collect_safe ")
|
||||
(emit (:fn node))
|
||||
" (" (str/join " " (map ffi-type->chez (:argtypes node))) ") "
|
||||
(ffi-type->chez (:rettype node)) "))"))
|
||||
|
||||
(defn- emit-recur [node]
|
||||
(when-not *recur-target* (throw (ex-info "emit: recur outside a loop/fn target" {})))
|
||||
(let [arg-nodes (:args node)]
|
||||
|
|
@ -500,6 +513,7 @@
|
|||
:loop (emit-loop node)
|
||||
:recur (emit-recur node)
|
||||
:ffi-fn (emit-ffi-fn node)
|
||||
:ffi-callable (emit-ffi-callable node)
|
||||
:fn (emit-fn node)
|
||||
;; (def name) with no init (declare): reserve the cell. A def with non-empty
|
||||
;; reader metadata lowers to def-var-with-meta! (ported in a later increment).
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue