hookproto(proto, hook)
Hooks a nested Luau proto so new closures created from it call the hook.
Syntax
hookproto(proto: function, hook: function) -> ()
Parameters
| Parameter | Type | Description |
|---|---|---|
proto | function | Handle returned by debug.getproto (or a closure over the proto). |
hook | function | Replacement callback receiving the original arguments. |
Returns
| Type | Description |
|---|---|
() | No direct return values. |
Description
Hooks the prototype behind the handle by temporarily replacing its instruction stream and constants with a vararg trampoline that calls the hook. New closures created from the proto (including shared DUPCLOSURE constants) take the hook.
The original code and constant arrays are retained so in-flight calls continue safely, and restoreproto puts everything back. Cached closure stack sizes are patched while hooked so the VM frame invariants hold.
Example
local function outer()
local function inner() return "original" end
return inner
end
local proto = debug.getproto(outer, 1)
hookproto(proto, function() return "hooked" end)
print(outer()()) -- hooked
restoreproto(proto)
Notes
- Hook handles come from debug.getproto; a plain closure that directly wraps the proto is accepted too.