From 3dcbe3ff2a08355810f21320db2921ac0ad8c9f3 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 16 Sep 2021 16:28:21 -0500 Subject: [PATCH] Add default c-layout and java-layout implementations for primitives --- src/coffi/ffi.clj | 66 ++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index 33a7e35..84745e9 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -131,18 +131,6 @@ ::float ::double ::pointer ::void}) -(def c-prim-layout - "Map of primitive type names to the [[CLinker]] types for a method handle." - {::byte CLinker/C_CHAR - ::short CLinker/C_SHORT - ::int CLinker/C_INT - ::long CLinker/C_LONG - ::long-long CLinker/C_LONG_LONG - ::char CLinker/C_CHAR - ::float CLinker/C_FLOAT - ::double CLinker/C_DOUBLE - ::pointer CLinker/C_POINTER}) - (defn- type-dispatch "Gets a type dispatch value from a (potentially composite) type." [type] @@ -150,19 +138,6 @@ (qualified-keyword? type) type (sequential? type) (keyword (first type)))) -(defmulti c-layout - "Gets the layout object for a given `type`. - - If a type is primitive it will return the appropriate primitive - layout (see [[c-prim-layout]]). - - Otherwise, it should return a [[GroupLayout]] for the given type." - type-dispatch) - -(defmethod c-layout :default - [type] - (c-prim-layout type)) - (defmulti primitive-type "Gets the primitive type that is used to pass as an argument for the `type`. @@ -177,6 +152,31 @@ [type] (primitive-types type)) +(def c-prim-layout + "Map of primitive type names to the [[CLinker]] types for a method handle." + {::byte CLinker/C_CHAR + ::short CLinker/C_SHORT + ::int CLinker/C_INT + ::long CLinker/C_LONG + ::long-long CLinker/C_LONG_LONG + ::char CLinker/C_CHAR + ::float CLinker/C_FLOAT + ::double CLinker/C_DOUBLE + ::pointer CLinker/C_POINTER}) + +(defmulti c-layout + "Gets the layout object for a given `type`. + + If a type is primitive it will return the appropriate primitive + layout (see [[c-prim-layout]]). + + Otherwise, it should return a [[GroupLayout]] for the given type." + type-dispatch) + +(defmethod c-layout :default + [type] + (c-prim-layout (or (primitive-type type) type))) + (def java-prim-layout "Map of primitive type names to the Java types for a method handle." {::byte Byte/TYPE @@ -191,12 +191,16 @@ ::void Void/TYPE}) (defmulti java-layout - "Gets the Java class to an argument of this type for a method handle." + "Gets the Java class to an argument of this type for a method handle. + + If a type serializes to a primitive it should return a Java primitive type. + + Otherwise, it should return [[MemorySegment]]." type-dispatch) (defmethod java-layout :default [type] - (java-prim-layout type MemorySegment)) + (java-prim-layout (or (primitive-type type) type) MemorySegment)) (defn size-of "The size in bytes of the given `type`." @@ -402,14 +406,6 @@ [_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)))