on master help sign in
Generate valid ports code
0f3f409Evan Czaplicki
compiler/Generate/JavaScript.hs+3−2
@@ -37,6 +37,7 @@ internalImports name =
37 , include "_L" "List"
37 , include "_L" "List"
38 , include "_A" "Array"
38 , include "_A" "Array"
39 , include "_E" "Error"
39 , include "_E" "Error"
40+ , include "_P" "Ports"
40 , varDecl Help.localModuleName (string name)
41 , varDecl Help.localModuleName (string name)
41 ]
42 ]
42 where
43 where
@@ -192,11 +193,11 @@ expression (A region expr) =
192 return $ ObjectLit () [(PropString () "src", literal (Str src))]
193 return $ ObjectLit () [(PropString () "src", literal (Str src))]
193
194
194 PortIn name tipe ->
195 PortIn name tipe ->
195− return $ obj ["Native","Ports","portIn"] `call` [ string name, Port.incoming tipe ]
196+ return $ obj ["_P","portIn"] `call` [ string name, Port.incoming tipe ]
196
197
197 PortOut name tipe value ->
198 PortOut name tipe value ->
198 do value' <- expression value
199 do value' <- expression value
199− return $ obj ["Native","Ports","portOut"] `call`
200+ return $ obj ["_P","portOut"] `call`
200 [ string name, Port.outgoing tipe, value' ]
201 [ string name, Port.outgoing tipe, value' ]
201
202
202definition :: Canonical.Def -> State Int [Statement ()]
203definition :: Canonical.Def -> State Int [Statement ()]
compiler/Generate/JavaScript/Ports.hs+10−4
@@ -5,6 +5,7 @@ import Generate.JavaScript.Helpers
5import AST.Type as T
5import AST.Type as T
6import qualified AST.Variable as Var
6import qualified AST.Variable as Var
7import Language.ECMAScript3.Syntax
7import Language.ECMAScript3.Syntax
8+import Generate.JavaScript.Variable (canonical)
8
9
9data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
10data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
10 deriving Show
11 deriving Show
@@ -29,7 +30,7 @@ incoming tipe =
29 Aliased _ t -> incoming t
30 Aliased _ t -> incoming t
30
31
31 App (Type v) [t]
32 App (Type v) [t]
32− | Var.isSignal v -> obj ["Native","Ports","incomingSignal"] <| incoming t
33+ | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
33
34
34 _ -> ["v"] ==> inc tipe (ref "v")
35 _ -> ["v"] ==> inc tipe (ref "v")
35
36
@@ -50,8 +51,11 @@ inc tipe x =
50 from checks = check x checks x
51 from checks = check x checks x
51
52
52 Type name
53 Type name
53− | Var.isJson name -> obj ["Native","Json","fromJS"] <| x
54+ | Var.isJson name -> canonical fromJS <| x
54 | otherwise -> error "bad type got to incoming port generation code"
55 | otherwise -> error "bad type got to incoming port generation code"
56+ where
57+ fromJS = Var.Canonical (Var.Module "Native.Json") "fromJS"
58+
55
59
56 App f args ->
60 App f args ->
57 case f : args of
61 case f : args of
@@ -96,7 +100,7 @@ outgoing tipe =
96 Aliased _ t -> outgoing t
100 Aliased _ t -> outgoing t
97
101
98 App (Type v) [t]
102 App (Type v) [t]
99− | Var.isSignal v -> obj ["Native","Ports","outgoingSignal"] <| outgoing t
103+ | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
100
104
101 _ -> ["v"] ==> out tipe (ref "v")
105 _ -> ["v"] ==> out tipe (ref "v")
102
106
@@ -125,8 +129,10 @@ out tipe x =
125 | name `elem` ["Int","Float","Bool","String"] -> x
129 | name `elem` ["Int","Float","Bool","String"] -> x
126
130
127 Type name
131 Type name
128− | Var.isJson name -> obj ["Native","Json","toJS"] <| x
132+ | Var.isJson name -> canonical toJS <| x
129 | otherwise -> error "bad type got to outgoing port generation code"
133 | otherwise -> error "bad type got to outgoing port generation code"
134+ where
135+ toJS = Var.Canonical (Var.Module "Native.Json") "toJS"
130
136
131 App f args ->
137 App f args ->
132 case f : args of
138 case f : args of