on master help sign in
javascript: do not include jsbn if not needed
b96f713raichoo
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+27−10
@@ -1398,7 +1398,7 @@ codegenJavaScript
1398 -> OutputType
1398 -> OutputType
1399 -> IO ()
1399 -> IO ()
1400codegenJavaScript target definitions includes filename outputType = do
1400codegenJavaScript target definitions includes filename outputType = do
1401− let (header, runtime) = case target of
1401+ let (header, rt) = case target of
1402 Node ->
1402 Node ->
1403 ("#!/usr/bin/env node\n", "-node")
1403 ("#!/usr/bin/env node\n", "-node")
1404 JavaScript ->
1404 JavaScript ->
@@ -1406,13 +1406,10 @@ codegenJavaScript target definitions includes filename outputType = do
1406 included <- getIncludes includes
1406 included <- getIncludes includes
1407 path <- (++) <$> getDataDir <*> (pure "/jsrts/")
1407 path <- (++) <$> getDataDir <*> (pure "/jsrts/")
1408 idrRuntime <- readFile $ path ++ "Runtime-common.js"
1408 idrRuntime <- readFile $ path ++ "Runtime-common.js"
1409− tgtRuntime <- readFile $ concat [path, "Runtime", runtime, ".js"]
1409+ tgtRuntime <- readFile $ concat [path, "Runtime", rt, ".js"]
1410 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
1410 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
1411 writeFile filename $ header ++ (
1411 writeFile filename $ header ++ (
1412− intercalate "\n" $ included ++ [ jsbn
1412+ intercalate "\n" $ included ++ runtime jsbn idrRuntime tgtRuntime ++ functions
1413− , idrRuntime
1414− , tgtRuntime
1415− ] ++ functions
1416 )
1413 )
1417
1414
1418 setPermissions filename (emptyPermissions { readable = True
1415 setPermissions filename (emptyPermissions { readable = True
@@ -1424,15 +1421,32 @@ codegenJavaScript target definitions includes filename outputType = do
1424 def = map (first translateNamespace) definitions
1421 def = map (first translateNamespace) definitions
1425
1422
1426
1423
1427− functions :: [String]
1424+ checkForBigInt :: [JS] -> Bool
1425+ checkForBigInt js = occur
1426+ where
1427+ occur :: Bool
1428+ occur = or $ map (foldJS match (||) False) js
1429+
1430+ match :: JS -> Bool
1431+ match (JSIdent "__IDRRT__bigInt") = True
1432+ match (JSNum (JSInteger _)) = True
1433+ match js = False
1434+
1435+
1436+ runtime :: String -> String -> String -> [String]
1437+ runtime jsbn idrRuntime tgtRuntime =
1438+ if checkForBigInt optimized
1439+ then [jsbn, idrRuntime, tgtRuntime]
1440+ else [idrRuntime, tgtRuntime]
1441+
1442+
1443+ optimized :: [JS]
1428− functions = translate >>> optimize >>> compile $ def
1444+ optimized = translate >>> optimize $ def
1429 where
1445 where
1430 translate p =
1446 translate p =
1431 prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
1447 prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
1432 optimize p =
1448 optimize p =
1433 foldl' (flip ($)) p opt
1449 foldl' (flip ($)) p opt
1434− compile =
1435− map compileJS
1436
1450
1437 opt =
1451 opt =
1438 [ removeEval
1452 [ removeEval
@@ -1454,6 +1468,9 @@ codegenJavaScript target definitions includes filename outputType = do
1454 , evalCons
1468 , evalCons
1455 ]
1469 ]
1456
1470
1471+ functions :: [String]
1472+ functions = map compileJS optimized
1473+
1457 prelude :: [JS]
1474 prelude :: [JS]
1458 prelude =
1475 prelude =
1459 [ JSAnnotation JSConstructor $
1476 [ JSAnnotation JSConstructor $