From 87f48ec68183df56bcfcf5296cfa8963cec1a262 Mon Sep 17 00:00:00 2001 From: Joshua Suskalo Date: Mon, 20 Sep 2021 14:17:43 -0500 Subject: [PATCH] Add support for union types --- src/coffi/ffi.clj | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/coffi/ffi.clj b/src/coffi/ffi.clj index 5e7eab0..36d1eb1 100644 --- a/src/coffi/ffi.clj +++ b/src/coffi/ffi.clj @@ -440,7 +440,7 @@ deserialize-from) obj type))) -;; C String type +;;; C String type (defmethod primitive-type ::c-string [_type] @@ -468,6 +468,27 @@ (rec (slice segment size))))))] (rec segment)))) +;;; Union types + +(defmethod c-layout ::union + [[_union _dispatch types & {:as _opts} :as _type]] + (let [items (map c-layout types)] + (MemoryLayout/unionLayout + (into-array MemoryLayout items)))) + +(defmethod serialize-into ::union + [obj [_union dispatch _types & {:keys [extract]}] segment scope] + (let [type (dispatch obj)] + (serialize-into + (if extract + (extract type obj) + obj) + type + segment + scope))) + +;;; FFI Code loading and function access + (defn load-system-library "Loads the library named `libname` from the system's load path." [libname]