Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
authorMatthew Wild <mwild1@gmail.com>
Fri, 26 Oct 2018 19:32:00 +0100
changeset 9565 acf74ad0b795
parent 9564 cfc7b2f7251e
child 9566 732314eb3258
Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
core/modulemanager.lua
net/http.lua
net/http/server.lua
plugins/mod_component.lua
util/async.lua
util/promise.lua
util/timer.lua
--- a/core/modulemanager.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/core/modulemanager.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -18,18 +18,13 @@
 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 @@
 	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
--- a/net/http.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/net/http.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -20,8 +20,9 @@
 
 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 @@
 		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";
--- a/net/http/server.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/net/http/server.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -8,7 +8,7 @@
 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 @@
 });
 
 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 @@
 		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
--- a/plugins/mod_component.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/plugins/mod_component.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -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 @@
 	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
 
--- a/util/async.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/util/async.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -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 @@
 		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;
--- a/util/promise.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/util/promise.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -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
--- a/util/timer.lua	Fri Oct 26 19:29:08 2018 +0100
+++ b/util/timer.lua	Fri Oct 26 19:32:00 2018 +0100
@@ -13,7 +13,7 @@
 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 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