@@ -49,28 +49,29 @@ import qualified Language.PureScript.Constants as C
4949-- module.
5050--
5151moduleToJs :: Options -> Module -> Environment -> Maybe JS
52−moduleToJs opts (Module name decls _) env =
52+moduleToJs opts (Module name decls (Just exps)) env =
5353 case jsDecls of
5454 [] -> Nothing
5555 _ -> Just $ JSAssignment (JSAccessor (moduleNameToJs name) (JSVar C._ps)) $
5656 JSApp (JSFunction Nothing ["module"] (JSBlock $ jsDecls ++ [JSReturn $ JSVar "module"]))
5757 [JSBinary Or (JSAccessor (moduleNameToJs name) (JSVar C._ps)) (JSObjectLiteral [])]
5858 where
59− jsDecls = concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts name decl env) decls
59+ jsDecls = (concat $ mapMaybe (\decl -> fmap (map $ optimize opts) $ declToJs opts name decl env) decls)
60+ ++ concatMap exportToJs exps
61+moduleToJs _ _ _ = error "Exports should have been elaborated in name desugaring"
6062
6163-- |
6264-- Generate code in the simplified Javascript intermediate representation for a declaration
6365--
6466declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
6567declToJs opts mp (ValueDeclaration ident _ _ _ val) e =
66− Just $ export ident $ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
68+ Just [JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))]
6769declToJs opts mp (BindingGroupDeclaration vals) e =
68− Just $ concatMap (\(ident, _, val) ->
69− export ident $ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
70− ) vals
70+ Just $ flip concatMap vals $ \(ident, _, val) ->
71+ [JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))]
7172declToJs _ mp (DataDeclaration _ _ ctors) _ =
7273 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), tys) ->
73− export (Escaped ctor) $ JSVariableIntroduction ctor (Just (go pn 0 tys []))
74+ [JSVariableIntroduction ctor (Just (go pn 0 tys []))]
7475 where
7576 go :: ProperName -> Integer -> [Type] -> [JS] -> JS
7677 go pn _ [] values =
@@ -78,18 +79,26 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
7879 go pn index (_ : tys') values =
7980 JSFunction Nothing ["value" ++ show index]
8081 (JSBlock [JSReturn (go pn (index + 1) tys' (JSVar ("value" ++ show index) : values))])
81−declToJs opts mp (DataBindingGroupDeclaration ds) e =
82− Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
83−declToJs _ _ (ExternDeclaration _ ident (Just js) _) _ =
84− Just $ export ident js
82+declToJs opts mp (DataBindingGroupDeclaration ds) e = Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
83+declToJs _ _ (ExternDeclaration _ _ (Just js) _) _ = Just [js]
8584declToJs _ _ _ _ = Nothing
8685
8786-- |
88−-- Generate code in the simplified Javascript intermediate representation for exporting a
89−-- declaration from a module.
87+-- Generate code in the simplified Javascript intermediate representation for an export from a
88+-- module.
89+--
90+exportToJs :: DeclarationRef -> [JS]
91+exportToJs (TypeRef _ (Just dctors)) = flip map dctors (export . Escaped . runProperName)
92+exportToJs (ValueRef name) = [export name]
93+exportToJs (TypeInstanceRef name _ _) = [export name]
94+exportToJs _ = []
95+
96+-- |
97+-- Generate code in the simplified Javascript intermediate representation for assigning an exported
98+-- value to the current module object.
9099--
91−export :: Ident -> JS -> [JS]
92−export ident value = [ value, JSAssignment (accessor ident (JSVar "module")) (var ident) ]
100+export :: Ident -> JS
101+export ident = JSAssignment (accessor ident (JSVar "module")) (var ident)
93102
94103-- |
95104-- Generate code in the simplified Javascript intermediate representation for a variable based on a