2022-06-12 15:02:02 +00:00
|
|
|
(def ffi/loc "ffitest/so.so")
|
|
|
|
(def ffi/source-loc "ffitest/so.c")
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(os/execute ["cc" ffi/source-loc "-shared" "-o" ffi/loc] :px)
|
|
|
|
(def module (ffi/native ffi/loc))
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def int-fn-sig (ffi/signature :default :int :int :int))
|
|
|
|
(def int-fn-pointer (ffi/lookup module "int_fn"))
|
2022-06-08 14:41:09 +00:00
|
|
|
(defn int-fn
|
|
|
|
[x y]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call int-fn-pointer int-fn-sig x y))
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def double-fn-sig (ffi/signature :default :double :double :double :double))
|
|
|
|
(def double-fn-pointer (ffi/lookup module "double_fn"))
|
2022-06-08 14:41:09 +00:00
|
|
|
(defn double-fn
|
|
|
|
[x y z]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call double-fn-pointer double-fn-sig x y z))
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def double-many-sig (ffi/signature :default :double :double :double :double :double :double :double))
|
|
|
|
(def double-many-pointer (ffi/lookup module "double_many"))
|
2022-06-08 14:41:09 +00:00
|
|
|
(defn double-many
|
|
|
|
[x y z w a b]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call double-many-pointer double-many-sig x y z w a b))
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def double-lots-sig (ffi/signature :default :double
|
2022-06-08 14:41:09 +00:00
|
|
|
:double :double :double :double :double
|
|
|
|
:double :double :double :double :double))
|
2022-06-12 15:02:02 +00:00
|
|
|
(def double-lots-pointer (ffi/lookup module "double_lots"))
|
2022-06-08 14:41:09 +00:00
|
|
|
(defn double-lots
|
|
|
|
[a b c d e f g h i j]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call double-lots-pointer double-lots-sig a b c d e f g h i j))
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def float-fn-sig (ffi/signature :default :double :float :float :float))
|
|
|
|
(def float-fn-pointer (ffi/lookup module "float_fn"))
|
2022-06-08 14:41:09 +00:00
|
|
|
(defn float-fn
|
|
|
|
[x y z]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call float-fn-pointer float-fn-sig x y z))
|
2022-06-08 14:41:09 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def intint-fn-sig (ffi/signature :default :int :double [:int :int]))
|
|
|
|
(def intint-fn-pointer (ffi/lookup module "intint_fn"))
|
2022-06-10 01:27:56 +00:00
|
|
|
(defn intint-fn
|
|
|
|
[x ii]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call intint-fn-pointer intint-fn-sig x ii))
|
2022-06-10 01:27:56 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def return-struct-sig (ffi/signature :default [:int :int] :int))
|
|
|
|
(def return-struct-pointer (ffi/lookup module "return_struct"))
|
2022-06-10 17:24:50 +00:00
|
|
|
(defn return-struct-fn
|
|
|
|
[i]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call return-struct-pointer return-struct-sig i))
|
2022-06-10 01:27:56 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def intintint (ffi/struct :int :int :int))
|
|
|
|
(def intintint-fn-sig (ffi/signature :default :int :double intintint))
|
|
|
|
(def intintint-fn-pointer (ffi/lookup module "intintint_fn"))
|
2022-06-10 01:27:56 +00:00
|
|
|
(defn intintint-fn
|
|
|
|
[x iii]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call intintint-fn-pointer intintint-fn-sig x iii))
|
2022-06-10 01:27:56 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def big (ffi/struct :s64 :s64 :s64))
|
|
|
|
(def struct-big-fn-sig (ffi/signature :default big :int :double))
|
|
|
|
(def struct-big-fn-pointer (ffi/lookup module "struct_big"))
|
2022-06-10 17:24:50 +00:00
|
|
|
(defn struct-big-fn
|
|
|
|
[i d]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call struct-big-fn-pointer struct-big-fn-sig i d))
|
2022-06-10 17:24:50 +00:00
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def void-fn-pointer (ffi/lookup module "void_fn"))
|
|
|
|
(def void-fn-sig (ffi/signature :default :void))
|
2022-06-10 17:33:01 +00:00
|
|
|
(defn void-fn
|
|
|
|
[]
|
2022-06-12 15:02:02 +00:00
|
|
|
(ffi/call void-fn-pointer void-fn-sig))
|
2022-06-10 17:33:01 +00:00
|
|
|
|
2022-06-08 14:41:09 +00:00
|
|
|
#
|
|
|
|
# Call functions
|
|
|
|
#
|
|
|
|
|
2022-06-10 17:33:01 +00:00
|
|
|
(pp (void-fn))
|
2022-06-10 01:27:56 +00:00
|
|
|
(pp (int-fn 10 20))
|
|
|
|
(pp (double-fn 1.5 2.5 3.5))
|
|
|
|
(pp (double-many 1 2 3 4 5 6))
|
|
|
|
(pp (double-lots 1 2 3 4 5 6 7 8 9 10))
|
|
|
|
(pp (float-fn 8 4 17))
|
|
|
|
(pp (intint-fn 123.456 [10 20]))
|
|
|
|
(pp (intintint-fn 123.456 [10 20 30]))
|
2022-06-10 17:24:50 +00:00
|
|
|
(pp (return-struct-fn 42))
|
|
|
|
(pp (struct-big-fn 11 99.5))
|
2022-06-10 01:27:56 +00:00
|
|
|
|
2022-06-08 14:41:09 +00:00
|
|
|
(assert (= 60 (int-fn 10 20)))
|
|
|
|
(assert (= 42 (double-fn 1.5 2.5 3.5)))
|
|
|
|
(assert (= 21 (double-many 1 2 3 4 5 6)))
|
|
|
|
(assert (= 19 (double-lots 1 2 3 4 5 6 7 8 9 10)))
|
|
|
|
(assert (= 204 (float-fn 8 4 17)))
|
|
|
|
|
2022-06-10 14:38:52 +00:00
|
|
|
#
|
|
|
|
# Struct reading and writing
|
|
|
|
#
|
|
|
|
|
|
|
|
(defn check-round-trip
|
|
|
|
[t value]
|
2022-06-12 15:02:02 +00:00
|
|
|
(def buf (ffi/write t value))
|
|
|
|
(def same-value (ffi/read t buf))
|
2022-06-10 14:38:52 +00:00
|
|
|
(assert (deep= value same-value)
|
|
|
|
(string/format "round trip %j (got %j)" value same-value)))
|
|
|
|
|
|
|
|
(check-round-trip :bool true)
|
|
|
|
(check-round-trip :bool false)
|
|
|
|
(check-round-trip :void nil)
|
|
|
|
(check-round-trip :void nil)
|
|
|
|
(check-round-trip :s8 10)
|
|
|
|
(check-round-trip :s8 0)
|
|
|
|
(check-round-trip :s8 -10)
|
|
|
|
(check-round-trip :u8 10)
|
|
|
|
(check-round-trip :u8 0)
|
|
|
|
(check-round-trip :s16 10)
|
|
|
|
(check-round-trip :s16 0)
|
|
|
|
(check-round-trip :s16 -12312)
|
|
|
|
(check-round-trip :u16 10)
|
|
|
|
(check-round-trip :u16 0)
|
|
|
|
(check-round-trip :u32 0)
|
|
|
|
(check-round-trip :u32 10)
|
|
|
|
(check-round-trip :u32 0xFFFF7777)
|
|
|
|
(check-round-trip :s32 0x7FFF7777)
|
|
|
|
(check-round-trip :s32 0)
|
|
|
|
(check-round-trip :s32 -1234567)
|
|
|
|
|
2022-06-12 15:02:02 +00:00
|
|
|
(def s (ffi/struct :s8 :s8 :s8 :float))
|
2022-06-10 14:38:52 +00:00
|
|
|
(check-round-trip s [1 3 5 123.5])
|
|
|
|
(check-round-trip s [-1 -3 -5 -123.5])
|
|
|
|
|
2022-06-08 14:41:09 +00:00
|
|
|
(print "Done.")
|