Home Explore Blog CI



neovim

4th chunk of `runtime/doc/luaref.txt`
45f81337d4a4414c539595944b369e9a7448ecbc1ef4d9070000000100000fa0
 character, including embedded zeros
(`\0`) (see |lua-literal|).

Lua can call (and manipulate) functions written in Lua and functions written
in C (see |lua-function|).

                                                            *lua-userdatatype*
The type userdata is provided to allow arbitrary C data to be stored in Lua
variables. This type corresponds to a block of raw memory and has no
pre-defined operations in Lua, except assignment and identity test. However,
by using metatables, the programmer can define operations for userdata values
(see |lua-metatable|). Userdata values cannot be created or modified in Lua,
only through the C API. This guarantees the integrity of data owned by the
host program.

                                                                 *lua-thread*
The type `thread` represents independent threads of execution and it is used to
implement coroutines (see |lua-coroutine|). Do not confuse Lua threads with
operating-system threads. Lua supports coroutines on all systems, even those
that do not support threads.

                                                                  *lua-table*
The type `table` implements associative arrays, that is, arrays that can be
indexed not only with numbers, but with any value (except `nil`). Tables can
be heterogeneous; that is, they can contain values of all types (except
`nil`). Tables are the sole data structuring mechanism in Lua; they may be
used to represent ordinary arrays, symbol tables, sets, records, graphs,
trees, etc. To represent records, Lua uses the field name as an index. The
language supports this representation by providing `a.name` as syntactic sugar
for `a["name"]`. There are several convenient ways to create tables in Lua
(see |lua-tableconstructor|).

Like indices, the value of a table field can be of any type (except `nil`). In
particular, because functions are first-class values, table fields may contain
functions. Thus tables may also carry methods (see |lua-function-define|).

Tables, functions, threads and (full) userdata values are objects: variables
do not actually contain these values, only references to them. Assignment,
parameter passing, and function returns always manipulate references to such
values; these operations do not imply any kind of copy.

The library function `type` returns a string describing the type of a given
value (see |lua-type()|).

------------------------------------------------------------------------------
2.2.1  Coercion                                            *lua-coercion*

Lua provides automatic conversion between string and number values at run
time. Any arithmetic operation applied to a string tries to convert that
string to a number, following the usual conversion rules. Conversely, whenever
a number is used where a string is expected, the number is converted to a
string, in a reasonable format. For complete control of how numbers are
converted to strings, use the `format` function from the string library (see
|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

Title: Lua Types: Userdata, Threads, Tables, and Variable Concepts
Summary
This section details userdata (C data storage), threads (coroutines), and tables (associative arrays) in Lua. It explains how userdata allows C data storage with metatables, Lua threads as coroutines, and tables as versatile data structures. It further describes tables as objects (references), automatic type coercion between strings and numbers, and variable types: global, local, and table fields, with lexical scoping for locals.