@@ -20,6 +20,7 @@ import Data.Maybe
20import System.IO
20import System.IO
21import System.Directory
21import System.Directory
23idrNamespace :: String
24idrNamespace :: String
24idrNamespace = "__IDR__"
25idrNamespace = "__IDR__"
25idrRTNamespace = "__IDRRT__"
26idrRTNamespace = "__IDRRT__"
@@ -578,8 +579,6 @@ removeEval js =
578 match (JSApp (JSIdent "__IDR__mEVAL0") [val]) = val
579 match (JSApp (JSIdent "__IDR__mEVAL0") [val]) = val
579 match js = transformJS match js
580 match js = transformJS match js
581− removeEvalApp js = js
583 checkEval :: [JS] -> ([JS], Bool)
582 checkEval :: [JS] -> ([JS], Bool)
584 checkEval js = foldr f ([], False) $ map checkEval' js
583 checkEval js = foldr f ([], False) $ map checkEval' js
@@ -723,7 +722,7 @@ inlineFunctions js =
723 inlineHelper ([], js)
722 inlineHelper ([], js)
725 inlineHelper :: ([JS], [JS]) -> [JS]
724 inlineHelper :: ([JS], [JS]) -> [JS]
726− inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
725+ inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
727 | countAll fun front + countAll fun back == 0 =
726 | countAll fun front + countAll fun back == 0 =
728 inlineHelper (front, back)
727 inlineHelper (front, back)
729 | Just new <- inlineAble (
728 | Just new <- inlineAble (
@@ -1398,7 +1397,7 @@ codegenJavaScript
1398 -> OutputType
1397 -> OutputType
1399 -> IO ()
1398 -> IO ()
1400codegenJavaScript target definitions includes filename outputType = do
1399codegenJavaScript target definitions includes filename outputType = do
1401− let (header, runtime) = case target of
1400+ let (header, rt) = case target of
1403 ("#!/usr/bin/env node\n", "-node")
1402 ("#!/usr/bin/env node\n", "-node")
1404 JavaScript ->
1403 JavaScript ->
@@ -1406,13 +1405,10 @@ codegenJavaScript target definitions includes filename outputType = do
1406 included <- getIncludes includes
1405 included <- getIncludes includes
1407 path <- (++) <$> getDataDir <*> (pure "/jsrts/")
1406 path <- (++) <$> getDataDir <*> (pure "/jsrts/")
1408 idrRuntime <- readFile $ path ++ "Runtime-common.js"
1407 idrRuntime <- readFile $ path ++ "Runtime-common.js"
1409− tgtRuntime <- readFile $ concat [path, "Runtime", runtime, ".js"]
1408+ tgtRuntime <- readFile $ concat [path, "Runtime", rt, ".js"]
1410 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
1409 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
1411 writeFile filename $ header ++ (
1410 writeFile filename $ header ++ (
1412− intercalate "\n" $ included ++ [ jsbn
1411+ intercalate "\n" $ included ++ runtime jsbn idrRuntime tgtRuntime ++ functions
1418 setPermissions filename (emptyPermissions { readable = True
1414 setPermissions filename (emptyPermissions { readable = True
@@ -1424,15 +1420,32 @@ codegenJavaScript target definitions includes filename outputType = do
1424 def = map (first translateNamespace) definitions
1420 def = map (first translateNamespace) definitions
1427− functions :: [String]
1423+ checkForBigInt :: [JS] -> Bool
1424+ checkForBigInt js = occur
1427+ occur = or $ map (foldJS match (||) False) js
1429+ match :: JS -> Bool
1430+ match (JSIdent "__IDRRT__bigInt") = True
1431+ match (JSNum (JSInteger _)) = True
1435+ runtime :: String -> String -> String -> [String]
1436+ runtime jsbn idrRuntime tgtRuntime =
1437+ if checkForBigInt optimized
1438+ then [jsbn, idrRuntime, tgtRuntime]
1439+ else [idrRuntime, tgtRuntime]
1428− functions = translate >>> optimize >>> compile $ def
1443+ optimized = translate >>> optimize $ def
1430 translate p =
1445 translate p =
1431 prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
1446 prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
1432 optimize p =
1447 optimize p =
1433 foldl' (flip ($)) p opt
1448 foldl' (flip ($)) p opt
1438 [ removeEval
1451 [ removeEval
@@ -1454,6 +1467,9 @@ codegenJavaScript target definitions includes filename outputType = do
1454 , evalCons
1467 , evalCons
1470+ functions :: [String]
1471+ functions = map compileJS optimized
1457 prelude :: [JS]
1473 prelude :: [JS]
1458 prelude =
1474 prelude =
1459 [ JSAnnotation JSConstructor $
1475 [ JSAnnotation JSConstructor $