2module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
5import qualified SourceSyntax.Helpers as Help
6import SourceSyntax.Type
7import Language.ECMAScript3.Syntax
9check :: a -> [a -> Expression ()] -> Expression () -> Expression ()
10check x checks continue =
11 CondExpr () (foldl1 (InfixExpr () OpLOr) (map ($x) checks)) continue throw
13 throw = obj "_E.raise" <| string "invalid input to port"
15incoming :: Type -> Expression ()
16incoming t = ["v"] ==> go t (ref "v")
18 go :: Type -> Expression () -> Expression ()
21 Lambda _ _ -> error "functions should not be allowed through input ports"
22 Var _ -> error "type variables should not be allowed through input ports"
24 | ctor == "Int" -> elm "Int" [typeof "number"]
25 | ctor == "Float" -> elm "Float" [typeof "number"]
26 | ctor == "Bool" -> elm "Bool" [typeof "boolean"]
27 | ctor == "String" -> elm "String" [typeof "string", instanceof "String"]
28 | ctor == "JavaScript.JSNumber" -> js [typeof "number"]
29 | ctor == "JavaScript.JSBool" -> js [typeof "boolean"]
30 | ctor == "JavaScript.JSString" -> js [typeof "string", instanceof "String"]
32 elm out checks = check x checks (obj ("_J.to" ++ out) <| x)
33 js checks = check x checks x
36 | ctor == "Maybe.Maybe" ->
37 CondExpr () (equal x (NullLit ()))
39 (obj "Maybe.Just" <| go t x)
41 | ctor == "Signal.Signal" ->
42 obj "Native.Ports.incomingSignal" <| incoming t
45 check x [instanceof "Array"] (obj "_J.toList" <| array)
47 array = DotRef () x (var "map") <| incoming t
49 Data ctor ts | Help.isTuple ctor -> check x [instanceof "Array"] tuple
51 tuple = ObjectLit () $ (PropId () (var "ctor"), string ctor) : values
52 values = zipWith convert [0..] ts
53 convert n t = ( PropId () $ var ('_':show n)
54 , go t (BracketRef () x (IntLit () n)))
56 Data _ _ -> error "bad ADT got to port generation code"
58 Record _ (Just _) -> error "bad record got to port generation code"
60 Record fields Nothing ->
61 check x (typeof "object" : map (member . fst) fields) object
63 object = ObjectLit () $ (PropId () (var "_"), ObjectLit () []) : keys
64 keys = map convert fields
65 convert (f,t) = (PropId () (var f), go t (DotRef () x (var f)))
67outgoing t = ["v"] ==> go t (ref "v")
69 go :: Type -> Expression () -> Expression ()
72 Lambda _ _ -> error "unimplemented"
73 Var _ -> error "type variables should not be allowed through input ports"
75 | ctor `elem` ["Int","Float","Bool","String"] -> obj ("_J.from" ++ ctor) <| x
76 | ctor `elem` jsPrims -> x
78 jsPrims = map ("JavaScript.JS"++) ["Number","Bool","String"]
81 | ctor == "Maybe.Maybe" ->
82 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
84 (DotRef () x (var "_0"))
86 | ctor == "Signal.Signal" ->
87 obj "Native.Ports.outgoingSignal" <| incoming t
90 DotRef () (obj "_J.fromList" <| x) (var "map") <| incoming t
92 Data ctor ts | Help.isTuple ctor ->
93 ArrayLit () $ zipWith convert [0..] ts
95 convert n t = go t $ DotRef () x $ var ('_':show n)
97 Data _ _ -> error "bad ADT got to port generation code"
99 Record _ (Just _) -> error "bad record got to port generation code"
101 Record fields Nothing ->
104 keys = map convert fields
105 convert (f,t) = (PropId () (var f), go 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