Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/Hint/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Hint.Context (
isModuleInterpreted,
loadModules, getLoadedModules, setTopLevelModules,
setImports, setImportsQ,
reset,
reset, reload,

PhantomModule(..),
cleanPhantomModules,
Expand Down Expand Up @@ -197,14 +197,18 @@ isPhantomModule mn = do (as,zs) <- getPhantomModules
loadModules :: MonadInterpreter m => [String] -> m ()
loadModules fs = do -- first, unload everything, and do some clean-up
reset
doLoad fs `catchIE` (\e -> reset >> throwM e)

doLoad :: MonadInterpreter m => [String] -> m ()
doLoad fs = mayFail $ do
targets <- mapM (\f->runGhc2 GHC.guessTarget f Nothing) fs
--
runGhc1 GHC.setTargets targets
res <- runGhc1 GHC.load GHC.LoadAllTargets
targets <- mapM (\f->runGhc2 GHC.guessTarget f Nothing) fs
runGhc1 GHC.setTargets targets
reload

-- | Reload any loaded modules that have changed, similar to a @:reload@ in GHCi.
-- Note that you may need to restore your set of top level modules afterwards.
reload :: MonadInterpreter m => m ()
reload = doLoad GHC.LoadAllTargets `catchIE` (\e -> reset >> throwM e)

doLoad :: MonadInterpreter m => GHC.LoadHowMuch -> m ()
doLoad howmuch = mayFail $ do
res <- runGhc1 GHC.load howmuch
-- loading the targets removes the support module
reinstallSupportModule
return $ guard (isSucceeded res) >> Just ()
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Haskell/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module Language.Haskell.Interpreter(
ModuleName, isModuleInterpreted,
loadModules, getLoadedModules, setTopLevelModules,
setImports, setImportsQ,
reset,
reset, reload,
-- ** Module querying
ModuleElem(..), Id, name, children,
getModuleExports,
Expand Down
43 changes: 40 additions & 3 deletions unit-tests/run-unit-tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import qualified Test.HUnit as HUnit

import Language.Haskell.Interpreter

test_reload_modified :: TestCase
test_reload_modified = TestCase "reload_modified" [mod_file] $ do
test_load_modified :: TestCase
test_load_modified = TestCase "load_modified" [mod_file] $ do
liftIO $ writeFile mod_file mod_v1
f_v1 <- get_f
--
Expand All @@ -30,7 +30,7 @@ test_reload_modified = TestCase "reload_modified" [mod_file] $ do
--
liftIO $ (f_v1 5, f_v2 5) @?= (5, 6)
--
where mod_name = "TEST_ReloadModified"
where mod_name = "TEST_LoadModified"
mod_file = mod_name ++ ".hs"
--
mod_v1 = unlines ["module " ++ mod_name,
Expand All @@ -46,6 +46,42 @@ test_reload_modified = TestCase "reload_modified" [mod_file] $ do
setTopLevelModules [mod_name]
interpret "f" (as :: Int -> Int)

test_reload_modified :: TestCase
test_reload_modified = TestCase "reload_modified" [mod_file_a, mod_file_b] $ do
liftIO $ writeFile mod_file_a mod_a_v1
loadModules [mod_file_a]
setTopLevelModules [mod_name_a]
f_v1 <- interpret "fin" (as :: String -> String)
let a = (f_v1 "foo")
--
liftIO $ writeFile mod_file_a mod_a_v2
liftIO $ writeFile mod_file_b mod_b
set [searchPath := ["."]]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite sure why this is necessary, will need to spend some time debugging. It breaks if you move it to the top, which isn't good!

reload
setTopLevelModules [mod_name_a]
f_v2 <- interpret "fun" (as :: String -> String)
let b = (f_v2 "foo")
--
liftIO $ (a, b) @?= ("foooof", "ooffoo")
--
where mod_name_a = "TEST_ReloadModified"
mod_file_a = mod_name_a ++ ".hs"
--
mod_name_b = "TEST_ReloadModifiedB"
mod_file_b = mod_name_b ++ ".hs"
--
mod_a_v1 = unlines ["module " ++ mod_name_a ++ " where",
"fin :: String -> String",
"fin s = s ++ reverse s"]
mod_a_v2 = unlines ["module " ++ mod_name_a ++ " where",
"import " ++ mod_name_b ++ " (gi)",
"fun :: String -> String",
"fun = gi"]
mod_b = unlines ["module " ++ mod_name_b ++ " where",
"gi :: String -> String",
"gi s = reverse s ++ s"]


test_lang_exts :: TestCase
test_lang_exts = TestCase "lang_exts" [mod_file] $ do
liftIO $ writeFile mod_file "data T where T :: T"
Expand Down Expand Up @@ -202,6 +238,7 @@ test_only_one_instance = TestCase "only_one_instance" [] $ liftIO $ do

tests :: [TestCase]
tests = [test_reload_modified
,test_load_modified
,test_lang_exts
,test_work_in_main
,test_comments_in_expr
Expand Down