Home Explore Blog CI



neovim

57th chunk of `runtime/doc/luaref.txt`
5b229e35fbac077e3ad24bf01ea9b9ea3d67d78b175f17510000000100000fa4
 current interpreter version. The current contents of this string is
        `"Lua 5.1"` .

xpcall({f}, {err})                                      *xpcall()*
        This function is similar to `pcall` (see |pcall()|), except that you
        can set a new error handler.

        `xpcall` calls function {f} in protected mode, using {err} as the
        error handler. Any error inside {f} is not propagated; instead,
        `xpcall` catches the error, calls the {err} function with the original
        error object, and returns a status code. Its first result is the
        status code (a boolean), which is true if the call succeeds without
        errors. In this case, `xpcall` also returns all results from the call,
        after this first result. In case of any error, `xpcall` returns
        `false` plus the result from {err}.

==============================================================================
5.2  Coroutine Manipulation                     *lua-lib-coroutine*

The operations related to coroutines comprise a sub-library of the basic
library and come inside the table `coroutine`. See |lua-coroutine| for a
general description of coroutines.

coroutine.create({f})                                       *coroutine.create()*
        Creates a new coroutine, with body {f}. {f} must be a Lua function.
        Returns this new coroutine, an object with type `"thread"`.

coroutine.resume({co} [, {val1}, {...}])                    *coroutine.resume()*
        Starts or continues the execution of coroutine {co}. The first time
        you resume a coroutine, it starts running its body. The values {val1},
        {...} are passed as arguments to the body function. If the coroutine has
        yielded, `resume` restarts it; the values {val1}, {...} are passed as
        the results from the yield.

        If the coroutine runs without any errors, `resume` returns `true` plus
        any values passed to `yield` (if the coroutine yields) or any values
        returned by the body function(if the coroutine terminates). If there
        is any error, `resume` returns `false` plus the error message.

coroutine.running()                                        *coroutine.running()*
        Returns the running coroutine, or `nil` when called by the main
        thread.

coroutine.status({co})                                      *coroutine.status()*
        Returns the status of coroutine {co}, as a string: `"running"`, if the
        coroutine is running (that is, it called `status`); `"suspended"`, if
        the coroutine is suspended in a call to `yield`, or if it has not
        started running yet; `"normal"` if the coroutine is active but not
        running (that is, it has resumed another coroutine); and `"dead"` if
        the coroutine has finished its body function, or if it has stopped
        with an error.

coroutine.wrap({f})                                           *coroutine.wrap()*
        Creates a new coroutine, with body {f}. {f} must be a Lua function.
        Returns a function that resumes the coroutine each time it is called.
        Any arguments passed to the function behave as the extra arguments to
        `resume`. Returns the same values returned by `resume`, except the
        first boolean. In case of error, propagates the error.

coroutine.yield({...})                                       *coroutine.yield()*
        Suspends the execution of the calling coroutine. The coroutine cannot
        be running a C function, a metamethod, or an |iterator|. Any arguments
        to `yield` are passed as extra results to `resume`.

==============================================================================
5.3 Modules                                                   *lua-modules*

The package library provides basic facilities for loading and building modules
in Lua. It exports two of its functions directly in the global environment:
`require` and `module` (see |require()| and |module()|). Everything else

Title: Lua: xpcall and Coroutine Manipulation
Summary
This section describes the `xpcall` function, which is similar to `pcall` but allows setting a custom error handler. It then introduces coroutine manipulation functions within the `coroutine` table: `coroutine.create` (creates a new coroutine), `coroutine.resume` (starts or continues a coroutine's execution), `coroutine.running` (returns the running coroutine), `coroutine.status` (returns the status of a coroutine), `coroutine.wrap` (creates a function that resumes a coroutine each time it is called), and `coroutine.yield` (suspends the execution of the calling coroutine). Finally, it introduces the concept of modules in Lua.