git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Don't clobber global scope
f0e4444Gary Burgess
src/Language/PureScript.hs+1−1
@@ -67,4 +67,4 @@ compile opts ms = do
67 Left "Main.main is undefined"
67 Left "Main.main is undefined"
68 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
68 return $ js ++ [JSApp (JSAccessor "main" (JSVar "Main")) []]
69 | otherwise -> return js
69 | 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 @@
18module Language.PureScript.CodeGen.JS (
18module Language.PureScript.CodeGen.JS (
19 module AST,
19 module AST,
20 declToJs,
20 declToJs,
21− moduleToJs
21+ moduleToJs,
22+ wrapExportsContainer
22) where
23) where
23
24
24import Data.Maybe (fromMaybe, mapMaybe)
25import Data.Maybe (fromMaybe, mapMaybe)
@@ -55,6 +56,7 @@ moduleToJs opts (Module pname@(ProperName name) decls) env =
55 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
56 (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
56 [JSAssignment (JSVar name)
57 [JSAssignment (JSVar name)
57 (JSBinary Or (JSVar name) (JSObjectLiteral []))]
58 (JSBinary Or (JSVar name) (JSObjectLiteral []))]
59+ , JSAssignment (JSAccessor name (JSVar "exports")) (JSVar name)
58 ]
60 ]
59
61
60-- |
62-- |
@@ -325,3 +327,11 @@ statementToJs opts m e (If ifst) = ifToJs ifst
325 elseToJs (Else sts) = JSBlock (map (statementToJs opts m e) sts)
327 elseToJs (Else sts) = JSBlock (map (statementToJs opts m e) sts)
326 elseToJs (ElseIf elif) = ifToJs elif
328 elseToJs (ElseIf elif) = ifToJs elif
327statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
329statementToJs 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
69 , fmap (intercalate ", ") $ forM xs prettyPrintJS'
69 , fmap (intercalate ", ") $ forM xs prettyPrintJS'
70 , return " ]"
70 , return " ]"
71 ]
71 ]
72+ match (JSObjectLiteral []) = return "{}"
72 match (JSObjectLiteral ps) = fmap concat $ sequence
73 match (JSObjectLiteral ps) = fmap concat $ sequence
73 [ return "{\n"
74 [ return "{\n"
74 , withIndent $ do
75 , withIndent $ do
@@ -107,7 +108,7 @@ literals = mkPattern' match
107 , prettyPrintJS' sts
108 , prettyPrintJS' sts
108 ]
109 ]
109 match (JSFor ident start end sts) = fmap concat $ sequence
110 match (JSFor ident start end sts) = fmap concat $ sequence
110− [ return $ "for (" ++ ident ++ " = "
111+ [ return $ "for (var " ++ ident ++ " = "
111 , prettyPrintJS' start
112 , prettyPrintJS' start
112 , return $ "; " ++ ident ++ " < "
113 , return $ "; " ++ ident ++ " < "
113 , prettyPrintJS' end
114 , prettyPrintJS' end