on master help sign in
Resolve infinite loops for ports, fix #653
commit8d69600031a08557ba94f46f835d230ba846cc14 authorEvan Czaplicki <info@elm-lang.org> dateWed 11 Jun 2014, 01:04 (12 years ago) parentff9d643
4 files changed+17−14
compiler/Generate/JavaScript.hs+2−3 compiler/Generate/JavaScript/Ports.hs+5−10 compiler/Generate/JavaScript/Variable.hs+8 compiler/Transform/Canonicalize.hs+2−1
compiler/Generate/JavaScript.hs+2−3
@@ -37,7 +37,6 @@ internalImports name =
3737 , include "_L" "List"
3838 , include "_A" "Array"
3939 , include "_E" "Error"
40− , include "_P" "Ports"
4140 , varDecl Help.localModuleName (string name)
4241 ]
4342 where
@@ -193,11 +192,11 @@ expression (A region expr) =
193192 return $ ObjectLit () [(PropString () "src", literal (Str src))]
194193
195194 PortIn name tipe ->
196− return $ obj ["_P","portIn"] `call` [ string name, Port.incoming tipe ]
195+ return $ Var.nativePorts "portIn" `call` [ string name, Port.incoming tipe ]
197196
198197 PortOut name tipe value ->
199198 do value' <- expression value
200− return $ obj ["_P","portOut"] `call`
199+ return $ Var.nativePorts "portOut" `call`
201200 [ string name, Port.outgoing tipe, value' ]
202201
203202definition :: Canonical.Def -> State Int [Statement ()]
compiler/Generate/JavaScript/Ports.hs+5−10
@@ -2,10 +2,10 @@
22module Generate.JavaScript.Ports (incoming, outgoing) where
33
44import Generate.JavaScript.Helpers
5+import Generate.JavaScript.Variable (nativePorts, nativeJson)
56import AST.Type as T
67import qualified AST.Variable as Var
78import Language.ECMAScript3.Syntax
8−import Generate.JavaScript.Variable (canonical)
99
1010data JSType = JSNumber | JSBoolean | JSString | JSArray | JSObject [String]
1111 deriving Show
@@ -30,7 +30,7 @@ incoming tipe =
3030 Aliased _ t -> incoming t
3131
3232 App (Type v) [t]
33− | Var.isSignal v -> obj ["_P","incomingSignal"] <| incoming t
33+ | Var.isSignal v -> nativePorts "incomingSignal" <| incoming t
3434
3535 _ -> ["v"] ==> inc tipe (ref "v")
3636
@@ -51,11 +51,8 @@ inc tipe x =
5151 from checks = check x checks x
5252
5353 Type name
54− | Var.isJson name -> canonical fromJS <| x
54+ | Var.isJson name -> nativeJson "fromJS" <| x
5555 | otherwise -> error "bad type got to incoming port generation code"
56− where
57− fromJS = Var.Canonical (Var.Module "Native.Json") "fromJS"
58−
5956
6057 App f args ->
6158 case f : args of
@@ -100,7 +97,7 @@ outgoing tipe =
10097 Aliased _ t -> outgoing t
10198
10299 App (Type v) [t]
103− | Var.isSignal v -> obj ["_P","outgoingSignal"] <| outgoing t
100+ | Var.isSignal v -> nativePorts "outgoingSignal" <| outgoing t
104101
105102 _ -> ["v"] ==> out tipe (ref "v")
106103
@@ -129,10 +126,8 @@ out tipe x =
129126 | name `elem` ["Int","Float","Bool","String"] -> x
130127
131128 Type name
132− | Var.isJson name -> canonical toJS <| x
129+ | Var.isJson name -> nativeJson "toJS" <| x
133130 | otherwise -> error "bad type got to outgoing port generation code"
134− where
135− toJS = Var.Canonical (Var.Module "Native.Json") "toJS"
136131
137132 App f args ->
138133 case f : args of
compiler/Generate/JavaScript/Variable.hs+8
@@ -30,6 +30,14 @@ varName x = map (swap '\'' '$') x'
3030 where
3131 x' = if Set.member x jsReserveds then '$' : x else x
3232
33+nativeJson :: String -> JS.Expression ()
34+nativeJson value =
35+ canonical (Var.Canonical (Var.Module "Native.Json") value)
36+
37+nativePorts :: String -> JS.Expression ()
38+nativePorts value =
39+ canonical (Var.Canonical (Var.Module "Native.Ports") value)
40+
3341jsReserveds :: Set.Set String
3442jsReserveds = Set.fromList
3543 [ "null", "undefined", "Nan", "Infinity", "true", "false", "eval"
compiler/Transform/Canonicalize.hs+2−1
@@ -38,7 +38,8 @@ module' interfaces modul =
3838 runState (runErrorT (moduleHelp interfaces modul)) Set.empty
3939
4040 filterImports m =
41− let used (name,_) = Set.member name usedModules
41+ let usedModules' = Set.insert "Native.Ports" usedModules
42+ used (name,_) = Set.member name usedModules'
4243 in m { Module.imports = filter used (Module.imports m) }
4344
4445moduleHelp :: Module.Interfaces -> Module.ValidModule