git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix for modules with names that overlap globals
f110f31Gary Burgess
examples/passing/FunctionScope.purs+2−2
@@ -1,4 +1,4 @@
1−module RecordScope where
1+module FunctionScope where
2
2
3 import Prelude
3 import Prelude
4
4
@@ -8,7 +8,7 @@ module RecordScope where
8module Main where
8module Main where
9
9
10 import Prelude
10 import Prelude
11− import RecordScope
11+ import FunctionScope
12
12
13 main = do
13 main = do
14 let value = mkValue 1
14 let value = mkValue 1
examples/passing/Math.pursnew file+7
@@ -0,0 +1,7 @@
1+module Main where
2+
3+ import Prelude
4+ import Math
5+
6+ main = Trace.print $ sin 1
7+
src/Language/PureScript.hs+2−2
@@ -59,12 +59,12 @@ compile opts ms = do
59 modify (\s -> s { checkCurrentModule = Just (ModuleName moduleName) })
59 modify (\s -> s { checkCurrentModule = Just (ModuleName moduleName) })
60 Module moduleName <$> typeCheckAll (ModuleName moduleName) decls
60 Module moduleName <$> typeCheckAll (ModuleName moduleName) decls
61 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
61 regrouped <- createBindingGroupsModule . collapseBindingGroupsModule $ elaborated
62− let js = concatMap (flip (moduleToJs opts) env) $ regrouped
62+ let js = map (flip (moduleToJs opts) env) $ regrouped
63 let exts = intercalate "\n" . map (flip moduleToPs env) $ regrouped
63 let exts = intercalate "\n" . map (flip moduleToPs env) $ regrouped
64 js' <- case () of
64 js' <- case () of
65 _ | optionsRunMain opts -> do
65 _ | optionsRunMain opts -> do
66 when ((ModuleName (ProperName "Main"), Ident "main") `M.notMember` (names env)) $
66 when ((ModuleName (ProperName "Main"), Ident "main") `M.notMember` (names env)) $
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" (JSAccessor "Main" (JSVar "_ps"))) []]
69 | otherwise -> return js
69 | otherwise -> return js
70 return (prettyPrintJS [(wrapExportsContainer js')], exts, env)
70 return (prettyPrintJS [(wrapExportsContainer js')], exts, env)
src/Language/PureScript/CodeGen/JS.hs+25−29
@@ -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
52-- module.
52-- module.
53--
53--
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+ [JSObjectLiteral []]
59+ where
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)
62− ]
63
61
64-- |
62-- |
65-- Generate code in the simplified Javascript intermediate representation for a declaration
63-- Generate code in the simplified Javascript intermediate representation for a declaration
66--
64--
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) ]
75 ) vals
73 ) vals
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) ]
79 where
78 where
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
90
89
91-- |
90-- |
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.
94--
93--
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
101
96
102-- |
97-- |
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
113--
108--
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)
117
113
118-- |
114-- |
@@ -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.
215--
211--
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)
219
215
220-- |
216-- |
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)
348
344
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"))