diff options
Diffstat (limited to 'testsuite/tests/ffi/should_run/ffi021.hs')
-rw-r--r-- | testsuite/tests/ffi/should_run/ffi021.hs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/testsuite/tests/ffi/should_run/ffi021.hs b/testsuite/tests/ffi/should_run/ffi021.hs new file mode 100644 index 0000000000..8f6ce1bf92 --- /dev/null +++ b/testsuite/tests/ffi/should_run/ffi021.hs @@ -0,0 +1,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 () |