debug.setstack
Sets a register value in a stack frame.
function debug.setstack(level: number, index: number, value: any): ()Synopsis
How it works
Writes a value into a live stack slot at the specified call level and register index. This directly modifies a local variable or temporary in the target function’s stack frameCall stackThe chain of CallInfo frames representing nested function calls. Each frame records the closure, base register, top, and saved program counter. Walking the call stack reveals the full execution trace. while it’s executing. The change takes effect immediately for subsequent instructions in that frame.
Caution
Setting a stack slot to an incompatible type can crash the VM if the nextnext()Table traversal primitive. next(t, key) returns the next key-value pair after key, or nil when done. next(t, nil) returns the first pair. The underlying function used by pairs(). Traversal order is not guaranteed. instruction assumes a specific type (e.g., setting a number slot to a table when an arithmetic instruction follows). Use
debug.getstack first to understand the current layout.Usage
Modify a return value
local function foo()
return "Goodbye, world!", debug.setstack(1, 1, "Hello, world!")
end
print(foo()) "cc">--> Hello, world!Parameters
level number Stack frame level.
index number Register index.
value any New value.