Functions & closuresReference
hookfunction
Represents interception of function behavior.
Aliases
hookfuncreplaceclosureDetails#
A hook replaces the behavior of a function value in the current session. Existing references to that value also call the replacement. Save the returned previous implementation and call that value inside the replacement; calling target there would recurse. Repeated hooks return the preceding replacement. restorefunction removes the chain and restores the initial implementation. hookfunc and replaceclosure are aliases.
Signature#
hookfunction(target: function, replacement: function): functionParameters#
| Parameter | Type | Description |
|---|---|---|
target | function | The exact function value to inspect or modify. |
replacement | function | Replacement callable, receiving the target's arguments. |
Returns#
The previous callable implementation. Both arguments must be functions.
Examples#
Example
local function add(a, b) return a + b end
local saved = add
local original
original = hookfunction(add, function(a, b)
return original(a, b) * 2
end)
print(saved(2, 3)) -- 10
print(restorefunction(add)) -- true
print(add(2, 3)) -- 5