debug.getproto

Gets a proto (nested function) from a function.

Syntax

debug.getproto(func: function | number | ProtoProxy, index: number, activated?: boolean) -> ProtoProxy | {function}

Parameters

ParameterTypeDescription
funcfunction or numberThe function or stack level
indexnumberThe proto index (1-based)
activatedboolean?If true, returns all activated instances

Returns

TypeDescription
ProtoProxyThe nested prototype when activated is false or omitted
tableArray of activated instances (if activated is true)

Description

debug.getproto retrieves a nested prototype. By default Volt returns a non-executable ProtoProxy that can be inspected by other debug APIs. With activated = true, it returns the live closures currently using that prototype.

Example

local function outer()
    local function inner1()
        print("inner1")
    end

    local function inner2()
        print("inner2")
    end

    inner1()
    inner2()
end

-- Get and inspect the first nested prototype
local proto1 = debug.getproto(outer, 1)
print(debug.getconstants(proto1))

-- Get the second nested function
local proto2 = debug.getproto(outer, 2)
print(debug.getconstants(proto2))

Activated Instances

When activated is true, returns all instances of the proto that have been created:

local retainedClosures = {}

local function factory()
    local function create()
        return {}
    end
    table.insert(retainedClosures, create)
end

factory()

-- Get all activated instances of the inner function
local activated = debug.getproto(factory, 1, true)
print(#activated) -- 1 while the retained closure is alive