on master help sign in
Merge pull request #948 from raichoo/javascript
af082b6Edwin Brady
jsrts/Runtime-common.js−3
@@ -1,6 +1,3 @@
1−var __IDRRT__ZERO = __IDRRT__bigInt("0");
2−var __IDRRT__ONE = __IDRRT__bigInt("1");
3−
4/** @constructor */
1/** @constructor */
5var __IDRRT__Type = function(type) {
2var __IDRRT__Type = function(type) {
6 this.type = type;
3 this.type = type;
jsrts/jsbn/jsbn.js+3
@@ -1233,3 +1233,6 @@ return function(val) {
1233 return new BigInteger(val);
1233 return new BigInteger(val);
1234}
1234}
1235})();
1235})();
1236+
1237+var __IDRRT__ZERO = __IDRRT__bigInt("0");
1238+var __IDRRT__ONE = __IDRRT__bigInt("1");
src/IRTS/CodegenJavaScript.hs+29−13
@@ -20,6 +20,7 @@ import Data.Maybe
20import System.IO
20import System.IO
21import System.Directory
21import System.Directory
22
22
23+
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
580
581
581− removeEvalApp js = js
582−
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
585 where
584 where
@@ -723,7 +722,7 @@ inlineFunctions js =
723 inlineHelper ([], js)
722 inlineHelper ([], js)
724 where
723 where
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
1402 Node ->
1401 Node ->
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
1413− , idrRuntime
1414− , tgtRuntime
1415− ] ++ functions
1416 )
1412 )
1417
1413
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
1425
1421
1426
1422
1427− functions :: [String]
1423+ checkForBigInt :: [JS] -> Bool
1424+ checkForBigInt js = occur
1425+ where
1426+ occur :: Bool
1427+ occur = or $ map (foldJS match (||) False) js
1428+
1429+ match :: JS -> Bool
1430+ match (JSIdent "__IDRRT__bigInt") = True
1431+ match (JSNum (JSInteger _)) = True
1432+ match js = False
1433+
1434+
1435+ runtime :: String -> String -> String -> [String]
1436+ runtime jsbn idrRuntime tgtRuntime =
1437+ if checkForBigInt optimized
1438+ then [jsbn, idrRuntime, tgtRuntime]
1439+ else [idrRuntime, tgtRuntime]
1440+
1441+
1442+ optimized :: [JS]
1428− functions = translate >>> optimize >>> compile $ def
1443+ optimized = translate >>> optimize $ def
1429 where
1444 where
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
1434− compile =
1435− map compileJS
1436
1449
1437 opt =
1450 opt =
1438 [ removeEval
1451 [ removeEval
@@ -1454,6 +1467,9 @@ codegenJavaScript target definitions includes filename outputType = do
1454 , evalCons
1467 , evalCons
1455 ]
1468 ]
1456
1469
1470+ functions :: [String]
1471+ functions = map compileJS optimized
1472+
1457 prelude :: [JS]
1473 prelude :: [JS]
1458 prelude =
1474 prelude =
1459 [ JSAnnotation JSConstructor $
1475 [ JSAnnotation JSConstructor $