debug

The debug library of LuaJIT 2.1, restricted for the sandboxed VM. It gives access to stack frames, locals, upvalues, metatables and hooks.


Functions

debug.getfenv(o: any): table | nil

debug.getfenv(o: any): table | nil

  • Returns the environment table of o (function, userdata or thread).

debug.gethook(co?: thread): function, string, number

debug.gethook(co?: thread): function, string, number

  • Returns the current hook function, the hook mask string and the hook count.

debug.getinfo(f: function | number, what?: string = "flnSu"): table | nil

debug.getinfo(f: function | number, what?: string = "flnSu"): table | nil

  • Returns a table describing a function. f is a function, or a stack level (0 is getinfo itself, 1 the caller). Returns nil if the level is out of range.
  • A thread can be given as first argument: debug.getinfo(co, f, what).
  • what selects fields: n gives name and namewhat; S gives source, short_src, what, linedefined, lastlinedefined; l gives currentline; u gives nups; f gives func; L gives activelines.
local info = debug.getinfo(1, "Sl")
print(info.short_src, info.currentline)

debug.getlocal(level: number | function, index: number): string, any

debug.getlocal(level: number | function, index: number): string, any

  • Returns the name and value of local variable number index at stack level level, or nil if there is none.
  • With a function instead of a level, returns only the name of the parameter index.
  • A thread can be given as first argument.

debug.getmetatable(obj: any): table | nil

debug.getmetatable(obj: any): table | nil

  • Returns the metatable of obj, ignoring any __metatable field.

debug.getregistry(): table

debug.getregistry(): table

  • Returns the Lua registry table.

debug.getupvalue(f: function, up: number): string, any

debug.getupvalue(f: function, up: number): string, any

  • Returns the name and value of upvalue number up of f, or nothing if there is none.

debug.setfenv(o: any, env: table): any

debug.setfenv(o: any, env: table): any

  • Sets the environment of o (function, userdata or thread) and returns o.

debug.sethook(hook?: function, mask?: string, count?: number)

debug.sethook(hook?: function, mask?: string, count?: number)

  • Installs hook. mask may contain "c" (call), "r" (return) and "l" (new line); count fires the hook every count instructions.
  • Called with no arguments, removes the hook.
  • See the warning above: the client keeps enforcing its time limits regardless.

debug.setlocal(level: number, index: number, value: any): string | nil

debug.setlocal(level: number, index: number, value: any): string | nil

  • Assigns value to local variable number index at stack level level. Returns the variable name, or nil if there is no such variable.

debug.setmetatable(obj: any, mt: table | nil): any

debug.setmetatable(obj: any, mt: table | nil): any

  • Sets the metatable of any value, including numbers, strings and functions. Returns obj.

debug.setupvalue(f: function, up: number, value: any): string | nil

debug.setupvalue(f: function, up: number, value: any): string | nil

  • Assigns value to upvalue number up of f. Returns the upvalue name, or nil if there is none.

debug.traceback(co?: thread, message?: any, level?: number = 1): string

debug.traceback(co?: thread, message?: any, level?: number = 1): string

  • Returns a string with a stack traceback, prefixed by message and a newline when message is a string.
  • If message is not a string and not nil, returns it unchanged.
  • Often used as the handler of xpcall.
print(debug.traceback("here"))

debug.upvalueid(f: function, n: number): userdata

debug.upvalueid(f: function, n: number): userdata

  • Returns a unique identifier for upvalue n of f. Two closures sharing an upvalue return the same identifier.

debug.upvaluejoin(f1: function, n1: number, f2: function, n2: number)

debug.upvaluejoin(f1: function, n1: number, f2: function, n2: number)

  • Makes upvalue n1 of closure f1 refer to upvalue n2 of closure f2.