@@ -14,6 +14,7 @@ module IRTS.JavaScript.Codegen( codegenJs
1414
1515import Idris.Core.TT
1616import IRTS.CodegenCommon
17+import IRTS.Exports
1718import IRTS.JavaScript.AST
1819import IRTS.JavaScript.LangTransforms
1920import IRTS.JavaScript.Name
@@ -39,7 +40,6 @@ import System.Directory (doesFileExist)
3940import System.Environment
4041import System.FilePath
4142
42−
4343-- | Code generation stats hold information about the generated user
4444-- code. Based on that information we add additional code to make
4545-- things work.
@@ -98,6 +98,31 @@ isYes (Just "Y") = True
9898isYes (Just "y") = True
9999isYes _ = False
100100
101+makeExportDecls :: Map Name LDecl -> ExportIFace -> [Text]
102+makeExportDecls defs (Export _ _ e) =
103+ concatMap makeExport e
104+ where
105+ uncurryF name argTy (Just args) =
106+ if length argTy == length args then name
107+ else T.concat [ "function(){ return "
108+ , name
109+ , ".apply(this, Array.prototype.slice.call(arguments, 0,", T.pack $ show $ length args,"))"
110+ , T.concat $ map (\x -> T.concat ["(arguments[", T.pack $ show x , "])"]) [length args .. (length argTy - 1)]
111+ , "}"
112+ ]
113+ uncurryF name argTy Nothing = name
114+
115+ addApplyForIO (FIO _) f = T.concat ["function(){ return (",f, ").apply(this,arguments)()}"]
116+ addApplyForIO _ f = f
117+
118+ makeExport (ExportData _) =
119+ []
120+ makeExport (ExportFun name (FStr exportname) retTy argTy) =
121+ [T.concat [ T.pack $ exportname
122+ , ": "
123+ , addApplyForIO retTy $ uncurryF (jsName name) argTy (getArgList' name defs)
124+ ]
125+ ]
101126
102127codegenJs :: CGConf -> CodeGenerator
103128codegenJs conf ci =
@@ -105,7 +130,10 @@ codegenJs conf ci =
105130 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
106131 let defs' = Map.fromList $ liftDecls ci
107132 let defs = globlToCon defs'
108− let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
133+ let iface = interfaces ci
134+ let used = if iface then
135+ Map.elems $ removeDeadCode defs (getExpNames $ exportDecls ci)
136+ else Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
109137 when debug $ do
110138 writeFile (outputFile ci ++ ".LDeclsDebug") $ (unlines $ intersperse "" $ map show used) ++ "\n\n\n"
111139 putStrLn $ "Finished calculating used"
@@ -136,7 +164,8 @@ codegenJs conf ci =
136164 , doPartials (partialApplications stats), "\n"
137165 , doHiddenClasses (hiddenClasses stats), "\n"
138166 , out, "\n"
139− , jsName (sMN 0 "runMain"), "();\n"
167+ , if iface then T.concat ["module.exports = {\n", T.intercalate ",\n" $ concatMap (makeExportDecls defs) (exportDecls ci), "\n};\n"]
168+ else jsName (sMN 0 "runMain") `T.append` "();\n"
140169 , "}.call(this))"
141170 , footer conf
142171 ]
@@ -223,13 +252,17 @@ getConsId n =
223252 Just (LConstructor _ conId arity) -> pure (conId, arity)
224253 _ -> error $ "Internal JS Backend error " ++ showCG n ++ " is not a constructor."
225254
255+getArgList' :: Name -> Map Name LDecl -> Maybe [Name]
256+getArgList' n defs =
257+ case Map.lookup n defs of
258+ Just (LFun _ _ a _) -> Just a
259+ _ -> Nothing
260+
226261getArgList :: Name -> State CGBodyState (Maybe [Name])
227262getArgList n =
228263 do
229264 st <- get
230− case Map.lookup n (defs st) of
231− Just (LFun _ _ a _) -> pure $ Just a
232− _ -> pure Nothing
265+ pure $ getArgList' n (defs st)
233266
234267data BodyResTarget = ReturnBT
235268 | DecBT Text