git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Don't clobber global scope

Also generated JS has `use strict` directive, and fixed a missing `var` when printing `for` statements.

commitf0e4444883e587a4afb60f9af8f2d279c53bb6cf authorGary Burgess <garyb@users.noreply.github.com> dateSun 9 Feb 2014, 13:52 (13 years ago) parent8749c70
3 files changed+14−3
src/Language/PureScript.hs+1−1 src/Language/PureScript/CodeGen/JS.hs+11−1 src/Language/PureScript/Pretty/JS.hs+2−1
src/Language/PureScript.hs+1−1
@@ -67,4 +67,4 @@ compile opts ms = do
6767 Left "Main.main is undefined"
6868 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
6969 | otherwise -> return js
70− return (prettyPrintJS js', exts, env)
70+ return (prettyPrintJS [(wrapExportsContainer js')], exts, env)
src/Language/PureScript/CodeGen/JS.hs+11−1
@@ -18,7 +18,8 @@
1818module Language.PureScript.CodeGen.JS (
1919 module AST,
2020 declToJs,
21− moduleToJs
21+ moduleToJs,
22+ wrapExportsContainer
2223) where
2324
2425import Data.Maybe (fromMaybe, mapMaybe)
@@ -55,6 +56,7 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
5556 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
5657 [JSAssignment (JSVar name)
5758 (JSBinary Or (JSVar name) (JSObjectLiteral []))]
59+ , JSAssignment (JSAccessor name (JSVar "exports")) (JSVar name)
5860 ]
5961
6062-- |
@@ -325,3 +327,11 @@ statementToJs opts m e (If ifst) = ifToJs ifst
325327 elseToJs (Else sts) = JSBlock (map (statementToJs opts m e) sts)
326328 elseToJs (ElseIf elif) = ifToJs elif
327329statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
330+
331+wrapExportsContainer :: [JS] -> JS
332+wrapExportsContainer modules = JSApp (JSFunction Nothing ["exports"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
333+ where exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
334+ (JSAccessor "exports" (JSVar "module"))
335+ (JSConditional (JSBinary NotEqualTo (JSTypeOf $ JSVar "window") (JSStringLiteral "undefined"))
336+ (JSAssignment (JSAccessor "PS" (JSVar "window")) (JSObjectLiteral []))
337+ (JSApp (JSFunction Nothing [] $ JSBlock [JSThrow $ JSStringLiteral "PureScript doesn't know how to export modules in the current environment"]) []))
src/Language/PureScript/Pretty/JS.hs+2−1
@@ -69,6 +69,7 @@ literals = mkPattern' match
6969 , fmap (intercalate ", ") $ forM xs prettyPrintJS'
7070 , return " ]"
7171 ]
72+ match (JSObjectLiteral []) = return "{}"
7273 match (JSObjectLiteral ps) = fmap concat $ sequence
7374 [ return "{\n"
7475 , withIndent $ do
@@ -107,7 +108,7 @@ literals = mkPattern' match
107108 , prettyPrintJS' sts
108109 ]
109110 match (JSFor ident start end sts) = fmap concat $ sequence
110− [ return $ "for (" ++ ident ++ " = "
111+ [ return $ "for (var " ++ ident ++ " = "
111112 , prettyPrintJS' start
112113 , return $ "; " ++ ident ++ " < "
113114 , prettyPrintJS' end