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.
commitd2d456acd441202f49595e0372d52894f7707c3b authorevancz <info@elm-lang.org> dateWed 21 Nov 2012, 18:46 (14 years ago) parent15e4664
1 file changed+12−11
elm/src/Gen/CompileToJS.hs+12−11
elm/src/Gen/CompileToJS.hs+12−11
@@ -35,15 +35,15 @@ mainEquals s = globalAssign "Elm.main" (jsFunc "" (ret s))
3535globalAssign m s = "\n" ++ m ++ "=" ++ s ++ ";"
3636
3737tryBlock escapees names e =
38− concat [ "\ntry{\n" ++ e ++ "\n} catch (e) {"
39− , "Elm.main=function() {"
40− , "var msg = ('<br/><h2>Your browser may not be supported. " ++
38+ concat [ "\n(function() {\ntry{\n" ++ e ++ "\n} catch (e) {"
39+ , "\nElm.main=function() {"
40+ , "\nvar msg = ('<br/><h2>Your browser may not be supported. " ++
4141 "Are you using a modern browser?</h2>' +" ++
4242 " '<br/><span style=\"color:grey\">Runtime Error in " ++
4343 intercalate "." names ++ " module:<br/>' + e + '" ++ msg ++ "</span>');"
44− , "document.body.innerHTML = Text.monospace(msg);"
44+ , "\ndocument.body.innerHTML = Elm.Text.monospace(msg);"
4545 , "throw e;"
46− , "};}"
46+ , "};}}());"
4747 ]
4848 where msg | escapees /= [] = concat [ "<br/><br/>The problem may stem from an improper usage of:<br/>"
4949 , intercalate ", " $ map (concatMap escape) escapees ]
@@ -55,7 +55,7 @@ tryBlock escapees names e =
5555
5656jsModule (escapees, Module names exports imports stmts) =
5757 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];'); }"
5959 , concatMap (\n -> globalAssign n $ n ++ " || {}") .
6060 map (intercalate ".") . drop 2 . inits $
6161 take (length modNames - 1) modNames
@@ -93,10 +93,11 @@ jsImport' (modul, As name) = assign name modul
9393jsImport' (modul, Importing vs) =
9494 concatMap (\v -> assign v $ modul ++ "." ++ v) vs
9595jsImport' (modul, Hiding vs) =
96− concat [ assign "hiddenVars" . jsList $ map (\v -> "'" ++ v ++ "'") vs
97− , "\nfor(var i in " ++ modul ++ "){"
98− , indent $ "\nif (hiddenVars.indexOf(i) >= 0) continue;"
99− , indent $ globalAssign "this[i]" $ modul ++ "[i]"
96+ concat [ assign "hiddenVars" . ("{"++) . (++"}") . intercalate "," $
97+ map (\v -> v ++ ":true") vs
98+ , "\nfor(this['i'] in " ++ modul ++ "){"
99+ , indent $ "\nif (hiddenVars[i]) continue;"
100+ , indent $ "\neval('var ' + this['i'] + ' = " ++ modul ++ "[this.i];');"
100101 , "}" ]
101102
102103stmtsToJS :: [Statement] -> String
@@ -201,7 +202,7 @@ jsNil = "[\"Nil\"]"
201202jsCons e1 e2 = jsList [ show "Cons", e1, e2 ]
202203jsRange e1 e2 = (++"()") . jsFunc "" $
203204 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)}" ++
205206 ret "lst"
206207
207208binop (o:p) e1 e2