Home Explore Blog CI



neovim

22th chunk of `runtime/doc/luaref.txt`
53860e2c44fdcd7e91a2bb96ed2f32629f65bda0389bcf550000000100000fa8
 arguments passed to `coroutine.resume`.

Like `coroutine.create`, the `coroutine.wrap` function (see
|coroutine.wrap()|) also creates a coroutine, but instead of returning
the coroutine itself, it returns a function that, when called, resumes the
coroutine. Any arguments passed to this function go as extra arguments to
`coroutine.resume`. `coroutine.wrap` returns all the values returned by
`coroutine.resume`, except the first one (the boolean error code). Unlike
`coroutine.resume`, `coroutine.wrap` does not catch errors; any error is
propagated to the caller.

As an example, consider the next code:
>lua
       function foo1 (a)
         print("foo", a)
         return coroutine.yield(2*a)
       end

       co = coroutine.create(function (a,b)
             print("co-body", a, b)
             local r = foo1(a+1)
             print("co-body", r)
             local r, s = coroutine.yield(a+b, a-b)
             print("co-body", r, s)
             return b, "end"
       end)

       print("main", coroutine.resume(co, 1, 10))
       print("main", coroutine.resume(co, "r"))
       print("main", coroutine.resume(co, "x", "y"))
       print("main", coroutine.resume(co, "x", "y"))
<
When you run it, it produces the following output:
>
       co-body 1       10
       foo     2
       main    true    4
       co-body r
       main    true    11      -9
       co-body x       y
       main    true    10      end
       main    false   cannot resume dead coroutine
<

==============================================================================
3  THE APPLICATION PROGRAM INTERFACE                                *lua-API*

This section describes the C API for Lua, that is, the set of C functions
available to the host program to communicate with Lua. All API functions and
related types and constants are declared in the header file `lua.h`.

Even when we use the term "function", any facility in the API may be provided
as a `macro` instead. All such macros use each of its arguments exactly once
(except for the first argument, which is always a Lua state), and so do not
generate hidden side-effects.

As in most C libraries, the Lua API functions do not check their arguments for
validity or consistency. However, you can change this behavior by compiling
Lua with a proper definition for the macro `luai_apicheck`,in file
`luaconf.h`.

==============================================================================
3.1  The Stack                                    *lua-stack* *lua-apiStack*

Lua uses a virtual stack to pass values to and from C. Each element in this
stack represents a Lua value (`nil`, number, string, etc.).

Whenever Lua calls C, the called function gets a new stack, which is
independent of previous stacks and of stacks of C functions that are still
active. This stack initially contains any arguments to the C function and it
is where the C function pushes its results to be returned to the caller (see
|lua_CFunction|).

                                                             *lua-stackindex*
For convenience, most query operations in the API do not follow a strict stack
discipline. Instead, they can refer to any element in the stack by using an
index: a positive index represents an absolute stack position (starting at 1);
a negative index represents an offset from the top of the stack. More
specifically, if the stack has `n` elements, then index 1 represents the first
element (that is, the element that was pushed onto the stack first) and index
`n` represents the last element; index `-1` also represents the last element
(that is, the element at the top) and index `-n` represents the first element.
We say that an index is valid if it lies between 1 and the stack top (that is,
if `1 <= abs(index) <= top`).

==============================================================================
3.2  Stack Size                                            *lua-apiStackSize*

When you interact with Lua API, you are responsible for ensuring consistency.

Title: Lua Coroutine Example and Introduction to the C API
Summary
This section continues the discussion on coroutines by providing a code example demonstrating how to create, resume, and yield from a coroutine. The example showcases the flow of execution and the values returned by `coroutine.resume` and `coroutine.yield`. Additionally, it introduces the Lua C API, the set of C functions that allows host programs to interact with Lua. It highlights the use of the `lua.h` header file and the use of macros in the API. The section notes that the API functions don't check arguments by default, but this can be changed using the `luai_apicheck` macro. It further describes the virtual stack used to pass values between C and Lua. This stack is independent for each C function call and can be accessed using positive or negative indices.