on 4e88d39 help sign in
136 lines 5.1 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Generate.JavaScript.Ports (incoming, outgoing) where
4import Generate.JavaScript.Helpers
5import qualified SourceSyntax.Helpers as Help
6import SourceSyntax.Type as T
7import Language.ECMAScript3.Syntax
9data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
10 deriving Show
12check :: Expression () -> JSType -> Expression () -> Expression ()
13check x jsType continue =
14 CondExpr () (jsFold OpLOr checks x) continue throw
15 where
16 jsFold op checks value = foldl1 (InfixExpr () op) (map ($value) checks)
17 throw = obj "_E.raise" <| InfixExpr () OpAdd msg x
18 msg = string ("invalid input, expecting " ++ show jsType ++ " but got ")
19 checks = case jsType of
20 JSNumber -> [typeof "number"]
21 JSBoolean -> [typeof "boolean"]
22 JSString -> [typeof "string", instanceof "String"]
23 JSArray -> [instanceof "Array"]
24 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
26incoming :: Type -> Expression ()
27incoming tipe =
28 case tipe of
29 Data "Signal.Signal" [t] ->
30 obj "Native.Ports.incomingSignal" <| incoming t
31 _ -> ["v"] ==> inc tipe (ref "v")
33inc :: Type -> Expression () -> Expression ()
34inc tipe x =
35 case tipe of
36 Lambda _ _ -> error "functions should not be allowed through input ports"
37 Var _ -> error "type variables should not be allowed through input ports"
38 Data ctor []
39 | ctor == "Int" -> elm JSNumber
40 | ctor == "Float" -> elm JSNumber
41 | ctor == "Bool" -> elm JSBoolean
42 | ctor == "String" -> elm JSString
43 | ctor == "JavaScript.JSNumber" -> js JSNumber
44 | ctor == "JavaScript.JSBool" -> js JSBoolean
45 | ctor == "JavaScript.JSString" -> js JSString
46 where
47 elm checks = check x checks (obj ("_J.to" ++ ctor) <| x)
48 js checks = check x checks x
50 Data ctor [t]
51 | ctor == "Maybe.Maybe" ->
52 CondExpr () (equal x (NullLit ()))
53 (obj "Maybe.Nothing")
54 (obj "Maybe.Just" <| inc t x)
56 | ctor == "_List" ->
57 check x JSArray (obj "_J.toList" <| array)
58 where
59 array = DotRef () x (var "map") <| incoming t
61 Data ctor ts
62 | Help.isTuple ctor -> check x JSArray tuple
63 where
64 tuple = ObjectLit () $ (prop "ctor", string ctor) : values
65 values = zipWith convert [0..] ts
66 convert n t = ( prop ('_':show n)
67 , inc t (BracketRef () x (IntLit () n)))
69 Data _ _ ->
70 error "bad ADT got to port generation code"
72 Record _ (Just _) ->
73 error "bad record got to port generation code"
75 Record fields Nothing ->
76 check x (JSObject (map fst fields)) object
77 where
78 object = ObjectLit () $ (prop "_", ObjectLit () []) : keys
79 keys = map convert fields
80 convert (f,t) = (prop f, inc t (DotRef () x (var f)))
82outgoing tipe =
83 case tipe of
84 Data "Signal.Signal" [t] ->
85 obj "Native.Ports.outgoingSignal" <| outgoing t
86 _ -> ["v"] ==> out tipe (ref "v")
88out :: Type -> Expression () -> Expression ()
89out tipe x =
90 case tipe of
91 Lambda _ _
92 | numArgs > 1 && numArgs < 10 ->
93 func (ref ('A':show numArgs) `call` (x:values))
94 | otherwise -> func (foldl (<|) x values)
95 where
96 ts = T.collectLambdas tipe
97 numArgs = length ts - 1
98 args = map (\n -> '_' : show n) [0..]
99 values = zipWith inc (init ts) (map ref args)
100 func body = function (take numArgs args)
101 [ VarDeclStmt () [VarDecl () (var "_r") (Just body)]
102 , ret (out (last ts) (ref "_r"))
103 ]
105 Var _ -> error "type variables should not be allowed through input ports"
106 Data ctor []
107 | ctor `elem` ["Int","Float","Bool","String"] -> obj ("_J.from" ++ ctor) <| x
108 | ctor `elem` jsPrims -> x
109 where
110 jsPrims = map ("JavaScript.JS"++) ["Number","Bool","String"]
112 Data ctor [t]
113 | ctor == "Maybe.Maybe" ->
114 CondExpr () (equal (DotRef () x (var "ctor")) (string "Nothing"))
115 (NullLit ())
116 (out t (DotRef () x (var "_0")))
118 | ctor == "_List" ->
119 DotRef () (obj "_J.fromList" <| x) (var "map") <| outgoing t
121 Data ctor ts
122 | Help.isTuple ctor ->
123 let convert n t = out t $ DotRef () x $ var ('_':show n)
124 in ArrayLit () $ zipWith convert [0..] ts
126 Data _ _ ->
127 error "bad ADT got to port generation code"
129 Record _ (Just _) ->
130 error "bad record got to port generation code"
132 Record fields Nothing ->
133 ObjectLit () keys
134 where
135 keys = map convert fields
136 convert (f,t) = (PropId () (var f), out t (DotRef () x (var f)))