\section[Foreign]{Module @Foreign@}
\begin{code}
+{-# OPTIONS -fno-implicit-prelude #-}
+
module Foreign (
module Foreign,
+ ForeignObj(..),
Addr, Word
) where
-import Prelude ()
import STBase
import ArrBase
import PrelBase
class CReturnable a
instance CCallable Char
+instance CCallable Char#
instance CReturnable Char
instance CCallable Int
+instance CCallable Int#
instance CReturnable Int
-- DsCCall knows how to pass strings...
instance CCallable [Char]
instance CCallable Float
+instance CCallable Float#
instance CReturnable Float
instance CCallable Double
+instance CCallable Double#
instance CReturnable Double
instance CCallable Addr
+instance CCallable Addr#
instance CReturnable Addr
instance CCallable Word
+instance CCallable Word#
instance CReturnable Word
-- Is this right?
instance CCallable (MutableByteArray s ix)
+instance CCallable (MutableByteArray# s)
instance CCallable (ByteArray ix)
+instance CCallable ByteArray#
instance CReturnable () -- Why, exactly?
\end{code}
%*********************************************************
\begin{code}
-data ForeignObj = ForeignObj ForeignObj#
+--Defined in PrelBase: data ForeignObj = ForeignObj ForeignObj#
instance CCallable ForeignObj
+instance CCallable ForeignObj#
+
+eqForeignObj :: ForeignObj -> ForeignObj -> Bool
+makeForeignObj :: Addr -> Addr -> PrimIO ForeignObj
+writeForeignObj :: ForeignObj -> Addr -> PrimIO ()
-eqForeignObj :: ForeignObj -> ForeignObj -> Bool
-makeForeignObj :: Addr -> Addr -> PrimIO ForeignObj
+{- derived op - attaching a free() finaliser to a malloc() allocated reference. -}
+makeMallocPtr :: Addr -> PrimIO ForeignObj
-makeForeignObj (A# obj) (A# finaliser) = ST $ \ (S# s#) ->
+makeForeignObj (A# obj) (A# finaliser) = ST ( \ (S# s#) ->
case makeForeignObj# obj finaliser s# of
- StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#)
+ StateAndForeignObj# s1# fo# -> (ForeignObj fo#, S# s1#))
+
+writeForeignObj (ForeignObj fo#) (A# datum#) = ST ( \ (S# s#) ->
+ case writeForeignObj# fo# datum# s# of { s1# -> ((), S# s1#) } )
+
+makeMallocPtr a = makeForeignObj a (``&free''::Addr)
eqForeignObj mp1 mp2
= unsafePerformPrimIO (_ccall_ eqForeignObj mp1 mp2) /= (0::Int)
#ifndef __PARALLEL_HASKELL__
data StablePtr a = StablePtr (StablePtr# a)
instance CCallable (StablePtr a)
+instance CCallable (StablePtr# a)
instance CReturnable (StablePtr a)
-- Nota Bene: it is important {\em not\/} to inline calls to