git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #678 from purescript/676
b0ad960Phil Freeman
examples/passing/TypeClassImport.pursnew file+18
@@ -0,0 +1,18 @@
1+module Main where
2+
3+foreign import data T :: *
4+
5+foreign import data C :: *
6+
7+foreign import t "var t = null;" :: T
8+
9+foreign import inst """
10+ var inst = {
11+ show: function(t) {
12+ return 'Done';
13+ }
14+ }""" :: C
15+
16+foreign import instance inst :: Show T
17+
18+main = Debug.Trace.print t
src/Language/PureScript/CodeGen/JS.hs+10−7
@@ -26,8 +26,11 @@ import Data.Maybe (catMaybes)
26import Data.Function (on)
26import Data.Function (on)
27import Data.List (nub, (\\), delete, sortBy)
27import Data.List (nub, (\\), delete, sortBy)
28
28
29+import qualified Data.Map as M
30+
29import Control.Monad (foldM, replicateM, forM)
31import Control.Monad (foldM, replicateM, forM)
30import Control.Applicative
32import Control.Applicative
33+import Control.Arrow (second)
31
34
32import Language.PureScript.Names
35import Language.PureScript.Names
33import Language.PureScript.Declarations
36import Language.PureScript.Declarations
@@ -51,7 +54,7 @@ moduleToJs opts (Module name decls (Just exps)) env = do
51 let optimized = concat $ map (map $ optimize opts) $ catMaybes jsDecls
54 let optimized = concat $ map (map $ optimize opts) $ catMaybes jsDecls
52 let isModuleEmpty = null exps
55 let isModuleEmpty = null exps
53 let moduleBody = JSStringLiteral "use strict" : jsImports ++ optimized
56 let moduleBody = JSStringLiteral "use strict" : jsImports ++ optimized
54− let moduleExports = JSObjectLiteral $ concatMap exportToJs exps
57+ let moduleExports = JSObjectLiteral . map (second var) . M.toList . M.unions $ map exportToJs exps
55 return $ case optionsAdditional opts of
58 return $ case optionsAdditional opts of
56 MakeOptions -> moduleBody ++ [JSAssignment (JSAccessor "exports" (JSVar "module")) moduleExports]
59 MakeOptions -> moduleBody ++ [JSAssignment (JSAccessor "exports" (JSVar "module")) moduleExports]
57 CompileOptions ns _ _ | not isModuleEmpty ->
60 CompileOptions ns _ _ | not isModuleEmpty ->
@@ -154,12 +157,12 @@ declToJs _ _ _ _ = return Nothing
154-- |
157-- |
155-- Generate key//value pairs for an object literal exporting values from a module.
158-- Generate key//value pairs for an object literal exporting values from a module.
156--
159--
157−exportToJs :: DeclarationRef -> [(String, JS)]
160+exportToJs :: DeclarationRef -> M.Map String Ident
158−exportToJs (TypeRef _ (Just dctors)) = map ((\n -> (n, var (Ident n))) . runProperName) dctors
161+exportToJs (TypeRef _ (Just dctors)) = M.fromList [ (n, Ident n) | (ProperName n) <- dctors ]
159−exportToJs (ValueRef name) = [(runIdent name, var name)]
162+exportToJs (ValueRef name) = M.singleton (runIdent name) name
160−exportToJs (TypeInstanceRef name) = [(runIdent name, var name)]
163+exportToJs (TypeInstanceRef name) = M.singleton (runIdent name) name
161−exportToJs (TypeClassRef name) = [(runProperName name, var $ Ident $ runProperName name)]
164+exportToJs (TypeClassRef name) = M.singleton (runProperName name) (Ident $ runProperName name)
162−exportToJs _ = []
165+exportToJs _ = M.empty
163
166
164-- |
167-- |
165-- Generate code in the simplified Javascript intermediate representation for a variable based on a
168-- Generate code in the simplified Javascript intermediate representation for a variable based on a