Debug
Protos, stacks, constants and upvalue access.
Returns child protos. Global: getproto / getprotos.
local function outer()
local function inner() end
return inner
end
print(debug.getproto(outer, 1))
print(#debug.getprotos(outer))
Reads or writes a stack level. Globals: getstack / setstack.
local stack = debug.getstack(1)
print(#stack)
debug.setstack(1, 1, "patched")
Returns debug information. Global: getinfo.
local info = debug.getinfo(print)
print(info.name, info.what, info.source)
Reads upvalues. Globals: getupvalue / getupvalues.
local value = 10
local function f() return value end
print(debug.getupvalue(f, 1))
print(#debug.getupvalues(f))
Writes an upvalue. Global: setupvalue.
local value = 10
local function f() return value end
debug.setupvalue(f, 1, 99)
print(f())
Reads function constants. Globals: getconstant / getconstants.
local function f() return 123 end
print(debug.getconstant(f, 1))
print(#debug.getconstants(f))
Writes a constant. Global: setconstant.
local function f() return 123 end
debug.setconstant(f, 1, 456)
print(f())
Alias of debug.setconstant. Global setvalue is also registered.
local value = "old"
local function f() return value end
debug.setvalue(f, 1, "new")
print(f())
Returns the Lua registry. The global getregistry from the cache library also returns it.
print(debug.getregistry())
Returns the active call stack as a table of function, name, source and line entries.
for _, frame in ipairs(debug.getcallstack()) do
print(frame.name, frame.source, frame.line)
end
Checks whether a stack level points to a valid debug frame.
print(debug.isvalidlevel(1), validlevel(1))
Sets the debug name of a function.
local fn = function() end
debug.setname(fn, "CustomName")
print(debug.info(fn, "n"))
Updates a function debug name, source and linedefined from a table.
local fn = function() end
debug.setinfo(fn, { name = "Custom", source = "@custom", linedefined = 10 })
Returns the raw metatable of an object without invoking Lua protection rules.
print(debug.getmetatable(workspace))
Sets an object metatable and returns the object.
local object = {}
debug.setmetatable(object, { __index = function() return 1 end })
print(object.anything)
Returns whether a function, table or thread has a safe environment.
print(debug.getsafeenv(function() end))
Marks a function, table or thread environment as safe or unsafe.
local env = getfenv(0)
debug.setsafeenv(env, false)
print(debug.getsafeenv(env)) -- false
Hooks a nested proto returned by debug.getproto so new closures call the hook.
local proto = debug.getproto(outer, 1)
hookproto(proto, function() return "hooked" end)