Debug
The debug library is an extension of the Luau debug library, providing greater control over Luau functions.
debug.getconstant
Returns the constant atindex
in the constant table of the function or level func
. Throws an error if the constant does not exist.
Parameters
func
- A function or stack level.index
- The numerical index of the constant to retrieve.
Example
local function foo()
print("Hello, world!")
end
print(debug.getconstant(foo, 1)) --> "print"
print(debug.getconstant(foo, 2)) --> nil
print(debug.getconstant(foo, 3)) --> "Hello, world!"
debug.getconstants
Returns the constant table of the function or levelfunc
.
Parameters
func
- A function or stack level.
Example
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"
debug.getinfo
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). |
Parameters
func
- A function or stack level.
Example
local function foo()
print("Hello, world!")
end
for k, v in pairs(debug.getinfo(foo)) do
print(k, v, "(" .. type(v) .. ")")
end
debug.setconstant
Sets the constant at index
in the constant table of the function or level func
to value
. Throws an error if the constant does not exist or if the types don't match.
Parameters
func
- A function or stack level.index
- The numerical index of the constant to set.value
- The new value for the constant.
Example
local function foo()
print("Hello, world!")
end
debug.setconstant(foo, 3, "Goodbye, world!")
foo() --> Prints: Goodbye, world!
debug.getproto
Returns the proto at index
in the proto table of the function or level func
. If active
is true, it returns all active closures with the specified proto.
Parameters
func
- A function or stack level.index
- The numerical index of the proto to retrieve.active
- (Optional) If true, returns active closures with the proto.
Example
local function foo()
local function bar() end
return bar
end
local proto = debug.getproto(foo, 1)
print(type(proto)) --> function
debug.getprotos
Returns a table containing all protos of the function or level func
.
Parameters
func
- A function or stack level.
Example
local function foo()
local function bar() end
local function baz() end
end
local protos = debug.getprotos(foo)
print(#protos) --> 2
debug.getstack
Returns the value at index
in the stack frame at level
. If index
is not provided, returns a table of all values in the stack frame.
Parameters
level
- The stack level to inspect.index
- (Optional) The index of the value to retrieve.
Example
local function foo()
local a, b = 1, 2
print(debug.getstack(1)) --> {1, 2}
print(debug.getstack(1, 2)) --> 2
end
foo()
debug.setstack
Sets the value at index
in the stack frame at level
to value
.
Parameters
level
- The stack level to modify.index
- The index of the value to set.value
- The new value to set.
Example
debug.getupvalue
Returns the upvalue at index
for the function or level func
.
Parameters
func
- A function or stack level.index
- The numerical index of the upvalue to retrieve.
Example
debug.getupvalues
Returns a table containing all upvalues of the function or level func
.
Parameters
func
- A function or stack level.
Example
local x, y = 10, 20
local function foo()
print(x, y)
end
local upvalues = debug.getupvalues(foo)
for i, v in ipairs(upvalues) do
print(i, v)
end
--> 1 10
--> 2 20
debug.setupvalue
Sets the upvalue at index
for the function or level func
to value
.
Parameters
func
- A function or stack level.index
- The numerical index of the upvalue to set.value
- The new value for the upvalue.
Example
debug.setname
Sets the debug name of the function func
to name
.
Parameters
func
- The function to rename.name
- The new debug name for the function.
Example
debug.isvalidlevel
Checks if the given stack level is valid.
Parameters
level
- The stack level to check.
Returns
true
if the level is valid, false
otherwise.
Example
debug.getregistry
Returns the Lua registry table.