Skip to content

Commit

Permalink
[ cleanup ] Do not wrap the mainExpression into lazy in ES backends
Browse files Browse the repository at this point in the history
  • Loading branch information
buzden committed Feb 12, 2024
1 parent 2e1c3fb commit 693df52
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Compiler/ES/Codegen.idr
Original file line number Diff line number Diff line change
Expand Up @@ -749,14 +749,14 @@ def (MkFunction n as body) = do
mde <- mode <$> get ESs
b <- stmt Returns body >>= stmt
let cmt = comment $ hsep (shown n :: toList ((":" <++>) <$> mty))
case args of
if null args && n /= mainExpr
-- zero argument toplevel functions are converted to
-- lazily evaluated constants.
[] => pure $ printDoc mde $ vcat
-- lazily evaluated constants (except the main expression).
then pure $ printDoc mde $ vcat
[ cmt
, constant (var !(get NoMangleMap) ref)
("__lazy(" <+> function neutral [] b <+> ")") ]
_ => pure $ printDoc mde $ vcat
else pure $ printDoc mde $ vcat
[ cmt
, function (var !(get NoMangleMap) ref)
(map (var !(get NoMangleMap)) args) b ]
Expand Down
4 changes: 4 additions & 0 deletions tests/node/node028/HelloWorld.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module HelloWorld

main : IO ()
main = putStrLn "Hello, world!"
7 changes: 7 additions & 0 deletions tests/node/node028/LazyIsStillThere.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module LazyIsStillThere

topLevelConst : Nat
topLevelConst = 16 + 8

main : IO ()
main = putStrLn "Top-level constant: \{show topLevelConst}"
15 changes: 15 additions & 0 deletions tests/node/node028/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Top-level constants are lazily evaluated and strongly memoised.
# This is implemented by wrapping them to the function called `__lazy`.
# The only top-level function that should not be treated so is the expression for `main : IO ()`.
# In this test we check this.
--------------
# Running an example without any top-level constants...
# We expect no usages of `__lazy` to be present, maybe only a definition.
Hello, world!
function __lazy(thunk) {
--------------
# Running an example with some top-level constant...
# We expect `__lazy` to be used on the RHS for the top-level constant called `topLevelConst`.
Top-level constant: 24
function __lazy(thunk) {
const LazyIsStillThere_topLevelConst = __lazy(function () {
20 changes: 20 additions & 0 deletions tests/node/node028/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
. ../../testutils.sh

echo '# Top-level constants are lazily evaluated and strongly memoised.'
echo '# This is implemented by wrapping them to the function called `__lazy`.'
echo '# The only top-level function that should not be treated so is the expression for `main : IO ()`.'
echo '# In this test we check this.'

echo '--------------'

echo '# Running an example without any top-level constants...'
echo '# We expect no usages of `__lazy` to be present, maybe only a definition.'
run --cg node -o hw.js HelloWorld.idr
grep '__lazy' build/exec/hw.js

echo '--------------'

echo '# Running an example with some top-level constant...'
echo '# We expect `__lazy` to be used on the RHS for the top-level constant called `topLevelConst`.'
run --cg node -o lsth.js LazyIsStillThere.idr
grep '__lazy' build/exec/lsth.js

0 comments on commit 693df52

Please sign in to comment.