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
69 | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
71 _ -> ["v"] ==> inc tipe (ref "v")
74inc :: CanonicalType -> Expression () -> Expression ()
77 Lambda _ _ -> error "functions should not be allowed through input ports"
78 Var _ -> error "type variables should not be allowed through input ports"
82 Type (Var.Canonical Var.BuiltIn name)
83 | name == "Int" -> from JSNumber
84 | name == "Float" -> from JSNumber
85 | name == "Bool" -> from JSBoolean
86 | name == "String" -> from JSString
88 from checks = check x checks x
98 error "bad type got to incoming port generation code"
103 | Var.isMaybe name ->
105 (equal x (NullLit ()))
107 (_Maybe "Just" <| inc t x)
110 check x JSArray (_List "fromArray" <| array)
112 | Var.isArray name ->
113 check x JSArray (_Array "fromJSArray" <| array)
115 array = DotRef () x (var "map") <| incoming t
118 | Var.isTuple name -> incomingTuple ts x
120 _ -> error "bad ADT got to incoming port generation code"
123 error "bad record got to incoming port generation code"
125 Record fields Nothing ->
126 check x (JSObject (map fst fields)) object
128 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
129 keys = map convert fields
130 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
133incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
134incomingTuple types x =
135 check x JSArray (ObjectLit () fields)
137 fields = (prop "ctor", ctor) : zipWith convert [0..] types
139 ctor = string ("_Tuple" ++ show (length types))
143 , inc t (BracketRef () x (IntLit () n))
147outgoing :: CanonicalType -> Expression ()
150 Aliased _ t -> outgoing t
153 | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
155 _ -> ["v"] ==> out tipe (ref "v")
158out :: CanonicalType -> Expression () -> Expression ()
161 Aliased _ t -> out t x
164 | numArgs > 1 && numArgs < 10 ->
165 func (ref ('A':show numArgs) `call` (x:values))
166 | otherwise -> func (foldl (<|) x values)
168 ts = T.collectLambdas tipe
169 numArgs = length ts - 1
170 args = map (\n -> '_' : show n) [0..]
171 values = zipWith inc (init ts) (map ref args)
173 function (take numArgs args)
174 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
175 , ret (out (last ts) (ref "_r"))
178 Var _ -> error "type variables should not be allowed through input ports"
180 Type (Var.Canonical Var.BuiltIn name)
181 | name `elem` ["Int","Float","Bool","String"] -> x
184 | Var.isJson name -> x
185 | Var.isTuple name -> ArrayLit () []
186 | otherwise -> error "bad type got to outgoing port generation code"
191 | Var.isMaybe name ->
193 (equal (DotRef () x (var "ctor")) (string "Nothing"))
195 (out t (DotRef () x (var "_0")))
197 | Var.isArray name ->
198 DotRef () (_Array "toJSArray" <| x) (var "map") <| outgoing t
201 DotRef () (_List "toArray" <| x) (var "map") <| outgoing t
204 | Var.isTuple name ->
205 let convert n t = out t $ DotRef () x $ var ('_':show n)
206 in ArrayLit () $ zipWith convert [0..] ts
208 _ -> error "bad ADT got to outgoing port generation code"
211 error "bad record got to outgoing port generation code"
213 Record fields Nothing ->
216 keys = map convert fields
217 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