on master help sign in
Get ports working, improve ports error messages
e8322d7Evan Czaplicki
compiler/Generate/JavaScript.hs+4−3
@@ -33,7 +33,8 @@ internalImports :: Module.Name -> [VarDecl ()]
33internalImports name =
33internalImports name =
34 [ varDecl "_N" (obj ["Elm","Native"])
34 [ varDecl "_N" (obj ["Elm","Native"])
35 , include "_U" "Utils"
35 , include "_U" "Utils"
36− , include "_L" "List"
36+ , include "_L" "List"
37+ , include "_P" "Ports"
37 , varDecl Help.localModuleName (string (Module.nameToString name))
38 , varDecl Help.localModuleName (string (Module.nameToString name))
38 ]
39 ]
39 where
40 where
@@ -188,12 +189,12 @@ expression (A region expr) =
188 return $ ObjectLit () [(PropString () "src", literal (Str src))]
189 return $ ObjectLit () [(PropString () "src", literal (Str src))]
189
190
190 PortIn name tipe ->
191 PortIn name tipe ->
191− return $ Var.value ["Native","Ports"] "portIn" `call`
192+ return $ obj ["_P","portIn"] `call`
192 [ string name, Port.incoming tipe ]
193 [ string name, Port.incoming tipe ]
193
194
194 PortOut name tipe value ->
195 PortOut name tipe value ->
195 do value' <- expression value
196 do value' <- expression value
196− return $ Var.value ["Native","Ports"] "portOut" `call`
197+ return $ obj ["_P","portOut"] `call`
197 [ string name, Port.outgoing tipe, value' ]
198 [ string name, Port.outgoing tipe, value' ]
198
199
199definition :: Canonical.Def -> State Int [Statement ()]
200definition :: Canonical.Def -> State Int [Statement ()]
compiler/Generate/JavaScript/Ports.hs+29−7
@@ -1,21 +1,42 @@
1module Generate.JavaScript.Ports (incoming, outgoing) where
1module Generate.JavaScript.Ports (incoming, outgoing) where
2
2
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
8
9
10+
11+data JSType
12+ = JSNumber
13+ | JSBoolean
14+ | JSString
15+ | JSArray
9−data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
16+ | JSObject [String]
10− deriving Show
17+
18+
19+typeToString :: JSType -> String
20+typeToString tipe =
21+ case tipe of
22+ JSNumber -> "a number"
23+ JSBoolean -> "a boolean (true or false)"
24+ JSString -> "a string"
25+ JSArray -> "an array"
26+ JSObject fields ->
27+ "an object with fields " ++ List.intercalate ", " fields
28+
11
29
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
15 where
33 where
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 ]
39+
19 checks =
40 checks =
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)]
26
47
48+
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
31
53
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
34
56
35 _ -> ["v"] ==> inc tipe (ref "v")
57 _ -> ["v"] ==> inc tipe (ref "v")
36
58
@@ -106,7 +128,7 @@ outgoing tipe =
106 Aliased _ t -> outgoing t
128 Aliased _ t -> outgoing t
107
129
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
110
132
111 _ -> ["v"] ==> out tipe (ref "v")
133 _ -> ["v"] ==> out tipe (ref "v")
112
134
compiler/Metadata/Prelude.hs−2
@@ -18,8 +18,6 @@ prelude :: ImportDict
18prelude =
18prelude =
19 Map.fromList $ map (\name -> (name, ([], Var.openListing))) $
19 Map.fromList $ map (\name -> (name, ([], Var.openListing))) $
20 [ -- ["Basics"]
20 [ -- ["Basics"]
21− ["Native","Ports"]
22− , ["Native","Json"]
23 ]
21 ]
24
22
25
23