Debug

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


debug.getconstant

⛔ Exception

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

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.

Read more on Lua visibility rules.

🔎 Note

Some Luau optimizations automatically inline certain constants like strings and integers. They can be retrieved through debug.getconstant instead.

Parameters

  • func - A function or stack level.

  • index - The numerical index of the upvalue to retrieve.

Example

An example of Luau optimization:


debug.getupvalues

Returns a list of upvalues of the function or level func.

🔎 Tip

Traversing the table with ipairs is not recommended, as upvalues can be nil or skipped entirely.

Parameters

  • func - A function or stack level.

Example


debug.setconstant

⛔ Exception

Sets the constant at index in the function or level func to value.

⛔ Exception

The type of value must match the type of the constant at index.

Parameters

  • func - A function or stack level.

  • index - The numerical index of the constant to set.

  • value - The value to set.

Example


debug.setstack

⛔ Exception

Sets the register at index in the stack frame level to value.

⛔ Exception

The type of value must match the type of the register at index.

Parameters

  • level - The stack frame to look up.

  • index - The numerical index of the register to set.

  • value - The value to set.

Example


debug.setupvalue

Sets the upvalue at index in 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 value to set.

Example

Last updated