From e00e62f99627552ec8e705b67ed72eacad9cf392 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Thu, 23 Sep 2021 08:30:11 -0500 Subject: [PATCH] Ensure that primitive values are cast to the correct types in serialization --- src/clj/coffi/ffi.clj | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/clj/coffi/ffi.clj b/src/clj/coffi/ffi.clj index 70779d8..e330524 100644 --- a/src/clj/coffi/ffi.clj +++ b/src/clj/coffi/ffi.clj @@ -255,10 +255,21 @@ [obj type scope] (type-dispatch type))) +(def ^:private primitive-cast + "Map from primitive type names to the function to cast it to a primitive." + {::byte byte + ::short short + ::int int + ::long long + ::long-long long + ::char char + ::float float + ::double double}) + (defmethod serialize* :default [obj type _scope] - (if (primitive-type type) - obj + (if-let [prim (primitive-type type)] + ((primitive-cast prim) obj) (throw (ex-info "Attempted to serialize a non-primitive type with primitive methods" {:type type :object obj}))))