oth.hook Synchronous

Add a hook layer to a C closure. Its optional filter selects which calls reach this layer; misses continue to earlier layers or the original.

Syntax
Luau
oth.hook(target: Function, callback: Function, filter: HookFilter?) -> Function

Parameters

Function parameters
ParameterTypeDescription
targetFunctionC closure to hook. Luau closures are rejected.
callbackFunctionLuau closure to run on matching calls. C closures are rejected as callbacks.
filterHookFilter?Optional HookFilter that applies only to this newly installed layer.

Returns

Function

A callable for the preceding layer. Call it inside the callback to forward the current arguments and results.

Usage notes

Each OTH callback runs on a hook thread. Newer layers are checked first; a filter miss falls through to the preceding layer.

Save the returned callable so oth.unhook can identify this registration when several layers exist. Use oth.get_root_callback inside a callback to bypass the full chain.

Example

Example
Luau
local target = newcclosure(function(value) return value * 2 end)
local previous
previous = oth.hook(target, function(value)
    return previous(value) + 1
end, ArgumentFilter.new(1, 3))
print(target(3)) -- 7
print(target(4)) -- 8
oth.unhook(target, previous)
Kawaii documentation