Home Explore Blog CI



neovim

45th chunk of `runtime/doc/luaref.txt`
48e3e2e97c575499fc00f08e7ae4d267b7e4710d3308a7f50000000100000fa5
 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 in the auxiliary library are used to check C function
arguments. Their names are always `luaL_check*` or `luaL_opt*`. All of these
functions raise an error if the check is not satisfied. Because the error
message is formatted for arguments (e.g., "bad argument #1"), you should not
use these functions for other stack values.

==============================================================================
4.1  Functions and Types                                   *lua-auxFunctions*

Here we list all functions and types from the auxiliary library in
alphabetical order.

luaL_addchar                                                    *luaL_addchar()*
>c
    void luaL_addchar (luaL_Buffer *B, char c);
<
        Adds the character `c` to the buffer `B` (see |luaL_Buffer|).

luaL_addlstring                                              *luaL_addlstring()*
>c
    void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);
<
        Adds the string pointed to by `s` with length `l` to the buffer `B`
        (see |luaL_Buffer|). The string may contain embedded zeros.

luaL_addsize                                                    *luaL_addsize()*
>c
    void luaL_addsize (luaL_Buffer *B, size_t n);
<
        Adds to the buffer `B` (see |luaL_Buffer|) a string of length
        `n` previously copied to the buffer area (see
        |luaL_prepbuffer()|).

luaL_addstring                                                *luaL_addstring()*
>c
    void luaL_addstring (luaL_Buffer *B, const char *s);
<
        Adds the zero-terminated string pointed to by `s` to the buffer `B`
        (see |luaL_Buffer|). The string may not contain embedded zeros.

luaL_addvalue                                                  *luaL_addvalue()*
>c
    void luaL_addvalue (luaL_Buffer *B);
<
        Adds the value at the top of the stack to the buffer `B` (see
        |luaL_Buffer|). Pops the value.

        This is the only function on string buffers that can (and must) be
        called with an extra element on the stack, which is the value to be
        added to the buffer.

luaL_argcheck                                                  *luaL_argcheck()*
>c
    void luaL_argcheck (lua_State *L,
                        int cond,
                        int narg,
                        const char *extramsg);
<
        Checks whether `cond` is true. If not, raises an error with the
        following message, where `func` is retrieved from the call stack:
>
               bad argument #<narg> to <func> (<extramsg>)
<

luaL_argerror                                                  *luaL_argerror()*
>c
    int luaL_argerror (lua_State *L, int narg, const char *extramsg);
<
        Raises an error with the following message, where `func` is retrieved
        from the call stack:
>
               bad argument #<narg> to <func> (<extramsg>)
<
        This function never returns, but it is an idiom to use it in C
        functions as `return luaL_argerror(` `args` `)`.

luaL_Buffer

Title: Lua Auxiliary Library: Introduction, Argument Checking, and Buffer Functions
Summary
This section delves into the Lua auxiliary library, highlighting its role in providing convenient functions for C-Lua interaction beyond the basic API. It emphasizes functions for checking C function arguments (`luaL_check*`, `luaL_opt*`) and then begins listing the functions and types from the library in alphabetical order, starting with buffer manipulation functions like `luaL_addchar`, `luaL_addlstring`, `luaL_addsize`, `luaL_addstring`, `luaL_addvalue`, along with error reporting functions `luaL_argcheck` and `luaL_argerror`.