git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #559 from purescript/js-unit

Use undefined instead of {} in generated code

commit3108d9387235fab39d250b1f111773a84eab4617 authorGary Burgess <gary.burgess@gmail.com> dateTue 12 Aug 2014, 21:15 (12 years ago) parentaf1adef1bd28e3
7 files changed+40−14
src/Language/PureScript/CodeGen/JS.hs+1 src/Language/PureScript/Constants.hs+5 src/Language/PureScript/Optimizer.hs+2 src/Language/PureScript/Optimizer/Inliner.hs+3−4 src/Language/PureScript/Optimizer/MagicDo.hs+5−5 src/Language/PureScript/Optimizer/Unused.hs+19−3 src/Language/PureScript/TypeChecker/Types.hs+5−2
src/Language/PureScript/CodeGen/JS.hs+1
@@ -307,6 +307,7 @@ varToJs m qual = qualifiedToJS m id qual
307307-- variable that may have a qualified name.
308308--
309309qualifiedToJS :: ModuleName -> (a -> Ident) -> Qualified a -> JS
310+qualifiedToJS _ f (Qualified (Just (ModuleName [ProperName mn])) a) | mn == C.prim = JSVar . runIdent $ f a
310311qualifiedToJS m f (Qualified (Just m') a) | m /= m' = accessor (f a) (JSVar (moduleNameToJs m'))
311312qualifiedToJS _ f (Qualified _ a) = JSVar $ identToJs (f a)
312313
src/Language/PureScript/Constants.hs+5
@@ -153,6 +153,11 @@ mkFn = "mkFn"
153153runFn :: String
154154runFn = "runFn"
155155
156+-- Prim values
157+
158+undefined :: String
159+undefined = "undefined"
160+
156161-- Type Class Dictionary Names
157162
158163monadEffDictionary :: String
src/Language/PureScript/Optimizer.hs+2
@@ -57,6 +57,8 @@ optimize opts | optionsNoOptimizations opts = id
5757 , tco opts
5858 , magicDo opts
5959 , removeCodeAfterReturnStatements
60+ , removeUnusedArg
61+ , removeUndefinedApp
6062 , unThunk
6163 , etaConvert
6264 , evaluateIifes
src/Language/PureScript/Optimizer/Inliner.hs+3−4
@@ -49,8 +49,7 @@ etaConvert = everywhereOnJS convert
4949 not (any (`isRebound` block) (map JSVar idents)) &&
5050 not (any (`isRebound` block) args)
5151 = JSBlock (map (replaceIdents (zip idents args)) body)
52− convert (JSFunction Nothing [arg] (JSBlock [JSReturn (JSApp fn@JSVar{} [JSObjectLiteral []])]))
53− | arg == C.__unused = fn
52+ convert (JSFunction Nothing [] (JSBlock [JSReturn (JSApp fn [])])) = fn
5453 convert js = js
5554
5655unThunk :: JS -> JS
@@ -59,7 +58,7 @@ unThunk = everywhereOnJS convert
5958 convert :: JS -> JS
6059 convert (JSBlock []) = JSBlock []
6160 convert (JSBlock jss) =
62− case (last jss) of
61+ case last jss of
6362 JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) []) -> JSBlock $ init jss ++ body
6463 _ -> JSBlock jss
6564 convert js = js
@@ -154,7 +153,7 @@ inlineCommonOperators = applyAll $
154153 convert other = other
155154 isOp (JSAccessor fnName' (JSVar prelude)) = prelude == C.prelude && fnName' == fnName
156155 isOp _ = False
157− isOpDict dictName (JSApp (JSAccessor prop (JSVar prelude)) [JSObjectLiteral []]) = prelude == C.prelude && prop == dictName
156+ isOpDict dictName (JSApp (JSAccessor prop (JSVar prelude)) []) = prelude == C.prelude && prop == dictName
158157 isOpDict _ _ = False
159158 mkFn :: Int -> JS -> JS
160159 mkFn 0 = everywhereOnJS convert
src/Language/PureScript/Optimizer/MagicDo.hs+5−5
@@ -60,7 +60,7 @@ magicDo' = everywhereOnJS undo . everywhereOnJSTopDown convert
6060 -- Desugar pure
6161 convert (JSApp (JSApp pure' [val]) []) | isPure pure' = val
6262 -- Desugar >>
63− convert (JSApp (JSApp bind [m]) [JSFunction Nothing [arg] (JSBlock js)]) | isBind bind && isJSReturn (last js) && arg == C.__unused =
63+ convert (JSApp (JSApp bind [m]) [JSFunction Nothing [] (JSBlock js)]) | isBind bind && isJSReturn (last js) =
6464 let JSReturn ret = last js in
6565 JSFunction (Just fnName) [] $ JSBlock (JSApp m [] : init js ++ [JSReturn (JSApp ret [])] )
6666 -- Desugar >>=
@@ -69,10 +69,10 @@ magicDo' = everywhereOnJS undo . everywhereOnJSTopDown convert
6969 JSFunction (Just fnName) [] $ JSBlock (JSVariableIntroduction arg (Just (JSApp m [])) : init js ++ [JSReturn (JSApp ret [])] )
7070 -- Desugar untilE
7171 convert (JSApp (JSApp f [arg]) []) | isEffFunc C.untilE f =
72− JSApp (JSFunction Nothing [] (JSBlock [ JSWhile (JSUnary Not (JSApp arg [])) (JSBlock []), JSReturn (JSObjectLiteral []) ])) []
72+ JSApp (JSFunction Nothing [] (JSBlock [ JSWhile (JSUnary Not (JSApp arg [])) (JSBlock []), JSReturn $ JSObjectLiteral []])) []
7373 -- Desugar whileE
7474 convert (JSApp (JSApp (JSApp f [arg1]) [arg2]) []) | isEffFunc C.whileE f =
75− JSApp (JSFunction Nothing [] (JSBlock [ JSWhile (JSApp arg1 []) (JSBlock [ JSApp arg2 [] ]), JSReturn (JSObjectLiteral []) ])) []
75+ JSApp (JSFunction Nothing [] (JSBlock [ JSWhile (JSApp arg1 []) (JSBlock [ JSApp arg2 [] ]), JSReturn $ JSObjectLiteral []])) []
7676 convert other = other
7777 -- Check if an expression represents a monomorphic call to >>= for the Eff monad
7878 isBind (JSApp bindPoly [effDict]) | isBindPoly bindPoly && isEffDict C.bindEffDictionary effDict = True
@@ -99,8 +99,8 @@ magicDo' = everywhereOnJS undo . everywhereOnJSTopDown convert
9999 isEffFunc name (JSAccessor name' (JSVar eff)) = eff == C.eff && name == name'
100100 isEffFunc _ _ = False
101101 -- Check if an expression represents the Monad Eff dictionary
102− isEffDict name (JSApp (JSVar ident) [JSObjectLiteral []]) | ident == name = True
103− isEffDict name (JSApp (JSAccessor prop (JSVar eff)) [JSObjectLiteral []]) = eff == C.eff && prop == name
102+ isEffDict name (JSApp (JSVar ident) []) | ident == name = True
103+ isEffDict name (JSApp (JSAccessor prop (JSVar eff)) []) = eff == C.eff && prop == name
104104 isEffDict _ _ = False
105105 -- Remove __do function applications which remain after desugaring
106106 undo :: JS -> JS
src/Language/PureScript/Optimizer/Unused.hs+19−3
@@ -13,13 +13,17 @@
1313--
1414-----------------------------------------------------------------------------
1515
16−module Language.PureScript.Optimizer.Unused (
17− removeCodeAfterReturnStatements
18−) where
16+module Language.PureScript.Optimizer.Unused
17+ ( removeCodeAfterReturnStatements
18+ , removeUnusedArg
19+ , removeUndefinedApp
20+ ) where
1921
2022import Language.PureScript.CodeGen.JS.AST
2123import Language.PureScript.Optimizer.Common
2224
25+import qualified Language.PureScript.Constants as C
26+
2327removeCodeAfterReturnStatements :: JS -> JS
2428removeCodeAfterReturnStatements = everywhereOnJS (removeFromBlock go)
2529 where
@@ -28,3 +32,15 @@ removeCodeAfterReturnStatements = everywhereOnJS (removeFromBlock go)
2832 | otherwise = let (body, ret : _) = span (not . isJSReturn) jss in body ++ [ret]
2933 isJSReturn (JSReturn _) = True
3034 isJSReturn _ = False
35+
36+removeUnusedArg :: JS -> JS
37+removeUnusedArg = everywhereOnJS convert
38+ where
39+ convert (JSFunction name [arg] body) | arg == C.__unused = JSFunction name [] body
40+ convert js = js
41+
42+removeUndefinedApp :: JS -> JS
43+removeUndefinedApp = everywhereOnJS convert
44+ where
45+ convert (JSApp fn [JSVar arg]) | arg == C.undefined = JSApp fn []
46+ convert js = js
src/Language/PureScript/TypeChecker/Types.hs+5−2
@@ -357,12 +357,12 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
357357 -- Turn a DictionaryValue into a Expr
358358 dictionaryValueToValue :: DictionaryValue -> Expr
359359 dictionaryValueToValue (LocalDictionaryValue fnName) = Var fnName
360− dictionaryValueToValue (GlobalDictionaryValue fnName) = App (Var fnName) (ObjectLiteral [])
360+ dictionaryValueToValue (GlobalDictionaryValue fnName) = App (Var fnName) valUndefined
361361 dictionaryValueToValue (DependentDictionaryValue fnName dicts) = foldl App (Var fnName) (map dictionaryValueToValue dicts)
362362 dictionaryValueToValue (SubclassDictionaryValue dict superclassName index) =
363363 App (Accessor (C.__superclass_ ++ show superclassName ++ "_" ++ show index)
364364 (dictionaryValueToValue dict))
365− (ObjectLiteral [])
365+ valUndefined
366366 -- Ensure that a substitution is valid
367367 verifySubstitution :: [(String, Type)] -> Maybe [(String, Type)]
368368 verifySubstitution subst = do
@@ -393,6 +393,9 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
393393 dictTrace (SubclassDictionaryValue dict _ _) = dictTrace dict
394394 dictTrace other = other
395395
396+ valUndefined :: Expr
397+ valUndefined = Var (Qualified (Just (ModuleName [ProperName C.prim])) (Ident C.undefined))
398+
396399-- |
397400-- Check all values in a list pairwise match a predicate
398401--