debug.getprotos
Returns all inner function protos of a function.
function debug.getprotos(func: function | number): {function}Synopsis
How it works
Returns the
Proto.p[] (child proto) array as a list of functions. Each child proto corresponds to an inner function definition in the source code. The CLOSURE instruction instantiates these protos into closures at runtime.Proto tree
Luau compiles each script into a tree of
Proto structs. The top-level script is the root; each nested function declaration becomes a child proto in p[]:
-- Source:
local function outer() -- Proto #0
local function inner() -- Proto #0.p[0]
local function deep() -- Proto #0.p[0].p[0]
end
end
end
debug.getprotos(outer) --> {inner}
debug.getprotos(inner) --> {deep}
The returned functions are prototype templates, not active closures. They share upvalueUpvalueA variable captured from an enclosing scope. When a function references a local variable from an outer function, that variable becomes an upvalue — stored in an UpVal struct that persists even after the outer function returns. references with the original scope.Usage
Call all inner protos
local function myFunction()
local function _1() print("Hello,") end
local function _2() print("world!") end
end
for i in ipairs(debug.getprotos(myFunction)) do
debug.getproto(myFunction, i, true)[1]()
endParameters
func function | number A Luau function or stack level.
Returns
{function} Array of inner proto functions.