diff options
Diffstat (limited to '')
-rw-r--r-- | manual/manual.of | 350 |
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 | |||
219 | A variable name refers to a global or a local variable according | ||
220 | to the declaration that is in context at that point of the code. | ||
221 | (For the purposes of this discussion, | ||
222 | a function's formal parameter is equivalent to a local variable.) | ||
223 | |||
224 | All chunks start with an implicit declaration @T{global *}, | ||
225 | which declares all free names as global variables; | ||
226 | this preambular declaration becomes void inside the scope of any other | ||
227 | @Rw{global} declaration, | ||
228 | as the following example illustrates: | ||
229 | @verbatim{ | ||
230 | X = 1 -- Ok, global by default | ||
231 | do | ||
232 | global Y -- voids the implicit initial declaration | ||
233 | Y = 1 -- Ok, Y declared as global | ||
234 | X = 1 -- ERROR, X not declared | ||
235 | end | ||
236 | X = 2 -- Ok, global by default again | ||
237 | } | ||
238 | So, outside any global declaration, | ||
239 | Lua works as @x{global-by-default}. | ||
240 | Inside any global declaration, | ||
241 | Lua works without a default: | ||
242 | All variables must be declared. | ||
243 | |||
244 | Lua is a lexically scoped language. | ||
245 | The scope of a variable declaration begins at the first statement after | ||
246 | the declaration and lasts until the last non-void statement | ||
247 | of the innermost block that includes the declaration. | ||
248 | (@emph{Void statements} are labels and empty statements.) | ||
249 | |||
250 | A declaration shadows any declaration for the same name that | ||
251 | is in context at the point of the declaration. Inside this | ||
252 | shadow, any outer declaration for that name is void. | ||
253 | See the next example: | ||
254 | @verbatim{ | ||
255 | global print, x | ||
256 | x = 10 -- global variable | ||
257 | do -- 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 | ||
266 | end | ||
267 | print(x) --> 10 (the global one) | ||
268 | } | ||
269 | |||
270 | Notice that, in a declaration like @T{local x = x}, | ||
271 | the new @id{x} being declared is not in scope yet, | ||
272 | and so the @id{x} in the right-hand side refers to the outside variable. | ||
273 | |||
274 | Because of the @x{lexical scoping} rules, | ||
275 | local variables can be freely accessed by functions | ||
276 | defined inside their scope. | ||
277 | A local variable used by an inner function is called an @def{upvalue} | ||
278 | (or @emphx{external local variable}, or simply @emphx{external variable}) | ||
279 | inside the inner function. | ||
280 | |||
281 | Notice that each execution of a @Rw{local} statement | ||
282 | defines new local variables. | ||
283 | Consider the following example: | ||
284 | @verbatim{ | ||
285 | a = {} | ||
286 | local x = 20 | ||
287 | for i = 1, 10 do | ||
288 | local y = 0 | ||
289 | a[i] = function () y = y + 1; return x + y end | ||
290 | end | ||
291 | } | ||
292 | The loop creates ten closures | ||
293 | (that is, ten instances of the anonymous function). | ||
294 | Each of these closures uses a different @id{y} variable, | ||
295 | while all of them share the same @id{x}. | ||
217 | 296 | ||
218 | As we will discuss further in @refsec{variables} and @refsec{assignment}, | 297 | As we will discuss further in @refsec{variables} and @refsec{assignment}, |
219 | any reference to a free name | 298 | any reference to a global variable @id{var} |
220 | (that is, a name not bound to any declaration) @id{var} | ||
221 | is syntactically translated to @T{_ENV.var}. | 299 | is syntactically translated to @T{_ENV.var}. |
222 | Moreover, every chunk is compiled in the scope of | 300 | Moreover, every chunk is compiled in the scope of |
223 | an external local variable named @id{_ENV} @see{chunks}, | 301 | an 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 | ||
226 | Despite the existence of this external @id{_ENV} variable and | 304 | Despite the existence of this external @id{_ENV} variable and |
227 | the translation of free names, | 305 | the translation of free names, |
228 | @id{_ENV} is a completely regular name. | 306 | @id{_ENV} is a regular name. |
229 | In particular, | 307 | In particular, |
230 | you can define new variables and parameters with that name. | 308 | you can define new variables and parameters with that name. |
231 | Each reference to a free name uses the @id{_ENV} that is | 309 | (However, you should not define @id{_ENV} as a global variable, |
232 | visible at that point in the program, | 310 | otherwise @T{_ENV.var} would translate to |
233 | following the usual visibility rules of Lua @see{visibility}. | 311 | @T{_ENV._ENV.var} and so on, in an infinite loop.) |
312 | Each reference to a global variable name uses the @id{_ENV} that is | ||
313 | visible at that point in the program. | ||
234 | 314 | ||
235 | Any table used as the value of @id{_ENV} is called an @def{environment}. | 315 | Any table used as the value of @id{_ENV} is called an @def{environment}. |
236 | 316 | ||
@@ -244,8 +324,8 @@ When Lua loads a chunk, | |||
244 | the default value for its @id{_ENV} variable | 324 | the default value for its @id{_ENV} variable |
245 | is the global environment @seeF{load}. | 325 | is the global environment @seeF{load}. |
246 | Therefore, by default, | 326 | Therefore, by default, |
247 | free names in Lua code refer to entries in the global environment | 327 | global variables in Lua code refer to entries in the global environment |
248 | and, therefore, they are also called @def{global variables}. | 328 | and, therefore, they act as conventional global variables. |
249 | Moreover, all standard libraries are loaded in the global environment | 329 | Moreover, all standard libraries are loaded in the global environment |
250 | and some functions there operate on that environment. | 330 | and some functions there operate on that environment. |
251 | You can use @Lid{load} (or @Lid{loadfile}) | 331 | You 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{ |
1033 | and break do else elseif end | 1113 | and break do else elseif end |
1034 | false for function goto if in | 1114 | false for function global goto if |
1035 | local nil not or repeat return | 1115 | in local nil not or repeat |
1036 | then true until while | 1116 | return then true until while |
1037 | } | 1117 | } |
1038 | 1118 | ||
1039 | Lua is a case-sensitive language: | 1119 | Lua is a case-sensitive language: |
@@ -1198,17 +1278,15 @@ global variables, local variables, and table fields. | |||
1198 | 1278 | ||
1199 | A single name can denote a global variable or a local variable | 1279 | A 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, |
1201 | which is a particular kind of local variable): | 1281 | which 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 | ||
1207 | Any variable name is assumed to be global unless explicitly declared | 1287 | Because variables are @emph{lexically scoped}, |
1208 | as a local @see{localvar}. | ||
1209 | @x{Local variables} are @emph{lexically scoped}: | ||
1210 | local variables can be freely accessed by functions | 1288 | local variables can be freely accessed by functions |
1211 | defined inside their scope @see{visibility}. | 1289 | defined inside their scope @see{globalenv}. |
1212 | 1290 | ||
1213 | Before the first assignment to a variable, its value is @nil. | 1291 | Before 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 | ||
1228 | An access to a global variable @id{x} | 1306 | An access to a global variable @id{x} |
1229 | is equivalent to @id{_ENV.x}. | 1307 | is equivalent to @id{_ENV.x}. |
1230 | Due to the way that chunks are compiled, | ||
1231 | the 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; | |||
1326 | see the program @idx{luac} and the function @Lid{string.dump} for details. | 1402 | see the program @idx{luac} and the function @Lid{string.dump} for details. |
1327 | Programs in source and compiled forms are interchangeable; | 1403 | Programs in source and compiled forms are interchangeable; |
1328 | Lua automatically detects the file type and acts accordingly @seeF{load}. | 1404 | Lua automatically detects the file type and acts accordingly @seeF{load}. |
1405 | Be aware that, unlike source code, | ||
1406 | maliciously crafted binary chunks can crash the interpreter. | ||
1329 | 1407 | ||
1330 | } | 1408 | } |
1331 | 1409 | ||
@@ -1428,7 +1506,7 @@ labels in Lua are considered statements too: | |||
1428 | A label is visible in the entire block where it is defined, | 1506 | A label is visible in the entire block where it is defined, |
1429 | except inside nested functions. | 1507 | except inside nested functions. |
1430 | A goto can jump to any visible label as long as it does not | 1508 | A goto can jump to any visible label as long as it does not |
1431 | enter into the scope of a local variable. | 1509 | enter into the scope of a variable declaration. |
1432 | A label should not be declared | 1510 | A label should not be declared |
1433 | where a previous label with the same name is visible, | 1511 | where a previous label with the same name is visible, |
1434 | even if this other label has been declared in an enclosing block. | 1512 | even 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. | 1653 | Local and global variables can be declared anywhere inside a block. |
1576 | The declaration can include an initialization: | 1654 | The 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 | } |
1582 | If present, an initial assignment has the same semantics | 1661 | If present, an initial assignment has the same semantics |
1583 | of a multiple assignment @see{assignment}. | 1662 | of a multiple assignment @see{assignment}. |
1584 | Otherwise, all variables are initialized with @nil. | 1663 | Otherwise, all local variables are initialized with @nil. |
1585 | 1664 | ||
1586 | Each variable name may be postfixed by an attribute | 1665 | The list of names may be prefixed by an attribute |
1587 | (a name between angle brackets): | 1666 | (a name between angle brackets) |
1667 | and 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 | } |
1674 | A prefixed attribute applies to all names in the list; | ||
1675 | a postfixed attribute applies to its particular name. | ||
1591 | There are two possible attributes: | 1676 | There 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} |
1594 | that is, a variable that cannot be assigned to | 1679 | that is, a variable that cannot be used as the left-hand side of an |
1595 | after its initialization; | 1680 | assignment, |
1596 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. | 1681 | and @id{close}, which declares a to-be-closed variable @see{to-be-closed}. |
1682 | Only local variables can have the @id{close} attribute. | ||
1597 | A list of variables can contain at most one to-be-closed variable. | 1683 | A list of variables can contain at most one to-be-closed variable. |
1598 | 1684 | ||
1685 | Lua offers also a collective declaration for global variables: | ||
1686 | @Produc{ | ||
1687 | @producname{stat}@producbody{@Rw{global} @bnfopt{attrib} @bnfter{*}} | ||
1688 | } | ||
1689 | This special form implicitly declares | ||
1690 | as globals all names not explicitly declared previously. | ||
1691 | In particular, | ||
1692 | @T{global<const> *} implicitly declares | ||
1693 | as read-only globals all names not explicitly declared previously; | ||
1694 | see the following example: | ||
1695 | @verbatim{ | ||
1696 | global X | ||
1697 | global<const> * | ||
1698 | print(math.pi) -- Ok, 'print' and 'math' are read-only | ||
1699 | X = 1 -- Ok, declared as read-write | ||
1700 | Y = 1 -- Error, Y is read-only | ||
1701 | } | ||
1702 | |||
1703 | As noted in @See{globalenv}, | ||
1704 | all chunks start with an implicit declaration @T{global *}, | ||
1705 | but this preambular declaration becomes void inside | ||
1706 | the scope of any other @Rw{global} declaration. | ||
1707 | Therefore, a program that does not use global declarations | ||
1708 | or start with @T{global *} | ||
1709 | has free read-write access to any global; | ||
1710 | a program that starts with @T{global<const> *} | ||
1711 | has free read-only access to any global; | ||
1712 | and a program that starts with any other global declaration | ||
1713 | (e.g., @T{global none}) can only refer to declared variables. | ||
1714 | |||
1715 | Note that, for global variables, | ||
1716 | the effect of any declaration is only syntactical | ||
1717 | (except for the optional assignment): | ||
1718 | @verbatim{ | ||
1719 | global X <const>, _G | ||
1720 | X = 1 -- ERROR | ||
1721 | _ENV.X = 1 -- Ok | ||
1722 | _G.print(X) -- Ok | ||
1723 | foo() -- 'foo' can freely change any global | ||
1724 | } | ||
1725 | |||
1599 | A chunk is also a block @see{chunks}, | 1726 | A chunk is also a block @see{chunks}, |
1600 | and so local variables can be declared in a chunk outside any explicit block. | 1727 | and so variables can be declared in a chunk outside any explicit block. |
1601 | 1728 | ||
1602 | The visibility rules for local variables are explained in @See{visibility}. | 1729 | The visibility rules for variable declarations |
1730 | are 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 | } |
2147 | The statement | 2276 | The statement |
@@ -2160,6 +2289,7 @@ translates to | |||
2160 | @verbatim{ | 2289 | @verbatim{ |
2161 | t.a.b.c.f = function () @rep{body} end | 2290 | t.a.b.c.f = function () @rep{body} end |
2162 | } | 2291 | } |
2292 | |||
2163 | The statement | 2293 | The statement |
2164 | @verbatim{ | 2294 | @verbatim{ |
2165 | local function f () @rep{body} end | 2295 | local function f () @rep{body} end |
@@ -2173,7 +2303,15 @@ not to | |||
2173 | local f = function () @rep{body} end | 2303 | local 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 |
2176 | contains references to @id{f}.) | 2306 | contains recursive references to @id{f}.) |
2307 | Similarly, the statement | ||
2308 | @verbatim{ | ||
2309 | global function f () @rep{body} end | ||
2310 | } | ||
2311 | translates to | ||
2312 | @verbatim{ | ||
2313 | global f; f = function () @rep{body} end | ||
2314 | } | ||
2177 | 2315 | ||
2178 | A function definition is an executable expression, | 2316 | A function definition is an executable expression, |
2179 | whose value has type @emph{function}. | 2317 | whose value has type @emph{function}. |
@@ -2236,7 +2374,7 @@ then the function returns with no results. | |||
2236 | @index{multiple return} | 2374 | @index{multiple return} |
2237 | There is a system-dependent limit on the number of values | 2375 | There is a system-dependent limit on the number of values |
2238 | that a function may return. | 2376 | that a function may return. |
2239 | This limit is guaranteed to be greater than 1000. | 2377 | This limit is guaranteed to be at least 1000. |
2240 | 2378 | ||
2241 | The @emphx{colon} syntax | 2379 | The @emphx{colon} syntax |
2242 | is used to emulate @def{methods}, | 2380 | is 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, |
2282 | for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} | 2420 | for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} |
2283 | 2421 | ||
2284 | @item{A local declaration, | 2422 | @item{A local or global declaration, |
2285 | for instance @T{local a , b, c = e1, e2, e3} @see{localvar}.} | 2423 | which 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, |
2288 | for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} | 2426 | for 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 | |||
2293 | must be @emph{adjusted} to a specific length: | 2431 | must be @emph{adjusted} to a specific length: |
2294 | the number of parameters in a call to a non-variadic function | 2432 | the number of parameters in a call to a non-variadic function |
2295 | @see{func-def}, | 2433 | @see{func-def}, |
2296 | the number of variables in a multiple assignment or | 2434 | the number of variables in a multiple assignment or a declaration, |
2297 | a local declaration, | ||
2298 | and exactly four values for a generic @rw{for} loop. | 2435 | and exactly four values for a generic @rw{for} loop. |
2299 | The @def{adjustment} follows these rules: | 2436 | The @def{adjustment} follows these rules: |
2300 | If there are more values than needed, | 2437 | If 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} | ||
2362 | Lua is a lexically scoped language. | ||
2363 | The scope of a local variable begins at the first statement after | ||
2364 | its declaration and lasts until the last non-void statement | ||
2365 | of the innermost block that includes the declaration. | ||
2366 | (@emph{Void statements} are labels and empty statements.) | ||
2367 | Consider the following example: | ||
2368 | @verbatim{ | ||
2369 | x = 10 -- global variable | ||
2370 | do -- 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 | ||
2379 | end | ||
2380 | print(x) --> 10 (the global one) | ||
2381 | } | ||
2382 | |||
2383 | Notice that, in a declaration like @T{local x = x}, | ||
2384 | the new @id{x} being declared is not in scope yet, | ||
2385 | and so the second @id{x} refers to the outside variable. | ||
2386 | |||
2387 | Because of the @x{lexical scoping} rules, | ||
2388 | local variables can be freely accessed by functions | ||
2389 | defined inside their scope. | ||
2390 | A local variable used by an inner function is called an @def{upvalue} | ||
2391 | (or @emphx{external local variable}, or simply @emphx{external variable}) | ||
2392 | inside the inner function. | ||
2393 | |||
2394 | Notice that each execution of a @Rw{local} statement | ||
2395 | defines new local variables. | ||
2396 | Consider the following example: | ||
2397 | @verbatim{ | ||
2398 | a = {} | ||
2399 | local x = 20 | ||
2400 | for i = 1, 10 do | ||
2401 | local y = 0 | ||
2402 | a[i] = function () y = y + 1; return x + y end | ||
2403 | end | ||
2404 | } | ||
2405 | The loop creates ten closures | ||
2406 | (that is, ten instances of the anonymous function). | ||
2407 | Each of these closures uses a different @id{y} variable, | ||
2408 | while 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 | ||
3183 | Resets a thread, cleaning its call stack and closing all pending | 3268 | Resets a thread, cleaning its call stack and closing all pending |
3184 | to-be-closed variables. | 3269 | to-be-closed variables. |
3185 | Returns a status code: | 3270 | The parameter @id{from} represents the coroutine that is resetting @id{L}. |
3271 | If there is no such coroutine, | ||
3272 | this parameter can be @id{NULL}. | ||
3273 | |||
3274 | Unless @id{L} is equal to @id{from}, | ||
3275 | the 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 |
3188 | errors in closing methods), | 3278 | errors in closing methods), |
3189 | or an error status otherwise. | 3279 | or an error status otherwise. |
3190 | In case of error, | 3280 | In case of error, |
3191 | leaves the error object on the top of the stack. | 3281 | the error object is put on the top of the stack. |
3192 | 3282 | ||
3193 | The parameter @id{from} represents the coroutine that is resetting @id{L}. | 3283 | If @id{L} is equal to @id{from}, |
3194 | If there is no such coroutine, | 3284 | it corresponds to a thread closing itself. |
3195 | this parameter can be @id{NULL}. | 3285 | In that case, |
3286 | the call does not return; | ||
3287 | instead, the resume that (re)started the thread returns. | ||
3288 | The 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 | ||
3839 | Converts the number at acceptable index @id{idx} to a string | 3932 | Converts 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, | |||
3981 | the length plus one (to account for the ending zero) as the old size, | 4074 | the length plus one (to account for the ending zero) as the old size, |
3982 | and 0 as the new size. | 4075 | and 0 as the new size. |
3983 | 4076 | ||
3984 | Lua always @x{internalizes} strings with lengths up to 40 characters. | ||
3985 | So, for strings in that range, | ||
3986 | this function will immediately internalize the string | ||
3987 | and call @id{falloc} to free the buffer. | ||
3988 | |||
3989 | Even when using an external buffer, | 4077 | Even when using an external buffer, |
3990 | Lua still has to allocate a header for the string. | 4078 | Lua still has to allocate a header for the string. |
3991 | In case of a memory-allocation error, | 4079 | In case of a memory-allocation error, |
@@ -6601,11 +6689,10 @@ It may be the string @St{b} (only @x{binary chunk}s), | |||
6601 | or @St{bt} (both binary and text). | 6689 | or @St{bt} (both binary and text). |
6602 | The default is @St{bt}. | 6690 | The default is @St{bt}. |
6603 | 6691 | ||
6604 | It is safe to load malformed binary chunks; | 6692 | Lua does not check the consistency of binary chunks. |
6605 | @id{load} signals an appropriate error. | 6693 | Maliciously crafted binary chunks can crash |
6606 | However, | 6694 | the interpreter. |
6607 | Lua does not check the consistency of the code inside binary chunks; | 6695 | You can use the @id{mode} parameter to prevent loading binary chunks. |
6608 | running maliciously crafted bytecode can crash the interpreter. | ||
6609 | 6696 | ||
6610 | } | 6697 | } |
6611 | 6698 | ||
@@ -6817,7 +6904,7 @@ and @St{userdata}. | |||
6817 | 6904 | ||
6818 | A global variable (not a function) that | 6905 | A global variable (not a function) that |
6819 | holds a string containing the running Lua version. | 6906 | holds a string containing the running Lua version. |
6820 | The current value of this variable is @St{Lua 5.4}. | 6907 | The 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}. | |||
6854 | See @See{coroutine} for a general description of coroutines. | 6941 | See @See{coroutine} for a general description of coroutines. |
6855 | 6942 | ||
6856 | 6943 | ||
6857 | @LibEntry{coroutine.close (co)| | 6944 | @LibEntry{coroutine.close ([co])| |
6858 | 6945 | ||
6859 | Closes coroutine @id{co}, | 6946 | Closes coroutine @id{co}, |
6860 | that is, | 6947 | that is, |
6861 | closes all its pending to-be-closed variables | 6948 | closes all its pending to-be-closed variables |
6862 | and puts the coroutine in a dead state. | 6949 | and puts the coroutine in a dead state. |
6863 | The given coroutine must be dead or suspended. | 6950 | The default for @id{co} is the running coroutine. |
6864 | In case of error | 6951 | |
6952 | The given coroutine must be dead, suspended, | ||
6953 | or be the running coroutine. | ||
6954 | For the running coroutine, | ||
6955 | this function does not return. | ||
6956 | Instead, the resume that (re)started the coroutine returns. | ||
6957 | |||
6958 | For other coroutines, | ||
6959 | in case of error | ||
6865 | (either the original error that stopped the coroutine or | 6960 | (either the original error that stopped the coroutine or |
6866 | errors in closing methods), | 6961 | errors in closing methods), |
6867 | returns @false plus the error object; | 6962 | this function returns @false plus the error object; |
6868 | otherwise returns @true. | 6963 | otherwise ir returns @true. |
6869 | 6964 | ||
6870 | } | 6965 | } |
6871 | 6966 | ||
@@ -7055,7 +7150,7 @@ to search for a @N{C loader}. | |||
7055 | 7150 | ||
7056 | Lua initializes the @N{C path} @Lid{package.cpath} in the same way | 7151 | Lua initializes the @N{C path} @Lid{package.cpath} in the same way |
7057 | it initializes the Lua path @Lid{package.path}, | 7152 | it initializes the Lua path @Lid{package.path}, |
7058 | using the environment variable @defid{LUA_CPATH_5_4}, | 7153 | using the environment variable @defid{LUA_CPATH_5_5}, |
7059 | or the environment variable @defid{LUA_CPATH}, | 7154 | or the environment variable @defid{LUA_CPATH}, |
7060 | or a default path defined in @id{luaconf.h}. | 7155 | or a default path defined in @id{luaconf.h}. |
7061 | 7156 | ||
@@ -7124,7 +7219,7 @@ A string with the path used by @Lid{require} | |||
7124 | to search for a Lua loader. | 7219 | to search for a Lua loader. |
7125 | 7220 | ||
7126 | At start-up, Lua initializes this variable with | 7221 | At start-up, Lua initializes this variable with |
7127 | the value of the environment variable @defid{LUA_PATH_5_4} or | 7222 | the value of the environment variable @defid{LUA_PATH_5_5} or |
7128 | the environment variable @defid{LUA_PATH} or | 7223 | the environment variable @defid{LUA_PATH} or |
7129 | with a default path defined in @id{luaconf.h}, | 7224 | with a default path defined in @id{luaconf.h}, |
7130 | if those environment variables are not defined. | 7225 | if 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 | ||
7498 | local t = {name="lua", version="5.4"} | 7593 | local t = {name="lua", version="5.5"} |
7499 | x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) | 7594 | x = 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, | |||
9233 | and as @T{lua -} otherwise. | 9328 | and as @T{lua -} otherwise. |
9234 | 9329 | ||
9235 | When called without the option @T{-E}, | 9330 | When called without the option @T{-E}, |
9236 | the interpreter checks for an environment variable @defid{LUA_INIT_5_4} | 9331 | the 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) |
9238 | before running any argument. | 9333 | before running any argument. |
9239 | If the variable content has the format @T{@At@rep{filename}}, | 9334 | If 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{ |
9411 | The control variable in @Rw{for} loops are read only. | 9506 | The word @Rw{global} is a reserved word. |
9507 | Do not use it as a regular name. | ||
9508 | } | ||
9509 | |||
9510 | @item{ | ||
9511 | The control variable in @Rw{for} loops is read only. | ||
9412 | If you need to change it, | 9512 | If you need to change it, |
9413 | declare a local variable with the same name in the loop body. | 9513 | declare 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{;}}} |