@@ -18,7 +18,8 @@
18module Language.PureScript.CodeGen.JS (
18module Language.PureScript.CodeGen.JS (
19 module AST,
19 module AST,
21− moduleToJs
21+ moduleToJs,
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)
@@ -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)
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"]) []))