Add a Clojure FFI so libraries can bind native code (jolt.ffi)
A jolt library can now bind its own native dependencies and expose a Clojure API over them — no jolt built-in required. This is the foundation for moving the http-client / db / adapter functionality out of the host and into real libraries. - jolt.ffi/foreign-fn (sugar: defcfn) is a compiler special form: a compile-time -typed C signature lowers to a real Chez foreign-procedure (analyzer :ffi-fn -> backend foreign-procedure), so calls are typed and marshaled, not eval'd. - host/chez/ffi.ss provides the rest under jolt.ffi: load-library, alloc/free, read/write/sizeof, ptr<->string, null/null?. Loaded after the loader snapshot so a library's (require '[jolt.ffi]) still loads the macro side. - Types: int/uint/long/ulong/int64/uint64/size_t/ssize_t/iptr/uptr/double/float/ pointer/string/void/uint8/char. Validated end to end: a pure-Clojure file binds libc (getpid/strlen/abs) and libsqlite3 (open/prepare/step/column/finalize over out-param pointers) and runs a query. Gate test test/chez/ffi-binding-test.ss (make ffi); selfhost holds.
This commit is contained in:
parent
ccf93c896a
commit
537cb360b4
8 changed files with 282 additions and 62 deletions
43
test/chez/ffi-binding-test.ss
Normal file
43
test/chez/ffi-binding-test.ss
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
;; jolt.ffi regression: a compile-time-typed foreign binding lowers to a real
|
||||
;; Chez foreign-procedure and calls native code. Run:
|
||||
;; chez --script test/chez/ffi-binding-test.ss
|
||||
;; Binds a few libc functions (process symbols, always present) through the
|
||||
;; jolt.ffi/__cfn special form + the host memory primitives — the same path a
|
||||
;; library uses to bind its native deps.
|
||||
|
||||
(import (chezscheme))
|
||||
(load "host/chez/rt.ss")
|
||||
(set-chez-ns! "clojure.core")
|
||||
(load "host/chez/seed/prelude.ss")
|
||||
(load "host/chez/post-prelude.ss")
|
||||
(set-chez-ns! "user")
|
||||
(load "host/chez/host-contract.ss")
|
||||
(load "host/chez/seed/image.ss")
|
||||
(load "host/chez/compile-eval.ss")
|
||||
(load "host/chez/ffi.ss")
|
||||
|
||||
(define total 0) (define fails 0)
|
||||
(define (ok name pred) (set! total (+ total 1)) (unless pred (set! fails (+ fails 1)) (printf "FAIL: ~a\n" name)))
|
||||
;; eval one form (string) in `user`, like the loader does form-by-form, so a def
|
||||
;; is visible to a later form.
|
||||
(define (ev s) (jolt-compile-eval s "user"))
|
||||
|
||||
;; load libc (process symbols) and bind typed foreign functions
|
||||
(ev "(jolt.ffi/load-library)")
|
||||
(ev "(def c-strlen (jolt.ffi/__cfn \"strlen\" [:string] :size_t))")
|
||||
(ev "(def c-abs (jolt.ffi/__cfn \"abs\" [:int] :int))")
|
||||
|
||||
(ok "foreign-procedure built for strlen" (procedure? (var-deref "user" "c-strlen")))
|
||||
(ok "typed call: strlen(\"hello\") = 5" (= 5 (jnum->exact (ev "(c-strlen \"hello\")"))))
|
||||
(ok "typed call: abs(-7) = 7" (= 7 (jnum->exact (ev "(c-abs -7)"))))
|
||||
|
||||
;; memory: alloc / write / read roundtrip through the host primitives
|
||||
(ok "mem int roundtrip"
|
||||
(= 4242 (jnum->exact
|
||||
(ev "(let [p (jolt.ffi/alloc (jolt.ffi/sizeof :int))]
|
||||
(jolt.ffi/write p :int 0 4242)
|
||||
(let [v (jolt.ffi/read p :int)] (jolt.ffi/free p) v))"))))
|
||||
(ok "sizeof :pointer is a word" (let ((n (jnum->exact (ev "(jolt.ffi/sizeof :pointer)")))) (or (= n 8) (= n 4))))
|
||||
|
||||
(printf "~a/~a passed~n" (- total fails) total)
|
||||
(exit (if (zero? fails) 0 1))
|
||||
Loading…
Add table
Add a link
Reference in a new issue