From f5f6460b2eab9d8cdd1de3231a5770bedd7e34f9 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 26 Oct 2018 19:32:00 +0100 Subject: Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall --- util/async.lua | 3 ++- util/promise.lua | 2 ++ util/timer.lua | 20 ++++++++------------ 3 files changed, 12 insertions(+), 13 deletions(-) (limited to 'util') 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 -- cgit v1.2.3