ReferenceClosures

hookproto

Makes every closure created from the prototype after this call run hook's body instead of its own.

Syntax

hookproto(proto: function, hook: function) -> function

Parameters

ParameterTypeDescription
protofunctionA function from debug.getproto or debug.getprotos (not the active form).
hookfunctionA Lua function with the same number of upvalues as the prototype, or fewer.

Returns

TypeDescription
functionThe proto you passed in.

Description

Closures that already exist keep their old body. The change is reversed with restoreproto. It raises an error if the prototype is already hooked, if it did not come from debug.getproto, or if the hook has more upvalues than the prototype.

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()())
restoreproto(proto)
print(outer()())

These are Game executor contracts. Check build requirements and session lifetimes before using them.