2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[Foreign]{Module @Foreign@}
21 %*********************************************************
23 \subsection{Classes @CCallable@ and @CReturnable@}
25 %*********************************************************
31 instance CCallable Char
32 instance CReturnable Char
34 instance CCallable Int
35 instance CReturnable Int
37 -- DsCCall knows how to pass strings...
38 instance CCallable [Char]
40 instance CCallable Float
41 instance CReturnable Float
43 instance CCallable Double
44 instance CReturnable Double
46 instance CCallable Addr
47 instance CReturnable Addr
49 instance CCallable Word
50 instance CReturnable Word
53 instance CCallable (MutableByteArray s ix)
55 instance CCallable (ByteArray ix)
57 instance CReturnable () -- Why, exactly?
61 %*********************************************************
63 \subsection{Type @ForeignObj@ and its operations}
65 %*********************************************************
68 data ForeignObj = ForeignObj ForeignObj#
69 instance CCallable ForeignObj
71 eqForeignObj :: ForeignObj -> ForeignObj -> Bool
72 makeForeignObj :: Addr -> Addr -> PrimIO ForeignObj
74 makeForeignObj (A# obj) (A# finaliser) = ST $ \ (S# s#) ->
75 case makeForeignObj# obj finaliser s# of
76 StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#)
79 = unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
81 instance Eq ForeignObj where
82 p == q = eqForeignObj p q
83 p /= q = not (eqForeignObj p q)
87 %*********************************************************
89 \subsection{Type @StablePtr@ and its operations}
91 %*********************************************************
94 #ifndef __PARALLEL_HASKELL__
95 data StablePtr a = StablePtr (StablePtr# a)
96 instance CCallable (StablePtr a)
97 instance CReturnable (StablePtr a)
99 -- Nota Bene: it is important {\em not\/} to inline calls to
100 -- @makeStablePtr#@ since the corresponding macro is very long and we'll
101 -- get terrible code-bloat.
103 makeStablePtr :: a -> PrimIO (StablePtr a)
104 deRefStablePtr :: StablePtr a -> PrimIO a
105 freeStablePtr :: StablePtr a -> PrimIO ()
106 performGC :: PrimIO ()
108 {-# INLINE deRefStablePtr #-}
109 {-# INLINE freeStablePtr #-}
110 {-# INLINE performGC #-}
112 makeStablePtr f = ST $ \ (S# rw1#) ->
113 case makeStablePtr# f rw1# of
114 StateAndStablePtr# rw2# sp# -> (StablePtr sp#, S# rw2#)
116 deRefStablePtr (StablePtr sp#) = ST $ \ (S# rw1#) ->
117 case deRefStablePtr# sp# rw1# of
118 StateAndPtr# rw2# a -> (a, S# rw2#)
120 freeStablePtr sp = _ccall_ freeStablePointer sp
122 performGC = _ccall_GC_ StgPerformGarbageCollection
124 #endif /* !__PARALLEL_HASKELL__ */
127 %*********************************************************
129 \subsection{Ghastly return types}
131 %*********************************************************
134 #ifndef __PARALLEL_HASKELL__
135 data StateAndStablePtr# s a = StateAndStablePtr# (State# s) (StablePtr# a)
137 data StateAndForeignObj# s = StateAndForeignObj# (State# s) ForeignObj#