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
|
|
@ -94,6 +94,7 @@
|
|||
(= op :set-var) (assoc node :val (f (get node :val)))
|
||||
(= op :set-field) (assoc node :obj (f (get node :obj)) :val (f (get node :val)))
|
||||
(= op :defmacro) (assoc node :fn (f (get node :fn)))
|
||||
(= op :ffi-callable) (assoc node :fn (f (get node :fn)))
|
||||
(= op :invoke) (assoc node :fn (f (get node :fn))
|
||||
:args (mapv f (get node :args)))
|
||||
(= op :vector) (assoc node :items (mapv f (get node :items)))
|
||||
|
|
@ -140,6 +141,7 @@
|
|||
(= op :set-var) (f acc (get node :val))
|
||||
(= op :set-field) (f (f acc (get node :obj)) (get node :val))
|
||||
(= op :defmacro) (f acc (get node :fn))
|
||||
(= op :ffi-callable) (f acc (get node :fn))
|
||||
(= op :invoke) (reduce f (f acc (get node :fn)) (get node :args))
|
||||
(= op :vector) (reduce f acc (get node :items))
|
||||
(= op :set) (reduce f acc (get node :items))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue