Fixed some issues with :load, closes #140

This commit is contained in:
Andrew Gibiansky 2014-05-17 22:06:44 -07:00
parent 5b918068bf
commit 0fc3a1a9bf
2 changed files with 47 additions and 36 deletions

View File

@ -36,20 +36,23 @@
"cell_type": "code",
"collapsed": false,
"input": [
"1+1"
":load a/Test.hs\n",
"test"
],
"language": "python",
"metadata": {},
"metadata": {
"hidden": false
},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"2"
"3"
]
}
],
"prompt_number": 1
"prompt_number": 14
},
{
"cell_type": "code",
@ -93,9 +96,7 @@
"parser"
],
"language": "python",
"metadata": {
"hidden": false
},
"metadata": {},
"outputs": [
{
"javascript": [

View File

@ -480,11 +480,8 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do
let filename = if endswith ".hs" name
then name
else name ++ ".hs"
let modName = replace "/" "." $
if endswith ".hs" name
then replace ".hs" "" name
else name
contents <- readFile $ fpFromString filename
modName <- intercalate "." <$> getModuleName contents
doLoadModule filename modName
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $
@ -920,43 +917,56 @@ readChars handle delims nchars = do
doLoadModule :: String -> String -> Ghc Display
doLoadModule name modName = flip gcatch unload $ do
-- Compile loaded modules.
flags <- getSessionDynFlags
let objTarget = defaultObjectTarget
setSessionDynFlags flags{ hscTarget = objTarget }
doLoadModule name modName = do
-- Remember which modules we've loaded before.
importedModules <- getContext
-- Create a new target
target <- guessTarget name Nothing
addTarget target
result <- load LoadAllTargets
flip gcatch (unload importedModules) $ do
-- Compile loaded modules.
flags <- getSessionDynFlags
let objTarget = defaultObjectTarget
setSessionDynFlags flags{ hscTarget = objTarget }
-- Reset the context, since loading things screws it up.
initializeItVariable
-- Clear old targets to be sure.
setTargets []
load LoadAllTargets
-- Add imports
importDecl <- parseImportDecl $ "import " ++ modName
let implicitImport = importDecl { ideclImplicit = True }
setContext $ IIDecl implicitImport : importedModules
-- Load the new target.
target <- guessTarget name Nothing
addTarget target
result <- load LoadAllTargets
-- Switch back to interpreted mode.
flags <- getSessionDynFlags
setSessionDynFlags flags{ hscTarget = HscInterpreted }
-- Reset the context, since loading things screws it up.
initializeItVariable
case result of
Succeeded -> return mempty
Failed -> return $ displayError $ "Failed to load module " ++ modName
-- Add imports
importDecl <- parseImportDecl $ "import " ++ modName
let implicitImport = importDecl { ideclImplicit = True }
setContext $ IIDecl implicitImport : importedModules
-- Switch back to interpreted mode.
flags <- getSessionDynFlags
setSessionDynFlags flags{ hscTarget = HscInterpreted }
case result of
Succeeded -> return mempty
Failed -> return $ displayError $ "Failed to load module " ++ modName
where
unload :: SomeException -> Ghc Display
unload exception = do
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 load module " ++ modName ++ ": " ++ show exception
keepingItVariable :: Interpreter a -> Interpreter a