diff --git a/exe/GarnetRs/Wrapped.hs b/exe/GarnetRs/Wrapped.hs index 504643e..6be065f 100644 --- a/exe/GarnetRs/Wrapped.hs +++ b/exe/GarnetRs/Wrapped.hs @@ -6,6 +6,7 @@ module GarnetRs.Wrapped ( hello, helloStruct, helloShape, + add, ) where import Data.ByteString @@ -15,6 +16,7 @@ import Foreign.C import GarnetRs qualified as Raw import GarnetRs.Safe qualified as Raw import HsBindgen.Runtime.PtrConst +import Unsafe.Coerce data T = T { a :: Bool @@ -39,3 +41,10 @@ helloStruct = Raw.hello_struct . convertT helloShape :: Shape -> IO () helloShape = Raw.hello_shape . convertShape + +add :: Int -> Int -> IO Int +add a b = from <$> Raw.add (to a) (to b) + where + -- these types should be the same in practice - both are pointer-sized + to = unsafeCoerce @Int @CIntPtr + from = unsafeCoerce @CIntPtr @Int diff --git a/exe/Main.hs b/exe/Main.hs index 5cd845c..80f3f47 100644 --- a/exe/Main.hs +++ b/exe/Main.hs @@ -9,3 +9,4 @@ main = do helloStruct T{a = False, b = maxBound} helloShape $ Circle 3.14 helloShape $ Rectangle 10.0 5.0 + putStrLn . ("3 + 4 = " <>) . show =<< add 3 4 diff --git a/generated/GarnetRs/FunPtr.hs b/generated/GarnetRs/FunPtr.hs index 13f202f..76031fb 100644 --- a/generated/GarnetRs/FunPtr.hs +++ b/generated/GarnetRs/FunPtr.hs @@ -10,6 +10,7 @@ import qualified GHC.IO.Unsafe import qualified GHC.Ptr as Ptr import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.HasFFIType +import qualified HsBindgen.Runtime.LibC import qualified HsBindgen.Runtime.PtrConst import Data.Void (Void) import GarnetRs @@ -41,6 +42,15 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un , "{" , " return &hello_shape;" , "}" + , "/* com_garnet_GarnetRs_get_add */" + , "__attribute__ ((const))" + , "intptr_t (*hs_bindgen_bbabdbe61cd1eeb2 (void)) (" + , " intptr_t arg1," + , " intptr_t arg2" + , ")" + , "{" + , " return &add;" + , "}" ])) -- __unique:__ @com_garnet_GarnetRs_get_hello@ @@ -102,3 +112,23 @@ hs_bindgen_287ff3ac660f333b = hello_shape :: Ptr.FunPtr (Shape -> IO ()) hello_shape = GHC.IO.Unsafe.unsafePerformIO hs_bindgen_287ff3ac660f333b + +-- __unique:__ @com_garnet_GarnetRs_get_add@ +foreign import ccall unsafe "hs_bindgen_bbabdbe61cd1eeb2" hs_bindgen_bbabdbe61cd1eeb2_base :: + IO (Ptr.FunPtr Void) + +-- __unique:__ @com_garnet_GarnetRs_get_add@ +hs_bindgen_bbabdbe61cd1eeb2 :: IO (Ptr.FunPtr (HsBindgen.Runtime.LibC.CIntPtr -> HsBindgen.Runtime.LibC.CIntPtr -> IO HsBindgen.Runtime.LibC.CIntPtr)) +hs_bindgen_bbabdbe61cd1eeb2 = + HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_bbabdbe61cd1eeb2_base + +{-# NOINLINE add #-} +{-| __C declaration:__ @add@ + + __defined at:__ @garnet_rs.h 38:10@ + + __exported by:__ @garnet_rs.h@ +-} +add :: Ptr.FunPtr (HsBindgen.Runtime.LibC.CIntPtr -> HsBindgen.Runtime.LibC.CIntPtr -> IO HsBindgen.Runtime.LibC.CIntPtr) +add = + GHC.IO.Unsafe.unsafePerformIO hs_bindgen_bbabdbe61cd1eeb2 diff --git a/generated/GarnetRs/Safe.hs b/generated/GarnetRs/Safe.hs index 202fdf0..28c2c65 100644 --- a/generated/GarnetRs/Safe.hs +++ b/generated/GarnetRs/Safe.hs @@ -7,9 +7,11 @@ module GarnetRs.Safe where import qualified Foreign as F import qualified Foreign.C as FC +import qualified GHC.Int import qualified GHC.Ptr as Ptr import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.HasFFIType +import qualified HsBindgen.Runtime.LibC import qualified HsBindgen.Runtime.PtrConst import Data.Void (Void) import GarnetRs @@ -35,6 +37,13 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un , "{" , " hello_shape(*arg1);" , "}" + , "intptr_t hs_bindgen_1c0c71fa74c428a9 (" + , " intptr_t arg1," + , " intptr_t arg2" + , ")" + , "{" + , " return add(arg1, arg2);" + , "}" ])) -- __unique:__ @com_garnet_GarnetRs_Safe_hello@ @@ -112,3 +121,31 @@ hello_shape :: hello_shape = \s0 -> F.with s0 (\s1 -> hs_bindgen_7de06f1fd827ca60 s1) + +-- __unique:__ @com_garnet_GarnetRs_Safe_add@ +foreign import ccall safe "hs_bindgen_1c0c71fa74c428a9" hs_bindgen_1c0c71fa74c428a9_base :: + GHC.Int.Int64 + -> GHC.Int.Int64 + -> IO GHC.Int.Int64 + +-- __unique:__ @com_garnet_GarnetRs_Safe_add@ +hs_bindgen_1c0c71fa74c428a9 :: + HsBindgen.Runtime.LibC.CIntPtr + -> HsBindgen.Runtime.LibC.CIntPtr + -> IO HsBindgen.Runtime.LibC.CIntPtr +hs_bindgen_1c0c71fa74c428a9 = + HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_1c0c71fa74c428a9_base + +{-| __C declaration:__ @add@ + + __defined at:__ @garnet_rs.h 38:10@ + + __exported by:__ @garnet_rs.h@ +-} +add :: + HsBindgen.Runtime.LibC.CIntPtr + -- ^ __C declaration:__ @a@ + -> HsBindgen.Runtime.LibC.CIntPtr + -- ^ __C declaration:__ @b@ + -> IO HsBindgen.Runtime.LibC.CIntPtr +add = hs_bindgen_1c0c71fa74c428a9 diff --git a/generated/GarnetRs/Unsafe.hs b/generated/GarnetRs/Unsafe.hs index bc0034d..cea8eee 100644 --- a/generated/GarnetRs/Unsafe.hs +++ b/generated/GarnetRs/Unsafe.hs @@ -7,9 +7,11 @@ module GarnetRs.Unsafe where import qualified Foreign as F import qualified Foreign.C as FC +import qualified GHC.Int import qualified GHC.Ptr as Ptr import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.HasFFIType +import qualified HsBindgen.Runtime.LibC import qualified HsBindgen.Runtime.PtrConst import Data.Void (Void) import GarnetRs @@ -35,6 +37,13 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un , "{" , " hello_shape(*arg1);" , "}" + , "intptr_t hs_bindgen_20eb651f0a8faf48 (" + , " intptr_t arg1," + , " intptr_t arg2" + , ")" + , "{" + , " return add(arg1, arg2);" + , "}" ])) -- __unique:__ @com_garnet_GarnetRs_Unsafe_hello@ @@ -112,3 +121,31 @@ hello_shape :: hello_shape = \s0 -> F.with s0 (\s1 -> hs_bindgen_b3f40a03f07eaa85 s1) + +-- __unique:__ @com_garnet_GarnetRs_Unsafe_add@ +foreign import ccall unsafe "hs_bindgen_20eb651f0a8faf48" hs_bindgen_20eb651f0a8faf48_base :: + GHC.Int.Int64 + -> GHC.Int.Int64 + -> IO GHC.Int.Int64 + +-- __unique:__ @com_garnet_GarnetRs_Unsafe_add@ +hs_bindgen_20eb651f0a8faf48 :: + HsBindgen.Runtime.LibC.CIntPtr + -> HsBindgen.Runtime.LibC.CIntPtr + -> IO HsBindgen.Runtime.LibC.CIntPtr +hs_bindgen_20eb651f0a8faf48 = + HsBindgen.Runtime.Internal.HasFFIType.fromFFIType hs_bindgen_20eb651f0a8faf48_base + +{-| __C declaration:__ @add@ + + __defined at:__ @garnet_rs.h 38:10@ + + __exported by:__ @garnet_rs.h@ +-} +add :: + HsBindgen.Runtime.LibC.CIntPtr + -- ^ __C declaration:__ @a@ + -> HsBindgen.Runtime.LibC.CIntPtr + -- ^ __C declaration:__ @b@ + -> IO HsBindgen.Runtime.LibC.CIntPtr +add = hs_bindgen_20eb651f0a8faf48 diff --git a/rust/garnet_rs.h b/rust/garnet_rs.h index 92cfeeb..76f3125 100644 --- a/rust/garnet_rs.h +++ b/rust/garnet_rs.h @@ -34,3 +34,5 @@ void hello(const char *c); void hello_struct(struct T t); void hello_shape(struct Shape s); + +intptr_t add(intptr_t a, intptr_t b); diff --git a/rust/lib.rs b/rust/lib.rs index 8db620a..59044e0 100644 --- a/rust/lib.rs +++ b/rust/lib.rs @@ -33,3 +33,8 @@ enum Shape { extern "C" fn hello_shape(s: Shape) -> () { say_hello(&format!("{:?}", s)) } + +#[unsafe(no_mangle)] +extern "C" fn add(a: isize, b: isize) -> isize { + a + b +}