Implement function with return value

This commit is contained in:
George Thomas 2026-02-19 15:26:27 +00:00
parent 40bb939302
commit 024b6aec87
7 changed files with 121 additions and 0 deletions

View File

@ -6,6 +6,7 @@ module GarnetRs.Wrapped (
hello, hello,
helloStruct, helloStruct,
helloShape, helloShape,
add,
) where ) where
import Data.ByteString import Data.ByteString
@ -15,6 +16,7 @@ import Foreign.C
import GarnetRs qualified as Raw import GarnetRs qualified as Raw
import GarnetRs.Safe qualified as Raw import GarnetRs.Safe qualified as Raw
import HsBindgen.Runtime.PtrConst import HsBindgen.Runtime.PtrConst
import Unsafe.Coerce
data T = T data T = T
{ a :: Bool { a :: Bool
@ -39,3 +41,10 @@ helloStruct = Raw.hello_struct . convertT
helloShape :: Shape -> IO () helloShape :: Shape -> IO ()
helloShape = Raw.hello_shape . convertShape 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

View File

@ -9,3 +9,4 @@ main = do
helloStruct T{a = False, b = maxBound} helloStruct T{a = False, b = maxBound}
helloShape $ Circle 3.14 helloShape $ Circle 3.14
helloShape $ Rectangle 10.0 5.0 helloShape $ Rectangle 10.0 5.0
putStrLn . ("3 + 4 = " <>) . show =<< add 3 4

View File

@ -10,6 +10,7 @@ import qualified GHC.IO.Unsafe
import qualified GHC.Ptr as Ptr import qualified GHC.Ptr as Ptr
import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.CAPI
import qualified HsBindgen.Runtime.Internal.HasFFIType import qualified HsBindgen.Runtime.Internal.HasFFIType
import qualified HsBindgen.Runtime.LibC
import qualified HsBindgen.Runtime.PtrConst import qualified HsBindgen.Runtime.PtrConst
import Data.Void (Void) import Data.Void (Void)
import GarnetRs import GarnetRs
@ -41,6 +42,15 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{" , "{"
, " return &hello_shape;" , " 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@ -- __unique:__ @com_garnet_GarnetRs_get_hello@
@ -102,3 +112,23 @@ hs_bindgen_287ff3ac660f333b =
hello_shape :: Ptr.FunPtr (Shape -> IO ()) hello_shape :: Ptr.FunPtr (Shape -> IO ())
hello_shape = hello_shape =
GHC.IO.Unsafe.unsafePerformIO hs_bindgen_287ff3ac660f333b 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

View File

@ -7,9 +7,11 @@ module GarnetRs.Safe where
import qualified Foreign as F import qualified Foreign as F
import qualified Foreign.C as FC import qualified Foreign.C as FC
import qualified GHC.Int
import qualified GHC.Ptr as Ptr import qualified GHC.Ptr as Ptr
import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.CAPI
import qualified HsBindgen.Runtime.Internal.HasFFIType import qualified HsBindgen.Runtime.Internal.HasFFIType
import qualified HsBindgen.Runtime.LibC
import qualified HsBindgen.Runtime.PtrConst import qualified HsBindgen.Runtime.PtrConst
import Data.Void (Void) import Data.Void (Void)
import GarnetRs import GarnetRs
@ -35,6 +37,13 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{" , "{"
, " hello_shape(*arg1);" , " hello_shape(*arg1);"
, "}" , "}"
, "intptr_t hs_bindgen_1c0c71fa74c428a9 ("
, " intptr_t arg1,"
, " intptr_t arg2"
, ")"
, "{"
, " return add(arg1, arg2);"
, "}"
])) ]))
-- __unique:__ @com_garnet_GarnetRs_Safe_hello@ -- __unique:__ @com_garnet_GarnetRs_Safe_hello@
@ -112,3 +121,31 @@ hello_shape ::
hello_shape = hello_shape =
\s0 -> \s0 ->
F.with s0 (\s1 -> hs_bindgen_7de06f1fd827ca60 s1) 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

View File

@ -7,9 +7,11 @@ module GarnetRs.Unsafe where
import qualified Foreign as F import qualified Foreign as F
import qualified Foreign.C as FC import qualified Foreign.C as FC
import qualified GHC.Int
import qualified GHC.Ptr as Ptr import qualified GHC.Ptr as Ptr
import qualified HsBindgen.Runtime.Internal.CAPI import qualified HsBindgen.Runtime.Internal.CAPI
import qualified HsBindgen.Runtime.Internal.HasFFIType import qualified HsBindgen.Runtime.Internal.HasFFIType
import qualified HsBindgen.Runtime.LibC
import qualified HsBindgen.Runtime.PtrConst import qualified HsBindgen.Runtime.PtrConst
import Data.Void (Void) import Data.Void (Void)
import GarnetRs import GarnetRs
@ -35,6 +37,13 @@ $(HsBindgen.Runtime.Internal.CAPI.addCSource (HsBindgen.Runtime.Internal.CAPI.un
, "{" , "{"
, " hello_shape(*arg1);" , " hello_shape(*arg1);"
, "}" , "}"
, "intptr_t hs_bindgen_20eb651f0a8faf48 ("
, " intptr_t arg1,"
, " intptr_t arg2"
, ")"
, "{"
, " return add(arg1, arg2);"
, "}"
])) ]))
-- __unique:__ @com_garnet_GarnetRs_Unsafe_hello@ -- __unique:__ @com_garnet_GarnetRs_Unsafe_hello@
@ -112,3 +121,31 @@ hello_shape ::
hello_shape = hello_shape =
\s0 -> \s0 ->
F.with s0 (\s1 -> hs_bindgen_b3f40a03f07eaa85 s1) 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

View File

@ -34,3 +34,5 @@ void hello(const char *c);
void hello_struct(struct T t); void hello_struct(struct T t);
void hello_shape(struct Shape s); void hello_shape(struct Shape s);
intptr_t add(intptr_t a, intptr_t b);

View File

@ -33,3 +33,8 @@ enum Shape {
extern "C" fn hello_shape(s: Shape) -> () { extern "C" fn hello_shape(s: Shape) -> () {
say_hello(&format!("{:?}", s)) say_hello(&format!("{:?}", s))
} }
#[unsafe(no_mangle)]
extern "C" fn add(a: isize, b: isize) -> isize {
a + b
}