1module Generate.JavaScript.Port (inbound, outbound, task) where
3import qualified Data.List as List
4import Language.ECMAScript3.Syntax
6import AST.PrettyPrint (pretty)
8import qualified AST.Variable as Var
9import Generate.JavaScript.Helpers
14task :: String -> Expression () -> T.PortType var -> Expression ()
15task name expr portType =
18 _Task "perform" `call` [ expr ]
21 _Task "performSignal" `call` [ string name, expr ]
35typeToString :: JSType -> String
38 JSNumber -> "a number"
39 JSBoolean -> "a boolean (true or false)"
40 JSString -> "a string"
43 "an object with fields '" ++ List.intercalate "', '" fields ++ "'"
46_Array :: String -> Expression ()
48 useLazy ["Elm","Native","Array"] functionName
51_List :: String -> Expression ()
53 useLazy ["Elm","Native","List"] functionName
56_Maybe :: String -> Expression ()
58 useLazy ["Elm","Maybe"] functionName
61_Port :: String -> Expression ()
63 useLazy ["Elm","Native","Port"] functionName
66_Task :: String -> Expression ()
68 useLazy ["Elm","Native","Task"] functionName
71check :: Expression () -> JSType -> Expression () -> Expression ()
72check x jsType continue =
73 CondExpr () (jsFold OpLOr checks x) continue throw
75 jsFold op checks value =
76 foldl1 (InfixExpr () op) (map ($ value) checks)
79 obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
83 JSNumber -> [typeof "number"]
84 JSBoolean -> [typeof "boolean"]
85 JSString -> [typeof "string", instanceof "String"]
86 JSArray -> [instanceof "Array"]
88 [jsFold OpLAnd (typeof "object" : map member fields)]
93inbound :: String -> T.PortType Var.Canonical -> Expression ()
94inbound name portType =
97 _Port "inbound" `call`
99 , string (show (pretty tipe))
100 , toTypeFunction tipe
103 T.Signal _root arg ->
104 _Port "inboundSignal" `call`
106 , string (show (pretty arg))
111toTypeFunction :: CanonicalType -> Expression ()
113 ["v"] ==> toType tipe (ref "v")
116toType :: CanonicalType -> Expression () -> Expression ()
120 error "functions should not be allowed through input ports"
123 error "type variables should not be allowed through input ports"
126 toType (dealias args t) x
128 Type (Var.Canonical Var.BuiltIn name)
129 | name == "Int" -> from JSNumber
130 | name == "Float" -> from JSNumber
131 | name == "Bool" -> from JSBoolean
132 | name == "String" -> from JSString
134 from checks = check x checks x
140 | Var.isTuple name ->
144 error "bad type got to foreign input conversion"
149 | Var.isMaybe name ->
151 (equal x (NullLit ()))
153 (_Maybe "Just" <| toType t x)
156 check x JSArray (_List "fromArray" <| array)
158 | Var.isArray name ->
159 check x JSArray (_Array "fromJSArray" <| array)
161 array = DotRef () x (var "map") <| toTypeFunction t
164 | Var.isTuple name ->
167 _ -> error "bad ADT got to foreign input conversion"
170 error "bad record got to foreign input conversion"
172 Record fields Nothing ->
173 check x (JSObject (map fst fields)) object
175 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
176 keys = map convert fields
177 convert (f,t) = (prop f, toType t (DotRef () x (var f)))
180toTuple :: [CanonicalType] -> Expression () -> Expression ()
182 check x JSArray (ObjectLit () fields)
185 (prop "ctor", ctor) : zipWith convert [0..] types
188 string ("_Tuple" ++ show (length types))
192 , toType t (BracketRef () x (IntLit () n))
198outbound :: String -> Expression () -> T.PortType Var.Canonical -> Expression ()
199outbound name expr portType =
202 _Port "outbound" `call` [ string name, fromTypeFunction tipe, expr ]
205 _Port "outboundSignal" `call` [ string name, fromTypeFunction arg, expr ]
208fromTypeFunction :: CanonicalType -> Expression ()
209fromTypeFunction tipe =
210 ["v"] ==> fromType tipe (ref "v")
213fromType :: CanonicalType -> Expression () -> Expression ()
217 fromType (dealias args t) x
220 | numArgs > 1 && numArgs < 10 ->
221 func (ref ('A':show numArgs) `call` (x:values))
223 func (foldl (<|) x values)
225 ts = T.collectLambdas tipe
226 numArgs = length ts - 1
227 args = map (\n -> '_' : show n) [0..]
228 values = zipWith toType (init ts) (map ref args)
230 function (take numArgs args)
231 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
232 , ret (fromType (last ts) (ref "_r"))
236 error "type variables should not be allowed through outputs"
238 Type (Var.Canonical Var.BuiltIn name)
239 | name `elem` ["Int","Float","Bool","String"] ->
243 | Var.isJson name -> x
244 | Var.isTuple name -> ArrayLit () []
245 | otherwise -> error "bad type got to an output"
250 | Var.isMaybe name ->
252 (equal (DotRef () x (var "ctor")) (string "Nothing"))
254 (fromType t (DotRef () x (var "_0")))
256 | Var.isArray name ->
257 DotRef () (_Array "toJSArray" <| x) (var "map") <| fromTypeFunction t
260 DotRef () (_List "toArray" <| x) (var "map") <| fromTypeFunction t
263 | Var.isTuple name ->
264 let convert n t = fromType t $ DotRef () x $ var ('_':show n)
265 in ArrayLit () $ zipWith convert [0..] ts
267 _ -> error "bad ADT got to an output"
270 error "bad record got to an output"
272 Record fields Nothing ->
275 keys = map convert fields
277 (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