2module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
5import qualified SourceSyntax.Helpers as Help
6import SourceSyntax.Type as T
7import Language.ECMAScript3.Syntax
9data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
12check :: Expression () -> JSType -> Expression () -> Expression ()
13check x jsType continue =
14 CondExpr () (jsFold OpLOr checks x) continue throw
16 jsFold op checks value = foldl1 (InfixExpr () op) (map ($value) checks)
17 throw = obj "_E.raise" <| InfixExpr () OpAdd msg x
18 msg = string ("invalid input, expecting " ++ show jsType ++ " but got ")
19 checks = case jsType of
20 JSNumber -> [typeof "number"]
21 JSBoolean -> [typeof "boolean"]
22 JSString -> [typeof "string", instanceof "String"]
23 JSArray -> [instanceof "Array"]
24 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
26incoming :: Type -> Expression ()
29 Data "Signal.Signal" [t] ->
30 obj "Native.Ports.incomingSignal" <| incoming t
31 _ -> ["v"] ==> inc tipe (ref "v")
33inc :: Type -> Expression () -> Expression ()
36 Lambda _ _ -> error "functions should not be allowed through input ports"
37 Var _ -> error "type variables should not be allowed through input ports"
39 | ctor == "Int" -> elm JSNumber
40 | ctor == "Float" -> elm JSNumber
41 | ctor == "Bool" -> elm JSBoolean
42 | ctor == "String" -> elm JSString
43 | ctor == "JavaScript.JSNumber" -> js JSNumber
44 | ctor == "JavaScript.JSBool" -> js JSBoolean
45 | ctor == "JavaScript.JSString" -> js JSString
47 elm checks = check x checks (obj ("_J.to" ++ ctor) <| x)
48 js checks = check x checks x
51 | ctor == "Maybe.Maybe" ->
52 CondExpr () (equal x (NullLit ()))
54 (obj "Maybe.Just" <| inc t x)
57 check x JSArray (obj "_J.toList" <| array)
59 array = DotRef () x (var "map") <| incoming t
61 Data ctor ts | Help.isTuple ctor -> check x JSArray tuple
63 tuple = ObjectLit () $ (PropId () (var "ctor"), string ctor) : values
64 values = zipWith convert [0..] ts
65 convert n t = ( PropId () $ var ('_':show n)
66 , inc t (BracketRef () x (IntLit () n)))
68 Data _ _ -> error "bad ADT got to port generation code"
70 Record _ (Just _) -> error "bad record got to port generation code"
72 Record fields Nothing -> check x (JSObject (map fst fields)) object
74 object = ObjectLit () $ (PropId () (var "_"), ObjectLit () []) : keys
75 keys = map convert fields
76 convert (f,t) = (PropId () (var f), inc t (DotRef () x (var f)))
80 Data "Signal.Signal" [t] ->
81 obj "Native.Ports.outgoingSignal" <| outgoing t
82 _ -> ["v"] ==> out tipe (ref "v")
84out :: Type -> Expression () -> Expression ()
88 | numArgs > 1 && numArgs < 10 ->
89 func (ref ('A':show numArgs) `call` (x:values))
90 | otherwise -> func (foldl (<|) x values)
92 ts = T.collectLambdas tipe
93 numArgs = length ts - 1
94 args = map (\n -> '_' : show n) [0..]
95 values = zipWith inc (init ts) (map ref args)
96 func body = function (take numArgs args)
97 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
98 , ret (out (last ts) (ref "_r"))
101 Var _ -> error "type variables should not be allowed through input ports"
103 | ctor `elem` ["Int","Float","Bool","String"] -> obj ("_J.from" ++ ctor) <| x
104 | ctor `elem` jsPrims -> x
106 jsPrims = map ("JavaScript.JS"++) ["Number","Bool","String"]
109 | ctor == "Maybe.Maybe" ->
110 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
112 (DotRef () x (var "_0"))
115 DotRef () (obj "_J.fromList" <| x) (var "map") <| outgoing t
117 Data ctor ts | Help.isTuple ctor ->
118 ArrayLit () $ zipWith convert [0..] ts
120 convert n t = out t $ DotRef () x $ var ('_':show n)
122 Data _ _ -> error "bad ADT got to port generation code"
124 Record _ (Just _) -> error "bad record got to port generation code"
126 Record fields Nothing ->
129 keys = map convert fields
130 convert (f,t) = (PropId () (var f), out 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