git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #262 from joneshf/record-updates

Record updates

commit6aa8fc6d177d718a960840a01157d148cb6b44fb authorPhil Freeman <paf31@cantab.net> dateFri 7 Mar 2014, 18:06 (13 years ago) parent6661c0373dd351
6 files changed+34−3
examples/passing/ObjectUpdate.purs+4−2 src/Language/PureScript/CodeGen/JS.hs+18−1 src/Language/PureScript/CodeGen/JS/AST.hs+4 src/Language/PureScript/Optimizer/Common.hs+1 src/Language/PureScript/Pretty/JS.hs+6 src/Language/PureScript/Scope.hs+1
examples/passing/ObjectUpdate.purs+4−2
@@ -14,7 +14,9 @@ module ObjectUpdate where
1414 polyUpdate = \o -> o { foo = "Foo" }
1515
1616 inferPolyUpdate = \o -> o { foo = "Foo" }
17−
17+
1818module Main where
1919
20−main = Debug.Trace.trace "Done"
20+import ObjectUpdate
21+
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
117117valueToJs _ _ _ (BooleanLiteral b) = JSBooleanLiteral b
118118valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral (map (valueToJs opts m e) xs)
119119valueToJs 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)
121121valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
122122valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
123123valueToJs 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
130130valueToJs _ _ _ (TypeClassDictionary _ _) = error "Type class dictionary was not replaced"
131131valueToJs _ _ _ _ = error "Invalid argument to valueToJs"
132132
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+
133150-- |
134151-- Temporarily extends the environment with a single local variable name
135152--
src/Language/PureScript/CodeGen/JS/AST.hs+4
@@ -200,6 +200,10 @@ data JS
200200 --
201201 | JSFor String JS JS JS
202202 -- |
203+ -- ForIn loop
204+ --
205+ | JSForIn String JS JS
206+ -- |
203207 -- If-then-else statement
204208 --
205209 | JSIfElse JS JS (Maybe JS)
src/Language/PureScript/Optimizer/Common.hs+1
@@ -43,6 +43,7 @@ isReassigned var1 = everything (||) (mkQ False check)
4343 check (JSVariableIntroduction arg _) | var1 == arg = True
4444 check (JSAssignment (JSVar arg) _) | var1 == arg = True
4545 check (JSFor arg _ _ _) | var1 == arg = True
46+ check (JSForIn arg _ _) | var1 == arg = True
4647 check _ = False
4748
4849isRebound :: (Data d) => JS -> d -> Bool
src/Language/PureScript/Pretty/JS.hs+6
@@ -110,6 +110,12 @@ literals = mkPattern' match
110110 , return $ "; " ++ ident ++ "++) "
111111 , prettyPrintJS' sts
112112 ]
113+ match (JSForIn ident obj sts) = fmap concat $ sequence
114+ [ return $ "for (var " ++ ident ++ " in "
115+ , prettyPrintJS' obj
116+ , return ") "
117+ , prettyPrintJS' sts
118+ ]
113119 match (JSIfElse cond thens elses) = fmap concat $ sequence
114120 [ return "if ("
115121 , prettyPrintJS' cond
src/Language/PureScript/Scope.hs+1
@@ -47,6 +47,7 @@ usedNames val = nub $ everything (++) (mkQ [] namesV `extQ` namesB `extQ` namesJ
4747 namesJS (JSFunction Nothing args _) = Ident <$> args
4848 namesJS (JSVariableIntroduction name _) = [Ident name]
4949 namesJS (JSFor name _ _ _) = [Ident name]
50+ namesJS (JSForIn name _ _) = [Ident name]
5051 namesJS _ = []
5152
5253-- |