forked from Unisay/purescript-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixture.hs
More file actions
73 lines (63 loc) · 1.83 KB
/
Fixture.hs
File metadata and controls
73 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{-# LANGUAGE QuasiQuotes #-}
module Language.PureScript.Backend.Lua.Fixture where
import Data.String.Interpolate (__i)
import Language.PureScript.Backend.Lua.Name (Name, name, unsafeName)
import Language.PureScript.Backend.Lua.Name qualified as Name
import Language.PureScript.Backend.Lua.Types hiding (var)
--------------------------------------------------------------------------------
-- Hard-coded Lua pieces -------------------------------------------------------
uniqueName ∷ MonadState Natural m ⇒ Text → m Name
uniqueName prefix = do
index ← get
modify' (+ 1)
pure $ unsafeName (prefix <> show index)
psluaName ∷ Name → Name
psluaName = Name.join2 [name|PSLUA|]
moduleName ∷ Name.Name
moduleName = [name|M|]
runtimeLazyName ∷ Name
runtimeLazyName = psluaName [name|runtime_lazy|]
runtimeLazy ∷ Statement
runtimeLazy =
ForeignSourceStat
[__i|
local function #{Name.toText runtimeLazyName}(name)
return function(init)
return function()
local state = 0
local val = nil
if state == 2 then
return val
else
if state == 1 then
return error(name .. " was needed before it finished initializing")
else
state = 1
val = init()
state = 2
return val
end
end
end
end
end
|]
objectUpdateName ∷ Name
objectUpdateName = psluaName [name|object_update|]
objectUpdate ∷ Statement
objectUpdate =
ForeignSourceStat
[__i|
local function #{Name.toText objectUpdateName}(o, patches)
local o_copy = {}
for k, v in pairs(o) do
local patch_v = patches[k]
if patch_v ~= nil then
o_copy[k] = patch_v
else
o_copy[k] = v
end
end
return o_copy
end
|]