Grafts the body of hook onto the prototype of target. Every closure that shares that prototype — including ones already created, and ones running right now — starts executing the hook’s code. This is a deeper hook than hookfunction, which redirects one closure. hookproto changes the compiled function itself.

Parameters

function
required
A Luau function, or a proto handle from getproto.
function
required
A Luau function whose body replaces the target’s. It must capture no upvalues.

Behavior

  • One hook per prototype. Hooking an already-hooked prototype raises; call restoreproto first.
  • The hook must not capture upvalues. The grafted body runs against the target’s upvalue layout, so a hook that closed over variables of its own cannot work. Pass data in through arguments or globals.
  • Live frames keep running correctly. A closure already executing the old body is moved onto a carrier prototype so it finishes with the code it started with, rather than jumping mid-function into different bytecode.
  • It affects all closures on the prototype. That is the point, and the risk: if the prototype is shared more widely than you expect, so is the hook.
  • C functions cannot be hooked, in either position.

Example

Errors