diff --git a/src/runtime/haskell/PGF2.hsc b/src/runtime/haskell/PGF2.hsc index cdb3c1e20..d65816840 100644 --- a/src/runtime/haskell/PGF2.hsc +++ b/src/runtime/haskell/PGF2.hsc @@ -208,66 +208,32 @@ writePGF fpath p mb_langs = withLangs clangs [] f = withArray0 nullPtr (reverse clangs) f withLangs clangs (lang:langs) f = withText lang $ \clang -> withLangs (clang:clangs) langs f -#if defined(__linux__) +#if defined(__linux__) || defined(__APPLE__) writePGF_ :: (Ptr Word8 -> Int -> IO Int) -> PGF -> Maybe [ConcName] -> IO () writePGF_ callback p mb_langs = - allocaBytes (#size cookie_io_functions_t) $ \io_functions -> withForeignPtr (a_revision p) $ \c_revision -> - maybe (\f -> f nullPtr) (withLangs []) mb_langs $ \c_langs -> do - cookie <- fmap castStablePtrToPtr (newStablePtr callback) - (#poke cookie_io_functions_t, read) io_functions nullPtr - (#poke cookie_io_functions_t, write) io_functions cookie_write_ptr - (#poke cookie_io_functions_t, seek) io_functions nullPtr - (#poke cookie_io_functions_t, close) io_functions cookie_close_ptr - withPgfExn "writePGF_" (pgf_write_pgf_cookie cookie io_functions (a_db p) c_revision c_langs) + maybe (\f -> f nullPtr) (withLangs []) mb_langs $ \c_langs -> + bracket (newStablePtr callback) freeStablePtr $ \cookie -> + withPgfExn "writePGF_" (pgf_write_pgf_cookie (castStablePtrToPtr cookie) cookie_write_ptr (a_db p) c_revision c_langs) where withLangs clangs [] f = withArray0 nullPtr (reverse clangs) f withLangs clangs (lang:langs) f = withText lang $ \clang -> withLangs (clang:clangs) langs f -cookie_write :: Ptr () -> Ptr Word8 -> CSize -> IO CSize -cookie_write cookie buf size = do - callback <- deRefStablePtr (castPtrToStablePtr cookie) - fmap fromIntegral $ (callback :: Ptr Word8 -> Int -> IO Int) buf (fromIntegral size) - +#if defined(__linux__) foreign export ccall cookie_write :: Ptr () -> Ptr Word8 -> CSize -> IO CSize foreign import ccall "&cookie_write" cookie_write_ptr :: FunPtr (Ptr () -> Ptr Word8 -> CSize -> IO CSize) -cookie_close :: Ptr () -> IO CInt -cookie_close cookie = do - freeStablePtr (castPtrToStablePtr cookie) - return 0 - -foreign export ccall cookie_close :: Ptr () -> IO CInt -foreign import ccall "&cookie_close" cookie_close_ptr :: FunPtr (Ptr () -> IO CInt) - -#elif defined(__APPLE__) - -writePGF_ :: (Ptr Word8 -> Int -> IO Int) -> PGF -> Maybe [ConcName] -> IO () -writePGF_ callback p mb_langs = - withForeignPtr (a_revision p) $ \c_revision -> - maybe (\f -> f nullPtr) (withLangs []) mb_langs $ \c_langs -> do - cookie <- fmap castStablePtrToPtr (newStablePtr callback) - withPgfExn "writePGF_" (pgf_write_pgf_cookie cookie cookie_write_ptr cookie_close_ptr (a_db p) c_revision c_langs) - where - withLangs clangs [] f = withArray0 nullPtr (reverse clangs) f - withLangs clangs (lang:langs) f = withText lang $ \clang -> withLangs (clang:clangs) langs f - -cookie_write :: Ptr () -> Ptr Word8 -> CInt -> IO CInt -cookie_write cookie buf size = do - callback <- deRefStablePtr (castPtrToStablePtr cookie) - fmap fromIntegral $ (callback :: Ptr Word8 -> Int -> IO Int) buf (fromIntegral size) - +cookie_write :: Ptr () -> Ptr Word8 -> CSize -> IO CSize +#else foreign export ccall cookie_write :: Ptr () -> Ptr Word8 -> CInt -> IO CInt foreign import ccall "&cookie_write" cookie_write_ptr :: FunPtr (Ptr () -> Ptr Word8 -> CInt -> IO CInt) -cookie_close :: Ptr () -> IO CInt -cookie_close cookie = do - freeStablePtr (castPtrToStablePtr cookie) - return 0 - -foreign export ccall cookie_close :: Ptr () -> IO CInt -foreign import ccall "&cookie_close" cookie_close_ptr +cookie_write :: Ptr () -> Ptr Word8 -> CInt -> IO CInt +#endif +cookie_write cookie buf size = do + callback <- deRefStablePtr (castPtrToStablePtr cookie) + fmap fromIntegral $ (callback :: Ptr Word8 -> Int -> IO Int) buf (fromIntegral size) #endif showPGF :: PGF -> String diff --git a/src/runtime/haskell/PGF2/FFI.hsc b/src/runtime/haskell/PGF2/FFI.hsc index 4d0a8db9b..a1b478967 100644 --- a/src/runtime/haskell/PGF2/FFI.hsc +++ b/src/runtime/haskell/PGF2/FFI.hsc @@ -17,10 +17,6 @@ import System.IO.Unsafe(unsafePerformIO) import PGF2.Expr -#ifdef __linux__ -#define _GNU_SOURCE -#include -#endif #include type AbsName = String -- ^ Name of abstract syntax @@ -86,10 +82,9 @@ foreign import ccall pgf_merge_pgf :: Ptr PgfDB -> Ptr PGF -> CString -> Ptr Pgf foreign import ccall pgf_write_pgf :: CString -> Ptr PgfDB -> Ptr PGF -> Ptr (Ptr PgfText) -> Ptr PgfExn -> IO () #if defined(__linux__) -data CookieIOFunctions -foreign import ccall pgf_write_pgf_cookie :: Ptr () -> Ptr CookieIOFunctions -> Ptr PgfDB -> Ptr PGF -> Ptr (Ptr PgfText) -> Ptr PgfExn -> IO () +foreign import ccall pgf_write_pgf_cookie :: Ptr () -> FunPtr (Ptr () -> Ptr Word8 -> CSize -> IO CSize) -> Ptr PgfDB -> Ptr PGF -> Ptr (Ptr PgfText) -> Ptr PgfExn -> IO () #elif defined(__APPLE__) -foreign import ccall pgf_write_pgf_cookie :: Ptr () -> FunPtr (Ptr () -> Ptr Word8 -> CInt -> IO CInt) -> FunPtr (Ptr () -> IO CInt) -> Ptr PgfDB -> Ptr PGF -> Ptr (Ptr PgfText) -> Ptr PgfExn -> IO () +foreign import ccall pgf_write_pgf_cookie :: Ptr () -> FunPtr (Ptr () -> Ptr Word8 -> CInt -> IO CInt) -> Ptr PgfDB -> Ptr PGF -> Ptr (Ptr PgfText) -> Ptr PgfExn -> IO () #endif foreign import ccall "pgf_free_revision" pgf_free_revision_ :: Ptr PgfDB -> Ptr PGF -> IO ()