Debug

getproto

Returns a nested Lua function by one-based index.

Syntax

getproto(target: function | number, index: number, active?: boolean): function | table
Aliases debug.getproto

Arguments

NameTypeDescription
targetfunction | numberLua function or stack level whose nested functions are inspected.
indexnumberOne-based nested function index.
active?booleanWhen true, returns live closures for the selected nested function instead of a standalone function.

Returns

NameTypeDescription
protofunction | tableNested function when active is false or omitted, or a table of live closures when active is true.

Description

Returns a nested Lua function by one-based index. target must be a Lua function or stack level number. Invalid stack levels raise "Level out of range", non-Lua functions raise "Lua function expected", and indexes outside the nested function range raise "index out of bounds". When active is true, returns currently live closures for that nested function instead. debug.getproto is an alias.

Call it with 3 parameter(s): target, index, active. The argument table explains which values are required and which ones only refine the behavior.

It returns proto (function | table). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Read a nested Proto as a cloned closure, or list live closures for that nested Proto.

local function outer()
    local function inner()
        return "ready"
    end

    return inner
end

local proto = getproto(outer, 1)
local active = getproto(outer, 1, true)

print(type(proto))
print(#active)
print(type(debug.getproto(outer, 1)))