@@ -51,31 +51,30 @@ import Language.PureScript.TypeChecker.Monad (canonicalizeDataConstructor)
51-- Generate code in the simplified Javascript intermediate representation for all declarations in a
51-- Generate code in the simplified Javascript intermediate representation for all declarations in a
54−moduleToJs :: Options -> Module -> Environment -> [JS]
54+moduleToJs :: Options -> Module -> Environment -> JS
55moduleToJs opts (Module pname@(ProperName name) decls) env =
55moduleToJs opts (Module pname@(ProperName name) decls) env =
56− [ JSVariableIntroduction name Nothing
57− , JSApp (JSFunction Nothing [name]
56+ JSAssignment (JSAccessor name (JSVar "_ps")) $ JSApp (JSFunction Nothing ["module"]
57+ (JSBlock $ jsDecls ++ [JSReturn $ JSVar "module"]))
58− (JSBlock (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))))
60+ jsDecls = (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts (ModuleName pname) decl env) (decls))
59− [JSAssignment (JSVar name)
60− (JSBinary Or (JSVar name) (JSObjectLiteral []))]
61− , JSAssignment (JSAccessor name (JSVar "exports")) (JSVar name)
65-- Generate code in the simplified Javascript intermediate representation for a declaration
63-- Generate code in the simplified Javascript intermediate representation for a declaration
67declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
65declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
68declToJs opts mp (ValueDeclaration ident _ _ val) e =
66declToJs opts mp (ValueDeclaration ident _ _ val) e =
69− Just $ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
67+ Just $ [ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
70− : setProperty ident (var ident) mp
68+ , setExportProperty ident (var ident) ]
71declToJs opts mp (BindingGroupDeclaration vals) e =
69declToJs opts mp (BindingGroupDeclaration vals) e =
72 Just $ concatMap (\(ident, val) ->
70 Just $ concatMap (\(ident, val) ->
73− JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
71+ [ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
74− : setProperty ident (var ident) mp
72+ , setExportProperty ident (var ident) ]
76declToJs _ mp (DataDeclaration _ _ ctors) _ =
74declToJs _ mp (DataDeclaration _ _ ctors) _ =
77 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), tys) ->
75 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), tys) ->
78− JSVariableIntroduction ctor (Just (go pn 0 tys [])) : setProperty (Escaped ctor) (JSVar ctor) mp
76+ [ JSVariableIntroduction ctor (Just (go pn 0 tys []))
77+ , setExportProperty (Escaped ctor) (JSVar ctor) ]
80 go pn _ [] values =
79 go pn _ [] values =
81 JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn))), ("values", JSArrayLiteral $ reverse values) ]
80 JSObjectLiteral [ ("ctor", JSStringLiteral (show (Qualified (Just mp) pn))), ("values", JSArrayLiteral $ reverse values) ]
@@ -85,19 +84,15 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
85declToJs opts mp (DataBindingGroupDeclaration ds) e =
84declToJs opts mp (DataBindingGroupDeclaration ds) e =
86 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
85 Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
87declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
86declToJs _ mp (ExternDeclaration importTy ident (Just js) _) _ =
88− Just $ js : setProperty ident (var ident) mp
87+ Just $ [js, setExportProperty ident (var ident)]
89declToJs _ _ _ _ = Nothing
88declToJs _ _ _ _ = Nothing
92−-- Generate code in the simplified Javascript intermediate representation for setting the property
91+-- Generate code in the simplified Javascript intermediate representation for exporting a
93−-- of an object.
92+-- declaration from a module.
95−setProperty :: Ident -> JS -> ModuleName -> [JS]
94+setExportProperty :: Ident -> JS -> JS
96−setProperty ident@(Op op) val (ModuleName (ProperName moduleName)) =
97− [ JSAssignment (accessor ident (JSVar moduleName)) val
98− , JSAssignment (JSIndexer (JSStringLiteral op) (JSVar moduleName)) (accessor ident (JSVar moduleName)) ]
99−setProperty ident val (ModuleName (ProperName moduleName)) =
100− [ JSAssignment (accessor ident (JSVar moduleName)) val ]
95+setExportProperty ident val = JSAssignment (accessor ident (JSVar "module")) val
103-- Generate code in the simplified Javascript intermediate representation for a variable based on a
98-- Generate code in the simplified Javascript intermediate representation for a variable based on a
@@ -113,6 +108,7 @@ var = JSVar . identToJs
114accessor :: Ident -> JS -> JS
109accessor :: Ident -> JS -> JS
115accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
110accessor (Ident name) | nameIsJsReserved name = JSIndexer (JSStringLiteral name)
111+accessor (Op op) = JSIndexer (JSStringLiteral op)
116accessor ident = JSAccessor (identToJs ident)
112accessor ident = JSAccessor (identToJs ident)
@@ -127,9 +123,9 @@ valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral (map (second (valueToJs
127valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
123valueToJs opts m e (ObjectUpdate o ps) = JSApp (JSAccessor "extend" (JSVar "Object")) [ valueToJs opts m e o, JSObjectLiteral (map (second (valueToJs opts m e)) ps)]
128valueToJs _ m e (Constructor (Qualified Nothing name)) =
124valueToJs _ m e (Constructor (Qualified Nothing name)) =
129 case M.lookup (m, name) (dataConstructors e) of
125 case M.lookup (m, name) (dataConstructors e) of
130− Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS id (Qualified (Just aliasModule) aliasIdent)
126+ Just (_, Alias aliasModule aliasIdent) -> qualifiedToJS m id (Qualified (Just aliasModule) aliasIdent)
131 _ -> JSVar . runProperName $ name
127 _ -> JSVar . runProperName $ name
132−valueToJs _ _ _ (Constructor name) = qualifiedToJS (Ident . runProperName) name
128+valueToJs _ m _ (Constructor name) = qualifiedToJS m (Ident . runProperName) name
133valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
129valueToJs opts m e (Block sts) = JSApp (JSFunction Nothing [] (JSBlock (map (statementToJs opts m e) sts))) []
134valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
130valueToJs opts m e (Case values binders) = bindersToJs opts m e binders (map (valueToJs opts m e) values)
135valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
131valueToJs opts m e (IfThenElse cond th el) = JSConditional (valueToJs opts m e cond) (valueToJs opts m e th) (valueToJs opts m e el)
@@ -202,7 +198,7 @@ varToJs m e qual@(Qualified _ ident) = go qual
202 Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
198 Just (_, Alias aliasModule aliasIdent) -> go (Qualified (Just aliasModule) aliasIdent)
203 _ -> case qual of
199 _ -> case qual of
204 Qualified Nothing _ -> var ident
200 Qualified Nothing _ -> var ident
205− _ -> qualifiedToJS id qual
201+ _ -> qualifiedToJS m id qual
206 isExtern (Extern ForeignImport) = True
202 isExtern (Extern ForeignImport) = True
207 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
203 isExtern (Alias m' ident') = case M.lookup (m', ident') (names e) of
208 Just (_, ty') -> isExtern ty'
204 Just (_, ty') -> isExtern ty'
@@ -213,9 +209,9 @@ varToJs m e qual@(Qualified _ ident) = go qual
213-- Generate code in the simplified Javascript intermediate representation for a reference to a
209-- Generate code in the simplified Javascript intermediate representation for a reference to a
214-- variable that may have a qualified name.
210-- variable that may have a qualified name.
216−qualifiedToJS :: (a -> Ident) -> Qualified a -> JS
212+qualifiedToJS :: ModuleName -> (a -> Ident) -> Qualified a -> JS
217−qualifiedToJS f (Qualified (Just (ModuleName (ProperName m))) a) = accessor (f a) (JSVar m)
213+qualifiedToJS m f (Qualified (Just m'@(ModuleName (ProperName mn))) a) | m /= m' = accessor (f a) (JSAccessor mn $ JSVar "_ps")
218−qualifiedToJS f (Qualified Nothing a) = JSVar $ identToJs (f a)
214+qualifiedToJS m f (Qualified _ a) = JSVar $ identToJs (f a)
221-- Generate code in the simplified Javascript intermediate representation for pattern match binders
217-- Generate code in the simplified Javascript intermediate representation for pattern match binders
@@ -347,7 +343,7 @@ statementToJs opts m e (If ifst) = ifToJs ifst
347statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
343statementToJs opts m e (Return value) = JSReturn (valueToJs opts m e value)
349wrapExportsContainer :: [JS] -> JS
345wrapExportsContainer :: [JS] -> JS
350−wrapExportsContainer modules = JSApp (JSFunction Nothing ["exports"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
346+wrapExportsContainer modules = JSApp (JSFunction Nothing ["_ps"] $ JSBlock $ (JSStringLiteral "use strict") : modules) [exportSelector]
351 where exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
347 where exportSelector = JSConditional (JSBinary And (JSBinary NotEqualTo (JSTypeOf $ JSVar "module") (JSStringLiteral "undefined")) (JSAccessor "exports" (JSVar "module")))
352 (JSAccessor "exports" (JSVar "module"))
348 (JSAccessor "exports" (JSVar "module"))
353 (JSConditional (JSBinary NotEqualTo (JSTypeOf $ JSVar "window") (JSStringLiteral "undefined"))
349 (JSConditional (JSBinary NotEqualTo (JSTypeOf $ JSVar "window") (JSStringLiteral "undefined"))