Home Explore Blog CI



neovim

5th chunk of `runtime/doc/luaref.txt`
2101c5f574b4d715c9e6e5f833ca0685cc0544fd43ae8ac20000000100000fa3

|string.format()|).

==============================================================================
2.3  Variables                                            *lua-variables*

Variables are places that store values. There are three kinds of variables in
Lua: global variables, local variables, and table fields.

A single name can denote a global variable or a local variable (or a
function's formal parameter, which is a particular form of local variable):
>
       var ::= Name
<
Name denotes identifiers, as defined in |lua-lexical|.

Any variable is assumed to be global unless explicitly declared as a local
(see |lua-local|). Local variables are lexically scoped: local
variables can be freely accessed by functions defined inside their scope (see
|lua-visibility|).

Before the first assignment to a variable, its value is `nil`.

Square brackets are used to index a table:
>
       var ::= prefixexp [ exp ]
<
The first expression (`prefixexp`) should result in a table value; the second
expression (`exp`) identifies a specific entry inside that table. The
expression denoting the table to be indexed has a restricted syntax; see
|lua-expressions| for details.

The syntax `var.NAME` is just syntactic sugar for `var["NAME"]` :
>
       var ::= prefixexp . Name
<
All global variables live as fields in ordinary Lua tables, called environment
tables or simply environments (see |lua-environments|). Each function
has its own reference to an environment, so that all global variables in this
function will refer to this environment table. When a function is created, it
inherits the environment from the function that created it. To get the
environment table of a Lua function, you call `getfenv` (see
|lua_getfenv()|). To replace it, you call `setfenv` (see |setfenv()|).
(You can only manipulate the environment of C functions through the debug
library; see |lua-lib-debug|.)

An access to a global variable `x` is equivalent to `_env.x`, which in turn is
equivalent to
>lua
       gettable_event(_env, "x")
<
where `_env` is the environment of the running function. (The `_env` variable is
not defined in Lua. We use it here only for explanatory purposes.)

The meaning of accesses to global variables and table fields can be changed
via metatables. An access to an indexed variable `t[i]` is equivalent to a
call `gettable_event(t,i)`. (See |lua-metatable| for a complete description of
the `gettable_event` function. This function is not defined or callable in
Lua. We use it here only for explanatory purposes.)

==============================================================================
2.4  Statements                                 *lua-statement*

Lua supports an almost conventional set of statements, similar to those in
Pascal or C. This set includes assignment, control structures, function
calls, and variable declarations.

------------------------------------------------------------------------------
2.4.1  Chunks                                   *lua-chunk*

The unit of execution of Lua is called a chunk. A chunk is simply a sequence
of statements, which are executed sequentially. Each statement can be
optionally followed by a semicolon:
>
       chunk ::= {stat [ ; ]}
<
There are no empty statements and thus `;;` is not legal.

Lua handles a chunk as the body of an anonymous function with a variable
number of arguments (see |lua-function-define|). As such, chunks can define
local variables, receive arguments, and return values.

A chunk may be stored in a file or in a string inside the host program. When a
chunk is executed, first it is pre-compiled into instructions for a virtual
machine, and then the compiled code is executed by an interpreter for the
virtual machine.

Chunks may also be pre-compiled into binary form; see program `luac` for
details. Programs in source and compiled forms are interchangeable; Lua
automatically detects the file type and acts accordingly.

------------------------------------------------------------------------------

Title: Lua Variables, Environments, and Chunks
Summary
This section elaborates on Lua variables (global, local, table fields), explaining their scope and access. It covers how global variables reside in environment tables, accessible via `getfenv` and modifiable via `setfenv`. It also explains how accessing global variables and table fields involves metatables and the `gettable_event` function. Furthermore, it introduces the concept of a 'chunk' as the unit of Lua execution, which is a sequence of statements compiled and executed by a virtual machine.