2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
8 {-# OPTIONS -fno-implicit-prelude #-}
22 %*********************************************************
24 \subsection{Classes @CCallable@ and @CReturnable@}
26 %*********************************************************
32 instance CCallable Char
33 instance CCallable Char#
34 instance CReturnable Char
36 instance CCallable Int
37 instance CCallable Int#
38 instance CReturnable Int
40 -- DsCCall knows how to pass strings...
41 instance CCallable [Char]
43 instance CCallable Float
44 instance CCallable Float#
45 instance CReturnable Float
47 instance CCallable Double
48 instance CCallable Double#
49 instance CReturnable Double
51 instance CCallable Addr
52 instance CCallable Addr#
53 instance CReturnable Addr
55 instance CCallable Word
56 instance CCallable Word#
57 instance CReturnable Word
60 instance CCallable (MutableByteArray s ix)
61 instance CCallable (MutableByteArray# s)
63 instance CCallable (ByteArray ix)
64 instance CCallable ByteArray#
66 instance CReturnable () -- Why, exactly?
70 %*********************************************************
72 \subsection{Type @ForeignObj@ and its operations}
74 %*********************************************************
77 data ForeignObj = ForeignObj ForeignObj#
78 instance CCallable ForeignObj
79 instance CCallable ForeignObj#
81 eqForeignObj :: ForeignObj -> ForeignObj -> Bool
82 makeForeignObj :: Addr -> Addr -> PrimIO ForeignObj
84 makeForeignObj (A# obj) (A# finaliser) = ST $ \ (S# s#) ->
85 case makeForeignObj# obj finaliser s# of
86 StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#)
89 = unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
91 instance Eq ForeignObj where
92 p == q = eqForeignObj p q
93 p /= q = not (eqForeignObj p q)
97 %*********************************************************
99 \subsection{Type @StablePtr@ and its operations}
101 %*********************************************************
104 #ifndef __PARALLEL_HASKELL__
105 data StablePtr a = StablePtr (StablePtr# a)
106 instance CCallable (StablePtr a)
107 instance CCallable (StablePtr# a)
108 instance CReturnable (StablePtr a)
110 -- Nota Bene: it is important {\em not\/} to inline calls to
111 -- @makeStablePtr#@ since the corresponding macro is very long and we'll
112 -- get terrible code-bloat.
114 makeStablePtr :: a -> PrimIO (StablePtr a)
115 deRefStablePtr :: StablePtr a -> PrimIO a
116 freeStablePtr :: StablePtr a -> PrimIO ()
117 performGC :: PrimIO ()
119 {-# INLINE deRefStablePtr #-}
120 {-# INLINE freeStablePtr #-}
121 {-# INLINE performGC #-}
123 makeStablePtr f = ST $ \ (S# rw1#) ->
124 case makeStablePtr# f rw1# of
125 StateAndStablePtr# rw2# sp# -> (StablePtr sp#, S# rw2#)
127 deRefStablePtr (StablePtr sp#) = ST $ \ (S# rw1#) ->
128 case deRefStablePtr# sp# rw1# of
129 StateAndPtr# rw2# a -> (a, S# rw2#)
131 freeStablePtr sp = _ccall_ freeStablePointer sp
133 performGC = _ccall_GC_ StgPerformGarbageCollection
135 #endif /* !__PARALLEL_HASKELL__ */
138 %*********************************************************
140 \subsection{Ghastly return types}
142 %*********************************************************
145 #ifndef __PARALLEL_HASKELL__
146 data StateAndStablePtr# s a = StateAndStablePtr# (State# s) (StablePtr# a)
148 data StateAndForeignObj# s = StateAndForeignObj# (State# s) ForeignObj#