Home Explore Blog CI



neovim

31th chunk of `runtime/doc/luaref.txt`
9f9e98da1ba35883744dda24136d0173f5c9ea108a2d48170000000100000fa0
 operator (that is, may call metamethods). Otherwise returns 0.
        Also returns 0 if any of the indices is non valid.

lua_load                                                            *lua_load()*
>c
    int lua_load (lua_State *L,
                  lua_Reader reader,
                  void *data,
                  const char *chunkname);
<
        Loads a Lua chunk. If there are no errors, `lua_load` pushes the
        compiled chunk as a Lua function on top of the stack. Otherwise, it
        pushes an error message. The return values of `lua_load` are:

         - `0`: no errors;
         - `LUA_ERRSYNTAX` : syntax error during pre-compilation;
         - `LUA_ERRMEM` : memory allocation error.

        This function only loads a chunk; it does not run it.

        `lua_load` automatically detects whether the chunk is text or binary,
        and loads it accordingly (see program `luac`).

        The `lua_load` function uses a user-supplied `reader` function to read
        the chunk (see |lua_Reader|). The `data` argument is an opaque
        value passed to the reader function.

        The `chunkname` argument gives a name to the chunk, which is used for
        error messages and in debug information (see |lua-apiDebug|).

lua_newstate                                                    *lua_newstate()*
>c
    lua_State *lua_newstate (lua_Alloc f, void *ud);
<
        Creates a new, independent state. Returns `NULL` if cannot create the
        state (due to lack of memory). The argument `f` is the allocator
        function; Lua does all memory allocation for this state through this
        function. The second argument, `ud`, is an opaque pointer that Lua
        simply passes to the allocator in every call.

lua_newtable                                                    *lua_newtable()*
>c
    void lua_newtable (lua_State *L);
<
        Creates a new empty table and pushes it onto the stack. It is
        equivalent to `lua_createtable(L, 0, 0)` (see
        |lua_createtable()|).

lua_newthread                                                  *lua_newthread()*
>c
    lua_State *lua_newthread (lua_State *L);
<
        Creates a new thread, pushes it on the stack, and returns a pointer to
        a `lua_State`  (see |lua_State|) that represents this new
        thread. The new state returned by this function shares with the
        original state all global objects (such as tables), but has an
        independent execution stack.

        There is no explicit function to close or to destroy a thread. Threads
        are subject to garbage collection, like any Lua object.

lua_newuserdata                                              *lua_newuserdata()*
>c
    void *lua_newuserdata (lua_State *L, size_t size);
<
        This function allocates a new block of memory with the given size,
        pushes onto the stack a new full userdata with the block address, and
        returns this address.
                                                                    *userdata*
        Userdata represents C values in Lua. A full userdata represents a
        block of memory. It is an object (like a table): you must create it,
        it can have its own metatable, and you can detect when it is being
        collected. A full userdata is only equal to itself (under raw
        equality).

        When Lua collects a full userdata with a `gc` metamethod, Lua calls
        the metamethod and marks the userdata as finalized. When this userdata
        is collected again then Lua frees its corresponding memory.

lua_next                                                            *lua_next()*
>c
    int lua_next (lua_State *L, int index);
<
        Pops a key from the stack, and pushes a key-value pair from the table
        at the given index (the "next" pair after the given key). If there are
        no more elements in the table, then `lua_next` returns 0 (and pushes
        nothing).

                                       

Title: Lua C API: Chunk Loading, State Management, Table/Thread/Userdata Creation, and Table Iteration
Summary
This section covers several Lua C API functions. `lua_load` loads a Lua chunk from a reader function. `lua_newstate` creates a new Lua state with a custom allocator. `lua_newtable` creates a new empty table. `lua_newthread` creates a new thread. `lua_newuserdata` allocates a block of memory and creates a new full userdata object on the stack. `lua_next` iterates through a table, retrieving key-value pairs.