git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1479, encode .js files as UTF8.
85a46e1Phil Freeman
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)
63import Data.Version (showVersion)
63import Data.Version (showVersion)
64import Data.Aeson (encode, decode)
64import Data.Aeson (encode, decode)
65import qualified Data.ByteString.Lazy as B
65import qualified Data.ByteString.Lazy as B
66+import qualified Data.ByteString.UTF8 as BU8
66import qualified Data.Set as S
67import qualified Data.Set as S
67import qualified Data.Map as M
68import qualified Data.Map as M
68
69
@@ -70,6 +71,7 @@ import System.Directory
70 (doesFileExist, getModificationTime, createDirectoryIfMissing)
71 (doesFileExist, getModificationTime, createDirectoryIfMissing)
71import System.FilePath ((</>), takeDirectory)
72import System.FilePath ((</>), takeDirectory)
72import System.IO.Error (tryIOError)
73import System.IO.Error (tryIOError)
74+import System.IO.UTF8 (readUTF8File, writeUTF8File)
73
75
74import Language.PureScript.Crash
76import Language.PureScript.Crash
75import Language.PureScript.AST
77import Language.PureScript.AST
@@ -123,7 +125,7 @@ data MakeActions m = MakeActions {
123 -- |
125 -- |
124 -- Read the externs file for a module as a string and also return the actual
126 -- Read the externs file for a module as a string and also return the actual
125 -- path for the file.
127 -- path for the file.
126− , readExterns :: ModuleName -> m (FilePath, B.ByteString)
128+ , readExterns :: ModuleName -> m (FilePath, Externs)
127 -- |
129 -- |
128 -- Run the code generator for the module and write any required output files.
130 -- Run the code generator for the module and write any required output files.
129 --
131 --
@@ -137,7 +139,7 @@ data MakeActions m = MakeActions {
137-- |
139-- |
138-- Generated code for an externs file.
140-- Generated code for an externs file.
139--
141--
140−type Externs = B.ByteString
142+type Externs = String
141
143
142-- |
144-- |
143-- Determines when to rebuild a module
145-- Determines when to rebuild a module
@@ -231,7 +233,7 @@ make MakeActions{..} ms = do
231 corefn = CF.moduleToCoreFn env' mod'
233 corefn = CF.moduleToCoreFn env' mod'
232 [renamed] = renameInModules [corefn]
234 [renamed] = renameInModules [corefn]
233 exts = moduleToExternsFile mod' env'
235 exts = moduleToExternsFile mod' env'
234− evalSupplyT nextVar $ codegen renamed env' $ encode exts
236+ evalSupplyT nextVar . codegen renamed env' . BU8.toString . B.toStrict . encode $ exts
235 return exts
237 return exts
236 markComplete (Just (warnings, exts)) Nothing
238 markComplete (Just (warnings, exts)) Nothing
237
239
@@ -258,9 +260,9 @@ make MakeActions{..} ms = do
258 shouldExist (Just t) = t
260 shouldExist (Just t) = t
259 shouldExist _ = internalError "make: dependency should already have been built."
261 shouldExist _ = internalError "make: dependency should already have been built."
260
262
261− decodeExterns :: B.ByteString -> Maybe ExternsFile
263+ decodeExterns :: Externs -> Maybe ExternsFile
262 decodeExterns bs = do
264 decodeExterns bs = do
263− externs <- decode bs
265+ externs <- decode (fromString bs)
264 guard $ efVersion externs == showVersion Paths.version
266 guard $ efVersion externs == showVersion Paths.version
265 return externs
267 return externs
266
268
@@ -335,7 +337,7 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
335 externsFile = outputDir </> filePath </> "externs.json"
337 externsFile = outputDir </> filePath </> "externs.json"
336 min <$> getTimestamp jsFile <*> getTimestamp externsFile
338 min <$> getTimestamp jsFile <*> getTimestamp externsFile
337
339
338− readExterns :: ModuleName -> Make (FilePath, B.ByteString)
340+ readExterns :: ModuleName -> Make (FilePath, Externs)
339 readExterns mn = do
341 readExterns mn = do
340 let path = outputDir </> runModuleName mn </> "externs.json"
342 let path = outputDir </> runModuleName mn </> "externs.json"
341 (path, ) <$> readTextFile path
343 (path, ) <$> readTextFile path
@@ -371,13 +373,13 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
371 exists <- doesFileExist path
373 exists <- doesFileExist path
372 traverse (const $ getModificationTime path) $ guard exists
374 traverse (const $ getModificationTime path) $ guard exists
373
375
374− readTextFile :: FilePath -> Make B.ByteString
376+ readTextFile :: FilePath -> Make String
375− readTextFile path = makeIO (const (ErrorMessage [] $ CannotReadFile path)) $ B.readFile path
377+ readTextFile path = makeIO (const (ErrorMessage [] $ CannotReadFile path)) $ readUTF8File path
376
378
377− writeTextFile :: FilePath -> B.ByteString -> Make ()
379+ writeTextFile :: FilePath -> String -> Make ()
378 writeTextFile path text = makeIO (const (ErrorMessage [] $ CannotWriteFile path)) $ do
380 writeTextFile path text = makeIO (const (ErrorMessage [] $ CannotWriteFile path)) $ do
379 mkdirp path
381 mkdirp path
380− B.writeFile path text
382+ writeUTF8File path text
381 where
383 where
382 mkdirp :: FilePath -> IO ()
384 mkdirp :: FilePath -> IO ()
383 mkdirp = createDirectoryIfMissing True . takeDirectory
385 mkdirp = createDirectoryIfMissing True . takeDirectory
src/System/IO/UTF8.hs+20−4
@@ -1,9 +1,25 @@
1module System.IO.UTF8
1module System.IO.UTF8
2+
2where
3where
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+ )
4
13
5readUTF8File :: FilePath -> IO String
14readUTF8File :: FilePath -> IO String
6readUTF8File inFile = do
15readUTF8File inFile = do
7− h <- openFile inFile ReadMode
16+ h <- openFile inFile ReadMode
8− hSetEncoding h utf8
17+ hSetEncoding h utf8
9− hGetContents h
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