2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
8 {-# OPTIONS -fno-implicit-prelude #-}
12 #ifndef __PARALLEL_HASKELL__
28 %*********************************************************
30 \subsection{Type @ForeignObj@ and its operations}
32 %*********************************************************
35 #ifndef __PARALLEL_HASKELL__
36 --instance CCallable ForeignObj
37 --instance CCallable ForeignObj#
39 makeForeignObj :: Addr -> IO ForeignObj
40 makeForeignObj (A# obj) = IO ( \ s# ->
41 case makeForeignObj# obj s# of
42 (# s1#, fo# #) -> (# s1#, ForeignObj fo# #) )
44 --makeForeignObj :: Addr -> Addr -> IO ForeignObj
45 writeForeignObj :: ForeignObj -> Addr -> IO ()
47 writeForeignObj (ForeignObj fo#) (A# datum#) = IO ( \ s# ->
48 case writeForeignObj# fo# datum# s# of { s1# -> (# s1#, () #) } )
49 #endif /* !__PARALLEL_HASKELL__ */
52 %*********************************************************
54 \subsection{Unpacking Foreigns}
56 %*********************************************************
58 Primitives for converting Foreigns pointing to external
59 sequence of bytes into a list of @Char@s (a renamed version
63 #ifndef __PARALLEL_HASKELL__
64 unpackCStringFO :: ForeignObj -> [Char]
65 unpackCStringFO (ForeignObj fo#) = unpackCStringFO# fo#
67 unpackCStringFO# :: ForeignObj# -> [Char]
68 unpackCStringFO# fo {- ptr. to NUL terminated string-}
72 | ch `eqChar#` '\0'# = []
73 | otherwise = C# ch : unpack (nh +# 1#)
75 ch = indexCharOffForeignObj# fo nh
77 unpackNBytesFO :: ForeignObj -> Int -> [Char]
78 unpackNBytesFO (ForeignObj fo) (I# l) = unpackNBytesFO# fo l
80 unpackNBytesFO# :: ForeignObj# -> Int# -> [Char]
81 -- This one is called by the compiler to unpack literal strings with NULs in them; rare.
82 unpackNBytesFO# fo len
87 | otherwise = C# ch : unpack (i +# 1#)
89 ch = indexCharOffForeignObj# fo i