on master help sign in
add support for FFI_Export in js backend but the command line option does not reach js yet
commit2dd2fd1df79fac0e1c26caf7474bb0e71e08dfec authorRui Barreiro <rui.barreiro@gmail.com> dateWed 13 Sep 2017, 08:44 (9 years ago) parentf925538
2 files changed+37−13
src/IRTS/CodegenJavaScript.hs+21−11 src/IRTS/JavaScript/Codegen.hs+16−2
src/IRTS/CodegenJavaScript.hs+21−11
@@ -56,14 +56,24 @@ codegenJavaScript ci =
5656codegenNode :: CodeGenerator
5757codegenNode ci =
5858 do
59− codegenJs (CGConf { header = "#!/usr/bin/env node\n"
60− , footer = ""
61− , jsbnPath = "jsbn/jsbn-browser.js"
62− , extraRunTime = "Runtime-node.js"
63− }
64− )
65− ci
66− setPermissions (outputFile ci) (emptyPermissions { readable = True
67− , executable = True
68− , writable = True
69− })
59+ if outputType ci == Executable then
60+ do
61+ codegenJs (CGConf { header = "#!/usr/bin/env node\n"
62+ , footer = ""
63+ , jsbnPath = "jsbn/jsbn-browser.js"
64+ , extraRunTime = "Runtime-node.js"
65+ }
66+ )
67+ ci
68+ setPermissions (outputFile ci) (emptyPermissions { readable = True
69+ , executable = True
70+ , writable = True
71+ })
72+ else
73+ codegenJs (CGConf { header = ""
74+ , footer = ""
75+ , jsbnPath = "jsbn/jsbn-browser.js"
76+ , extraRunTime = "Runtime-node.js"
77+ }
78+ )
79+ ci
src/IRTS/JavaScript/Codegen.hs+16−2
@@ -21,6 +21,7 @@ import IRTS.JavaScript.PrimOp
2121import IRTS.JavaScript.Specialize
2222import IRTS.Lang
2323import IRTS.System
24+import IRTS.Exports
2425
2526import Control.Applicative (pure, (<$>))
2627import Control.Monad
@@ -98,6 +99,14 @@ isYes (Just "Y") = True
9899isYes (Just "y") = True
99100isYes _ = False
100101
102+makeExportDecls :: ExportIFace -> [Text]
103+makeExportDecls (Export _ _ e) =
104+ concatMap makeExport e
105+ where
106+ makeExport (ExportData _) =
107+ []
108+ makeExport (ExportFun name (FStr exportname) _ _) =
109+ [T.concat [T.pack $ exportname, ": ", jsName name]]
101110
102111codegenJs :: CGConf -> CodeGenerator
103112codegenJs conf ci =
@@ -105,7 +114,11 @@ codegenJs conf ci =
105114 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
106115 let defs' = Map.fromList $ liftDecls ci
107116 let defs = globlToCon defs'
108− let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
117+ let isExec = outputType ci == Executable
118+ putStrLn $ show $ isExec
119+ let used = if isExec then
120+ Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
121+ else Map.elems $ removeDeadCode defs (getExpNames $ exportDecls ci)
109122 when debug $ do
110123 writeFile (outputFile ci ++ ".LDeclsDebug") $ (unlines $ intersperse "" $ map show used) ++ "\n\n\n"
111124 putStrLn $ "Finished calculating used"
@@ -136,7 +149,8 @@ codegenJs conf ci =
136149 , doPartials (partialApplications stats), "\n"
137150 , doHiddenClasses (hiddenClasses stats), "\n"
138151 , out, "\n"
139− , jsName (sMN 0 "runMain"), "();\n"
152+ , if isExec then jsName (sMN 0 "runMain") `T.append` "();\n"
153+ else T.concat ["module.exports = {\n", T.intercalate ";\n" $ concatMap makeExportDecls (exportDecls ci), "\n};\n"]
140154 , "}.call(this))"
141155 , footer conf
142156 ]