Skip to content

Debug

The debug library is an extension of the Luau debug library, providing greater control over Luau functions.


debug.getconstant

function debug.getconstant(func: function | number, index: number): any
Returns the constant at index 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

function debug.getconstants(func: function | number): {any}
Returns the constant table of the function or level func.

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

function debug.getinfo(func: function | number): DebugInfo

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

function debug.setconstant(func: function | number, index: number, value: any)

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

function debug.getproto(func: function | number, index: number, active: boolean?): function

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

function debug.getprotos(func: function | number): {function}

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

function debug.getstack(level: number, index: number?): any | {any}

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

function debug.setstack(level: number, index: number, value: any)

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

local function foo()
    local a = 1
    debug.setstack(1, 1, 10)
    print(a) --> 10
end
foo()

debug.getupvalue

function debug.getupvalue(func: function | number, index: number): any

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

local x = 10
local function foo()
    print(x)
end
print(debug.getupvalue(foo, 1)) --> 10

debug.getupvalues

function debug.getupvalues(func: function | number): {any}

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

function debug.setupvalue(func: function | number, index: number, value: any)

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

local x = 10
local function foo()
    print(x)
end
debug.setupvalue(foo, 1, 20)
foo() --> Prints: 20

debug.setname

function debug.setname(func: function, name: string)

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

local function foo() end
debug.setname(foo, "bar")
print(debug.getinfo(foo).name) --> "bar"

debug.isvalidlevel

function debug.isvalidlevel(level: number): boolean

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

print(debug.isvalidlevel(1)) --> true
print(debug.isvalidlevel(100)) --> false

debug.getregistry

function debug.getregistry(): table

Returns the Lua registry table.

Example

local registry = debug.getregistry()
print(type(registry)) --> table