@@ -81,7 +81,7 @@ bindToJs mp (NonRec ident val) = return <$> nonRecToJS mp ident val
81bindToJs mp (Rec vals) = forM vals (uncurry (nonRecToJS mp))
81bindToJs mp (Rec vals) = forM vals (uncurry (nonRecToJS mp))
84−-- Generate code in the simplified Javascript intermediate representation for a single non-recursive
84+-- Generate code in the simplified Javascript intermediate representation for a single non-recursive
85-- declaration.
85-- declaration.
87-- The main purpose of this function is to handle code generation for comments.
87-- The main purpose of this function is to handle code generation for comments.
@@ -92,7 +92,7 @@ nonRecToJS m i e@(extractAnn -> (_, com, _, _)) | not (null com) =
92nonRecToJS mp ident val = do
92nonRecToJS mp ident val = do
93 js <- valueToJs mp val
93 js <- valueToJs mp val
94 return $ JSVariableIntroduction (identToJs ident) (Just js)
94 return $ JSVariableIntroduction (identToJs ident) (Just js)
98-- Generate code in the simplified Javascript intermediate representation for a variable based on a
98-- Generate code in the simplified Javascript intermediate representation for a variable based on a
@@ -171,26 +171,23 @@ valueToJs _ (Constructor (_, _, _, Just IsNewtype) _ (ProperName ctor) _) =
171 JSObjectLiteral [("create",
171 JSObjectLiteral [("create",
172 JSFunction Nothing ["value"]
172 JSFunction Nothing ["value"]
173 (JSBlock [JSReturn $ JSVar "value"]))])
173 (JSBlock [JSReturn $ JSVar "value"]))])
174−valueToJs _ (Constructor _ _ (ProperName ctor) 0) =
174+valueToJs _ (Constructor _ _ (ProperName ctor) []) =
175 return $ iife ctor [ JSFunction (Just ctor) [] (JSBlock [])
175 return $ iife ctor [ JSFunction (Just ctor) [] (JSBlock [])
176 , JSAssignment (JSAccessor "value" (JSVar ctor))
176 , JSAssignment (JSAccessor "value" (JSVar ctor))
177 (JSUnary JSNew $ JSApp (JSVar ctor) []) ]
177 (JSUnary JSNew $ JSApp (JSVar ctor) []) ]
178−valueToJs _ (Constructor _ _ (ProperName ctor) arity) =
178+valueToJs _ (Constructor _ _ (ProperName ctor) fields) =
179− return $ iife ctor [ makeConstructor ctor arity
179+ return $ iife ctor [ makeConstructor ctor fields
180− , JSAssignment (JSAccessor "create" (JSVar ctor)) (go ctor 0 arity [])
180+ , JSAssignment (JSAccessor "create" (JSVar ctor)) (makeCreateFn ctor fields)
183− makeConstructor :: String -> Int -> JS
183+ makeConstructor :: String -> [Ident] -> JS
184− makeConstructor ctorName n =
184+ makeConstructor ctorName fs =
185− let args = [ "value" ++ show index | index <- [0..n-1] ]
186− body = [ JSAssignment (JSAccessor arg (JSVar "this")) (JSVar arg) | arg <- args ]
185+ let body = [ JSAssignment (JSAccessor (identToJs f) (JSVar "this")) (var f) | f <- fs ]
187− in JSFunction (Just ctorName) args (JSBlock body)
186+ in JSFunction (Just ctorName) (identToJs `map` fs) (JSBlock body)
188− go :: String -> Int -> Int -> [JS] -> JS
187+ makeCreateFn :: String -> [Ident] -> JS
188+ makeCreateFn ctorName fs =
189− go pn _ 0 values = JSUnary JSNew $ JSApp (JSVar pn) (reverse values)
189+ let body = JSUnary JSNew $ JSApp (JSVar ctorName) (var `map` fs)
190− go pn index n values =
190+ in foldr (\f inner -> JSFunction Nothing [identToJs f] (JSBlock [JSReturn inner])) body fields
191− JSFunction Nothing ["value" ++ show index]
192− (JSBlock [JSReturn (go pn (index + 1) (n - 1) (JSVar ("value" ++ show index) : values))])
194iife :: String -> [JS] -> JS
191iife :: String -> [JS] -> JS
195iife v exprs = JSApp (JSFunction Nothing [] (JSBlock $ exprs ++ [JSReturn $ JSVar v])) []
192iife v exprs = JSApp (JSFunction Nothing [] (JSBlock $ exprs ++ [JSReturn $ JSVar v])) []