From 21ea19b80bf4e16898406bd3241e8ab3de0c6c66 Mon Sep 17 00:00:00 2001 From: Simon Marlow Date: Tue, 18 Apr 2006 14:37:14 +0000 Subject: [PATCH] update commentry for foreign import "wrapper" handling --- compiler/deSugar/DsForeign.lhs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/compiler/deSugar/DsForeign.lhs b/compiler/deSugar/DsForeign.lhs index fcac3a6..725681e 100644 --- a/compiler/deSugar/DsForeign.lhs +++ b/compiler/deSugar/DsForeign.lhs @@ -314,26 +314,34 @@ dsFExport fn_id ty ext_name cconv isDyn fe_arg_tys res_ty is_IO_res_ty cconv \end{code} -@foreign export dynamic@ lets you dress up Haskell IO actions -of some fixed type behind an externally callable interface (i.e., -as a C function pointer). Useful for callbacks and stuff. +@foreign import "wrapper"@ (previously "foreign export dynamic") lets +you dress up Haskell IO actions of some fixed type behind an +externally callable interface (i.e., as a C function pointer). Useful +for callbacks and stuff. \begin{verbatim} -foreign export dynamic f :: (Addr -> Int -> IO Int) -> IO Addr +type Fun = Bool -> Int -> IO Int +foreign import "wrapper" f :: Fun -> IO (FunPtr Fun) -- Haskell-visible constructor, which is generated from the above: -- SUP: No check for NULL from createAdjustor anymore??? -f :: (Addr -> Int -> IO Int) -> IO Addr +f :: Fun -> IO (FunPtr Fun) f cback = bindIO (newStablePtr cback) (\StablePtr sp# -> IO (\s1# -> case _ccall_ createAdjustor cconv sp# ``f_helper'' s1# of (# s2#, a# #) -> (# s2#, A# a# #))) -foreign export "f_helper" f_helper :: StablePtr (Addr -> Int -> IO Int) -> Addr -> Int -> IO Int --- `special' foreign export that invokes the closure pointed to by the --- first argument. +foreign import "&f_helper" f_helper :: FunPtr (StablePtr Fun -> Fun) + +-- and the helper in C: + +f_helper(StablePtr s, HsBool b, HsInt i) +{ + rts_evalIO(rts_apply(rts_apply(deRefStablePtr(s), + rts_mkBool(b)), rts_mkInt(i))); +} \end{verbatim} \begin{code} -- 1.7.10.4