@@ -78,6 +78,15 @@ getIncludes l = do
78 incs <- mapM getInclude l
78 incs <- mapM getInclude l
79 return $ T.intercalate "\n\n" incs
79 return $ T.intercalate "\n\n" incs
81+includeLibs :: [String] -> String
88+ concatMap (\lib -> "var " ++ (repl <$> lib) ++ " = require(\"" ++ lib ++"\");\n")
81isYes :: Maybe String -> Bool
90isYes :: Maybe String -> Bool
82isYes (Just "Y") = True
91isYes (Just "Y") = True
83isYes (Just "y") = True
92isYes (Just "y") = True
@@ -87,12 +96,7 @@ isYes _ = False
87codegenJs :: CGConf -> CodeGenerator
96codegenJs :: CGConf -> CodeGenerator
88codegenJs conf ci =
97codegenJs conf ci =
90− optim <- isYes <$> lookupEnv "IDRISJS_OPTIM"
91 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
99 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
93− if optim then putStrLn "compiling width idris-js optimizations"
94− else putStrLn "compiling widthout idris-js optimizations"
96 let defs = Map.fromList $ liftDecls ci
100 let defs = Map.fromList $ liftDecls ci
97 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
101 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
98 if debug then
102 if debug then
@@ -112,15 +116,20 @@ codegenJs conf ci =
112 extraRT <- TIO.readFile $ path </> (extraRunTime conf)
116 extraRT <- TIO.readFile $ path </> (extraRunTime conf)
114 includes <- getIncludes $ includes ci
118 includes <- getIncludes $ includes ci
119+ let libs = T.pack $ includeLibs $ compileLibs ci
115 TIO.writeFile (outputFile ci) $ T.concat [ header conf
120 TIO.writeFile (outputFile ci) $ T.concat [ header conf
116 , "\"use strict\";\n\n"
121 , "\"use strict\";\n\n"
117 , "(function(){\n\n"
122 , "(function(){\n\n"
118 , runtimeCommon, "\n"
124 , runtimeCommon, "\n"
119 , extraRT, "\n"
125 , extraRT, "\n"
120 , jsbn, "\n"
126 , jsbn, "\n"
127+ -- external libraries
121 , doPartials (partialApplications stats), "\n"
131 , doPartials (partialApplications stats), "\n"
122 , doHiddenClasses (hiddenClasses stats), "\n"
132 , doHiddenClasses (hiddenClasses stats), "\n"
124 , out, "\n"
133 , out, "\n"
125 , jsName (sMN 0 "runMain"), "();\n"
134 , jsName (sMN 0 "runMain"), "();\n"
126 , "}.call(this))"
135 , "}.call(this))"
@@ -142,6 +151,7 @@ doHiddenClasses :: Set HiddenClass -> Text
142doHiddenClasses x =
151doHiddenClasses x =
143 T.intercalate "\n" (map f $ Set.toList x)
152 T.intercalate "\n" (map f $ Set.toList x)
154+ f p@(HiddenClass n id 0) = jsStmt2Text $ JsDecConst (jsNameHiddenClass p) $ JsObj [("type", JsInt id)]
145 f p@(HiddenClass n id arity) =
155 f p@(HiddenClass n id arity) =
146 let vars = map dataPartName $ take arity [1..]
156 let vars = map dataPartName $ take arity [1..]
147 in jsStmt2Text $
157 in jsStmt2Text $
@@ -356,27 +366,9 @@ cgBody' rt (LApp tailcall (LV (Glob fn)) args) =
356 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
366 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
357 addUsedArgsTailCallOptim y3
367 addUsedArgsTailCallOptim y3
358 pure (preDecs, y1 `JsSeq` y2)
368 pure (preDecs, y1 `JsSeq` y2)
360− do
370+ app <- formApp fn argVals
361− argsFn <- getArgList fn
365− pure (preDecs, addRT rt $ jsCurryApp fn' argVals )
368− let lenAgFn = length agFn
369− let lenArgs = length args
370− case compare lenAgFn lenArgs of
372− pure (preDecs, addRT rt $ jsAppN fname argVals)
371+ pure (preDecs, addRT rt app)
374− pure (preDecs, addRT rt $ jsCurryApp (jsAppN fname (take lenAgFn argVals)) (drop lenAgFn argVals) )
377− let part = Partial fn lenArgs lenAgFn
379− pure (preDecs, addRT rt $ jsAppN (jsNamePartial part) argVals )
381cgBody' rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
373cgBody' rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
382cgBody' rt (LLazyApp n args) =
374cgBody' rt (LLazyApp n args) =
@@ -454,18 +446,36 @@ altHasNoProj :: LAlt -> Bool
454altHasNoProj (LConCase _ _ args _) = args == []
446altHasNoProj (LConCase _ _ args _) = args == []
455altHasNoProj _ = True
447altHasNoProj _ = True
449+formApp :: Name -> [JsExpr] -> State CGBodyState JsExpr
450+formApp fn argVals = case specialCall fn of
451+ Just (arity, g) | arity == length argVals -> pure $ g argVals
453+ argsFn <- getArgList fn
456+ Nothing -> pure $ jsCurryApp fname argVals
458+ let lenAgFn = length agFn
459+ let lenArgs = length argVals
460+ case compare lenAgFn lenArgs of
461+ EQ -> pure $ JsApp fname argVals
462+ LT -> pure $ jsCurryApp (JsApp fname (take lenAgFn argVals)) (drop lenAgFn argVals)
464+ let part = Partial fn lenArgs lenAgFn
466+ pure $ jsAppN (jsNamePartial part) argVals
457formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
468formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
458formCon n args = do
469formCon n args = do
459 case specialCased n of
470 case specialCased n of
460 Just (ctor, test, match) -> pure $ ctor args
471 Just (ctor, test, match) -> pure $ ctor args
461 Nothing -> do
472 Nothing -> do
462 (conId, arity) <- getConsId n
473 (conId, arity) <- getConsId n
465− let hc = HiddenClass n conId arity
474+ let hc = HiddenClass n conId arity
466− addHiddenClass hc
475+ addHiddenClass hc
476+ pure $ if (arity > 0)
467− pure $ JsNew (JsVar $ jsNameHiddenClass hc) args
477+ then JsNew (JsVar $ jsNameHiddenClass hc) args
468− else pure $ JsInt conId
478+ else JsVar $ jsNameHiddenClass hc
470formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
480formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
471formConTest n x = do
481formConTest n x = do
@@ -473,9 +483,10 @@ formConTest n x = do
473 Just (ctor, test, match) -> pure $ test x
483 Just (ctor, test, match) -> pure $ test x
474 Nothing -> do
484 Nothing -> do
475 (conId, arity) <- getConsId n
485 (conId, arity) <- getConsId n
486+ pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
476− if (arity > 0)
487+ -- if (arity > 0)
477− then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
488+ -- then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
478− else pure $ JsBinOp "===" x (JsInt conId)
489+ -- else pure $ JsBinOp "===" x (JsInt conId)
480formProj :: Name -> JsExpr -> Int -> JsExpr
491formProj :: Name -> JsExpr -> Int -> JsExpr
481formProj n v i =
492formProj n v i =