diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-10-26 19:32:00 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-10-26 19:32:00 +0100 |
commit | f5f6460b2eab9d8cdd1de3231a5770bedd7e34f9 (patch) | |
tree | 0eff02506e6364d1dcb5c71b2edaacd6662ffe20 | |
parent | 1454ac8208cfb734d1ae90dd89c442b6be9f9598 (diff) | |
download | prosody-f5f6460b2eab9d8cdd1de3231a5770bedd7e34f9.tar.gz prosody-f5f6460b2eab9d8cdd1de3231a5770bedd7e34f9.zip |
Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
-rw-r--r-- | core/modulemanager.lua | 9 | ||||
-rw-r--r-- | net/http.lua | 7 | ||||
-rw-r--r-- | net/http/server.lua | 8 | ||||
-rw-r--r-- | plugins/mod_component.lua | 5 | ||||
-rw-r--r-- | util/async.lua | 3 | ||||
-rw-r--r-- | util/promise.lua | 2 | ||||
-rw-r--r-- | util/timer.lua | 20 |
7 files changed, 23 insertions, 31 deletions
diff --git a/core/modulemanager.lua b/core/modulemanager.lua index 81c28aa0..63b5e177 100644 --- a/core/modulemanager.lua +++ b/core/modulemanager.lua @@ -18,18 +18,13 @@ local api = require "core.moduleapi"; -- Module API container local prosody = prosody; local hosts = prosody.hosts; -local xpcall = xpcall; +local xpcall = require "util.xpcall".xpcall; local setmetatable, rawget = setmetatable, rawget; local ipairs, pairs, type, tostring, t_insert = ipairs, pairs, type, tostring, table.insert; local debug_traceback = debug.traceback; local select = select; local unpack = table.unpack or unpack; --luacheck: ignore 113 -local pcall = function(f, ...) - local n = select("#", ...); - local params = {...}; - return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end); -end local autoload_modules = {prosody.platform, "presence", "message", "iq", "offline", "c2s", "s2s", "s2s_auth_certs"}; local component_inheritable_modules = {"tls", "saslauth", "dialback", "iq", "s2s"}; @@ -183,7 +178,7 @@ local function do_load_module(host, module_name, state) api_instance.path = err; modulemap[host][module_name] = pluginenv; - local ok, err = pcall(mod); + local ok, err = xpcall(mod, debug.traceback); if ok then -- Call module's "load" if module_has_method(pluginenv, "load") then diff --git a/net/http.lua b/net/http.lua index 6e5ad67c..b401b040 100644 --- a/net/http.lua +++ b/net/http.lua @@ -20,8 +20,9 @@ local ssl_available = pcall(require, "ssl"); local t_insert, t_concat = table.insert, table.concat; local pairs = pairs; -local tonumber, tostring, xpcall, traceback = - tonumber, tostring, xpcall, debug.traceback; +local tonumber, tostring, traceback = + tonumber, tostring, debug.traceback; +local xpcall = require "util.xpcall".xpcall; local error = error local setmetatable = setmetatable; @@ -101,7 +102,7 @@ function listener.onconnect(conn) end log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---"); - return log_if_failed(req.id, xpcall(function () return callback(content, code, response, request) end, handleerr)); + return log_if_failed(req.id, xpcall(callback, handleerr, content, code, response, request)); end req.reader = request_reader; req.state = "status"; diff --git a/net/http/server.lua b/net/http/server.lua index b73f3385..7fbf6377 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -8,7 +8,7 @@ local os_date = os.date; local pairs = pairs; local s_upper = string.upper; local setmetatable = setmetatable; -local xpcall = xpcall; +local xpcall = require "util.xpcall".xpcall; local traceback = debug.traceback; local tostring = tostring; local cache = require "util.cache"; @@ -88,8 +88,6 @@ setmetatable(events._handlers, { }); local handle_request; -local _1, _2, _3; -local function _handle_request() return handle_request(_1, _2, _3); end local last_err; local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end @@ -107,9 +105,7 @@ function listener.onconnect(conn) while sessions[conn] and #pending > 0 do local request = t_remove(pending); --log("debug", "process_next: %s", request.path); - --handle_request(conn, request, process_next); - _1, _2, _3 = conn, request, process_next; - if not xpcall(_handle_request, _traceback_handler) then + if not xpcall(handle_request, _traceback_handler, conn, request, process_next) then conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err })); conn:close(); end diff --git a/plugins/mod_component.lua b/plugins/mod_component.lua index 743a16a3..b41204a2 100644 --- a/plugins/mod_component.lua +++ b/plugins/mod_component.lua @@ -9,7 +9,8 @@ module:set_global(); local t_concat = table.concat; -local xpcall, tostring, type = xpcall, tostring, type; +local tostring, type = tostring, type; +local xpcall = require "util.xpcall".xpcall; local traceback = debug.traceback; local logger = require "util.logger"; @@ -238,7 +239,7 @@ function stream_callbacks.handlestanza(session, stanza) end if stanza then - return xpcall(function () return core_process_stanza(session, stanza) end, handleerr); + return xpcall(core_process_stanza, handleerr, session, stanza); end end diff --git a/util/async.lua b/util/async.lua index 4d9f159f..20397785 100644 --- a/util/async.lua +++ b/util/async.lua @@ -1,6 +1,7 @@ local logger = require "util.logger"; local log = logger.init("util.async"); local new_id = require "util.id".short; +local xpcall = require "util.xpcall".xpcall; local function checkthread() local thread, main = coroutine.running(); @@ -27,7 +28,7 @@ local function call_watcher(runner, watcher_name, ...) return false; end runner:log("debug", "Calling '%s' watcher", watcher_name); - local ok, err = pcall(watcher, runner, ...); -- COMPAT: Switch to xpcall after Lua 5.1 + local ok, err = xpcall(watcher, debug.traceback, runner, ...); if not ok then runner:log("error", "Error in '%s' watcher: %s", watcher_name, err); return nil, err; diff --git a/util/promise.lua b/util/promise.lua index b368f34e..07c9c4dc 100644 --- a/util/promise.lua +++ b/util/promise.lua @@ -1,6 +1,8 @@ local promise_methods = {}; local promise_mt = { __name = "promise", __index = promise_methods }; +local xpcall = require "util.xpcall".xpcall; + function promise_mt:__tostring() return "promise (" .. (self._state or "invalid") .. ")"; end diff --git a/util/timer.lua b/util/timer.lua index 22f547df..4670e196 100644 --- a/util/timer.lua +++ b/util/timer.lua @@ -13,7 +13,7 @@ local get_time = require "util.time".now local type = type; local debug_traceback = debug.traceback; local tostring = tostring; -local xpcall = xpcall; +local xpcall = require "util.xpcall".xpcall; local math_max = math.max; local _ENV = nil; @@ -26,24 +26,20 @@ local _active_timers = 0; local h = indexedbheap.create(); local params = {}; local next_time = nil; -local _id, _callback, _now, _param; -local function _call() return _callback(_now, _id, _param); end local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end local function _on_timer(now) local peek; while true do peek = h:peek(); if peek == nil or peek > now then break; end - local _; - _, _callback, _id = h:pop(); - _now = now; - _param = params[_id]; - params[_id] = nil; - --item(now, id, _param); -- FIXME pcall - local success, err = xpcall(_call, _traceback_handler); + local _, callback, id = h:pop(); + local param = params[id]; + params[id] = nil; + --item(now, id, _param); + local success, err = xpcall(callback, _traceback_handler, now, id, param); if success and type(err) == "number" then - h:insert(_callback, err + now, _id); -- re-add - params[_id] = _param; + h:insert(callback, err + now, id); -- re-add + params[id] = param; end end |