@@ -322,6 +322,17 @@ transformJS tr js =
322 | otherwise = js
322 | otherwise = js
325+moveJSDeclToTop :: String -> [JS] -> [JS]
326+moveJSDeclToTop decl js = move ([], js)
328+ move :: ([JS], [JS]) -> [JS]
329+ move (front, js@(JSAlloc name _):back)
330+ | name == decl = js : front ++ back
332+ move (front, js:back) =
333+ move (front ++ [js], back)
325jsSubst :: JS -> JS -> JS -> JS
336jsSubst :: JS -> JS -> JS -> JS
326jsSubst var new old
337jsSubst var new old
327 | var == old = new
338 | var == old = new
@@ -518,8 +529,12 @@ deadEvalApplyCases js =
518initConstructors :: [JS] -> [JS]
529initConstructors :: [JS] -> [JS]
519initConstructors js =
530initConstructors js =
520 let tags = nub $ sort $ concat (map getTags js) in
531 let tags = nub $ sort $ concat (map getTags js) in
521− map createConstant tags ++ replaceConstructors tags js
532+ rearrangePrelude $ map createConstant tags ++ replaceConstructors tags js
534+ rearrangePrelude :: [JS] -> [JS]
535+ rearrangePrelude = moveJSDeclToTop $ idrRTNamespace ++ "Con"
523 getTags :: JS -> [Int]
538 getTags :: JS -> [Int]
524 getTags = foldJS match (++) []
539 getTags = foldJS match (++) []
@@ -670,6 +685,8 @@ inlineFunctions js =
671 | JSApp (JSIdent ident) _ <- js
686 | JSApp (JSIdent ident) _ <- js
672 , name == ident = 1
687 , name == ident = 1
673 | otherwise = 0
690 | otherwise = 0
@@ -854,9 +871,12 @@ codegenJavaScript target definitions filename outputType = do
854 functions :: [String]
871 functions :: [String]
855 functions = translate >>> optimize >>> compile $ def
872 functions = translate >>> optimize >>> compile $ def
857− translate p = concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
875+ prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
858− optimize p = foldl' (flip ($)) p opt
877+ foldl' (flip ($)) p opt
859− compile = map compileJS
879+ map compileJS
862 [ map optimizeJS
882 [ map optimizeJS
@@ -874,6 +894,17 @@ codegenJavaScript target definitions filename outputType = do
874 , map reduceContinuations
894 , map reduceContinuations
899+ [ JSAlloc (idrRTNamespace ++ "Cont") (Just $ JSFunction ["k"] (
900+ JSAssign (JSProj JSThis "k") (JSIdent "k")
902+ , JSAlloc (idrRTNamespace ++ "Con") (Just $ JSFunction ["tag", "vars"] (
903+ JSSeq [ JSAssign (JSProj JSThis "tag") (JSIdent "tag")
904+ , JSAssign (JSProj JSThis "vars") (JSIdent "vars")
878 mainLoop :: JS
909 mainLoop :: JS
879 mainLoop =
910 mainLoop =