on master help sign in
Remove instance of unsafePerformIO
8fc72d2Justin Leitgeb
compiler/Compiler.hs+8−4
@@ -184,10 +184,14 @@ getRuntime flags =
184build :: Flags -> FilePath -> IO ()
184build :: Flags -> FilePath -> IO ()
185build flags rootFile = do
185build flags rootFile = do
186 let noPrelude = no_prelude flags
186 let noPrelude = no_prelude flags
187+ builtIns <- if noPrelude then return Map.empty else Prelude.interfaces
188+
189+ files <- if make flags
187− files <- if make flags then getSortedDependencies (src_dir flags) noPrelude rootFile
190+ then getSortedDependencies (src_dir flags) builtIns rootFile
188− else return [rootFile]
191+ else return [rootFile]
189− let ifaces = if noPrelude then Map.empty else Prelude.interfaces
192+
190− (moduleName, interfaces) <- buildFiles flags (length files) ifaces "" files
193+ (moduleName, interfaces) <- buildFiles flags (length files) builtIns "" files
194+
191 js <- foldM appendToOutput BS.empty files
195 js <- foldM appendToOutput BS.empty files
192
196
193 (extension, code) <- case only_js flags of
197 (extension, code) <- case only_js flags of
compiler/Initialize.hs+10−11
@@ -63,9 +63,9 @@ buildFromSource noPrelude interfaces source =
63 return $ metaModule { types = types }
63 return $ metaModule { types = types }
64
64
65
65
66−getSortedDependencies :: [FilePath] -> Bool -> FilePath -> IO [String]
66+getSortedDependencies :: [FilePath] -> Interfaces -> FilePath -> IO [String]
67−getSortedDependencies srcDirs noPrelude root =
67+getSortedDependencies srcDirs builtIns root =
68− sortDeps =<< readDeps srcDirs noPrelude root
68+ sortDeps =<< readDeps srcDirs builtIns root
69
69
70type Deps = (FilePath, String, [String])
70type Deps = (FilePath, String, [String])
71
71
@@ -80,14 +80,13 @@ sortDeps depends =
80 mistakes = filter (\scc -> length scc > 1) sccs
80 mistakes = filter (\scc -> length scc > 1) sccs
81 msg = "A cyclical module dependency or was detected in: "
81 msg = "A cyclical module dependency or was detected in: "
82
82
83−readDeps :: [FilePath] -> Bool -> FilePath -> IO [Deps]
83+readDeps :: [FilePath] -> Interfaces -> FilePath -> IO [Deps]
84+readDeps srcDirs builtIns root = do
85+ let ifaces = (Set.fromList . Map.keys) builtIns
84−readDeps srcDirs noPrelude root = evalStateT (go root) Set.empty
86+ evalStateT (go ifaces root) Set.empty
85 where
87 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]
88+ go :: Set.Set String -> FilePath -> StateT (Set.Set String) IO [Deps]
90− go root = do
89+ go builtIns root = do
91 (root', txt) <- liftIO $ getFile srcDirs root
90 (root', txt) <- liftIO $ getFile srcDirs root
92 case Parse.dependencies txt of
91 case Parse.dependencies txt of
93 Left err -> liftIO (putStrLn msg >> print err >> exitFailure)
92 Left err -> liftIO (putStrLn msg >> print err >> exitFailure)
@@ -98,7 +97,7 @@ readDeps srcDirs noPrelude root = evalStateT (go root) Set.empty
98 let realDeps = Set.difference (Set.fromList deps) builtIns
97 let realDeps = Set.difference (Set.fromList deps) builtIns
99 newDeps = Set.difference (Set.filter (not . isNative) realDeps) seen
98 newDeps = Set.difference (Set.filter (not . isNative) realDeps) seen
100 put (Set.insert name (Set.union newDeps seen))
99 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)
102 return ((makeRelative "." root', name, Set.toList realDeps) : concat rest)
101 return ((makeRelative "." root', name, Set.toList realDeps) : concat rest)
103
102
104getFile :: [FilePath] -> FilePath -> IO (FilePath,String)
103getFile :: [FilePath] -> FilePath -> IO (FilePath,String)
compiler/Language/Elm.hs+16−6
@@ -5,7 +5,14 @@
5 <http://elm-lang.org/Documentation.elm>, and many interactive examples are
5 <http://elm-lang.org/Documentation.elm>, and many interactive examples are
6 available at <http://elm-lang.org/Examples.elm>
6 available at <http://elm-lang.org/Examples.elm>
7-}
7-}
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
9
16
10import qualified Data.List as List
17import qualified Data.List as List
11import qualified Data.Map as Map
18import qualified Data.Map as Map
@@ -21,11 +28,14 @@ import Paths_Elm
21
28
22-- |This function compiles Elm code to JavaScript. It will return either
29-- |This function compiles Elm code to JavaScript. It will return either
23-- an error message or the compiled JS code.
30-- an error message or the compiled JS code.
24−compile :: String -> Either String String
31+compile :: Interfaces -> String -> Either String String
25−compile source =
32+compile ifaces source = do
26− case buildFromSource False Prelude.interfaces source of
33+ case buildFromSource False ifaces source of
27− Left docs -> Left . unlines . List.intersperse "" $ map P.render docs
34+ Left docs -> Left . unlines . List.intersperse "" $ map P.render docs
28− Right modul -> Right $ jsModule (modul :: MetadataModule () ())
35+ Right modul -> Right $ jsModule (modul :: MetadataModule () ())
36+
37+interfaces :: IO Interfaces
38+interfaces = Prelude.interfaces
29
39
30-- |This function extracts the module name of a given source program.
40-- |This function extracts the module name of a given source program.
31moduleName :: String -> Maybe String
41moduleName :: String -> Maybe String
compiler/Metadata/Prelude.hs+2−5
@@ -7,7 +7,6 @@ import System.Directory
7import System.Exit
7import System.Exit
8import System.FilePath
8import System.FilePath
9import System.IO
9import System.IO
10−import System.IO.Unsafe (unsafePerformIO)
11import SourceSyntax.Module
10import SourceSyntax.Module
12import qualified Data.Binary as Binary
11import qualified Data.Binary as Binary
13import qualified Data.ByteString.Lazy as BS
12import qualified Data.ByteString.Lazy as BS
@@ -31,10 +30,8 @@ prelude = text ++ map (\n -> (n, Hiding [])) modules
31 modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
30 modules = [ "Basics", "Signal", "List", "Maybe", "Time", "Prelude"
32 , "Graphics.Element", "Color", "Graphics.Collage" ]
31 , "Graphics.Element", "Color", "Graphics.Collage" ]
33
32
34−{-# NOINLINE interfaces #-}
35−interfaces :: Interfaces
33+interfaces :: IO Interfaces
36−interfaces =
37− unsafePerformIO (safeReadDocs =<< Path.getDataFileName "interfaces.data")
34+interfaces = safeReadDocs =<< Path.getDataFileName "interfaces.data"
38
35
39safeReadDocs :: FilePath -> IO Interfaces
36safeReadDocs :: FilePath -> IO Interfaces
40safeReadDocs name =
37safeReadDocs name =