forked from gonengazit/Celia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepcopy.lua
218 lines (202 loc) · 6.55 KB
/
deepcopy.lua
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
local love11_4,table_new = pcall(require, "table.new")
if not love11_4 then
--LOVE 11.3 compatibility hack
table_new=function() return {} end
end
if not debug.upvalueid or not debug.upvaluejoin then
print("debug.upvalueid or debug.upvaluejoin are not available; expect breakage with rewinding")
end
local handle_funcs = {
["nil"] = function(orig) return orig end,
number = function(orig) return orig end,
string = function(orig) return orig end,
boolean = function(orig) return orig end,
table = function(orig,seen,upvalues)
local see=seen[orig]
if see then
return see
end
local ret=table_new(#orig,0)
seen[orig]=ret
for k,v in pairs(orig) do
if k == "__tas_id" then
ret[k]=v
else
rawset(ret,deepcopy(k,seen,upvalues),deepcopy(v,seen,upvalues))
end
end
setmetatable(ret, deepcopy(getmetatable(orig), seen, upvalues))
return ret
end,
["function"] = function(orig,seen,upvalues)
local see=seen[orig]
if see then
return see
end
local ret = loadstring(string.dump(orig))
seen[orig] = ret
-- there are at most 255 upvalues
setfenv(ret,deepcopy(getfenv(orig),seen,upvalues))
for i=1,255 do
local name,val= debug.getupvalue(orig,i)
if name == nil then
break
end
debug.setupvalue(ret,i,deepcopy(val,seen,upvalues))
if debug.upvalueid and debug.upvaluejoin then
local uid = debug.upvalueid(orig, i)
if upvalues[uid] then
local other_func, other_i = unpack(upvalues[uid])
debug.upvaluejoin(ret, i , other_func, other_i)
else
upvalues[uid] = {ret, i}
end
end
end
return ret
end,
userdata = function(orig,seen)
local see=seen[orig]
if see then
return see
end
if getmetatable(orig).type and orig:type()=="Canvas" then
local ret=love.graphics.newCanvas(orig:getDimensions())
ret:renderTo(function()
love.graphics.setShader()
love.graphics.origin()
love.graphics.setScissor()
love.graphics.setColor(1,1,1)
love.graphics.draw(orig, 0, 0)
end)
seen[orig]=ret
return ret
else
seen[orig]=orig
return orig
end
end
}
local err_function = function (orig) error(("can't copy type %q"):format(type(orig))) end
function deepcopy(orig, seen, upvalues)
return (handle_funcs[type(orig) or err_function]) (orig,seen or {}, upvalues or {})
end
local function deepcopy_debug(orig, seen, upvalues, path)
--path is used for debugging purposes
--can delete it to improve performance
seen = seen or {}
upvalues = upvalues or {}
path = path or {}
if seen[orig] then
return seen[orig]
end
if type(orig) == "nil" or type(orig) == "number" or
type(orig) == "string" or type(orig) == "boolean" then
return orig
elseif type(orig)=="table" then
print(table.concat(path,"."))
local ret={}
seen[orig]=ret
for k,v in pairs(orig) do
-- print(k)
-- print(deepcopy(v,seen,upvalues))
if k == "__tas_id" then
ret[k]=v
else
table.insert(path,k)
rawset(ret,deepcopy_debug(k,seen,upvalues,{"key "..tostring(k)}),deepcopy_debug(v,seen,upvalues,path))
table.remove(path)
end
end
setmetatable(ret, deepcopy_debug(getmetatable(orig), seen, upvalues))
return ret
elseif type(orig)=="function" then
print(table.concat(path,"."))
local ret = loadstring(string.dump(orig))
seen[orig] = ret
-- there are at most 255 upvalues
table.insert(path,"_ENV")
setfenv(ret,deepcopy_debug(getfenv(orig),seen,upvalues,path))
table.remove(path)
for i=1,255 do
local name,val= debug.getupvalue(orig,i)
if name == nil then
break
end
table.insert(path,"up_"..tostring(name))
debug.setupvalue(ret,i,deepcopy_debug(val,seen,upvalues,path))
table.remove(path)
local uid = debug.upvalueid(orig, i)
if upvalues[uid] then
local other_func, other_i = unpack(upvalues[uid])
debug.upvaluejoin(ret, i , other_func, other_i)
else
upvalues[uid] = {ret, i}
end
end
return ret
elseif type(orig)=="userdata" then
print(table.concat(path,"."))
if getmetatable(orig).type and orig:type()=="Canvas" then
local ret=love.graphics.newCanvas(orig:getDimensions())
ret:renderTo(function()
love.graphics.setShader()
love.graphics.origin()
love.graphics.setScissor()
love.graphics.setColor(1,1,1)
love.graphics.draw(orig, 0, 0)
end)
return ret
else
return orig
end
else
error(("can't copy type %q"):format(type(orig)))
end
end
function deepcopy_no_api(orig)
local nocopy = {[_G]=_G}
for _,v in pairs(new_sandbox()) do
nocopy[v]=v
end
local ret={}
local upvalues={}
for k,v in pairs(orig) do
-- don't copy sfx or rom, because (currently) they are immutaable
-- sfx might still be an issue, but a small one
if k=='sfx' or k=='rom' then
ret[k]=v
nocopy[v]=v
else
ret[k]=deepcopy(orig[k],nocopy,upvalues)
end
end
return ret
end
function deepcopy_benchmark(v)
local nocopy = {}
for _,v in pairs(new_sandbox()) do
nocopy[v]=v
end
nocopy[_G]=_G
-- nocopy[pico8.sfx]=pico8.sfx
local t=love.timer.getTime()
local ret={}
local times={}
for k,_ in pairs(v) do
local t=love.timer.getTime()
ret[k]= deepcopy(v[k], nocopy)
-- print(k.." "..love.timer.getTime()-t)
table.insert(times,{love.timer.getTime()-t,k})
end
table.sort(times,function(a,b) return a[1]<b[1] end)
local s=0
local total=love.timer.getTime()-t
for k,v in ipairs(times) do
print(v[1].." "..v[2])
s=s+v[1]
end
print("total ".. total .." "..s)
print()
return ret
end