module GHC.Vis.View.Common (
visSignal,
visRunning,
visState,
visBoxes,
evaluate
)
where
#if __GLASGOW_HASKELL__ < 706
import Prelude hiding (catch)
#endif
import Control.Concurrent
import Control.DeepSeq
import Control.Exception hiding (evaluate)
import Data.IORef
import System.IO.Unsafe
import GHC.Vis.Internal
import GHC.Vis.Types
import GHC.HeapView
visSignal :: MVar Signal
visSignal = unsafePerformIO (newEmptyMVar :: IO (MVar Signal))
visRunning :: MVar Bool
visRunning = unsafePerformIO (newMVar False)
visState :: IORef State
visState = unsafePerformIO $ newIORef $ State (0, 0) ListView 1 (0, 0) False False
visBoxes :: MVar [(Box, String)]
visBoxes = unsafePerformIO (newMVar [] :: IO (MVar [(Box, String)]))
evaluate :: String -> IO ()
evaluate identifier = do (_,hm) <- printAll
(show (map go hm) `deepseq` return ()) `catch`
\(e :: SomeException) -> putStrLn $ "Caught exception while evaluating: " ++ show e
where go (Box a,(Just n, y)) | n == identifier = seq a (Just n, y)
| otherwise = (Just n, y)
go (_,(x,y)) = (x,y)
printAll :: IO (String, HeapMap)
printAll = do
bs <- readMVar visBoxes
(t, PState{heapMap = h}) <- parseBoxesHeap bs
return (show t, h)