on master help sign in
Merge pull request #4500 from melted/cb_closure_err
f660d36Niklas Larsson
docs/reference/ffi.rst+6
@@ -178,6 +178,12 @@ compilation error:
178 example : (Int -> ()) -> IO ()
178 example : (Int -> ()) -> IO ()
179 example f = foreign FFI_C "callbacker" (CFnPtr (Int -> ()) -> IO ()) f
179 example f = foreign FFI_C "callbacker" (CFnPtr (Int -> ()) -> IO ()) f
180
180
181+Note that the function that is used as a callback can't be a closure, that is
182+it can't be a partially applied function. This is because the mechanism used is
183+unable to pass the closed-over values through C. If we want to pass Idris values
184+to the callback we have to pass them through C explicitly. Non-primitive Idris
185+values can be passed to C via the ``Raw`` type.
186+
181The other big limitation is that it doesn't support IO functions. Use
187The other big limitation is that it doesn't support IO functions. Use
182``unsafePerformIO`` to wrap them (i.e. to make an IO function usable as a callback, change the return type
188``unsafePerformIO`` to wrap them (i.e. to make an IO function usable as a callback, change the return type
183from IOr to r, and change the = do to = unsafePerformIO $ do).
189from IOr to r, and change the = do to = unsafePerformIO $ do).
src/IRTS/CodegenC.hs+4−1
@@ -889,7 +889,10 @@ getCallback bc = getCallback' (reverse bc)
889 getCallback' [] = []
889 getCallback' [] = []
890 findCons (c:cs) xs = findCon c xs ++ findCons cs xs
890 findCons (c:cs) xs = findCon c xs ++ findCons cs xs
891 findCons [] _ = []
891 findCons [] _ = []
892− findCon c ((MKCON l loc tag args):xs) | snd c == l = [(fst c, tag)]
892+ findCon c ((MKCON l loc tag args):xs) | snd c == l =
893+ if null args
894+ then [(fst c, tag)]
895+ else error "Can't wrap a closure as callback."
893 findCon c (_:xs) = findCon c xs
896 findCon c (_:xs) = findCon c xs
894 findCon c [] = []
897 findCon c [] = []
895
898