2module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
6import qualified AST.Variable as Var
7import Language.ECMAScript3.Syntax
8import Generate.JavaScript.Variable (canonical)
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 ")
20 checks = case jsType of
21 JSNumber -> [typeof "number"]
22 JSBoolean -> [typeof "boolean"]
23 JSString -> [typeof "string", instanceof "String"]
24 JSArray -> [(obj ["_U","isJSArray"] <|)]
25 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
27incoming :: CanonicalType -> Expression ()
30 Aliased _ t -> incoming t
33 | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
35 _ -> ["v"] ==> inc tipe (ref "v")
37inc :: CanonicalType -> Expression () -> Expression ()
40 Lambda _ _ -> error "functions should not be allowed through input ports"
41 Var _ -> error "type variables should not be allowed through input ports"
45 Type (Var.Canonical Var.BuiltIn name)
46 | name == "Int" -> from JSNumber
47 | name == "Float" -> from JSNumber
48 | name == "Bool" -> from JSBoolean
49 | name == "String" -> from JSString
51 from checks = check x checks x
54 | Var.isJson name -> canonical fromJS <| x
55 | otherwise -> error "bad type got to incoming port generation code"
57 fromJS = Var.Canonical (Var.Module "Native.Json") "fromJS"
63 | Var.isMaybe name -> CondExpr () (equal x (NullLit ()))
64 (obj ["Maybe","Nothing"])
65 (obj ["Maybe","Just"] <| inc t x)
67 | Var.isList name -> check x JSArray (obj ["_L","fromArray"] <| array)
69 | Var.isArray name -> check x JSArray (obj ["_A","fromJSArray"] <| array)
71 array = DotRef () x (var "map") <| incoming t
74 | Var.isTuple name -> check x JSArray tuple
76 tuple = ObjectLit () $ (prop "ctor", ctor) : values
78 ctor = string ("_Tuple" ++ show (length ts))
79 values = zipWith convert [0..] ts
81 convert n t = ( prop ('_':show n)
82 , inc t (BracketRef () x (IntLit () n))
85 _ -> error "bad ADT got to incoming port generation code"
88 error "bad record got to incoming port generation code"
90 Record fields Nothing ->
91 check x (JSObject (map fst fields)) object
93 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
94 keys = map convert fields
95 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
97outgoing :: CanonicalType -> Expression ()
100 Aliased _ t -> outgoing t
103 | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
105 _ -> ["v"] ==> out tipe (ref "v")
107out :: CanonicalType -> Expression () -> Expression ()
110 Aliased _ t -> out t x
113 | numArgs > 1 && numArgs < 10 ->
114 func (ref ('A':show numArgs) `call` (x:values))
115 | otherwise -> func (foldl (<|) x values)
117 ts = T.collectLambdas tipe
118 numArgs = length ts - 1
119 args = map (\n -> '_' : show n) [0..]
120 values = zipWith inc (init ts) (map ref args)
121 func body = function (take numArgs args)
122 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
123 , ret (out (last ts) (ref "_r"))
126 Var _ -> error "type variables should not be allowed through input ports"
128 Type (Var.Canonical Var.BuiltIn name)
129 | name `elem` ["Int","Float","Bool","String"] -> x
132 | Var.isJson name -> canonical toJS <| x
133 | otherwise -> error "bad type got to outgoing port generation code"
135 toJS = Var.Canonical (Var.Module "Native.Json") "toJS"
140 | Var.isMaybe name ->
141 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
143 (out t (DotRef () x (var "_0")))
145 | Var.isArray name ->
146 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
149 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
152 | Var.isTuple name ->
153 let convert n t = out t $ DotRef () x $ var ('_':show n)
154 in ArrayLit () $ zipWith convert [0..] ts
156 _ -> error "bad ADT got to outgoing port generation code"
159 error "bad record got to outgoing port generation code"
161 Record fields Nothing ->
164 keys = map convert fields
165 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