git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #262 from joneshf/record-updates
6aa8fc6Phil Freeman
examples/passing/ObjectUpdate.purs+4−2
@@ -14,7 +14,9 @@ module ObjectUpdate where
14 polyUpdate = \o -> o { foo = "Foo" }
14 polyUpdate = \o -> o { foo = "Foo" }
15
15
16 inferPolyUpdate = \o -> o { foo = "Foo" }
16 inferPolyUpdate = \o -> o { foo = "Foo" }
17−
17+
18module Main where
18module Main where
19
19
20+import ObjectUpdate
21+
20−main = Debug.Trace.trace "Done"
22+main = Debug.Trace.trace ((update1 {foo: ""}).foo)
src/Language/PureScript/CodeGen/JS.hs+18−1
@@ -117,7 +117,7 @@ valueToJs _ _ _ (StringLiteral s) = JSStringLiteral s
117valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
117valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
118valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral (map (valueToJs opts m e) xs)
118valueToJs 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)
119valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (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)]
120+valueToJs opts m e (ObjectUpdate o ps) = extendObj (valueToJs opts m e o) (map (second (valueToJs opts m e)) ps)
121valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
121valueToJs _ 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)
122valueToJs 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)
123valueToJs 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 +130,23 @@ valueToJs opts m e (TypedValue _ val _) = valueToJs opts m e val
130valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
130valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
131valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
131valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
132
132
133+-- |
134+-- Shallow copy an object.
135+--
136+extendObj :: JS -> [(String, JS)] -> JS
137+extendObj obj sts = JSApp (JSFunction Nothing [] block) []
138+ where
139+ [newObj, key] = take 2 . map identToJs . unusedNames $ (obj, sts)
140+ jsKey = JSVar key
141+ jsNewObj = JSVar newObj
142+ block = JSBlock (objAssign:copy:extend ++ [JSReturn jsNewObj])
143+ objAssign = JSVariableIntroduction newObj (Just $ JSObjectLiteral [])
144+ copy = JSForIn key obj $ JSBlock [JSIfElse cond assign Nothing]
145+ cond = JSApp (JSAccessor "hasOwnProperty" obj) [jsKey]
146+ assign = JSBlock [JSAssignment (JSIndexer jsKey jsNewObj) (JSIndexer jsKey obj)]
147+ stToAssign (s, js) = JSAssignment (JSAccessor s jsNewObj) js
148+ extend = map stToAssign sts
149+
133-- |
150-- |
134-- Temporarily extends the environment with a single local variable name
151-- Temporarily extends the environment with a single local variable name
135--
152--
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/Optimizer/Common.hs+1
@@ -43,6 +43,7 @@ isReassigned var1 = everything (||) (mkQ False check)
43 check (JSVariableIntroduction arg _) | var1 == arg = True
43 check (JSVariableIntroduction arg _) | var1 == arg = True
44 check (JSAssignment (JSVar arg) _) | var1 == arg = True
44 check (JSAssignment (JSVar arg) _) | var1 == arg = True
45 check (JSFor arg _ _ _) | var1 == arg = True
45 check (JSFor arg _ _ _) | var1 == arg = True
46+ check (JSForIn arg _ _) | var1 == arg = True
46 check _ = False
47 check _ = False
47
48
48isRebound :: (Data d) => JS -> d -> Bool
49isRebound :: (Data d) => JS -> d -> Bool
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-- |