Skip to content

Commit c00896a

Browse files
committed
Make the direct fn call code stop crashing
1 parent 7bf4e11 commit c00896a

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

compiler/src/Generate/JavaScript.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,10 @@ makeArgLookup graph home name =
652652
Just (length args)
653653

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

657660
_ ->
658661
Nothing

test/Test/JsOutput.hs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,101 @@ suite =
100100
expectTextContains jsOutput "n1 = 0"
101101
expectTextContains jsOutput "n2 = $author$project$Main$N2$(0, 0)"
102102

103+
Nothing ->
104+
crash "JS output could not be read."
105+
, scope "direct function calls - mutual recursion" $ do
106+
project <- io $ Lamdera.Relative.requireDir "test/direct-fn-calls-mutual-recursion"
107+
let
108+
elmHome = project ++ "/elm-home"
109+
elmStuff = project ++ "/elm-stuff"
110+
111+
maybeJsOutput <- io $ do
112+
rmdir elmHome
113+
rmdir elmStuff
114+
115+
Test.Helpers.withElmHome elmHome $
116+
Ext.Common.withProjectRoot project $
117+
Make.run ["src/Main.elm"] $
118+
Make.Flags
119+
{ _debug = False
120+
, _optimize = True
121+
, _output = Just (Make.JS "elm-stuff/tmp.js")
122+
, _report = Nothing
123+
, _docs = Nothing
124+
, _noWire = True
125+
, _optimizeLegible = False
126+
}
127+
128+
fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"
129+
130+
rmdir elmHome
131+
rmdir elmStuff
132+
133+
pure fileContents
134+
135+
case maybeJsOutput of
136+
Just jsOutput ->
137+
do
138+
expectTextContains jsOutput "$Main$a2 = function (n) {"
139+
expectTextContains jsOutput "$Main$cyclic$a1() {"
140+
expectTextContains jsOutput "$Main$a1 ="
141+
expectTextContains jsOutput "$Main$cyclic$a1 = function () {"
142+
expectTextContains jsOutput "$Main$a1(1)"
143+
expectTextContains jsOutput "$Main$a2(1)"
144+
145+
expectTextContains jsOutput "$Main$b2$ = function (m, n) {"
146+
expectTextContains jsOutput "$Main$cyclic$b1()"
147+
expectTextContains jsOutput "$Main$b2 = F2("
148+
expectTextContains jsOutput "$Main$cyclic$b1 = "
149+
expectTextContains jsOutput "$Main$b1 ="
150+
expectTextContains jsOutput "$Main$cyclic$b1 = function () {"
151+
expectTextContains jsOutput "$Main$b1, 1, 1)" -- A2
152+
expectTextContains jsOutput "$Main$b2$(1, 1)"
153+
154+
Nothing ->
155+
crash "JS output could not be read."
156+
, scope "direct function calls - mutual recursion with partial application" $ do
157+
project <- io $ Lamdera.Relative.requireDir "test/direct-fn-calls-mutual-recursion-partial-application"
158+
let
159+
elmHome = project ++ "/elm-home"
160+
elmStuff = project ++ "/elm-stuff"
161+
162+
maybeJsOutput <- io $ do
163+
rmdir elmHome
164+
rmdir elmStuff
165+
166+
Test.Helpers.withElmHome elmHome $
167+
Ext.Common.withProjectRoot project $
168+
Make.run ["src/Main.elm"] $
169+
Make.Flags
170+
{ _debug = False
171+
, _optimize = True
172+
, _output = Just (Make.JS "elm-stuff/tmp.js")
173+
, _report = Nothing
174+
, _docs = Nothing
175+
, _noWire = True
176+
, _optimizeLegible = False
177+
}
178+
179+
fileContents <- readUtf8Text $ elmStuff ++ "/tmp.js"
180+
181+
rmdir elmHome
182+
rmdir elmStuff
183+
184+
pure fileContents
185+
186+
case maybeJsOutput of
187+
Just jsOutput ->
188+
do
189+
expectTextContains jsOutput "$Main$a2$ = function (x1, x2, x3, x4, x5) {"
190+
expectTextContains jsOutput "$Main$a2 = F5"
191+
expectTextContains jsOutput "$Main$cyclic$a1() {"
192+
expectTextContains jsOutput "$Main$a2, 1, 2);"
193+
expectTextContains jsOutput "$Main$a1 ="
194+
expectTextContains jsOutput "$Main$cyclic$a1 = function () {"
195+
expectTextContains jsOutput "$Main$a1, 3, 4, 5)" -- A3
196+
expectTextContains jsOutput "$Main$a2$(1, 2, 3, 4, 5)"
197+
103198
Nothing ->
104199
crash "JS output could not be read."
105200
]

0 commit comments

Comments
 (0)