| 1 |
{-# OPTIONS_GHC -Wno-name-shadowing #-} |
| 2 |
{-# LANGUAGE CPP #-} |
| 3 |
{-# LANGUAGE FlexibleInstances #-} |
| 4 |
|
| 5 |
{-# OPTIONS_GHC -fno-cse -fno-warn-orphans #-} |
| 6 |
-- -fno-cse is needed for GLOBAL_VAR's to behave properly |
| 7 |
|
| 8 |
----------------------------------------------------------------------------- |
| 9 |
-- |
| 10 |
-- Monadery code used in InteractiveUI |
| 11 |
-- |
| 12 |
-- (c) The GHC Team 2005-2006 |
| 13 |
-- |
| 14 |
----------------------------------------------------------------------------- |
| 15 |
|
| 16 |
module Clash.GHCi.UI.Monad ( |
| 17 |
GHCi(..), startGHCi, |
| 18 |
GHCiState(..), setGHCiState, getGHCiState, modifyGHCiState, |
| 19 |
GHCiOption(..), isOptionSet, setOption, unsetOption, |
| 20 |
Command(..), CommandResult(..), cmdSuccess, |
| 21 |
PromptFunction, |
| 22 |
BreakLocation(..), |
| 23 |
TickArray, |
| 24 |
getDynFlags, |
| 25 |
|
| 26 |
runStmt, runDecls, runDecls', resume, recordBreak, revertCAFs, |
| 27 |
ActionStats(..), runAndPrintStats, runWithStats, printStats, |
| 28 |
|
| 29 |
printForUserNeverQualify, printForUserModInfo, |
| 30 |
printForUser, printForUserPartWay, prettyLocations, |
| 31 |
|
| 32 |
compileGHCiExpr, |
| 33 |
initInterpBuffering, |
| 34 |
turnOffBuffering, turnOffBuffering_, |
| 35 |
flushInterpBuffers, |
| 36 |
mkEvalWrapper |
| 37 |
) where |
| 38 |
|
| 39 |
#include "HsVersions.h" |
| 40 |
|
| 41 |
import Clash.GHCi.UI.Info (ModInfo) |
| 42 |
import qualified GHC |
| 43 |
import GhcMonad hiding (liftIO) |
| 44 |
import Outputable hiding (printForUser, printForUserPartWay) |
| 45 |
import qualified Outputable |
| 46 |
import OccName |
| 47 |
import DynFlags |
| 48 |
import FastString |
| 49 |
import HscTypes |
| 50 |
import SrcLoc |
| 51 |
import Module |
| 52 |
import RdrName (mkOrig) |
| 53 |
import PrelNames (gHC_GHCI_HELPERS) |
| 54 |
import GHCi |
| 55 |
import GHCi.RemoteTypes |
| 56 |
import HsSyn (ImportDecl, GhcPs, GhciLStmt, LHsDecl) |
| 57 |
import HsUtils |
| 58 |
import Util |
| 59 |
|
| 60 |
import Exception |
| 61 |
import Numeric |
| 62 |
import Data.Array |
| 63 |
import Data.IORef |
| 64 |
import Data.Time |
| 65 |
import System.Environment |
| 66 |
import System.IO |
| 67 |
import Control.Monad |
| 68 |
import Prelude hiding ((<>)) |
| 69 |
|
| 70 |
import System.Console.Haskeline (CompletionFunc, InputT) |
| 71 |
import qualified System.Console.Haskeline as Haskeline |
| 72 |
import Control.Monad.Trans.Class |
| 73 |
import Control.Monad.IO.Class |
| 74 |
import Data.Map.Strict (Map) |
| 75 |
import qualified GHC.LanguageExtensions as LangExt |
| 76 |
|
| 77 |
----------------------------------------------------------------------------- |
| 78 |
-- GHCi monad |
| 79 |
|
| 80 |
data GHCiState = GHCiState |
| 81 |
{ |
| 82 |
progname :: String, |
| 83 |
args :: [String], |
| 84 |
evalWrapper :: ForeignHValue, -- ^ of type @IO a -> IO a@ |
| 85 |
prompt :: PromptFunction, |
| 86 |
prompt_cont :: PromptFunction, |
| 87 |
editor :: String, |
| 88 |
stop :: String, |
| 89 |
options :: [GHCiOption], |
| 90 |
line_number :: !Int, -- ^ input line |
| 91 |
break_ctr :: !Int, |
| 92 |
breaks :: ![(Int, BreakLocation)], |
| 93 |
tickarrays :: ModuleEnv TickArray, |
| 94 |
-- ^ 'tickarrays' caches the 'TickArray' for loaded modules, |
| 95 |
-- so that we don't rebuild it each time the user sets |
| 96 |
-- a breakpoint. |
| 97 |
ghci_commands :: [Command], |
| 98 |
-- ^ available ghci commands |
| 99 |
ghci_macros :: [Command], |
| 100 |
-- ^ user-defined macros |
| 101 |
last_command :: Maybe Command, |
| 102 |
-- ^ @:@ at the GHCi prompt repeats the last command, so we |
| 103 |
-- remember it here |
| 104 |
cmd_wrapper :: InputT GHCi CommandResult -> InputT GHCi (Maybe Bool), |
| 105 |
-- ^ The command wrapper is run for each command or statement. |
| 106 |
-- The 'Bool' value denotes whether the command is successful and |
| 107 |
-- 'Nothing' means to exit GHCi. |
| 108 |
cmdqueue :: [String], |
| 109 |
|
| 110 |
remembered_ctx :: [InteractiveImport], |
| 111 |
-- ^ The imports that the user has asked for, via import |
| 112 |
-- declarations and :module commands. This list is |
| 113 |
-- persistent over :reloads (but any imports for modules |
| 114 |
-- that are not loaded are temporarily ignored). After a |
| 115 |
-- :load, all the home-package imports are stripped from |
| 116 |
-- this list. |
| 117 |
-- |
| 118 |
-- See bugs #2049, #1873, #1360 |
| 119 |
|
| 120 |
transient_ctx :: [InteractiveImport], |
| 121 |
-- ^ An import added automatically after a :load, usually of |
| 122 |
-- the most recently compiled module. May be empty if |
| 123 |
-- there are no modules loaded. This list is replaced by |
| 124 |
-- :load, :reload, and :add. In between it may be modified |
| 125 |
-- by :module. |
| 126 |
|
| 127 |
extra_imports :: [ImportDecl GhcPs], |
| 128 |
-- ^ These are "always-on" imports, added to the |
| 129 |
-- context regardless of what other imports we have. |
| 130 |
-- This is useful for adding imports that are required |
| 131 |
-- by setGHCiMonad. Be careful adding things here: |
| 132 |
-- you can create ambiguities if these imports overlap |
| 133 |
-- with other things in scope. |
| 134 |
-- |
| 135 |
-- NB. although this is not currently used by GHCi itself, |
| 136 |
-- it was added to support other front-ends that are based |
| 137 |
-- on the GHCi code. Potentially we could also expose |
| 138 |
-- this functionality via GHCi commands. |
| 139 |
|
| 140 |
prelude_imports :: [ImportDecl GhcPs], |
| 141 |
-- ^ These imports are added to the context when |
| 142 |
-- -XImplicitPrelude is on and we don't have a *-module |
| 143 |
-- in the context. They can also be overridden by another |
| 144 |
-- import for the same module, e.g. |
| 145 |
-- "import Prelude hiding (map)" |
| 146 |
|
| 147 |
ghc_e :: Bool, -- ^ True if this is 'ghc -e' (or runghc) |
| 148 |
|
| 149 |
short_help :: String, |
| 150 |
-- ^ help text to display to a user |
| 151 |
long_help :: String, |
| 152 |
lastErrorLocations :: IORef [(FastString, Int)], |
| 153 |
|
| 154 |
mod_infos :: !(Map ModuleName ModInfo), |
| 155 |
|
| 156 |
flushStdHandles :: ForeignHValue, |
| 157 |
-- ^ @hFlush stdout; hFlush stderr@ in the interpreter |
| 158 |
noBuffering :: ForeignHValue |
| 159 |
-- ^ @hSetBuffering NoBuffering@ for stdin/stdout/stderr |
| 160 |
} |
| 161 |
|
| 162 |
type TickArray = Array Int [(GHC.BreakIndex,RealSrcSpan)] |
| 163 |
|
| 164 |
-- | A GHCi command |
| 165 |
data Command |
| 166 |
= Command |
| 167 |
{ cmdName :: String |
| 168 |
-- ^ Name of GHCi command (e.g. "exit") |
| 169 |
, cmdAction :: String -> InputT GHCi Bool |
| 170 |
-- ^ The 'Bool' value denotes whether to exit GHCi |
| 171 |
, cmdHidden :: Bool |
| 172 |
-- ^ Commands which are excluded from default completion |
| 173 |
-- and @:help@ summary. This is usually set for commands not |
| 174 |
-- useful for interactive use but rather for IDEs. |
| 175 |
, cmdCompletionFunc :: CompletionFunc GHCi |
| 176 |
-- ^ 'CompletionFunc' for arguments |
| 177 |
} |
| 178 |
|
| 179 |
data CommandResult |
| 180 |
= CommandComplete |
| 181 |
{ cmdInput :: String |
| 182 |
, cmdResult :: Either SomeException (Maybe Bool) |
| 183 |
, cmdStats :: ActionStats |
| 184 |
} |
| 185 |
| CommandIncomplete |
| 186 |
-- ^ Unterminated multiline command |
| 187 |
deriving Show |
| 188 |
|
| 189 |
cmdSuccess :: Haskeline.MonadException m => CommandResult -> m (Maybe Bool) |
| 190 |
cmdSuccess CommandComplete{ cmdResult = Left e } = liftIO $ throwIO e |
| 191 |
cmdSuccess CommandComplete{ cmdResult = Right r } = return r |
| 192 |
cmdSuccess CommandIncomplete = return $ Just True |
| 193 |
|
| 194 |
type PromptFunction = [String] |
| 195 |
-> Int |
| 196 |
-> GHCi SDoc |
| 197 |
|
| 198 |
data GHCiOption |
| 199 |
= ShowTiming -- show time/allocs after evaluation |
| 200 |
| ShowType -- show the type of expressions |
| 201 |
| RevertCAFs -- revert CAFs after every evaluation |
| 202 |
| Multiline -- use multiline commands |
| 203 |
| CollectInfo -- collect and cache information about |
| 204 |
-- modules after load |
| 205 |
deriving Eq |
| 206 |
|
| 207 |
data BreakLocation |
| 208 |
= BreakLocation |
| 209 |
{ breakModule :: !GHC.Module |
| 210 |
, breakLoc :: !SrcSpan |
| 211 |
, breakTick :: {-# UNPACK #-} !Int |
| 212 |
, onBreakCmd :: String |
| 213 |
} |
| 214 |
|
| 215 |
instance Eq BreakLocation where |
| 216 |
loc1 == loc2 = breakModule loc1 == breakModule loc2 && |
| 217 |
breakTick loc1 == breakTick loc2 |
| 218 |
|
| 219 |
prettyLocations :: [(Int, BreakLocation)] -> SDoc |
| 220 |
prettyLocations [] = text "No active breakpoints." |
| 221 |
prettyLocations locs = vcat $ map (\(i, loc) -> brackets (int i) <+> ppr loc) $ reverse $ locs |
| 222 |
|
| 223 |
instance Outputable BreakLocation where |
| 224 |
ppr loc = (ppr $ breakModule loc) <+> ppr (breakLoc loc) <+> |
| 225 |
if null (onBreakCmd loc) |
| 226 |
then Outputable.empty |
| 227 |
else doubleQuotes (text (onBreakCmd loc)) |
| 228 |
|
| 229 |
recordBreak :: BreakLocation -> GHCi (Bool{- was already present -}, Int) |
| 230 |
recordBreak brkLoc = do |
| 231 |
st <- getGHCiState |
| 232 |
let oldActiveBreaks = breaks st |
| 233 |
-- don't store the same break point twice |
| 234 |
case [ nm | (nm, loc) <- oldActiveBreaks, loc == brkLoc ] of |
| 235 |
(nm:_) -> return (True, nm) |
| 236 |
[] -> do |
| 237 |
let oldCounter = break_ctr st |
| 238 |
newCounter = oldCounter + 1 |
| 239 |
setGHCiState $ st { break_ctr = newCounter, |
| 240 |
breaks = (oldCounter, brkLoc) : oldActiveBreaks |
| 241 |
} |
| 242 |
return (False, oldCounter) |
| 243 |
|
| 244 |
newtype GHCi a = GHCi { unGHCi :: IORef GHCiState -> Ghc a } |
| 245 |
|
| 246 |
reflectGHCi :: (Session, IORef GHCiState) -> GHCi a -> IO a |
| 247 |
reflectGHCi (s, gs) m = unGhc (unGHCi m gs) s |
| 248 |
|
| 249 |
reifyGHCi :: ((Session, IORef GHCiState) -> IO a) -> GHCi a |
| 250 |
reifyGHCi f = GHCi f' |
| 251 |
where |
| 252 |
-- f' :: IORef GHCiState -> Ghc a |
| 253 |
f' gs = reifyGhc (f'' gs) |
| 254 |
-- f'' :: IORef GHCiState -> Session -> IO a |
| 255 |
f'' gs s = f (s, gs) |
| 256 |
|
| 257 |
startGHCi :: GHCi a -> GHCiState -> Ghc a |
| 258 |
startGHCi g state = do ref <- liftIO $ newIORef state; unGHCi g ref |
| 259 |
|
| 260 |
instance Functor GHCi where |
| 261 |
fmap = liftM |
| 262 |
|
| 263 |
instance Applicative GHCi where |
| 264 |
pure a = GHCi $ \_ -> pure a |
| 265 |
(<*>) = ap |
| 266 |
|
| 267 |
instance Monad GHCi where |
| 268 |
(GHCi m) >>= k = GHCi $ \s -> m s >>= \a -> unGHCi (k a) s |
| 269 |
|
| 270 |
class HasGhciState m where |
| 271 |
getGHCiState :: m GHCiState |
| 272 |
setGHCiState :: GHCiState -> m () |
| 273 |
modifyGHCiState :: (GHCiState -> GHCiState) -> m () |
| 274 |
|
| 275 |
instance HasGhciState GHCi where |
| 276 |
getGHCiState = GHCi $ \r -> liftIO $ readIORef r |
| 277 |
setGHCiState s = GHCi $ \r -> liftIO $ writeIORef r s |
| 278 |
modifyGHCiState f = GHCi $ \r -> liftIO $ modifyIORef r f |
| 279 |
|
| 280 |
instance (MonadTrans t, Monad m, HasGhciState m) => HasGhciState (t m) where |
| 281 |
getGHCiState = lift getGHCiState |
| 282 |
setGHCiState = lift . setGHCiState |
| 283 |
modifyGHCiState = lift . modifyGHCiState |
| 284 |
|
| 285 |
liftGhc :: Ghc a -> GHCi a |
| 286 |
liftGhc m = GHCi $ \_ -> m |
| 287 |
|
| 288 |
instance MonadIO GHCi where |
| 289 |
liftIO = liftGhc . liftIO |
| 290 |
|
| 291 |
instance HasDynFlags GHCi where |
| 292 |
getDynFlags = getSessionDynFlags |
| 293 |
|
| 294 |
instance GhcMonad GHCi where |
| 295 |
setSession s' = liftGhc $ setSession s' |
| 296 |
getSession = liftGhc $ getSession |
| 297 |
|
| 298 |
instance HasDynFlags (InputT GHCi) where |
| 299 |
getDynFlags = lift getDynFlags |
| 300 |
|
| 301 |
instance GhcMonad (InputT GHCi) where |
| 302 |
setSession = lift . setSession |
| 303 |
getSession = lift getSession |
| 304 |
|
| 305 |
instance ExceptionMonad GHCi where |
| 306 |
gcatch m h = GHCi $ \r -> unGHCi m r `gcatch` (\e -> unGHCi (h e) r) |
| 307 |
gmask f = |
| 308 |
GHCi $ \s -> gmask $ \io_restore -> |
| 309 |
let |
| 310 |
g_restore (GHCi m) = GHCi $ \s' -> io_restore (m s') |
| 311 |
in |
| 312 |
unGHCi (f g_restore) s |
| 313 |
|
| 314 |
instance Haskeline.MonadException Ghc where |
| 315 |
controlIO f = Ghc $ \s -> Haskeline.controlIO $ \(Haskeline.RunIO run) -> let |
| 316 |
run' = Haskeline.RunIO (fmap (Ghc . const) . run . flip unGhc s) |
| 317 |
in fmap (flip unGhc s) $ f run' |
| 318 |
|
| 319 |
instance Haskeline.MonadException GHCi where |
| 320 |
controlIO f = GHCi $ \s -> Haskeline.controlIO $ \(Haskeline.RunIO run) -> let |
| 321 |
run' = Haskeline.RunIO (fmap (GHCi . const) . run . flip unGHCi s) |
| 322 |
in fmap (flip unGHCi s) $ f run' |
| 323 |
|
| 324 |
instance ExceptionMonad (InputT GHCi) where |
| 325 |
gcatch = Haskeline.catch |
| 326 |
gmask f = Haskeline.liftIOOp gmask (f . Haskeline.liftIOOp_) |
| 327 |
|
| 328 |
isOptionSet :: GHCiOption -> GHCi Bool |
| 329 |
isOptionSet opt |
| 330 |
= do st <- getGHCiState |
| 331 |
return (opt `elem` options st) |
| 332 |
|
| 333 |
setOption :: GHCiOption -> GHCi () |
| 334 |
setOption opt |
| 335 |
= do st <- getGHCiState |
| 336 |
setGHCiState (st{ options = opt : filter (/= opt) (options st) }) |
| 337 |
|
| 338 |
unsetOption :: GHCiOption -> GHCi () |
| 339 |
unsetOption opt |
| 340 |
= do st <- getGHCiState |
| 341 |
setGHCiState (st{ options = filter (/= opt) (options st) }) |
| 342 |
|
| 343 |
printForUserNeverQualify :: GhcMonad m => SDoc -> m () |
| 344 |
printForUserNeverQualify doc = do |
| 345 |
dflags <- getDynFlags |
| 346 |
liftIO $ Outputable.printForUser dflags stdout neverQualify doc |
| 347 |
|
| 348 |
printForUserModInfo :: GhcMonad m => GHC.ModuleInfo -> SDoc -> m () |
| 349 |
printForUserModInfo info doc = do |
| 350 |
dflags <- getDynFlags |
| 351 |
mUnqual <- GHC.mkPrintUnqualifiedForModule info |
| 352 |
unqual <- maybe GHC.getPrintUnqual return mUnqual |
| 353 |
liftIO $ Outputable.printForUser dflags stdout unqual doc |
| 354 |
|
| 355 |
printForUser :: GhcMonad m => SDoc -> m () |
| 356 |
printForUser doc = do |
| 357 |
unqual <- GHC.getPrintUnqual |
| 358 |
dflags <- getDynFlags |
| 359 |
liftIO $ Outputable.printForUser dflags stdout unqual doc |
| 360 |
|
| 361 |
printForUserPartWay :: SDoc -> GHCi () |
| 362 |
printForUserPartWay doc = do |
| 363 |
unqual <- GHC.getPrintUnqual |
| 364 |
dflags <- getDynFlags |
| 365 |
liftIO $ Outputable.printForUserPartWay dflags stdout (pprUserLength dflags) unqual doc |
| 366 |
|
| 367 |
-- | Run a single Haskell expression |
| 368 |
runStmt :: GhciLStmt GhcPs -> String -> GHC.SingleStep -> GHCi (Maybe GHC.ExecResult) |
| 369 |
runStmt stmt stmt_text step = do |
| 370 |
st <- getGHCiState |
| 371 |
GHC.handleSourceError (\e -> do GHC.printException e; return Nothing) $ do |
| 372 |
let opts = GHC.execOptions |
| 373 |
{ GHC.execSourceFile = progname st |
| 374 |
, GHC.execLineNumber = line_number st |
| 375 |
, GHC.execSingleStep = step |
| 376 |
, GHC.execWrap = \fhv -> EvalApp (EvalThis (evalWrapper st)) |
| 377 |
(EvalThis fhv) } |
| 378 |
Just <$> GHC.execStmt' stmt stmt_text opts |
| 379 |
|
| 380 |
runDecls :: String -> GHCi (Maybe [GHC.Name]) |
| 381 |
runDecls decls = do |
| 382 |
st <- getGHCiState |
| 383 |
reifyGHCi $ \x -> |
| 384 |
withProgName (progname st) $ |
| 385 |
withArgs (args st) $ |
| 386 |
reflectGHCi x $ do |
| 387 |
GHC.handleSourceError (\e -> do GHC.printException e; |
| 388 |
return Nothing) $ do |
| 389 |
r <- GHC.runDeclsWithLocation (progname st) (line_number st) decls |
| 390 |
return (Just r) |
| 391 |
|
| 392 |
runDecls' :: [LHsDecl GhcPs] -> GHCi (Maybe [GHC.Name]) |
| 393 |
runDecls' decls = do |
| 394 |
st <- getGHCiState |
| 395 |
reifyGHCi $ \x -> |
| 396 |
withProgName (progname st) $ |
| 397 |
withArgs (args st) $ |
| 398 |
reflectGHCi x $ |
| 399 |
GHC.handleSourceError |
| 400 |
(\e -> do GHC.printException e; |
| 401 |
return Nothing) |
| 402 |
(Just <$> GHC.runParsedDecls decls) |
| 403 |
|
| 404 |
resume :: (SrcSpan -> Bool) -> GHC.SingleStep -> GHCi GHC.ExecResult |
| 405 |
resume canLogSpan step = do |
| 406 |
st <- getGHCiState |
| 407 |
reifyGHCi $ \x -> |
| 408 |
withProgName (progname st) $ |
| 409 |
withArgs (args st) $ |
| 410 |
reflectGHCi x $ do |
| 411 |
GHC.resumeExec canLogSpan step |
| 412 |
|
| 413 |
-- -------------------------------------------------------------------------- |
| 414 |
-- timing & statistics |
| 415 |
|
| 416 |
data ActionStats = ActionStats |
| 417 |
{ actionAllocs :: Maybe Integer |
| 418 |
, actionElapsedTime :: Double |
| 419 |
} deriving Show |
| 420 |
|
| 421 |
runAndPrintStats |
| 422 |
:: (a -> Maybe Integer) |
| 423 |
-> InputT GHCi a |
| 424 |
-> InputT GHCi (ActionStats, Either SomeException a) |
| 425 |
runAndPrintStats getAllocs action = do |
| 426 |
result <- runWithStats getAllocs action |
| 427 |
case result of |
| 428 |
(stats, Right{}) -> do |
| 429 |
showTiming <- lift $ isOptionSet ShowTiming |
| 430 |
when showTiming $ do |
| 431 |
dflags <- getDynFlags |
| 432 |
liftIO $ printStats dflags stats |
| 433 |
_ -> return () |
| 434 |
return result |
| 435 |
|
| 436 |
runWithStats |
| 437 |
:: ExceptionMonad m |
| 438 |
=> (a -> Maybe Integer) -> m a -> m (ActionStats, Either SomeException a) |
| 439 |
runWithStats getAllocs action = do |
| 440 |
t0 <- liftIO getCurrentTime |
| 441 |
result <- gtry action |
| 442 |
let allocs = either (const Nothing) getAllocs result |
| 443 |
t1 <- liftIO getCurrentTime |
| 444 |
let elapsedTime = realToFrac $ t1 `diffUTCTime` t0 |
| 445 |
return (ActionStats allocs elapsedTime, result) |
| 446 |
|
| 447 |
printStats :: DynFlags -> ActionStats -> IO () |
| 448 |
printStats dflags ActionStats{actionAllocs = mallocs, actionElapsedTime = secs} |
| 449 |
= do let secs_str = showFFloat (Just 2) secs |
| 450 |
putStrLn (showSDoc dflags ( |
| 451 |
parens (text (secs_str "") <+> text "secs" <> comma <+> |
| 452 |
case mallocs of |
| 453 |
Nothing -> empty |
| 454 |
Just allocs -> |
| 455 |
text (separateThousands allocs) <+> text "bytes"))) |
| 456 |
where |
| 457 |
separateThousands n = reverse . sep . reverse . show $ n |
| 458 |
where sep n' |
| 459 |
| n' `lengthAtMost` 3 = n' |
| 460 |
| otherwise = take 3 n' ++ "," ++ sep (drop 3 n') |
| 461 |
|
| 462 |
----------------------------------------------------------------------------- |
| 463 |
-- reverting CAFs |
| 464 |
|
| 465 |
revertCAFs :: GHCi () |
| 466 |
revertCAFs = do |
| 467 |
liftIO rts_revertCAFs |
| 468 |
s <- getGHCiState |
| 469 |
when (not (ghc_e s)) turnOffBuffering |
| 470 |
-- Have to turn off buffering again, because we just |
| 471 |
-- reverted stdout, stderr & stdin to their defaults. |
| 472 |
|
| 473 |
foreign import ccall "revertCAFs" rts_revertCAFs :: IO () |
| 474 |
-- Make it "safe", just in case |
| 475 |
|
| 476 |
----------------------------------------------------------------------------- |
| 477 |
-- To flush buffers for the *interpreted* computation we need |
| 478 |
-- to refer to *its* stdout/stderr handles |
| 479 |
|
| 480 |
-- | Compile "hFlush stdout; hFlush stderr" once, so we can use it repeatedly |
| 481 |
initInterpBuffering :: Ghc (ForeignHValue, ForeignHValue) |
| 482 |
initInterpBuffering = do |
| 483 |
let mkHelperExpr :: OccName -> Ghc ForeignHValue |
| 484 |
mkHelperExpr occ = |
| 485 |
GHC.compileParsedExprRemote |
| 486 |
$ GHC.nlHsVar $ RdrName.mkOrig gHC_GHCI_HELPERS occ |
| 487 |
nobuf <- mkHelperExpr $ mkVarOcc "disableBuffering" |
| 488 |
flush <- mkHelperExpr $ mkVarOcc "flushAll" |
| 489 |
return (nobuf, flush) |
| 490 |
|
| 491 |
-- | Invoke "hFlush stdout; hFlush stderr" in the interpreter |
| 492 |
flushInterpBuffers :: GHCi () |
| 493 |
flushInterpBuffers = do |
| 494 |
st <- getGHCiState |
| 495 |
hsc_env <- GHC.getSession |
| 496 |
liftIO $ evalIO hsc_env (flushStdHandles st) |
| 497 |
|
| 498 |
-- | Turn off buffering for stdin, stdout, and stderr in the interpreter |
| 499 |
turnOffBuffering :: GHCi () |
| 500 |
turnOffBuffering = do |
| 501 |
st <- getGHCiState |
| 502 |
turnOffBuffering_ (noBuffering st) |
| 503 |
|
| 504 |
turnOffBuffering_ :: GhcMonad m => ForeignHValue -> m () |
| 505 |
turnOffBuffering_ fhv = do |
| 506 |
hsc_env <- getSession |
| 507 |
liftIO $ evalIO hsc_env fhv |
| 508 |
|
| 509 |
mkEvalWrapper :: GhcMonad m => String -> [String] -> m ForeignHValue |
| 510 |
mkEvalWrapper progname args = |
| 511 |
runInternal $ GHC.compileParsedExprRemote |
| 512 |
$ evalWrapper `GHC.mkHsApp` nlHsString progname |
| 513 |
`GHC.mkHsApp` nlList (map nlHsString args) |
| 514 |
where |
| 515 |
nlHsString = nlHsLit . mkHsString |
| 516 |
evalWrapper = |
| 517 |
GHC.nlHsVar $ RdrName.mkOrig gHC_GHCI_HELPERS (mkVarOcc "evalWrapper") |
| 518 |
|
| 519 |
-- | Run a 'GhcMonad' action to compile an expression for internal usage. |
| 520 |
runInternal :: GhcMonad m => m a -> m a |
| 521 |
runInternal = |
| 522 |
withTempSession mkTempSession |
| 523 |
where |
| 524 |
mkTempSession hsc_env = hsc_env |
| 525 |
{ hsc_dflags = (hsc_dflags hsc_env) |
| 526 |
-- RebindableSyntax can wreak havoc with GHCi in several ways |
| 527 |
-- (see #13385 and #14342 for examples), so we take care to disable it |
| 528 |
-- for the duration of running expressions that are internal to GHCi. |
| 529 |
`xopt_unset` LangExt.RebindableSyntax |
| 530 |
-- We heavily depend on -fimplicit-import-qualified to compile expr |
| 531 |
-- with fully qualified names without imports. |
| 532 |
`gopt_set` Opt_ImplicitImportQualified |
| 533 |
} |
| 534 |
|
| 535 |
compileGHCiExpr :: GhcMonad m => String -> m ForeignHValue |
| 536 |
compileGHCiExpr expr = runInternal $ GHC.compileExprRemote expr |