Home Explore Blog CI



neovim

44th chunk of `runtime/doc/luaref.txt`
76b28c926c10b2e51c72c2a418ea4c45d52bd25ac32fff560000000100000fa0
 `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 while Lua is
           executing a Lua function.)

        A hook is disabled by setting `mask` to zero.

lua_setlocal                                                    *lua_setlocal()*
>c
    const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);
<
        Sets the value of a local variable of a given activation record.
        Parameters `ar` and `n` are as in `lua_getlocal` (see
        |lua_getlocal()|). `lua_setlocal` assigns the value at the top of
        the stack to the variable and returns its name. It also pops the value
        from the stack.

        Returns `NULL` (and pops nothing) when the index is greater than the
        number of active local variables.

lua_setupvalue                                                *lua_setupvalue()*
>c
    const char *lua_setupvalue (lua_State *L, int funcindex, int n);
<
        Sets the value of a closure's upvalue. It assigns the value at the top
        of the stack to the upvalue and returns its name. It also pops the
        value from the stack. Parameters `funcindex` and `n` are as in the
        `lua_getupvalue` (see |lua_getupvalue()|).

        Returns `NULL` (and pops nothing) when the index is greater than the
        number of upvalues.

                                                           *lua-debugexample*
        As an example, the following function lists the names of all local
        variables and upvalues for a function at a given level of the stack:
>c
               int listvars (lua_State *L, int level) {
                 lua_Debug ar;
                 int i;
                 const char *name;
                 if (lua_getstack(L, level, &ar) == 0)
                   return 0;  /* failure: no such level in the stack */
                 i = 1;
                 while ((name = lua_getlocal(L, &ar, i++)) != NULL) {
                   printf("local %d %s\n", i-1, name);
                   lua_pop(L, 1);  /* remove variable value */
                 }
                 lua_getinfo(L, "f", &ar);  /* retrieves function */
                 i = 1;
                 while ((name = lua_getupvalue(L, -1, i++)) != NULL) {
                   printf("upvalue %d %s\n", i-1, name);
                   lua_pop(L, 1);  /* remove upvalue value */
                 }
                 return 1;
               }
<

==============================================================================
4  THE AUXILIARY LIBRARY                                            *lua-aux*

The auxiliary library provides several convenient functions to interface C
with Lua. While the basic API provides the primitive functions for all
interactions between C and Lua, the auxiliary library provides higher-level
functions for some common tasks.

All functions from the auxiliary library are defined in header file `lauxlib.h`
and have a prefix `luaL_`.

All functions in the auxiliary library are built on top of the basic API, and
so they provide nothing that cannot be done with this API.

Several functions

Title: Lua C API: Hook Details, `lua_setlocal`, `lua_setupvalue`, Debugging Example, and Auxiliary Library Introduction
Summary
This section details the different types of hooks that can be set using `lua_sethook`, including call hooks, return hooks, line hooks, and count hooks. It then describes `lua_setlocal`, which sets the value of a local variable, and `lua_setupvalue`, which sets the value of a closure's upvalue. A debugging example, `listvars`, is provided to illustrate how to list local variables and upvalues. Finally, it introduces the auxiliary library (`lauxlib.h`) and its functions prefixed with `luaL_`, which provide higher-level functions for common tasks when interfacing C with Lua.