on master help sign in
Merge pull request #948 from raichoo/javascript

Javascript

commitaf082b6f0df5e0109a451c4a7f03d2891542b205 authorEdwin Brady <edwin.brady@gmail.com> dateWed 5 Mar 2014, 10:07 (13 years ago) parent6e74bd73ccbc55
3 files changed+32−16
jsrts/Runtime-common.js−3 jsrts/jsbn/jsbn.js+3 src/IRTS/CodegenJavaScript.hs+29−13
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+29−13
@@ -20,6 +20,7 @@ import Data.Maybe
2020import System.IO
2121import System.Directory
2222
23+
2324idrNamespace :: String
2425idrNamespace = "__IDR__"
2526idrRTNamespace = "__IDRRT__"
@@ -578,8 +579,6 @@ removeEval js =
578579 match (JSApp (JSIdent "__IDR__mEVAL0") [val]) = val
579580 match js = transformJS match js
580581
581− removeEvalApp js = js
582−
583582 checkEval :: [JS] -> ([JS], Bool)
584583 checkEval js = foldr f ([], False) $ map checkEval' js
585584 where
@@ -723,7 +722,7 @@ inlineFunctions js =
723722 inlineHelper ([], js)
724723 where
725724 inlineHelper :: ([JS], [JS]) -> [JS]
726− inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
725+ inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
727726 | countAll fun front + countAll fun back == 0 =
728727 inlineHelper (front, back)
729728 | Just new <- inlineAble (
@@ -1398,7 +1397,7 @@ codegenJavaScript
13981397 -> OutputType
13991398 -> IO ()
14001399codegenJavaScript target definitions includes filename outputType = do
1401− let (header, runtime) = case target of
1400+ let (header, rt) = case target of
14021401 Node ->
14031402 ("#!/usr/bin/env node\n", "-node")
14041403 JavaScript ->
@@ -1406,13 +1405,10 @@ codegenJavaScript target definitions includes filename outputType = do
14061405 included <- getIncludes includes
14071406 path <- (++) <$> getDataDir <*> (pure "/jsrts/")
14081407 idrRuntime <- readFile $ path ++ "Runtime-common.js"
1409− tgtRuntime <- readFile $ concat [path, "Runtime", runtime, ".js"]
1408+ tgtRuntime <- readFile $ concat [path, "Runtime", rt, ".js"]
14101409 jsbn <- readFile $ path ++ "jsbn/jsbn.js"
14111410 writeFile filename $ header ++ (
1412− intercalate "\n" $ included ++ [ jsbn
1413− , idrRuntime
1414− , tgtRuntime
1415− ] ++ functions
1411+ intercalate "\n" $ included ++ runtime jsbn idrRuntime tgtRuntime ++ functions
14161412 )
14171413
14181414 setPermissions filename (emptyPermissions { readable = True
@@ -1424,15 +1420,32 @@ codegenJavaScript target definitions includes filename outputType = do
14241420 def = map (first translateNamespace) definitions
14251421
14261422
1427− functions :: [String]
1428− functions = translate >>> optimize >>> compile $ def
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]
1443+ optimized = translate >>> optimize $ def
14291444 where
14301445 translate p =
14311446 prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
14321447 optimize p =
14331448 foldl' (flip ($)) p opt
1434− compile =
1435− map compileJS
14361449
14371450 opt =
14381451 [ removeEval
@@ -1454,6 +1467,9 @@ codegenJavaScript target definitions includes filename outputType = do
14541467 , evalCons
14551468 ]
14561469
1470+ functions :: [String]
1471+ functions = map compileJS optimized
1472+
14571473 prelude :: [JS]
14581474 prelude =
14591475 [ JSAnnotation JSConstructor $