Skip to content

Direct fn calls: disable for cyclic calls #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/Generate/JavaScript.hs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,10 @@ makeArgLookup graph home name =
Just (length args)

_ ->
error (show names)
-- This disables direct function calls eg. for mutually recursive
-- functions (with or without partial application). These are
-- technically possible but not implemented here.
Nothing

_ ->
Nothing
Expand Down
95 changes: 95 additions & 0 deletions test/Test/JsOutput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,101 @@ suite =
expectTextContains jsOutput "n1 = 0"
expectTextContains jsOutput "n2 = $author$project$Main$N2$(0, 0)"

Nothing ->
crash "JS output could not be read."
, scope "direct function calls - mutual recursion" $ do
project <- io $ Lamdera.Relative.requireDir "test/direct-fn-calls-mutual-recursion"
let
elmHome = project ++ "/elm-home"
elmStuff = project ++ "/elm-stuff"

maybeJsOutput <- io $ do
rmdir elmHome
rmdir elmStuff

Test.Helpers.withElmHome elmHome $
Ext.Common.withProjectRoot project $
Make.run ["src/Main.elm"] $
Make.Flags
{ _debug = False
, _optimize = True
, _output = Just (Make.JS "elm-stuff/tmp.js")
, _report = Nothing
, _docs = Nothing
, _noWire = True
, _optimizeLegible = False
}

fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"

rmdir elmHome
rmdir elmStuff

pure fileContents

case maybeJsOutput of
Just jsOutput ->
do
expectTextContains jsOutput "$Main$a2 = function (n) {"
expectTextContains jsOutput "$Main$cyclic$a1() {"
expectTextContains jsOutput "$Main$a1 ="
expectTextContains jsOutput "$Main$cyclic$a1 = function () {"
expectTextContains jsOutput "$Main$a1(1)"
expectTextContains jsOutput "$Main$a2(1)"

expectTextContains jsOutput "$Main$b2$ = function (m, n) {"
expectTextContains jsOutput "$Main$cyclic$b1()"
expectTextContains jsOutput "$Main$b2 = F2("
expectTextContains jsOutput "$Main$cyclic$b1 = "
expectTextContains jsOutput "$Main$b1 ="
expectTextContains jsOutput "$Main$cyclic$b1 = function () {"
expectTextContains jsOutput "$Main$b1, 1, 1)" -- A2
expectTextContains jsOutput "$Main$b2$(1, 1)"
Comment on lines +151 to +152
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the test we're showing that the designated "beginning of the cycle" doesn't get called directly (there's no $b1$), but the other parts of the cycle do.


Nothing ->
crash "JS output could not be read."
, scope "direct function calls - mutual recursion with partial application" $ do
project <- io $ Lamdera.Relative.requireDir "test/direct-fn-calls-mutual-recursion-partial-application"
let
elmHome = project ++ "/elm-home"
elmStuff = project ++ "/elm-stuff"

maybeJsOutput <- io $ do
rmdir elmHome
rmdir elmStuff

Test.Helpers.withElmHome elmHome $
Ext.Common.withProjectRoot project $
Make.run ["src/Main.elm"] $
Make.Flags
{ _debug = False
, _optimize = True
, _output = Just (Make.JS "elm-stuff/tmp.js")
, _report = Nothing
, _docs = Nothing
, _noWire = True
, _optimizeLegible = False
}

fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"

rmdir elmHome
rmdir elmStuff

pure fileContents

case maybeJsOutput of
Just jsOutput ->
do
expectTextContains jsOutput "$Main$a2$ = function (x1, x2, x3, x4, x5) {"
expectTextContains jsOutput "$Main$a2 = F5"
expectTextContains jsOutput "$Main$cyclic$a1() {"
expectTextContains jsOutput "$Main$a2, 1, 2);"
expectTextContains jsOutput "$Main$a1 ="
expectTextContains jsOutput "$Main$cyclic$a1 = function () {"
expectTextContains jsOutput "$Main$a1, 3, 4, 5)" -- A3
expectTextContains jsOutput "$Main$a2$(1, 2, 3, 4, 5)"

Nothing ->
crash "JS output could not be read."
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elm-stuff
*.js
29 changes: 29 additions & 0 deletions test/direct-fn-calls-mutual-recursion-partial-application/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/url": "1.0.0",
"lamdera/codecs": "1.0.0",
"lamdera/core": "1.0.0"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.3"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Main exposing (main)

import Html exposing (Html)


main : Html msg
main =
Html.div []
[ Html.text (a1 3 4 5)
, Html.text (a2 1 2 3 4 5)
]


a1 : Int -> Int -> Int -> String
a1 =
a2 1 2


a2 : Int -> Int -> Int -> Int -> Int -> String
a2 x1 x2 x3 x4 x5 =
if True then
"done"

else
a1 3 4 5
2 changes: 2 additions & 0 deletions test/direct-fn-calls-mutual-recursion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
elm-stuff
*.js
29 changes: 29 additions & 0 deletions test/direct-fn-calls-mutual-recursion/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/http": "2.0.0",
"elm/json": "1.1.3",
"elm/url": "1.0.0",
"lamdera/codecs": "1.0.0",
"lamdera/core": "1.0.0"
},
"indirect": {
"elm/bytes": "1.0.8",
"elm/file": "1.0.5",
"elm/time": "1.0.0",
"elm/virtual-dom": "1.0.3"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
41 changes: 41 additions & 0 deletions test/direct-fn-calls-mutual-recursion/src/Main.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module Main exposing (main)

import Html exposing (Html)


main : Html msg
main =
Html.div []
[ Html.text (a1 1)
, Html.text (a2 1)
, Html.text (b1 1 1)
, Html.text (b2 1 1)
]


a1 : Int -> String
a1 =
a2


a2 : Int -> String
a2 n =
if n > 0 then
a1 (n - 1)

else
"done"


b1 : Int -> Int -> String
b1 =
b2


b2 : Int -> Int -> String
b2 m n =
if n > 0 then
b1 (m - 1) (n - 1)

else
"done"