[project @ 2005-02-03 10:32:11 by ross]
[ghc-base.git] / GHC / TopHandler.lhs
1 \begin{code}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  GHC.TopHandler
5 -- Copyright   :  (c) The University of Glasgow, 2001-2002
6 -- License     :  see libraries/base/LICENSE
7 -- 
8 -- Maintainer  :  cvs-ghc@haskell.org
9 -- Stability   :  internal
10 -- Portability :  non-portable (GHC Extensions)
11 --
12 -- Support for catching exceptions raised during top-level computations
13 -- (e.g. @Main.main@, 'Control.Concurrent.forkIO', and foreign exports)
14 --
15 -----------------------------------------------------------------------------
16
17 -- #hide
18 module GHC.TopHandler (
19    runMainIO, runIO, runNonIO, reportStackOverflow, reportError
20   ) where
21
22 import Prelude
23
24 import System.IO
25 import Control.Exception
26
27 import GHC.IOBase
28 import GHC.Exception
29 import GHC.Prim (unsafeCoerce#)
30
31 -- | 'runMainIO' is wrapped around 'Main.main' (or whatever main is
32 -- called in the program).  It catches otherwise uncaught exceptions,
33 -- and also flushes stdout\/stderr before exiting.
34 runMainIO :: IO a -> IO a
35 runMainIO main = (do a <- main; cleanUp; return a) `catchException` topHandler
36
37 -- | 'runIO' is wrapped around every @foreign export@ and @foreign
38 -- import \"wrapper\"@ to mop up any uncaught exceptions.  Thus, the
39 -- result of running 'System.Exit.exitWith' in a foreign-exported
40 -- function is the same as in the main thread: it terminates the
41 -- program.
42 --
43 runIO :: IO a -> IO a
44 runIO main = catchException main topHandler
45
46 -- | The same as 'runIO', but for non-IO computations.  Used for
47 -- wrapping @foreign export@ and @foreign import \"wrapper\"@ when these
48 -- are used to export Haskell functions with non-IO types.
49 --
50 runNonIO :: a -> IO a
51 runNonIO a = catchException (a `seq` return a) topHandler
52
53 topHandler :: Exception -> IO a
54 topHandler err = catchException (real_handler err) topHandler
55
56 -- Make sure we handle errors while reporting the error!
57 -- (e.g. evaluating the string passed to 'error' might generate
58 --  another error, etc.)
59 --
60 real_handler :: Exception -> IO a
61 real_handler ex =
62   cleanUp >>
63   case ex of
64         AsyncException StackOverflow -> do
65            reportStackOverflow
66            safeExit 2
67
68         -- only the main thread gets ExitException exceptions
69         ExitException ExitSuccess     -> safeExit 0
70         ExitException (ExitFailure n) -> safeExit n
71
72         other -> do
73            reportError other
74            safeExit 1
75            
76
77 reportStackOverflow :: IO a
78 reportStackOverflow = do callStackOverflowHook; return undefined
79
80 reportError :: Exception -> IO a
81 reportError ex = do
82    handler <- getUncaughtExceptionHandler
83    handler ex
84    return undefined
85
86 -- SUP: Are the hooks allowed to re-enter Haskell land?  If so, remove
87 -- the unsafe below.
88 foreign import ccall unsafe "stackOverflow"
89         callStackOverflowHook :: IO ()
90
91 -- try to flush stdout/stderr, but don't worry if we fail
92 -- (these handles might have errors, and we don't want to go into
93 -- an infinite loop).
94 cleanUp :: IO ()
95 cleanUp = do
96   hFlush stdout `catchException` \_ -> return ()
97   hFlush stderr `catchException` \_ -> return ()
98
99 cleanUpAndExit :: Int -> IO a
100 cleanUpAndExit r = do cleanUp; safeExit r
101
102 -- we have to use unsafeCoerce# to get the 'IO a' result type, since the
103 -- compiler doesn't let us declare that as the result type of a foreign export.
104 safeExit :: Int -> IO a
105 safeExit r = unsafeCoerce# (shutdownHaskellAndExit r)
106
107 -- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
108 -- re-enter Haskell land through finalizers.
109 foreign import ccall "shutdownHaskellAndExit" 
110   shutdownHaskellAndExit :: Int -> IO ()
111 \end{code}