1module Generate.JavaScript.Foreign (input, output) where
3import qualified Data.List as List
4import Generate.JavaScript.Helpers
6import qualified AST.Variable as Var
7import Language.ECMAScript3.Syntax
18typeToString :: JSType -> String
21 JSNumber -> "a number"
22 JSBoolean -> "a boolean (true or false)"
23 JSString -> "a string"
26 "an object with fields '" ++ List.intercalate "', '" fields ++ "'"
29_Array :: String -> Expression ()
31 useLazy ["Elm","Native","Array"] functionName
34_List :: String -> Expression ()
36 useLazy ["Elm","Native","List"] functionName
39_Maybe :: String -> Expression ()
41 useLazy ["Elm","Maybe"] functionName
44check :: Expression () -> JSType -> Expression () -> Expression ()
45check x jsType continue =
46 CondExpr () (jsFold OpLOr checks x) continue throw
48 jsFold op checks value =
49 foldl1 (InfixExpr () op) (map ($ value) checks)
52 obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
56 JSNumber -> [typeof "number"]
57 JSBoolean -> [typeof "boolean"]
58 JSString -> [typeof "string", instanceof "String"]
59 JSArray -> [instanceof "Array"]
61 [jsFold OpLAnd (typeof "object" : map member fields)]
66input :: String -> CanonicalType -> Expression ()
68 case T.dealias tipe of
70 | Var.isStream signal ->
71 obj ["_P","inputStream"] <| toTypeFunction t
73 | Var.isVarying signal ->
74 obj ["_P","inputVarying"] <| toTypeFunction t
77 obj ["_P","inputValue"] <| toTypeFunction dealiasedType
80toTypeFunction :: CanonicalType -> Expression ()
82 ["v"] ==> toType tipe (ref "v")
85toType :: CanonicalType -> Expression () -> Expression ()
89 error "functions should not be allowed through input ports"
92 error "type variables should not be allowed through input ports"
97 Type (Var.Canonical Var.BuiltIn name)
98 | name == "Int" -> from JSNumber
99 | name == "Float" -> from JSNumber
100 | name == "Bool" -> from JSBoolean
101 | name == "String" -> from JSString
103 from checks = check x checks x
109 | Var.isTuple name ->
113 error "bad type got to foreign input conversion"
118 | Var.isMaybe name ->
120 (equal x (NullLit ()))
122 (_Maybe "Just" <| toType t x)
125 check x JSArray (_List "fromArray" <| array)
127 | Var.isArray name ->
128 check x JSArray (_Array "fromJSArray" <| array)
130 array = DotRef () x (var "map") <| toTypeFunction t
133 | Var.isTuple name ->
136 _ -> error "bad ADT got to foreign input conversion"
139 error "bad record got to foreign input conversion"
141 Record fields Nothing ->
142 check x (JSObject (map fst fields)) object
144 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
145 keys = map convert fields
146 convert (f,t) = (prop f, toType t (DotRef () x (var f)))
149toTuple :: [CanonicalType] -> Expression () -> Expression ()
151 check x JSArray (ObjectLit () fields)
154 (prop "ctor", ctor) : zipWith convert [0..] types
157 string ("_Tuple" ++ show (length types))
161 , toType t (BracketRef () x (IntLit () n))
167output :: String -> CanonicalType -> Expression () -> Expression ()
168output name tipe value =
169 case T.dealias tipe of
170 App (Type signal) [t]
171 | Var.isStream signal ->
172 obj ["_P","outputStream"] `call` [ fromTypeFunction t, value ]
174 | Var.isVarying signal ->
175 obj ["_P","outputVarying"] `call` [ fromTypeFunction t, value ]
178 obj ["_P","outputValue"] `call` [ fromTypeFunction dealiasedType, value ]
181fromTypeFunction :: CanonicalType -> Expression ()
182fromTypeFunction tipe =
183 ["v"] ==> fromType tipe (ref "v")
186fromType :: CanonicalType -> Expression () -> Expression ()
193 | numArgs > 1 && numArgs < 10 ->
194 func (ref ('A':show numArgs) `call` (x:values))
196 func (foldl (<|) x values)
198 ts = T.collectLambdas tipe
199 numArgs = length ts - 1
200 args = map (\n -> '_' : show n) [0..]
201 values = zipWith toType (init ts) (map ref args)
203 function (take numArgs args)
204 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
205 , ret (fromType (last ts) (ref "_r"))
209 error "type variables should not be allowed through outputs"
211 Type (Var.Canonical Var.BuiltIn name)
212 | name `elem` ["Int","Float","Bool","String"] ->
216 | Var.isJson name -> x
217 | Var.isTuple name -> ArrayLit () []
218 | otherwise -> error "bad type got to an output"
223 | Var.isMaybe name ->
225 (equal (DotRef () x (var "ctor")) (string "Nothing"))
227 (fromType t (DotRef () x (var "_0")))
229 | Var.isArray name ->
230 DotRef () (_Array "toJSArray" <| x) (var "map") <| fromTypeFunction t
233 DotRef () (_List "toArray" <| x) (var "map") <| fromTypeFunction t
236 | Var.isTuple name ->
237 let convert n t = fromType t $ DotRef () x $ var ('_':show n)
238 in ArrayLit () $ zipWith convert [0..] ts
240 _ -> error "bad ADT got to an output"
243 error "bad record got to an output"
245 Record fields Nothing ->
248 keys = map convert fields
250 (PropId () (var f), fromType t (DotRef () x (var f)))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list