git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1646 from purescript/1479

Fix #1479, encode .js files as UTF8.

commitd1e7309c7476bb40840bdfb85ca425ea8fda79c7 authorPhil Freeman <paf31@cantab.net> dateSat 21 Nov 2015, 13:56 (11 years ago) parent2627e5a85a46e1
3 files changed+37−14
examples/passing/UnicodeIdentifier.purs+5 src/Language/PureScript/Make.hs+12−10 src/System/IO/UTF8.hs+20−4
examples/passing/UnicodeIdentifier.pursnew file+5
@@ -0,0 +1,5 @@
1+module Main where
2+
3+f asgård = asgård
4+
5+main = Control.Monad.Eff.Console.log (f "Done")
src/Language/PureScript/Make.hs+12−10
@@ -63,6 +63,7 @@ import Data.Traversable (for)
6363import Data.Version (showVersion)
6464import Data.Aeson (encode, decode)
6565import qualified Data.ByteString.Lazy as B
66+import qualified Data.ByteString.UTF8 as BU8
6667import qualified Data.Set as S
6768import qualified Data.Map as M
6869
@@ -70,6 +71,7 @@ import System.Directory
7071 (doesFileExist, getModificationTime, createDirectoryIfMissing)
7172import System.FilePath ((</>), takeDirectory)
7273import System.IO.Error (tryIOError)
74+import System.IO.UTF8 (readUTF8File, writeUTF8File)
7375
7476import Language.PureScript.Crash
7577import Language.PureScript.AST
@@ -123,7 +125,7 @@ data MakeActions m = MakeActions {
123125 -- |
124126 -- Read the externs file for a module as a string and also return the actual
125127 -- path for the file.
126− , readExterns :: ModuleName -> m (FilePath, B.ByteString)
128+ , readExterns :: ModuleName -> m (FilePath, Externs)
127129 -- |
128130 -- Run the code generator for the module and write any required output files.
129131 --
@@ -137,7 +139,7 @@ data MakeActions m = MakeActions {
137139-- |
138140-- Generated code for an externs file.
139141--
140−type Externs = B.ByteString
142+type Externs = String
141143
142144-- |
143145-- Determines when to rebuild a module
@@ -231,7 +233,7 @@ make MakeActions{..} ms = do
231233 corefn = CF.moduleToCoreFn env' mod'
232234 [renamed] = renameInModules [corefn]
233235 exts = moduleToExternsFile mod' env'
234− evalSupplyT nextVar $ codegen renamed env' $ encode exts
236+ evalSupplyT nextVar . codegen renamed env' . BU8.toString . B.toStrict . encode $ exts
235237 return exts
236238 markComplete (Just (warnings, exts)) Nothing
237239
@@ -258,9 +260,9 @@ make MakeActions{..} ms = do
258260 shouldExist (Just t) = t
259261 shouldExist _ = internalError "make: dependency should already have been built."
260262
261− decodeExterns :: B.ByteString -> Maybe ExternsFile
263+ decodeExterns :: Externs -> Maybe ExternsFile
262264 decodeExterns bs = do
263− externs <- decode bs
265+ externs <- decode (fromString bs)
264266 guard $ efVersion externs == showVersion Paths.version
265267 return externs
266268
@@ -335,7 +337,7 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
335337 externsFile = outputDir </> filePath </> "externs.json"
336338 min <$> getTimestamp jsFile <*> getTimestamp externsFile
337339
338− readExterns :: ModuleName -> Make (FilePath, B.ByteString)
340+ readExterns :: ModuleName -> Make (FilePath, Externs)
339341 readExterns mn = do
340342 let path = outputDir </> runModuleName mn </> "externs.json"
341343 (path, ) <$> readTextFile path
@@ -371,13 +373,13 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
371373 exists <- doesFileExist path
372374 traverse (const $ getModificationTime path) $ guard exists
373375
374− readTextFile :: FilePath -> Make B.ByteString
375− readTextFile path = makeIO (const (ErrorMessage [] $ CannotReadFile path)) $ B.readFile path
376+ readTextFile :: FilePath -> Make String
377+ readTextFile path = makeIO (const (ErrorMessage [] $ CannotReadFile path)) $ readUTF8File path
376378
377− writeTextFile :: FilePath -> B.ByteString -> Make ()
379+ writeTextFile :: FilePath -> String -> Make ()
378380 writeTextFile path text = makeIO (const (ErrorMessage [] $ CannotWriteFile path)) $ do
379381 mkdirp path
380− B.writeFile path text
382+ writeUTF8File path text
381383 where
382384 mkdirp :: FilePath -> IO ()
383385 mkdirp = createDirectoryIfMissing True . takeDirectory
src/System/IO/UTF8.hs+20−4
@@ -1,9 +1,25 @@
11module System.IO.UTF8
2+
23where
3−import System.IO (hGetContents, hSetEncoding, openFile, utf8, IOMode (..))
4+
5+import System.IO ( IOMode(..)
6+ , hGetContents
7+ , hSetEncoding
8+ , hClose
9+ , hPutStr
10+ , openFile
11+ , utf8
12+ )
413
514readUTF8File :: FilePath -> IO String
615readUTF8File inFile = do
7− h <- openFile inFile ReadMode
8− hSetEncoding h utf8
9− hGetContents h
16+ h <- openFile inFile ReadMode
17+ hSetEncoding h utf8
18+ hGetContents h
19+
20+writeUTF8File :: FilePath -> String -> IO ()
21+writeUTF8File inFile text = do
22+ h <- openFile inFile WriteMode
23+ hSetEncoding h utf8
24+ hPutStr h text
25+ hClose h