on master help sign in
javascript: do not include jsbn if not needed
commitb96f713dff2b7a35ca59ef9b6273e0fc578834b8 authorraichoo <raichoo@googlemail.com> dateTue 4 Mar 2014, 20:50 (13 years ago) parent1ffa80d
3 files changed+30−13
jsrts/Runtime-common.js−3 jsrts/jsbn/jsbn.js+3 src/IRTS/CodegenJavaScript.hs+27−10
jsrts/Runtime-common.js−3
@@ -1,6 +1,3 @@
1−var __IDRRT__ZERO = __IDRRT__bigInt("0");
2−var __IDRRT__ONE = __IDRRT__bigInt("1");
3−
41/** @constructor */
52var __IDRRT__Type = function(type) {
63 this.type = type;
jsrts/jsbn/jsbn.js+3
@@ -1233,3 +1233,6 @@ return function(val) {
12331233 return new BigInteger(val);
12341234}
12351235})();
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
13981398 -> OutputType
13991399 -> IO ()
14001400codegenJavaScript target definitions includes filename outputType = do
1401− let (header, runtime) = case target of
1401+ let (header, rt) = case target of
14021402 Node ->
14031403 ("#!/usr/bin/env node\n", "-node")
14041404 JavaScript ->
@@ -1406,13 +1406,10 @@ codegenJavaScript target definitions includes filename outputType = do
14061406 included <- getIncludes includes
14071407 path <- (++) <$> getDataDir <*> (pure "/jsrts/")
14081408 idrRuntime <- readFile $ path ++ "Runtime-common.js"
1409− tgtRuntime <- readFile $ concat [path, "Runtime", runtime, ".js"]
1409+ tgtRuntime <- readFile $ concat [path, "Runtime", rt, ".js"]
14101410 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
14111411 writeFile filename $ header ++ (
1412− intercalate "\n" $ included ++ [ jsbn
1413− , idrRuntime
1414− , tgtRuntime
1415− ] ++ functions
1412+ intercalate "\n" $ included ++ runtime jsbn idrRuntime tgtRuntime ++ functions
14161413 )
14171414
14181415 setPermissions filename (emptyPermissions { readable = True
@@ -1424,15 +1421,32 @@ codegenJavaScript target definitions includes filename outputType = do
14241421 def = map (first translateNamespace) definitions
14251422
14261423
1427− functions :: [String]
1428− functions = translate >>> optimize >>> compile $ def
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]
1444+ optimized = translate >>> optimize $ def
14291445 where
14301446 translate p =
14311447 prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
14321448 optimize p =
14331449 foldl' (flip ($)) p opt
1434− compile =
1435− map compileJS
14361450
14371451 opt =
14381452 [ removeEval
@@ -1454,6 +1468,9 @@ codegenJavaScript target definitions includes filename outputType = do
14541468 , evalCons
14551469 ]
14561470
1471+ functions :: [String]
1472+ functions = map compileJS optimized
1473+
14571474 prelude :: [JS]
14581475 prelude =
14591476 [ JSAnnotation JSConstructor $