1 % ------------------------------------------------------------------------------
2 % $Id: PrelMain.lhs,v 1.8 2001/05/18 16:54:05 simonmar Exp $
4 % (c) The University of Glasgow, 1994-2000
7 \section[PrelMain]{Module @PrelMain@}
10 module PrelMain( mainIO, reportStackOverflow, reportError ) where
13 import {-# SOURCE #-} qualified Main -- for type of "Main.main"
22 mainIO :: IO () -- It must be of type (IO t) because that's what
23 -- the RTS expects. GHC doesn't check this, so
24 -- make sure this type signature stays!
25 mainIO = catchException Main.main topHandler
27 -- 'Top-level' IO actions want to catch exceptions (e.g., forkIO and
28 -- PrelMain.mainIO) and report them - topHandler is the exception
29 -- handler they should use for this:
31 -- make sure we handle errors while reporting the error!
32 -- (e.g. evaluating the string passed to 'error' might generate
33 -- another error, etc.)
34 topHandler :: Exception -> IO ()
35 topHandler err = catchException (real_handler err) topHandler
37 real_handler :: Exception -> IO ()
40 AsyncException StackOverflow -> reportStackOverflow True
41 ErrorCall s -> reportError True s
42 other -> reportError True (showsPrec 0 other "\n")
44 reportStackOverflow :: Bool -> IO ()
45 reportStackOverflow bombOut = do
46 (hFlush stdout) `catchException` (\ _ -> return ())
53 reportError :: Bool -> String -> IO ()
54 reportError bombOut str = do
55 (hFlush stdout) `catchException` (\ _ -> return ())
56 withCStringLen str $ \(cstr,len) -> do
57 writeErrString addrOf_ErrorHdrHook cstr len
62 foreign import ccall "addrOf_ErrorHdrHook" unsafe
63 addrOf_ErrorHdrHook :: Ptr ()
65 foreign import ccall "writeErrString__" unsafe
66 writeErrString :: Ptr () -> CString -> Int -> IO ()
68 -- SUP: Are the hooks allowed to re-enter Haskell land? If so, remove
70 foreign import ccall "stackOverflow" unsafe
71 callStackOverflowHook :: IO ()
73 foreign import ccall "stg_exit" unsafe
74 stg_exit :: Int -> IO ()