1module Generate.JavaScript.Ports (incoming, outgoing) 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"]
60 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
63incoming :: CanonicalType -> Expression ()
66 Aliased _ t -> incoming t
70 obj ["_P","incomingSignal"] <| incoming t
72 _ -> ["v"] ==> inc tipe (ref "v")
75inc :: CanonicalType -> Expression () -> Expression ()
78 Lambda _ _ -> error "functions should not be allowed through input ports"
79 Var _ -> error "type variables should not be allowed through input ports"
83 Type (Var.Canonical Var.BuiltIn name)
84 | name == "Int" -> from JSNumber
85 | name == "Float" -> from JSNumber
86 | name == "Bool" -> from JSBoolean
87 | name == "String" -> from JSString
89 from checks = check x checks x
99 error "bad type got to incoming port generation code"
104 | Var.isMaybe name ->
106 (equal x (NullLit ()))
108 (_Maybe "Just" <| inc t x)
111 check x JSArray (_List "fromArray" <| array)
113 | Var.isArray name ->
114 check x JSArray (_Array "fromJSArray" <| array)
116 array = DotRef () x (var "map") <| incoming t
119 | Var.isTuple name -> incomingTuple ts x
121 _ -> error "bad ADT got to incoming port generation code"
124 error "bad record got to incoming port generation code"
126 Record fields Nothing ->
127 check x (JSObject (map fst fields)) object
129 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
130 keys = map convert fields
131 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
134incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
135incomingTuple types x =
136 check x JSArray (ObjectLit () fields)
138 fields = (prop "ctor", ctor) : zipWith convert [0..] types
140 ctor = string ("_Tuple" ++ show (length types))
144 , inc t (BracketRef () x (IntLit () n))
148outgoing :: CanonicalType -> Expression ()
150 case T.dealias tipe of
151 App (Type signal) [ App (Type promise) [_,_] ]
152 | Var.isSignal signal && Var.isPromise promise ->
153 obj ["_P","promiseStream"]
155 App (Type promise) [_,_]
156 | Var.isPromise promise ->
163outgoingFunc :: CanonicalType -> Expression ()
168 obj ["_P","outgoingSignal"] <| outgoingFunc t
170 _ -> ["v"] ==> out tipe (ref "v")
173out :: CanonicalType -> Expression () -> Expression ()
176 Aliased _ t -> out t x
179 | numArgs > 1 && numArgs < 10 ->
180 func (ref ('A':show numArgs) `call` (x:values))
181 | otherwise -> func (foldl (<|) x values)
183 ts = T.collectLambdas tipe
184 numArgs = length ts - 1
185 args = map (\n -> '_' : show n) [0..]
186 values = zipWith inc (init ts) (map ref args)
188 function (take numArgs args)
189 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
190 , ret (out (last ts) (ref "_r"))
193 Var _ -> error "type variables should not be allowed through input ports"
195 Type (Var.Canonical Var.BuiltIn name)
196 | name `elem` ["Int","Float","Bool","String"] -> x
199 | Var.isJson name -> x
200 | Var.isTuple name -> ArrayLit () []
201 | otherwise -> error "bad type got to outgoing port generation code"
206 | Var.isMaybe name ->
208 (equal (DotRef () x (var "ctor")) (string "Nothing"))
210 (out t (DotRef () x (var "_0")))
212 | Var.isArray name ->
213 DotRef () (_Array "toJSArray" <| x) (var "map") <| outgoingFunc t
216 DotRef () (_List "toArray" <| x) (var "map") <| outgoingFunc t
219 | Var.isTuple name ->
220 let convert n t = out t $ DotRef () x $ var ('_':show n)
221 in ArrayLit () $ zipWith convert [0..] ts
223 _ -> error "bad ADT got to outgoing port generation code"
226 error "bad record got to outgoing port generation code"
228 Record fields Nothing ->
231 keys = map convert fields
232 convert (f,t) = (PropId () (var f), out 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