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 | otherwise -> error "bad type got to incoming port generation code"
60 | Var.isMaybe name -> CondExpr () (equal x (NullLit ()))
61 (V.value "Maybe" "Nothing")
62 (V.value "Maybe" "Just" <| inc t x)
64 | Var.isList name -> check x JSArray (obj ["_L","fromArray"] <| array)
66 | Var.isArray name -> check x JSArray (obj ["_A","fromJSArray"] <| array)
68 array = DotRef () x (var "map") <| incoming t
71 | Var.isTuple name -> check x JSArray tuple
73 tuple = ObjectLit () $ (prop "ctor", ctor) : values
75 ctor = string ("_Tuple" ++ show (length ts))
76 values = zipWith convert [0..] ts
78 convert n t = ( prop ('_':show n)
79 , inc t (BracketRef () x (IntLit () n))
82 _ -> error "bad ADT got to incoming port generation code"
85 error "bad record got to incoming port generation code"
87 Record fields Nothing ->
88 check x (JSObject (map fst fields)) object
90 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
91 keys = map convert fields
92 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
94outgoing :: CanonicalType -> Expression ()
97 Aliased _ t -> outgoing t
100 | Var.isSignal v -> V.value "Native.Ports" "outgoingSignal" <| outgoing t
102 _ -> ["v"] ==> out tipe (ref "v")
104out :: CanonicalType -> Expression () -> Expression ()
107 Aliased _ t -> out t x
110 | numArgs > 1 && numArgs < 10 ->
111 func (ref ('A':show numArgs) `call` (x:values))
112 | otherwise -> func (foldl (<|) x values)
114 ts = T.collectLambdas tipe
115 numArgs = length ts - 1
116 args = map (\n -> '_' : show n) [0..]
117 values = zipWith inc (init ts) (map ref args)
118 func body = function (take numArgs args)
119 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
120 , ret (out (last ts) (ref "_r"))
123 Var _ -> error "type variables should not be allowed through input ports"
125 Type (Var.Canonical Var.BuiltIn name)
126 | name `elem` ["Int","Float","Bool","String"] -> x
129 | Var.isJson name -> V.value "Native.Json" "toJS" <| x
130 | otherwise -> error "bad type got to outgoing port generation code"
135 | Var.isMaybe name ->
136 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
138 (out t (DotRef () x (var "_0")))
140 | Var.isArray name ->
141 DotRef () (obj ["_A","toJSArray"] <| x) (var "map") <| outgoing t
144 DotRef () (obj ["_L","toArray"] <| x) (var "map") <| outgoing t
147 | Var.isTuple name ->
148 let convert n t = out t $ DotRef () x $ var ('_':show n)
149 in ArrayLit () $ zipWith convert [0..] ts
151 _ -> error "bad ADT got to outgoing port generation code"
154 error "bad record got to outgoing port generation code"
156 Record fields Nothing ->
159 keys = map convert fields
160 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