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 ")
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 -> V.value "Native.Ports" "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 -> V.value "Native.Json" "fromJS" <| x
55 | Var.isTuple name -> incomingTuple [] x
56 | otherwise -> error "bad type got to incoming port generation code"
61 | Var.isMaybe name -> CondExpr () (equal x (NullLit ()))
62 (V.value "Maybe" "Nothing")
63 (V.value "Maybe" "Just" <| inc t x)
65 | Var.isList name -> check x JSArray (obj ["_L","fromArray"] <| array)
67 | Var.isArray name -> check x JSArray (obj ["_A","fromJSArray"] <| array)
69 array = DotRef () x (var "map") <| incoming t
72 | Var.isTuple name -> incomingTuple ts x
74 _ -> error "bad ADT got to incoming port generation code"
77 error "bad record got to incoming port generation code"
79 Record fields Nothing ->
80 check x (JSObject (map fst fields)) object
82 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
83 keys = map convert fields
84 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
86incomingTuple :: [CanonicalType] -> Expression () -> Expression ()
87incomingTuple types x =
88 check x JSArray (ObjectLit () fields)
90 fields = (prop "ctor", ctor) : zipWith convert [0..] types
92 ctor = string ("_Tuple" ++ show (length types))
96 , inc t (BracketRef () x (IntLit () n))
99outgoing :: CanonicalType -> Expression ()
102 Aliased _ t -> outgoing t
105 | Var.isSignal v -> V.value "Native.Ports" "outgoingSignal" <| outgoing t
107 _ -> ["v"] ==> out tipe (ref "v")
109out :: CanonicalType -> Expression () -> Expression ()
112 Aliased _ t -> out t x
115 | numArgs > 1 && numArgs < 10 ->
116 func (ref ('A':show numArgs) `call` (x:values))
117 | otherwise -> func (foldl (<|) x values)
119 ts = T.collectLambdas tipe
120 numArgs = length ts - 1
121 args = map (\n -> '_' : show n) [0..]
122 values = zipWith inc (init ts) (map ref args)
123 func body = function (take numArgs args)
124 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
125 , ret (out (last ts) (ref "_r"))
128 Var _ -> error "type variables should not be allowed through input ports"
130 Type (Var.Canonical Var.BuiltIn name)
131 | name `elem` ["Int","Float","Bool","String"] -> x
134 | Var.isJson name -> V.value "Native.Json" "toJS" <| x
135 | Var.isTuple name -> ArrayLit () []
136 | otherwise -> error "bad type got to outgoing port generation code"
141 | Var.isMaybe name ->
142 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
144 (out t (DotRef () x (var "_0")))
146 | Var.isArray name ->
147 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
150 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
153 | Var.isTuple name ->
154 let convert n t = out t $ DotRef () x $ var ('_':show n)
155 in ArrayLit () $ zipWith convert [0..] ts
157 _ -> error "bad ADT got to outgoing port generation code"
160 error "bad record got to outgoing port generation code"
162 Record fields Nothing ->
165 keys = map convert fields
166 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