2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
8 {-# OPTIONS -fno-implicit-prelude #-}
12 #ifndef __PARALLEL_HASKELL__
27 %*********************************************************
29 \subsection{Type @ForeignObj@ and its operations}
31 %*********************************************************
34 #ifndef __PARALLEL_HASKELL__
35 --instance CCallable ForeignObj
36 --instance CCallable ForeignObj#
38 makeForeignObj :: Addr -> IO ForeignObj
39 makeForeignObj (A# obj) = IO ( \ s# ->
40 case makeForeignObj# obj s# of
41 (# s1#, fo# #) -> (# s1#, ForeignObj fo# #) )
43 --makeForeignObj :: Addr -> Addr -> IO ForeignObj
44 writeForeignObj :: ForeignObj -> Addr -> IO ()
46 writeForeignObj (ForeignObj fo#) (A# datum#) = IO ( \ s# ->
47 case writeForeignObj# fo# datum# s# of { s1# -> (# s1#, () #) } )
48 #endif /* !__PARALLEL_HASKELL__ */
51 %*********************************************************
53 \subsection{Unpacking Foreigns}
55 %*********************************************************
57 Primitives for converting Foreigns pointing to external
58 sequence of bytes into a list of @Char@s (a renamed version
62 #ifndef __PARALLEL_HASKELL__
63 unpackCStringFO :: ForeignObj -> [Char]
64 unpackCStringFO (ForeignObj fo#) = unpackCStringFO# fo#
66 unpackCStringFO# :: ForeignObj# -> [Char]
67 unpackCStringFO# fo {- ptr. to NUL terminated string-}
71 | ch `eqChar#` '\0'# = []
72 | otherwise = C# ch : unpack (nh +# 1#)
74 ch = indexCharOffForeignObj# fo nh
76 unpackNBytesFO :: ForeignObj -> Int -> [Char]
77 unpackNBytesFO (ForeignObj fo) (I# l) = unpackNBytesFO# fo l
79 unpackNBytesFO# :: ForeignObj# -> Int# -> [Char]
80 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
81 unpackNBytesFO# fo len
86 | otherwise = C# ch : unpack (i +# 1#)
88 ch = indexCharOffForeignObj# fo i