dreamworkAPI
Ctrl K
GitHub
libraryDocs / debug

debug

librarysharedmenusource

The debug library is intended to help you debug your scripts, however it also has several other powerful uses.

[SHARED AND MENU]

Just empty function, do nothing.

Sometimes makes jit happy :>

[SHARED AND MENU]

Sets the metatable for the given value to the given table (which can be nil).

View documents

Fields

NameTypeDescription
newproxynewproxy
femptyfun()
setmetatablefun( object: any, metatable: ( metatable | nil ) )

Functions

debug.calledbyChecks whether any of the given functions is on the call stack, starting from stack_level and walking outward all the w…
debug.fcallCalls the given function or object with the given arguments.
debug.fempty
debug.findmetatableReturns the metatable of the given name or nil if not found.
debug.getfReturns the function at the given stack level.
debug.getfmainReturns the main function of the current stack.
debug.getfnameAttempts to determine the name and kind of a function, the same way the n option of the standard debug.getinfo does.
debug.getfsourceReturns the path to the file that the function is defined in.
debug.getlocalsReturns all available local variables at the given stack level (and, optionally, in the given thread/coroutine) as a ta…
debug.getmetatableReturns the metatable of the given value, or nil if it doesn't have one.
debug.getmetavalueReturns the value of the given key in the metatable of the given value.
debug.getregistryReturns the registry table.
debug.getstackCaptures the current call stack, starting at stack_level relative to the caller of this function, and returns it as a l…
debug.getupvaluesReturns all upvalues of the given function as a table, keyed by upvalue name.
debug.initmetatable
debug.iscfChecks if the given function or stack level is a C function.
debug.registermetatableRegisters the metatable of the given name and table.
debug.setregistrySets registry table.

#debug.calledby

sharedmenunodiscardsource
function debug.calledby(stack_level: integer, ...: function): boolean

Checks whether any of the given functions is on the call stack, starting from stack_level and walking outward all the way to the bottom of the stack.

Levels follow the same convention as debug.getinfo, relative to the function that calls debug.calledby: level 1 is that function itself, level 2 is whoever called it, and so on. Levels below 1 are treated as 1, so the check always includes at least the calling function. This function's own stack frame is skipped automatically.

Because the whole remaining stack is scanned, a true result means "one of these functions is somewhere at or above stack_level", not that it is exactly at that level. To test a single level, use debug.getinfo directly.

Only functions that are actually on the stack are seen. Tail calls replace their caller's frame, so a function that was tail-called away won't be found.

Returns false if no functions are given.

lua
local function guarded()
    -- true if `guarded` was reached through `handler` at any depth
    return debug.calledby( 1, handler )
end
Parameters
NameTypeDescription
stack_levelintegerThe stack level to start checking from (1 = the function calling calledby). Values below 1 are treated as 1.
...functionThe functions to look for on the call stack.
Returns
TypeNameDescription
booleanresulttrue if any of the given functions is on the stack at or above stack_level, false otherwise.

#debug.fcall

sharedmenusource
function debug.fcall(f: function | any, ...: any): any

Calls the given function or object with the given arguments.

Parameters
NameTypeDescription
ffunction | anyThe function or object to call.
...anyThe arguments to pass to the function or object.
Returns
TypeNameDescription
any...The result of execution.

#debug.fempty

function debug.fempty()

#debug.findmetatable

sharedmenunodiscardsource
function debug.findmetatable(name: string): metatable | nil

Returns the metatable of the given name or nil if not found.

Parameters
NameTypeDescription
namestringThe name of the metatable.
Returns
TypeNameDescription
metatable | nilmetaThe metatable.

#debug.getf

sharedmenunodiscardsource
function debug.getf(level: integer): function | nil

Returns the function at the given stack level.

Parameters
NameTypeDescription
levelintegerThe stack level.
Returns
TypeNameDescription
function | nilThe function at the given level, or nil if the level is invalid.

#debug.getfmain

sharedmenunodiscardsource
function debug.getfmain(stack_level?: integer): function | nil

Returns the main function of the current stack.

Parameters
NameTypeDescription
stack_leveloptionalintegerThe stack stack_level to get the main function from.
Returns
TypeNameDescription
function | nilmain_fnThe main function or nil if not found.

#debug.getfname

sharedmenunodiscardsource
function debug.getfname(location?: integer): string | nil, "global" | "local" | "method" | "field" | "upvalue" | nil

Attempts to determine the name and kind of a function, the same way the n option of the standard debug.getinfo does.

location can be either a function reference or a stack level (an integer, where 1 is the function that called getfname).

Since Lua functions have no inherent name, this works by inspecting how the function was called or accessed - e.g. as a global, a local variable, a table field, a method, or an upvalue - and can fail to find anything, in which case both results are nil.

Parameters
NameTypeDescription
locationoptionalintegerThe function or stack level.
Returns
TypeNameDescription
string | nilnameThe name of the function, or nil if unknown.
"global" | "local" | "method" | "field" | "upvalue" | nilname_whatThe kind of the function, or nil if unknown.

#debug.getfsource

sharedmenunodiscardsource
function debug.getfsource(location: function | integer): string | nil

Returns the path to the file that the function is defined in.

Parameters
NameTypeDescription
locationfunction | integerThe function or stack level to get the path from.
Returns
TypeNameDescription
string | nilfile_pathThe file path or nil if not found.

#debug.getlocals

sharedmenunodiscardsource
function debug.getlocals(stack_level?: integer, thread?: thread): table<string, any>, integer

Returns all available local variables at the given stack level (and, optionally, in the given thread/coroutine) as a table keyed by variable name.

Parameters
NameTypeDescription
stack_leveloptionalintegerThe stack level to inspect, relative to the caller (defaults to the immediate caller's frame). Cannot be negative.
threadoptionalthreadThe thread/coroutine to inspect. If omitted, the current thread is used.
Returns
TypeNameDescription
table<string, any>valuesA table mapping each local variable's name to its current value.
integervalue_countThe number of locals found.

#debug.getmetatable

sharedmenunodiscardsource
function debug.getmetatable(object: any): metatable | nil

Returns the metatable of the given value, or nil if it doesn't have one.

Unlike the standard getmetatable, this ignores the __metatable field, so it can reach metatables that are protected from normal code.

View documents

Parameters
NameTypeDescription
objectanyThe value to get the metatable of.
Returns
TypeNameDescription
metatable | nilmetatableThe metatable of object, or nil if it has none.

#debug.getmetavalue

sharedmenunodiscardsource
function debug.getmetavalue(value: any, key: string): any | nil

Returns the value of the given key in the metatable of the given value.

Returns nil if not found.

Parameters
NameTypeDescription
valueanyThe value to get the metatable from.
keystringThe searchable key.
Returns
TypeNameDescription
any | nilvalueThe value of the given key.

#debug.getregistry

sharedmenusource
function debug.getregistry()

Returns the registry table.

#debug.getstack

sharedmenunodiscardsource
function debug.getstack(stack_level?: integer, what?: infowhat, head_skip?: integer, tail_skip?: integer, max_levels?: integer): debuginfo[], integer

Captures the current call stack, starting at stack_level relative to the caller of this function, and returns it as a list of debug info tables.

Parameters
NameTypeDescription
stack_leveloptionalintegerThe level to start capturing from, where 0 is the function that called getstackinfo. Default is 0.
whatoptionalinfowhatA string of debug.getinfo-style flags selecting which fields to populate per frame. Default is "Snluf".
head_skipoptionalintegerNumber of extra frames to skip from the top ( nearest the caller ) before capturing begins. Default is 0.
tail_skipoptionalintegerNumber of frames to drop from the bottom ( nearest the root ) of the captured stack after capturing ends. Default is 0.
max_levelsoptionalintegerMaximum number of frames to read (after head_skip is applied). Stack walking stops as soon as this many frames have been collected, so no debug.getinfo call is made beyond that point. tail_skip is still applied afterward to this (already capped) set. nil/0 means no limit.
Returns
TypeNameDescription
debuginfo[]stackA list of debug info tables, one per captured stack frame, ordered from stack_level outward.
integerstack_sizeThe number of entries in stack ( equivalent to #stack ).

#debug.getupvalues

sharedmenunodiscardsource
function debug.getupvalues(fn: function): table<string, any>, integer

Returns all upvalues of the given function as a table, keyed by upvalue name.

If the function is a C function (or otherwise has no debug info), an empty table and a count of 0 are returned.

Parameters
NameTypeDescription
fnfunctionThe function to get upvalues from.
Returns
TypeNameDescription
table<string, any>valuesA table mapping each upvalue's name to its current value.
integervalue_countThe number of upvalues found.

#debug.initmetatable

sharedmenusource
function debug.initmetatable(name: string): metatable
Parameters
NameTypeDescription
namestringThe name of the metatable.
Returns
TypeNameDescription
metatablemetatableThe metatable.

#debug.iscf

sharedmenunodiscardsource
function debug.iscf(location: integer | function): boolean

Checks if the given function or stack level is a C function.

Parameters
NameTypeDescription
locationinteger | functionThe function or stack level.
Returns
TypeNameDescription
booleaniscftrue if the function is a C function, false otherwise.

#debug.registermetatable

sharedmenusource
function debug.registermetatable(name: string, tbl: metatable, do_full_register?: boolean): integer

Registers the metatable of the given name and table.

Parameters
NameTypeDescription
namestringThe name of the metatable.
tblmetatableThe metatable to register.
do_full_registeroptionalbooleantrue, the metatable will be registered, false otherwise.
Returns
TypeNameDescription
integermeta_idThe ID of the metatable or -1 if not fully registered.

#debug.setregistry

sharedmenusource
function debug.setregistry(tbl: table)

Sets registry table.

ATTENTION

This function is extremly dangerous, only reason why this exists is fact that getregistry originally broken by cockpunch and i cannot set locals over debug, so only way to implement 3rd party moudles support is implement this function :c

Parameters
NameTypeDescription
tbltable
Generated from the dreamwork meta branch.