Skip to content

Commit 47faca3

Browse files
committed
Fix linking of apps
1 parent 46ace58 commit 47faca3

File tree

13 files changed

+373
-320
lines changed

13 files changed

+373
-320
lines changed

exe/Main.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Language.PureScript.Backend.IR qualified as IR
99
import Language.PureScript.Backend.Lua qualified as Lua
1010
import Language.PureScript.Backend.Lua.Printer qualified as Printer
1111
import Language.PureScript.CoreFn.Reader qualified as CoreFn
12-
import Language.PureScript.Names (runModuleName)
12+
import Language.PureScript.Names (runIdent, runModuleName)
1313
import Main.Utf8 qualified as Utf8
1414
import Path (Abs, Dir, Path, SomeBase (..), toFilePath)
1515
import Path.IO qualified as Path
@@ -96,3 +96,9 @@ handleLuaError =
9696
]
9797
Lua.LinkerErrorForeign e
9898
die $ "Linker error:\n" <> show e
99+
Lua.AppEntryPointNotFound modname ident
100+
die . toString $
101+
"App entry point not found: "
102+
<> runModuleName modname
103+
<> "."
104+
<> runIdent ident

lib/Language/PureScript/Backend/Lua.hs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module Language.PureScript.Backend.Lua
1010
import Control.Monad (ap)
1111
import Control.Monad.Oops (CouldBe, Variant)
1212
import Control.Monad.Oops qualified as Oops
13-
import Data.DList (DList)
1413
import Data.DList qualified as DList
14+
import Data.List qualified as List
1515
import Data.Set qualified as Set
1616
import Data.Tagged (Tagged (..), untag)
1717
import Data.Text qualified as Text
@@ -28,6 +28,7 @@ import Language.PureScript.Backend.Lua.Types (ParamF (..))
2828
import Language.PureScript.Backend.Lua.Types qualified as Lua
2929
import Language.PureScript.Backend.Types (AppOrModule (..))
3030
import Language.PureScript.Names (ModuleName, runModuleName)
31+
import Language.PureScript.Names qualified as PS
3132
import Path (Abs, Dir, Path, toFilePath)
3233
import Prelude hiding (exp, local)
3334

@@ -36,6 +37,7 @@ type LuaM e a = StateT Natural (ExceptT (Variant e) IO) a
3637
data Error
3738
= UnexpectedRefBound ModuleName IR.Exp
3839
| LinkerErrorForeign Foreign.Error
40+
| AppEntryPointNotFound ModuleName PS.Ident
3941
deriving stock (Show)
4042

4143
fromUberModule
@@ -52,18 +54,28 @@ fromUberModule foreigns needsRuntimeLazy appOrModule uber = (`evalStateT` 0) do
5254
IR.Standalone (IR.QName modname name, irExp) do
5355
exp fromExp foreigns Set.empty modname irExp
5456
pure $ DList.singleton (Lua.local1 (fromQName modname name) exp)
55-
IR.RecursiveGroup binds
56-
recBindStatements <$> forM binds \(IR.QName modname name, irExp)
57+
IR.RecursiveGroup recGroup do
58+
recBinds forM (toList recGroup) \(IR.QName modname name, irExp)
5759
(fromQName modname name,) <$> fromExp foreigns Set.empty modname irExp
60+
let declarations = Lua.local0 . fst <$> DList.fromList recBinds
61+
assignments = DList.fromList do
62+
recBinds <&> \(name, exp) Lua.assign (Lua.VarName name) exp
63+
pure $ declarations <> assignments
5864

5965
returnExp
6066
case appOrModule of
6167
AsModule modname
6268
Lua.table <$> do
6369
forM (uberModuleExports uber) \(fromName name, expr)
6470
Lua.tableRowNV name <$> fromExp foreigns mempty modname expr
65-
AsApplication modname (IR.identToName name)
66-
pure $ Lua.functionCall (Lua.varName (fromQName modname name)) []
71+
AsApplication modname ident do
72+
case List.lookup name (uberModuleExports uber) of
73+
Just expr do
74+
entry fromExp foreigns mempty modname expr
75+
pure $ Lua.functionCall entry []
76+
_ Oops.throw $ AppEntryPointNotFound modname ident
77+
where
78+
name = IR.identToName ident
6779

6880
pure . mconcat $
6981
[ if usesPrimModule uber then [Fixture.prim] else empty
@@ -202,14 +214,6 @@ fromExp foreigns topLevelNames modname ir = case ir of
202214
keyCtor Lua.Exp
203215
keyCtor = Lua.String "$ctor"
204216

205-
recBindStatements NonEmpty (Lua.Name, Lua.Exp) DList Lua.Statement
206-
recBindStatements (toList binds) = fmap Lua.local0 names <> assigns
207-
where
208-
names DList Lua.Name =
209-
DList.fromList (fst <$> binds)
210-
assigns DList Lua.Statement =
211-
DList.fromList (binds <&> \(name, exp) Lua.assign (Lua.VarName name) exp)
212-
213217
fromIfThenElse Lua.Exp Lua.Exp Lua.Exp Lua.Exp
214218
fromIfThenElse cond thenExp elseExp = Lua.functionCall fun []
215219
where

lib/Language/PureScript/Backend/Lua/Optimizer.hs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import Language.PureScript.Backend.Lua.Types qualified as Lua
2525
import Prelude hiding (return)
2626

2727
optimizeChunk Chunk Chunk
28-
optimizeChunk = identity
29-
30-
-- fmap optimizeStatement
31-
-- >>> inlineTopLevelLocalDefs
28+
optimizeChunk = fmap optimizeStatement >>> inlineTopLevelLocalDefs
3229

3330
inlineTopLevelLocalDefs Chunk Chunk
3431
inlineTopLevelLocalDefs = snd . foldr inlineTopLevelLocalDef mempty

test/ps/output/Golden.TestCaseStatements/golden.lua

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
local Golden_TestValues_I_f = function(unused0) return true end
21
return {
32
a = 1,
43
b = "b",
54
c = (function()
65
local v = function(unused1) return 0 end
76
return (function()
8-
if true == Golden_TestValues_I_f(2) then
7+
if true == (function(unused0) return true end)(2) then
98
return (function()
10-
if true == Golden_TestValues_I_f(1) then
9+
if true == (function(unused0) return true end)(1) then
1110
return 42
1211
else
1312
return v(true)
@@ -25,31 +24,27 @@ return {
2524
d = function(m)
2625
return function(n)
2726
return function(x)
27+
local v = function(unused2)
28+
if "y" == x then return 0 else return 1 end
29+
end
2830
return (function()
29-
local v = function(unused2)
31+
if "x" == x then
3032
return (function()
31-
if "y" == x then return 0 else return 1 end
33+
if "Golden.TestCaseStatements.J" == m["$ctor"] then
34+
return (function()
35+
if "Golden.TestCaseStatements.N" == n["$ctor"] then
36+
return m[0]
37+
else
38+
return v(true)
39+
end
40+
end)()
41+
else
42+
return v(true)
43+
end
3244
end)()
45+
else
46+
return v(true)
3347
end
34-
return (function()
35-
if "x" == x then
36-
return (function()
37-
if "Golden.TestCaseStatements.J" == m["$ctor"] then
38-
return (function()
39-
if "Golden.TestCaseStatements.N" == n["$ctor"] then
40-
return m[0]
41-
else
42-
return v(true)
43-
end
44-
end)()
45-
else
46-
return v(true)
47-
end
48-
end)()
49-
else
50-
return v(true)
51-
end
52-
end)()
5348
end)()
5449
end
5550
end

test/ps/output/Golden.TestDataDeclarations2/golden.lua

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@ return {
44
end,
55
test = function(v)
66
return function(v1)
7-
return (function()
8-
if "Golden.TestDataDeclarations1.CtorSameName" == v["$ctor"] then
9-
return (function()
10-
if "Golden.TestDataDeclarations2.CtorSameName" == v1["$ctor"] then
11-
return true
12-
else
13-
return error("No patterns matched")
14-
end
15-
end)()
16-
else
17-
return error("No patterns matched")
18-
end
19-
end)()
7+
if "Golden.TestDataDeclarations1.CtorSameName" == v["$ctor"] then
8+
return (function()
9+
if "Golden.TestDataDeclarations2.CtorSameName" == v1["$ctor"] then
10+
return true
11+
else
12+
return error("No patterns matched")
13+
end
14+
end)()
15+
else
16+
return error("No patterns matched")
17+
end
2018
end
2119
end
2220
}

0 commit comments

Comments
 (0)