2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
8 {-# OPTIONS -fno-implicit-prelude #-}
23 %*********************************************************
25 \subsection{Classes @CCallable@ and @CReturnable@}
27 %*********************************************************
33 instance CCallable Char
34 instance CCallable Char#
35 instance CReturnable Char
37 instance CCallable Int
38 instance CCallable Int#
39 instance CReturnable Int
41 -- DsCCall knows how to pass strings...
42 instance CCallable [Char]
44 instance CCallable Float
45 instance CCallable Float#
46 instance CReturnable Float
48 instance CCallable Double
49 instance CCallable Double#
50 instance CReturnable Double
52 instance CCallable Addr
53 instance CCallable Addr#
54 instance CReturnable Addr
56 instance CCallable Word
57 instance CCallable Word#
58 instance CReturnable Word
61 instance CCallable (MutableByteArray s ix)
62 instance CCallable (MutableByteArray# s)
64 instance CCallable (ByteArray ix)
65 instance CCallable ByteArray#
67 instance CReturnable () -- Why, exactly?
71 %*********************************************************
73 \subsection{Type @ForeignObj@ and its operations}
75 %*********************************************************
78 --Defined in PrelBase: data ForeignObj = ForeignObj ForeignObj#
79 instance CCallable ForeignObj
80 instance CCallable ForeignObj#
82 eqForeignObj :: ForeignObj -> ForeignObj -> Bool
83 makeForeignObj :: Addr -> Addr -> PrimIO ForeignObj
85 makeForeignObj (A# obj) (A# finaliser) = ST $ \ (S# s#) ->
86 case makeForeignObj# obj finaliser s# of
87 StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#)
90 = unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
92 instance Eq ForeignObj where
93 p == q = eqForeignObj p q
94 p /= q = not (eqForeignObj p q)
98 %*********************************************************
100 \subsection{Type @StablePtr@ and its operations}
102 %*********************************************************
105 #ifndef __PARALLEL_HASKELL__
106 data StablePtr a = StablePtr (StablePtr# a)
107 instance CCallable (StablePtr a)
108 instance CCallable (StablePtr# a)
109 instance CReturnable (StablePtr a)
111 -- Nota Bene: it is important {\em not\/} to inline calls to
112 -- @makeStablePtr#@ since the corresponding macro is very long and we'll
113 -- get terrible code-bloat.
115 makeStablePtr :: a -> PrimIO (StablePtr a)
116 deRefStablePtr :: StablePtr a -> PrimIO a
117 freeStablePtr :: StablePtr a -> PrimIO ()
118 performGC :: PrimIO ()
120 {-# INLINE deRefStablePtr #-}
121 {-# INLINE freeStablePtr #-}
122 {-# INLINE performGC #-}
124 makeStablePtr f = ST $ \ (S# rw1#) ->
125 case makeStablePtr# f rw1# of
126 StateAndStablePtr# rw2# sp# -> (StablePtr sp#, S# rw2#)
128 deRefStablePtr (StablePtr sp#) = ST $ \ (S# rw1#) ->
129 case deRefStablePtr# sp# rw1# of
130 StateAndPtr# rw2# a -> (a, S# rw2#)
132 freeStablePtr sp = _ccall_ freeStablePointer sp
134 performGC = _ccall_GC_ StgPerformGarbageCollection
136 #endif /* !__PARALLEL_HASKELL__ */
139 %*********************************************************
141 \subsection{Ghastly return types}
143 %*********************************************************
146 #ifndef __PARALLEL_HASKELL__
147 data StateAndStablePtr# s a = StateAndStablePtr# (State# s) (StablePtr# a)
149 data StateAndForeignObj# s = StateAndForeignObj# (State# s) ForeignObj#