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 ]
90 [jsFold OpLAnd intChecks]
96 [typeof "string", instanceof "String"]
102 [jsFold OpLAnd (typeof "object" : map member fields)]
105intChecks :: [Expression () -> Expression ()]
108 , \x -> ref "isFinite" <| x
109 , \x -> equal (obj ["Math","floor"] <| x) x
115inbound :: String -> T.Port T.Canonical -> Expression ()
116inbound name portType =
119 _Port "inbound" `call`
121 , StringLit () (show (RenderType.toDoc Map.empty tipe))
122 , toTypeFunction tipe
125 T.Signal _root arg ->
126 _Port "inboundSignal" `call`
128 , StringLit () (show (RenderType.toDoc Map.empty arg))
133toTypeFunction :: T.Canonical -> Expression ()
135 function ["v"] [ ReturnStmt () (Just (toType tipe (ref "v"))) ]
138toType :: T.Canonical -> Expression () -> Expression ()
142 error "functions should not be allowed through input ports"
145 error "type variables should not be allowed through input ports"
147 T.Aliased _ args t ->
148 toType (T.dealias args t) x
150 T.Type (Var.Canonical Var.BuiltIn name)
151 | name == "Float" -> from JSNumber
152 | name == "Int" -> from JSInt
153 | name == "Bool" -> from JSBoolean
154 | name == "String" -> from JSString
156 from checks = check x checks x
162 | Var.isTuple name ->
166 error "bad type got to foreign input conversion"
171 | Var.isMaybe name ->
173 (equal x (NullLit ()))
175 (_Maybe "Just" <| toType t x)
178 check x JSArray (_List "fromArray" <| array)
180 | Var.isArray name ->
181 check x JSArray (_Array "fromJSArray" <| array)
183 array = DotRef () x (var "map") <| toTypeFunction t
186 | Var.isTuple name ->
189 _ -> error "bad ADT got to foreign input conversion"
191 T.Record _ (Just _) ->
192 error "bad record got to foreign input conversion"
194 T.Record fields Nothing ->
195 check x (JSObject (map fst fields)) object
197 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
198 keys = map convert fields
199 convert (f,t) = (prop f, toType t (DotRef () x (var f)))
202toTuple :: [T.Canonical] -> Expression () -> Expression ()
204 check x JSArray (ObjectLit () fields)
207 (prop "ctor", ctor) : zipWith convert [0..] types
210 StringLit () ("_Tuple" ++ show (length types))
214 , toType t (BracketRef () x (IntLit () n))
220outbound :: String -> Expression () -> T.Port T.Canonical -> Expression ()
221outbound name expr portType =
224 _Port "outbound" `call` [ StringLit () name, fromTypeFunction tipe, expr ]
227 _Port "outboundSignal" `call` [ StringLit () name, fromTypeFunction arg, expr ]
230fromTypeFunction :: T.Canonical -> Expression ()
231fromTypeFunction tipe =
232 function ["v"] [ ReturnStmt () (Just (fromType tipe (ref "v"))) ]
235fromType :: T.Canonical -> Expression () -> Expression ()
238 T.Aliased _ args t ->
239 fromType (T.dealias args t) x
242 | numArgs > 1 && numArgs < 10 ->
243 func (ref ('A':show numArgs) `call` (x:values))
245 func (foldl (<|) x values)
247 ts = T.collectLambdas tipe
248 numArgs = length ts - 1
249 args = map (\n -> '_' : show n) [0..]
250 values = zipWith toType (init ts) (map ref args)
252 function (take numArgs args)
253 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
254 , ReturnStmt () (Just (fromType (last ts) (ref "_r")))
258 error "type variables should not be allowed through outputs"
260 T.Type (Var.Canonical Var.BuiltIn name)
261 | name `elem` ["Int","Float","Bool","String"] ->
265 | Var.isJson name -> x
266 | Var.isTuple name -> ArrayLit () []
267 | otherwise -> error "bad type got to an output"
272 | Var.isMaybe name ->
274 (equal (DotRef () x (var "ctor")) (StringLit () "Nothing"))
276 (fromType t (DotRef () x (var "_0")))
278 | Var.isArray name ->
279 DotRef () (_Array "toJSArray" <| x) (var "map") <| fromTypeFunction t
282 DotRef () (_List "toArray" <| x) (var "map") <| fromTypeFunction t
285 | Var.isTuple name ->
286 let convert n t = fromType t $ DotRef () x $ var ('_':show n)
287 in ArrayLit () $ zipWith convert [0..] ts
289 _ -> error "bad ADT got to an output"
291 T.Record _ (Just _) ->
292 error "bad record got to an output"
294 T.Record fields Nothing ->
297 keys = map convert fields
299 (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