debug.getproto
Gets a proto (nested function) from a function.
Syntax
debug.getproto(func: function | number | ProtoProxy, index: number, activated?: boolean) -> ProtoProxy | {function}Parameters
| Parameter | Type | Description |
|---|---|---|
func | function or number | The function or stack level |
index | number | The proto index (1-based) |
activated | boolean? | If true, returns all activated instances |
Returns
| Type | Description |
|---|---|
ProtoProxy | The nested prototype when activated is false or omitted |
table | Array 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 aliveRelated Functions
debug.getprotos- Get all protos from a function