Skip to content

Instantly share code, notes, and snippets.

@phadej
Created January 10, 2025 10:59
Show Gist options
  • Save phadej/059993b3d430515fd97a3ee6033510c4 to your computer and use it in GitHub Desktop.
Save phadej/059993b3d430515fd97a3ee6033510c4 to your computer and use it in GitHub Desktop.
cabal-version: 3.0
name: callback-exception
version: 0
executable problem
build-depends: base
include-dirs: include
hs-source-dirs: src
main-is: problem.hs
static inline int function( int(*callback)(int), int x) {
return callback(x);
}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE TypeApplications #-}
module Main (main) where
import Control.Exception
import Foreign
import Foreign.C.Types
x :: Int
x = 10
foreign import ccall "wrapper" mkWrapper :: (CInt -> IO CInt) -> IO (FunPtr (CInt -> IO CInt))
foreign import capi "ce.h function" func :: FunPtr (CInt -> IO CInt) -> CInt -> IO CInt
main :: IO ()
main = do
w <- mkWrapper $ \_ -> fail "blargh"
res <- try @SomeException $ func w 10
print res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment