Volt

debug.getupvalues

Gets all upvalues from a function.

Syntax

debug.getupvalues(func: function | number) -> table

Parameters

ParameterTypeDescription
funcfunction or numberThe function or stack level

Returns

TypeDescription
tableA table mapping indices to upvalue values

Description

debug.getupvalues returns a table containing all upvalues (captured variables) of a function.

Example

local a = 1
local b = "hello"
local c = {key = "value"}

local function example()
    print(a, b, c.key)
end

local upvalues = debug.getupvalues(example)
for i, v in pairs(upvalues) do
    print(i, type(v), v)
end
--[[
Output:
1  number   1
2  string   hello
3  table    table: 0x...
]]

Practical Use

-- Find all string upvalues in a function
local function getStringUpvalues(func)
    local strings = {}
    for i, upvalue in pairs(debug.getupvalues(func)) do
        if type(upvalue) == "string" then
            strings[i] = upvalue
        end
    end
    return strings
end

On this page