Debug

getprotos

Returns all nested Lua functions from a Lua function or stack level as a one-based array table.

Syntax

getprotos(target: function | number): table
Aliases debug.getprotos

Arguments

NameTypeDescription
targetfunction | numberLua function or stack level whose nested functions are returned.

Returns

NameTypeDescription
protostableOne-based array table containing nested functions. Empty when none are present.

Description

Returns all nested Lua functions from a Lua function or stack level as a one-based array table. Invalid stack levels raise "Level out of range", and non-Lua functions raise "Lua function expected". debug.getprotos is an alias.

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

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

Example

Clone every nested Proto from a Lua closure into an array of new L closures.

local function outer()
    local function first()
        return "one"
    end

    local function second()
        return "two"
    end

    return first, second
end

local protos = getprotos(outer)

for index, proto in ipairs(protos) do
    print(index, type(proto))
end

print(#debug.getprotos(outer))