on master help sign in
Fix decoders for bringing tuples through ports
215251fEvan Czaplicki
src/Generate/JavaScript/Foreign.hs+19−4
@@ -161,6 +161,11 @@ field name decoder =
161 to "field" <== [ Opt.Literal (Literal.Str name), decoder ]
161 to "field" <== [ Opt.Literal (Literal.Str name), decoder ]
162
162
163
163
164+index :: Int -> Opt.Expr -> Opt.Expr
165+index index decoder =
166+ to "index" <== [ Opt.Literal (Literal.IntNum index), decoder ]
167+
168+
164(<==) :: Opt.Expr -> [Opt.Expr] -> Opt.Expr
169(<==) :: Opt.Expr -> [Opt.Expr] -> Opt.Expr
165(<==) func args =
170(<==) func args =
166 Opt.Call func args
171 Opt.Call func args
@@ -195,13 +200,23 @@ toTuple types =
195 size =
200 size =
196 length types
201 length types
197
202
198− args =
203+ pairs =
199− map (\n -> 'x' : show n) [ 1 .. size ]
204+ zipWith (,) [ 0 .. size - 1] types
205+
206+ toVar i =
207+ 'x' : show i
200
208
201 makeTuple =
209 makeTuple =
202− Opt.Function args (Opt.Data ("_Tuple" ++ show size) (map (Opt.Var . Var.local) args))
210+ to "succeed" <==
211+ [ Opt.Data
212+ ("_Tuple" ++ show size)
213+ (map (Opt.Var . Var.local . toVar . fst) pairs)
214+ ]
215+
216+ andThen (i, tipe) decoder =
217+ to "andThen" <== [ Opt.Function [toVar i] decoder, index i (decode tipe) ]
203 in
218 in
204− to ("tuple" ++ show size) <== (makeTuple : map decode types)
219+ List.foldr andThen makeTuple pairs
205
220
206
221
207toRecord :: [(String, T.Canonical)] -> Opt.Expr
222toRecord :: [(String, T.Canonical)] -> Opt.Expr