on master help sign in
Get namespacing right in generated JS. All Elm libraries are now contained in the Elm object, no more libraries leaking out to pollute the global namespace.
d2d456aevancz
elm/src/Gen/CompileToJS.hs+12−11
@@ -35,15 +35,15 @@ mainEquals s = globalAssign "Elm.main" (jsFunc "" (ret s))
35globalAssign m s = "\n" ++ m ++ "=" ++ s ++ ";"
35globalAssign m s = "\n" ++ m ++ "=" ++ s ++ ";"
36
36
37tryBlock escapees names e =
37tryBlock escapees names e =
38− concat [ "\ntry{\n" ++ e ++ "\n} catch (e) {"
38+ concat [ "\n(function() {\ntry{\n" ++ e ++ "\n} catch (e) {"
39− , "Elm.main=function() {"
39+ , "\nElm.main=function() {"
40− , "var msg = ('<br/><h2>Your browser may not be supported. " ++
40+ , "\nvar msg = ('<br/><h2>Your browser may not be supported. " ++
41 "Are you using a modern browser?</h2>' +" ++
41 "Are you using a modern browser?</h2>' +" ++
42 " '<br/><span style=\"color:grey\">Runtime Error in " ++
42 " '<br/><span style=\"color:grey\">Runtime Error in " ++
43 intercalate "." names ++ " module:<br/>' + e + '" ++ msg ++ "</span>');"
43 intercalate "." names ++ " module:<br/>' + e + '" ++ msg ++ "</span>');"
44− , "document.body.innerHTML = Text.monospace(msg);"
44+ , "\ndocument.body.innerHTML = Elm.Text.monospace(msg);"
45 , "throw e;"
45 , "throw e;"
46− , "};}"
46+ , "};}}());"
47 ]
47 ]
48 where msg | escapees /= [] = concat [ "<br/><br/>The problem may stem from an improper usage of:<br/>"
48 where msg | escapees /= [] = concat [ "<br/><br/>The problem may stem from an improper usage of:<br/>"
49 , intercalate ", " $ map (concatMap escape) escapees ]
49 , intercalate ", " $ map (concatMap escape) escapees ]
@@ -55,7 +55,7 @@ tryBlock escapees names e =
55
55
56jsModule (escapees, Module names exports imports stmts) =
56jsModule (escapees, Module names exports imports stmts) =
57 tryBlock escapees (tail modNames) $ concat
57 tryBlock escapees (tail modNames) $ concat
58− [ "\nfor(var i in Elm) { this[i] = Elm[i]; }"
58+ [ "\nfor(this['i'] in Elm) { eval('var ' + this['i'] + '=Elm[this.i];'); }"
59 , concatMap (\n -> globalAssign n $ n ++ " || {}") .
59 , concatMap (\n -> globalAssign n $ n ++ " || {}") .
60 map (intercalate ".") . drop 2 . inits $
60 map (intercalate ".") . drop 2 . inits $
61 take (length modNames - 1) modNames
61 take (length modNames - 1) modNames
@@ -93,10 +93,11 @@ jsImport' (modul, As name) = assign name modul
93jsImport' (modul, Importing vs) =
93jsImport' (modul, Importing vs) =
94 concatMap (\v -> assign v $ modul ++ "." ++ v) vs
94 concatMap (\v -> assign v $ modul ++ "." ++ v) vs
95jsImport' (modul, Hiding vs) =
95jsImport' (modul, Hiding vs) =
96− concat [ assign "hiddenVars" . jsList $ map (\v -> "'" ++ v ++ "'") vs
96+ concat [ assign "hiddenVars" . ("{"++) . (++"}") . intercalate "," $
97+ map (\v -> v ++ ":true") vs
97− , "\nfor(var i in " ++ modul ++ "){"
98+ , "\nfor(this['i'] in " ++ modul ++ "){"
98− , indent $ "\nif (hiddenVars.indexOf(i) >= 0) continue;"
99+ , indent $ "\nif (hiddenVars[i]) continue;"
99− , indent $ globalAssign "this[i]" $ modul ++ "[i]"
100+ , indent $ "\neval('var ' + this['i'] + ' = " ++ modul ++ "[this.i];');"
100 , "}" ]
101 , "}" ]
101
102
102stmtsToJS :: [Statement] -> String
103stmtsToJS :: [Statement] -> String
@@ -201,7 +202,7 @@ jsNil = "[\"Nil\"]"
201jsCons e1 e2 = jsList [ show "Cons", e1, e2 ]
202jsCons e1 e2 = jsList [ show "Cons", e1, e2 ]
202jsRange e1 e2 = (++"()") . jsFunc "" $
203jsRange e1 e2 = (++"()") . jsFunc "" $
203 assign "lo" e1 ++ assign "hi" e2 ++ assign "lst" jsNil ++
204 assign "lo" e1 ++ assign "hi" e2 ++ assign "lst" jsNil ++
204− "if(lo<=hi){do{" ++ assign "lst" (jsCons "hi" "lst") ++ "}while(hi-->lo)}" ++
205+ "if(lo<=hi){do{lst=" ++ (jsCons "hi" "lst") ++ "}while(hi-->lo)}" ++
205 ret "lst"
206 ret "lst"
206
207
207binop (o:p) e1 e2
208binop (o:p) e1 e2