@@ -71,6 +71,15 @@ getIncludes l = do
71 incs <- mapM getInclude l
71 incs <- mapM getInclude l
72 return $ T.intercalate "\n\n" incs
72 return $ T.intercalate "\n\n" incs
74+includeLibs :: [String] -> String
81+ concatMap (\lib -> "var " ++ (repl <$> lib) ++ " = require(\"" ++ lib ++"\");\n")
74isYes :: Maybe String -> Bool
83isYes :: Maybe String -> Bool
75isYes (Just "Y") = True
84isYes (Just "Y") = True
76isYes (Just "y") = True
85isYes (Just "y") = True
@@ -80,12 +89,7 @@ isYes _ = False
80codegenJs :: CGConf -> CodeGenerator
89codegenJs :: CGConf -> CodeGenerator
81codegenJs conf ci =
90codegenJs conf ci =
83− optim <- isYes <$> lookupEnv "IDRISJS_OPTIM"
84 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
92 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
86− if optim then putStrLn "compiling width idris-js optimizations"
87− else putStrLn "compiling widthout idris-js optimizations"
89 let defs = Map.fromList $ liftDecls ci
93 let defs = Map.fromList $ liftDecls ci
90 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
94 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
91 if debug then
95 if debug then
@@ -105,15 +109,20 @@ codegenJs conf ci =
105 extraRT <- TIO.readFile $ path </> (extraRunTime conf)
109 extraRT <- TIO.readFile $ path </> (extraRunTime conf)
107 includes <- getIncludes $ includes ci
111 includes <- getIncludes $ includes ci
112+ let libs = T.pack $ includeLibs $ compileLibs ci
108 TIO.writeFile (outputFile ci) $ T.concat [ header conf
113 TIO.writeFile (outputFile ci) $ T.concat [ header conf
109 , "\"use strict\";\n\n"
114 , "\"use strict\";\n\n"
110 , "(function(){\n\n"
115 , "(function(){\n\n"
111 , runtimeCommon, "\n"
117 , runtimeCommon, "\n"
112 , extraRT, "\n"
118 , extraRT, "\n"
113 , jsbn, "\n"
119 , jsbn, "\n"
120+ -- external libraries
114 , doPartials (partialApplications stats), "\n"
124 , doPartials (partialApplications stats), "\n"
115 , doHiddenClasses (hiddenClasses stats), "\n"
125 , doHiddenClasses (hiddenClasses stats), "\n"
117 , out, "\n"
126 , out, "\n"
118 , jsName (sMN 0 "runMain"), "();\n"
127 , jsName (sMN 0 "runMain"), "();\n"
119 , "}.call(this))"
128 , "}.call(this))"
@@ -135,6 +144,7 @@ doHiddenClasses :: Set HiddenClass -> Text
135doHiddenClasses x =
144doHiddenClasses x =
136 T.intercalate "\n" (map f $ Set.toList x)
145 T.intercalate "\n" (map f $ Set.toList x)
147+ f p@(HiddenClass n id 0) = jsStmt2Text $ JsDecConst (jsNameHiddenClass p) $ JsObj [("type", JsInt id)]
138 f p@(HiddenClass n id arity) =
148 f p@(HiddenClass n id arity) =
139 let vars = map dataPartName $ take arity [1..]
149 let vars = map dataPartName $ take arity [1..]
140 in jsStmt2Text $
150 in jsStmt2Text $
@@ -349,27 +359,9 @@ cgBody' rt (LApp tailcall (LV (Glob fn)) args) =
349 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
359 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
350 addUsedArgsTailCallOptim y3
360 addUsedArgsTailCallOptim y3
351 pure (preDecs, y1 `JsSeq` y2)
361 pure (preDecs, y1 `JsSeq` y2)
353− do
363+ app <- formApp fn argVals
354− argsFn <- getArgList fn
358− pure (preDecs, addRT rt $ jsCurryApp fn' argVals )
361− let lenAgFn = length agFn
362− let lenArgs = length args
363− case compare lenAgFn lenArgs of
365− pure (preDecs, addRT rt $ jsAppN fname argVals)
364+ pure (preDecs, addRT rt app)
367− pure (preDecs, addRT rt $ jsCurryApp (jsAppN fname (take lenAgFn argVals)) (drop lenAgFn argVals) )
370− let part = Partial fn lenArgs lenAgFn
372− pure (preDecs, addRT rt $ jsAppN (jsNamePartial part) argVals )
374cgBody' rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
366cgBody' rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
375cgBody' rt (LLazyApp n args) =
367cgBody' rt (LLazyApp n args) =
@@ -447,18 +439,36 @@ altHasNoProj :: LAlt -> Bool
447altHasNoProj (LConCase _ _ args _) = args == []
439altHasNoProj (LConCase _ _ args _) = args == []
448altHasNoProj _ = True
440altHasNoProj _ = True
442+formApp :: Name -> [JsExpr] -> State CGBodyState JsExpr
443+formApp fn argVals = case specialCall fn of
444+ Just (arity, g) | arity == length argVals -> pure $ g argVals
446+ argsFn <- getArgList fn
449+ Nothing -> pure $ jsCurryApp fname argVals
451+ let lenAgFn = length agFn
452+ let lenArgs = length argVals
453+ case compare lenAgFn lenArgs of
454+ EQ -> pure $ JsApp fname argVals
455+ LT -> pure $ jsCurryApp (JsApp fname (take lenAgFn argVals)) (drop lenAgFn argVals)
457+ let part = Partial fn lenArgs lenAgFn
459+ pure $ jsAppN (jsNamePartial part) argVals
450formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
461formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
451formCon n args = do
462formCon n args = do
452 case specialCased n of
463 case specialCased n of
453 Just (ctor, test, match) -> pure $ ctor args
464 Just (ctor, test, match) -> pure $ ctor args
454 Nothing -> do
465 Nothing -> do
455 (conId, arity) <- getConsId n
466 (conId, arity) <- getConsId n
458− let hc = HiddenClass n conId arity
467+ let hc = HiddenClass n conId arity
459− addHiddenClass hc
468+ addHiddenClass hc
469+ pure $ if (arity > 0)
460− pure $ JsNew (JsVar $ jsNameHiddenClass hc) args
470+ then JsNew (JsVar $ jsNameHiddenClass hc) args
461− else pure $ JsInt conId
471+ else JsVar $ jsNameHiddenClass hc
463formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
473formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
464formConTest n x = do
474formConTest n x = do
@@ -466,9 +476,10 @@ formConTest n x = do
466 Just (ctor, test, match) -> pure $ test x
476 Just (ctor, test, match) -> pure $ test x
467 Nothing -> do
477 Nothing -> do
468 (conId, arity) <- getConsId n
478 (conId, arity) <- getConsId n
479+ pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
469− if (arity > 0)
480+ -- if (arity > 0)
470− then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
481+ -- then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
471− else pure $ JsBinOp "===" x (JsInt conId)
482+ -- else pure $ JsBinOp "===" x (JsInt conId)
473formProj :: Name -> JsExpr -> Int -> JsExpr
484formProj :: Name -> JsExpr -> Int -> JsExpr
474formProj n v i =
485formProj n v i =