aboutsummaryrefslogtreecommitdiff
path: root/manual
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-05 16:24:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2025-05-05 16:24:59 -0300
commitbe8120906304a8658fab998587b969e0e42f5650 (patch)
tree81cf2d38522b10468d09763fc25d261486008197 /manual
parente05590591410a5e007a1e3f1691f6c1cf9d8fe45 (diff)
downloadlua-be8120906304a8658fab998587b969e0e42f5650.tar.gz
lua-be8120906304a8658fab998587b969e0e42f5650.tar.bz2
lua-be8120906304a8658fab998587b969e0e42f5650.zip
First implementation of global declarations
Diffstat (limited to 'manual')
-rw-r--r--manual/manual.of184
1 files changed, 110 insertions, 74 deletions
diff --git a/manual/manual.of b/manual/manual.of
index 7cd0d4db..ace5d375 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -213,11 +213,88 @@ 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 implicit declaration becomes void inside the scope of any other
227@Rw{global} declaration, regardless of the names being declared.
228@verbatim{
229X = 1 -- Ok, global by default
230do
231 global Y -- voids implicit initial declaration
232 X = 1 -- ERROR, X not declared
233 Y = 1 -- Ok, Y declared as global
234end
235X = 2 -- Ok, global by default again
236}
237So, outside any global declaration,
238Lua works as @x{global-by-default}.
239Inside any global declaration,
240Lua works without a default:
241All variables must be declared.
242
243Lua is a lexically scoped language.
244The scope of a variable declaration begins at the first statement after
245the declaration and lasts until the last non-void statement
246of the innermost block that includes the declaration.
247(@emph{Void statements} are labels and empty statements.)
248
249A declaration shadows any declaration for the same name that
250is in context at the point of the declaration. Inside this
251shadow, any outer declaration for that name is void.
252See the next example:
253@verbatim{
254global print, x
255x = 10 -- global variable
256do -- new block
257 local x = x -- new 'x', with value 10
258 print(x) --> 10
259 x = x+1
260 do -- another block
261 local x = x+1 -- another 'x'
262 print(x) --> 12
263 end
264 print(x) --> 11
265end
266print(x) --> 10 (the global one)
267}
268
269Notice that, in a declaration like @T{local x = x},
270the new @id{x} being declared is not in scope yet,
271and so the @id{x} in the left-hand side refers to the outside variable.
272
273Because of the @x{lexical scoping} rules,
274local variables can be freely accessed by functions
275defined inside their scope.
276A local variable used by an inner function is called an @def{upvalue}
277(or @emphx{external local variable}, or simply @emphx{external variable})
278inside the inner function.
279
280Notice that each execution of a @Rw{local} statement
281defines new local variables.
282Consider the following example:
283@verbatim{
284a = {}
285local x = 20
286for i = 1, 10 do
287 local y = 0
288 a[i] = function () y = y + 1; return x + y end
289end
290}
291The loop creates ten closures
292(that is, ten instances of the anonymous function).
293Each of these closures uses a different @id{y} variable,
294while all of them share the same @id{x}.
217 295
218As we will discuss further in @refsec{variables} and @refsec{assignment}, 296As we will discuss further in @refsec{variables} and @refsec{assignment},
219any reference to a free name 297any 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}. 298is syntactically translated to @T{_ENV.var}.
222Moreover, every chunk is compiled in the scope of 299Moreover, every chunk is compiled in the scope of
223an external local variable named @id{_ENV} @see{chunks}, 300an external local variable named @id{_ENV} @see{chunks},
@@ -225,12 +302,14 @@ so @id{_ENV} itself is never a free name in a chunk.
225 302
226Despite the existence of this external @id{_ENV} variable and 303Despite the existence of this external @id{_ENV} variable and
227the translation of free names, 304the translation of free names,
228@id{_ENV} is a completely regular name. 305@id{_ENV} is a regular name.
229In particular, 306In particular,
230you can define new variables and parameters with that name. 307you can define new variables and parameters with that name.
231Each reference to a free name uses the @id{_ENV} that is 308(However, you should not define @id{_ENV} as a global variable,
232visible at that point in the program, 309otherwise @T{_ENV.var} would translate to
233following the usual visibility rules of Lua @see{visibility}. 310@T{_ENV._ENV.var} and so on, in an infinite loop.)
311Each reference to a global variable name uses the @id{_ENV} that is
312visible at that point in the program.
234 313
235Any table used as the value of @id{_ENV} is called an @def{environment}. 314Any table used as the value of @id{_ENV} is called an @def{environment}.
236 315
@@ -244,8 +323,8 @@ When Lua loads a chunk,
244the default value for its @id{_ENV} variable 323the default value for its @id{_ENV} variable
245is the global environment @seeF{load}. 324is the global environment @seeF{load}.
246Therefore, by default, 325Therefore, by default,
247free names in Lua code refer to entries in the global environment 326global variables in Lua code refer to entries in the global environment
248and, therefore, they are also called @def{global variables}. 327and, therefore, they act as conventional global variables.
249Moreover, all standard libraries are loaded in the global environment 328Moreover, all standard libraries are loaded in the global environment
250and some functions there operate on that environment. 329and some functions there operate on that environment.
251You can use @Lid{load} (or @Lid{loadfile}) 330You can use @Lid{load} (or @Lid{loadfile})
@@ -1198,17 +1277,15 @@ global variables, local variables, and table fields.
1198 1277
1199A single name can denote a global variable or a local variable 1278A single name can denote a global variable or a local variable
1200(or a function's formal parameter, 1279(or a function's formal parameter,
1201which is a particular kind of local variable): 1280which is a particular kind of local variable) @see{globalenv}:
1202@Produc{ 1281@Produc{
1203@producname{var}@producbody{@bnfNter{Name}} 1282@producname{var}@producbody{@bnfNter{Name}}
1204} 1283}
1205@bnfNter{Name} denotes identifiers @see{lexical}. 1284@bnfNter{Name} denotes identifiers @see{lexical}.
1206 1285
1207Any variable name is assumed to be global unless explicitly declared 1286Because 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 1287local variables can be freely accessed by functions
1211defined inside their scope @see{visibility}. 1288defined inside their scope @see{globalenv}.
1212 1289
1213Before the first assignment to a variable, its value is @nil. 1290Before the first assignment to a variable, its value is @nil.
1214 1291
@@ -1227,8 +1304,6 @@ The syntax @id{var.Name} is just syntactic sugar for
1227 1304
1228An access to a global variable @id{x} 1305An access to a global variable @id{x}
1229is equivalent to @id{_ENV.x}. 1306is equivalent to @id{_ENV.x}.
1230Due to the way that chunks are compiled,
1231the variable @id{_ENV} itself is never global @see{globalenv}.
1232 1307
1233} 1308}
1234 1309
@@ -1571,17 +1646,18 @@ Function calls are explained in @See{functioncall}.
1571 1646
1572} 1647}
1573 1648
1574@sect3{localvar| @title{Local Declarations} 1649@sect3{localvar| @title{Variable Declarations}
1575@x{Local variables} can be declared anywhere inside a block. 1650Local and global variables can be declared anywhere inside a block.
1576The declaration can include an initialization: 1651The declaration for locals can include an initialization:
1577@Produc{ 1652@Produc{
1578@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}} 1653@producname{stat}@producbody{@Rw{local} attnamelist @bnfopt{@bnfter{=} explist}}
1654@producname{stat}@producbody{@Rw{global} attnamelist}
1579@producname{attnamelist}@producbody{ 1655@producname{attnamelist}@producbody{
1580 @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}} 1656 @bnfNter{Name} attrib @bnfrep{@bnfter{,} @bnfNter{Name} attrib}}
1581} 1657}
1582If present, an initial assignment has the same semantics 1658If present, an initial assignment has the same semantics
1583of a multiple assignment @see{assignment}. 1659of a multiple assignment @see{assignment}.
1584Otherwise, all variables are initialized with @nil. 1660Otherwise, all local variables are initialized with @nil.
1585 1661
1586Each variable name may be postfixed by an attribute 1662Each variable name may be postfixed by an attribute
1587(a name between angle brackets): 1663(a name between angle brackets):
@@ -1595,11 +1671,22 @@ that is, a variable that cannot be assigned to
1595after its initialization; 1671after its initialization;
1596and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. 1672and @id{close}, which declares a to-be-closed variable @see{to-be-closed}.
1597A list of variables can contain at most one to-be-closed variable. 1673A list of variables can contain at most one to-be-closed variable.
1674Only local variables can have the @id{close} attribute.
1675
1676Note that, for global variables,
1677the @emph{read-only} atribute is only a syntactical restriction:
1678@verbatim{
1679global X <const>
1680X = 1 -- ERROR
1681_ENV.X = 1 -- Ok
1682foo() -- 'foo' can freely change the global X
1683}
1598 1684
1599A chunk is also a block @see{chunks}, 1685A chunk is also a block @see{chunks},
1600and so local variables can be declared in a chunk outside any explicit block. 1686and so variables can be declared in a chunk outside any explicit block.
1601 1687
1602The visibility rules for local variables are explained in @See{visibility}. 1688The visibility rules for variable declarations
1689are explained in @See{globalenv}.
1603 1690
1604} 1691}
1605 1692
@@ -2356,58 +2443,6 @@ return x,y,f() -- returns x, y, and all results from f().
2356 2443
2357} 2444}
2358 2445
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 2446
2412} 2447}
2413 2448
@@ -9535,6 +9570,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9535@OrNL @Rw{function} funcname funcbody 9570@OrNL @Rw{function} funcname funcbody
9536@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody 9571@OrNL @Rw{local} @Rw{function} @bnfNter{Name} funcbody
9537@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist} 9572@OrNL @Rw{local} attnamelist @bnfopt{@bnfter{=} explist}
9573@OrNL @Rw{global} attnamelist
9538} 9574}
9539 9575
9540@producname{attnamelist}@producbody{ 9576@producname{attnamelist}@producbody{