2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
8 {-# OPTIONS -fcompiling-prelude -fno-implicit-prelude #-}
12 #ifndef __PARALLEL_HASKELL__
26 import PrelWeak ( addForeignFinalizer )
30 %*********************************************************
32 \subsection{Type @ForeignObj@ and its operations}
34 %*********************************************************
36 mkForeignObj and writeForeignObj are the building blocks
37 for makeForeignObj, they can probably be nuked in the future.
40 #ifndef __PARALLEL_HASKELL__
41 --instance CCallable ForeignObj
42 --instance CCallable ForeignObj#
44 makeForeignObj :: Addr -> IO () -> IO ForeignObj
45 makeForeignObj addr finalizer = do
46 fObj <- mkForeignObj addr
47 addForeignFinalizer fObj finalizer
50 mkForeignObj :: Addr -> IO ForeignObj
51 mkForeignObj (A# obj) = IO ( \ s# ->
52 case mkForeignObj# obj s# of
53 (# s1#, fo# #) -> (# s1#, ForeignObj fo# #) )
55 writeForeignObj :: ForeignObj -> Addr -> IO ()
56 writeForeignObj (ForeignObj fo#) (A# datum#) = IO ( \ s# ->
57 case writeForeignObj# fo# datum# s# of { s1# -> (# s1#, () #) } )
58 #endif /* !__PARALLEL_HASKELL__ */
61 %*********************************************************
63 \subsection{Unpacking Foreigns}
65 %*********************************************************
67 Primitives for converting Foreigns pointing to external
68 sequence of bytes into a list of @Char@s (a renamed version
72 #ifndef __PARALLEL_HASKELL__
73 unpackCStringFO :: ForeignObj -> [Char]
74 unpackCStringFO (ForeignObj fo#) = unpackCStringFO# fo#
76 unpackCStringFO# :: ForeignObj# -> [Char]
77 unpackCStringFO# fo {- ptr. to NUL terminated string-}
81 | ch `eqChar#` '\0'# = []
82 | otherwise = C# ch : unpack (nh +# 1#)
84 ch = indexCharOffForeignObj# fo nh
86 unpackNBytesFO :: ForeignObj -> Int -> [Char]
87 unpackNBytesFO (ForeignObj fo) (I# l) = unpackNBytesFO# fo l
89 unpackNBytesFO# :: ForeignObj# -> Int# -> [Char]
90 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
91 unpackNBytesFO# fo len
96 | otherwise = C# ch : unpack (i +# 1#)
98 ch = indexCharOffForeignObj# fo i