Merge branch 'release'

This commit is contained in:
Joshua Suskalo 2021-09-30 20:05:07 -05:00
commit d039b980ad
4 changed files with 34 additions and 19 deletions

View file

@ -1,6 +1,13 @@
# Change Log # Change Log
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/).
## [0.1.192] - 2021-09-30
### Added
- An `::ffi/address` key to wrapper functions' metadata
### Fixed
- Usage of a method no longer in Panama that breaks `with-acquired`
## [0.1.184] - 2021-09-30 ## [0.1.184] - 2021-09-30
### Fixed ### Fixed
- Deserializing nullpointers as functions threw an exception - Deserializing nullpointers as functions threw an exception
@ -21,6 +28,7 @@ All notable changes to this project will be documented in this file. This change
- Support for serializing and deserializing arbitrary Clojure functions - Support for serializing and deserializing arbitrary Clojure functions
- Support for serializing and deserializing arbitrary Clojure data structures - Support for serializing and deserializing arbitrary Clojure data structures
[0.1.192]: https://github.com/IGJoshua/coffi/compare/v0.1.184...v0.1.192
[0.1.184]: https://github.com/IGJoshua/coffi/compare/v0.1.176...v0.1.184 [0.1.184]: https://github.com/IGJoshua/coffi/compare/v0.1.176...v0.1.184
[0.1.176]: https://github.com/IGJoshua/coffi/compare/v0.1.169...v0.1.176 [0.1.176]: https://github.com/IGJoshua/coffi/compare/v0.1.169...v0.1.176
[0.1.169]: https://github.com/IGJoshua/coffi/compare/16f56bc31d69142ec4d2fb61b15b069d78b127ca...v0.1.169 [0.1.169]: https://github.com/IGJoshua/coffi/compare/16f56bc31d69142ec4d2fb61b15b069d78b127ca...v0.1.169

View file

@ -17,8 +17,8 @@ This library is available on Clojars. Add one of the following entries to the
`:deps` key of your `deps.edn`: `:deps` key of your `deps.edn`:
```clojure ```clojure
org.suskalo/coffi {:mvn/version "0.1.184"} org.suskalo/coffi {:mvn/version "0.1.192"}
io.github.IGJoshua/coffi {:git/tag "v0.1.184" :git/sha "ea53cfb"} io.github.IGJoshua/coffi {:git/tag "v0.1.192" :git/sha "9d65f47"}
``` ```
If you use this library as a git dependency, you will need to prepare the If you use this library as a git dependency, you will need to prepare the

View file

@ -368,13 +368,15 @@
(defmethod mem/deserialize* ::fn (defmethod mem/deserialize* ::fn
[addr [_fn arg-types ret-type & {:keys [raw-fn?]}]] [addr [_fn arg-types ret-type & {:keys [raw-fn?]}]]
(when-not (mem/null? addr) (when-not (mem/null? addr)
(vary-meta
(-> addr (-> addr
(downcall-handle (downcall-handle
(method-type arg-types ret-type) (method-type arg-types ret-type)
(function-descriptor arg-types ret-type)) (function-descriptor arg-types ret-type))
(downcall-fn arg-types ret-type) (downcall-fn arg-types ret-type)
(cond-> (cond->
(not raw-fn?) (make-serde-wrapper arg-types ret-type))))) (not raw-fn?) (make-serde-wrapper arg-types ret-type)))
assoc ::address addr)))
;;; Static memory access ;;; Static memory access
@ -543,6 +545,7 @@
(let [args (s/conform ::defcfn-args args) (let [args (s/conform ::defcfn-args args)
args-types (gensym "args-types") args-types (gensym "args-types")
ret-type (gensym "ret-type") ret-type (gensym "ret-type")
address (gensym "symbol")
invoke (gensym "invoke") invoke (gensym "invoke")
native-sym (gensym "native") native-sym (gensym "native")
[arity fn-tail] (-> args :wrapper :fn-tail) [arity fn-tail] (-> args :wrapper :fn-tail)
@ -556,7 +559,8 @@
nil))] nil))]
`(let [~args-types ~(:native-arglist args) `(let [~args-types ~(:native-arglist args)
~ret-type ~(:return-type args) ~ret-type ~(:return-type args)
~invoke (make-downcall ~(name (:symbol args)) ~args-types ~ret-type) ~address (find-symbol ~(name (:symbol args)))
~invoke (make-downcall ~address ~args-types ~ret-type)
~(or (-> args :wrapper :native-fn) native-sym) ~(or (-> args :wrapper :native-fn) native-sym)
~(if (and (every? #(= % (mem/primitive-type %)) ~(if (and (every? #(= % (mem/primitive-type %))
(:native-arglist args)) (:native-arglist args))
@ -579,7 +583,8 @@
(list (list
(mapv (comp symbol name) (mapv (comp symbol name)
(:native-arglist args))))))) (:native-arglist args)))))))
(:attr-map args))) (assoc (:attr-map args)
::address address)))
~@(when-let [doc (:doc args)] ~@(when-let [doc (:doc args)]
(list doc)) (list doc))
fun#)))) fun#))))

View file

@ -29,6 +29,7 @@
MemoryLayout MemoryLayout
MemorySegment MemorySegment
ResourceScope ResourceScope
ResourceScope$Handle
SegmentAllocator))) SegmentAllocator)))
(defn stack-scope (defn stack-scope
@ -96,11 +97,6 @@
([allocator size alignment] ([allocator size alignment]
(.allocate ^SegmentAllocator allocator (long size) (long alignment)))) (.allocate ^SegmentAllocator allocator (long size) (long alignment))))
(defn keep-alive
"Ensures that the scope `target` is not released before `source`."
[source target]
(.keepAlive ^ResourceScope source ^ResourceScope target))
(defmacro with-acquired (defmacro with-acquired
"Acquires one or more `scopes` until the `body` completes. "Acquires one or more `scopes` until the `body` completes.
@ -109,9 +105,15 @@
with it wrapped in this." with it wrapped in this."
{:style/indent 1} {:style/indent 1}
[scopes & body] [scopes & body]
`(with-open [scope# (stack-scope)] `(let [scopes# (vec ~scopes)
(run! (partial keep-alive scope#) ~scopes) handles# (mapv #(.acquire ^ResourceScope %) scopes#)]
~@body)) (try ~@body
(finally
(doseq [idx# (range (count scopes#))
:let [scope# (nth scopes# idx#)
handle# (nth handles# idx#)]]
(.release ^ResourceScope scope#
^ResourceScope$Handle handle#))))))
(s/fdef with-acquired (s/fdef with-acquired
:args (s/cat :scopes any? :args (s/cat :scopes any?
:body (s/* any?))) :body (s/* any?)))