@@ -80,12 +80,7 @@ isYes _ = False
80codegenJs :: CGConf -> CodeGenerator
80codegenJs :: CGConf -> CodeGenerator
81codegenJs conf ci =
81codegenJs conf ci =
83− optim <- isYes <$> lookupEnv "IDRISJS_OPTIM"
84 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
83 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
84 let defs = Map.fromList $ liftDecls ci
90 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
85 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
91 if debug then
86 if debug then
@@ -108,12 +103,15 @@ codegenJs conf ci =
108 TIO.writeFile (outputFile ci) $ T.concat [ header conf
103 TIO.writeFile (outputFile ci) $ T.concat [ header conf
109 , "\"use strict\";\n\n"
104 , "\"use strict\";\n\n"
110 , "(function(){\n\n"
105 , "(function(){\n\n"
111 , runtimeCommon, "\n"
107 , runtimeCommon, "\n"
112 , extraRT, "\n"
108 , extraRT, "\n"
113 , jsbn, "\n"
109 , jsbn, "\n"
110+ -- external libraries
114 , doPartials (partialApplications stats), "\n"
113 , doPartials (partialApplications stats), "\n"
115 , doHiddenClasses (hiddenClasses stats), "\n"
114 , doHiddenClasses (hiddenClasses stats), "\n"
117 , out, "\n"
115 , out, "\n"
118 , jsName (sMN 0 "runMain"), "();\n"
116 , jsName (sMN 0 "runMain"), "();\n"
119 , "}.call(this))"
117 , "}.call(this))"
@@ -135,6 +133,7 @@ doHiddenClasses :: Set HiddenClass -> Text
135doHiddenClasses x =
133doHiddenClasses x =
136 T.intercalate "\n" (map f $ Set.toList x)
134 T.intercalate "\n" (map f $ Set.toList x)
136+ f p@(HiddenClass n id 0) = jsStmt2Text $ JsDecConst (jsNameHiddenClass p) $ JsObj [("type", JsInt id)]
138 f p@(HiddenClass n id arity) =
137 f p@(HiddenClass n id arity) =
139 let vars = map dataPartName $ take arity [1..]
138 let vars = map dataPartName $ take arity [1..]
140 in jsStmt2Text $
139 in jsStmt2Text $
@@ -349,27 +348,9 @@ cgBody' rt (LApp tailcall (LV (Glob fn)) args) =
349 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
348 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
350 addUsedArgsTailCallOptim y3
349 addUsedArgsTailCallOptim y3
351 pure (preDecs, y1 `JsSeq` y2)
350 pure (preDecs, y1 `JsSeq` y2)
353− do
352+ 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)
353+ 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)
355cgBody' rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
375cgBody' rt (LLazyApp n args) =
356cgBody' rt (LLazyApp n args) =
@@ -447,18 +428,36 @@ altHasNoProj :: LAlt -> Bool
447altHasNoProj (LConCase _ _ args _) = args == []
428altHasNoProj (LConCase _ _ args _) = args == []
448altHasNoProj _ = True
429altHasNoProj _ = True
431+formApp :: Name -> [JsExpr] -> State CGBodyState JsExpr
432+formApp fn argVals = case specialCall fn of
433+ Just (arity, g) | arity == length argVals -> pure $ g argVals
435+ argsFn <- getArgList fn
438+ Nothing -> pure $ jsCurryApp fname argVals
440+ let lenAgFn = length agFn
441+ let lenArgs = length argVals
442+ case compare lenAgFn lenArgs of
443+ EQ -> pure $ JsApp fname argVals
444+ LT -> pure $ jsCurryApp (JsApp fname (take lenAgFn argVals)) (drop lenAgFn argVals)
446+ let part = Partial fn lenArgs lenAgFn
448+ pure $ jsAppN (jsNamePartial part) argVals
450formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
450formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
451formCon n args = do
451formCon n args = do
452 case specialCased n of
452 case specialCased n of
453 Just (ctor, test, match) -> pure $ ctor args
453 Just (ctor, test, match) -> pure $ ctor args
454 Nothing -> do
454 Nothing -> do
455 (conId, arity) <- getConsId n
455 (conId, arity) <- getConsId n
458− let hc = HiddenClass n conId arity
456+ let hc = HiddenClass n conId arity
459− addHiddenClass hc
457+ addHiddenClass hc
458+ pure $ if (arity > 0)
460− pure $ JsNew (JsVar $ jsNameHiddenClass hc) args
459+ then JsNew (JsVar $ jsNameHiddenClass hc) args
461− else pure $ JsInt conId
460+ else JsVar $ jsNameHiddenClass hc
463formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
462formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
464formConTest n x = do
463formConTest n x = do
@@ -466,9 +465,10 @@ formConTest n x = do
466 Just (ctor, test, match) -> pure $ test x
465 Just (ctor, test, match) -> pure $ test x
467 Nothing -> do
466 Nothing -> do
468 (conId, arity) <- getConsId n
467 (conId, arity) <- getConsId n
468+ pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
469− if (arity > 0)
469+ -- if (arity > 0)
470− then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
470+ -- then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
471− else pure $ JsBinOp "===" x (JsInt conId)
471+ -- else pure $ JsBinOp "===" x (JsInt conId)
473formProj :: Name -> JsExpr -> Int -> JsExpr
473formProj :: Name -> JsExpr -> Int -> JsExpr
474formProj n v i =
474formProj n v i =