From 34ce6078ba001864fb2701cbfaa9e9748b27f026 Mon Sep 17 00:00:00 2001 From: George Thomas Date: Thu, 19 Feb 2026 11:21:38 +0000 Subject: [PATCH] wip --- generate-bindings | 10 +++++++++- haskell/exe/Main.hs | 24 +++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/generate-bindings b/generate-bindings index 30d4b1b..786d2fd 100755 --- a/generate-bindings +++ b/generate-bindings @@ -3,6 +3,13 @@ set -euo pipefail # TODO this is a complete vibe-coded hack, but the header patching at least is crucial +# TODO fully-vibed hack +# we should do a few things quite differently anyway + +# generated Haskell code shouldn't be checked in as it's not portable +# does the TH way of using `hs-bindgen` help here? +# I guess we shouldn't check in the generated C header either + # Generate Haskell FFI bindings from Rust source code. # # Pipeline: cargo build -> cbindgen -> patch header -> hs-bindgen @@ -90,7 +97,8 @@ pending_enum && /^typedef [A-Za-z0-9_]+ / { } { print } -' "$HEADER" > "${HEADER}.tmp" && mv "${HEADER}.tmp" "$HEADER" +' "$HEADER" > "${HEADER}.tmp" +mv "${HEADER}.tmp" "$HEADER" echo " Patched header at $HEADER" diff --git a/haskell/exe/Main.hs b/haskell/exe/Main.hs index 0b25177..08278a5 100644 --- a/haskell/exe/Main.hs +++ b/haskell/exe/Main.hs @@ -1,14 +1,32 @@ module Main (main) where import Data.ByteString +import Data.Word import Foreign.C import GarnetRs import GarnetRs.Safe import HsBindgen.Runtime.PtrConst +import Unsafe.Coerce main :: IO () main = do useAsCString "Haskell" $ hello . unsafeFromPtr - hello_struct T{a = CBool 1, b = 42} - hello_shape $ Shape (Shape_Tag 0) $ set_shape_body_circle $ Circle_Body 3.14 - hello_shape $ Shape (Shape_Tag 1) $ set_shape_body_rectangle $ Rectangle_Body 10.0 5.0 + hello_struct $ convertT T'{a = True, b = 42} + hello_struct $ convertT T'{a = False, b = 42} + hello_shape $ convertShape $ Circle 3.14 + hello_shape $ convertShape $ Rectangle 10.0 5.0 + +data T' = T' + { a :: Bool + , b :: Word8 + } +convertT T'{a, b} = + -- T{a = CBool $ unsafeCoerce @Bool @Word8 a, b} + T{a = CBool $ fromIntegral $ fromEnum a, b} + +data Shape' + = Circle CDouble + | Rectangle CDouble CDouble +convertShape = \case + Circle r -> Shape (Shape_Tag 0) $ set_shape_body_circle $ Circle_Body r + Rectangle w h -> Shape (Shape_Tag 1) $ set_shape_body_rectangle $ Rectangle_Body w h