Returns the constant table of the function or level func.
🔎 Tip
Traversing the table with ipairs is not recommended, as constants can be nil or skipped entirely.
Parameters
func - A function or stack level.
Example
debug.getinfo
🪲 Inconsistent
Returns debugger information about a function or stack level.
DebugInfo
Field
Type
Description
source
string
The name of the chunk that created the function.
short_src
string
A "printable" version of source to be used in error messages.
func
function
The function itself.
what
string
The string "Lua" if the function is a Luau function, or "C" if it is a C function.
currentline
number
The current line where the given function is executing. When no line information is available, currentline is set to -1.
name
string
The name of the function. If it cannot find a name, then name is a blank string.
nups
number
The number of upvalues in the function.
numparams
number
The number of parameters in the function (always 0 for C functions).
is_vararg
number
Whether the function has a variadic argument (1 if it does, 0 if it does not).
🪲 Compatibility
Some executors are missing certain fields.
Parameters
func - A function or stack level.
Example
debug.getproto
⛔ Exception🛡️ Security
Returns the proto at index in the function or level func if active is false.
If active is true, then every active function of the proto is returned.
🛡️ Security
In some executors, the proto is non-functional if active is false. Debug information is preserved.
To retrieve a callable function, you can set active to true and index the first proto.
Parameters
func - A function or stack level.
index - The numerical index of the proto to retrieve.
active - Whether to return its list of active closures.
Example
debug.getprotos
🛡️ Security
Returns a list of protos of the function or level func.
🛡️ Security
In some executors, the proto is non-functional, but debug information is preserved.
To retrieve a callable function, see debug.getproto.
Parameters
func - A function or stack level.
Example
debug.getstack
⛔ Exception
Returns the value at index in the stack frame level. Throws an error if no value could be found.
If index is not specified, then the entire stack frame is returned.
Parameters
level - The stack frame to look up.
index - The numerical index of the value to retrieve.
Example
debug.getupvalue
⛔ Exception
Returns the upvalue at index in the function or level func. Throws an error if the upvalue does not exist.
An upvalue is a local variable used by an inner function, and is also called an external local variable.
function debug.getconstants(func: function | number): {any}
local function foo()
local num = 5000 .. 50000
print("Hello, world!", num, warn)
end
for i, v in pairs(debug.getconstants(foo)) do
print(i, v)
end
--> 1 50000
--> 2 "print"
--> 4 "Hello, world!"
--> 5 "warn"
function debug.getinfo(func: function | number): DebugInfo
local function foo()
print("Hello, world!")
end
for k, v in pairs(debug.getinfo(foo)) do
print(k, v, "(" .. type(v) .. ")")
end
function debug.getproto(func: function | number, index: number, active: boolean?): function | {function}
local function myFunction()
local function proto()
print("Hello, world!")
end
end
local proto = debug.getproto(myFunction, 1, true)[1]
proto() --> Hello, world!
function debug.getprotos(func: function | number): {function}
local function myFunction()
local function _1()
print("Hello,")
end
local function _2()
print("world!")
end
end
for i in ipairs(debug.getprotos(myFunction)) do
local proto = debug.getproto(myFunction, i, true)[1]
proto()
end
--> Hello,
--> world!
function debug.getstack(level: number, index: number?): any | {any}
local _ = "a" .. "b"
print(debug.getstack(1, 1)) --> ab
local _ = "a" .. "b"
table.foreach(debug.getstack(1), print)
--> ab
--> table.foreach()
--> debug.getstack()
--> 1
function debug.getupvalue(func: function | number, index: number): any
local upvalue = function ()
end
local function foo()
print(upvalue)
end
print(debug.getupvalue(foo, 1)) --> upvalue
local upvalue = "Hello, world!"
local function foo()
print(upvalue)
end
print(debug.getupvalue(foo, 1)) --> Errors "upvalue index out of range"
print(debug.getconstant(foo, 3)) --> Hello, world!
function debug.getupvalues(func: function | number): {any}
local upvalue1, upvalue2 = function () end, function () end
local function foo()
print(upvalue1, upvalue2)
end
for k, v in pairs(debug.getupvalues(foo)) do
print(k, v, "(" .. type(v) .. ")")
end
--> 1 upvalue1() (function)
--> 2 upvalue2() (function)
function debug.setconstant(func: function | number, index: number, value: any): ()
local function foo()
print("Goodbye, world!")
end
debug.setconstant(foo, 3, "Hello, world!")
foo() --> Hello, world!
function debug.setstack(level: number, index: number, value: any): ()
local function foo()
-- Change the first value from "Goodbye, world!" to "Hello, world!"
return "Goodbye, world!", debug.setstack(1, 1, "Hello, world!")
end
print(foo()) --> Hello, world!
function debug.setupvalue(func: function | number, index: number, value: any): ()
local function somethingImportant()
print("Goodbye, world!")
end
local function foo()
somethingImportant()
end
debug.setupvalue(foo, 1, function ()
print("Hello, world!")
end)
foo() --> Hello, world!