Skip to content

Commit 9447151

Browse files
committedAug 2, 2015
Initial version
0 parents  commit 9447151

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
 

‎.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "test-run"]
2+
path = test-run
3+
url = https://github.com/tarantool/test-run.git

‎package.reload-scm-1.rockspec

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package = 'package.reload'
2+
version = 'scm-1'
3+
source = {
4+
url = 'git://github.com/Mons/tnt-package-reload.git',
5+
branch = 'master',
6+
}
7+
description = {
8+
summary = "Module for unloading previously loaded modules",
9+
homepage = 'https://github.com/Mons/tnt-package-reload.git',
10+
license = 'BSD',
11+
}
12+
dependencies = {
13+
'lua >= 5.1'
14+
}
15+
build = {
16+
type = 'builtin',
17+
modules = {
18+
['package.reload'] = 'package/reload.lua'
19+
}
20+
}
21+
22+
-- vim: syntax=lua

‎package/reload.lua

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
--[[
2+
3+
-- usage --
4+
5+
package.reload:register(object) -> object:destroy()
6+
package.reload:register(cb) -> cb()
7+
8+
package.reload:register(cb,...) -> cb(...)
9+
package.reload:register(ref,cb) -> cb(ref)
10+
11+
]]
12+
13+
local M
14+
local N = ...
15+
local fiber = require 'fiber'
16+
local log = require 'log'
17+
18+
local function numstr( n )
19+
if n == 1 then return '1st'
20+
elseif n == 2 then return '2nd'
21+
elseif n == 3 then return '3rd'
22+
else return tostring(n)..'th'
23+
end
24+
end
25+
26+
package.loaded[N] = nil
27+
28+
if not package.reload then
29+
M = setmetatable({
30+
O = {};
31+
F = {};
32+
C = {};
33+
loaded = {};
34+
count = 1;
35+
script = arg[0];
36+
},{
37+
__tostring = function () return 'package.reload{}' end;
38+
__call = function(m,...)
39+
dofile(M.script)
40+
end;
41+
})
42+
local loaded = {}
43+
for m in pairs(package.loaded) do
44+
M.loaded[m] = package.loaded[m]
45+
table.insert(loaded,m)
46+
end
47+
log.info("1st load. loaded: %s",table.concat(loaded, ", "))
48+
package.reload = M
49+
else
50+
M = package.reload
51+
M:_reload()
52+
end
53+
54+
function M:_reload()
55+
M.count = M.count + 1
56+
local unload = {}
57+
for m in pairs(package.loaded) do
58+
if not M.loaded[m] then
59+
table.insert(unload,m)
60+
package.loaded[m] = nil
61+
end
62+
end
63+
log.info("%s load. Unloading {%s}",numstr(M.count),table.concat(unload, ", "))
64+
M:cleanup()
65+
end
66+
67+
function M:cleanup()
68+
log.info("%s:cleanup...",N)
69+
if self.main then
70+
package.loaded[self.main] = nil
71+
end
72+
collectgarbage()
73+
for ref,cb in pairs(self.O) do
74+
cb(ref)
75+
self.O[ref] = nil
76+
end
77+
for f in pairs(self.F) do
78+
f()
79+
self.F[f] = nil
80+
end
81+
for t in pairs(self.C) do
82+
local cb = t[1]
83+
table.remove(t,1)
84+
cb(unpack(t))
85+
self.C[t] = nil
86+
end
87+
log.info("%s:cleanup finished",N)
88+
collectgarbage()
89+
end
90+
91+
M.deregister = M.cleanup
92+
93+
function M:register(...)
94+
assert( self == box.reload, "Static call" )
95+
if select('#',...) == 1 then
96+
local arg = ...
97+
print("one arg ",type(arg))
98+
if type(arg) == 'table' then
99+
if arg.destroy then
100+
self.O[ arg ] = arg.destroy
101+
else
102+
error("One arg call with object, but have no destroy method")
103+
end
104+
elseif type(arg) == 'function' then
105+
self.F[arg] = arg
106+
else
107+
error("One arg call with unsupported type: ",type(arg))
108+
end
109+
else
110+
local arg1 = ...
111+
if type(arg1) == 'function' then
112+
local t = {...}
113+
self.C[t] = t
114+
elseif select('#',...) == 2 then
115+
local ref,cb = ...
116+
if type(cb) == 'function' then
117+
self.O[ref] = cb
118+
else
119+
error("Bad arguments")
120+
end
121+
else
122+
error("Bad arguments: ", ...)
123+
end
124+
end
125+
end
126+
fiber.create(function()
127+
fiber.sleep(0)
128+
package.loaded[N] = nil
129+
return
130+
end)
131+
return M

‎test-run

Submodule test-run added at 6b4dcd6

0 commit comments

Comments
 (0)
Please sign in to comment.