on master help sign in
Ensure that binary serialization works on Windows. Previously had issues with line ending conversions messing with binary data.
commita446d609f2492a372178d8d2d6c311ae0249de2e authorEvan Czaplicki <info@elm-lang.org> dateTue 6 Aug 2013, 00:36 (13 years ago) parent48ee312
3 files changed+33−7
Setup.hs+10−2 compiler/Compiler.hs+13−3 compiler/Metadata/Prelude.hs+10−2
Setup.hs+10−2
@@ -11,6 +11,7 @@ import System.Process
1111
1212import Control.Monad
1313import qualified Data.Binary as Binary
14+import qualified Data.ByteString.Lazy as BS
1415
1516-- Part 1
1617-- ------
@@ -130,8 +131,15 @@ buildInterfaces :: LocalBuildInfo -> [FilePath] -> IO ()
130131buildInterfaces lbi elmis = do
131132 createDirectoryIfMissing True (rtsDir lbi)
132133 let ifaces = interfaces lbi
133− Binary.encodeFile ifaces (length elmis)
134− mapM_ (\elmi -> readFile elmi >>= appendFile ifaces) elmis
134+ ifaceHandle <- openBinaryFile ifaces WriteMode
135+ BS.hPut ifaceHandle (Binary.encode (length elmis))
136+ let append file = do
137+ handle <- openBinaryFile file ReadMode
138+ bits <- hGetContents handle
139+ length bits `seq` hPutStr ifaceHandle bits
140+ hClose handle
141+ mapM_ append elmis
142+ hClose ifaceHandle
135143
136144buildRuntime :: LocalBuildInfo -> [FilePath] -> IO ()
137145buildRuntime lbi elmos = do
compiler/Compiler.hs+13−3
@@ -10,12 +10,14 @@ import System.Console.CmdArgs hiding (program)
1010import System.Directory
1111import System.Exit
1212import System.FilePath
13+import System.IO
1314import GHC.Conc
1415
1516import qualified Text.Blaze.Html.Renderer.Pretty as Pretty
1617import qualified Text.Blaze.Html.Renderer.String as Normal
1718import qualified Text.Jasmine as JS
1819import qualified Data.ByteString.Lazy.Char8 as BS
20+import qualified Data.ByteString.Lazy as L
1921
2022import qualified Metadata.Prelude as Prelude
2123import qualified Transform.Canonicalize as Canonical
@@ -91,8 +93,14 @@ elmi flags filePath = file flags filePath "elmi"
9193buildFile :: Flags -> Int -> Int -> Interfaces -> FilePath -> IO ModuleInterface
9294buildFile flags moduleNum numModules interfaces filePath =
9395 do compiled <- alreadyCompiled
94− if not compiled then compile
95− else getInterface `fmap` Binary.decodeFile (elmi flags filePath)
96+ case compiled of
97+ False -> compile
98+ True -> do
99+ handle <- openBinaryFile (elmi flags filePath) ReadMode
100+ bits <- L.hGetContents handle
101+ let iface = getInterface (Binary.decode bits)
102+ L.length bits `seq` hClose handle
103+ return iface
96104 where
97105 getInterface :: (String, ModuleInterface) -> ModuleInterface
98106 getInterface = snd
@@ -139,7 +147,9 @@ buildFile flags moduleNum numModules interfaces filePath =
139147 iAliases = aliases metaModule
140148 }
141149 createDirectoryIfMissing True . dropFileName $ elmi flags filePath
142− Binary.encodeFile (elmi flags filePath) (name,interface)
150+ handle <- openBinaryFile (elmi flags filePath) WriteMode
151+ L.hPut handle (Binary.encode (name,interface))
152+ hClose handle
143153 writeFile (elmo flags filePath) (jsModule metaModule)
144154 return interface
145155
compiler/Metadata/Prelude.hs+10−2
@@ -6,9 +6,11 @@ import qualified Paths_Elm as Path
66import System.Directory
77import System.Exit
88import System.FilePath
9+import System.IO
910import System.IO.Unsafe (unsafePerformIO)
1011import SourceSyntax.Module
1112import qualified Data.Binary as Binary
13+import qualified Data.ByteString.Lazy as BS
1214
1315
1416add :: Module t v -> Module t v
@@ -47,5 +49,11 @@ safeReadDocs name =
4749readDocs :: FilePath -> IO Interfaces
4850readDocs name = do
4951 exists <- doesFileExist name
50− if exists then Map.fromList `fmap` Binary.decodeFile name
51− else ioError . userError $ "File Not Found"
52+ case exists of
53+ False -> ioError . userError $ "File Not Found"
54+ True -> do
55+ handle <- openBinaryFile name ReadMode
56+ bits <- BS.hGetContents handle
57+ let ifaces = Map.fromList (Binary.decode bits)
58+ BS.length bits `seq` hClose handle
59+ return ifaces