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.Render.Type as RenderType
15task :: String -> Expression () -> T.Port t -> Expression ()
16task name expr portType =
19 _Task "perform" `call` [ expr ]
22 _Task "performSignal" `call` [ StringLit () name, expr ]
37typeToString :: JSType -> String
40 JSNumber -> "a number"
42 JSBoolean -> "a boolean (true or false)"
43 JSString -> "a string"
46 "an object with fields `" ++ List.intercalate "`, `" fields ++ "`"
49_Array :: String -> Expression ()
51 useLazy ["Elm","Native","Array"] functionName
54_List :: String -> Expression ()
56 useLazy ["Elm","Native","List"] functionName
59_Maybe :: String -> Expression ()
61 useLazy ["Elm","Maybe"] functionName
64_Port :: String -> Expression ()
66 useLazy ["Elm","Native","Port"] functionName
69_Task :: String -> Expression ()
71 useLazy ["Elm","Native","Task"] functionName
74check :: Expression () -> JSType -> Expression () -> Expression ()
75check x jsType continue =
76 CondExpr () (jsFold OpLOr checks x) continue throw
78 jsFold op checks value =
79 foldl1 (InfixExpr () op) (map ($ value) checks)
82 obj ["_U","badPort"] `call` [ StringLit () (typeToString jsType), x ]
86 JSNumber -> [typeof "number"]
88 [jsFold OpLAnd (typeof "number" : intChecks)]
89 JSBoolean -> [typeof "boolean"]
90 JSString -> [typeof "string", instanceof "String"]
91 JSArray -> [instanceof "Array"]
93 [jsFold OpLAnd (typeof "object" : map member fields)]
96 [ \x -> ref "isFinite" <| x
97 , \x -> equal (obj ["Math","floor"] <| x) x
103inbound :: String -> T.Port T.Canonical -> Expression ()
104inbound name portType =
107 _Port "inbound" `call`
109 , StringLit () (show (RenderType.toDoc Map.empty tipe))
110 , toTypeFunction tipe
113 T.Signal _root arg ->
114 _Port "inboundSignal" `call`
116 , StringLit () (show (RenderType.toDoc Map.empty arg))
121toTypeFunction :: T.Canonical -> Expression ()
123 function ["v"] [ ReturnStmt () (Just (toType tipe (ref "v"))) ]
126toType :: T.Canonical -> Expression () -> Expression ()
130 error "functions should not be allowed through input ports"
133 error "type variables should not be allowed through input ports"
135 T.Aliased _ args t ->
136 toType (T.dealias args t) x
138 T.Type (Var.Canonical Var.BuiltIn name)
139 | name == "Float" -> from JSNumber
140 | name == "Int" -> from JSInt
141 | name == "Bool" -> from JSBoolean
142 | name == "String" -> from JSString
144 from checks = check x checks x
150 | Var.isTuple name ->
154 error "bad type got to foreign input conversion"
159 | Var.isMaybe name ->
161 (equal x (NullLit ()))
163 (_Maybe "Just" <| toType t x)
166 check x JSArray (_List "fromArray" <| array)
168 | Var.isArray name ->
169 check x JSArray (_Array "fromJSArray" <| array)
171 array = DotRef () x (var "map") <| toTypeFunction t
174 | Var.isTuple name ->
177 _ -> error "bad ADT got to foreign input conversion"
179 T.Record _ (Just _) ->
180 error "bad record got to foreign input conversion"
182 T.Record fields Nothing ->
183 check x (JSObject (map fst fields)) object
185 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
186 keys = map convert fields
187 convert (f,t) = (prop f, toType t (DotRef () x (var f)))
190toTuple :: [T.Canonical] -> Expression () -> Expression ()
192 check x JSArray (ObjectLit () fields)
195 (prop "ctor", ctor) : zipWith convert [0..] types
198 StringLit () ("_Tuple" ++ show (length types))
202 , toType t (BracketRef () x (IntLit () n))
208outbound :: String -> Expression () -> T.Port T.Canonical -> Expression ()
209outbound name expr portType =
212 _Port "outbound" `call` [ StringLit () name, fromTypeFunction tipe, expr ]
215 _Port "outboundSignal" `call` [ StringLit () name, fromTypeFunction arg, expr ]
218fromTypeFunction :: T.Canonical -> Expression ()
219fromTypeFunction tipe =
220 function ["v"] [ ReturnStmt () (Just (fromType tipe (ref "v"))) ]
223fromType :: T.Canonical -> Expression () -> Expression ()
226 T.Aliased _ args t ->
227 fromType (T.dealias args t) x
230 | numArgs > 1 && numArgs < 10 ->
231 func (ref ('A':show numArgs) `call` (x:values))
233 func (foldl (<|) x values)
235 ts = T.collectLambdas tipe
236 numArgs = length ts - 1
237 args = map (\n -> '_' : show n) [0..]
238 values = zipWith toType (init ts) (map ref args)
240 function (take numArgs args)
241 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
242 , ReturnStmt () (Just (fromType (last ts) (ref "_r")))
246 error "type variables should not be allowed through outputs"
248 T.Type (Var.Canonical Var.BuiltIn name)
249 | name `elem` ["Int","Float","Bool","String"] ->
253 | Var.isJson name -> x
254 | Var.isTuple name -> ArrayLit () []
255 | otherwise -> error "bad type got to an output"
260 | Var.isMaybe name ->
262 (equal (DotRef () x (var "ctor")) (StringLit () "Nothing"))
264 (fromType t (DotRef () x (var "_0")))
266 | Var.isArray name ->
267 DotRef () (_Array "toJSArray" <| x) (var "map") <| fromTypeFunction t
270 DotRef () (_List "toArray" <| x) (var "map") <| fromTypeFunction t
273 | Var.isTuple name ->
274 let convert n t = fromType t $ DotRef () x $ var ('_':show n)
275 in ArrayLit () $ zipWith convert [0..] ts
277 _ -> error "bad ADT got to an output"
279 T.Record _ (Just _) ->
280 error "bad record got to an output"
282 T.Record fields Nothing ->
285 keys = map convert fields
287 (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