aboutsummaryrefslogtreecommitdiff
path: root/manual/manual.of
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--manual/manual.of350
1 files changed, 227 insertions, 123 deletions
diff --git a/manual/manual.of b/manual/manual.of
index 7cd0d4db..b2765774 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -213,11 +213,89 @@ of a given value @seeF{type}.
213 213
214} 214}
215 215
216@sect2{globalenv| @title{Environments and the Global Environment} 216@sect2{globalenv| @title{Scopes, Variables, and Environments}
217@index{visibility}
218
219A variable name refers to a global or a local variable according
220to the declaration that is in context at that point of the code.
221(For the purposes of this discussion,
222a function's formal parameter is equivalent to a local variable.)
223
224All chunks start with an implicit declaration @T{global *},
225which declares all free names as global variables;
226this preambular declaration becomes void inside the scope of any other
227@Rw{global} declaration,
228as the following example illustrates:
229@verbatim{
230X = 1 -- Ok, global by default
231do
232 global Y -- voids the implicit initial declaration
233 Y = 1 -- Ok, Y declared as global
234 X = 1 -- ERROR, X not declared
235end
236X = 2 -- Ok, global by default again
237}
238So, outside any global declaration,
239Lua works as @x{global-by-default}.
240Inside any global declaration,
241Lua works without a default:
242All variables must be declared.
243
244Lua is a lexically scoped language.
245The scope of a variable declaration begins at the first statement after
246the declaration and lasts until the last non-void statement
247of the innermost block that includes the declaration.
248(@emph{Void statements} are labels and empty statements.)
249
250A declaration shadows any declaration for the same name that
251is in context at the point of the declaration. Inside this
252shadow, any outer declaration for that name is void.
253See the next example:
254@verbatim{
255global print, x
256x = 10 -- global variable
257do -- new block
258 local x = x -- new 'x', with value 10
259 print(x) --> 10
260 x = x+1
261 do -- another block
262 local x = x+1 -- another 'x'
263 print(x) --> 12
264 end
265 print(x) --> 11
266end
267print(x) --> 10 (the global one)
268}
269
270Notice that, in a declaration like @T{local x = x},
271the new @id{x} being declared is not in scope yet,
272and so the @id{x} in the right-hand side refers to the outside variable.
273
274Because of the @x{lexical scoping} rules,
275local variables can be freely accessed by functions
276defined inside their scope.
277A local variable used by an inner function is called an @def{upvalue}
278(or @emphx{external local variable}, or simply @emphx{external variable})
279inside the inner function.
280
281Notice that each execution of a @Rw{local} statement
282defines new local variables.
283Consider the following example:
284@verbatim{
285a = {}
286local x = 20
287for i = 1, 10 do
288 local y = 0
289 a[i] = function () y = y + 1; return x + y end
290end
291}
292The loop creates ten closures
293(that is, ten instances of the anonymous function).
294Each of these closures uses a different @id{y} variable,
295while all of them share the same @id{x}.
217 296
218As we will discuss further in @refsec{variables} and @refsec{assignment}, 297As we will discuss further in @refsec{variables} and @refsec{assignment},
219any reference to a free name 298any reference to a global variable @id{var}
220(that is, a name not bound to any declaration) @id{var}
221is syntactically translated to @T{_ENV.var}. 299is syntactically translated to @T{_ENV.var}.
222Moreover, every chunk is compiled in the scope of 300Moreover, every chunk is compiled in the scope of
223an external local variable named @id{_ENV} @see{chunks}, 301an external local variable named @id{_ENV} @see{chunks},
@@ -225,12 +303,14 @@ so @id{_ENV} itself is never a free name in a chunk.
225 303
226Despite the existence of this external @id{_ENV} variable and 304Despite the existence of this external @id{_ENV} variable and
227the translation of free names, 305the translation of free names,
228@id{_ENV} is a completely regular name. 306@id{_ENV} is a regular name.
229In particular, 307In particular,
230you can define new variables and parameters with that name. 308you can define new variables and parameters with that name.
231Each reference to a free name uses the @id{_ENV} that is 309(However, you should not define @id{_ENV} as a global variable,
232visible at that point in the program, 310otherwise @T{_ENV.var} would translate to
233following the usual visibility rules of Lua @see{visibility}. 311@T{_ENV._ENV.var} and so on, in an infinite loop.)
312Each reference to a global variable name uses the @id{_ENV} that is
313visible at that point in the program.
234 314
235Any table used as the value of @id{_ENV} is called an @def{environment}. 315Any table used as the value of @id{_ENV} is called an @def{environment}.
236 316
@@ -244,8 +324,8 @@ When Lua loads a chunk,
244the default value for its @id{_ENV} variable 324the default value for its @id{_ENV} variable
245is the global environment @seeF{load}. 325is the global environment @seeF{load}.
246Therefore, by default, 326Therefore, by default,
247free names in Lua code refer to entries in the global environment 327global variables in Lua code refer to entries in the global environment
248and, therefore, they are also called @def{global variables}. 328and, therefore, they act as conventional global variables.
249Moreover, all standard libraries are loaded in the global environment 329Moreover, all standard libraries are loaded in the global environment
250and some functions there operate on that environment. 330and some functions there operate on that environment.
251You can use @Lid{load} (or @Lid{loadfile}) 331You can use @Lid{load} (or @Lid{loadfile})
@@ -1031,9 +1111,9 @@ and cannot be used as names:
1031@index{reserved words} 1111@index{reserved words}
1032@verbatim{ 1112@verbatim{
1033and break do else elseif end 1113and break do else elseif end
1034false for function goto if in 1114false for function global goto if
1035local nil not or repeat return 1115in local nil not or repeat
1036then true until while 1116return then true until while
1037} 1117}
1038 1118
1039Lua is a case-sensitive language: 1119Lua is a case-sensitive language:
@@ -1198,17 +1278,15 @@ global variables, local variables, and table fields.
1198 1278
1199A single name can denote a global variable or a local variable 1279A single name can denote a global variable or a local variable
1200(or a function's formal parameter, 1280(or a function's formal parameter,
1201which is a particular kind of local variable): 1281which is a particular kind of local variable) @see{globalenv}:
1202@Produc{ 1282@Produc{
1203@producname{var}@producbody{@bnfNter{Name}} 1283@producname{var}@producbody{@bnfNter{Name}}
1204} 1284}
1205@bnfNter{Name} denotes identifiers @see{lexical}. 1285@bnfNter{Name} denotes identifiers @see{lexical}.
1206 1286
1207Any variable name is assumed to be global unless explicitly declared 1287Because variables are @emph{lexically scoped},
1208as a local @see{localvar}.
1209@x{Local variables} are @emph{lexically scoped}:
1210local variables can be freely accessed by functions 1288local variables can be freely accessed by functions
1211defined inside their scope @see{visibility}. 1289defined inside their scope @see{globalenv}.
1212 1290
1213Before the first assignment to a variable, its value is @nil. 1291Before the first assignment to a variable, its value is @nil.
1214 1292
@@ -1227,8 +1305,6 @@ The syntax @id{var.Name} is just syntactic sugar for
1227 1305
1228An access to a global variable @id{x} 1306An access to a global variable @id{x}
1229is equivalent to @id{_ENV.x}. 1307is equivalent to @id{_ENV.x}.
1230Due to the way that chunks are compiled,
1231the variable @id{_ENV} itself is never global @see{globalenv}.
1232 1308
1233} 1309}
1234 1310
@@ -1326,6 +1402,8 @@ Chunks can also be precompiled into binary form;
1326see the program @idx{luac} and the function @Lid{string.dump} for details. 1402see the program @idx{luac} and the function @Lid{string.dump} for details.
1327Programs in source and compiled forms are interchangeable; 1403Programs in source and compiled forms are interchangeable;
1328Lua automatically detects the file type and acts accordingly @seeF{load}. 1404Lua automatically detects the file type and acts accordingly @seeF{load}.
1405Be aware that, unlike source code,
1406maliciously crafted binary chunks can crash the interpreter.
1329 1407
1330} 1408}
1331 1409
@@ -1428,7 +1506,7 @@ labels in Lua are considered statements too:
1428A label is visible in the entire block where it is defined, 1506A label is visible in the entire block where it is defined,
1429except inside nested functions. 1507except inside nested functions.
1430A goto can jump to any visible label as long as it does not 1508A goto can jump to any visible label as long as it does not
1431enter into the scope of a local variable. 1509enter into the scope of a variable declaration.
1432A label should not be declared 1510A label should not be declared
1433where a previous label with the same name is visible, 1511where a previous label with the same name is visible,
1434even if this other label has been declared in an enclosing block. 1512even if this other label has been declared in an enclosing block.
@@ -1571,35 +1649,85 @@ Function calls are explained in @See{functioncall}.
1571 1649
1572} 1650}
1573 1651
1574@sect3{localvar| @title{Local Declarations} 1652@sect3{localvar| @title{Variable Declarations}
1575@x{Local variables} can be declared anywhere inside a block. 1653Local and global variables can be declared anywhere inside a block.
1576The declaration can include an initialization: 1654The declaration can include an initialization:
1577@Produc{ 1655@Produc{
1578@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} 1656@producname{stat}@producbody{@Rw{local}
1579@producname{attnamelist}@producbody{ 1657 attnamelist @bnfopt{@bnfter{=} explist}}
1580 @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} 1658@producname{stat}@producbody{@Rw{global}
1659 attnamelist @bnfopt{@bnfter{=} explist}}
1581} 1660}
1582If present, an initial assignment has the same semantics 1661If present, an initial assignment has the same semantics
1583of a multiple assignment @see{assignment}. 1662of a multiple assignment @see{assignment}.
1584Otherwise, all variables are initialized with @nil. 1663Otherwise, all local variables are initialized with @nil.
1585 1664
1586Each variable name may be postfixed by an attribute 1665The list of names may be prefixed by an attribute
1587(a name between angle brackets): 1666(a name between angle brackets)
1667and each variable name may be postfixed by an attribute:
1588@Produc{ 1668@Produc{
1589@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} 1669@producname{attnamelist}@producbody{
1670 @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib}
1671 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
1672@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
1590} 1673}
1674A prefixed attribute applies to all names in the list;
1675a postfixed attribute applies to its particular name.
1591There are two possible attributes: 1676There are two possible attributes:
1592@id{const}, which declares a @emph{constant} or @emph{read-only} variable, 1677@id{const}, which declares a @emph{constant} or @emph{read-only} variable,
1593@index{constant variable} 1678@index{constant variable}
1594that is, a variable that cannot be assigned to 1679that is, a variable that cannot be used as the left-hand side of an
1595after its initialization; 1680assignment,
1596and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. 1681and @id{close}, which declares a to-be-closed variable @see{to-be-closed}.
1682Only local variables can have the @id{close} attribute.
1597A list of variables can contain at most one to-be-closed variable. 1683A list of variables can contain at most one to-be-closed variable.
1598 1684
1685Lua offers also a collective declaration for global variables:
1686@Produc{
1687@producname{stat}@producbody{@Rw{global} @bnfopt{attrib} @bnfter{*}}
1688}
1689This special form implicitly declares
1690as globals all names not explicitly declared previously.
1691In particular,
1692@T{global<const> *} implicitly declares
1693as read-only globals all names not explicitly declared previously;
1694see the following example:
1695@verbatim{
1696global X
1697global<const> *
1698print(math.pi) -- Ok, 'print' and 'math' are read-only
1699X = 1 -- Ok, declared as read-write
1700Y = 1 -- Error, Y is read-only
1701}
1702
1703As noted in @See{globalenv},
1704all chunks start with an implicit declaration @T{global *},
1705but this preambular declaration becomes void inside
1706the scope of any other @Rw{global} declaration.
1707Therefore, a program that does not use global declarations
1708or start with @T{global *}
1709has free read-write access to any global;
1710a program that starts with @T{global<const> *}
1711has free read-only access to any global;
1712and a program that starts with any other global declaration
1713(e.g., @T{global none}) can only refer to declared variables.
1714
1715Note that, for global variables,
1716the effect of any declaration is only syntactical
1717(except for the optional assignment):
1718@verbatim{
1719global X <const>, _G
1720X = 1 -- ERROR
1721_ENV.X = 1 -- Ok
1722_G.print(X) -- Ok
1723foo() -- 'foo' can freely change any global
1724}
1725
1599A chunk is also a block @see{chunks}, 1726A chunk is also a block @see{chunks},
1600and so local variables can be declared in a chunk outside any explicit block. 1727and so variables can be declared in a chunk outside any explicit block.
1601 1728
1602The visibility rules for local variables are explained in @See{visibility}. 1729The visibility rules for variable declarations
1730are explained in @See{globalenv}.
1603 1731
1604} 1732}
1605 1733
@@ -2142,6 +2270,7 @@ The following syntactic sugar simplifies function definitions:
2142@Produc{ 2270@Produc{
2143@producname{stat}@producbody{@Rw{function} funcname funcbody} 2271@producname{stat}@producbody{@Rw{function} funcname funcbody}
2144@producname{stat}@producbody{@Rw{local} @Rw{function} @bnfNter{Name} funcbody} 2272@producname{stat}@producbody{@Rw{local} @Rw{function} @bnfNter{Name} funcbody}
2273@producname{stat}@producbody{@Rw{global} @Rw{function} @bnfNter{Name} funcbody}
2145@producname{funcname}@producbody{@bnfNter{Name} @bnfrep{@bnfter{.} @bnfNter{Name}} @bnfopt{@bnfter{:} @bnfNter{Name}}} 2274@producname{funcname}@producbody{@bnfNter{Name} @bnfrep{@bnfter{.} @bnfNter{Name}} @bnfopt{@bnfter{:} @bnfNter{Name}}}
2146} 2275}
2147The statement 2276The statement
@@ -2160,6 +2289,7 @@ translates to
2160@verbatim{ 2289@verbatim{
2161t.a.b.c.f = function () @rep{body} end 2290t.a.b.c.f = function () @rep{body} end
2162} 2291}
2292
2163The statement 2293The statement
2164@verbatim{ 2294@verbatim{
2165local function f () @rep{body} end 2295local function f () @rep{body} end
@@ -2173,7 +2303,15 @@ not to
2173local f = function () @rep{body} end 2303local f = function () @rep{body} end
2174} 2304}
2175(This only makes a difference when the body of the function 2305(This only makes a difference when the body of the function
2176contains references to @id{f}.) 2306contains recursive references to @id{f}.)
2307Similarly, the statement
2308@verbatim{
2309global function f () @rep{body} end
2310}
2311translates to
2312@verbatim{
2313global f; f = function () @rep{body} end
2314}
2177 2315
2178A function definition is an executable expression, 2316A function definition is an executable expression,
2179whose value has type @emph{function}. 2317whose value has type @emph{function}.
@@ -2236,7 +2374,7 @@ then the function returns with no results.
2236@index{multiple return} 2374@index{multiple return}
2237There is a system-dependent limit on the number of values 2375There is a system-dependent limit on the number of values
2238that a function may return. 2376that a function may return.
2239This limit is guaranteed to be greater than 1000. 2377This limit is guaranteed to be at least 1000.
2240 2378
2241The @emphx{colon} syntax 2379The @emphx{colon} syntax
2242is used to emulate @def{methods}, 2380is used to emulate @def{methods},
@@ -2281,8 +2419,8 @@ for instance @T{foo(e1, e2, e3)} @see{functioncall}.}
2281@item{A multiple assignment, 2419@item{A multiple assignment,
2282for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} 2420for instance @T{a , b, c = e1, e2, e3} @see{assignment}.}
2283 2421
2284@item{A local declaration, 2422@item{A local or global declaration,
2285for instance @T{local a , b, c = e1, e2, e3} @see{localvar}.} 2423which is a special case of multiple assignment.}
2286 2424
2287@item{The initial values in a generic @rw{for} loop, 2425@item{The initial values in a generic @rw{for} loop,
2288for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} 2426for instance @T{for k in e1, e2, e3 do ... end} @see{for}.}
@@ -2293,8 +2431,7 @@ the list of values from the list of expressions
2293must be @emph{adjusted} to a specific length: 2431must be @emph{adjusted} to a specific length:
2294the number of parameters in a call to a non-variadic function 2432the number of parameters in a call to a non-variadic function
2295@see{func-def}, 2433@see{func-def},
2296the number of variables in a multiple assignment or 2434the number of variables in a multiple assignment or a declaration,
2297a local declaration,
2298and exactly four values for a generic @rw{for} loop. 2435and exactly four values for a generic @rw{for} loop.
2299The @def{adjustment} follows these rules: 2436The @def{adjustment} follows these rules:
2300If there are more values than needed, 2437If there are more values than needed,
@@ -2356,58 +2493,6 @@ return x,y,f() -- returns x, y, and all results from f().
2356 2493
2357} 2494}
2358 2495
2359@sect2{visibility| @title{Visibility Rules}
2360
2361@index{visibility}
2362Lua is a lexically scoped language.
2363The scope of a local variable begins at the first statement after
2364its declaration and lasts until the last non-void statement
2365of the innermost block that includes the declaration.
2366(@emph{Void statements} are labels and empty statements.)
2367Consider the following example:
2368@verbatim{
2369x = 10 -- global variable
2370do -- new block
2371 local x = x -- new 'x', with value 10
2372 print(x) --> 10
2373 x = x+1
2374 do -- another block
2375 local x = x+1 -- another 'x'
2376 print(x) --> 12
2377 end
2378 print(x) --> 11
2379end
2380print(x) --> 10 (the global one)
2381}
2382
2383Notice that, in a declaration like @T{local x = x},
2384the new @id{x} being declared is not in scope yet,
2385and so the second @id{x} refers to the outside variable.
2386
2387Because of the @x{lexical scoping} rules,
2388local variables can be freely accessed by functions
2389defined inside their scope.
2390A local variable used by an inner function is called an @def{upvalue}
2391(or @emphx{external local variable}, or simply @emphx{external variable})
2392inside the inner function.
2393
2394Notice that each execution of a @Rw{local} statement
2395defines new local variables.
2396Consider the following example:
2397@verbatim{
2398a = {}
2399local x = 20
2400for i = 1, 10 do
2401 local y = 0
2402 a[i] = function () y = y + 1; return x + y end
2403end
2404}
2405The loop creates ten closures
2406(that is, ten instances of the anonymous function).
2407Each of these closures uses a different @id{y} variable,
2408while all of them share the same @id{x}.
2409
2410}
2411 2496
2412} 2497}
2413 2498
@@ -3182,17 +3267,25 @@ when called through this function.
3182 3267
3183Resets a thread, cleaning its call stack and closing all pending 3268Resets a thread, cleaning its call stack and closing all pending
3184to-be-closed variables. 3269to-be-closed variables.
3185Returns a status code: 3270The parameter @id{from} represents the coroutine that is resetting @id{L}.
3271If there is no such coroutine,
3272this parameter can be @id{NULL}.
3273
3274Unless @id{L} is equal to @id{from},
3275the call returns a status code:
3186@Lid{LUA_OK} for no errors in the thread 3276@Lid{LUA_OK} for no errors in the thread
3187(either the original error that stopped the thread or 3277(either the original error that stopped the thread or
3188errors in closing methods), 3278errors in closing methods),
3189or an error status otherwise. 3279or an error status otherwise.
3190In case of error, 3280In case of error,
3191leaves the error object on the top of the stack. 3281the error object is put on the top of the stack.
3192 3282
3193The parameter @id{from} represents the coroutine that is resetting @id{L}. 3283If @id{L} is equal to @id{from},
3194If there is no such coroutine, 3284it corresponds to a thread closing itself.
3195this parameter can be @id{NULL}. 3285In that case,
3286the call does not return;
3287instead, the resume that (re)started the thread returns.
3288The thread must be running inside a resume.
3196 3289
3197} 3290}
3198 3291
@@ -3832,8 +3925,8 @@ This macro may evaluate its arguments more than once.
3832 3925
3833} 3926}
3834 3927
3835@APIEntry{unsigned (lua_numbertocstring) (lua_State *L, int idx, 3928@APIEntry{unsigned lua_numbertocstring (lua_State *L, int idx,
3836 char *buff);| 3929 char *buff);|
3837@apii{0,0,-} 3930@apii{0,0,-}
3838 3931
3839Converts the number at acceptable index @id{idx} to a string 3932Converts the number at acceptable index @id{idx} to a string
@@ -3958,7 +4051,7 @@ This function is equivalent to @Lid{lua_pushcclosure} with no upvalues.
3958 4051
3959} 4052}
3960 4053
3961@APIEntry{const char *(lua_pushexternalstring) (lua_State *L, 4054@APIEntry{const char *lua_pushexternalstring (lua_State *L,
3962 const char *s, size_t len, lua_Alloc falloc, void *ud);| 4055 const char *s, size_t len, lua_Alloc falloc, void *ud);|
3963@apii{0,1,m} 4056@apii{0,1,m}
3964 4057
@@ -3981,11 +4074,6 @@ the string @id{s} as the block,
3981the length plus one (to account for the ending zero) as the old size, 4074the length plus one (to account for the ending zero) as the old size,
3982and 0 as the new size. 4075and 0 as the new size.
3983 4076
3984Lua always @x{internalizes} strings with lengths up to 40 characters.
3985So, for strings in that range,
3986this function will immediately internalize the string
3987and call @id{falloc} to free the buffer.
3988
3989Even when using an external buffer, 4077Even when using an external buffer,
3990Lua still has to allocate a header for the string. 4078Lua still has to allocate a header for the string.
3991In case of a memory-allocation error, 4079In case of a memory-allocation error,
@@ -6601,11 +6689,10 @@ It may be the string @St{b} (only @x{binary chunk}s),
6601or @St{bt} (both binary and text). 6689or @St{bt} (both binary and text).
6602The default is @St{bt}. 6690The default is @St{bt}.
6603 6691
6604It is safe to load malformed binary chunks; 6692Lua does not check the consistency of binary chunks.
6605@id{load} signals an appropriate error. 6693Maliciously crafted binary chunks can crash
6606However, 6694the interpreter.
6607Lua does not check the consistency of the code inside binary chunks; 6695You can use the @id{mode} parameter to prevent loading binary chunks.
6608running maliciously crafted bytecode can crash the interpreter.
6609 6696
6610} 6697}
6611 6698
@@ -6817,7 +6904,7 @@ and @St{userdata}.
6817 6904
6818A global variable (not a function) that 6905A global variable (not a function) that
6819holds a string containing the running Lua version. 6906holds a string containing the running Lua version.
6820The current value of this variable is @St{Lua 5.4}. 6907The current value of this variable is @St{Lua 5.5}.
6821 6908
6822} 6909}
6823 6910
@@ -6854,18 +6941,26 @@ which come inside the table @defid{coroutine}.
6854See @See{coroutine} for a general description of coroutines. 6941See @See{coroutine} for a general description of coroutines.
6855 6942
6856 6943
6857@LibEntry{coroutine.close (co)| 6944@LibEntry{coroutine.close ([co])|
6858 6945
6859Closes coroutine @id{co}, 6946Closes coroutine @id{co},
6860that is, 6947that is,
6861closes all its pending to-be-closed variables 6948closes all its pending to-be-closed variables
6862and puts the coroutine in a dead state. 6949and puts the coroutine in a dead state.
6863The given coroutine must be dead or suspended. 6950The default for @id{co} is the running coroutine.
6864In case of error 6951
6952The given coroutine must be dead, suspended,
6953or be the running coroutine.
6954For the running coroutine,
6955this function does not return.
6956Instead, the resume that (re)started the coroutine returns.
6957
6958For other coroutines,
6959in case of error
6865(either the original error that stopped the coroutine or 6960(either the original error that stopped the coroutine or
6866errors in closing methods), 6961errors in closing methods),
6867returns @false plus the error object; 6962this function returns @false plus the error object;
6868otherwise returns @true. 6963otherwise ir returns @true.
6869 6964
6870} 6965}
6871 6966
@@ -7055,7 +7150,7 @@ to search for a @N{C loader}.
7055 7150
7056Lua initializes the @N{C path} @Lid{package.cpath} in the same way 7151Lua initializes the @N{C path} @Lid{package.cpath} in the same way
7057it initializes the Lua path @Lid{package.path}, 7152it initializes the Lua path @Lid{package.path},
7058using the environment variable @defid{LUA_CPATH_5_4}, 7153using the environment variable @defid{LUA_CPATH_5_5},
7059or the environment variable @defid{LUA_CPATH}, 7154or the environment variable @defid{LUA_CPATH},
7060or a default path defined in @id{luaconf.h}. 7155or a default path defined in @id{luaconf.h}.
7061 7156
@@ -7124,7 +7219,7 @@ A string with the path used by @Lid{require}
7124to search for a Lua loader. 7219to search for a Lua loader.
7125 7220
7126At start-up, Lua initializes this variable with 7221At start-up, Lua initializes this variable with
7127the value of the environment variable @defid{LUA_PATH_5_4} or 7222the value of the environment variable @defid{LUA_PATH_5_5} or
7128the environment variable @defid{LUA_PATH} or 7223the environment variable @defid{LUA_PATH} or
7129with a default path defined in @id{luaconf.h}, 7224with a default path defined in @id{luaconf.h},
7130if those environment variables are not defined. 7225if those environment variables are not defined.
@@ -7495,9 +7590,9 @@ x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
7495 end) 7590 end)
7496-- x="4+5 = 9" 7591-- x="4+5 = 9"
7497 7592
7498local t = {name="lua", version="5.4"} 7593local t = {name="lua", version="5.5"}
7499x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) 7594x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
7500-- x="lua-5.4.tar.gz" 7595-- x="lua-5.5.tar.gz"
7501} 7596}
7502 7597
7503} 7598}
@@ -9233,7 +9328,7 @@ when the standard input (@id{stdin}) is a terminal,
9233and as @T{lua -} otherwise. 9328and as @T{lua -} otherwise.
9234 9329
9235When called without the option @T{-E}, 9330When called without the option @T{-E},
9236the interpreter checks for an environment variable @defid{LUA_INIT_5_4} 9331the interpreter checks for an environment variable @defid{LUA_INIT_5_5}
9237(or @defid{LUA_INIT} if the versioned name is not defined) 9332(or @defid{LUA_INIT} if the versioned name is not defined)
9238before running any argument. 9333before running any argument.
9239If the variable content has the format @T{@At@rep{filename}}, 9334If the variable content has the format @T{@At@rep{filename}},
@@ -9408,7 +9503,12 @@ change between versions.
9408@itemize{ 9503@itemize{
9409 9504
9410@item{ 9505@item{
9411The control variable in @Rw{for} loops are read only. 9506The word @Rw{global} is a reserved word.
9507Do not use it as a regular name.
9508}
9509
9510@item{
9511The control variable in @Rw{for} loops is read only.
9412If you need to change it, 9512If you need to change it,
9413declare a local variable with the same name in the loop body. 9513declare a local variable with the same name in the loop body.
9414} 9514}
@@ -9534,13 +9634,17 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9534@OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end} 9634@OrNL @Rw{for} namelist @Rw{in} explist @Rw{do} block @Rw{end}
9535@OrNL @Rw{function} funcname funcbody 9635@OrNL @Rw{function} funcname funcbody
9536@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody 9636@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody
9637@OrNL @Rw{global} @Rw{function} @bnfNter{Name} funcbody
9537@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} 9638@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist}
9639@OrNL @Rw{global} attnamelist
9640@OrNL @Rw{global} @bnfopt{attrib} @bnfter{*}
9538} 9641}
9539 9642
9540@producname{attnamelist}@producbody{ 9643@producname{attnamelist}@producbody{
9541 @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} 9644 @bnfopt{attrib} @bnfNter{Name} @bnfopt{attrib}
9645 @bnfrep{@bnfter{,} @bnfNter{Name} @bnfopt{attrib}}}
9542 9646
9543@producname{attrib}@producbody{@bnfopt{@bnfter{<} @bnfNter{Name} @bnfter{>}}} 9647@producname{attrib}@producbody{@bnfter{<} @bnfNter{Name} @bnfter{>}}
9544 9648
9545@producname{retstat}@producbody{@Rw{return} 9649@producname{retstat}@producbody{@Rw{return}
9546 @bnfopt{explist} @bnfopt{@bnfter{;}}} 9650 @bnfopt{explist} @bnfopt{@bnfter{;}}}