debug.getupvalues
Gets all upvalues from a function.
Syntax
debug.getupvalues(func: function | number) -> tableParameters
| Parameter | Type | Description |
|---|---|---|
func | function or number | The function or stack level |
Returns
| Type | Description |
|---|---|
table | A 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
endRelated Functions
debug.getupvalue- Get a single upvaluedebug.setupvalue- Modify an upvalue