Home Explore Blog CI



neovim

41th chunk of `runtime/doc/luaref.txt`
608af1f5fa3d763a2c484bb1fe16c15ec9f49ce09bac15ea0000000100000fa0
        /* (S) */
        char short_src[LUA_IDSIZE]; /* (S) */
        /* private part */
        other fields
    } lua_Debug;
<

A structure used to carry different pieces of information about an active
function. `lua_getstack` (see |lua_getstack()|) fills only the private part
of this structure, for later use. To fill the other fields of `lua_Debug` with
useful information, call `lua_getinfo` (see |lua_getinfo()|).

The fields of `lua_Debug` have the following meaning:

- `source`             If the function was defined in a string, then `source` is
                     that string. If the function was defined in a file, then
                     `source` starts with a `@` followed by the file name.
- `short_src`          a "printable" version of `source`, to be used in error messages.
- `linedefined`        the line number where the definition of the function starts.
- `lastlinedefined`    the line number where the definition of the function ends.
- `what`               the string `"Lua"` if the function is a Lua function,
                     `"C"` if it is a C function, `"main"` if it is the main
                     part of a chunk, and `"tail"` if it was a function that
                     did a tail call. In the latter case, Lua has no other
                     information about the function.
- `currentline`        the current line where the given function is executing.
                     When no line information is available, `currentline` is
                     set to -1.
- `name`               a reasonable name for the given function. Because
                     functions in Lua are first-class values, they do not have
                     a fixed name: some functions may be the value of multiple
                     global variables, while others may be stored only in a
                     table field. The `lua_getinfo` function checks how the
                     function was called to find a suitable name. If it cannot
                     find a name, then `name` is set to `NULL`.
- `namewhat`           explains the `name` field. The value of `namewhat` can be
                     `"global"`, `"local"`, `"method"`, `"field"`,
                     `"upvalue"`, or `""` (the empty string), according to how
                     the function was called. (Lua uses the empty string when
                     no other option seems to apply.) `nups`  the number of
                     upvalues of the function.

lua_gethook                                                      *lua_gethook()*
>c
    lua_Hook lua_gethook (lua_State *L);
<
        Returns the current hook function.

lua_gethookcount                                            *lua_gethookcount()*
>c
    int lua_gethookcount (lua_State *L);
<
        Returns the current hook count.

lua_gethookmask                                              *lua_gethookmask()*
>c
    int lua_gethookmask (lua_State *L);
<
        Returns the current hook mask.

lua_getinfo                                                      *lua_getinfo()*
>c
    int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
<
        Returns information about a specific function or function invocation.

        To get information about a function invocation, the parameter `ar`
        must be a valid activation record that was filled by a previous call
        to `lua_getstack` (see |lua_getstack()|) or given as argument to
        a hook (see |lua_Hook|).

        To get information about a function you push it onto the stack and
        start the `what` string with the character `>`. (In that case,
        `lua_getinfo` pops the function in the top of the stack.) For
        instance, to know in which line a function `f` was defined, you can
        write the following code:
>c
               lua_Debug ar;
               lua_getfield(L, LUA_GLOBALSINDEX, "f");  /* get global 'f' */
               lua_getinfo(L, ">S", &ar);
               printf("%d\n", ar.linedefined);
<
       

Title: Lua C API: Debug Structure Details and Information Retrieval Functions
Summary
This section details the fields of the `lua_Debug` structure, including `what`, `currentline`, `name`, `namewhat`, and `nups`. It also describes the Lua C API functions for retrieving debug-related information: `lua_gethook`, `lua_gethookcount`, `lua_gethookmask`, and `lua_getinfo`. The `lua_getinfo` function is explained in detail, including how to use it to get information about a specific function invocation or a function itself, and provides an example.