Commit 0fc3a1a9 authored by Andrew Gibiansky's avatar Andrew Gibiansky

Fixed some issues with :load, closes #140

parent 5b918068
...@@ -36,20 +36,23 @@ ...@@ -36,20 +36,23 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"1+1" ":load a/Test.hs\n",
"test"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {
"hidden": false
},
"outputs": [ "outputs": [
{ {
"metadata": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data",
"text": [ "text": [
"2" "3"
] ]
} }
], ],
"prompt_number": 1 "prompt_number": 14
}, },
{ {
"cell_type": "code", "cell_type": "code",
...@@ -93,9 +96,7 @@ ...@@ -93,9 +96,7 @@
"parser" "parser"
], ],
"language": "python", "language": "python",
"metadata": { "metadata": {},
"hidden": false
},
"outputs": [ "outputs": [
{ {
"javascript": [ "javascript": [
......
...@@ -480,11 +480,8 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do ...@@ -480,11 +480,8 @@ evalCommand _ (Directive LoadFile name) state = wrapExecution state $ do
let filename = if endswith ".hs" name let filename = if endswith ".hs" name
then name then name
else name ++ ".hs" else name ++ ".hs"
let modName = replace "/" "." $ contents <- readFile $ fpFromString filename
if endswith ".hs" name modName <- intercalate "." <$> getModuleName contents
then replace ".hs" "" name
else name
doLoadModule filename modName doLoadModule filename modName
evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $ evalCommand publish (Directive ShellCmd ('!':cmd)) state = wrapExecution state $ liftIO $
...@@ -920,43 +917,56 @@ readChars handle delims nchars = do ...@@ -920,43 +917,56 @@ readChars handle delims nchars = do
doLoadModule :: String -> String -> Ghc Display doLoadModule :: String -> String -> Ghc Display
doLoadModule name modName = flip gcatch unload $ do doLoadModule name modName = do
-- Compile loaded modules.
flags <- getSessionDynFlags
let objTarget = defaultObjectTarget
setSessionDynFlags flags{ hscTarget = objTarget }
-- Remember which modules we've loaded before. -- Remember which modules we've loaded before.
importedModules <- getContext importedModules <- getContext
-- Create a new target flip gcatch (unload importedModules) $ do
target <- guessTarget name Nothing -- Compile loaded modules.
addTarget target flags <- getSessionDynFlags
result <- load LoadAllTargets let objTarget = defaultObjectTarget
setSessionDynFlags flags{ hscTarget = objTarget }
-- Reset the context, since loading things screws it up. -- Clear old targets to be sure.
initializeItVariable setTargets []
load LoadAllTargets
-- Add imports -- Load the new target.
importDecl <- parseImportDecl $ "import " ++ modName target <- guessTarget name Nothing
let implicitImport = importDecl { ideclImplicit = True } addTarget target
setContext $ IIDecl implicitImport : importedModules result <- load LoadAllTargets
-- Switch back to interpreted mode. -- Reset the context, since loading things screws it up.
flags <- getSessionDynFlags initializeItVariable
setSessionDynFlags flags{ hscTarget = HscInterpreted }
case result of -- Add imports
Succeeded -> return mempty importDecl <- parseImportDecl $ "import " ++ modName
Failed -> return $ displayError $ "Failed to load module " ++ 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 where
unload :: SomeException -> Ghc Display unload :: [InteractiveImport] -> SomeException -> Ghc Display
unload exception = do unload imported exception = do
print $ show exception
-- Explicitly clear targets -- Explicitly clear targets
setTargets [] setTargets []
load LoadAllTargets load LoadAllTargets
-- Switch to interpreted mode!
flags <- getSessionDynFlags
setSessionDynFlags flags{ hscTarget = HscInterpreted }
-- Return to old context, make sure we have `it`.
setContext imported
initializeItVariable initializeItVariable
return $ displayError $ "Failed to load module " ++ modName ++ ": " ++ show exception return $ displayError $ "Failed to load module " ++ modName ++ ": " ++ show exception
keepingItVariable :: Interpreter a -> Interpreter a keepingItVariable :: Interpreter a -> Interpreter a
......
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