2module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
5import qualified Generate.JavaScript.Variable as V
7import qualified AST.Variable as Var
8import Language.ECMAScript3.Syntax
10data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
13check :: Expression () -> JSType -> Expression () -> Expression ()
14check x jsType continue =
15 CondExpr () (jsFold OpLOr checks x) continue throw
17 jsFold op checks value = foldl1 (InfixExpr () op) (map ($value) checks)
18 throw = obj ["_E","raise"] <| InfixExpr () OpAdd msg x
19 msg = string ("invalid input, expecting " ++ show jsType ++ " but got ")
22 JSNumber -> [typeof "number"]
23 JSBoolean -> [typeof "boolean"]
24 JSString -> [typeof "string", instanceof "String"]
25 JSArray -> [(obj ["_U","isJSArray"] <|)]
26 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
28incoming :: CanonicalType -> Expression ()
31 Aliased _ t -> incoming t
34 | Var.isSignal v -> V.value ["Native","Ports"] "incomingSignal" <| incoming t
36 _ -> ["v"] ==> inc tipe (ref "v")
38inc :: CanonicalType -> Expression () -> Expression ()
41 Lambda _ _ -> error "functions should not be allowed through input ports"
42 Var _ -> error "type variables should not be allowed through input ports"
46 Type (Var.Canonical Var.BuiltIn name)
47 | name == "Int" -> from JSNumber
48 | name == "Float" -> from JSNumber
49 | name == "Bool" -> from JSBoolean
50 | name == "String" -> from JSString
52 from checks = check x checks x
55 | Var.isJson name -> V.value ["Native","Json"] "fromJS" <| x
56 | Var.isTuple name -> incomingTuple [] x
57 | otherwise -> error "bad type got to incoming port generation code"
64 (equal x (NullLit ()))
65 (V.value ["Maybe"] "Nothing")
66 (V.value ["Maybe"] "Just" <| inc t x)
69 check x JSArray (obj ["_L","fromArray"] <| array)
72 check x JSArray (obj ["_A","fromJSArray"] <| array)
74 array = DotRef () x (var "map") <| incoming t
77 | Var.isTuple name -> incomingTuple ts x
79 _ -> error "bad ADT got to incoming port generation code"
82 error "bad record got to incoming port generation code"
84 Record fields Nothing ->
85 check x (JSObject (map fst fields)) object
87 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
88 keys = map convert fields
89 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
91incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
92incomingTuple types x =
93 check x JSArray (ObjectLit () fields)
95 fields = (prop "ctor", ctor) : zipWith convert [0..] types
97 ctor = string ("_Tuple" ++ show (length types))
101 , inc t (BracketRef () x (IntLit () n))
104outgoing :: CanonicalType -> Expression ()
107 Aliased _ t -> outgoing t
110 | Var.isSignal v -> V.value ["Native","Ports"] "outgoingSignal" <| outgoing t
112 _ -> ["v"] ==> out tipe (ref "v")
114out :: CanonicalType -> Expression () -> Expression ()
117 Aliased _ t -> out t x
120 | numArgs > 1 && numArgs < 10 ->
121 func (ref ('A':show numArgs) `call` (x:values))
122 | otherwise -> func (foldl (<|) x values)
124 ts = T.collectLambdas tipe
125 numArgs = length ts - 1
126 args = map (\n -> '_' : show n) [0..]
127 values = zipWith inc (init ts) (map ref args)
129 function (take numArgs args)
130 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
131 , ret (out (last ts) (ref "_r"))
134 Var _ -> error "type variables should not be allowed through input ports"
136 Type (Var.Canonical Var.BuiltIn name)
137 | name `elem` ["Int","Float","Bool","String"] -> x
140 | Var.isJson name -> V.value ["Native","Json"] "toJS" <| x
141 | Var.isTuple name -> ArrayLit () []
142 | otherwise -> error "bad type got to outgoing port generation code"
147 | Var.isMaybe name ->
149 (equal (DotRef () x (var "ctor")) (string "Nothing"))
151 (out t (DotRef () x (var "_0")))
153 | Var.isArray name ->
154 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
157 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
160 | Var.isTuple name ->
161 let convert n t = out t $ DotRef () x $ var ('_':show n)
162 in ArrayLit () $ zipWith convert [0..] ts
164 _ -> error "bad ADT got to outgoing port generation code"
167 error "bad record got to outgoing port generation code"
169 Record fields Nothing ->
172 keys = map convert fields
173 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