1module Generate.JavaScript.Port (inbound, internal, outbound) where
3import qualified Data.List as List
4import Generate.JavaScript.Helpers
5import AST.PrettyPrint (pretty)
7import qualified AST.Variable as Var
8import Language.ECMAScript3.Syntax
19typeToString :: JSType -> String
22 JSNumber -> "a number"
23 JSBoolean -> "a boolean (true or false)"
24 JSString -> "a string"
27 "an object with fields '" ++ List.intercalate "', '" fields ++ "'"
30_Array :: String -> Expression ()
32 useLazy ["Elm","Native","Array"] functionName
35_List :: String -> Expression ()
37 useLazy ["Elm","Native","List"] functionName
40_Maybe :: String -> Expression ()
42 useLazy ["Elm","Maybe"] functionName
45_Port :: String -> Expression ()
47 useLazy ["Elm","Native","Port"] functionName
50check :: Expression () -> JSType -> Expression () -> Expression ()
51check x jsType continue =
52 CondExpr () (jsFold OpLOr checks x) continue throw
54 jsFold op checks value =
55 foldl1 (InfixExpr () op) (map ($ value) checks)
58 obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
62 JSNumber -> [typeof "number"]
63 JSBoolean -> [typeof "boolean"]
64 JSString -> [typeof "string", instanceof "String"]
65 JSArray -> [instanceof "Array"]
67 [jsFold OpLAnd (typeof "object" : map member fields)]
72internal :: String -> Expression ()
74 _Port "port" `call` [ string name ]
79inbound :: String -> CanonicalType -> Expression ()
81 _Port "inbound" `call`
83 , string (show (pretty tipe))
88toTypeFunction :: CanonicalType -> Expression ()
90 ["v"] ==> toType tipe (ref "v")
93toType :: CanonicalType -> Expression () -> Expression ()
97 error "functions should not be allowed through input ports"
100 error "type variables should not be allowed through input ports"
103 toType (dealias args t) x
105 Type (Var.Canonical Var.BuiltIn name)
106 | name == "Int" -> from JSNumber
107 | name == "Float" -> from JSNumber
108 | name == "Bool" -> from JSBoolean
109 | name == "String" -> from JSString
111 from checks = check x checks x
117 | Var.isTuple name ->
121 error "bad type got to foreign input conversion"
126 | Var.isMaybe name ->
128 (equal x (NullLit ()))
130 (_Maybe "Just" <| toType t x)
133 check x JSArray (_List "fromArray" <| array)
135 | Var.isArray name ->
136 check x JSArray (_Array "fromJSArray" <| array)
138 array = DotRef () x (var "map") <| toTypeFunction t
141 | Var.isTuple name ->
144 _ -> error "bad ADT got to foreign input conversion"
147 error "bad record got to foreign input conversion"
149 Record fields Nothing ->
150 check x (JSObject (map fst fields)) object
152 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
153 keys = map convert fields
154 convert (f,t) = (prop f, toType t (DotRef () x (var f)))
157toTuple :: [CanonicalType] -> Expression () -> Expression ()
159 check x JSArray (ObjectLit () fields)
162 (prop "ctor", ctor) : zipWith convert [0..] types
165 string ("_Tuple" ++ show (length types))
169 , toType t (BracketRef () x (IntLit () n))
175outbound :: String -> CanonicalType -> Expression ()
177 _Port "outbound" `call` [ string name, fromTypeFunction tipe ]
180fromTypeFunction :: CanonicalType -> Expression ()
181fromTypeFunction tipe =
182 ["v"] ==> fromType tipe (ref "v")
185fromType :: CanonicalType -> Expression () -> Expression ()
189 fromType (dealias args t) x
192 | numArgs > 1 && numArgs < 10 ->
193 func (ref ('A':show numArgs) `call` (x:values))
195 func (foldl (<|) x values)
197 ts = T.collectLambdas tipe
198 numArgs = length ts - 1
199 args = map (\n -> '_' : show n) [0..]
200 values = zipWith toType (init ts) (map ref args)
202 function (take numArgs args)
203 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
204 , ret (fromType (last ts) (ref "_r"))
208 error "type variables should not be allowed through outputs"
210 Type (Var.Canonical Var.BuiltIn name)
211 | name `elem` ["Int","Float","Bool","String"] ->
215 | Var.isJson name -> x
216 | Var.isTuple name -> ArrayLit () []
217 | otherwise -> error "bad type got to an output"
222 | Var.isMaybe name ->
224 (equal (DotRef () x (var "ctor")) (string "Nothing"))
226 (fromType t (DotRef () x (var "_0")))
228 | Var.isArray name ->
229 DotRef () (_Array "toJSArray" <| x) (var "map") <| fromTypeFunction t
232 DotRef () (_List "toArray" <| x) (var "map") <| fromTypeFunction t
235 | Var.isTuple name ->
236 let convert n t = fromType t $ DotRef () x $ var ('_':show n)
237 in ArrayLit () $ zipWith convert [0..] ts
239 _ -> error "bad ADT got to an output"
242 error "bad record got to an output"
244 Record fields Nothing ->
247 keys = map convert fields
249 (PropId () (var f), fromType 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