on master help sign in
Use bytestring builders to generate JS

This way we can write the result directly to disk. Before we creating a lazy Text buffer, then did some additional encoding from UTF16 to UTF8 when it went into the file.

commitabd5880ddbcf09153ae58677247dc1e05121b1a5 authorEvan Czaplicki <evancz@users.noreply.github.com> dateWed 22 Mar 2017, 17:43 (10 years ago) parentc756a83
3 files changed+21−18
src/Elm/Compiler.hs+2−2 src/Generate/JavaScript.hs+3−3 src/Generate/JavaScript/Builder.hs+16−13
src/Elm/Compiler.hs+2−2
@@ -11,8 +11,8 @@ module Elm.Compiler
1111
1212import qualified Data.Aeson as Json
1313import qualified Data.Map as Map
14−import qualified Data.Text.Lazy as LazyText
1514import Data.Text (Text)
15+import qualified Data.ByteString.Builder as BS
1616import Text.PrettyPrint.ANSI.Leijen (Doc)
1717
1818import qualified AST.Module as Module
@@ -127,7 +127,7 @@ data Result =
127127 Result
128128 { _docs :: Maybe Docs.Documentation
129129 , _iface :: M.Interface
130− , _js :: LazyText.Text
130+ , _js :: BS.Builder
131131 }
132132
133133
src/Generate/JavaScript.hs+3−3
@@ -4,7 +4,7 @@ module Generate.JavaScript (generate) where
44
55import qualified Control.Monad.State as State
66import Data.Text (Text)
7−import qualified Data.Text.Lazy as LazyText
7+import qualified Data.ByteString.Builder as BS
88
99import qualified AST.Effects as Effects
1010import qualified AST.Module as Module
@@ -20,7 +20,7 @@ import qualified Generate.JavaScript.Variable as Var
2020-- GENERATE JAVASCRIPT
2121
2222
23−generate :: Module.Optimized -> LazyText.Text
23+generate :: Module.Optimized -> BS.Builder
2424generate (Module.Module moduleName info) =
2525 let
2626 genBody =
@@ -31,7 +31,7 @@ generate (Module.Module moduleName info) =
3131 body =
3232 State.evalState genBody 0
3333 in
34− JS.stmtsToText body
34+ JS.encodeUtf8 body
3535
3636
3737
src/Generate/JavaScript/Builder.hs+16−13
@@ -1,10 +1,10 @@
11{-# OPTIONS_GHC -Wall #-}
22{-# LANGUAGE OverloadedStrings #-}
33module Generate.JavaScript.Builder
4− ( Expr(..), Id(..), Prop(..), LValue(..)
4+ ( encodeUtf8
5+ , Expr(..), Id(..), Prop(..), LValue(..)
56 , Stmt(..), Case(..), VarDecl(..)
67 , InfixOp(..), PrefixOp(..)
7− , stmtsToText
88 )
99 where
1010
@@ -15,12 +15,10 @@ module Generate.JavaScript.Builder
1515
1616import Prelude hiding (lines)
1717import qualified Data.List as List
18+import Data.ByteString.Builder
1819import Data.Monoid ((<>))
1920import Data.Text (Text)
20−import Data.Text.Lazy.Builder
21−import Data.Text.Lazy.Builder.Int (decimal)
22−import Data.Text.Lazy.Builder.RealFloat (realFloat)
23−import qualified Data.Text.Lazy as LazyText
21+import qualified Data.Text.Encoding as Text
2422
2523
2624
@@ -129,12 +127,12 @@ data PrefixOp
129127
130128
131129
132−-- CONVERT TO LAZY TEXT
130+-- ENCODE
133131
134132
135−stmtsToText :: [Stmt] -> LazyText.Text
136−stmtsToText stmts =
137− toLazyText (fromStmtBlock "" stmts)
133+encodeUtf8 :: [Stmt] -> Builder
134+encodeUtf8 stmts =
135+ fromStmtBlock "" stmts
138136
139137
140138
@@ -277,6 +275,11 @@ fromId (Id name) =
277275 fromText name
278276
279277
278+fromText :: Text -> Builder
279+fromText txt =
280+ byteString (Text.encodeUtf8 txt)
281+
282+
280283
281284-- VAR DECLS
282285
@@ -334,10 +337,10 @@ fromExpr indent grouping expression =
334337 (One, quoted string)
335338
336339 Float n ->
337− (One, realFloat n)
340+ (One, doubleDec n)
338341
339342 Int n ->
340− (One, decimal n)
343+ (One, intDec n)
341344
342345 Bool bool ->
343346 (One, if bool then "true" else "false")
@@ -482,7 +485,7 @@ fromProp prop =
482485 quoted string
483486
484487 IntProp n ->
485− decimal n
488+ intDec n
486489
487490
488491