1313--
1414-----------------------------------------------------------------------------
1515
16- {-# LANGUAGE DoAndIfThenElse #-}
16+ {-# LANGUAGE GADTs, DoAndIfThenElse #-}
1717
1818module Language.PureScript.CodeGen.JS (
1919 module AST ,
20- ModuleType (.. ),
2120 declToJs ,
2221 moduleToJs ,
2322 identNeedsEscaping
2423) where
2524
26- import Data.Maybe (catMaybes , fromJust )
25+ import Data.Maybe (catMaybes )
2726import Data.Function (on )
2827import Data.List (nub , (\\) , delete , sortBy )
2928
@@ -42,40 +41,36 @@ import Language.PureScript.Supply
4241import Language.PureScript.Traversals (sndM )
4342import qualified Language.PureScript.Constants as C
4443
45- -- |
46- -- Different types of modules which are supported
47- --
48- data ModuleType = CommonJS | Globals
49-
5044-- |
5145-- Generate code in the simplified Javascript intermediate representation for all declarations in a
5246-- module.
5347--
54- moduleToJs :: (Functor m , Applicative m , Monad m ) => ModuleType -> Options -> Module -> Environment -> SupplyT m [JS ]
55- moduleToJs mt opts (Module name decls (Just exps)) env = do
56- let jsImports = map (importToJs mt opts) . delete (ModuleName [ProperName C. prim]) . (\\ [name]) . nub $ concatMap imports decls
48+ moduleToJs :: (Functor m , Applicative m , Monad m ) => Options mode -> Module -> Environment -> SupplyT m [JS ]
49+ moduleToJs opts (Module name decls (Just exps)) env = do
50+ let jsImports = map (importToJs opts) . delete (ModuleName [ProperName C. prim]) . (\\ [name]) . nub $ concatMap imports decls
5751 jsDecls <- mapM (\ decl -> declToJs opts name decl env) decls
5852 let optimized = concat $ map (map $ optimize opts) $ catMaybes jsDecls
5953 let isModuleEmpty = null exps
6054 let moduleBody = JSStringLiteral " use strict" : jsImports ++ optimized
6155 let moduleExports = JSObjectLiteral $ concatMap exportToJs exps
62- return $ case mt of
63- CommonJS -> moduleBody ++ [JSAssignment (JSAccessor " exports" (JSVar " module" )) moduleExports]
64- Globals | not isModuleEmpty ->
65- [ JSVariableIntroduction (fromJust (optionsBrowserNamespace opts))
66- (Just (JSBinary Or (JSVar (fromJust (optionsBrowserNamespace opts)) ) (JSObjectLiteral [] )) )
67- , JSAssignment (JSAccessor (moduleNameToJs name) (JSVar (fromJust (optionsBrowserNamespace opts)) ))
56+ return $ case optionsAdditional opts of
57+ MakeOptions -> moduleBody ++ [JSAssignment (JSAccessor " exports" (JSVar " module" )) moduleExports]
58+ CompileOptions ns _ _ | not isModuleEmpty ->
59+ [ JSVariableIntroduction ns
60+ (Just (JSBinary Or (JSVar ns ) (JSObjectLiteral [] )) )
61+ , JSAssignment (JSAccessor (moduleNameToJs name) (JSVar ns ))
6862 (JSApp (JSFunction Nothing [] (JSBlock (moduleBody ++ [JSReturn moduleExports]))) [] )
6963 ]
7064 _ -> []
71- moduleToJs _ _ _ _ = error " Exports should have been elaborated in name desugaring"
65+ moduleToJs _ _ _ = error " Exports should have been elaborated in name desugaring"
7266
73- importToJs :: ModuleType -> Options -> ModuleName -> JS
74- importToJs mt opts mn = JSVariableIntroduction (moduleNameToJs mn) (Just moduleBody)
67+ importToJs :: Options mode -> ModuleName -> JS
68+ importToJs opts mn =
69+ JSVariableIntroduction (moduleNameToJs mn) (Just moduleBody)
7570 where
76- moduleBody = case mt of
77- CommonJS -> JSApp (JSVar " require" ) [JSStringLiteral (runModuleName mn)]
78- Globals -> JSAccessor (moduleNameToJs mn) (JSVar (fromJust (optionsBrowserNamespace opts)) )
71+ moduleBody = case optionsAdditional opts of
72+ MakeOptions -> JSApp (JSVar " require" ) [JSStringLiteral (runModuleName mn)]
73+ CompileOptions ns _ _ -> JSAccessor (moduleNameToJs mn) (JSVar ns )
7974
8075imports :: Declaration -> [ModuleName ]
8176imports (ImportDeclaration mn _ _) = [mn]
@@ -95,7 +90,7 @@ imports other =
9590-- |
9691-- Generate code in the simplified Javascript intermediate representation for a declaration
9792--
98- declToJs :: (Functor m , Applicative m , Monad m ) => Options -> ModuleName -> Declaration -> Environment -> SupplyT m (Maybe [JS ])
93+ declToJs :: (Functor m , Applicative m , Monad m ) => Options mode -> ModuleName -> Declaration -> Environment -> SupplyT m (Maybe [JS ])
9994declToJs opts mp (ValueDeclaration ident _ _ _ val) e = do
10095 js <- valueToJs opts mp e val
10196 return $ Just [JSVariableIntroduction (identToJs ident) (Just js)]
@@ -190,7 +185,7 @@ accessorString prop | identNeedsEscaping prop = JSIndexer (JSStringLiteral prop)
190185-- |
191186-- Generate code in the simplified Javascript intermediate representation for a value or expression.
192187--
193- valueToJs :: (Functor m , Applicative m , Monad m ) => Options -> ModuleName -> Environment -> Expr -> SupplyT m JS
188+ valueToJs :: (Functor m , Applicative m , Monad m ) => Options mode -> ModuleName -> Environment -> Expr -> SupplyT m JS
194189valueToJs _ _ _ (NumericLiteral n) = return $ JSNumericLiteral n
195190valueToJs _ _ _ (StringLiteral s) = return $ JSStringLiteral s
196191valueToJs _ _ _ (BooleanLiteral b) = return $ JSBooleanLiteral b
@@ -315,7 +310,7 @@ qualifiedToJS _ f (Qualified _ a) = JSVar $ identToJs (f a)
315310-- Generate code in the simplified Javascript intermediate representation for pattern match binders
316311-- and guards.
317312--
318- bindersToJs :: (Functor m , Applicative m , Monad m ) => Options -> ModuleName -> Environment -> [CaseAlternative ] -> [JS ] -> SupplyT m JS
313+ bindersToJs :: (Functor m , Applicative m , Monad m ) => Options mode -> ModuleName -> Environment -> [CaseAlternative ] -> [JS ] -> SupplyT m JS
319314bindersToJs opts m e binders vals = do
320315 valNames <- replicateM (length vals) freshName
321316 let assignments = zipWith JSVariableIntroduction valNames (map Just vals)
@@ -405,7 +400,7 @@ binderToJs m e varName done binder@(ConsBinder _ _) = do
405400 ( JSVariableIntroduction tailVar (Just (JSApp (JSAccessor " slice" (JSVar varName)) [JSNumericLiteral (Left numberOfHeadBinders)])) :
406401 js2
407402 )) Nothing ]
408- where
403+ where
409404 uncons :: [Binder ] -> Binder -> ([Binder ], Binder )
410405 uncons acc (ConsBinder h t) = uncons (h : acc) t
411406 uncons acc (PositionedBinder _ b) = uncons acc b
@@ -416,3 +411,4 @@ binderToJs m e varName done (NamedBinder ident binder) = do
416411binderToJs m e varName done (PositionedBinder _ binder) =
417412 binderToJs m e varName done binder
418413
414+
0 commit comments