Home Explore Blog CI



neovim

40th chunk of `runtime/doc/luaref.txt`
41026d57e8accd0cab1072ae4e3ccc19b9d5b820d7cd054f0000000100000fa4
 of chunk, `lua_dump` calls the writer,
        passing along the buffer to be written (`p`), its size (`sz`), and the
        `data` parameter supplied to `lua_dump`.

        The writer returns an error code: 0 means no errors; any other value
        means an error and stops `lua_dump` from calling the writer again.

lua_xmove                                                          *lua_xmove()*
>c
    void lua_xmove (lua_State *from, lua_State *to, int n);
<
        Exchange values between different threads of the `same` global state.

        This function pops `n` values from the stack `from`, and pushes them
        onto the stack `to`.

lua_yield                                                          *lua_yield()*
>c
    int lua_yield (lua_State *L, int nresults);
<
        Yields a coroutine.

        This function should only be called as the return expression of a C
        function, as follows:
>c
               return lua_yield (L, nresults);
<
        When a C function calls `lua_yield` in that way, the running coroutine
        suspends its execution, and the call to `lua_resume` (see
        |lua_resume()|) that started this coroutine returns. The
        parameter `nresults` is the number of values from the stack that are
        passed as results to `lua_resume`.

                                                           *lua-stackexample*
        As an example of stack manipulation, if the stack starts as
        `10 20 30 40 50*` (from bottom to top; the `*` marks the top), then
>
               lua_pushvalue(L, 3)    --> 10 20 30 40 50 30*
               lua_pushvalue(L, -1)   --> 10 20 30 40 50 30 30*
               lua_remove(L, -3)      --> 10 20 30 40 30 30*
               lua_remove(L,  6)      --> 10 20 30 40 30*
               lua_insert(L,  1)      --> 30 10 20 30 40*
               lua_insert(L, -1)      --> 30 10 20 30 40*  (no effect)
               lua_replace(L, 2)      --> 30 40 20 30*
               lua_settop(L, -3)      --> 30 40*
               lua_settop(L,  6)      --> 30 40 nil nil nil nil*
<

==============================================================================
3.8  The Debug Interface                                       *lua-apiDebug*

Lua has no built-in debugging facilities. Instead, it offers a special
interface by means of functions and hooks. This interface allows the
construction of different kinds of debuggers, profilers, and other tools that
need "inside information" from the interpreter.

lua_Debug                                                            *lua_Debug*

>c
    typedef struct lua_Debug {
        int event;
        const char *name;           /* (n) */
        const char *namewhat;       /* (n) */
        const char *what;           /* (S) */
        const char *source;         /* (S) */
        int currentline;            /* (l) */
        int nups;                   /* (u) number of upvalues */
        int linedefined;            /* (S) */
        int lastlinedefined;        /* (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

Title: Lua C API: Coroutine Yield, Stack Manipulation Example, and Debug Interface Overview
Summary
This section continues the Lua C API documentation, focusing on `lua_yield` for coroutine control, providing an example of Lua stack manipulation using functions like `lua_pushvalue`, `lua_remove`, `lua_insert`, `lua_replace`, and `lua_settop`. It then introduces the debug interface, explaining the `lua_Debug` structure, which is used to carry information about active functions, and describes the meaning of its fields, including `source`, `short_src`, `linedefined`, `lastlinedefined`, and `what`.