@@ -89,12 +89,7 @@ isYes _ = False
89codegenJs :: CGConf -> CodeGenerator
89codegenJs :: CGConf -> CodeGenerator
90codegenJs conf ci =
90codegenJs conf ci =
92− optim <- isYes <$> lookupEnv "IDRISJS_OPTIM"
93 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
92 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
95− if optim then putStrLn "compiling width idris-js optimizations"
96− else putStrLn "compiling widthout idris-js optimizations"
98 let defs = Map.fromList $ liftDecls ci
93 let defs = Map.fromList $ liftDecls ci
99 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
94 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
100 if debug then
95 if debug then
@@ -118,13 +113,16 @@ codegenJs conf ci =
118 TIO.writeFile (outputFile ci) $ T.concat [ header conf
113 TIO.writeFile (outputFile ci) $ T.concat [ header conf
119 , "\"use strict\";\n\n"
114 , "\"use strict\";\n\n"
120 , "(function(){\n\n"
115 , "(function(){\n\n"
121 , runtimeCommon, "\n"
117 , runtimeCommon, "\n"
122 , extraRT, "\n"
118 , extraRT, "\n"
123 , jsbn, "\n"
119 , jsbn, "\n"
124− , doPartials (partialApplications stats), "\n"
120+ -- external libraries
125− , doHiddenClasses (hiddenClasses stats), "\n"
126 , includes, "\n"
121 , includes, "\n"
127 , libs, "\n"
122 , libs, "\n"
124+ , doPartials (partialApplications stats), "\n"
125+ , doHiddenClasses (hiddenClasses stats), "\n"
128 , out, "\n"
126 , out, "\n"
129 , jsName (sMN 0 "runMain"), "();\n"
127 , jsName (sMN 0 "runMain"), "();\n"
130 , "}.call(this))"
128 , "}.call(this))"
@@ -146,6 +144,7 @@ doHiddenClasses :: Set HiddenClass -> Text
146doHiddenClasses x =
144doHiddenClasses x =
147 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)]
149 f p@(HiddenClass n id arity) =
148 f p@(HiddenClass n id arity) =
150 let vars = map dataPartName $ take arity [1..]
149 let vars = map dataPartName $ take arity [1..]
151 in jsStmt2Text $
150 in jsStmt2Text $
@@ -360,27 +359,9 @@ cgBody' rt (LApp tailcall (LV (Glob fn)) args) =
360 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
359 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
361 addUsedArgsTailCallOptim y3
360 addUsedArgsTailCallOptim y3
362 pure (preDecs, y1 `JsSeq` y2)
361 pure (preDecs, y1 `JsSeq` y2)
364− do
363+ app <- formApp fn argVals
365− argsFn <- getArgList fn
369− pure (preDecs, addRT rt $ jsCurryApp fn' argVals )
372− let lenAgFn = length agFn
373− let lenArgs = length args
374− case compare lenAgFn lenArgs of
376− pure (preDecs, addRT rt $ jsAppN fname argVals)
364+ pure (preDecs, addRT rt app)
378− pure (preDecs, addRT rt $ jsCurryApp (jsAppN fname (take lenAgFn argVals)) (drop lenAgFn argVals) )
381− let part = Partial fn lenArgs lenAgFn
383− pure (preDecs, addRT rt $ jsAppN (jsNamePartial part) argVals )
385cgBody' 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)
386cgBody' rt (LLazyApp n args) =
367cgBody' rt (LLazyApp n args) =
@@ -458,18 +439,36 @@ altHasNoProj :: LAlt -> Bool
458altHasNoProj (LConCase _ _ args _) = args == []
439altHasNoProj (LConCase _ _ args _) = args == []
459altHasNoProj _ = 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
461formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
461formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
462formCon n args = do
462formCon n args = do
463 case specialCased n of
463 case specialCased n of
464 Just (ctor, test, match) -> pure $ ctor args
464 Just (ctor, test, match) -> pure $ ctor args
465 Nothing -> do
465 Nothing -> do
466 (conId, arity) <- getConsId n
466 (conId, arity) <- getConsId n
469− let hc = HiddenClass n conId arity
467+ let hc = HiddenClass n conId arity
470− addHiddenClass hc
468+ addHiddenClass hc
469+ pure $ if (arity > 0)
471− pure $ JsNew (JsVar $ jsNameHiddenClass hc) args
470+ then JsNew (JsVar $ jsNameHiddenClass hc) args
472− else pure $ JsInt conId
471+ else JsVar $ jsNameHiddenClass hc
474formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
473formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
475formConTest n x = do
474formConTest n x = do
@@ -477,9 +476,10 @@ formConTest n x = do
477 Just (ctor, test, match) -> pure $ test x
476 Just (ctor, test, match) -> pure $ test x
478 Nothing -> do
477 Nothing -> do
479 (conId, arity) <- getConsId n
478 (conId, arity) <- getConsId n
479+ pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
480− if (arity > 0)
480+ -- if (arity > 0)
481− then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
481+ -- then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
482− else pure $ JsBinOp "===" x (JsInt conId)
482+ -- else pure $ JsBinOp "===" x (JsInt conId)
484formProj :: Name -> JsExpr -> Int -> JsExpr
484formProj :: Name -> JsExpr -> Int -> JsExpr
485formProj n v i =
485formProj n v i =