mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-31 15:43:01 +00:00 
			
		
		
		
	Add ffi/defbind-alias
This commit is contained in:
		| @@ -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))) | ||||||
|   | |||||||
| @@ -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 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 John W Higgins
					John W Higgins