blob: 8f6ce1bf92d830f17bb293f88d61d713d3ac4bad (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{-# LANGUAGE ForeignFunctionInterface #-}
import Foreign
import Foreign.C
-- test for calling functions by importing them by address and then
-- using dynamic calls. In 6.10 and earlier, GHCi rejected the
-- foreign import '&foo' declarations, for no apparently good reason.
type Malloc = CSize -> IO (Ptr ())
type Write = CInt -> Ptr CChar -> CSize -> IO CSize
foreign import ccall unsafe "&malloc" pmalloc:: FunPtr Malloc
foreign import ccall unsafe "dynamic" callMalloc :: FunPtr Malloc -> Malloc
foreign import ccall unsafe "&write" pwrite:: FunPtr Write
foreign import ccall unsafe "dynamic" callWrite :: FunPtr Write -> Write
main = do
p <- callMalloc pmalloc 32
free p
withCStringLen "hello\n" $ \(p,len) -> callWrite pwrite 1 p (fromIntegral len)
return ()
|