-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchaincaller.lua
59 lines (46 loc) · 1.78 KB
/
chaincaller.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-- Special hook dispatcher implementation. (Do not touch unless you absolutely know what you are doing!)
do
local index -- Shared upvalue between the safe (chainDispatcher) and protected (chainExecutor) context
-- used to communicate the 'to be called' hooks index.
-- This upvalue is used for two way communication:
-- The safe context tells the next hook index to be called in the protected context
-- The protected context tells the safe context what hook index caused the error that
-- resulted in the context change
local function hookExecutor( hooks, ... )
local a, b, c, d, e, f
while index ~= nil do
local func = hooks[index]
local nextIndex = next(hooks, index)
do
if isstring( index ) then
a, b, c, d, e, f = func( ... )
elseif IsValid( index ) then
a, b, c, d, e, f = func( index, ... )
else
hooks[index] = nil
end
if a ~= nil then
return a, b, c, d, e, f
end
end
index = nextIndex
end
end
local function hookDispatcher( hooks, ... )
local context = index -- The caller context is saved here, so nested calls to hookDispatcher can work
local succ, a, b, f, c, e, f
index = nil -- It is crucial that however this method exists, it must restore the 'index'
-- upvalue to its original value, or nested calls of 'hookDispatcher' may misbehave,
-- therefore no errors must happen beyond this point!
while true do
index = next(hooks, index)
succ, a, b, c, d, e, f = pcall(hookExecutor, hooks, ...)
if succ then
index = context
return a, b, c, d, e, f
else
print("Hook failure: ", index, " - Err: ", a )
end
end
end
end