Unverified Commit 9741786d authored by David Davó's avatar David Davó Committed by GitHub

Merge branch 'gibiansky:master' into master

parents cc718f3e 6f1b51bb
...@@ -698,6 +698,8 @@ evalCommand _ (Directive LoadFile names) state = wrapExecution state $ do ...@@ -698,6 +698,8 @@ evalCommand _ (Directive LoadFile names) state = wrapExecution state $ do
doLoadModule filename modName doLoadModule filename modName
return (ManyDisplay displays) return (ManyDisplay displays)
evalCommand _ (Directive Reload _) state = wrapExecution state doReload
evalCommand publish (Directive ShellCmd cmd) state = wrapExecution state $ evalCommand publish (Directive ShellCmd cmd) state = wrapExecution state $
-- Assume the first character of 'cmd' is '!'. -- Assume the first character of 'cmd' is '!'.
case words $ drop 1 cmd of case words $ drop 1 cmd of
...@@ -1151,6 +1153,67 @@ doLoadModule name modName = do ...@@ -1151,6 +1153,67 @@ doLoadModule name modName = do
return $ displayError $ "Failed to load module " ++ modName ++ ": " ++ show exception return $ displayError $ "Failed to load module " ++ modName ++ ": " ++ show exception
doReload :: Ghc Display
doReload = do
-- Remember which modules we've loaded before.
importedModules <- getContext
flip gcatch (unload importedModules) $ do
-- Compile loaded modules.
flags <- getSessionDynFlags
errRef <- liftIO $ newIORef []
_ <- setSessionDynFlags $ flip gopt_set Opt_BuildDynamicToo
flags
{ hscTarget = objTarget flags
#if MIN_VERSION_ghc(9,0,0)
, log_action = \_dflags _warn _sev _srcspan msg -> modifyIORef' errRef (showSDoc flags msg :)
#else
, log_action = \_dflags _sev _srcspan _ppr _style msg -> modifyIORef' errRef (showSDoc flags msg :)
#endif
}
-- Store the old targets in case of failure.
oldTargets <- getTargets
result <- load LoadAllTargets
-- Reset the context, since loading things screws it up.
initializeItVariable
-- Reset targets if we failed.
case result of
Failed -> setTargets oldTargets
Succeeded{} -> return ()
-- Add imports
setContext importedModules
-- Switch back to interpreted mode.
_ <- setSessionDynFlags flags
case result of
Succeeded -> return mempty
Failed -> do
errorStrs <- unlines <$> reverse <$> liftIO (readIORef errRef)
return $ displayError $ "Failed to reload.\n" ++ errorStrs
where
unload :: [InteractiveImport] -> SomeException -> Ghc Display
unload imported exception = do
print $ show exception
-- Explicitly clear targets
setTargets []
_ <- load LoadAllTargets
-- Switch to interpreted mode!
flags <- getSessionDynFlags
_ <- setSessionDynFlags flags { hscTarget = HscInterpreted }
-- Return to old context, make sure we have `it`.
setContext imported
initializeItVariable
return $ displayError $ "Failed to reload."
objTarget :: DynFlags -> HscTarget objTarget :: DynFlags -> HscTarget
#if MIN_VERSION_ghc(8,10,0) #if MIN_VERSION_ghc(8,10,0)
objTarget = defaultObjectTarget objTarget = defaultObjectTarget
......
...@@ -65,6 +65,7 @@ data DirectiveType = GetType -- ^ Get the type of an expression via ':type' ...@@ -65,6 +65,7 @@ data DirectiveType = GetType -- ^ Get the type of an expression via ':type'
| GetKindBang -- ^ Get the kind and normalised type via ':kind!'. | GetKindBang -- ^ Get the kind and normalised type via ':kind!'.
| LoadModule -- ^ Load and unload modules via ':module'. | LoadModule -- ^ Load and unload modules via ':module'.
| SPrint -- ^ Print without evaluating via ':sprint'. | SPrint -- ^ Print without evaluating via ':sprint'.
| Reload -- ^ Reload.
deriving (Show, Eq) deriving (Show, Eq)
-- | Pragma types. Only LANGUAGE pragmas are currently supported. Other pragma types are kept around -- | Pragma types. Only LANGUAGE pragmas are currently supported. Other pragma types are kept around
...@@ -288,6 +289,7 @@ parseDirective (':':directive) ln = ...@@ -288,6 +289,7 @@ parseDirective (':':directive) ln =
, (SetExtension, "extension") , (SetExtension, "extension")
, (GetHelp, "?") , (GetHelp, "?")
, (GetHelp, "help") , (GetHelp, "help")
, (Reload, "reload")
, (SPrint, "sprint") , (SPrint, "sprint")
] ]
parseDirective _ _ = error "Directive must start with colon!" parseDirective _ _ = error "Directive must start with colon!"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment