diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-10 12:41:56 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-10 12:41:56 -0300 |
commit | 979ad95eb114d8d43f928b184f051ac0cfacedf7 (patch) | |
tree | fbea7150dbbb3bbbf68d80b6bd24d42a81d2afdb /manual | |
parent | 0f028b9008097f00aa3953a3425c72e7ae2b4c98 (diff) | |
download | lua-979ad95eb114d8d43f928b184f051ac0cfacedf7.tar.gz lua-979ad95eb114d8d43f928b184f051ac0cfacedf7.tar.bz2 lua-979ad95eb114d8d43f928b184f051ac0cfacedf7.zip |
Thorough revision of the reference manual
Diffstat (limited to 'manual')
-rw-r--r-- | manual/manual.of | 692 |
1 files changed, 351 insertions, 341 deletions
diff --git a/manual/manual.of b/manual/manual.of index fc2550e0..9f1ef631 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -15,7 +15,7 @@ Lua is dynamically typed, | |||
15 | runs by interpreting bytecode with a register-based | 15 | runs by interpreting bytecode with a register-based |
16 | virtual machine, | 16 | virtual machine, |
17 | and has automatic memory management with | 17 | and has automatic memory management with |
18 | incremental garbage collection, | 18 | a generational garbage collection, |
19 | making it ideal for configuration, scripting, | 19 | making it ideal for configuration, scripting, |
20 | and rapid prototyping. | 20 | and rapid prototyping. |
21 | 21 | ||
@@ -79,7 +79,7 @@ There are eight @x{basic types} in Lua: | |||
79 | @def{thread}, and @def{table}. | 79 | @def{thread}, and @def{table}. |
80 | The type @emph{nil} has one single value, @nil, | 80 | The type @emph{nil} has one single value, @nil, |
81 | whose main property is to be different from any other value; | 81 | whose main property is to be different from any other value; |
82 | it usually represents the absence of a useful value. | 82 | it often represents the absence of a useful value. |
83 | The type @emph{boolean} has two values, @false and @true. | 83 | The type @emph{boolean} has two values, @false and @true. |
84 | Both @nil and @false make a condition false; | 84 | Both @nil and @false make a condition false; |
85 | any other value makes it true. | 85 | any other value makes it true. |
@@ -130,7 +130,8 @@ the programmer can define operations for full userdata values | |||
130 | @see{metatable}. | 130 | @see{metatable}. |
131 | Userdata values cannot be created or modified in Lua, | 131 | Userdata values cannot be created or modified in Lua, |
132 | only through the @N{C API}. | 132 | only through the @N{C API}. |
133 | This guarantees the integrity of data owned by the host program. | 133 | This guarantees the integrity of data owned by |
134 | the host program and @N{C libraries}. | ||
134 | 135 | ||
135 | The type @def{thread} represents independent threads of execution | 136 | The type @def{thread} represents independent threads of execution |
136 | and it is used to implement coroutines @see{coroutine}. | 137 | and it is used to implement coroutines @see{coroutine}. |
@@ -146,7 +147,7 @@ used by the @x{IEEE 754} standard to represent | |||
146 | undefined numerical results, such as @T{0/0}.) | 147 | undefined numerical results, such as @T{0/0}.) |
147 | Tables can be @emph{heterogeneous}; | 148 | Tables can be @emph{heterogeneous}; |
148 | that is, they can contain values of all types (except @nil). | 149 | that is, they can contain values of all types (except @nil). |
149 | Any key with value @nil is not considered part of the table. | 150 | Any key associated to the value @nil is not considered part of the table. |
150 | Conversely, any key that is not part of a table has | 151 | Conversely, any key that is not part of a table has |
151 | an associated value @nil. | 152 | an associated value @nil. |
152 | 153 | ||
@@ -176,14 +177,10 @@ In particular, floats with integral values | |||
176 | are equal to their respective integers | 177 | are equal to their respective integers |
177 | (e.g., @T{1.0 == 1}). | 178 | (e.g., @T{1.0 == 1}). |
178 | To avoid ambiguities, | 179 | To avoid ambiguities, |
179 | any float with integral value used as a key | 180 | any float used as a key that is equal to an integer |
180 | is converted to its respective integer. | 181 | is converted to that integer. |
181 | For instance, if you write @T{a[2.0] = true}, | 182 | For instance, if you write @T{a[2.0] = true}, |
182 | the actual key inserted into the table will be the | 183 | the actual key inserted into the table will be the integer @T{2}. |
183 | integer @T{2}. | ||
184 | (On the other hand, | ||
185 | 2 and @St{2} are different Lua values and therefore | ||
186 | denote different table entries.) | ||
187 | 184 | ||
188 | 185 | ||
189 | Tables, functions, threads, and (full) userdata values are @emph{objects}: | 186 | Tables, functions, threads, and (full) userdata values are @emph{objects}: |
@@ -194,13 +191,13 @@ always manipulate references to such values; | |||
194 | these operations do not imply any kind of copy. | 191 | these operations do not imply any kind of copy. |
195 | 192 | ||
196 | The library function @Lid{type} returns a string describing the type | 193 | The library function @Lid{type} returns a string describing the type |
197 | of a given value @see{predefined}. | 194 | of a given value @seeF{type}. |
198 | 195 | ||
199 | } | 196 | } |
200 | 197 | ||
201 | @sect2{globalenv| @title{Environments and the Global Environment} | 198 | @sect2{globalenv| @title{Environments and the Global Environment} |
202 | 199 | ||
203 | As will be discussed in @refsec{variables} and @refsec{assignment}, | 200 | As we will discuss further in @refsec{variables} and @refsec{assignment}, |
204 | any reference to a free name | 201 | any reference to a free name |
205 | (that is, a name not bound to any declaration) @id{var} | 202 | (that is, a name not bound to any declaration) @id{var} |
206 | is syntactically translated to @T{_ENV.var}. | 203 | is syntactically translated to @T{_ENV.var}. |
@@ -222,14 +219,15 @@ Any table used as the value of @id{_ENV} is called an @def{environment}. | |||
222 | Lua keeps a distinguished environment called the @def{global environment}. | 219 | Lua keeps a distinguished environment called the @def{global environment}. |
223 | This value is kept at a special index in the C registry @see{registry}. | 220 | This value is kept at a special index in the C registry @see{registry}. |
224 | In Lua, the global variable @Lid{_G} is initialized with this same value. | 221 | In Lua, the global variable @Lid{_G} is initialized with this same value. |
225 | (@Lid{_G} is never used internally.) | 222 | (@Lid{_G} is never used internally, |
223 | so changing its value will affect only your own code.) | ||
226 | 224 | ||
227 | When Lua loads a chunk, | 225 | When Lua loads a chunk, |
228 | the default value for its @id{_ENV} upvalue | 226 | the default value for its @id{_ENV} upvalue |
229 | is the global environment @seeF{load}. | 227 | is the global environment @seeF{load}. |
230 | Therefore, by default, | 228 | Therefore, by default, |
231 | free names in Lua code refer to entries in the global environment | 229 | free names in Lua code refer to entries in the global environment |
232 | (and, therefore, they are also called @def{global variables}). | 230 | and, therefore, they are also called @def{global variables}. |
233 | Moreover, all standard libraries are loaded in the global environment | 231 | Moreover, all standard libraries are loaded in the global environment |
234 | and some functions there operate on that environment. | 232 | and some functions there operate on that environment. |
235 | You can use @Lid{load} (or @Lid{loadfile}) | 233 | You can use @Lid{load} (or @Lid{loadfile}) |
@@ -284,6 +282,12 @@ Lua breaks it and returns an appropriate message. | |||
284 | It is not called for memory-allocation errors | 282 | It is not called for memory-allocation errors |
285 | nor for errors while running finalizers.) | 283 | nor for errors while running finalizers.) |
286 | 284 | ||
285 | Lua also offers a system of @emph{warnings} @seeF{warn}. | ||
286 | Unlike errors, warnings do not interfere | ||
287 | in any way with program execution. | ||
288 | They typically only generate a message to the user, | ||
289 | although this behavior can be adapted from C @see{lua_setwarnf}. | ||
290 | |||
287 | } | 291 | } |
288 | 292 | ||
289 | @sect2{metatable| @title{Metatables and Metamethods} | 293 | @sect2{metatable| @title{Metatables and Metamethods} |
@@ -310,20 +314,14 @@ metamethods should be function values. | |||
310 | You can query the metatable of any value | 314 | You can query the metatable of any value |
311 | using the @Lid{getmetatable} function. | 315 | using the @Lid{getmetatable} function. |
312 | Lua queries metamethods in metatables using a raw access @seeF{rawget}. | 316 | Lua queries metamethods in metatables using a raw access @seeF{rawget}. |
313 | So, to retrieve the metamethod for event @id{ev} in object @id{o}, | ||
314 | Lua does the equivalent to the following code: | ||
315 | @verbatim{ | ||
316 | rawget(getmetatable(@rep{o}) or {}, "__@rep{ev}") | ||
317 | } | ||
318 | 317 | ||
319 | You can replace the metatable of tables | 318 | You can replace the metatable of tables |
320 | using the @Lid{setmetatable} function. | 319 | using the @Lid{setmetatable} function. |
321 | You cannot change the metatable of other types from Lua code | 320 | You cannot change the metatable of other types from Lua code, |
322 | (except by using the @link{debuglib|debug library}); | 321 | except by using the @link{debuglib|debug library}. |
323 | you should use the @N{C API} for that. | ||
324 | 322 | ||
325 | Tables and full userdata have individual metatables | 323 | Tables and full userdata have individual metatables, |
326 | (although multiple tables and userdata can share their metatables). | 324 | although multiple tables and userdata can share their metatables. |
327 | Values of all other types share one single metatable per type; | 325 | Values of all other types share one single metatable per type; |
328 | that is, there is one single metatable for all numbers, | 326 | that is, there is one single metatable for all numbers, |
329 | one for all strings, etc. | 327 | one for all strings, etc. |
@@ -351,8 +349,7 @@ Each operation is identified by its corresponding key. | |||
351 | 349 | ||
352 | @item{@idx{__add}| | 350 | @item{@idx{__add}| |
353 | the addition (@T{+}) operation. | 351 | the addition (@T{+}) operation. |
354 | If any operand for an addition is not a number | 352 | If any operand for an addition is not a number, |
355 | (nor a string coercible to a number), | ||
356 | Lua will try to call a metamethod. | 353 | Lua will try to call a metamethod. |
357 | First, Lua will check the first operand (even if it is valid). | 354 | First, Lua will check the first operand (even if it is valid). |
358 | If that operand does not define a metamethod for @idx{__add}, | 355 | If that operand does not define a metamethod for @idx{__add}, |
@@ -406,7 +403,7 @@ the bitwise AND (@T{&}) operation. | |||
406 | Behavior similar to the addition operation, | 403 | Behavior similar to the addition operation, |
407 | except that Lua will try a metamethod | 404 | except that Lua will try a metamethod |
408 | if any operand is neither an integer | 405 | if any operand is neither an integer |
409 | nor a value coercible to an integer @see{coercion}. | 406 | nor a float coercible to an integer @see{coercion}. |
410 | } | 407 | } |
411 | 408 | ||
412 | @item{@idx{__bor}| | 409 | @item{@idx{__bor}| |
@@ -493,8 +490,8 @@ and the result of the call | |||
493 | is the result of the operation. | 490 | is the result of the operation. |
494 | If it is a table, | 491 | If it is a table, |
495 | the final result is the result of indexing this table with @id{key}. | 492 | the final result is the result of indexing this table with @id{key}. |
496 | (This indexing is regular, not raw, | 493 | This indexing is regular, not raw, |
497 | and therefore can trigger another metamethod.) | 494 | and therefore can trigger another metamethod. |
498 | } | 495 | } |
499 | 496 | ||
500 | @item{@idx{__newindex}| | 497 | @item{@idx{__newindex}| |
@@ -510,8 +507,8 @@ If it is a function, | |||
510 | it is called with @id{table}, @id{key}, and @id{value} as arguments. | 507 | it is called with @id{table}, @id{key}, and @id{value} as arguments. |
511 | If it is a table, | 508 | If it is a table, |
512 | Lua does an indexing assignment to this table with the same key and value. | 509 | Lua does an indexing assignment to this table with the same key and value. |
513 | (This assignment is regular, not raw, | 510 | This assignment is regular, not raw, |
514 | and therefore can trigger another metamethod.) | 511 | and therefore can trigger another metamethod. |
515 | 512 | ||
516 | Whenever there is a @idx{__newindex} metamethod, | 513 | Whenever there is a @idx{__newindex} metamethod, |
517 | Lua does not perform the primitive assignment. | 514 | Lua does not perform the primitive assignment. |
@@ -530,7 +527,7 @@ the metamethod is called with @id{func} as its first argument, | |||
530 | followed by the arguments of the original call (@id{args}). | 527 | followed by the arguments of the original call (@id{args}). |
531 | All results of the call | 528 | All results of the call |
532 | are the result of the operation. | 529 | are the result of the operation. |
533 | (This is the only metamethod that allows multiple results.) | 530 | This is the only metamethod that allows multiple results. |
534 | } | 531 | } |
535 | 532 | ||
536 | } | 533 | } |
@@ -566,17 +563,17 @@ incremental and generational. | |||
566 | 563 | ||
567 | The default GC mode with the default parameters | 564 | The default GC mode with the default parameters |
568 | are adequate for most uses. | 565 | are adequate for most uses. |
569 | Programs that waste a large proportion of its time | 566 | However, programs that waste a large proportion of their time |
570 | allocating and freeing memory can benefit from other settings. | 567 | allocating and freeing memory can benefit from other settings. |
571 | Keep in mind that the GC behavior is non-portable | 568 | Keep in mind that the GC behavior is non-portable |
572 | both across platforms and across different Lua releases; | 569 | both across platforms and across different Lua releases; |
573 | therefore, optimal settings are also non-portable. | 570 | therefore, optimal settings are also non-portable. |
574 | 571 | ||
575 | You can change the GC mode and parameters by calling | 572 | You can change the GC mode and parameters by calling |
576 | @Lid{lua_gc} in C | 573 | @Lid{lua_gc} @N{in C} |
577 | or @Lid{collectgarbage} in Lua. | 574 | or @Lid{collectgarbage} in Lua. |
578 | You can also use these functions to control | 575 | You can also use these functions to control |
579 | the collector directly (e.g., stop and restart it). | 576 | the collector directly (e.g., to stop and restart it). |
580 | 577 | ||
581 | @sect3{@title{Incremental Garbage Collection} | 578 | @sect3{@title{Incremental Garbage Collection} |
582 | 579 | ||
@@ -601,7 +598,7 @@ to double before starting a new cycle. | |||
601 | The default value is 200; the maximum value is 1000. | 598 | The default value is 200; the maximum value is 1000. |
602 | 599 | ||
603 | The garbage-collector step multiplier | 600 | The garbage-collector step multiplier |
604 | controls the relative speed of the collector relative to | 601 | controls the speed of the collector relative to |
605 | memory allocation, | 602 | memory allocation, |
606 | that is, | 603 | that is, |
607 | how many elements it marks or sweeps for each | 604 | how many elements it marks or sweeps for each |
@@ -623,7 +620,7 @@ bytes between steps and perform equivalent work during the step. | |||
623 | A large value (e.g., 60) makes the collector a stop-the-world | 620 | A large value (e.g., 60) makes the collector a stop-the-world |
624 | (non-incremental) collector. | 621 | (non-incremental) collector. |
625 | The default value is 13, | 622 | The default value is 13, |
626 | which makes for steps of approximately @N{8 Kbytes}. | 623 | which means steps of approximately @N{8 Kbytes}. |
627 | 624 | ||
628 | } | 625 | } |
629 | 626 | ||
@@ -669,8 +666,8 @@ These metamethods, called @def{finalizers}, | |||
669 | are called when the garbage collector detects that the | 666 | are called when the garbage collector detects that the |
670 | corresponding table or userdata is unreachable. | 667 | corresponding table or userdata is unreachable. |
671 | Finalizers allow you to coordinate Lua's garbage collection | 668 | Finalizers allow you to coordinate Lua's garbage collection |
672 | with external resource management | 669 | with external resource management such as closing files, |
673 | such as closing files, network or database connections, | 670 | network or database connections, |
674 | or freeing your own memory. | 671 | or freeing your own memory. |
675 | 672 | ||
676 | For an object (table or userdata) to be finalized when collected, | 673 | For an object (table or userdata) to be finalized when collected, |
@@ -682,7 +679,7 @@ Note that if you set a metatable without a @idx{__gc} field | |||
682 | and later create that field in the metatable, | 679 | and later create that field in the metatable, |
683 | the object will not be marked for finalization. | 680 | the object will not be marked for finalization. |
684 | 681 | ||
685 | When a marked object becomes garbage, | 682 | When a marked object becomes unreachable, |
686 | it is not collected immediately by the garbage collector. | 683 | it is not collected immediately by the garbage collector. |
687 | Instead, Lua puts it in a list. | 684 | Instead, Lua puts it in a list. |
688 | After the collection, | 685 | After the collection, |
@@ -693,7 +690,7 @@ If it is present, | |||
693 | Lua calls it with the object as its single argument. | 690 | Lua calls it with the object as its single argument. |
694 | 691 | ||
695 | At the end of each garbage-collection cycle, | 692 | At the end of each garbage-collection cycle, |
696 | the finalizers for objects are called in | 693 | the finalizers are called in |
697 | the reverse order that the objects were marked for finalization, | 694 | the reverse order that the objects were marked for finalization, |
698 | among those collected in that cycle; | 695 | among those collected in that cycle; |
699 | that is, the first finalizer to be called is the one associated | 696 | that is, the first finalizer to be called is the one associated |
@@ -723,9 +720,16 @@ If any finalizer marks objects for collection during that phase, | |||
723 | these marks have no effect. | 720 | these marks have no effect. |
724 | 721 | ||
725 | Finalizers cannot yield. | 722 | Finalizers cannot yield. |
723 | Except for that, they can do anything, | ||
724 | such as raise errors, create new objects, | ||
725 | or even run the garbage collector. | ||
726 | However, because they can run in unpredictable times, | ||
727 | it is good practice to restrict each finalizer | ||
728 | to the minimum necessary to properly release | ||
729 | its associated resource. | ||
726 | 730 | ||
727 | Any error while running a finalizer generates a warning; | 731 | Any error while running a finalizer generates a warning; |
728 | it is not propagated. | 732 | the error is not propagated. |
729 | 733 | ||
730 | } | 734 | } |
731 | 735 | ||
@@ -773,8 +777,10 @@ are not subject to garbage collection, | |||
773 | and therefore are not removed from weak tables | 777 | and therefore are not removed from weak tables |
774 | (unless their associated values are collected). | 778 | (unless their associated values are collected). |
775 | Although strings are subject to garbage collection, | 779 | Although strings are subject to garbage collection, |
776 | they do not have an explicit construction, | 780 | they do not have an explicit construction and |
777 | and therefore are not removed from weak tables. | 781 | their equality is by value; |
782 | they behave more like values than like objects. | ||
783 | Therefore, they are not removed from weak tables. | ||
778 | 784 | ||
779 | Resurrected objects | 785 | Resurrected objects |
780 | (that is, objects being finalized | 786 | (that is, objects being finalized |
@@ -828,7 +834,7 @@ In case of normal termination, | |||
828 | @Lid{coroutine.resume} returns @true, | 834 | @Lid{coroutine.resume} returns @true, |
829 | plus any values returned by the coroutine main function. | 835 | plus any values returned by the coroutine main function. |
830 | In case of errors, @Lid{coroutine.resume} returns @false | 836 | In case of errors, @Lid{coroutine.resume} returns @false |
831 | plus an error object. | 837 | plus the error object. |
832 | 838 | ||
833 | A coroutine yields by calling @Lid{coroutine.yield}. | 839 | A coroutine yields by calling @Lid{coroutine.yield}. |
834 | When a coroutine yields, | 840 | When a coroutine yields, |
@@ -922,7 +928,7 @@ at the end of this manual. | |||
922 | 928 | ||
923 | Lua is a @x{free-form} language. | 929 | Lua is a @x{free-form} language. |
924 | It ignores spaces and comments between lexical elements (@x{tokens}), | 930 | It ignores spaces and comments between lexical elements (@x{tokens}), |
925 | except as delimiters between @x{names} and @x{keywords}. | 931 | except as delimiters between two tokens. |
926 | In source code, | 932 | In source code, |
927 | Lua recognizes as spaces the standard ASCII white-space | 933 | Lua recognizes as spaces the standard ASCII white-space |
928 | characters space, form feed, newline, | 934 | characters space, form feed, newline, |
@@ -1001,11 +1007,12 @@ it must be expressed using exactly three digits.) | |||
1001 | The @x{UTF-8} encoding of a @x{Unicode} character | 1007 | The @x{UTF-8} encoding of a @x{Unicode} character |
1002 | can be inserted in a literal string with | 1008 | can be inserted in a literal string with |
1003 | the escape sequence @T{\u{@rep{XXX}}} | 1009 | the escape sequence @T{\u{@rep{XXX}}} |
1004 | (note the mandatory enclosing brackets), | 1010 | (with mandatory enclosing brackets), |
1005 | where @rep{XXX} is a sequence of one or more hexadecimal digits | 1011 | where @rep{XXX} is a sequence of one or more hexadecimal digits |
1006 | representing the character code point. | 1012 | representing the character code point. |
1007 | This code point can be any value less than @M{2@sp{31}}. | 1013 | This code point can be any value less than @M{2@sp{31}}. |
1008 | (Lua uses the original UTF-8 specification here.) | 1014 | (Lua uses the original UTF-8 specification here, |
1015 | which is not restricted to valid Unicode code points.) | ||
1009 | 1016 | ||
1010 | Literal strings can also be defined using a long format | 1017 | Literal strings can also be defined using a long format |
1011 | enclosed by @def{long brackets}. | 1018 | enclosed by @def{long brackets}. |
@@ -1028,9 +1035,9 @@ Any kind of end-of-line sequence | |||
1028 | (carriage return, newline, carriage return followed by newline, | 1035 | (carriage return, newline, carriage return followed by newline, |
1029 | or newline followed by carriage return) | 1036 | or newline followed by carriage return) |
1030 | is converted to a simple newline. | 1037 | is converted to a simple newline. |
1031 | |||
1032 | When the opening long bracket is immediately followed by a newline, | 1038 | When the opening long bracket is immediately followed by a newline, |
1033 | the newline is not included in the string. | 1039 | the newline is not included in the string. |
1040 | |||
1034 | As an example, in a system using ASCII | 1041 | As an example, in a system using ASCII |
1035 | (in which @Char{a} is coded @N{as 97}, | 1042 | (in which @Char{a} is coded @N{as 97}, |
1036 | newline is coded @N{as 10}, and @Char{1} is coded @N{as 49}), | 1043 | newline is coded @N{as 10}, and @Char{1} is coded @N{as 49}), |
@@ -1052,7 +1059,7 @@ However, Lua opens files for parsing in text mode, | |||
1052 | and the system's file functions may have problems with | 1059 | and the system's file functions may have problems with |
1053 | some control characters. | 1060 | some control characters. |
1054 | So, it is safer to represent | 1061 | So, it is safer to represent |
1055 | non-text data as a quoted literal with | 1062 | binary data as a quoted literal with |
1056 | explicit escape sequences for the non-text characters. | 1063 | explicit escape sequences for the non-text characters. |
1057 | 1064 | ||
1058 | A @def{numeric constant} (or @def{numeral}) | 1065 | A @def{numeric constant} (or @def{numeral}) |
@@ -1106,7 +1113,7 @@ which is a particular kind of local variable): | |||
1106 | @Produc{ | 1113 | @Produc{ |
1107 | @producname{var}@producbody{@bnfNter{Name}} | 1114 | @producname{var}@producbody{@bnfNter{Name}} |
1108 | } | 1115 | } |
1109 | @bnfNter{Name} denotes identifiers, as defined in @See{lexical}. | 1116 | @bnfNter{Name} denotes identifiers @see{lexical}. |
1110 | 1117 | ||
1111 | Any variable name is assumed to be global unless explicitly declared | 1118 | Any variable name is assumed to be global unless explicitly declared |
1112 | as a local @see{localvar}. | 1119 | as a local @see{localvar}. |
@@ -1139,9 +1146,9 @@ the variable @id{_ENV} itself is never global @see{globalenv}. | |||
1139 | @sect2{stats| @title{Statements} | 1146 | @sect2{stats| @title{Statements} |
1140 | 1147 | ||
1141 | Lua supports an almost conventional set of @x{statements}, | 1148 | Lua supports an almost conventional set of @x{statements}, |
1142 | similar to those in Pascal or C. | 1149 | similar to those in other conventional languages. |
1143 | This set includes | 1150 | This set includes |
1144 | assignments, control structures, function calls, | 1151 | blocks, assignments, control structures, function calls, |
1145 | and variable declarations. | 1152 | and variable declarations. |
1146 | 1153 | ||
1147 | @sect3{@title{Blocks} | 1154 | @sect3{@title{Blocks} |
@@ -1159,7 +1166,7 @@ or write two semicolons in sequence: | |||
1159 | @producname{stat}@producbody{@bnfter{;}} | 1166 | @producname{stat}@producbody{@bnfter{;}} |
1160 | } | 1167 | } |
1161 | 1168 | ||
1162 | Function calls and assignments | 1169 | Both function calls and assignments |
1163 | can start with an open parenthesis. | 1170 | can start with an open parenthesis. |
1164 | This possibility leads to an ambiguity in Lua's grammar. | 1171 | This possibility leads to an ambiguity in Lua's grammar. |
1165 | Consider the following fragment: | 1172 | Consider the following fragment: |
@@ -1167,7 +1174,7 @@ Consider the following fragment: | |||
1167 | a = b + c | 1174 | a = b + c |
1168 | (print or io.write)('done') | 1175 | (print or io.write)('done') |
1169 | } | 1176 | } |
1170 | The grammar could see it in two ways: | 1177 | The grammar could see this fragment in two ways: |
1171 | @verbatim{ | 1178 | @verbatim{ |
1172 | a = b + c(print or io.write)('done') | 1179 | a = b + c(print or io.write)('done') |
1173 | 1180 | ||
@@ -1223,7 +1230,7 @@ and then Lua executes the compiled code | |||
1223 | with an interpreter for the virtual machine. | 1230 | with an interpreter for the virtual machine. |
1224 | 1231 | ||
1225 | Chunks can also be precompiled into binary form; | 1232 | Chunks can also be precompiled into binary form; |
1226 | see program @idx{luac} and function @Lid{string.dump} for details. | 1233 | see the program @idx{luac} and the function @Lid{string.dump} for details. |
1227 | Programs in source and compiled forms are interchangeable; | 1234 | Programs in source and compiled forms are interchangeable; |
1228 | Lua automatically detects the file type and acts accordingly @seeF{load}. | 1235 | Lua automatically detects the file type and acts accordingly @seeF{load}. |
1229 | 1236 | ||
@@ -1249,7 +1256,7 @@ the list of variables.@index{adjustment} | |||
1249 | If there are more values than needed, | 1256 | If there are more values than needed, |
1250 | the excess values are thrown away. | 1257 | the excess values are thrown away. |
1251 | If there are fewer values than needed, | 1258 | If there are fewer values than needed, |
1252 | the list is extended with as many @nil's as needed. | 1259 | the list is extended with @nil's. |
1253 | If the list of expressions ends with a function call, | 1260 | If the list of expressions ends with a function call, |
1254 | then all values returned by that call enter the list of values, | 1261 | then all values returned by that call enter the list of values, |
1255 | before the adjustment | 1262 | before the adjustment |
@@ -1306,7 +1313,7 @@ The @x{condition expression} of a | |||
1306 | control structure can return any value. | 1313 | control structure can return any value. |
1307 | Both @false and @nil test false. | 1314 | Both @false and @nil test false. |
1308 | All values different from @nil and @false test true. | 1315 | All values different from @nil and @false test true. |
1309 | (In particular, the number 0 and the empty string also test true). | 1316 | In particular, the number 0 and the empty string also test true. |
1310 | 1317 | ||
1311 | In the @Rw{repeat}@En@Rw{until} loop, | 1318 | In the @Rw{repeat}@En@Rw{until} loop, |
1312 | the inner block does not end at the @Rw{until} keyword, | 1319 | the inner block does not end at the @Rw{until} keyword, |
@@ -1347,7 +1354,7 @@ A @Rw{break} ends the innermost enclosing loop. | |||
1347 | 1354 | ||
1348 | The @Rw{return} statement is used to return values | 1355 | The @Rw{return} statement is used to return values |
1349 | from a function or a chunk | 1356 | from a function or a chunk |
1350 | (which is an anonymous function). | 1357 | (which is handled as an anonymous function). |
1351 | @index{return statement} | 1358 | @index{return statement} |
1352 | Functions can return more than one value, | 1359 | Functions can return more than one value, |
1353 | so the syntax for the @Rw{return} statement is | 1360 | so the syntax for the @Rw{return} statement is |
@@ -1357,7 +1364,7 @@ so the syntax for the @Rw{return} statement is | |||
1357 | 1364 | ||
1358 | The @Rw{return} statement can only be written | 1365 | The @Rw{return} statement can only be written |
1359 | as the last statement of a block. | 1366 | as the last statement of a block. |
1360 | If it is really necessary to @Rw{return} in the middle of a block, | 1367 | If it is necessary to @Rw{return} in the middle of a block, |
1361 | then an explicit inner block can be used, | 1368 | then an explicit inner block can be used, |
1362 | as in the idiom @T{do return end}, | 1369 | as in the idiom @T{do return end}, |
1363 | because now @Rw{return} is the last statement in its (inner) block. | 1370 | because now @Rw{return} is the last statement in its (inner) block. |
@@ -1395,11 +1402,12 @@ until that value passes the limit. | |||
1395 | A negative step makes a decreasing sequence; | 1402 | A negative step makes a decreasing sequence; |
1396 | a step equal to zero raises an error. | 1403 | a step equal to zero raises an error. |
1397 | If the initial value is already greater than the limit | 1404 | If the initial value is already greater than the limit |
1398 | (or less than, if the step is negative), the body is not executed. | 1405 | (or less than, if the step is negative), |
1406 | the body is not executed. | ||
1399 | 1407 | ||
1400 | If both the initial value and the step are integers, | 1408 | If both the initial value and the step are integers, |
1401 | the loop is done with integers; | 1409 | the loop is done with integers; |
1402 | in this case, the range of the control variable is limited | 1410 | in this case, the range of the control variable is clipped |
1403 | by the range of integers. | 1411 | by the range of integers. |
1404 | Otherwise, the loop is done with floats. | 1412 | Otherwise, the loop is done with floats. |
1405 | (Beware of floating-point accuracy in this case.) | 1413 | (Beware of floating-point accuracy in this case.) |
@@ -1426,52 +1434,37 @@ The generic @Rw{for} loop has the following syntax: | |||
1426 | } | 1434 | } |
1427 | A @Rw{for} statement like | 1435 | A @Rw{for} statement like |
1428 | @verbatim{ | 1436 | @verbatim{ |
1429 | for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{block} end | 1437 | for @rep{var_1}, @Cdots, @rep{var_n} in @rep{explist} do @rep{body} end |
1430 | } | 1438 | } |
1431 | is equivalent to the code: | 1439 | works as follows. |
1432 | @verbatim{ | ||
1433 | do | ||
1434 | local @rep{f}, @rep{s}, @rep{var} | ||
1435 | local *toclose @rep{tbc} = nil | ||
1436 | @rep{f}, @rep{s}, @rep{var}, @rep{tbc} = @rep{explist} | ||
1437 | while true do | ||
1438 | local @rep{var_1}, @Cdots, @rep{var_n} = @rep{f}(@rep{s}, @rep{var}) | ||
1439 | if @rep{var_1} == nil then break end | ||
1440 | @rep{var} = @rep{var_1} | ||
1441 | @rep{block} | ||
1442 | end | ||
1443 | end | ||
1444 | } | ||
1445 | Note the following: | ||
1446 | @itemize{ | ||
1447 | 1440 | ||
1448 | @item{ | 1441 | The names @rep{var_i} declare loop variables local to the loop body. |
1449 | @T{@rep{explist}} is evaluated only once. | 1442 | The first of these variables is the @emph{control variable}. |
1450 | Its results are an @emph{iterator} function, | 1443 | |
1444 | The loop starts by evaluating @rep{explist} | ||
1445 | to produce four values: | ||
1446 | an @emph{iterator function}, | ||
1451 | a @emph{state}, | 1447 | a @emph{state}, |
1452 | an initial value for the first @emph{iterator variable}, | 1448 | an initial value for the control variable, |
1453 | and a to-be-closed variable @see{to-be-closed}, | 1449 | and a @emph{closing value}. |
1450 | |||
1451 | Then, at each iteration, | ||
1452 | Lua calls the iterator function with two arguments: | ||
1453 | the state and the control variable. | ||
1454 | The results from this call are then assigned to the loop variables, | ||
1455 | following the rules of multiple assignments @see{assignment}. | ||
1456 | If the control variable becomes @nil, | ||
1457 | the loop terminates. | ||
1458 | Otherwise, the body is executed and the loop goes | ||
1459 | to the next iteration. | ||
1460 | |||
1461 | The closing value behaves like a | ||
1462 | to-be-closed variable @see{to-be-closed}, | ||
1454 | which can be used to release resources when the loop ends. | 1463 | which can be used to release resources when the loop ends. |
1455 | } | 1464 | Otherwise, it does not interfere with the loop. |
1456 | |||
1457 | @item{ | ||
1458 | @T{@rep{f}}, @T{@rep{s}}, @T{@rep{var}}, and @T{@rep{tbc}} | ||
1459 | are invisible variables. | ||
1460 | The names are here for explanatory purposes only. | ||
1461 | } | ||
1462 | |||
1463 | @item{ | ||
1464 | You can use @Rw{break} to exit a @Rw{for} loop. | ||
1465 | } | ||
1466 | 1465 | ||
1467 | @item{ | 1466 | You should not change the value of the control variable |
1468 | The loop variables @T{@rep{var_i}} are local to the loop; | 1467 | during the loop. |
1469 | you cannot use their values after the @Rw{for} ends. | ||
1470 | If you need these values, | ||
1471 | then assign them to other variables before breaking or exiting the loop. | ||
1472 | } | ||
1473 | |||
1474 | } | ||
1475 | 1468 | ||
1476 | } | 1469 | } |
1477 | 1470 | ||
@@ -1541,7 +1534,8 @@ the other pending closing methods will still be called. | |||
1541 | If a coroutine yields inside a block and is never resumed again, | 1534 | If a coroutine yields inside a block and is never resumed again, |
1542 | the variables visible at that block will never go out of scope, | 1535 | the variables visible at that block will never go out of scope, |
1543 | and therefore they will not be closed. | 1536 | and therefore they will not be closed. |
1544 | (You should use finalizers to handle this case.) | 1537 | (You should use finalizers to handle this case, |
1538 | or else call @Lid{coroutine.kill} to close the variables.) | ||
1545 | 1539 | ||
1546 | } | 1540 | } |
1547 | 1541 | ||
@@ -1659,7 +1653,7 @@ so that it works for non-integer exponents too. | |||
1659 | 1653 | ||
1660 | Floor division (@T{//}) is a division | 1654 | Floor division (@T{//}) is a division |
1661 | that rounds the quotient towards minus infinity, | 1655 | that rounds the quotient towards minus infinity, |
1662 | that is, the floor of the division of its operands. | 1656 | resulting in the floor of the division of its operands. |
1663 | 1657 | ||
1664 | Modulo is defined as the remainder of a division | 1658 | Modulo is defined as the remainder of a division |
1665 | that rounds the quotient towards minus infinity (floor division). | 1659 | that rounds the quotient towards minus infinity (floor division). |
@@ -1725,21 +1719,30 @@ it is in the range of integer representation). | |||
1725 | If it does, that representation is the result. | 1719 | If it does, that representation is the result. |
1726 | Otherwise, the conversion fails. | 1720 | Otherwise, the conversion fails. |
1727 | 1721 | ||
1728 | The string library uses metamethods that try to coerce | 1722 | Several places in Lua coerce strings to numbers when necessary. |
1729 | strings to numbers in all arithmetic operations. | 1723 | A string is converted to an integer or a float |
1730 | Any string operator is converted to an integer or a float, | ||
1731 | following its syntax and the rules of the Lua lexer. | 1724 | following its syntax and the rules of the Lua lexer. |
1732 | (The string may have also leading and trailing spaces and a sign.) | 1725 | (The string may have also leading and trailing spaces and a sign.) |
1733 | All conversions from strings to numbers | 1726 | All conversions from strings to numbers |
1734 | accept both a dot and the current locale mark | 1727 | accept both a dot and the current locale mark |
1735 | as the radix character. | 1728 | as the radix character. |
1736 | (The Lua lexer, however, accepts only a dot.) | 1729 | (The Lua lexer, however, accepts only a dot.) |
1730 | If the string is not a valid numeral, | ||
1731 | the conversion fails. | ||
1732 | If necessary, the result of this first step is then converted | ||
1733 | to the required number subtype following the previous rules | ||
1734 | for conversions between floats and integers. | ||
1735 | |||
1736 | The string library uses metamethods that try to coerce | ||
1737 | strings to numbers in all arithmetic operations. | ||
1738 | If the conversion fails, | ||
1739 | the library calls the metamethod of the other operand | ||
1740 | (if present) or it raises an error. | ||
1737 | 1741 | ||
1738 | The conversion from numbers to strings uses a | 1742 | The conversion from numbers to strings uses a |
1739 | non-specified human-readable format. | 1743 | non-specified human-readable format. |
1740 | For complete control over how numbers are converted to strings, | 1744 | To convert numbers to strings in any specific way, |
1741 | use the @id{format} function from the string library | 1745 | use the function @Lid{string.format}. |
1742 | @seeF{string.format}. | ||
1743 | 1746 | ||
1744 | } | 1747 | } |
1745 | 1748 | ||
@@ -1758,7 +1761,7 @@ These operators always result in @false or @true. | |||
1758 | Equality (@T{==}) first compares the type of its operands. | 1761 | Equality (@T{==}) first compares the type of its operands. |
1759 | If the types are different, then the result is @false. | 1762 | If the types are different, then the result is @false. |
1760 | Otherwise, the values of the operands are compared. | 1763 | Otherwise, the values of the operands are compared. |
1761 | Strings are equal if they have the same content. | 1764 | Strings are equal if they have the same byte content. |
1762 | Numbers are equal if they denote the same mathematical value. | 1765 | Numbers are equal if they denote the same mathematical value. |
1763 | 1766 | ||
1764 | Tables, userdata, and threads | 1767 | Tables, userdata, and threads |
@@ -1787,8 +1790,8 @@ The operator @T{~=} is exactly the negation of equality (@T{==}). | |||
1787 | 1790 | ||
1788 | The order operators work as follows. | 1791 | The order operators work as follows. |
1789 | If both arguments are numbers, | 1792 | If both arguments are numbers, |
1790 | then they are compared according to their mathematical values | 1793 | then they are compared according to their mathematical values, |
1791 | (regardless of their subtypes). | 1794 | regardless of their subtypes. |
1792 | Otherwise, if both arguments are strings, | 1795 | Otherwise, if both arguments are strings, |
1793 | then their values are compared according to the current locale. | 1796 | then their values are compared according to the current locale. |
1794 | Otherwise, Lua tries to call the @idx{__lt} or the @idx{__le} | 1797 | Otherwise, Lua tries to call the @idx{__lt} or the @idx{__le} |
@@ -1797,8 +1800,8 @@ A comparison @T{a > b} is translated to @T{b < a} | |||
1797 | and @T{a >= b} is translated to @T{b <= a}. | 1800 | and @T{a >= b} is translated to @T{b <= a}. |
1798 | 1801 | ||
1799 | Following the @x{IEEE 754} standard, | 1802 | Following the @x{IEEE 754} standard, |
1800 | @x{NaN} is considered neither less than, | 1803 | the special value @x{NaN} is considered neither less than, |
1801 | nor equal to, nor greater than any value (including itself). | 1804 | nor equal to, nor greater than any value, including itself. |
1802 | 1805 | ||
1803 | } | 1806 | } |
1804 | 1807 | ||
@@ -1836,8 +1839,9 @@ false or nil --> nil | |||
1836 | @sect3{concat| @title{Concatenation} | 1839 | @sect3{concat| @title{Concatenation} |
1837 | The string @x{concatenation} operator in Lua is | 1840 | The string @x{concatenation} operator in Lua is |
1838 | denoted by two dots (@Char{..}). | 1841 | denoted by two dots (@Char{..}). |
1839 | If both operands are strings or numbers, then they are converted to | 1842 | If both operands are strings or numbers, |
1840 | strings according to the rules described in @See{coercion}. | 1843 | then the numbers are converted to strings |
1844 | in a non-specified format @see{coercion}. | ||
1841 | Otherwise, the @idx{__concat} metamethod is called @see{metatable}. | 1845 | Otherwise, the @idx{__concat} metamethod is called @see{metatable}. |
1842 | 1846 | ||
1843 | } | 1847 | } |
@@ -1846,9 +1850,9 @@ Otherwise, the @idx{__concat} metamethod is called @see{metatable}. | |||
1846 | 1850 | ||
1847 | The length operator is denoted by the unary prefix operator @T{#}. | 1851 | The length operator is denoted by the unary prefix operator @T{#}. |
1848 | 1852 | ||
1849 | The length of a string is its number of bytes | 1853 | The length of a string is its number of bytes. |
1850 | (that is, the usual meaning of string length when each | 1854 | (That is the usual meaning of string length when each |
1851 | character is one byte). | 1855 | character is one byte.) |
1852 | 1856 | ||
1853 | The length operator applied on a table | 1857 | The length operator applied on a table |
1854 | returns a @x{border} in that table. | 1858 | returns a @x{border} in that table. |
@@ -1867,8 +1871,10 @@ For instance, the table @T{{10, 20, 30, 40, 50}} is a sequence, | |||
1867 | as it has only one border (5). | 1871 | as it has only one border (5). |
1868 | The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), | 1872 | The table @T{{10, 20, 30, nil, 50}} has two borders (3 and 5), |
1869 | and therefore it is not a sequence. | 1873 | and therefore it is not a sequence. |
1874 | (The @nil at index 4 is called a @emphx{hole}.) | ||
1870 | The table @T{{nil, 20, 30, nil, nil, 60, nil}} | 1875 | The table @T{{nil, 20, 30, nil, nil, 60, nil}} |
1871 | has three borders (0, 3, and 6), | 1876 | has three borders (0, 3, and 6) and three holes |
1877 | (at indices 1, 4, and 5), | ||
1872 | so it is not a sequence, too. | 1878 | so it is not a sequence, too. |
1873 | The table @T{{}} is a sequence with border 0. | 1879 | The table @T{{}} is a sequence with border 0. |
1874 | Note that non-natural keys do not interfere | 1880 | Note that non-natural keys do not interfere |
@@ -1936,10 +1942,10 @@ Each field of the form @T{[exp1] = exp2} adds to the new table an entry | |||
1936 | with key @id{exp1} and value @id{exp2}. | 1942 | with key @id{exp1} and value @id{exp2}. |
1937 | A field of the form @T{name = exp} is equivalent to | 1943 | A field of the form @T{name = exp} is equivalent to |
1938 | @T{["name"] = exp}. | 1944 | @T{["name"] = exp}. |
1939 | Finally, fields of the form @id{exp} are equivalent to | 1945 | Fields of the form @id{exp} are equivalent to |
1940 | @T{[i] = exp}, where @id{i} are consecutive integers | 1946 | @T{[i] = exp}, where @id{i} are consecutive integers |
1941 | starting with 1. | 1947 | starting with 1; |
1942 | Fields in the other formats do not affect this counting. | 1948 | fields in the other formats do not affect this counting. |
1943 | For example, | 1949 | For example, |
1944 | @verbatim{ | 1950 | @verbatim{ |
1945 | a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } | 1951 | a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } |
@@ -1982,8 +1988,9 @@ first @bnfNter{prefixexp} and @bnfNter{args} are evaluated. | |||
1982 | If the value of @bnfNter{prefixexp} has type @emph{function}, | 1988 | If the value of @bnfNter{prefixexp} has type @emph{function}, |
1983 | then this function is called | 1989 | then this function is called |
1984 | with the given arguments. | 1990 | with the given arguments. |
1985 | Otherwise, the @bnfNter{prefixexp} @idx{__call} metamethod is called, | 1991 | Otherwise, if present, |
1986 | having as first argument the value of @bnfNter{prefixexp}, | 1992 | the @bnfNter{prefixexp} @idx{__call} metamethod is called: |
1993 | its first argument is the value of @bnfNter{prefixexp}, | ||
1987 | followed by the original call arguments | 1994 | followed by the original call arguments |
1988 | @see{metatable}. | 1995 | @see{metatable}. |
1989 | 1996 | ||
@@ -1991,7 +1998,7 @@ The form | |||
1991 | @Produc{ | 1998 | @Produc{ |
1992 | @producname{functioncall}@producbody{prefixexp @bnfter{:} @bnfNter{Name} args} | 1999 | @producname{functioncall}@producbody{prefixexp @bnfter{:} @bnfNter{Name} args} |
1993 | } | 2000 | } |
1994 | can be used to call @Q{methods}. | 2001 | can be used to emulate methods. |
1995 | A call @T{v:name(@rep{args})} | 2002 | A call @T{v:name(@rep{args})} |
1996 | is syntactic sugar for @T{v.name(v,@rep{args})}, | 2003 | is syntactic sugar for @T{v.name(v,@rep{args})}, |
1997 | except that @id{v} is evaluated only once. | 2004 | except that @id{v} is evaluated only once. |
@@ -2014,7 +2021,7 @@ that is, the argument list is a single literal string. | |||
2014 | A call of the form @T{return @rep{functioncall}} not in the | 2021 | A call of the form @T{return @rep{functioncall}} not in the |
2015 | scope of a to-be-closed variable is called a @def{tail call}. | 2022 | scope of a to-be-closed variable is called a @def{tail call}. |
2016 | Lua implements @def{proper tail calls} | 2023 | Lua implements @def{proper tail calls} |
2017 | (or @emph{proper tail recursion}): | 2024 | (or @def{proper tail recursion}): |
2018 | in a tail call, | 2025 | in a tail call, |
2019 | the called function reuses the stack entry of the calling function. | 2026 | the called function reuses the stack entry of the calling function. |
2020 | Therefore, there is no limit on the number of nested tail calls that | 2027 | Therefore, there is no limit on the number of nested tail calls that |
@@ -2086,10 +2093,11 @@ contains references to @id{f}.) | |||
2086 | A function definition is an executable expression, | 2093 | A function definition is an executable expression, |
2087 | whose value has type @emph{function}. | 2094 | whose value has type @emph{function}. |
2088 | When Lua precompiles a chunk, | 2095 | When Lua precompiles a chunk, |
2089 | all its function bodies are precompiled too. | 2096 | all its function bodies are precompiled too, |
2097 | but they are not created yet. | ||
2090 | Then, whenever Lua executes the function definition, | 2098 | Then, whenever Lua executes the function definition, |
2091 | the function is @emph{instantiated} (or @emph{closed}). | 2099 | the function is @emph{instantiated} (or @emph{closed}). |
2092 | This function instance (or @emphx{closure}) | 2100 | This function instance, or @emphx{closure}, |
2093 | is the final value of the expression. | 2101 | is the final value of the expression. |
2094 | 2102 | ||
2095 | Parameters act as local variables that are | 2103 | Parameters act as local variables that are |
@@ -2152,8 +2160,8 @@ that a function may return. | |||
2152 | This limit is guaranteed to be greater than 1000. | 2160 | This limit is guaranteed to be greater than 1000. |
2153 | 2161 | ||
2154 | The @emphx{colon} syntax | 2162 | The @emphx{colon} syntax |
2155 | is used for defining @def{methods}, | 2163 | is used to emulate @def{methods}, |
2156 | that is, functions that have an implicit extra parameter @idx{self}. | 2164 | adding an implicit extra parameter @idx{self} to the function. |
2157 | Thus, the statement | 2165 | Thus, the statement |
2158 | @verbatim{ | 2166 | @verbatim{ |
2159 | function t.a.b.c:f (@rep{params}) @rep{body} end | 2167 | function t.a.b.c:f (@rep{params}) @rep{body} end |
@@ -2282,8 +2290,8 @@ For convenience, | |||
2282 | most query operations in the API do not follow a strict stack discipline. | 2290 | most query operations in the API do not follow a strict stack discipline. |
2283 | Instead, they can refer to any element in the stack | 2291 | Instead, they can refer to any element in the stack |
2284 | by using an @emph{index}:@index{index (API stack)} | 2292 | by using an @emph{index}:@index{index (API stack)} |
2285 | A positive index represents an absolute stack position | 2293 | A positive index represents an absolute stack position, |
2286 | (starting @N{at 1}); | 2294 | starting @N{at 1} as the bottom of the stack; |
2287 | a negative index represents an offset relative to the top of the stack. | 2295 | a negative index represents an offset relative to the top of the stack. |
2288 | More specifically, if the stack has @rep{n} elements, | 2296 | More specifically, if the stack has @rep{n} elements, |
2289 | then @N{index 1} represents the first element | 2297 | then @N{index 1} represents the first element |
@@ -2353,7 +2361,7 @@ functions in the API work with acceptable indices. | |||
2353 | Acceptable indices serve to avoid extra tests | 2361 | Acceptable indices serve to avoid extra tests |
2354 | against the stack top when querying the stack. | 2362 | against the stack top when querying the stack. |
2355 | For instance, a @N{C function} can query its third argument | 2363 | For instance, a @N{C function} can query its third argument |
2356 | without the need to first check whether there is a third argument, | 2364 | without the need to check whether there is a third argument, |
2357 | that is, without the need to check whether 3 is a valid index. | 2365 | that is, without the need to check whether 3 is a valid index. |
2358 | 2366 | ||
2359 | For functions that can be called with acceptable indices, | 2367 | For functions that can be called with acceptable indices, |
@@ -2385,7 +2393,8 @@ current function | |||
2385 | which is one plus the maximum number of upvalues in a closure), | 2393 | which is one plus the maximum number of upvalues in a closure), |
2386 | produces an acceptable but invalid index. | 2394 | produces an acceptable but invalid index. |
2387 | 2395 | ||
2388 | A @N{C closure} can also change the values of its corresponding upvalues. | 2396 | A @N{C closure} can also change the values |
2397 | of its corresponding upvalues. | ||
2389 | 2398 | ||
2390 | } | 2399 | } |
2391 | 2400 | ||
@@ -2394,7 +2403,7 @@ A @N{C closure} can also change the values of its corresponding upvalues. | |||
2394 | Lua provides a @def{registry}, | 2403 | Lua provides a @def{registry}, |
2395 | a predefined table that can be used by any @N{C code} to | 2404 | a predefined table that can be used by any @N{C code} to |
2396 | store whatever Lua values it needs to store. | 2405 | store whatever Lua values it needs to store. |
2397 | The registry table is always located at pseudo-index | 2406 | The registry table is always accessible at pseudo-index |
2398 | @defid{LUA_REGISTRYINDEX}. | 2407 | @defid{LUA_REGISTRYINDEX}. |
2399 | Any @N{C library} can store data into this table, | 2408 | Any @N{C library} can store data into this table, |
2400 | but it must take care to choose keys | 2409 | but it must take care to choose keys |
@@ -2410,7 +2419,8 @@ uppercase letters are reserved for Lua. | |||
2410 | The integer keys in the registry are used | 2419 | The integer keys in the registry are used |
2411 | by the reference mechanism @seeC{luaL_ref} | 2420 | by the reference mechanism @seeC{luaL_ref} |
2412 | and by some predefined values. | 2421 | and by some predefined values. |
2413 | Therefore, integer keys must not be used for other purposes. | 2422 | Therefore, integer keys in the registry |
2423 | must not be used for other purposes. | ||
2414 | 2424 | ||
2415 | When you create a new Lua state, | 2425 | When you create a new Lua state, |
2416 | its registry comes with some predefined values. | 2426 | its registry comes with some predefined values. |
@@ -2435,15 +2445,16 @@ the @x{global environment}. | |||
2435 | Internally, Lua uses the C @id{longjmp} facility to handle errors. | 2445 | Internally, Lua uses the C @id{longjmp} facility to handle errors. |
2436 | (Lua will use exceptions if you compile it as C++; | 2446 | (Lua will use exceptions if you compile it as C++; |
2437 | search for @id{LUAI_THROW} in the source code for details.) | 2447 | search for @id{LUAI_THROW} in the source code for details.) |
2438 | When Lua faces any error | 2448 | When Lua faces any error, |
2439 | (such as a @x{memory allocation error} or a type error) | 2449 | such as a @x{memory allocation error} or a type error, |
2440 | it @emph{raises} an error; | 2450 | it @emph{raises} an error; |
2441 | that is, it does a long jump. | 2451 | that is, it does a long jump. |
2442 | A @emphx{protected environment} uses @id{setjmp} | 2452 | A @emphx{protected environment} uses @id{setjmp} |
2443 | to set a recovery point; | 2453 | to set a recovery point; |
2444 | any error jumps to the most recent active recovery point. | 2454 | any error jumps to the most recent active recovery point. |
2445 | 2455 | ||
2446 | Inside a @N{C function} you can raise an error by calling @Lid{lua_error}. | 2456 | Inside a @N{C function} you can raise an error explicitly |
2457 | by calling @Lid{lua_error}. | ||
2447 | 2458 | ||
2448 | Most functions in the API can raise an error, | 2459 | Most functions in the API can raise an error, |
2449 | for instance due to a @x{memory allocation error}. | 2460 | for instance due to a @x{memory allocation error}. |
@@ -2503,9 +2514,9 @@ the @emph{original function}. | |||
2503 | This original function then calls one of those three functions in the C API, | 2514 | This original function then calls one of those three functions in the C API, |
2504 | which we will call the @emph{callee function}, | 2515 | which we will call the @emph{callee function}, |
2505 | that then yields the current thread. | 2516 | that then yields the current thread. |
2506 | (This can happen when the callee function is @Lid{lua_yieldk}, | 2517 | This can happen when the callee function is @Lid{lua_yieldk}, |
2507 | or when the callee function is either @Lid{lua_callk} or @Lid{lua_pcallk} | 2518 | or when the callee function is either @Lid{lua_callk} or @Lid{lua_pcallk} |
2508 | and the function called by them yields.) | 2519 | and the function called by them yields. |
2509 | 2520 | ||
2510 | Suppose the running thread yields while executing the callee function. | 2521 | Suppose the running thread yields while executing the callee function. |
2511 | After the thread resumes, | 2522 | After the thread resumes, |
@@ -2566,11 +2577,11 @@ you can do the equivalent work directly inside the original function.) | |||
2566 | 2577 | ||
2567 | Besides the Lua state, | 2578 | Besides the Lua state, |
2568 | the continuation function has two other parameters: | 2579 | the continuation function has two other parameters: |
2569 | the final status of the call plus the context value (@id{ctx}) that | 2580 | the final status of the call and the context value (@id{ctx}) that |
2570 | was passed originally to @Lid{lua_pcallk}. | 2581 | was passed originally to @Lid{lua_pcallk}. |
2571 | (Lua does not use this context value; | 2582 | Lua does not use this context value; |
2572 | it only passes this value from the original function to the | 2583 | it only passes this value from the original function to the |
2573 | continuation function.) | 2584 | continuation function. |
2574 | For @Lid{lua_pcallk}, | 2585 | For @Lid{lua_pcallk}, |
2575 | the status is the same value that would be returned by @Lid{lua_pcallk}, | 2586 | the status is the same value that would be returned by @Lid{lua_pcallk}, |
2576 | except that it is @Lid{LUA_YIELD} when being executed after a yield | 2587 | except that it is @Lid{LUA_YIELD} when being executed after a yield |
@@ -2617,8 +2628,8 @@ A field in the form @T{x|y} means the function can push (or pop) | |||
2617 | depending on the situation; | 2628 | depending on the situation; |
2618 | an interrogation mark @Char{?} means that | 2629 | an interrogation mark @Char{?} means that |
2619 | we cannot know how many elements the function pops/pushes | 2630 | we cannot know how many elements the function pops/pushes |
2620 | by looking only at its arguments | 2631 | by looking only at its arguments. |
2621 | (e.g., they may depend on what is on the stack). | 2632 | (For instance, they may depend on what is on the stack.) |
2622 | The third field, @T{x}, | 2633 | The third field, @T{x}, |
2623 | tells whether the function may raise errors: | 2634 | tells whether the function may raise errors: |
2624 | @Char{-} means the function never raises any error; | 2635 | @Char{-} means the function never raises any error; |
@@ -2673,11 +2684,11 @@ Lua assumes the following behavior from the allocator function: | |||
2673 | 2684 | ||
2674 | When @id{nsize} is zero, | 2685 | When @id{nsize} is zero, |
2675 | the allocator must behave like @id{free} | 2686 | the allocator must behave like @id{free} |
2676 | and return @id{NULL}. | 2687 | and then return @id{NULL}. |
2677 | 2688 | ||
2678 | When @id{nsize} is not zero, | 2689 | When @id{nsize} is not zero, |
2679 | the allocator must behave like @id{realloc}. | 2690 | the allocator must behave like @id{realloc}. |
2680 | The allocator returns @id{NULL} | 2691 | In particular, the allocator returns @id{NULL} |
2681 | if and only if it cannot fulfill the request. | 2692 | if and only if it cannot fulfill the request. |
2682 | 2693 | ||
2683 | Here is a simple implementation for the @x{allocator function}. | 2694 | Here is a simple implementation for the @x{allocator function}. |
@@ -2752,9 +2763,9 @@ in direct order; | |||
2752 | that is, the first argument is pushed first. | 2763 | that is, the first argument is pushed first. |
2753 | Finally you call @Lid{lua_call}; | 2764 | Finally you call @Lid{lua_call}; |
2754 | @id{nargs} is the number of arguments that you pushed onto the stack. | 2765 | @id{nargs} is the number of arguments that you pushed onto the stack. |
2755 | All arguments and the function value are popped from the stack | 2766 | When the function returns, |
2756 | when the function is called. | 2767 | all arguments and the function value are popped |
2757 | The function results are pushed onto the stack when the function returns. | 2768 | and the function results are pushed onto the stack. |
2758 | The number of results is adjusted to @id{nresults}, | 2769 | The number of results is adjusted to @id{nresults}, |
2759 | unless @id{nresults} is @defid{LUA_MULTRET}. | 2770 | unless @id{nresults} is @defid{LUA_MULTRET}. |
2760 | In this case, all results from the function are pushed; | 2771 | In this case, all results from the function are pushed; |
@@ -2819,7 +2830,7 @@ The first argument (if any) is at index 1 | |||
2819 | and its last argument is at index @T{lua_gettop(L)}. | 2830 | and its last argument is at index @T{lua_gettop(L)}. |
2820 | To return values to Lua, a @N{C function} just pushes them onto the stack, | 2831 | To return values to Lua, a @N{C function} just pushes them onto the stack, |
2821 | in direct order (the first result is pushed first), | 2832 | in direct order (the first result is pushed first), |
2822 | and returns the number of results. | 2833 | and returns in C the number of results. |
2823 | Any other value in the stack below the results will be properly | 2834 | Any other value in the stack below the results will be properly |
2824 | discarded by Lua. | 2835 | discarded by Lua. |
2825 | Like a Lua function, a @N{C function} called by Lua can also return | 2836 | Like a Lua function, a @N{C function} called by Lua can also return |
@@ -2853,8 +2864,8 @@ static int foo (lua_State *L) { | |||
2853 | @APIEntry{int lua_checkstack (lua_State *L, int n);| | 2864 | @APIEntry{int lua_checkstack (lua_State *L, int n);| |
2854 | @apii{0,0,-} | 2865 | @apii{0,0,-} |
2855 | 2866 | ||
2856 | Ensures that the stack has space for at least @id{n} extra slots | 2867 | Ensures that the stack has space for at least @id{n} extra slots, |
2857 | (that is, that you can safely push up to @id{n} values into it). | 2868 | that is, that you can safely push up to @id{n} values into it. |
2858 | It returns false if it cannot fulfill the request, | 2869 | It returns false if it cannot fulfill the request, |
2859 | either because it would cause the stack | 2870 | either because it would cause the stack |
2860 | to be greater than a fixed maximum size | 2871 | to be greater than a fixed maximum size |
@@ -2872,6 +2883,7 @@ it is left unchanged. | |||
2872 | Destroys all objects in the given Lua state | 2883 | Destroys all objects in the given Lua state |
2873 | (calling the corresponding garbage-collection metamethods, if any) | 2884 | (calling the corresponding garbage-collection metamethods, if any) |
2874 | and frees all dynamic memory used by this state. | 2885 | and frees all dynamic memory used by this state. |
2886 | |||
2875 | On several platforms, you may not need to call this function, | 2887 | On several platforms, you may not need to call this function, |
2876 | because all resources are naturally released when the host program ends. | 2888 | because all resources are naturally released when the host program ends. |
2877 | On the other hand, long-running programs that create multiple states, | 2889 | On the other hand, long-running programs that create multiple states, |
@@ -2934,7 +2946,7 @@ will have as a sequence; | |||
2934 | parameter @id{nrec} is a hint for how many other elements | 2946 | parameter @id{nrec} is a hint for how many other elements |
2935 | the table will have. | 2947 | the table will have. |
2936 | Lua may use these hints to preallocate memory for the new table. | 2948 | Lua may use these hints to preallocate memory for the new table. |
2937 | This preallocation is useful for performance when you know in advance | 2949 | This preallocation may help performance when you know in advance |
2938 | how many elements the table will have. | 2950 | how many elements the table will have. |
2939 | Otherwise you can use the function @Lid{lua_newtable}. | 2951 | Otherwise you can use the function @Lid{lua_newtable}. |
2940 | 2952 | ||
@@ -3369,11 +3381,11 @@ Other upvalues are initialized with @nil. | |||
3369 | @APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| | 3381 | @APIEntry{lua_State *lua_newstate (lua_Alloc f, void *ud);| |
3370 | @apii{0,0,-} | 3382 | @apii{0,0,-} |
3371 | 3383 | ||
3372 | Creates a new thread running in a new, independent state. | 3384 | Creates a new independent state and returns its main thread. |
3373 | Returns @id{NULL} if it cannot create the thread or the state | 3385 | Returns @id{NULL} if it cannot create the state |
3374 | (due to lack of memory). | 3386 | (due to lack of memory). |
3375 | The argument @id{f} is the @x{allocator function}; | 3387 | The argument @id{f} is the @x{allocator function}; |
3376 | Lua does all memory allocation for this state | 3388 | Lua will do all memory allocation for this state |
3377 | through this function @seeF{lua_Alloc}. | 3389 | through this function @seeF{lua_Alloc}. |
3378 | The second argument, @id{ud}, is an opaque pointer that Lua | 3390 | The second argument, @id{ud}, is an opaque pointer that Lua |
3379 | passes to the allocator in every call. | 3391 | passes to the allocator in every call. |
@@ -3407,7 +3419,7 @@ like any Lua object. | |||
3407 | @apii{0,1,m} | 3419 | @apii{0,1,m} |
3408 | 3420 | ||
3409 | This function creates and pushes on the stack a new full userdata, | 3421 | This function creates and pushes on the stack a new full userdata, |
3410 | with @id{nuvalue} associated Lua values (called @id{user values}) | 3422 | with @id{nuvalue} associated Lua values, called @id{user values}, |
3411 | plus an associated block of raw memory with @id{size} bytes. | 3423 | plus an associated block of raw memory with @id{size} bytes. |
3412 | (The user values can be set and read with the functions | 3424 | (The user values can be set and read with the functions |
3413 | @Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) | 3425 | @Lid{lua_setiuservalue} and @Lid{lua_getiuservalue}.) |
@@ -3420,12 +3432,12 @@ The function returns the address of the block of memory. | |||
3420 | @apii{1,2|0,v} | 3432 | @apii{1,2|0,v} |
3421 | 3433 | ||
3422 | Pops a key from the stack, | 3434 | Pops a key from the stack, |
3423 | and pushes a key@En{}value pair from the table at the given index | 3435 | and pushes a key@En{}value pair from the table at the given index, |
3424 | (the @Q{next} pair after the given key). | 3436 | the @Q{next} pair after the given key. |
3425 | If there are no more elements in the table, | 3437 | If there are no more elements in the table, |
3426 | then @Lid{lua_next} returns 0 (and pushes nothing). | 3438 | then @Lid{lua_next} returns 0 and pushes nothing. |
3427 | 3439 | ||
3428 | A typical traversal looks like this: | 3440 | A typical table traversal looks like this: |
3429 | @verbatim{ | 3441 | @verbatim{ |
3430 | /* table is in the stack at index 't' */ | 3442 | /* table is in the stack at index 't' */ |
3431 | lua_pushnil(L); /* first key */ | 3443 | lua_pushnil(L); /* first key */ |
@@ -3440,7 +3452,7 @@ while (lua_next(L, t) != 0) { | |||
3440 | } | 3452 | } |
3441 | 3453 | ||
3442 | While traversing a table, | 3454 | While traversing a table, |
3443 | do not call @Lid{lua_tolstring} directly on a key, | 3455 | avoid calling @Lid{lua_tolstring} directly on a key, |
3444 | unless you know that the key is actually a string. | 3456 | unless you know that the key is actually a string. |
3445 | Recall that @Lid{lua_tolstring} may change | 3457 | Recall that @Lid{lua_tolstring} may change |
3446 | the value at the given index; | 3458 | the value at the given index; |
@@ -3465,15 +3477,14 @@ but that can be changed to a single float or a long double. | |||
3465 | 3477 | ||
3466 | @APIEntry{int lua_numbertointeger (lua_Number n, lua_Integer *p);| | 3478 | @APIEntry{int lua_numbertointeger (lua_Number n, lua_Integer *p);| |
3467 | 3479 | ||
3468 | Converts a Lua float to a Lua integer. | 3480 | Tries to convert a Lua float to a Lua integer; |
3469 | This macro assumes that @id{n} has an integral value. | 3481 | the float @id{n} must have an integral value. |
3470 | If that value is within the range of Lua integers, | 3482 | If that value is within the range of Lua integers, |
3471 | it is converted to an integer and assigned to @T{*p}. | 3483 | it is converted to an integer and assigned to @T{*p}. |
3472 | The macro results in a boolean indicating whether the | 3484 | The macro results in a boolean indicating whether the |
3473 | conversion was successful. | 3485 | conversion was successful. |
3474 | (Note that this range test can be tricky to do | 3486 | (Note that this range test can be tricky to do |
3475 | correctly without this macro, | 3487 | correctly without this macro, due to rounding.) |
3476 | due to roundings.) | ||
3477 | 3488 | ||
3478 | This macro may evaluate its arguments more than once. | 3489 | This macro may evaluate its arguments more than once. |
3479 | 3490 | ||
@@ -3503,7 +3514,7 @@ Otherwise, @id{msgh} is the stack index of a | |||
3503 | @emph{message handler}. | 3514 | @emph{message handler}. |
3504 | (This index cannot be a pseudo-index.) | 3515 | (This index cannot be a pseudo-index.) |
3505 | In case of runtime errors, | 3516 | In case of runtime errors, |
3506 | this function will be called with the error object | 3517 | this handler will be called with the error object |
3507 | and its return value will be the object | 3518 | and its return value will be the object |
3508 | returned on the stack by @Lid{lua_pcall}. | 3519 | returned on the stack by @Lid{lua_pcall}. |
3509 | 3520 | ||
@@ -3580,11 +3591,12 @@ and return its results @seeC{lua_CFunction}. | |||
3580 | 3591 | ||
3581 | When a @N{C function} is created, | 3592 | When a @N{C function} is created, |
3582 | it is possible to associate some values with it, | 3593 | it is possible to associate some values with it, |
3583 | thus creating a @x{@N{C closure}} @see{c-closure}; | 3594 | the so called upvalues; |
3584 | these values are then accessible to the function whenever it is called. | 3595 | these upvalues are then accessible to the function whenever it is called. |
3585 | To associate values with a @N{C function}, | 3596 | This association is called a @x{@N{C closure}} @see{c-closure}. |
3586 | first these values must be pushed onto the stack | 3597 | To create a @N{C closure}, |
3587 | (when there are multiple values, the first value is pushed first). | 3598 | first the initial values for its upvalues must be pushed onto the stack. |
3599 | (When there are multiple upvalues, the first value is pushed first.) | ||
3588 | Then @Lid{lua_pushcclosure} | 3600 | Then @Lid{lua_pushcclosure} |
3589 | is called to create and push the @N{C function} onto the stack, | 3601 | is called to create and push the @N{C function} onto the stack, |
3590 | with the argument @id{n} telling how many values will be | 3602 | with the argument @id{n} telling how many values will be |
@@ -3604,6 +3616,7 @@ In that case, it never raises a memory error. | |||
3604 | @apii{0,1,-} | 3616 | @apii{0,1,-} |
3605 | 3617 | ||
3606 | Pushes a @N{C function} onto the stack. | 3618 | Pushes a @N{C function} onto the stack. |
3619 | This function is equivalent to @Lid{lua_pushcclosure} with no upvalues. | ||
3607 | 3620 | ||
3608 | } | 3621 | } |
3609 | 3622 | ||
@@ -3626,7 +3639,7 @@ The conversion specifiers can only be | |||
3626 | @Char{%s} (inserts a zero-terminated string, with no size restrictions), | 3639 | @Char{%s} (inserts a zero-terminated string, with no size restrictions), |
3627 | @Char{%f} (inserts a @Lid{lua_Number}), | 3640 | @Char{%f} (inserts a @Lid{lua_Number}), |
3628 | @Char{%I} (inserts a @Lid{lua_Integer}), | 3641 | @Char{%I} (inserts a @Lid{lua_Integer}), |
3629 | @Char{%p} (inserts a pointer as a hexadecimal numeral), | 3642 | @Char{%p} (inserts a pointer), |
3630 | @Char{%d} (inserts an @T{int}), | 3643 | @Char{%d} (inserts an @T{int}), |
3631 | @Char{%c} (inserts an @T{int} as a one-byte character), and | 3644 | @Char{%c} (inserts an @T{int} as a one-byte character), and |
3632 | @Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence). | 3645 | @Char{%U} (inserts a @T{long int} as a @x{UTF-8} byte sequence). |
@@ -3670,6 +3683,7 @@ light userdata with the same @N{C address}. | |||
3670 | 3683 | ||
3671 | This macro is equivalent to @Lid{lua_pushstring}, | 3684 | This macro is equivalent to @Lid{lua_pushstring}, |
3672 | but should be used only when @id{s} is a literal string. | 3685 | but should be used only when @id{s} is a literal string. |
3686 | (Lua may optimize this case.) | ||
3673 | 3687 | ||
3674 | } | 3688 | } |
3675 | 3689 | ||
@@ -3678,7 +3692,7 @@ but should be used only when @id{s} is a literal string. | |||
3678 | 3692 | ||
3679 | Pushes the string pointed to by @id{s} with size @id{len} | 3693 | Pushes the string pointed to by @id{s} with size @id{len} |
3680 | onto the stack. | 3694 | onto the stack. |
3681 | Lua makes (or reuses) an internal copy of the given string, | 3695 | Lua will make or reuse an internal copy of the given string, |
3682 | so the memory at @id{s} can be freed or reused immediately after | 3696 | so the memory at @id{s} can be freed or reused immediately after |
3683 | the function returns. | 3697 | the function returns. |
3684 | The string can contain any binary data, | 3698 | The string can contain any binary data, |
@@ -3707,7 +3721,7 @@ Pushes a float with value @id{n} onto the stack. | |||
3707 | 3721 | ||
3708 | Pushes the zero-terminated string pointed to by @id{s} | 3722 | Pushes the zero-terminated string pointed to by @id{s} |
3709 | onto the stack. | 3723 | onto the stack. |
3710 | Lua makes (or reuses) an internal copy of the given string, | 3724 | Lua will make or reuse an internal copy of the given string, |
3711 | so the memory at @id{s} can be freed or reused immediately after | 3725 | so the memory at @id{s} can be freed or reused immediately after |
3712 | the function returns. | 3726 | the function returns. |
3713 | 3727 | ||
@@ -3749,7 +3763,7 @@ instead of a variable number of arguments. | |||
3749 | 3763 | ||
3750 | Returns 1 if the two values in indices @id{index1} and | 3764 | Returns 1 if the two values in indices @id{index1} and |
3751 | @id{index2} are primitively equal | 3765 | @id{index2} are primitively equal |
3752 | (that is, without calling the @idx{__eq} metamethod). | 3766 | (that is, equal without calling the @idx{__eq} metamethod). |
3753 | Otherwise @N{returns 0}. | 3767 | Otherwise @N{returns 0}. |
3754 | Also @N{returns 0} if any of the indices are not valid. | 3768 | Also @N{returns 0} if any of the indices are not valid. |
3755 | 3769 | ||
@@ -3796,8 +3810,8 @@ for strings, this is the string length; | |||
3796 | for tables, this is the result of the length operator (@Char{#}) | 3810 | for tables, this is the result of the length operator (@Char{#}) |
3797 | with no metamethods; | 3811 | with no metamethods; |
3798 | for userdata, this is the size of the block of memory allocated | 3812 | for userdata, this is the size of the block of memory allocated |
3799 | for the userdata; | 3813 | for the userdata. |
3800 | for other values, it @N{is 0}. | 3814 | For other values, this call @N{returns 0}. |
3801 | 3815 | ||
3802 | } | 3816 | } |
3803 | 3817 | ||
@@ -3842,8 +3856,8 @@ typedef const char * (*lua_Reader) (lua_State *L, | |||
3842 | size_t *size);| | 3856 | size_t *size);| |
3843 | 3857 | ||
3844 | The reader function used by @Lid{lua_load}. | 3858 | The reader function used by @Lid{lua_load}. |
3845 | Every time it needs another piece of the chunk, | 3859 | Every time @Lid{lua_load} needs another piece of the chunk, |
3846 | @Lid{lua_load} calls the reader, | 3860 | it calls the reader, |
3847 | passing along its @id{data} parameter. | 3861 | passing along its @id{data} parameter. |
3848 | The reader must return a pointer to a block of memory | 3862 | The reader must return a pointer to a block of memory |
3849 | with a new piece of the chunk | 3863 | with a new piece of the chunk |
@@ -3912,9 +3926,9 @@ then you call @Lid{lua_resume}, | |||
3912 | with @id{nargs} being the number of arguments. | 3926 | with @id{nargs} being the number of arguments. |
3913 | This call returns when the coroutine suspends or finishes its execution. | 3927 | This call returns when the coroutine suspends or finishes its execution. |
3914 | When it returns, | 3928 | When it returns, |
3915 | @id{nresults} is updated and | 3929 | @id{*nresults} is updated and |
3916 | the top of the stack contains | 3930 | the top of the stack contains |
3917 | the @id{nresults} values passed to @Lid{lua_yield} | 3931 | the @id{*nresults} values passed to @Lid{lua_yield} |
3918 | or returned by the body function. | 3932 | or returned by the body function. |
3919 | @Lid{lua_resume} returns | 3933 | @Lid{lua_resume} returns |
3920 | @Lid{LUA_YIELD} if the coroutine yields, | 3934 | @Lid{LUA_YIELD} if the coroutine yields, |
@@ -3924,10 +3938,8 @@ or an error code in case of errors @seeC{lua_pcall}. | |||
3924 | In case of errors, | 3938 | In case of errors, |
3925 | the error object is on the top of the stack. | 3939 | the error object is on the top of the stack. |
3926 | 3940 | ||
3927 | To resume a coroutine, | 3941 | To resume a coroutine, you clear its stack, |
3928 | you remove all results from the last @Lid{lua_yield}, | 3942 | push only the values to be passed as results from @id{yield}, |
3929 | put on its stack only the values to | ||
3930 | be passed as results from @id{yield}, | ||
3931 | and then call @Lid{lua_resume}. | 3943 | and then call @Lid{lua_resume}. |
3932 | 3944 | ||
3933 | The parameter @id{from} represents the coroutine that is resuming @id{L}. | 3945 | The parameter @id{from} represents the coroutine that is resuming @id{L}. |
@@ -4066,12 +4078,12 @@ which creates a Lua state from scratch. | |||
4066 | 4078 | ||
4067 | Returns the status of the thread @id{L}. | 4079 | Returns the status of the thread @id{L}. |
4068 | 4080 | ||
4069 | The status can be 0 (@Lid{LUA_OK}) for a normal thread, | 4081 | The status can be @Lid{LUA_OK} for a normal thread, |
4070 | an error code if the thread finished the execution | 4082 | an error code if the thread finished the execution |
4071 | of a @Lid{lua_resume} with an error, | 4083 | of a @Lid{lua_resume} with an error, |
4072 | or @defid{LUA_YIELD} if the thread is suspended. | 4084 | or @defid{LUA_YIELD} if the thread is suspended. |
4073 | 4085 | ||
4074 | You can only call functions in threads with status @Lid{LUA_OK}. | 4086 | You can call functions only in threads with status @Lid{LUA_OK}. |
4075 | You can resume threads with status @Lid{LUA_OK} | 4087 | You can resume threads with status @Lid{LUA_OK} |
4076 | (to start a new coroutine) or @Lid{LUA_YIELD} | 4088 | (to start a new coroutine) or @Lid{LUA_YIELD} |
4077 | (to resume a coroutine). | 4089 | (to resume a coroutine). |
@@ -4127,7 +4139,7 @@ Like a to-be-closed variable in Lua, | |||
4127 | the value at that index in the stack will be closed | 4139 | the value at that index in the stack will be closed |
4128 | when it goes out of scope. | 4140 | when it goes out of scope. |
4129 | Here, in the context of a C function, | 4141 | Here, in the context of a C function, |
4130 | to go out of scope means that the running function returns (to Lua), | 4142 | to go out of scope means that the running function returns to Lua, |
4131 | there is an error, | 4143 | there is an error, |
4132 | or the index is removed from the stack through | 4144 | or the index is removed from the stack through |
4133 | @Lid{lua_settop} or @Lid{lua_pop}. | 4145 | @Lid{lua_settop} or @Lid{lua_pop}. |
@@ -4250,7 +4262,7 @@ otherwise, the function returns @id{NULL}. | |||
4250 | If the value at the given index is a full userdata, | 4262 | If the value at the given index is a full userdata, |
4251 | returns its memory-block address. | 4263 | returns its memory-block address. |
4252 | If the value is a light userdata, | 4264 | If the value is a light userdata, |
4253 | returns its pointer. | 4265 | returns its value (a pointer). |
4254 | Otherwise, returns @id{NULL}. | 4266 | Otherwise, returns @id{NULL}. |
4255 | 4267 | ||
4256 | } | 4268 | } |
@@ -4259,7 +4271,7 @@ Otherwise, returns @id{NULL}. | |||
4259 | @apii{0,0,-} | 4271 | @apii{0,0,-} |
4260 | 4272 | ||
4261 | Returns the type of the value in the given valid index, | 4273 | Returns the type of the value in the given valid index, |
4262 | or @id{LUA_TNONE} for a non-valid (but acceptable) index. | 4274 | or @id{LUA_TNONE} for a non-valid but acceptable index. |
4263 | The types returned by @Lid{lua_type} are coded by the following constants | 4275 | The types returned by @Lid{lua_type} are coded by the following constants |
4264 | defined in @id{lua.h}: | 4276 | defined in @id{lua.h}: |
4265 | @defid{LUA_TNIL}, | 4277 | @defid{LUA_TNIL}, |
@@ -4335,8 +4347,8 @@ typedef int (*lua_Writer) (lua_State *L, | |||
4335 | void* ud);| | 4347 | void* ud);| |
4336 | 4348 | ||
4337 | The type of the writer function used by @Lid{lua_dump}. | 4349 | The type of the writer function used by @Lid{lua_dump}. |
4338 | Every time it produces another piece of chunk, | 4350 | Every time @Lid{lua_dump} produces another piece of chunk, |
4339 | @Lid{lua_dump} calls the writer, | 4351 | it calls the writer, |
4340 | passing along the buffer to be written (@id{p}), | 4352 | passing along the buffer to be written (@id{p}), |
4341 | its size (@id{sz}), | 4353 | its size (@id{sz}), |
4342 | and the @id{ud} parameter supplied to @Lid{lua_dump}. | 4354 | and the @id{ud} parameter supplied to @Lid{lua_dump}. |
@@ -4414,7 +4426,7 @@ of the (Lua) function that triggered the hook. | |||
4414 | 4426 | ||
4415 | This function can raise an error if it is called from a thread | 4427 | This function can raise an error if it is called from a thread |
4416 | with a pending C call with no continuation function | 4428 | with a pending C call with no continuation function |
4417 | (what is called a @emphx{C-call boundary}, | 4429 | (what is called a @emphx{C-call boundary}), |
4418 | or it is called from a thread that is not running inside a resume | 4430 | or it is called from a thread that is not running inside a resume |
4419 | (typically the main thread). | 4431 | (typically the main thread). |
4420 | 4432 | ||
@@ -4439,6 +4451,7 @@ typedef struct lua_Debug { | |||
4439 | const char *namewhat; /* (n) */ | 4451 | const char *namewhat; /* (n) */ |
4440 | const char *what; /* (S) */ | 4452 | const char *what; /* (S) */ |
4441 | const char *source; /* (S) */ | 4453 | const char *source; /* (S) */ |
4454 | size_t srclen; /* (S) */ | ||
4442 | int currentline; /* (l) */ | 4455 | int currentline; /* (l) */ |
4443 | int linedefined; /* (S) */ | 4456 | int linedefined; /* (S) */ |
4444 | int lastlinedefined; /* (S) */ | 4457 | int lastlinedefined; /* (S) */ |
@@ -4459,13 +4472,13 @@ information about a function or an activation record. | |||
4459 | @Lid{lua_getstack} fills only the private part | 4472 | @Lid{lua_getstack} fills only the private part |
4460 | of this structure, for later use. | 4473 | of this structure, for later use. |
4461 | To fill the other fields of @Lid{lua_Debug} with useful information, | 4474 | To fill the other fields of @Lid{lua_Debug} with useful information, |
4462 | call @Lid{lua_getinfo}. | 4475 | you must call @Lid{lua_getinfo}. |
4463 | 4476 | ||
4464 | The fields of @Lid{lua_Debug} have the following meaning: | 4477 | The fields of @Lid{lua_Debug} have the following meaning: |
4465 | @description{ | 4478 | @description{ |
4466 | 4479 | ||
4467 | @item{@id{source}| | 4480 | @item{@id{source}| |
4468 | the name of the chunk that created the function. | 4481 | the source of the chunk that created the function. |
4469 | If @T{source} starts with a @Char{@At}, | 4482 | If @T{source} starts with a @Char{@At}, |
4470 | it means that the function was defined in a file where | 4483 | it means that the function was defined in a file where |
4471 | the file name follows the @Char{@At}. | 4484 | the file name follows the @Char{@At}. |
@@ -4476,6 +4489,10 @@ the function was defined in a string where | |||
4476 | @T{source} is that string. | 4489 | @T{source} is that string. |
4477 | } | 4490 | } |
4478 | 4491 | ||
4492 | @item{@id{srclen}| | ||
4493 | The length of the string @id{source}. | ||
4494 | } | ||
4495 | |||
4479 | @item{@id{short_src}| | 4496 | @item{@id{short_src}| |
4480 | a @Q{printable} version of @T{source}, to be used in error messages. | 4497 | a @Q{printable} version of @T{source}, to be used in error messages. |
4481 | } | 4498 | } |
@@ -4694,9 +4711,9 @@ of the function executing at a given level. | |||
4694 | @N{Level 0} is the current running function, | 4711 | @N{Level 0} is the current running function, |
4695 | whereas level @M{n+1} is the function that has called level @M{n} | 4712 | whereas level @M{n+1} is the function that has called level @M{n} |
4696 | (except for tail calls, which do not count on the stack). | 4713 | (except for tail calls, which do not count on the stack). |
4697 | When there are no errors, @Lid{lua_getstack} returns 1; | 4714 | When called with a level greater than the stack depth, |
4698 | when called with a level greater than the stack depth, | 4715 | @Lid{lua_getstack} returns 0; |
4699 | it returns 0. | 4716 | otherwise it returns 1. |
4700 | 4717 | ||
4701 | } | 4718 | } |
4702 | 4719 | ||
@@ -4716,10 +4733,6 @@ as a name for all upvalues. | |||
4716 | upvalues are the external local variables that the function uses, | 4733 | upvalues are the external local variables that the function uses, |
4717 | and that are consequently included in its closure.) | 4734 | and that are consequently included in its closure.) |
4718 | 4735 | ||
4719 | Upvalues have no particular order, | ||
4720 | as they are active through the whole function. | ||
4721 | They are numbered in an arbitrary order. | ||
4722 | |||
4723 | } | 4736 | } |
4724 | 4737 | ||
4725 | @APIEntry{typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);| | 4738 | @APIEntry{typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);| |
@@ -4780,24 +4793,22 @@ before the function gets its arguments. | |||
4780 | 4793 | ||
4781 | @item{The return hook| is called when the interpreter returns from a function. | 4794 | @item{The return hook| is called when the interpreter returns from a function. |
4782 | The hook is called just before Lua leaves the function. | 4795 | The hook is called just before Lua leaves the function. |
4783 | There is no standard way to access the values | ||
4784 | to be returned by the function. | ||
4785 | } | 4796 | } |
4786 | 4797 | ||
4787 | @item{The line hook| is called when the interpreter is about to | 4798 | @item{The line hook| is called when the interpreter is about to |
4788 | start the execution of a new line of code, | 4799 | start the execution of a new line of code, |
4789 | or when it jumps back in the code (even to the same line). | 4800 | or when it jumps back in the code (even to the same line). |
4790 | (This event only happens while Lua is executing a Lua function.) | 4801 | This event only happens while Lua is executing a Lua function. |
4791 | } | 4802 | } |
4792 | 4803 | ||
4793 | @item{The count hook| is called after the interpreter executes every | 4804 | @item{The count hook| is called after the interpreter executes every |
4794 | @T{count} instructions. | 4805 | @T{count} instructions. |
4795 | (This event only happens while Lua is executing a Lua function.) | 4806 | This event only happens while Lua is executing a Lua function. |
4796 | } | 4807 | } |
4797 | 4808 | ||
4798 | } | 4809 | } |
4799 | 4810 | ||
4800 | A hook is disabled by setting @id{mask} to zero. | 4811 | Hooks are disabled by setting @id{mask} to zero. |
4801 | 4812 | ||
4802 | } | 4813 | } |
4803 | 4814 | ||
@@ -4813,7 +4824,7 @@ Returns @id{NULL} (and pops nothing) | |||
4813 | when the index is greater than | 4824 | when the index is greater than |
4814 | the number of active local variables. | 4825 | the number of active local variables. |
4815 | 4826 | ||
4816 | Parameters @id{ar} and @id{n} are as in function @Lid{lua_getlocal}. | 4827 | Parameters @id{ar} and @id{n} are as in the function @Lid{lua_getlocal}. |
4817 | 4828 | ||
4818 | } | 4829 | } |
4819 | 4830 | ||
@@ -4828,7 +4839,8 @@ It also pops the value from the stack. | |||
4828 | Returns @id{NULL} (and pops nothing) | 4839 | Returns @id{NULL} (and pops nothing) |
4829 | when the index @id{n} is greater than the number of upvalues. | 4840 | when the index @id{n} is greater than the number of upvalues. |
4830 | 4841 | ||
4831 | Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}. | 4842 | Parameters @id{funcindex} and @id{n} are as in |
4843 | the function @Lid{lua_getupvalue}. | ||
4832 | 4844 | ||
4833 | } | 4845 | } |
4834 | 4846 | ||
@@ -4844,7 +4856,8 @@ Lua closures that share an upvalue | |||
4844 | (that is, that access a same external local variable) | 4856 | (that is, that access a same external local variable) |
4845 | will return identical ids for those upvalue indices. | 4857 | will return identical ids for those upvalue indices. |
4846 | 4858 | ||
4847 | Parameters @id{funcindex} and @id{n} are as in function @Lid{lua_getupvalue}, | 4859 | Parameters @id{funcindex} and @id{n} are as in |
4860 | the function @Lid{lua_getupvalue}, | ||
4848 | but @id{n} cannot be greater than the number of upvalues. | 4861 | but @id{n} cannot be greater than the number of upvalues. |
4849 | 4862 | ||
4850 | } | 4863 | } |
@@ -5086,7 +5099,7 @@ this function calls this field passing the object as its only argument. | |||
5086 | In this case this function returns true and pushes onto the | 5099 | In this case this function returns true and pushes onto the |
5087 | stack the value returned by the call. | 5100 | stack the value returned by the call. |
5088 | If there is no metatable or no metamethod, | 5101 | If there is no metatable or no metamethod, |
5089 | this function returns false (without pushing any value on the stack). | 5102 | this function returns false without pushing any value on the stack. |
5090 | 5103 | ||
5091 | } | 5104 | } |
5092 | 5105 | ||
@@ -5103,7 +5116,7 @@ of any type (including @nil) at position @id{arg}. | |||
5103 | 5116 | ||
5104 | Checks whether the function argument @id{arg} is an integer | 5117 | Checks whether the function argument @id{arg} is an integer |
5105 | (or can be converted to an integer) | 5118 | (or can be converted to an integer) |
5106 | and returns this integer cast to a @Lid{lua_Integer}. | 5119 | and returns this integer. |
5107 | 5120 | ||
5108 | } | 5121 | } |
5109 | 5122 | ||
@@ -5112,7 +5125,7 @@ and returns this integer cast to a @Lid{lua_Integer}. | |||
5112 | 5125 | ||
5113 | Checks whether the function argument @id{arg} is a string | 5126 | Checks whether the function argument @id{arg} is a string |
5114 | and returns this string; | 5127 | and returns this string; |
5115 | if @id{l} is not @id{NULL} fills @T{*l} | 5128 | if @id{l} is not @id{NULL} fills its referent |
5116 | with the string's length. | 5129 | with the string's length. |
5117 | 5130 | ||
5118 | This function uses @Lid{lua_tolstring} to get its result, | 5131 | This function uses @Lid{lua_tolstring} to get its result, |
@@ -5124,7 +5137,7 @@ so all conversions and caveats of that function apply here. | |||
5124 | @apii{0,0,v} | 5137 | @apii{0,0,v} |
5125 | 5138 | ||
5126 | Checks whether the function argument @id{arg} is a number | 5139 | Checks whether the function argument @id{arg} is a number |
5127 | and returns this number. | 5140 | and returns this number converted to a @id{lua_Number}. |
5128 | 5141 | ||
5129 | } | 5142 | } |
5130 | 5143 | ||
@@ -5300,8 +5313,8 @@ const char *luaL_gsub (lua_State *L, | |||
5300 | const char *r);| | 5313 | const char *r);| |
5301 | @apii{0,1,m} | 5314 | @apii{0,1,m} |
5302 | 5315 | ||
5303 | Creates a copy of string @id{s} by replacing | 5316 | Creates a copy of string @id{s}, |
5304 | any occurrence of the string @id{p} | 5317 | replacing any occurrence of the string @id{p} |
5305 | with the string @id{r}. | 5318 | with the string @id{r}. |
5306 | Pushes the resulting string on the stack and returns it. | 5319 | Pushes the resulting string on the stack and returns it. |
5307 | 5320 | ||
@@ -5314,7 +5327,7 @@ Returns the @Q{length} of the value at the given index | |||
5314 | as a number; | 5327 | as a number; |
5315 | it is equivalent to the @Char{#} operator in Lua @see{len-op}. | 5328 | it is equivalent to the @Char{#} operator in Lua @see{len-op}. |
5316 | Raises an error if the result of the operation is not an integer. | 5329 | Raises an error if the result of the operation is not an integer. |
5317 | (This case only can happen through metamethods.) | 5330 | (This case can only happen through metamethods.) |
5318 | 5331 | ||
5319 | } | 5332 | } |
5320 | 5333 | ||
@@ -5345,7 +5358,7 @@ buffer pointed to by @id{buff} with size @id{sz}. | |||
5345 | This function returns the same results as @Lid{lua_load}. | 5358 | This function returns the same results as @Lid{lua_load}. |
5346 | @id{name} is the chunk name, | 5359 | @id{name} is the chunk name, |
5347 | used for debug information and error messages. | 5360 | used for debug information and error messages. |
5348 | The string @id{mode} works as in function @Lid{lua_load}. | 5361 | The string @id{mode} works as in the function @Lid{lua_load}. |
5349 | 5362 | ||
5350 | } | 5363 | } |
5351 | 5364 | ||
@@ -5368,7 +5381,7 @@ If @id{filename} is @id{NULL}, | |||
5368 | then it loads from the standard input. | 5381 | then it loads from the standard input. |
5369 | The first line in the file is ignored if it starts with a @T{#}. | 5382 | The first line in the file is ignored if it starts with a @T{#}. |
5370 | 5383 | ||
5371 | The string @id{mode} works as in function @Lid{lua_load}. | 5384 | The string @id{mode} works as in the function @Lid{lua_load}. |
5372 | 5385 | ||
5373 | This function returns the same results as @Lid{lua_load}, | 5386 | This function returns the same results as @Lid{lua_load}, |
5374 | but it has an extra error code @defid{LUA_ERRFILE} | 5387 | but it has an extra error code @defid{LUA_ERRFILE} |
@@ -5399,7 +5412,7 @@ it does not run it. | |||
5399 | @apii{0,1,m} | 5412 | @apii{0,1,m} |
5400 | 5413 | ||
5401 | Creates a new table and registers there | 5414 | Creates a new table and registers there |
5402 | the functions in list @id{l}. | 5415 | the functions in the list @id{l}. |
5403 | 5416 | ||
5404 | It is implemented as the following macro: | 5417 | It is implemented as the following macro: |
5405 | @verbatim{ | 5418 | @verbatim{ |
@@ -5437,7 +5450,8 @@ adds to the registry the pair @T{[tname] = new table}, | |||
5437 | and returns 1. | 5450 | and returns 1. |
5438 | (The entry @idx{__name} is used by some error-reporting functions.) | 5451 | (The entry @idx{__name} is used by some error-reporting functions.) |
5439 | 5452 | ||
5440 | In both cases pushes onto the stack the final value associated | 5453 | In both cases, |
5454 | the function pushes onto the stack the final value associated | ||
5441 | with @id{tname} in the registry. | 5455 | with @id{tname} in the registry. |
5442 | 5456 | ||
5443 | } | 5457 | } |
@@ -5447,10 +5461,9 @@ with @id{tname} in the registry. | |||
5447 | 5461 | ||
5448 | Creates a new Lua state. | 5462 | Creates a new Lua state. |
5449 | It calls @Lid{lua_newstate} with an | 5463 | It calls @Lid{lua_newstate} with an |
5450 | allocator based on the @N{standard C} @id{realloc} function | 5464 | allocator based on the @N{standard C} allocation functions |
5451 | and then sets a panic function @see{C-error} that prints | 5465 | and then sets a warning function and a panic function @see{C-error} |
5452 | an error message to the standard error output in case of fatal | 5466 | that print messages to the standard error output. |
5453 | errors. | ||
5454 | 5467 | ||
5455 | Returns the new state, | 5468 | Returns the new state, |
5456 | or @id{NULL} if there is a @x{memory allocation error}. | 5469 | or @id{NULL} if there is a @x{memory allocation error}. |
@@ -5488,7 +5501,7 @@ lua_Integer luaL_optinteger (lua_State *L, | |||
5488 | @apii{0,0,v} | 5501 | @apii{0,0,v} |
5489 | 5502 | ||
5490 | If the function argument @id{arg} is an integer | 5503 | If the function argument @id{arg} is an integer |
5491 | (or convertible to an integer), | 5504 | (or it is convertible to an integer), |
5492 | returns this integer. | 5505 | returns this integer. |
5493 | If this argument is absent or is @nil, | 5506 | If this argument is absent or is @nil, |
5494 | returns @id{d}. | 5507 | returns @id{d}. |
@@ -5510,7 +5523,7 @@ returns @id{d}. | |||
5510 | Otherwise, raises an error. | 5523 | Otherwise, raises an error. |
5511 | 5524 | ||
5512 | If @id{l} is not @id{NULL}, | 5525 | If @id{l} is not @id{NULL}, |
5513 | fills the position @T{*l} with the result's length. | 5526 | fills its referent with the result's length. |
5514 | If the result is @id{NULL} | 5527 | If the result is @id{NULL} |
5515 | (only possible when returning @id{d} and @T{d == NULL}), | 5528 | (only possible when returning @id{d} and @T{d == NULL}), |
5516 | its length is considered zero. | 5529 | its length is considered zero. |
@@ -5524,7 +5537,7 @@ so all conversions and caveats of that function apply here. | |||
5524 | @apii{0,0,v} | 5537 | @apii{0,0,v} |
5525 | 5538 | ||
5526 | If the function argument @id{arg} is a number, | 5539 | If the function argument @id{arg} is a number, |
5527 | returns this number. | 5540 | returns this number as a @id{lua_Number}. |
5528 | If this argument is absent or is @nil, | 5541 | If this argument is absent or is @nil, |
5529 | returns @id{d}. | 5542 | returns @id{d}. |
5530 | Otherwise, raises an error. | 5543 | Otherwise, raises an error. |
@@ -5588,11 +5601,11 @@ in the table at index @id{t}, | |||
5588 | for the object on the top of the stack (and pops the object). | 5601 | for the object on the top of the stack (and pops the object). |
5589 | 5602 | ||
5590 | A reference is a unique integer key. | 5603 | A reference is a unique integer key. |
5591 | As long as you do not manually add integer keys into table @id{t}, | 5604 | As long as you do not manually add integer keys into the table @id{t}, |
5592 | @Lid{luaL_ref} ensures the uniqueness of the key it returns. | 5605 | @Lid{luaL_ref} ensures the uniqueness of the key it returns. |
5593 | You can retrieve an object referred by reference @id{r} | 5606 | You can retrieve an object referred by the reference @id{r} |
5594 | by calling @T{lua_rawgeti(L, t, r)}. | 5607 | by calling @T{lua_rawgeti(L, t, r)}. |
5595 | Function @Lid{luaL_unref} frees a reference and its associated object. | 5608 | The function @Lid{luaL_unref} frees a reference. |
5596 | 5609 | ||
5597 | If the object on the top of the stack is @nil, | 5610 | If the object on the top of the stack is @nil, |
5598 | @Lid{luaL_ref} returns the constant @defid{LUA_REFNIL}. | 5611 | @Lid{luaL_ref} returns the constant @defid{LUA_REFNIL}. |
@@ -5623,12 +5636,12 @@ void luaL_requiref (lua_State *L, const char *modname, | |||
5623 | @apii{0,1,e} | 5636 | @apii{0,1,e} |
5624 | 5637 | ||
5625 | If @T{package.loaded[modname]} is not true, | 5638 | If @T{package.loaded[modname]} is not true, |
5626 | calls function @id{openf} with string @id{modname} as an argument | 5639 | calls the function @id{openf} with the string @id{modname} as an argument |
5627 | and sets the call result to @T{package.loaded[modname]}, | 5640 | and sets the call result to @T{package.loaded[modname]}, |
5628 | as if that function has been called through @Lid{require}. | 5641 | as if that function has been called through @Lid{require}. |
5629 | 5642 | ||
5630 | If @id{glb} is true, | 5643 | If @id{glb} is true, |
5631 | also stores the module into global @id{modname}. | 5644 | also stores the module into the global @id{modname}. |
5632 | 5645 | ||
5633 | Leaves a copy of the module on the stack. | 5646 | Leaves a copy of the module on the stack. |
5634 | 5647 | ||
@@ -5666,8 +5679,8 @@ typedef struct luaL_Stream { | |||
5666 | } luaL_Stream; | 5679 | } luaL_Stream; |
5667 | | | 5680 | | |
5668 | 5681 | ||
5669 | The standard representation for @x{file handles}, | 5682 | The standard representation for @x{file handles} |
5670 | which is used by the standard I/O library. | 5683 | used by the standard I/O library. |
5671 | 5684 | ||
5672 | A file handle is implemented as a full userdata, | 5685 | A file handle is implemented as a full userdata, |
5673 | with a metatable called @id{LUA_FILEHANDLE} | 5686 | with a metatable called @id{LUA_FILEHANDLE} |
@@ -5677,14 +5690,14 @@ The metatable is created by the I/O library | |||
5677 | 5690 | ||
5678 | This userdata must start with the structure @id{luaL_Stream}; | 5691 | This userdata must start with the structure @id{luaL_Stream}; |
5679 | it can contain other data after this initial structure. | 5692 | it can contain other data after this initial structure. |
5680 | Field @id{f} points to the corresponding C stream | 5693 | The field @id{f} points to the corresponding C stream |
5681 | (or it can be @id{NULL} to indicate an incompletely created handle). | 5694 | (or it can be @id{NULL} to indicate an incompletely created handle). |
5682 | Field @id{closef} points to a Lua function | 5695 | The field @id{closef} points to a Lua function |
5683 | that will be called to close the stream | 5696 | that will be called to close the stream |
5684 | when the handle is closed or collected; | 5697 | when the handle is closed or collected; |
5685 | this function receives the file handle as its sole argument and | 5698 | this function receives the file handle as its sole argument and |
5686 | must return either @true (in case of success) | 5699 | must return either @true, in case of success, |
5687 | or @nil plus an error message (in case of error). | 5700 | or @nil plus an error message, in case of error. |
5688 | Once Lua calls this field, | 5701 | Once Lua calls this field, |
5689 | it changes the field value to @id{NULL} | 5702 | it changes the field value to @id{NULL} |
5690 | to signal that the handle is closed. | 5703 | to signal that the handle is closed. |
@@ -5723,7 +5736,7 @@ void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, | |||
5723 | @apii{0,1,m} | 5736 | @apii{0,1,m} |
5724 | 5737 | ||
5725 | Creates and pushes a traceback of the stack @id{L1}. | 5738 | Creates and pushes a traceback of the stack @id{L1}. |
5726 | If @id{msg} is not @id{NULL} it is appended | 5739 | If @id{msg} is not @id{NULL}, it is appended |
5727 | at the beginning of the traceback. | 5740 | at the beginning of the traceback. |
5728 | The @id{level} parameter tells at which level | 5741 | The @id{level} parameter tells at which level |
5729 | to start the traceback. | 5742 | to start the traceback. |
@@ -5735,7 +5748,7 @@ to start the traceback. | |||
5735 | const char *tname);| | 5748 | const char *tname);| |
5736 | @apii{0,0,v} | 5749 | @apii{0,0,v} |
5737 | 5750 | ||
5738 | Raises a type error for argument @id{arg} | 5751 | Raises a type error for the argument @id{arg} |
5739 | of the @N{C function} that called it, | 5752 | of the @N{C function} that called it, |
5740 | using a standard message; | 5753 | using a standard message; |
5741 | @id{tname} is a @Q{name} for the expected type. | 5754 | @id{tname} is a @Q{name} for the expected type. |
@@ -5753,7 +5766,7 @@ Returns the name of the type of the value at the given index. | |||
5753 | @APIEntry{void luaL_unref (lua_State *L, int t, int ref);| | 5766 | @APIEntry{void luaL_unref (lua_State *L, int t, int ref);| |
5754 | @apii{0,0,-} | 5767 | @apii{0,0,-} |
5755 | 5768 | ||
5756 | Releases reference @id{ref} from the table at index @id{t} | 5769 | Releases the reference @id{ref} from the table at index @id{t} |
5757 | @seeC{luaL_ref}. | 5770 | @seeC{luaL_ref}. |
5758 | The entry is removed from the table, | 5771 | The entry is removed from the table, |
5759 | so that the referred object can be collected. | 5772 | so that the referred object can be collected. |
@@ -5787,15 +5800,15 @@ This function is used to build a prefix for error messages. | |||
5787 | 5800 | ||
5788 | 5801 | ||
5789 | @C{-------------------------------------------------------------------------} | 5802 | @C{-------------------------------------------------------------------------} |
5790 | @sect1{libraries| @title{Standard Libraries} | 5803 | @sect1{libraries| @title{The Standard Libraries} |
5791 | 5804 | ||
5792 | The standard Lua libraries provide useful functions | 5805 | The standard Lua libraries provide useful functions |
5793 | that are implemented directly through the @N{C API}. | 5806 | that are implemented @N{in C} through the @N{C API}. |
5794 | Some of these functions provide essential services to the language | 5807 | Some of these functions provide essential services to the language |
5795 | (e.g., @Lid{type} and @Lid{getmetatable}); | 5808 | (e.g., @Lid{type} and @Lid{getmetatable}); |
5796 | others provide access to @Q{outside} services (e.g., I/O); | 5809 | others provide access to outside services (e.g., I/O); |
5797 | and others could be implemented in Lua itself, | 5810 | and others could be implemented in Lua itself, |
5798 | but are quite useful or have critical performance requirements that | 5811 | but that for different reasons |
5799 | deserve an implementation in C (e.g., @Lid{table.sort}). | 5812 | deserve an implementation in C (e.g., @Lid{table.sort}). |
5800 | 5813 | ||
5801 | All libraries are implemented through the official @N{C API} | 5814 | All libraries are implemented through the official @N{C API} |
@@ -5844,7 +5857,7 @@ the host program can open them individually by using | |||
5844 | @defid{luaopen_package} (for the package library), | 5857 | @defid{luaopen_package} (for the package library), |
5845 | @defid{luaopen_coroutine} (for the coroutine library), | 5858 | @defid{luaopen_coroutine} (for the coroutine library), |
5846 | @defid{luaopen_string} (for the string library), | 5859 | @defid{luaopen_string} (for the string library), |
5847 | @defid{luaopen_utf8} (for the UTF8 library), | 5860 | @defid{luaopen_utf8} (for the UTF-8 library), |
5848 | @defid{luaopen_table} (for the table library), | 5861 | @defid{luaopen_table} (for the table library), |
5849 | @defid{luaopen_math} (for the mathematical library), | 5862 | @defid{luaopen_math} (for the mathematical library), |
5850 | @defid{luaopen_io} (for the I/O library), | 5863 | @defid{luaopen_io} (for the I/O library), |
@@ -5896,8 +5909,7 @@ restarts automatic execution of the garbage collector. | |||
5896 | returns the total memory in use by Lua in Kbytes. | 5909 | returns the total memory in use by Lua in Kbytes. |
5897 | The value has a fractional part, | 5910 | The value has a fractional part, |
5898 | so that it multiplied by 1024 | 5911 | so that it multiplied by 1024 |
5899 | gives the exact number of bytes in use by Lua | 5912 | gives the exact number of bytes in use by Lua. |
5900 | (except for overflows). | ||
5901 | } | 5913 | } |
5902 | 5914 | ||
5903 | @item{@St{step}| | 5915 | @item{@St{step}| |
@@ -5938,6 +5950,8 @@ returns a boolean that tells whether the collector is running | |||
5938 | } | 5950 | } |
5939 | 5951 | ||
5940 | } | 5952 | } |
5953 | See @See{GC} for more details about garbage collection | ||
5954 | and some of these options. | ||
5941 | 5955 | ||
5942 | } | 5956 | } |
5943 | 5957 | ||
@@ -5947,14 +5961,15 @@ When called without arguments, | |||
5947 | @id{dofile} executes the contents of the standard input (@id{stdin}). | 5961 | @id{dofile} executes the contents of the standard input (@id{stdin}). |
5948 | Returns all values returned by the chunk. | 5962 | Returns all values returned by the chunk. |
5949 | In case of errors, @id{dofile} propagates the error | 5963 | In case of errors, @id{dofile} propagates the error |
5950 | to its caller (that is, @id{dofile} does not run in protected mode). | 5964 | to its caller. |
5965 | (That is, @id{dofile} does not run in protected mode.) | ||
5951 | 5966 | ||
5952 | } | 5967 | } |
5953 | 5968 | ||
5954 | @LibEntry{error (message [, level])| | 5969 | @LibEntry{error (message [, level])| |
5955 | Terminates the last protected function called | 5970 | Terminates the last protected function called |
5956 | and returns @id{message} as the error object. | 5971 | and returns @id{message} as the error object. |
5957 | Function @id{error} never returns. | 5972 | This function never returns. |
5958 | 5973 | ||
5959 | Usually, @id{error} adds some information about the error position | 5974 | Usually, @id{error} adds some information about the error position |
5960 | at the beginning of the message, if the message is a string. | 5975 | at the beginning of the message, if the message is a string. |
@@ -6066,7 +6081,7 @@ if no file name is given. | |||
6066 | Allows a program to traverse all fields of a table. | 6081 | Allows a program to traverse all fields of a table. |
6067 | Its first argument is a table and its second argument | 6082 | Its first argument is a table and its second argument |
6068 | is an index in this table. | 6083 | is an index in this table. |
6069 | @id{next} returns the next index of the table | 6084 | A call to @id{next} returns the next index of the table |
6070 | and its associated value. | 6085 | and its associated value. |
6071 | When called with @nil as its second argument, | 6086 | When called with @nil as its second argument, |
6072 | @id{next} returns an initial index | 6087 | @id{next} returns an initial index |
@@ -6112,7 +6127,7 @@ the table during its traversal. | |||
6112 | 6127 | ||
6113 | @LibEntry{pcall (f [, arg1, @Cdots])| | 6128 | @LibEntry{pcall (f [, arg1, @Cdots])| |
6114 | 6129 | ||
6115 | Calls function @id{f} with | 6130 | Calls the function @id{f} with |
6116 | the given arguments in @def{protected mode}. | 6131 | the given arguments in @def{protected mode}. |
6117 | This means that any error @N{inside @T{f}} is not propagated; | 6132 | This means that any error @N{inside @T{f}} is not propagated; |
6118 | instead, @id{pcall} catches the error | 6133 | instead, @id{pcall} catches the error |
@@ -6121,7 +6136,7 @@ Its first result is the status code (a boolean), | |||
6121 | which is true if the call succeeds without errors. | 6136 | which is true if the call succeeds without errors. |
6122 | In such case, @id{pcall} also returns all results from the call, | 6137 | In such case, @id{pcall} also returns all results from the call, |
6123 | after this first result. | 6138 | after this first result. |
6124 | In case of any error, @id{pcall} returns @false plus the error message. | 6139 | In case of any error, @id{pcall} returns @false plus the error object. |
6125 | 6140 | ||
6126 | } | 6141 | } |
6127 | 6142 | ||
@@ -6184,8 +6199,6 @@ and @id{select} returns the total number of extra arguments it received. | |||
6184 | @LibEntry{setmetatable (table, metatable)| | 6199 | @LibEntry{setmetatable (table, metatable)| |
6185 | 6200 | ||
6186 | Sets the metatable for the given table. | 6201 | Sets the metatable for the given table. |
6187 | (To change the metatable of other types from Lua code, | ||
6188 | you must use the @link{debuglib|debug library}.) | ||
6189 | If @id{metatable} is @nil, | 6202 | If @id{metatable} is @nil, |
6190 | removes the metatable of the given table. | 6203 | removes the metatable of the given table. |
6191 | If the original metatable has a @idx{__metatable} field, | 6204 | If the original metatable has a @idx{__metatable} field, |
@@ -6193,6 +6206,9 @@ raises an error. | |||
6193 | 6206 | ||
6194 | This function returns @id{table}. | 6207 | This function returns @id{table}. |
6195 | 6208 | ||
6209 | To change the metatable of other types from Lua code, | ||
6210 | you must use the @link{debuglib|debug library}. | ||
6211 | |||
6196 | } | 6212 | } |
6197 | 6213 | ||
6198 | @LibEntry{tonumber (e [, base])| | 6214 | @LibEntry{tonumber (e [, base])| |
@@ -6206,7 +6222,7 @@ otherwise, it returns @nil. | |||
6206 | 6222 | ||
6207 | The conversion of strings can result in integers or floats, | 6223 | The conversion of strings can result in integers or floats, |
6208 | according to the lexical conventions of Lua @see{lexical}. | 6224 | according to the lexical conventions of Lua @see{lexical}. |
6209 | (The string may have leading and trailing spaces and a sign.) | 6225 | The string may have leading and trailing spaces and a sign. |
6210 | 6226 | ||
6211 | When called with @id{base}, | 6227 | When called with @id{base}, |
6212 | then @id{e} must be a string to be interpreted as | 6228 | then @id{e} must be a string to be interpreted as |
@@ -6298,7 +6314,7 @@ it is not inside a non-yieldable @N{C function}. | |||
6298 | 6314 | ||
6299 | } | 6315 | } |
6300 | 6316 | ||
6301 | @LibEntry{coroutine.kill(co)| | 6317 | @LibEntry{coroutine.kill (co)| |
6302 | 6318 | ||
6303 | Kills coroutine @id{co}, | 6319 | Kills coroutine @id{co}, |
6304 | closing all its pending to-be-closed variables | 6320 | closing all its pending to-be-closed variables |
@@ -6339,7 +6355,7 @@ true when the running coroutine is the main one. | |||
6339 | 6355 | ||
6340 | @LibEntry{coroutine.status (co)| | 6356 | @LibEntry{coroutine.status (co)| |
6341 | 6357 | ||
6342 | Returns the status of coroutine @id{co}, as a string: | 6358 | Returns the status of the coroutine @id{co}, as a string: |
6343 | @T{"running"}, | 6359 | @T{"running"}, |
6344 | if the coroutine is running (that is, it called @id{status}); | 6360 | if the coroutine is running (that is, it called @id{status}); |
6345 | @T{"suspended"}, if the coroutine is suspended in a call to @id{yield}, | 6361 | @T{"suspended"}, if the coroutine is suspended in a call to @id{yield}, |
@@ -6353,7 +6369,7 @@ or if it has stopped with an error. | |||
6353 | 6369 | ||
6354 | @LibEntry{coroutine.wrap (f)| | 6370 | @LibEntry{coroutine.wrap (f)| |
6355 | 6371 | ||
6356 | Creates a new coroutine, with body @id{f}. | 6372 | Creates a new coroutine, with body @id{f}; |
6357 | @id{f} must be a function. | 6373 | @id{f} must be a function. |
6358 | Returns a function that resumes the coroutine each time it is called. | 6374 | Returns a function that resumes the coroutine each time it is called. |
6359 | Any arguments passed to the function behave as the | 6375 | Any arguments passed to the function behave as the |
@@ -6379,7 +6395,7 @@ The package library provides basic | |||
6379 | facilities for loading modules in Lua. | 6395 | facilities for loading modules in Lua. |
6380 | It exports one function directly in the global environment: | 6396 | It exports one function directly in the global environment: |
6381 | @Lid{require}. | 6397 | @Lid{require}. |
6382 | Everything else is exported in a table @defid{package}. | 6398 | Everything else is exported in the table @defid{package}. |
6383 | 6399 | ||
6384 | 6400 | ||
6385 | @LibEntry{require (modname)| | 6401 | @LibEntry{require (modname)| |
@@ -6729,7 +6745,8 @@ after the two indices. | |||
6729 | @LibEntry{string.format (formatstring, @Cdots)| | 6745 | @LibEntry{string.format (formatstring, @Cdots)| |
6730 | 6746 | ||
6731 | Returns a formatted version of its variable number of arguments | 6747 | Returns a formatted version of its variable number of arguments |
6732 | following the description given in its first argument (which must be a string). | 6748 | following the description given in its first argument, |
6749 | which must be a string. | ||
6733 | The format string follows the same rules as the @ANSI{sprintf}. | 6750 | The format string follows the same rules as the @ANSI{sprintf}. |
6734 | The only differences are that the conversion specifiers and modifiers | 6751 | The only differences are that the conversion specifiers and modifiers |
6735 | @T{*}, @id{h}, @id{L}, @id{l}, and @id{n} are not supported | 6752 | @T{*}, @id{h}, @id{L}, @id{l}, and @id{n} are not supported |
@@ -6830,9 +6847,9 @@ If @id{repl} is a string, then its value is used for replacement. | |||
6830 | The @N{character @T{%}} works as an escape character: | 6847 | The @N{character @T{%}} works as an escape character: |
6831 | any sequence in @id{repl} of the form @T{%@rep{d}}, | 6848 | any sequence in @id{repl} of the form @T{%@rep{d}}, |
6832 | with @rep{d} between 1 and 9, | 6849 | with @rep{d} between 1 and 9, |
6833 | stands for the value of the @rep{d}-th captured substring. | 6850 | stands for the value of the @rep{d}-th captured substring; |
6834 | The sequence @T{%0} stands for the whole match. | 6851 | the sequence @T{%0} stands for the whole match; |
6835 | The sequence @T{%%} stands for a @N{single @T{%}}. | 6852 | the sequence @T{%%} stands for a @N{single @T{%}}. |
6836 | 6853 | ||
6837 | If @id{repl} is a table, then the table is queried for every match, | 6854 | If @id{repl} is a table, then the table is queried for every match, |
6838 | using the first capture as the key. | 6855 | using the first capture as the key. |
@@ -6899,7 +6916,7 @@ The definition of what an uppercase letter is depends on the current locale. | |||
6899 | @LibEntry{string.match (s, pattern [, init])| | 6916 | @LibEntry{string.match (s, pattern [, init])| |
6900 | 6917 | ||
6901 | Looks for the first @emph{match} of | 6918 | Looks for the first @emph{match} of |
6902 | @id{pattern} @see{pm} in the string @id{s}. | 6919 | the @id{pattern} @see{pm} in the string @id{s}. |
6903 | If it finds one, then @id{match} returns | 6920 | If it finds one, then @id{match} returns |
6904 | the captures from the pattern; | 6921 | the captures from the pattern; |
6905 | otherwise it returns @nil. | 6922 | otherwise it returns @nil. |
@@ -6914,7 +6931,7 @@ its default value @N{is 1} and can be negative. | |||
6914 | @LibEntry{string.pack (fmt, v1, v2, @Cdots)| | 6931 | @LibEntry{string.pack (fmt, v1, v2, @Cdots)| |
6915 | 6932 | ||
6916 | Returns a binary string containing the values @id{v1}, @id{v2}, etc. | 6933 | Returns a binary string containing the values @id{v1}, @id{v2}, etc. |
6917 | packed (that is, serialized in binary form) | 6934 | serialized in binary form (packed) |
6918 | according to the format string @id{fmt} @see{pack}. | 6935 | according to the format string @id{fmt} @see{pack}. |
6919 | 6936 | ||
6920 | } | 6937 | } |
@@ -7042,8 +7059,7 @@ represents the character @rep{x}. | |||
7042 | This is the standard way to escape the magic characters. | 7059 | This is the standard way to escape the magic characters. |
7043 | Any non-alphanumeric character | 7060 | Any non-alphanumeric character |
7044 | (including all punctuation characters, even the non-magical) | 7061 | (including all punctuation characters, even the non-magical) |
7045 | can be preceded by a @Char{%} | 7062 | can be preceded by a @Char{%} to represent itself in a pattern. |
7046 | when used to represent itself in a pattern. | ||
7047 | } | 7063 | } |
7048 | 7064 | ||
7049 | @item{@T{[@rep{set}]}| | 7065 | @item{@T{[@rep{set}]}| |
@@ -7099,19 +7115,19 @@ which matches any single character in the class; | |||
7099 | 7115 | ||
7100 | @item{ | 7116 | @item{ |
7101 | a single character class followed by @Char{*}, | 7117 | a single character class followed by @Char{*}, |
7102 | which matches zero or more repetitions of characters in the class. | 7118 | which matches sequences of zero or more characters in the class. |
7103 | These repetition items will always match the longest possible sequence; | 7119 | These repetition items will always match the longest possible sequence; |
7104 | } | 7120 | } |
7105 | 7121 | ||
7106 | @item{ | 7122 | @item{ |
7107 | a single character class followed by @Char{+}, | 7123 | a single character class followed by @Char{+}, |
7108 | which matches one or more repetitions of characters in the class. | 7124 | which matches sequences of one or more characters in the class. |
7109 | These repetition items will always match the longest possible sequence; | 7125 | These repetition items will always match the longest possible sequence; |
7110 | } | 7126 | } |
7111 | 7127 | ||
7112 | @item{ | 7128 | @item{ |
7113 | a single character class followed by @Char{-}, | 7129 | a single character class followed by @Char{-}, |
7114 | which also matches zero or more repetitions of characters in the class. | 7130 | which also matches sequences of zero or more characters in the class. |
7115 | Unlike @Char{*}, | 7131 | Unlike @Char{*}, |
7116 | these repetition items will always match the shortest possible sequence; | 7132 | these repetition items will always match the shortest possible sequence; |
7117 | } | 7133 | } |
@@ -7172,7 +7188,7 @@ that match captures are stored (@emph{captured}) for future use. | |||
7172 | Captures are numbered according to their left parentheses. | 7188 | Captures are numbered according to their left parentheses. |
7173 | For instance, in the pattern @T{"(a*(.)%w(%s*))"}, | 7189 | For instance, in the pattern @T{"(a*(.)%w(%s*))"}, |
7174 | the part of the string matching @T{"a*(.)%w(%s*)"} is | 7190 | the part of the string matching @T{"a*(.)%w(%s*)"} is |
7175 | stored as the first capture (and therefore has @N{number 1}); | 7191 | stored as the first capture, and therefore has @N{number 1}; |
7176 | the character matching @St{.} is captured with @N{number 2}, | 7192 | the character matching @St{.} is captured with @N{number 2}, |
7177 | and the part matching @St{%s*} has @N{number 3}. | 7193 | and the part matching @St{%s*} has @N{number 3}. |
7178 | 7194 | ||
@@ -7188,7 +7204,7 @@ The function @Lid{string.gsub} and the iterator @Lid{string.gmatch} | |||
7188 | match multiple occurrences of the given pattern in the subject. | 7204 | match multiple occurrences of the given pattern in the subject. |
7189 | For these functions, | 7205 | For these functions, |
7190 | a new match is considered valid only | 7206 | a new match is considered valid only |
7191 | if it ends at least one byte after the previous match. | 7207 | if it ends at least one byte after the end of the previous match. |
7192 | In other words, the pattern machine never accepts the | 7208 | In other words, the pattern machine never accepts the |
7193 | empty string as a match immediately after another match. | 7209 | empty string as a match immediately after another match. |
7194 | As an example, | 7210 | As an example, |
@@ -7253,14 +7269,16 @@ according to option @id{op} | |||
7253 | (A @St{[@rep{n}]} means an optional integral numeral.) | 7269 | (A @St{[@rep{n}]} means an optional integral numeral.) |
7254 | Except for padding, spaces, and configurations | 7270 | Except for padding, spaces, and configurations |
7255 | (options @St{xX <=>!}), | 7271 | (options @St{xX <=>!}), |
7256 | each option corresponds to an argument (in @Lid{string.pack}) | 7272 | each option corresponds to an argument in @Lid{string.pack} |
7257 | or a result (in @Lid{string.unpack}). | 7273 | or a result in @Lid{string.unpack}. |
7258 | 7274 | ||
7259 | For options @St{!@rep{n}}, @St{s@rep{n}}, @St{i@rep{n}}, and @St{I@rep{n}}, | 7275 | For options @St{!@rep{n}}, @St{s@rep{n}}, @St{i@rep{n}}, and @St{I@rep{n}}, |
7260 | @id{n} can be any integer between 1 and 16. | 7276 | @id{n} can be any integer between 1 and 16. |
7261 | All integral options check overflows; | 7277 | All integral options check overflows; |
7262 | @Lid{string.pack} checks whether the given value fits in the given size; | 7278 | @Lid{string.pack} checks whether the given value fits in the given size; |
7263 | @Lid{string.unpack} checks whether the read value fits in a Lua integer. | 7279 | @Lid{string.unpack} checks whether the read value fits in a Lua integer. |
7280 | For the unsigned options, | ||
7281 | Lua integers are treated as unsigned values too. | ||
7264 | 7282 | ||
7265 | Any format string starts as if prefixed by @St{!1=}, | 7283 | Any format string starts as if prefixed by @St{!1=}, |
7266 | that is, | 7284 | that is, |
@@ -7283,7 +7301,7 @@ option @St{s} follows the alignment of its starting integer. | |||
7283 | 7301 | ||
7284 | 7302 | ||
7285 | All padding is filled with zeros by @Lid{string.pack} | 7303 | All padding is filled with zeros by @Lid{string.pack} |
7286 | (and ignored by @Lid{string.unpack}). | 7304 | and ignored by @Lid{string.unpack}. |
7287 | 7305 | ||
7288 | } | 7306 | } |
7289 | 7307 | ||
@@ -7344,7 +7362,7 @@ Returns values so that the construction | |||
7344 | @verbatim{ | 7362 | @verbatim{ |
7345 | for p, c in utf8.codes(s) do @rep{body} end | 7363 | for p, c in utf8.codes(s) do @rep{body} end |
7346 | } | 7364 | } |
7347 | will iterate over all characters in string @id{s}, | 7365 | will iterate over all UTF-8 characters in string @id{s}, |
7348 | with @id{p} being the position (in bytes) and @id{c} the code point | 7366 | with @id{p} being the position (in bytes) and @id{c} the code point |
7349 | of each character. | 7367 | of each character. |
7350 | It raises an error if it meets any invalid byte sequence. | 7368 | It raises an error if it meets any invalid byte sequence. |
@@ -7353,7 +7371,7 @@ It raises an error if it meets any invalid byte sequence. | |||
7353 | 7371 | ||
7354 | @LibEntry{utf8.codepoint (s [, i [, j [, lax]]])| | 7372 | @LibEntry{utf8.codepoint (s [, i [, j [, lax]]])| |
7355 | 7373 | ||
7356 | Returns the codepoints (as integers) from all characters in @id{s} | 7374 | Returns the code points (as integers) from all characters in @id{s} |
7357 | that start between byte position @id{i} and @id{j} (both included). | 7375 | that start between byte position @id{i} and @id{j} (both included). |
7358 | The default for @id{i} is 1 and for @id{j} is @id{i}. | 7376 | The default for @id{i} is 1 and for @id{j} is @id{i}. |
7359 | It raises an error if it meets any invalid byte sequence. | 7377 | It raises an error if it meets any invalid byte sequence. |
@@ -7423,13 +7441,13 @@ shifting up the elements | |||
7423 | @T{list[pos], list[pos+1], @Cdots, list[#list]}. | 7441 | @T{list[pos], list[pos+1], @Cdots, list[#list]}. |
7424 | The default value for @id{pos} is @T{#list+1}, | 7442 | The default value for @id{pos} is @T{#list+1}, |
7425 | so that a call @T{table.insert(t,x)} inserts @id{x} at the end | 7443 | so that a call @T{table.insert(t,x)} inserts @id{x} at the end |
7426 | of list @id{t}. | 7444 | of the list @id{t}. |
7427 | 7445 | ||
7428 | } | 7446 | } |
7429 | 7447 | ||
7430 | @LibEntry{table.move (a1, f, e, t [,a2])| | 7448 | @LibEntry{table.move (a1, f, e, t [,a2])| |
7431 | 7449 | ||
7432 | Moves elements from table @id{a1} to table @id{a2}, | 7450 | Moves elements from the table @id{a1} to the table @id{a2}, |
7433 | performing the equivalent to the following | 7451 | performing the equivalent to the following |
7434 | multiple assignment: | 7452 | multiple assignment: |
7435 | @T{a2[t],@Cdots = a1[f],@Cdots,a1[e]}. | 7453 | @T{a2[t],@Cdots = a1[f],@Cdots,a1[e]}. |
@@ -7463,13 +7481,13 @@ or @T{#list + 1}. | |||
7463 | 7481 | ||
7464 | The default value for @id{pos} is @T{#list}, | 7482 | The default value for @id{pos} is @T{#list}, |
7465 | so that a call @T{table.remove(l)} removes the last element | 7483 | so that a call @T{table.remove(l)} removes the last element |
7466 | of list @id{l}. | 7484 | of the list @id{l}. |
7467 | 7485 | ||
7468 | } | 7486 | } |
7469 | 7487 | ||
7470 | @LibEntry{table.sort (list [, comp])| | 7488 | @LibEntry{table.sort (list [, comp])| |
7471 | 7489 | ||
7472 | Sorts list elements in a given order, @emph{in-place}, | 7490 | Sorts the list elements in a given order, @emph{in-place}, |
7473 | from @T{list[1]} to @T{list[#list]}. | 7491 | from @T{list[1]} to @T{list[#list]}. |
7474 | If @id{comp} is given, | 7492 | If @id{comp} is given, |
7475 | then it must be a function that receives two list elements | 7493 | then it must be a function that receives two list elements |
@@ -7511,8 +7529,8 @@ It provides all its functions and constants inside the table @defid{math}. | |||
7511 | Functions with the annotation @St{integer/float} give | 7529 | Functions with the annotation @St{integer/float} give |
7512 | integer results for integer arguments | 7530 | integer results for integer arguments |
7513 | and float results for float (or mixed) arguments. | 7531 | and float results for float (or mixed) arguments. |
7514 | Rounding functions | 7532 | the rounding functions |
7515 | (@Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf}) | 7533 | @Lid{math.ceil}, @Lid{math.floor}, and @Lid{math.modf} |
7516 | return an integer when the result fits in the range of an integer, | 7534 | return an integer when the result fits in the range of an integer, |
7517 | or a float otherwise. | 7535 | or a float otherwise. |
7518 | 7536 | ||
@@ -7540,7 +7558,7 @@ Returns the arc sine of @id{x} (in radians). | |||
7540 | Returns the arc tangent of @T{y/x} (in radians), | 7558 | Returns the arc tangent of @T{y/x} (in radians), |
7541 | but uses the signs of both arguments to find the | 7559 | but uses the signs of both arguments to find the |
7542 | quadrant of the result. | 7560 | quadrant of the result. |
7543 | (It also handles correctly the case of @id{x} being zero.) | 7561 | It also handles correctly the case of @id{x} being zero. |
7544 | 7562 | ||
7545 | The default value for @id{x} is 1, | 7563 | The default value for @id{x} is 1, |
7546 | so that the call @T{math.atan(y)} | 7564 | so that the call @T{math.atan(y)} |
@@ -7604,7 +7622,7 @@ The default for @id{base} is @M{e} | |||
7604 | @LibEntry{math.max (x, @Cdots)| | 7622 | @LibEntry{math.max (x, @Cdots)| |
7605 | 7623 | ||
7606 | Returns the argument with the maximum value, | 7624 | Returns the argument with the maximum value, |
7607 | according to the Lua operator @T{<}. (integer/float) | 7625 | according to the Lua operator @T{<}. |
7608 | 7626 | ||
7609 | } | 7627 | } |
7610 | 7628 | ||
@@ -7616,7 +7634,7 @@ An integer with the maximum value for an integer. | |||
7616 | @LibEntry{math.min (x, @Cdots)| | 7634 | @LibEntry{math.min (x, @Cdots)| |
7617 | 7635 | ||
7618 | Returns the argument with the minimum value, | 7636 | Returns the argument with the minimum value, |
7619 | according to the Lua operator @T{<}. (integer/float) | 7637 | according to the Lua operator @T{<}. |
7620 | 7638 | ||
7621 | } | 7639 | } |
7622 | 7640 | ||
@@ -7674,7 +7692,7 @@ some number of previous results.) | |||
7674 | 7692 | ||
7675 | When called with at least one argument, | 7693 | When called with at least one argument, |
7676 | the integer parameters @id{x} and @id{y} are | 7694 | the integer parameters @id{x} and @id{y} are |
7677 | concatenated into a 128-bit @emphx{seed} that | 7695 | joined into a 128-bit @emphx{seed} that |
7678 | is used to reinitialize the pseudo-random generator; | 7696 | is used to reinitialize the pseudo-random generator; |
7679 | equal seeds produce equal sequences of numbers. | 7697 | equal seeds produce equal sequences of numbers. |
7680 | The default for @id{y} is zero. | 7698 | The default for @id{y} is zero. |
@@ -7741,7 +7759,7 @@ The I/O library provides two different styles for file manipulation. | |||
7741 | The first one uses implicit file handles; | 7759 | The first one uses implicit file handles; |
7742 | that is, there are operations to set a default input file and a | 7760 | that is, there are operations to set a default input file and a |
7743 | default output file, | 7761 | default output file, |
7744 | and all input/output operations are over these default files. | 7762 | and all input/output operations are done over these default files. |
7745 | The second style uses explicit file handles. | 7763 | The second style uses explicit file handles. |
7746 | 7764 | ||
7747 | When using implicit file handles, | 7765 | When using implicit file handles, |
@@ -7750,18 +7768,19 @@ When using explicit file handles, | |||
7750 | the operation @Lid{io.open} returns a file handle | 7768 | the operation @Lid{io.open} returns a file handle |
7751 | and then all operations are supplied as methods of the file handle. | 7769 | and then all operations are supplied as methods of the file handle. |
7752 | 7770 | ||
7771 | The metatable for file handles provides metamethods | ||
7772 | for @idx{__gc} and @idx{__close} that try | ||
7773 | to close the file when called. | ||
7774 | |||
7753 | The table @id{io} also provides | 7775 | The table @id{io} also provides |
7754 | three predefined file handles with their usual meanings from C: | 7776 | three predefined file handles with their usual meanings from C: |
7755 | @defid{io.stdin}, @defid{io.stdout}, and @defid{io.stderr}. | 7777 | @defid{io.stdin}, @defid{io.stdout}, and @defid{io.stderr}. |
7756 | The I/O library never closes these files. | 7778 | The I/O library never closes these files. |
7757 | The metatable for file handles provides metamethods | ||
7758 | for @idx{__gc} and @idx{__close} that try | ||
7759 | to close the file when called. | ||
7760 | 7779 | ||
7761 | Unless otherwise stated, | 7780 | Unless otherwise stated, |
7762 | all I/O functions return @nil on failure | 7781 | all I/O functions return @nil on failure, |
7763 | (plus an error message as a second result and | 7782 | plus an error message as a second result and |
7764 | a system-dependent error code as a third result) | 7783 | a system-dependent error code as a third result, |
7765 | and some value different from @nil on success. | 7784 | and some value different from @nil on success. |
7766 | On non-POSIX systems, | 7785 | On non-POSIX systems, |
7767 | the computation of the error message and error code | 7786 | the computation of the error message and error code |
@@ -7854,7 +7873,7 @@ Similar to @Lid{io.input}, but operates over the default output file. | |||
7854 | This function is system dependent and is not available | 7873 | This function is system dependent and is not available |
7855 | on all platforms. | 7874 | on all platforms. |
7856 | 7875 | ||
7857 | Starts program @id{prog} in a separated process and returns | 7876 | Starts the program @id{prog} in a separated process and returns |
7858 | a file handle that you can use to read data from this program | 7877 | a file handle that you can use to read data from this program |
7859 | (if @id{mode} is @T{"r"}, the default) | 7878 | (if @id{mode} is @T{"r"}, the default) |
7860 | or to write data to this program | 7879 | or to write data to this program |
@@ -8097,10 +8116,9 @@ If @id{format} is not @St{*t}, | |||
8097 | then @id{date} returns the date as a string, | 8116 | then @id{date} returns the date as a string, |
8098 | formatted according to the same rules as the @ANSI{strftime}. | 8117 | formatted according to the same rules as the @ANSI{strftime}. |
8099 | 8118 | ||
8100 | When called without arguments, | 8119 | If @id{format} is absent, it defaults to @St{%c}, |
8101 | @id{date} returns a reasonable date and time representation that depends on | 8120 | which gives a reasonable date and time representation |
8102 | the host system and on the current locale. | 8121 | using the current locale. |
8103 | (More specifically, @T{os.date()} is equivalent to @T{os.date("%c")}.) | ||
8104 | 8122 | ||
8105 | On non-POSIX systems, | 8123 | On non-POSIX systems, |
8106 | this function may be not @x{thread safe} | 8124 | this function may be not @x{thread safe} |
@@ -8314,8 +8332,8 @@ within any function and so have no direct access to local variables. | |||
8314 | 8332 | ||
8315 | Returns the current hook settings of the thread, as three values: | 8333 | Returns the current hook settings of the thread, as three values: |
8316 | the current hook function, the current hook mask, | 8334 | the current hook function, the current hook mask, |
8317 | and the current hook count | 8335 | and the current hook count, |
8318 | (as set by the @Lid{debug.sethook} function). | 8336 | as set by the @Lid{debug.sethook} function. |
8319 | 8337 | ||
8320 | } | 8338 | } |
8321 | 8339 | ||
@@ -8359,7 +8377,7 @@ about the @Lid{print} function. | |||
8359 | This function returns the name and the value of the local variable | 8377 | This function returns the name and the value of the local variable |
8360 | with index @id{local} of the function at level @id{f} of the stack. | 8378 | with index @id{local} of the function at level @id{f} of the stack. |
8361 | This function accesses not only explicit local variables, | 8379 | This function accesses not only explicit local variables, |
8362 | but also parameters, temporaries, etc. | 8380 | but also parameters and temporary values. |
8363 | 8381 | ||
8364 | The first parameter or local variable has @N{index 1}, and so on, | 8382 | The first parameter or local variable has @N{index 1}, and so on, |
8365 | following the order that they are declared in the code, | 8383 | following the order that they are declared in the code, |
@@ -8416,7 +8434,7 @@ to the userdata @id{u} plus a boolean, | |||
8416 | 8434 | ||
8417 | @LibEntry{debug.sethook ([thread,] hook, mask [, count])| | 8435 | @LibEntry{debug.sethook ([thread,] hook, mask [, count])| |
8418 | 8436 | ||
8419 | Sets the given function as a hook. | 8437 | Sets the given function as the debug hook. |
8420 | The string @id{mask} and the number @id{count} describe | 8438 | The string @id{mask} and the number @id{count} describe |
8421 | when the hook will be called. | 8439 | when the hook will be called. |
8422 | The string mask may have any combination of the following characters, | 8440 | The string mask may have any combination of the following characters, |
@@ -8435,16 +8453,15 @@ When called without arguments, | |||
8435 | 8453 | ||
8436 | When the hook is called, its first parameter is a string | 8454 | When the hook is called, its first parameter is a string |
8437 | describing the event that has triggered its call: | 8455 | describing the event that has triggered its call: |
8438 | @T{"call"} (or @T{"tail call"}), | 8456 | @T{"call"}, @T{"tail call"}, @T{"return"}, |
8439 | @T{"return"}, | ||
8440 | @T{"line"}, and @T{"count"}. | 8457 | @T{"line"}, and @T{"count"}. |
8441 | For line events, | 8458 | For line events, |
8442 | the hook also gets the new line number as its second parameter. | 8459 | the hook also gets the new line number as its second parameter. |
8443 | Inside a hook, | 8460 | Inside a hook, |
8444 | you can call @id{getinfo} with @N{level 2} to get more information about | 8461 | you can call @id{getinfo} with @N{level 2} to get more information about |
8445 | the running function | 8462 | the running function. |
8446 | (@N{level 0} is the @id{getinfo} function, | 8463 | (@N{Level 0} is the @id{getinfo} function, |
8447 | and @N{level 1} is the hook function). | 8464 | and @N{level 1} is the hook function.) |
8448 | 8465 | ||
8449 | } | 8466 | } |
8450 | 8467 | ||
@@ -8542,7 +8559,7 @@ An interpreter for Lua as a standalone language, | |||
8542 | called simply @id{lua}, | 8559 | called simply @id{lua}, |
8543 | is provided with the standard distribution. | 8560 | is provided with the standard distribution. |
8544 | The @x{standalone interpreter} includes | 8561 | The @x{standalone interpreter} includes |
8545 | all standard libraries, including the debug library. | 8562 | all standard libraries. |
8546 | Its usage is: | 8563 | Its usage is: |
8547 | @verbatim{ | 8564 | @verbatim{ |
8548 | lua [options] [script [args]] | 8565 | lua [options] [script [args]] |
@@ -8564,7 +8581,7 @@ When called without arguments, | |||
8564 | when the standard input (@id{stdin}) is a terminal, | 8581 | when the standard input (@id{stdin}) is a terminal, |
8565 | and as @T{lua -} otherwise. | 8582 | and as @T{lua -} otherwise. |
8566 | 8583 | ||
8567 | When called without option @T{-E}, | 8584 | When called without the option @T{-E}, |
8568 | the interpreter checks for an environment variable @defid{LUA_INIT_5_4} | 8585 | the interpreter checks for an environment variable @defid{LUA_INIT_5_4} |
8569 | (or @defid{LUA_INIT} if the versioned name is not defined) | 8586 | (or @defid{LUA_INIT} if the versioned name is not defined) |
8570 | before running any argument. | 8587 | before running any argument. |
@@ -8572,7 +8589,7 @@ If the variable content has the format @T{@At@rep{filename}}, | |||
8572 | then @id{lua} executes the file. | 8589 | then @id{lua} executes the file. |
8573 | Otherwise, @id{lua} executes the string itself. | 8590 | Otherwise, @id{lua} executes the string itself. |
8574 | 8591 | ||
8575 | When called with option @T{-E}, | 8592 | When called with the option @T{-E}, |
8576 | besides ignoring @id{LUA_INIT}, | 8593 | besides ignoring @id{LUA_INIT}, |
8577 | Lua also ignores | 8594 | Lua also ignores |
8578 | the values of @id{LUA_PATH} and @id{LUA_CPATH}, | 8595 | the values of @id{LUA_PATH} and @id{LUA_CPATH}, |
@@ -8619,8 +8636,8 @@ will print @St{-e}. | |||
8619 | If there is a script, | 8636 | If there is a script, |
8620 | the script is called with arguments | 8637 | the script is called with arguments |
8621 | @T{arg[1]}, @Cdots, @T{arg[#arg]}. | 8638 | @T{arg[1]}, @Cdots, @T{arg[#arg]}. |
8622 | (Like all chunks in Lua, | 8639 | Like all chunks in Lua, |
8623 | the script is compiled as a vararg function.) | 8640 | the script is compiled as a vararg function. |
8624 | 8641 | ||
8625 | In interactive mode, | 8642 | In interactive mode, |
8626 | Lua repeatedly prompts and waits for a line. | 8643 | Lua repeatedly prompts and waits for a line. |
@@ -8645,6 +8662,7 @@ has a metamethod @idx{__tostring}, | |||
8645 | the interpreter calls this metamethod to produce the final message. | 8662 | the interpreter calls this metamethod to produce the final message. |
8646 | Otherwise, the interpreter converts the error object to a string | 8663 | Otherwise, the interpreter converts the error object to a string |
8647 | and adds a stack traceback to it. | 8664 | and adds a stack traceback to it. |
8665 | Warnings are simply printed in the standard error output. | ||
8648 | 8666 | ||
8649 | When finishing normally, | 8667 | When finishing normally, |
8650 | the interpreter closes its main Lua state | 8668 | the interpreter closes its main Lua state |
@@ -8654,22 +8672,21 @@ calling @Lid{os.exit} to terminate. | |||
8654 | 8672 | ||
8655 | To allow the use of Lua as a | 8673 | To allow the use of Lua as a |
8656 | script interpreter in Unix systems, | 8674 | script interpreter in Unix systems, |
8657 | the standalone interpreter skips | 8675 | Lua skips the first line of a file chunk if it starts with @T{#}. |
8658 | the first line of a chunk if it starts with @T{#}. | ||
8659 | Therefore, Lua scripts can be made into executable programs | 8676 | Therefore, Lua scripts can be made into executable programs |
8660 | by using @T{chmod +x} and @N{the @T{#!}} form, | 8677 | by using @T{chmod +x} and @N{the @T{#!}} form, |
8661 | as in | 8678 | as in |
8662 | @verbatim{ | 8679 | @verbatim{ |
8663 | #!/usr/local/bin/lua | 8680 | #!/usr/local/bin/lua |
8664 | } | 8681 | } |
8665 | (Of course, | 8682 | Of course, |
8666 | the location of the Lua interpreter may be different in your machine. | 8683 | the location of the Lua interpreter may be different in your machine. |
8667 | If @id{lua} is in your @id{PATH}, | 8684 | If @id{lua} is in your @id{PATH}, |
8668 | then | 8685 | then |
8669 | @verbatim{ | 8686 | @verbatim{ |
8670 | #!/usr/bin/env lua | 8687 | #!/usr/bin/env lua |
8671 | } | 8688 | } |
8672 | is a more portable solution.) | 8689 | is a more portable solution. |
8673 | 8690 | ||
8674 | } | 8691 | } |
8675 | 8692 | ||
@@ -8688,7 +8705,7 @@ do not imply source-code changes in a program, | |||
8688 | such as the numeric values for constants | 8705 | such as the numeric values for constants |
8689 | or the implementation of functions as macros. | 8706 | or the implementation of functions as macros. |
8690 | Therefore, | 8707 | Therefore, |
8691 | you should not assume that binaries are compatible between | 8708 | you should never assume that binaries are compatible between |
8692 | different Lua versions. | 8709 | different Lua versions. |
8693 | Always recompile clients of the Lua API when | 8710 | Always recompile clients of the Lua API when |
8694 | using a new version. | 8711 | using a new version. |
@@ -8700,7 +8717,7 @@ precompiled chunks are not compatible between different Lua versions. | |||
8700 | The standard paths in the official distribution may | 8717 | The standard paths in the official distribution may |
8701 | change between versions. | 8718 | change between versions. |
8702 | 8719 | ||
8703 | @sect2{@title{Changes in the Language} | 8720 | @sect2{@title{Incompatibilities in the Language} |
8704 | @itemize{ | 8721 | @itemize{ |
8705 | 8722 | ||
8706 | @item{ | 8723 | @item{ |
@@ -8752,7 +8769,7 @@ like any other error when calling a finalizer.) | |||
8752 | 8769 | ||
8753 | } | 8770 | } |
8754 | 8771 | ||
8755 | @sect2{@title{Changes in the Libraries} | 8772 | @sect2{@title{Incompatibilities in the Libraries} |
8756 | @itemize{ | 8773 | @itemize{ |
8757 | 8774 | ||
8758 | @item{ | 8775 | @item{ |
@@ -8762,13 +8779,6 @@ Moreover, it uses a different algorithm. | |||
8762 | } | 8779 | } |
8763 | 8780 | ||
8764 | @item{ | 8781 | @item{ |
8765 | The function @Lid{io.lines} now returns three extra values, | ||
8766 | besides the iterator function. | ||
8767 | You can enclose the call in parentheses if you need to | ||
8768 | discard these extra results. | ||
8769 | } | ||
8770 | |||
8771 | @item{ | ||
8772 | By default, the decoding functions in the @Lid{utf8} library | 8782 | By default, the decoding functions in the @Lid{utf8} library |
8773 | do not accept surrogates as valid code points. | 8783 | do not accept surrogates as valid code points. |
8774 | An extra parameter in these functions makes them more permissive. | 8784 | An extra parameter in these functions makes them more permissive. |
@@ -8778,7 +8788,7 @@ An extra parameter in these functions makes them more permissive. | |||
8778 | 8788 | ||
8779 | } | 8789 | } |
8780 | 8790 | ||
8781 | @sect2{@title{Changes in the API} | 8791 | @sect2{@title{Incompatibilities in the API} |
8782 | 8792 | ||
8783 | @itemize{ | 8793 | @itemize{ |
8784 | 8794 | ||
@@ -8792,8 +8802,8 @@ which have an extra argument. | |||
8792 | 8802 | ||
8793 | For compatibility, the old names still work as macros assuming | 8803 | For compatibility, the old names still work as macros assuming |
8794 | one single user value. | 8804 | one single user value. |
8795 | Note, however, that the call @T{lua_newuserdatauv(L,size,0)} | 8805 | Note, however, that userdata with zero user values |
8796 | produces a smaller userdata. | 8806 | are more efficient memory-wise. |
8797 | } | 8807 | } |
8798 | 8808 | ||
8799 | @item{ | 8809 | @item{ |
@@ -8807,10 +8817,10 @@ those values were the entire stack.) | |||
8807 | @item{ | 8817 | @item{ |
8808 | The function @Lid{lua_version} returns the version number, | 8818 | The function @Lid{lua_version} returns the version number, |
8809 | instead of an address of the version number. | 8819 | instead of an address of the version number. |
8810 | (The Lua core should work correctly with libraries using their | 8820 | The Lua core should work correctly with libraries using their |
8811 | own static copies of the same core, | 8821 | own static copies of the same core, |
8812 | so there is no need to check whether they are using the same | 8822 | so there is no need to check whether they are using the same |
8813 | address space.) | 8823 | address space. |
8814 | } | 8824 | } |
8815 | 8825 | ||
8816 | @item{ | 8826 | @item{ |