@@ -407,8 +407,8 @@ removeAllocations (JSSeq body) =
407removeAllocations js = transformJS removeAllocations js
407removeAllocations js = transformJS removeAllocations js
410−isJSConstant :: JS -> Bool
410+isJSConstant :: [String] -> JS -> Bool
411−isJSConstant js
411+isJSConstant constants js
412 | JSString _ <- js = True
412 | JSString _ <- js = True
413 | JSChar _ <- js = True
413 | JSChar _ <- js = True
414 | JSNum _ <- js = True
414 | JSNum _ <- js = True
@@ -416,6 +416,13 @@ isJSConstant js
416 | JSNull <- js = True
416 | JSNull <- js = True
418 | JSApp (JSIdent "__IDRRT__bigInt") _ <- js = True
418 | JSApp (JSIdent "__IDRRT__bigInt") _ <- js = True
420+ | JSArray vals <- js = all (isJSConstant constants) vals
421+ | JSNew "__IDRRT__Con" args <- js = all (isJSConstant constants) args
423+ | JSIdent name <- js
424+ , name `elem` constants = True
419 | otherwise = False
426 | otherwise = False
@@ -443,12 +450,12 @@ inlineJS (JSApp (JSProj (JSFunction args (JSReturn body)) "apply") [
443 ) (n + 1)
450 ) (n + 1)
445inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
452inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
446− | isJSConstant val = val
453+ | isJSConstant [] val = val
448inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
455inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
449 JSFunction [] (JSReturn val)
456 JSFunction [] (JSReturn val)
451− | isJSConstant val = val
458+ | isJSConstant [] val = val
453inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
460inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
454 | JSNew con [tag, vals] <- ret
461 | JSNew con [tag, vals] <- ret
@@ -1000,6 +1007,59 @@ optimizeJS = inlineLoop
1000 | otherwise = js
1007 | otherwise = js
1010+extractLocalConstructors :: [JS] -> [JS]
1011+extractLocalConstructors js =
1012+ concatMap extractLocalConstructors' js
1014+ globalCons :: [String]
1015+ globalCons = concatMap (getGlobalCons) js
1018+ extractLocalConstructors' :: JS -> [JS]
1019+ extractLocalConstructors' js@(JSAlloc fun (Just (JSFunction args body))) =
1020+ map allocCon cons ++ [foldr (uncurry jsSubst) js (reverse cons)]
1023+ cons = zipWith genName (foldJS match (++) [] body) [1..]
1025+ genName :: JS -> Int -> (JS, JS)
1027+ (js, JSIdent $ idrCTRNamespace ++ fun ++ "_" ++ show id)
1029+ match :: JS -> [JS]
1031+ | JSNew "__IDRRT__Con" args <- js
1032+ , all isConstant args = [js]
1036+ allocCon :: (JS, JS) -> JS
1037+ allocCon (js, JSIdent name) =
1038+ JSAlloc name (Just js)
1041+ isConstant :: JS -> Bool
1043+ | JSNew "__IDRRT__Con" args <- js
1044+ , all isConstant args =
1047+ isJSConstant globalCons js
1049+ extractLocalConstructors' js = [js]
1052+ getGlobalCons :: JS -> [String]
1053+ getGlobalCons js = foldJS match (++) [] js
1055+ match :: JS -> [String]
1057+ | (JSAlloc name (Just (JSNew "__IDRRT__Con" _))) <- js =
1003codegenJavaScript
1063codegenJavaScript
1004 :: JSTarget
1064 :: JSTarget
1005 -> [(Name, SDecl)]
1065 -> [(Name, SDecl)]
@@ -1056,6 +1116,7 @@ codegenJavaScript target definitions filename outputType = do
1056 , map removeInstanceChecks
1116 , map removeInstanceChecks
1057 , inlineFunctions
1117 , inlineFunctions
1058 , map reduceContinuations
1118 , map reduceContinuations
1119+ , extractLocalConstructors
1059 , unfoldLookupTable
1120 , unfoldLookupTable