From 81bbbf3433c56b845c6c646aad915150d021236a Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 16 Sep 2021 16:15:31 -0500 Subject: [PATCH] Add all the c-string type stuff together in one place --- src/coffi/ffi.clj | 54 ++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index b657e19..8c1cff2 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -163,10 +163,6 @@ [type] (c-prim-layout type)) -(defmethod c-layout ::c-string - [_type] - CLinker/C_POINTER) - (defmulti primitive-type "Gets the primitive type that is used to pass as an argument for the `type`. @@ -181,10 +177,6 @@ [type] (primitive-types type)) -(defmethod primitive-type ::c-string - [_type] - ::pointer) - (def java-prim-layout "Map of primitive type names to the Java types for a method handle." {::byte Byte/TYPE @@ -206,10 +198,6 @@ [type] (java-prim-layout type MemorySegment)) -(defmethod java-layout ::c-string - [_type] - MemoryAddress) - (defn size-of "The size in bytes of the given `type`." [type] @@ -265,10 +253,6 @@ [obj type segment scope] (type-dispatch type))) -(defmethod serialize* ::c-string - [obj _type scope] - (address-of (CLinker/toCString (str obj) ^ResourceScope scope))) - (defmethod serialize-into :default [obj type segment scope] (if-some [prim-layout (primitive-type type)] @@ -383,16 +367,6 @@ [obj _type] obj) -(defmethod deserialize-from ::c-string - [segment type] - (-> segment - (deserialize-from ::pointer) - (deserialize* type))) - -(defmethod deserialize* ::c-string - [addr _type] - (CLinker/toJavaString ^MemoryAddress addr)) - (defn deserialize "Deserializes an arbitrary type. @@ -406,6 +380,34 @@ deserialize-from) obj type))) +;; C String type + +(defmethod primitive-type ::c-string + [_type] + ::pointer) + +(defmethod c-layout ::c-string + [_type] + CLinker/C_POINTER) + +(defmethod java-layout ::c-string + [_type] + MemoryAddress) + +(defmethod serialize* ::c-string + [obj _type scope] + (address-of (CLinker/toCString (str obj) ^ResourceScope scope))) + +(defmethod deserialize-from ::c-string + [segment type] + (-> segment + (deserialize-from ::pointer) + (deserialize* type))) + +(defmethod deserialize* ::c-string + [addr _type] + (CLinker/toJavaString ^MemoryAddress addr)) + #_(defn seq-of "Constructs a lazy sequence of `type` elements deserialized from `segment`." [type segment]