diff --git a/lualib/skynet/debug.lua b/lualib/skynet/debug.lua index 6f30d4e5..5b7b5afa 100644 --- a/lualib/skynet/debug.lua +++ b/lualib/skynet/debug.lua @@ -47,70 +47,9 @@ function dbgcmd.EXIT() skynet.exit() end -local function getupvaluetable(u, func, unique) - i = 1 - while true do - local name, value = debug.getupvalue(func, i) - if name == nil then - return - end - local t = type(value) - if t == "table" then - u[name] = value - elseif t == "function" then - if not unique[value] then - unique[value] = true - getupvaluetable(u, value, unique) - end - end - i=i+1 - end -end - function dbgcmd.RUN(source, filename) - if filename then - filename = "@" .. filename - else - filename = "=(load)" - end - local output = {} - do - local function print(...) - local value = { ... } - for k,v in ipairs(value) do - value[k] = tostring(v) - end - table.insert(output, table.concat(value, "\t")) - end - local u = {} - local unique = {} - getupvaluetable(u, dispatch_func, unique) - getupvaluetable(u, skynet.register_protocol, unique) - local p = {} - local proto = u.proto - if proto then - for k,v in pairs(proto) do - local name, dispatch = v.name, v.dispatch - if name and dispatch then - local pp = {} - p[name] = pp - getupvaluetable(pp, dispatch, unique) - end - end - end - local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) - local func, err = load(source, filename, "bt", env) - if not func then - skynet.ret(skynet.pack(err)) - return - end - local ok, err = pcall(func) - if not ok then - table.insert(output, err) - end - - source, filename = nil - end + local inject = require "skynet.inject" + local output = inject(source, filename , dispatch_func, skynet.register_protocol) collectgarbage "collect" skynet.ret(skynet.pack(table.concat(output, "\n"))) end diff --git a/lualib/skynet/inject.lua b/lualib/skynet/inject.lua new file mode 100644 index 00000000..68e3954b --- /dev/null +++ b/lualib/skynet/inject.lua @@ -0,0 +1,65 @@ +local function getupvaluetable(u, func, unique) + i = 1 + while true do + local name, value = debug.getupvalue(func, i) + if name == nil then + return + end + local t = type(value) + if t == "table" then + u[name] = value + elseif t == "function" then + if not unique[value] then + unique[value] = true + getupvaluetable(u, value, unique) + end + end + i=i+1 + end +end + +return function(source, filename , ...) + if filename then + filename = "@" .. filename + else + filename = "=(load)" + end + local output = {} + + local function print(...) + local value = { ... } + for k,v in ipairs(value) do + value[k] = tostring(v) + end + table.insert(output, table.concat(value, "\t")) + end + local u = {} + local unique = {} + local funcs = { ... } + for k, func in ipairs(funcs) do + getupvaluetable(u, func, unique) + end + local p = {} + local proto = u.proto + if proto then + for k,v in pairs(proto) do + local name, dispatch = v.name, v.dispatch + if name and dispatch then + local pp = {} + p[name] = pp + getupvaluetable(pp, dispatch, unique) + end + end + end + local env = setmetatable( { print = print , _U = u, _P = p}, { __index = _ENV }) + local func, err = load(source, filename, "bt", env) + if not func then + return { err } + end + local ok, err = xpcall(func, debug.traceback) + if not ok then + table.insert(output, err) + end + + return output +end