hookproto

Hook a nested Luau proto.

Syntax

hookproto(proto: ProtoProxy, hook: (...any) -> ...any) -> ()

Parameters

ParameterTypeDescription
protoProtoProxyProto to hook
hook(...any) -> ...anyNew Luau behavior for the proto

Returns

This function does not return a value.

Description

Use debug.getproto or debug.getprotos to get a ProtoProxy. Pass it to hookproto.

The hook applies to existing and future closures that use the proto. It stays active until you call restoreproto.

hookproto does not return the original function.

Example

local function makeGreeter()
    local function greet(name)
        return `Hello, {name}!`
    end

    return greet
end

local greet = makeGreeter()
local proto = debug.getproto(makeGreeter, 1)

hookproto(proto, function(name)
    return `Hooked: {name}`
end)

print(greet("Volt")) -- Hooked: Volt
print(makeGreeter()("Docs")) -- Hooked: Docs

restoreproto(proto)
print(greet("Volt")) -- Hello, Volt!

Hook Requirements

  • hook must be a Luau function. C functions are not accepted.
  • hook must have zero upvalues. It cannot capture a local variable.
local prefix = "Hooked"

-- Rejected because the function captures prefix.
hookproto(proto, function(value)
    return `{prefix}: {value}`
end)