on master help sign in
Merge pull request #338 from jsl/fix_unsafe_io

Remove instance of unsafePerformIO

commitbbede87f1ad0d5a7378f34ad7f4429255a208bd3 authorEvan Czaplicki <info@elm-lang.org> dateThu 14 Nov 2013, 07:26 (13 years ago) parent0fcd3428fc72d2
4 files changed+36−26
compiler/Compiler.hs+8−4 compiler/Initialize.hs+10−11 compiler/Language/Elm.hs+16−6 compiler/Metadata/Prelude.hs+2−5
compiler/Compiler.hs+8−4
@@ -184,10 +184,14 @@ getRuntime flags =
184184build :: Flags -> FilePath -> IO ()
185185build flags rootFile = do
186186 let noPrelude = no_prelude flags
187− files <- if make flags then getSortedDependencies (src_dir flags) noPrelude rootFile
188− else return [rootFile]
189− let ifaces = if noPrelude then Map.empty else Prelude.interfaces
190− (moduleName, interfaces) <- buildFiles flags (length files) ifaces "" files
187+ builtIns <- if noPrelude then return Map.empty else Prelude.interfaces
188+
189+ files <- if make flags
190+ then getSortedDependencies (src_dir flags) builtIns rootFile
191+ else return [rootFile]
192+
193+ (moduleName, interfaces) <- buildFiles flags (length files) builtIns "" files
194+
191195 js <- foldM appendToOutput BS.empty files
192196
193197 (extension, code) <- case only_js flags of
compiler/Initialize.hs+10−11
@@ -63,9 +63,9 @@ buildFromSource noPrelude interfaces source =
6363 return $ metaModule { types = types }
6464
6565
66−getSortedDependencies :: [FilePath] -> Bool -> FilePath -> IO [String]
67−getSortedDependencies srcDirs noPrelude root =
68− sortDeps =<< readDeps srcDirs noPrelude root
66+getSortedDependencies :: [FilePath] -> Interfaces -> FilePath -> IO [String]
67+getSortedDependencies srcDirs builtIns root =
68+ sortDeps =<< readDeps srcDirs builtIns root
6969
7070type Deps = (FilePath, String, [String])
7171
@@ -80,14 +80,13 @@ sortDeps depends =
8080 mistakes = filter (\scc -> length scc > 1) sccs
8181 msg = "A cyclical module dependency or was detected in: "
8282
83−readDeps :: [FilePath] -> Bool -> FilePath -> IO [Deps]
84−readDeps srcDirs noPrelude root = evalStateT (go root) Set.empty
83+readDeps :: [FilePath] -> Interfaces -> FilePath -> IO [Deps]
84+readDeps srcDirs builtIns root = do
85+ let ifaces = (Set.fromList . Map.keys) builtIns
86+ evalStateT (go ifaces root) Set.empty
8587 where
86− builtIns = if noPrelude then Set.empty
87− else Set.fromList (Map.keys Prelude.interfaces)
88−
89− go :: FilePath -> StateT (Set.Set String) IO [Deps]
90− go root = do
88+ go :: Set.Set String -> FilePath -> StateT (Set.Set String) IO [Deps]
89+ go builtIns root = do
9190 (root', txt) <- liftIO $ getFile srcDirs root
9291 case Parse.dependencies txt of
9392 Left err -> liftIO (putStrLn msg >> print err >> exitFailure)
@@ -98,7 +97,7 @@ readDeps srcDirs noPrelude root = evalStateT (go root) Set.empty
9897 let realDeps = Set.difference (Set.fromList deps) builtIns
9998 newDeps = Set.difference (Set.filter (not . isNative) realDeps) seen
10099 put (Set.insert name (Set.union newDeps seen))
101− rest <- mapM (go . toFilePath) (Set.toList newDeps)
100+ rest <- mapM (go builtIns . toFilePath) (Set.toList newDeps)
102101 return ((makeRelative "." root', name, Set.toList realDeps) : concat rest)
103102
104103getFile :: [FilePath] -> FilePath -> IO (FilePath,String)
compiler/Language/Elm.hs+16−6
@@ -5,7 +5,14 @@
55 <http://elm-lang.org/Documentation.elm>, and many interactive examples are
66 available at <http://elm-lang.org/Examples.elm>
77-}
8−module Language.Elm (compile, moduleName, runtime, docs) where
8+module Language.Elm ( compile
9+ , interfaces
10+ , moduleName
11+ , runtime
12+ , docs
13+ , Interfaces
14+ , ModuleInterface )
15+where
916
1017import qualified Data.List as List
1118import qualified Data.Map as Map
@@ -21,11 +28,14 @@ import Paths_Elm
2128
2229-- |This function compiles Elm code to JavaScript. It will return either
2330-- an error message or the compiled JS code.
24−compile :: String -> Either String String
25−compile source =
26− case buildFromSource False Prelude.interfaces source of
27− Left docs -> Left . unlines . List.intersperse "" $ map P.render docs
28− Right modul -> Right $ jsModule (modul :: MetadataModule () ())
31+compile :: Interfaces -> String -> Either String String
32+compile ifaces source = do
33+ case buildFromSource False ifaces source of
34+ Left docs -> Left . unlines . List.intersperse "" $ map P.render docs
35+ Right modul -> Right $ jsModule (modul :: MetadataModule () ())
36+
37+interfaces :: IO Interfaces
38+interfaces = Prelude.interfaces
2939
3040-- |This function extracts the module name of a given source program.
3141moduleName :: String -> Maybe String
compiler/Metadata/Prelude.hs+2−5
@@ -7,7 +7,6 @@ import System.Directory
77import System.Exit
88import System.FilePath
99import System.IO
10−import System.IO.Unsafe (unsafePerformIO)
1110import SourceSyntax.Module
1211import qualified Data.Binary as Binary
1312import qualified Data.ByteString.Lazy as BS
@@ -31,10 +30,8 @@ prelude = text ++ map (\n -> (n, Hiding [])) modules
3130 modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
3231 , "Graphics.Element", "Color", "Graphics.Collage" ]
3332
34−{-# NOINLINE interfaces #-}
35−interfaces :: Interfaces
36−interfaces =
37− unsafePerformIO (safeReadDocs =<< Path.getDataFileName "interfaces.data")
33+interfaces :: IO Interfaces
34+interfaces = safeReadDocs =<< Path.getDataFileName "interfaces.data"
3835
3936safeReadDocs :: FilePath -> IO Interfaces
4037safeReadDocs name =