Home Explore Blog CI



neovim

58th chunk of `runtime/doc/luaref.txt`
e439b9d14380bacde3f7914c76157424dd5169ea6f7853d30000000100000fa1
 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 is
exported in a table `package`.

module({name} [, {...}])                                *module()*
        Creates a module. If there is a table in `package.loaded[name]`, this
        table is the module. Otherwise, if there is a global table `t` with
        the given name, this table is the module. Otherwise creates a new
        table `t` and sets it as the value of the global {name} and the value
        of `package.loaded[name]`. This function also initializes `t._NAME`
        with the given name, `t._M` with the module (`t` itself), and
        `t._PACKAGE` with the package name (the full module name minus last
        component; see below). Finally, `module` sets `t` as the new
        environment of the current function and the new value of
        `package.loaded[name]`, so that |require()| returns `t`.

        If {name} is a compound name (that is, one with components separated
        by dots), `module` creates (or reuses, if they already exist) tables
        for each component. For instance, if {name} is `a.b.c`, then `module`
        stores the module table in field `c` of field `b` of global `a`.

        This function may receive optional `options` after the module name,
        where each option is a function to be applied over the module.

require({modname})                                      *require()*
        Loads the given module. The function starts by looking into the
        `package.loaded` table to determine whether {modname} is already
        loaded. If it is, then `require` returns the value stored at
        `package.loaded[modname]`. Otherwise, it tries to find a `loader` for
        the module.

        To find a loader, first `require` queries `package.preload[modname]`.
        If it has a value, this value (which should be a function) is the
        loader. Otherwise `require` searches for a Lua loader using the path
        stored in `package.path`. If that also fails, it searches for a C
        loader using the path stored in `package.cpath`. If that also fails,
        it tries an `all-in-one` loader (see below).

        When loading a C library, `require` first uses a dynamic link facility
        to link the application with the library. Then it tries to find a C
        function inside this library to be used as the loader. The name of
        this C function is the string `"luaopen_"` concatenated with a copy of
        the module name where each dot is replaced by an underscore. Moreover,
        if the module name has a hyphen, its prefix up to (and including) the
        first hyphen is removed. For instance, if the module name is
        `a.v1-b.c`, the function name will be `luaopen_b_c`.

        If `require` finds neither a Lua library nor a C library for a module,
        it calls the `all-in-one loader`. This loader searches the C path for
        a library for the root name of the given module. For instance, when
        requiring `a.b.c`, it will search for a C library for `a`. If

Title: Lua Modules: Creation and Loading
Summary
This section delves into Lua modules, focusing on the `package` library that provides tools for loading and building modules. It details the `module` function, which creates modules and handles compound names, setting up the module table in various ways. The `require` function is explained, outlining its process for loading modules: checking `package.loaded`, searching for Lua loaders using `package.path`, then C loaders using `package.cpath`, and finally resorting to an 'all-in-one' loader.