hookproto Synchronous

Replace a nested Luau prototype so calls to closures created from it use replacement code. Use this for a specific nested function obtained with debug.getproto, and restore it after the inspection or experiment.

Syntax
Luau
hookproto(target: ProtoProxy, replacement: Function) -> ()

Parameters

Function parameters
ParameterTypeDescription
targetProtoProxyProtoProxy returned by debug.getproto or debug.getprotos.
replacementFunctionLuau function with no captured upvalues.

Returns

()

No value.

Usage notes

The target prototype must also have no upvalues. A later hookproto on the same target replaces the earlier proto hook.

Example

Example
Luau
local function factory()
    return function() return "before" end
end
local proto = debug.getproto(factory, 1)
hookproto(proto, function() return "after" end)
print(factory()()) -- after
restoreproto(proto)
Kawaii documentation