git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Use export list when generating JS
5df930cGary Burgess
src/Language/PureScript/CodeGen/JS.hs+24−15
@@ -49,28 +49,29 @@ import qualified Language.PureScript.Constants as C
49-- module.
49-- module.
50--
50--
51moduleToJs :: Options -> Module -> Environment -> Maybe JS
51moduleToJs :: Options -> Module -> Environment -> Maybe JS
52−moduleToJs opts (Module name decls _) env =
52+moduleToJs opts (Module name decls (Just exps)) env =
53 case jsDecls of
53 case jsDecls of
54 [] -> Nothing
54 [] -> Nothing
55 _ -> Just $ JSAssignment (JSAccessor (moduleNameToJs name) (JSVar C._ps)) $
55 _ -> Just $ JSAssignment (JSAccessor (moduleNameToJs name) (JSVar C._ps)) $
56 JSApp (JSFunction Nothing ["module"] (JSBlock $ jsDecls ++ [JSReturn $ JSVar "module"]))
56 JSApp (JSFunction Nothing ["module"] (JSBlock $ jsDecls ++ [JSReturn $ JSVar "module"]))
57 [JSBinary Or (JSAccessor (moduleNameToJs name) (JSVar C._ps)) (JSObjectLiteral [])]
57 [JSBinary Or (JSAccessor (moduleNameToJs name) (JSVar C._ps)) (JSObjectLiteral [])]
58 where
58 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"
60
62
61-- |
63-- |
62-- Generate code in the simplified Javascript intermediate representation for a declaration
64-- Generate code in the simplified Javascript intermediate representation for a declaration
63--
65--
64declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
66declToJs :: Options -> ModuleName -> Declaration -> Environment -> Maybe [JS]
65declToJs opts mp (ValueDeclaration ident _ _ _ val) e =
67declToJs 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))]
67declToJs opts mp (BindingGroupDeclaration vals) e =
69declToJs opts mp (BindingGroupDeclaration vals) e =
68− Just $ concatMap (\(ident, _, val) ->
70+ Just $ flip concatMap vals $ \(ident, _, val) ->
69− export ident $ JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))
71+ [JSVariableIntroduction (identToJs ident) (Just (valueToJs opts mp e val))]
70− ) vals
71declToJs _ mp (DataDeclaration _ _ ctors) _ =
72declToJs _ mp (DataDeclaration _ _ ctors) _ =
72 Just $ flip concatMap ctors $ \(pn@(ProperName ctor), tys) ->
73 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 []))]
74 where
75 where
75 go :: ProperName -> Integer -> [Type] -> [JS] -> JS
76 go :: ProperName -> Integer -> [Type] -> [JS] -> JS
76 go pn _ [] values =
77 go pn _ [] values =
@@ -78,18 +79,26 @@ declToJs _ mp (DataDeclaration _ _ ctors) _ =
78 go pn index (_ : tys') values =
79 go pn index (_ : tys') values =
79 JSFunction Nothing ["value" ++ show index]
80 JSFunction Nothing ["value" ++ show index]
80 (JSBlock [JSReturn (go pn (index + 1) tys' (JSVar ("value" ++ show index) : values))])
81 (JSBlock [JSReturn (go pn (index + 1) tys' (JSVar ("value" ++ show index) : values))])
81−declToJs opts mp (DataBindingGroupDeclaration ds) e =
82+declToJs opts mp (DataBindingGroupDeclaration ds) e = Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
82− Just $ concat $ mapMaybe (flip (declToJs opts mp) e) ds
83−declToJs _ _ (ExternDeclaration _ ident (Just js) _) _ =
83+declToJs _ _ (ExternDeclaration _ _ (Just js) _) _ = Just [js]
84− Just $ export ident js
85declToJs _ _ _ _ = Nothing
84declToJs _ _ _ _ = Nothing
86
85
87-- |
86-- |
88−-- Generate code in the simplified Javascript intermediate representation for exporting a
87+-- Generate code in the simplified Javascript intermediate representation for an export from a
89−-- declaration from a module.
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.
90--
99--
91−export :: Ident -> JS -> [JS]
100+export :: Ident -> JS
92−export ident value = [ value, JSAssignment (accessor ident (JSVar "module")) (var ident) ]
101+export ident = JSAssignment (accessor ident (JSVar "module")) (var ident)
93
102
94-- |
103-- |
95-- Generate code in the simplified Javascript intermediate representation for a variable based on a
104-- Generate code in the simplified Javascript intermediate representation for a variable based on a