1module Generate.JavaScript.Port (inbound, outbound, task) where
3import qualified Data.List as List
4import qualified Data.Map as Map
5import Language.ECMAScript3.Syntax
7import qualified AST.Type as T
8import qualified AST.Variable as Var
9import Generate.JavaScript.Helpers
10import qualified Reporting.PrettyPrint as P
15task :: String -> Expression () -> T.Port t -> Expression ()
16task name expr portType =
19 _Task "perform" `call` [ expr ]
22 _Task "performSignal" `call` [ string name, expr ]
36typeToString :: JSType -> String
39 JSNumber -> "a number"
40 JSBoolean -> "a boolean (true or false)"
41 JSString -> "a string"
44 "an object with fields '" ++ List.intercalate "', '" fields ++ "'"
47_Array :: String -> Expression ()
49 useLazy ["Elm","Native","Array"] functionName
52_List :: String -> Expression ()
54 useLazy ["Elm","Native","List"] functionName
57_Maybe :: String -> Expression ()
59 useLazy ["Elm","Maybe"] functionName
62_Port :: String -> Expression ()
64 useLazy ["Elm","Native","Port"] functionName
67_Task :: String -> Expression ()
69 useLazy ["Elm","Native","Task"] functionName
72check :: Expression () -> JSType -> Expression () -> Expression ()
73check x jsType continue =
74 CondExpr () (jsFold OpLOr checks x) continue throw
76 jsFold op checks value =
77 foldl1 (InfixExpr () op) (map ($ value) checks)
80 obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
84 JSNumber -> [typeof "number"]
85 JSBoolean -> [typeof "boolean"]
86 JSString -> [typeof "string", instanceof "String"]
87 JSArray -> [instanceof "Array"]
89 [jsFold OpLAnd (typeof "object" : map member fields)]
94inbound :: String -> T.Port T.Canonical -> Expression ()
95inbound name portType =
98 _Port "inbound" `call`
100 , string (show (P.pretty Map.empty False tipe))
101 , toTypeFunction tipe
104 T.Signal _root arg ->
105 _Port "inboundSignal" `call`
107 , string (show (P.pretty Map.empty False arg))
112toTypeFunction :: T.Canonical -> Expression ()
114 ["v"] ==> toType tipe (ref "v")
117toType :: T.Canonical -> Expression () -> Expression ()
121 error "functions should not be allowed through input ports"
124 error "type variables should not be allowed through input ports"
126 T.Aliased _ args t ->
127 toType (T.dealias args t) x
129 T.Type (Var.Canonical Var.BuiltIn name)
130 | name == "Int" -> from JSNumber
131 | name == "Float" -> from JSNumber
132 | name == "Bool" -> from JSBoolean
133 | name == "String" -> from JSString
135 from checks = check x checks x
141 | Var.isTuple name ->
145 error "bad type got to foreign input conversion"
150 | Var.isMaybe name ->
152 (equal x (NullLit ()))
154 (_Maybe "Just" <| toType t x)
157 check x JSArray (_List "fromArray" <| array)
159 | Var.isArray name ->
160 check x JSArray (_Array "fromJSArray" <| array)
162 array = DotRef () x (var "map") <| toTypeFunction t
165 | Var.isTuple name ->
168 _ -> error "bad ADT got to foreign input conversion"
170 T.Record _ (Just _) ->
171 error "bad record got to foreign input conversion"
173 T.Record fields Nothing ->
174 check x (JSObject (map fst fields)) object
176 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
177 keys = map convert fields
178 convert (f,t) = (prop f, toType t (DotRef () x (var f)))
181toTuple :: [T.Canonical] -> Expression () -> Expression ()
183 check x JSArray (ObjectLit () fields)
186 (prop "ctor", ctor) : zipWith convert [0..] types
189 string ("_Tuple" ++ show (length types))
193 , toType t (BracketRef () x (IntLit () n))
199outbound :: String -> Expression () -> T.Port T.Canonical -> Expression ()
200outbound name expr portType =
203 _Port "outbound" `call` [ string name, fromTypeFunction tipe, expr ]
206 _Port "outboundSignal" `call` [ string name, fromTypeFunction arg, expr ]
209fromTypeFunction :: T.Canonical -> Expression ()
210fromTypeFunction tipe =
211 ["v"] ==> fromType tipe (ref "v")
214fromType :: T.Canonical -> Expression () -> Expression ()
217 T.Aliased _ args t ->
218 fromType (T.dealias args t) x
221 | numArgs > 1 && numArgs < 10 ->
222 func (ref ('A':show numArgs) `call` (x:values))
224 func (foldl (<|) x values)
226 ts = T.collectLambdas tipe
227 numArgs = length ts - 1
228 args = map (\n -> '_' : show n) [0..]
229 values = zipWith toType (init ts) (map ref args)
231 function (take numArgs args)
232 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
233 , ret (fromType (last ts) (ref "_r"))
237 error "type variables should not be allowed through outputs"
239 T.Type (Var.Canonical Var.BuiltIn name)
240 | name `elem` ["Int","Float","Bool","String"] ->
244 | Var.isJson name -> x
245 | Var.isTuple name -> ArrayLit () []
246 | otherwise -> error "bad type got to an output"
251 | Var.isMaybe name ->
253 (equal (DotRef () x (var "ctor")) (string "Nothing"))
255 (fromType t (DotRef () x (var "_0")))
257 | Var.isArray name ->
258 DotRef () (_Array "toJSArray" <| x) (var "map") <| fromTypeFunction t
261 DotRef () (_List "toArray" <| x) (var "map") <| fromTypeFunction t
264 | Var.isTuple name ->
265 let convert n t = fromType t $ DotRef () x $ var ('_':show n)
266 in ArrayLit () $ zipWith convert [0..] ts
268 _ -> error "bad ADT got to an output"
270 T.Record _ (Just _) ->
271 error "bad record got to an output"
273 T.Record fields Nothing ->
276 keys = map convert fields
278 (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