From 0cfe7edb36354be3c7ccab3c4ebc75fa463df632 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Fri, 17 Sep 2021 10:50:46 -0500 Subject: [PATCH] Add functions for getting const and variable values from native vars --- src/coffi/ffi.clj | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index 367d576..50cef80 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -5,6 +5,8 @@ [clojure.spec.alpha :as s] [insn.core :as insn]) (:import + (clojure.lang + IDeref IMeta IObj IReference) (java.lang.invoke VarHandle MethodHandle @@ -592,6 +594,52 @@ [:areturn]]}]} ^MethodHandle handle)) +(defn- ensure-address + [symbol-or-addr] + (if (instance? Addressable symbol-or-addr) + (address-of symbol-or-addr) + (find-symbol symbol-or-addr))) + +(defn const + [symbol-or-addr type] + (deserialize (ensure-address symbol-or-addr) [::pointer type])) + +(deftype StaticVariable [addr type meta] + Addressable + (address [_] + addr) + IDeref + (deref [_] + (deserialize addr [::pointer type])) + + IObj + (withMeta [_ meta-map] + (StaticVariable. addr type (atom meta-map))) + IMeta + (meta [_] + @meta) + IReference + (resetMeta [_ meta-map] + (reset! meta meta-map)) + (alterMeta [_ f args] + (apply swap! meta f args))) + +(defn freset! + [^StaticVariable static-var newval] + (serialize-into + newval (.-type static-var) + (slice-global (.-addr static-var) (size-of (.-type static-var))) + (global-scope)) + newval) + +(defn fswap! + [static-var f & args] + (freset! static-var (apply f @static-var args))) + +(defn static-variable + [symbol-or-addr type] + (StaticVariable. (ensure-address symbol-or-addr) type (atom nil))) + (s/def ::defcfn-args (s/cat :name simple-symbol? :doc (s/? string?)