Debug

getconstant

Returns one constant from a Lua function or stack level by one-based index.

Syntax

getconstant(target: function | number, index: number): any | nil
Aliases debug.getconstant

Arguments

NameTypeDescription
targetfunction | numberLua function or stack level whose constants are inspected.
indexnumberOne-based constant index.

Returns

NameTypeDescription
constantany | nilConstant value at the requested index, or nil when that constant is a function or table.

Description

Returns one constant from a Lua function or stack level by one-based index. target must be a function or stack level number. Invalid stack levels raise "Level out of range". Non-function and non-number targets raise a type error expecting "function or number". C closures raise "Lua function expected". Indexes below 1 raise "Constant index starts at 1", and indexes past the available constants raise "Constant index out of range". Function and table constants are returned as nil. debug.getconstant is an alias.

Call it with 2 parameter(s): target, index. The argument table explains which values are required and which ones only refine the behavior.

It returns constant (any | nil). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Read a one-based Proto constant from a Lua closure.

local function target()
    return "ready"
end

local value = getconstant(target, 1)

print(value)
print(debug.getconstant(target, 1))