git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Generates a for-in loop.
db8e1b0joneshf
src/Language/PureScript/CodeGen/JS.hs+23−2
@@ -25,7 +25,8 @@ module Language.PureScript.CodeGen.JS (
25import Data.Maybe (fromMaybe, mapMaybe)
25import Data.Maybe (fromMaybe, mapMaybe)
26import Data.Function (on)
26import Data.Function (on)
27
27
28−import Control.Arrow (second)
28+import Control.Applicative ((<$>), (<*>))
29+import Control.Arrow (first, second)
29import Control.Monad (replicateM, forM)
30import Control.Monad (replicateM, forM)
30
31
31import qualified Data.Map as M
32import qualified Data.Map as M
@@ -117,7 +118,8 @@ valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
117valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
118valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
118valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral (map (valueToJs opts m e) xs)
119valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral (map (valueToJs opts m e) xs)
119valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs opts m e)) ps)
120valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs opts m e)) ps)
121+valueToJs opts m e (ObjectUpdate o ps) = extendObj (valueToJs opts m e o) (map (second (valueToJs opts m e)) ps)
120−valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
122+--valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
121valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
123valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
122valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
124valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
123valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
125valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
@@ -130,6 +132,25 @@ valueToJs opts m e (TypedValue _ val _) = valueToJs opts m e val
130valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
132valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
131valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
133valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
132
134
135+-- |
136+-- Shallow copy an object.
137+--
138+extendObj :: JS -> [(String, JS)] -> JS
139+extendObj obj sts = iife
140+ where
141+ iife = JSApp fun []
142+ fun = JSFunction Nothing [] block
143+ block = JSBlock ([objAssign, copy] ++ extensions ++ ret)
144+ objAssign = JSVariableIntroduction newObj (Just $ JSObjectLiteral [])
145+ copy = JSForIn key obj test
146+ (newObj, key) = runGen ["newObj", "key"] ((,) <$> fresh <*> fresh)
147+ --(newObj, key) = runGen (map identToJs (unusedNames obj)) ((,) <$> fresh <*> fresh)
148+ test = JSBlock [JSIfElse cond assign Nothing]
149+ cond = JSApp (JSAccessor "hasOwnProperty" obj) [JSVar key]
150+ assign = JSBlock [JSAssignment (JSIndexer (JSVar key) (JSVar newObj)) (JSIndexer (JSVar key) obj)]
151+ extensions = map (uncurry JSAssignment . first ((`JSIndexer` JSVar newObj) . JSVar)) sts
152+ ret = [JSReturn (JSVar newObj)]
153+
133-- |
154-- |
134-- Temporarily extends the environment with a single local variable name
155-- Temporarily extends the environment with a single local variable name
135--
156--
src/Language/PureScript/CodeGen/JS/AST.hs+4
@@ -200,6 +200,10 @@ data JS
200 --
200 --
201 | JSFor String JS JS JS
201 | JSFor String JS JS JS
202 -- |
202 -- |
203+ -- ForIn loop
204+ --
205+ | JSForIn String JS JS
206+ -- |
203 -- If-then-else statement
207 -- If-then-else statement
204 --
208 --
205 | JSIfElse JS JS (Maybe JS)
209 | JSIfElse JS JS (Maybe JS)
src/Language/PureScript/Pretty/JS.hs+6
@@ -110,6 +110,12 @@ literals = mkPattern' match
110 , return $ "; " ++ ident ++ "++) "
110 , return $ "; " ++ ident ++ "++) "
111 , prettyPrintJS' sts
111 , prettyPrintJS' sts
112 ]
112 ]
113+ match (JSForIn ident obj sts) = fmap concat $ sequence
114+ [ return $ "for (var " ++ ident ++ " in "
115+ , prettyPrintJS' obj
116+ , return ") "
117+ , prettyPrintJS' sts
118+ ]
113 match (JSIfElse cond thens elses) = fmap concat $ sequence
119 match (JSIfElse cond thens elses) = fmap concat $ sequence
114 [ return "if ("
120 [ return "if ("
115 , prettyPrintJS' cond
121 , prettyPrintJS' cond
src/Language/PureScript/Scope.hs+1
@@ -47,6 +47,7 @@ usedNames val = nub $ everything (++) (mkQ [] namesV `extQ` namesB `extQ` namesJ
47 namesJS (JSFunction Nothing args _) = Ident <$> args
47 namesJS (JSFunction Nothing args _) = Ident <$> args
48 namesJS (JSVariableIntroduction name _) = [Ident name]
48 namesJS (JSVariableIntroduction name _) = [Ident name]
49 namesJS (JSFor name _ _ _) = [Ident name]
49 namesJS (JSFor name _ _ _) = [Ident name]
50+ namesJS (JSForIn name _ _) = [Ident name]
50 namesJS _ = []
51 namesJS _ = []
51
52
52-- |
53-- |