Syntax
setupvalue(target: function | number, index: number, value: any)Aliases
debug.setupvalueArguments
| Name | Type | Description |
|---|---|---|
target | function | number | Lua function or stack level whose upvalue is modified. |
index | number | One-based upvalue index. |
value | any | New value for the upvalue. |
Description
Replaces one upvalue in a Lua function or stack level by one-based index. Invalid stack levels raise "Level out of range", non-Lua functions raise "Lua function expected", indexes below 1 raise "Upvalue index starts at 1", and indexes past the available upvalues raise "Upvalue index is out of range". debug.setupvalue is an alias.
Call it with 3 parameter(s): target, index, value. The argument table explains which values are required and which ones only refine the behavior.
It does not return data. Treat the call as an action and handle errors around the call site when needed.
Example
Replace one upvalue in a Lua closure or stack level.
local captured = "before"
local function target()
return captured
end
print(target())
setupvalue(target, 1, "after")
print(target())
debug.setupvalue(target, 1, "done")
print(target())