@@ -14,6 +14,8 @@ import Initialize
1414import Rename (derename)
1515import Cases
1616import Guid
17+import Parse.Library (isOp)
18+import Rename (deprime)
1719
1820showErr :: String -> String
1921showErr err = mainEquals $ "Elm.Graphics.text(Elm.Text.monospace(" ++ msg ++ "))"
@@ -35,7 +37,7 @@ mainEquals s = globalAssign "Elm.main" (jsFunc "" (ret s))
3537globalAssign m s = "\n" ++ m ++ "=" ++ s ++ ";"
3638
3739tryBlock escapees names e =
38− concat [ "\n(function() {\ntry{\n" ++ e ++ "\n} catch (e) {"
40+ concat [ "\ntry{\n" ++ e ++ "\n} catch (e) {"
3941 , "\nElm.main=function() {"
4042 , "\nvar msg = ('<br/><h2>Your browser may not be supported. " ++
4143 "Are you using a modern browser?</h2>' +" ++
@@ -43,7 +45,7 @@ tryBlock escapees names e =
4345 intercalate "." names ++ " module:<br/>' + e + '" ++ msg ++ "</span>');"
4446 , "\ndocument.body.innerHTML = Elm.Text.monospace(msg);"
4547 , "throw e;"
46− , "};}}());"
48+ , "};}"
4749 ]
4850 where msg | escapees /= [] = concat [ "<br/><br/>The problem may stem from an improper usage of:<br/>"
4951 , intercalate ", " $ map (concatMap escape) escapees ]
@@ -55,14 +57,12 @@ tryBlock escapees names e =
5557
5658jsModule (escapees, Module names exports imports stmts) =
5759 tryBlock escapees (tail modNames) $ concat
58− [ assign "$op" "{}"
59− , "\nfor(this['i'] in Elm){eval('var '+this['i']+'=Elm[this.i];');}"
60− , concatMap (\n -> globalAssign n $ n ++ " || {}") .
60+ [ concatMap (\n -> globalAssign n $ n ++ " || {}") .
6161 map (intercalate ".") . drop 2 . inits $
6262 take (length modNames - 1) modNames
6363 , "\nif (" ++ modName ++ ") throw \"Module name collision, '" ++
6464 intercalate "." (tail modNames) ++ "' is already defined.\"; "
65− , globalAssign modName $ jsFunc "" (includes ++body++ export) ++ "()"
65+ , globalAssign modName $ jsFunc "" (defs ++ includes ++ body ++ export) ++ "()"
6666 , mainEquals $ modName ++ ".main" ]
6767 where modNames = if null names then ["Elm", "Main"]
6868 else "Elm" : names
@@ -71,9 +71,11 @@ jsModule (escapees, Module names exports imports stmts) =
7171 body = stmtsToJS stmts
7272 export = getExports exports stmts
7373 exps = if null exports then ["main"] else exports
74+ defs = concat [ assign "$op" "{}"
75+ , "\nfor(Elm['i'] in Elm){eval('var '+Elm['i']+'=Elm[Elm.i];');}" ]
7476
7577getExports names stmts = ret . braces $ intercalate ",\n" (op : map fnPair fns)
76− where exNames n = either id id n `elem` names
78+ where exNames n = either derename id n `elem` names
7779 exports | null names = concatMap get stmts
7880 | otherwise = filter exNames (concatMap get stmts)
7981
@@ -100,16 +102,21 @@ jsImport (modul, how) =
100102 jsImport' (modul, how)
101103
102104jsImport' (modul, As name) = assign name modul
103−jsImport' (modul, Importing vs) =
104− concatMap (\v -> assign v $ modul ++ "." ++ v) vs
105+jsImport' (modul, Importing vs) = concatMap def vs
106+ where def [] = []
107+ def (o:p) | isOp o = let v = "$op['" ++ o:p ++ "']" in
108+ "\n" ++ v ++ " = " ++ modul ++ "." ++ v ++ ";"
109+ | otherwise = let v = deprime (o:p) in
110+ assign v $ modul ++ "." ++ v
111+
105112jsImport' (modul, Hiding vs) =
106113 concat [ assign "hiddenVars" . ("{"++) . (++"}") . intercalate "," $
107− map (\v -> v ++ ":true") vs
108− , "\nfor (this['i'] in " ++ modul ++ ") "
114+ map (\v -> v ++ ":true") (map deprime vs)
115+ , "\nfor (Elm['i'] in " ++ modul ++ ") "
109116 , braces . indent . concat $
110− [ "\nif (hiddenVars[this['i']]) continue;"
111− , "\neval('var ' + this['i'] + ' = "
112− , modul, "[this.i];');" ]
117+ [ "\nif (hiddenVars[Elm['i']]) continue;"
118+ , "\neval('var ' + Elm['i'] + ' = "
119+ , modul, "[Elm.i];');" ]
113120 ]
114121
115122stmtsToJS :: [Statement] -> String