return geterrorhandler()(err) |
end |
|
local function CreateDispatcher(argCount) |
local code = [[ |
local next, xpcall, eh = ... |
|
local method, ARGS |
local function call() method(ARGS) end |
|
local function dispatch(handlers, ...) |
local index |
index, method = next(handlers) |
if not method then return end |
local OLD_ARGS = ARGS |
ARGS = ... |
repeat |
xpcall(call, eh) |
index, method = next(handlers, index) |
until not method |
ARGS = OLD_ARGS |
end |
|
return dispatch |
]] |
|
local ARGS, OLD_ARGS = {}, {} |
for i = 1, argCount do ARGS[i], OLD_ARGS[i] = "arg"..i, "old_arg"..i end |
code = code:gsub("OLD_ARGS", tconcat(OLD_ARGS, ", ")):gsub("ARGS", tconcat(ARGS, ", ")) |
return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(next, xpcall, errorhandler) |
local function Dispatch(handlers, ...) |
local index, method = next(handlers) |
if not method then return end |
repeat |
xpcall(method, errorhandler, ...) |
index, method = next(handlers, index) |
until not method |
end |
|
local Dispatchers = setmetatable({}, {__index=function(self, argCount) |
local dispatcher = CreateDispatcher(argCount) |
rawset(self, argCount, dispatcher) |
return dispatcher |
end}) |
|
-------------------------------------------------------------------------- |
-- CallbackHandler:New |
-- |