on master help sign in
Generate valid ports code
commit0f3f4097fb4f679d0cae1d6f3905fb9a4b8db1e7 authorEvan Czaplicki <info@elm-lang.org> dateTue 10 Jun 2014, 13:38 (12 years ago) parent34b72a2
2 files changed+13−6
compiler/Generate/JavaScript.hs+3−2 compiler/Generate/JavaScript/Ports.hs+10−4
compiler/Generate/JavaScript.hs+3−2
@@ -37,6 +37,7 @@ internalImports name =
3737 , include "_L" "List"
3838 , include "_A" "Array"
3939 , include "_E" "Error"
40+ , include "_P" "Ports"
4041 , varDecl Help.localModuleName (string name)
4142 ]
4243 where
@@ -192,11 +193,11 @@ expression (A region expr) =
192193 return $ ObjectLit () [(PropString () "src", literal (Str src))]
193194
194195 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 ]
196197
197198 PortOut name tipe value ->
198199 do value' <- expression value
199− return $ obj ["Native","Ports","portOut"] `call`
200+ return $ obj ["_P","portOut"] `call`
200201 [ string name, Port.outgoing tipe, value' ]
201202
202203definition :: Canonical.Def -> State Int [Statement ()]
compiler/Generate/JavaScript/Ports.hs+10−4
@@ -5,6 +5,7 @@ import Generate.JavaScript.Helpers
55import AST.Type as T
66import qualified AST.Variable as Var
77import Language.ECMAScript3.Syntax
8+import Generate.JavaScript.Variable (canonical)
89
910data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
1011 deriving Show
@@ -29,7 +30,7 @@ incoming tipe =
2930 Aliased _ t -> incoming t
3031
3132 App (Type v) [t]
32− | Var.isSignal v -> obj ["Native","Ports","incomingSignal"] <| incoming t
33+ | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
3334
3435 _ -> ["v"] ==> inc tipe (ref "v")
3536
@@ -50,8 +51,11 @@ inc tipe x =
5051 from checks = check x checks x
5152
5253 Type name
53− | Var.isJson name -> obj ["Native","Json","fromJS"] <| x
54+ | Var.isJson name -> canonical fromJS <| x
5455 | otherwise -> error "bad type got to incoming port generation code"
56+ where
57+ fromJS = Var.Canonical (Var.Module "Native.Json") "fromJS"
58+
5559
5660 App f args ->
5761 case f : args of
@@ -96,7 +100,7 @@ outgoing tipe =
96100 Aliased _ t -> outgoing t
97101
98102 App (Type v) [t]
99− | Var.isSignal v -> obj ["Native","Ports","outgoingSignal"] <| outgoing t
103+ | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
100104
101105 _ -> ["v"] ==> out tipe (ref "v")
102106
@@ -125,8 +129,10 @@ out tipe x =
125129 | name `elem` ["Int","Float","Bool","String"] -> x
126130
127131 Type name
128− | Var.isJson name -> obj ["Native","Json","toJS"] <| x
132+ | Var.isJson name -> canonical toJS <| x
129133 | otherwise -> error "bad type got to outgoing port generation code"
134+ where
135+ toJS = Var.Canonical (Var.Module "Native.Json") "toJS"
130136
131137 App f args ->
132138 case f : args of