Skip to content

hookfunction

Notes on hookfunction

The hook should not have more upvalues than the function you want to hook. There are ways to bypass the upvalue restriction, such as using newlclosure or newcclosure to wrap the hook

All possible hooking closure pairs should be supported throughout L, NC, C (where NC = newcclosure)

hookfunction allows you to hook a function with another wanted function, returning the original unhooked function.

function hookfunction<A1..., R1...>(functionToHook: (A1...) -> R1..., hook: (A1...) -> R1...): (A1...) -> R1...

Parameters

Parameter Description
functionToHook The function that will be hooked
hook The function that will be used as a hook

Example

Hooking functions with hookfunction
local function dummy_func()
    print("I am not hooked!")
end

local function dummy_hook()
    print("I am hooked!")
end

dummy_func() -- Output: I am not hooked!

local old_func = hookfunction(dummy_func, dummy_hook)

dummy_func() -- Output: I am hooked!
old_func() -- Output: I am not hooked!