Home Explore Blog CI



neovim

43th chunk of `runtime/doc/luaref.txt`
d5644809bb6101d82045bb50fcbb0c8580b44a0891c2978d0000000100000fa4
 lua_getstack (lua_State *L, int level, lua_Debug *ar);
<
        Gets information about the interpreter runtime stack.

        This function fills parts of a `lua_Debug` (see |lua_Debug|)
        structure with an identification of the `activation record` of the
        function executing at a given level. Level 0 is the current running
        function, whereas level `n+1` is the function that has called level
        `n`. When there are no errors, `lua_getstack` returns 1; when called
        with a level greater than the stack depth, it returns 0.

lua_getupvalue                                                *lua_getupvalue()*
>c
    const char *lua_getupvalue (lua_State *L, int funcindex, int n);
<
        Gets information about a closure's upvalue. (For Lua functions,
        upvalues are the external local variables that the function uses, and
        that are consequently included in its closure.) `lua_getupvalue` gets
        the index `n` of an upvalue, pushes the upvalue's value onto the
        stack, and returns its name. `funcindex` points to the closure in the
        stack. (Upvalues have no particular order, as they are active through
        the whole function. So, they are numbered in an arbitrary order.)

        Returns `NULL` (and pushes nothing) when the index is greater than the
        number of upvalues. For C functions, this function uses the empty
        string `""` as a name for all upvalues.

lua_Hook                                                              *lua_Hook*
>c
    typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
<
        Type for debugging hook functions.

        Whenever a hook is called, its `ar` argument has its field `event` set
        to the specific event that triggered the hook. Lua identifies these
        events with the following constants: `LUA_HOOKCALL`, `LUA_HOOKRET`,
        `LUA_HOOKTAILRET`, `LUA_HOOKLINE`, and `LUA_HOOKCOUNT`. Moreover, for
        line events, the field `currentline` is also set. To get the value of
        any other field in `ar`, the hook must call `lua_getinfo` (see
        |lua_getinfo()|). For return events, `event` may be
        `LUA_HOOKRET`, the normal value, or `LUA_HOOKTAILRET`. In the latter
        case, Lua is simulating a return from a function that did a tail call;
        in this case, it is useless to call `lua_getinfo`.

        While Lua is running a hook, it disables other calls to hooks.
        Therefore, if a hook calls back Lua to execute a function or a chunk,
        this execution occurs without any calls to hooks.


lua_sethook                                                      *lua_sethook()*
>c
    int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);
<
        Sets the debugging hook function.

        Argument `f` is the hook function. `mask` specifies on which events
        the hook will be called: it is formed by a bitwise `or` of the
        constants `LUA_MASKCALL`, `LUA_MASKRET`, `LUA_MASKLINE`, and
        `LUA_MASKCOUNT`. The `count` argument is only meaningful when the mask
        includes `LUA_MASKCOUNT`. For each event, the hook is called as
        explained below:

         - `The call hook`: is called when the interpreter calls a function.
           The hook is called just after Lua enters the new function, before
           the function gets its arguments.
         - `The return hook`: is called when the interpreter returns from a
           function. The hook is called just before Lua leaves the function.
           You have no access to the values to be returned by the function.
         - `The line hook`: is called when the interpreter is about to start
           the execution of a new line of code, or when it jumps back in the
           code (even to the same line). (This event only happens while Lua is
           executing a Lua function.)
         - `The count hook`: is called after the interpreter executes every
           `count` instructions. (This event only happens

Title: Lua C API: `lua_getupvalue`, `lua_Hook`, and `lua_sethook` Explained
Summary
This section covers the functions `lua_getupvalue`, `lua_Hook`, and `lua_sethook`. `lua_getupvalue` retrieves information about a closure's upvalue, pushing its value onto the stack and returning its name. `lua_Hook` defines the type for debugging hook functions and explains how the `ar` argument's `event` field is set. `lua_sethook` sets the debugging hook function and specifies the events on which the hook will be called using a mask of `LUA_MASKCALL`, `LUA_MASKRET`, `LUA_MASKLINE`, and `LUA_MASKCOUNT`.