Commit 21ea19b8 authored by Simon Marlow's avatar Simon Marlow
Browse files

update commentry for foreign import "wrapper" handling

parent ae08a1e6
...@@ -314,26 +314,34 @@ dsFExport fn_id ty ext_name cconv isDyn ...@@ -314,26 +314,34 @@ dsFExport fn_id ty ext_name cconv isDyn
fe_arg_tys res_ty is_IO_res_ty cconv fe_arg_tys res_ty is_IO_res_ty cconv
\end{code} \end{code}
@foreign export dynamic@ lets you dress up Haskell IO actions @foreign import "wrapper"@ (previously "foreign export dynamic") lets
of some fixed type behind an externally callable interface (i.e., you dress up Haskell IO actions of some fixed type behind an
as a C function pointer). Useful for callbacks and stuff. externally callable interface (i.e., as a C function pointer). Useful
for callbacks and stuff.
\begin{verbatim} \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: -- Haskell-visible constructor, which is generated from the above:
-- SUP: No check for NULL from createAdjustor anymore??? -- SUP: No check for NULL from createAdjustor anymore???
f :: (Addr -> Int -> IO Int) -> IO Addr f :: Fun -> IO (FunPtr Fun)
f cback = f cback =
bindIO (newStablePtr cback) bindIO (newStablePtr cback)
(\StablePtr sp# -> IO (\s1# -> (\StablePtr sp# -> IO (\s1# ->
case _ccall_ createAdjustor cconv sp# ``f_helper'' s1# of case _ccall_ createAdjustor cconv sp# ``f_helper'' s1# of
(# s2#, a# #) -> (# s2#, A# a# #))) (# s2#, a# #) -> (# s2#, A# a# #)))
foreign export "f_helper" f_helper :: StablePtr (Addr -> Int -> IO Int) -> Addr -> Int -> IO Int foreign import "&f_helper" f_helper :: FunPtr (StablePtr Fun -> Fun)
-- `special' foreign export that invokes the closure pointed to by the
-- first argument. -- 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} \end{verbatim}
\begin{code} \begin{code}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment