1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-24 07:20:27 +00:00

Merge pull request #1445 from wishdev/defbind-alias

Add ffi/defbind-alias
This commit is contained in:
Calvin Rose 2024-05-11 14:25:03 -05:00 committed by GitHub
commit 60e22d9703
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View File

@ -55,6 +55,7 @@
(ffi/defbind sixints-fn six-ints []) (ffi/defbind sixints-fn six-ints [])
(ffi/defbind sixints-fn-2 :int [x :int s six-ints]) (ffi/defbind sixints-fn-2 :int [x :int s six-ints])
(ffi/defbind sixints-fn-3 :int [s six-ints x :int]) (ffi/defbind sixints-fn-3 :int [s six-ints x :int])
(ffi/defbind-alias int-fn int-fn-aliased :int [a :int b :int])
# #
# Struct reading and writing # Struct reading and writing
@ -119,6 +120,7 @@
(tracev (return-struct 42)) (tracev (return-struct 42))
(tracev (double-lots 1 2 3 4 5 6 700 800 9 10)) (tracev (double-lots 1 2 3 4 5 6 700 800 9 10))
(tracev (struct-big 11 99.5)) (tracev (struct-big 11 99.5))
(tracev (int-fn-aliased 10 20))
(assert (= [10 10 12 12] (split-ret-fn 10 12))) (assert (= [10 10 12 12] (split-ret-fn 10 12)))
(assert (= [12 12 10 10] (split-flip-ret-fn 10 12))) (assert (= [12 12 10 10] (split-flip-ret-fn 10 12)))

View File

@ -3853,9 +3853,11 @@
:lazy lazy :lazy lazy
:map-symbols map-symbols})) :map-symbols map-symbols}))
(defmacro ffi/defbind (defmacro ffi/defbind-alias
"Generate bindings for native functions in a convenient manner." "Generate bindings for native functions in a convenient manner.
[name ret-type & body] Similar to defbind but allows for the janet function name to be
different than the FFI function."
[name alias ret-type & body]
(def real-ret-type (eval ret-type)) (def real-ret-type (eval ret-type))
(def meta (slice body 0 -2)) (def meta (slice body 0 -2))
(def arg-pairs (partition 2 (last body))) (def arg-pairs (partition 2 (last body)))
@ -3872,11 +3874,16 @@
(defn make-ptr [] (defn make-ptr []
(assert (ffi/lookup (if lazy (llib) lib) raw-symbol) (string "failed to find ffi symbol " raw-symbol))) (assert (ffi/lookup (if lazy (llib) lib) raw-symbol) (string "failed to find ffi symbol " raw-symbol)))
(if lazy (if lazy
~(defn ,name ,;meta [,;formal-args] ~(defn ,alias ,;meta [,;formal-args]
(,ffi/call (,(delay (make-ptr))) (,(delay (make-sig))) ,;formal-args)) (,ffi/call (,(delay (make-ptr))) (,(delay (make-sig))) ,;formal-args))
~(defn ,name ,;meta [,;formal-args] ~(defn ,alias ,;meta [,;formal-args]
(,ffi/call ,(make-ptr) ,(make-sig) ,;formal-args))))) (,ffi/call ,(make-ptr) ,(make-sig) ,;formal-args)))))
(defmacro ffi/defbind
"Generate bindings for native functions in a convenient manner."
[name ret-type & body]
~(ffi/defbind-alias ,name ,name ,ret-type ,;body))
### ###
### ###
### Flychecking ### Flychecking