@@ -1,21 +1,42 @@
1module Generate.JavaScript.Ports (incoming, outgoing) where
1module Generate.JavaScript.Ports (incoming, outgoing) where
3+import qualified Data.List as List
3import Generate.JavaScript.Helpers
4import Generate.JavaScript.Helpers
4import qualified Generate.JavaScript.Variable as V
5import qualified Generate.JavaScript.Variable as V
5import AST.Type as T
6import AST.Type as T
6import qualified AST.Variable as Var
7import qualified AST.Variable as Var
7import Language.ECMAScript3.Syntax
8import Language.ECMAScript3.Syntax
9−data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
16+ | JSObject [String]
19+typeToString :: JSType -> String
22+ JSNumber -> "a number"
23+ JSBoolean -> "a boolean (true or false)"
24+ JSString -> "a string"
25+ JSArray -> "an array"
27+ "an object with fields " ++ List.intercalate ", " fields
12check :: Expression () -> JSType -> Expression () -> Expression ()
30check :: Expression () -> JSType -> Expression () -> Expression ()
13check x jsType continue =
31check x jsType continue =
14 CondExpr () (jsFold OpLOr checks x) continue throw
32 CondExpr () (jsFold OpLOr checks x) continue throw
34+ jsFold op checks value =
16− jsFold op checks value = foldl1 (InfixExpr () op) (map ($value) checks)
35+ foldl1 (InfixExpr () op) (map ($ value) checks)
17− throw = obj ["_E","raise"] <| InfixExpr () OpAdd msg x
36+
18− msg = string ("invalid input, expecting " ++ show jsType ++ " but got ")
37+ throw =
38+ obj ["_U","badPort"] `call` [ string (typeToString jsType), x ]
20 case jsType of
41 case jsType of
21 JSNumber -> [typeof "number"]
42 JSNumber -> [typeof "number"]
@@ -24,13 +45,14 @@ check x jsType continue =
24 JSArray -> [instanceof "Array"]
45 JSArray -> [instanceof "Array"]
25 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
46 JSObject fields -> [jsFold OpLAnd (typeof "object" : map member fields)]
27incoming :: CanonicalType -> Expression ()
49incoming :: CanonicalType -> Expression ()
28incoming tipe =
50incoming tipe =
29 case tipe of
51 case tipe of
30 Aliased _ t -> incoming t
52 Aliased _ t -> incoming t
32 App (Type v) [t]
54 App (Type v) [t]
33− | Var.isSignal v -> V.value ["Native","Ports"] "incomingSignal" <| incoming t
55+ | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
35 _ -> ["v"] ==> inc tipe (ref "v")
57 _ -> ["v"] ==> inc tipe (ref "v")
@@ -106,7 +128,7 @@ outgoing tipe =
106 Aliased _ t -> outgoing t
128 Aliased _ t -> outgoing t
108 App (Type v) [t]
130 App (Type v) [t]
109− | Var.isSignal v -> V.value ["Native","Ports"] "outgoingSignal" <| outgoing t
131+ | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
111 _ -> ["v"] ==> out tipe (ref "v")
133 _ -> ["v"] ==> out tipe (ref "v")