git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #676
commit232797bfac1e284796492673e8d4156c375ad929 authorPhil Freeman <paf31@cantab.net> dateTue 4 Nov 2014, 19:29 (12 years ago) parentff2ddf1
2 files changed+28−7
examples/passing/TypeClassImport.purs+18 src/Language/PureScript/CodeGen/JS.hs+10−7
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)
2626import Data.Function (on)
2727import Data.List (nub, (\\), delete, sortBy)
2828
29+import qualified Data.Map as M
30+
2931import Control.Monad (foldM, replicateM, forM)
3032import Control.Applicative
33+import Control.Arrow (second)
3134
3235import Language.PureScript.Names
3336import Language.PureScript.Declarations
@@ -51,7 +54,7 @@ moduleToJs opts (Module name decls (Just exps)) env = do
5154 let optimized = concat $ map (map $ optimize opts) $ catMaybes jsDecls
5255 let isModuleEmpty = null exps
5356 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
5558 return $ case optionsAdditional opts of
5659 MakeOptions -> moduleBody ++ [JSAssignment (JSAccessor "exports" (JSVar "module")) moduleExports]
5760 CompileOptions ns _ _ | not isModuleEmpty ->
@@ -154,12 +157,12 @@ declToJs _ _ _ _ = return Nothing
154157-- |
155158-- Generate key//value pairs for an object literal exporting values from a module.
156159--
157−exportToJs :: DeclarationRef -> [(String, JS)]
158−exportToJs (TypeRef _ (Just dctors)) = map ((\n -> (n, var (Ident n))) . runProperName) dctors
159−exportToJs (ValueRef name) = [(runIdent name, var name)]
160−exportToJs (TypeInstanceRef name) = [(runIdent name, var name)]
161−exportToJs (TypeClassRef name) = [(runProperName name, var $ Ident $ runProperName name)]
162−exportToJs _ = []
160+exportToJs :: DeclarationRef -> M.Map String Ident
161+exportToJs (TypeRef _ (Just dctors)) = M.fromList [ (n, Ident n) | (ProperName n) <- dctors ]
162+exportToJs (ValueRef name) = M.singleton (runIdent name) name
163+exportToJs (TypeInstanceRef name) = M.singleton (runIdent name) name
164+exportToJs (TypeClassRef name) = M.singleton (runProperName name) (Ident $ runProperName name)
165+exportToJs _ = M.empty
163166
164167-- |
165168-- Generate code in the simplified Javascript intermediate representation for a variable based on a