| @@ -38,7 +38,7 @@ Freely available under the terms of the · 37 unmodified lines |
| 38 | <!-- ====================================================================== --> | 38 | <!-- ====================================================================== --> |
| 39 | <p> | 39 | <p> |
| 40 | | 40 | |
| 41 | <!-- $Id: manual.of,v 1.63 2010/11/17 15:35:05 roberto Exp $ --> | 41 | <!-- $Id: manual.of,v 1.66 2010/11/22 18:07:40 roberto Exp $ --> |
| 42 | | 42 | |
| 43 | | 43 | |
| 44 | | 44 | |
| @@ -53,22 +53,23 @@ It also offers good support for object-oriented programming, · 8 unmodified lines |
| 53 | functional programming, and data-driven programming. | 53 | functional programming, and data-driven programming. |
| 54 | Lua is intended to be used as a powerful, light-weight | 54 | Lua is intended to be used as a powerful, light-weight |
| 55 | scripting language for any program that needs one. | 55 | scripting language for any program that needs one. |
| 56 | Lua is implemented as a library, written in <em>clean</em> C | 56 | Lua is implemented as a library, written in <em>clean</em> C, |
| 57 | (that is, in the common subset of ANSI C and C++). | 57 | the common subset of ANSI C and C++. |
| 58 | | 58 | |
| 59 | | 59 | |
| 60 | <p> | 60 | <p> |
| 61 | Being an extension language, Lua has no notion of a "main" program: | 61 | Being an extension language, Lua has no notion of a "main" program: |
| 62 | it only works <em>embedded</em> in a host client, | 62 | it only works <em>embedded</em> in a host client, |
| 63 | called the <em>embedding program</em> or simply the <em>host</em>. | 63 | called the <em>embedding program</em> or simply the <em>host</em>. |
| 64 | This host program can invoke functions to execute a piece of Lua code, | 64 | The host program can invoke functions to execute a piece of Lua code, |
| 65 | can write and read Lua variables, | 65 | can write and read Lua variables, |
| 66 | and can register C functions to be called by Lua code. | 66 | and can register C functions to be called by Lua code. |
| 67 | Through the use of C functions, Lua can be augmented to cope with | 67 | Through the use of C functions, Lua can be augmented to cope with |
| 68 | a wide range of different domains, | 68 | a wide range of different domains, |
| 69 | thus creating customized programming languages sharing a syntactical framework. | 69 | thus creating customized programming languages sharing a syntactical framework. |
| 70 | The Lua distribution includes a sample host program called <code>lua</code>, | 70 | The Lua distribution includes a sample host program called <code>lua</code>, |
| 71 | which uses the Lua library to offer a complete, standalone Lua interpreter. | 71 | which uses the Lua library to offer a complete, standalone Lua interpreter, |
| | 72 | for interactive or batch use. |
| 72 | | 73 | |
| 73 | | 74 | |
| 74 | <p> | 75 | <p> |
| @@ -85,7 +86,7 @@ this document is dry in places. · 10 unmodified lines |
| 85 | For a discussion of the decisions behind the design of Lua, | 86 | For a discussion of the decisions behind the design of Lua, |
| 86 | see the technical papers available at Lua's web site. | 87 | see the technical papers available at Lua's web site. |
| 87 | For a detailed introduction to programming in Lua, | 88 | For a detailed introduction to programming in Lua, |
| 88 | see Roberto's book, <em>Programming in Lua (Second Edition)</em>. | 89 | see Roberto's book, <em>Programming in Lua</em> (second edition). |
| 89 | | 90 | |
| 90 | | 91 | |
| 91 | | 92 | |
| @@ -126,7 +127,7 @@ any other value makes it true. · 34 unmodified lines |
| 126 | <em>Number</em> represents real (double-precision floating-point) numbers. | 127 | <em>Number</em> represents real (double-precision floating-point) numbers. |
| 127 | (It is easy to build Lua interpreters that use other | 128 | (It is easy to build Lua interpreters that use other |
| 128 | internal representations for numbers, | 129 | internal representations for numbers, |
| 129 | such as single-precision float or long integers; | 130 | such as single-precision floats or long integers; |
| 130 | see file <code>luaconf.h</code>.) | 131 | see file <code>luaconf.h</code>.) |
| 131 | <em>String</em> represents arrays of characters. | 132 | <em>String</em> represents arrays of characters. |
| 132 | | 133 | |
| @@ -211,7 +212,7 @@ of a given value. · 78 unmodified lines |
| 211 | As discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, | 212 | As discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, |
| 212 | any reference to a global name <code>var</code> is syntactically translated | 213 | any reference to a global name <code>var</code> is syntactically translated |
| 213 | to <code>_ENV.var</code>. | 214 | to <code>_ENV.var</code>. |
| 214 | Moreover, any chunk is compiled in the scope of an external | 215 | Moreover, every chunk is compiled in the scope of an external |
| 215 | variable called <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), | 216 | variable called <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), |
| 216 | so <code>_ENV</code> itself is never a global name in a chunk. | 217 | so <code>_ENV</code> itself is never a global name in a chunk. |
| 217 | | 218 | |
| @@ -256,7 +257,7 @@ If you change the global environment in the registry · 38 unmodified lines |
| 256 | (through C code or the debug library), | 257 | (through C code or the debug library), |
| 257 | all chunks loaded after the change will get the new environment. | 258 | all chunks loaded after the change will get the new environment. |
| 258 | Previously loaded chunks are not affected, however, | 259 | Previously loaded chunks are not affected, however, |
| 259 | as each has its own copy of the environment in its <code>_ENV</code> variable. | 260 | as each has its own reference to the environment in its <code>_ENV</code> variable. |
| 260 | Moreover, the variable <a href="#pdf-_G"><code>_G</code></a> | 261 | Moreover, the variable <a href="#pdf-_G"><code>_G</code></a> |
| 261 | (which is stored in the original global environment) | 262 | (which is stored in the original global environment) |
| 262 | is never updated by Lua. | 263 | is never updated by Lua. |
| @@ -271,8 +272,9 @@ is never updated by Lua. · 8 unmodified lines |
| 271 | Because Lua is an embedded extension language, | 272 | Because Lua is an embedded extension language, |
| 272 | all Lua actions start from C code in the host program | 273 | all Lua actions start from C code in the host program |
| 273 | calling a function from the Lua library (see <a href="#lua_pcall"><code>lua_pcall</code></a>). | 274 | calling a function from the Lua library (see <a href="#lua_pcall"><code>lua_pcall</code></a>). |
| 274 | Whenever an error occurs during Lua compilation or execution, | 275 | Whenever an error occurs during |
| 275 | control returns to C, | 276 | the compilation or execution of a Lua chunk, |
| | 277 | control returns to the host, |
| 276 | which can take appropriate measures | 278 | which can take appropriate measures |
| 277 | (such as printing an error message). | 279 | (such as printing an error message). |
| 278 | | 280 | |
| @@ -281,7 +283,7 @@ which can take appropriate measures · 2 unmodified lines |
| 281 | Lua code can explicitly generate an error by calling the | 283 | Lua code can explicitly generate an error by calling the |
| 282 | <a href="#pdf-error"><code>error</code></a> function. | 284 | <a href="#pdf-error"><code>error</code></a> function. |
| 283 | If you need to catch errors in Lua, | 285 | If you need to catch errors in Lua, |
| 284 | you can use the <a href="#pdf-pcall"><code>pcall</code></a> function. | 286 | you can use the <a href="#pdf-pcall"><code>pcall</code></a> or the <a href="#pdf-xpcall"><code>xpcall</code></a> function. |
| 285 | | 287 | |
| 286 | | 288 | |
| 287 | | 289 | |
| @@ -297,27 +299,26 @@ under certain special operations. · 9 unmodified lines |
| 297 | You can change several aspects of the behavior | 299 | You can change several aspects of the behavior |
| 298 | of operations over a value by setting specific fields in its metatable. | 300 | of operations over a value by setting specific fields in its metatable. |
| 299 | For instance, when a non-numeric value is the operand of an addition, | 301 | For instance, when a non-numeric value is the operand of an addition, |
| 300 | Lua checks for a function in the field "<code>__add</code>" in its metatable. | 302 | Lua checks for a function in the field "<code>__add</code>" of the value's metatable. |
| 301 | If it finds one, | 303 | If it finds one, |
| 302 | Lua calls this function to perform the addition. | 304 | Lua calls this function to perform the addition. |
| 303 | | 305 | |
| 304 | | 306 | |
| 305 | <p> | 307 | <p> |
| 306 | We call the keys in a metatable <em>events</em> | 308 | The keys in a metatable are derived from the <em>event</em> names; |
| 307 | and the values <em>metamethods</em>. | 309 | the corresponding values are called <em>metamethods</em>. |
| 308 | In the previous example, the event is <code>"__add"</code> | 310 | In the previous example, the event is <code>"add"</code> |
| 309 | and the metamethod is the function that performs the addition. | 311 | and the metamethod is the function that performs the addition. |
| 310 | | 312 | |
| 311 | | 313 | |
| 312 | <p> | 314 | <p> |
| 313 | You can query the metatable of any value | 315 | You can query the metatable of any value |
| 314 | through the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. | 316 | using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. |
| 315 | | 317 | |
| 316 | | 318 | |
| 317 | <p> | 319 | <p> |
| 318 | You can replace the metatable of tables | 320 | You can replace the metatable of tables |
| 319 | through the <a href="#pdf-setmetatable"><code>setmetatable</code></a> | 321 | using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. |
| 320 | function. | | |
| 321 | You cannot change the metatable of other types from Lua | 322 | You cannot change the metatable of other types from Lua |
| 322 | (except by using the debug library); | 323 | (except by using the debug library); |
| 323 | you must use the C API for that. | 324 | you must use the C API for that. |
| @@ -338,8 +339,6 @@ A metatable controls how an object behaves in arithmetic operations, · 14 unmodified lines |
| 338 | order comparisons, concatenation, length operation, and indexing. | 339 | order comparisons, concatenation, length operation, and indexing. |
| 339 | A metatable also can define a function to be called when a userdata | 340 | A metatable also can define a function to be called when a userdata |
| 340 | is garbage collected. | 341 | is garbage collected. |
| 341 | For each of these operations Lua associates a specific key | | |
| 342 | called an <em>event</em>. | | |
| 343 | When Lua performs one of these operations over a value, | 342 | When Lua performs one of these operations over a value, |
| 344 | it checks whether this value has a metatable with the corresponding event. | 343 | it checks whether this value has a metatable with the corresponding event. |
| 345 | If so, the value associated with that key (the metamethod) | 344 | If so, the value associated with that key (the metamethod) |
| @@ -353,11 +352,11 @@ The key for each operation is a string with its name prefixed by · 7 unmodified lines |
| 353 | two underscores, '<code>__</code>'; | 352 | two underscores, '<code>__</code>'; |
| 354 | for instance, the key for operation "add" is the | 353 | for instance, the key for operation "add" is the |
| 355 | string "<code>__add</code>". | 354 | string "<code>__add</code>". |
| 356 | The semantics of these operations is better explained by a Lua function | | |
| 357 | describing how the interpreter executes the operation. | | |
| 358 | | 355 | |
| 359 | | 356 | |
| 360 | <p> | 357 | <p> |
| | 358 | The semantics of these operations is better explained by a Lua function |
| | 359 | describing how the interpreter executes the operation. |
| 361 | The code shown here in Lua is only illustrative; | 360 | The code shown here in Lua is only illustrative; |
| 362 | the real behavior is hard coded in the interpreter | 361 | the real behavior is hard coded in the interpreter |
| 363 | and it is much more efficient than this simulation. | 362 | and it is much more efficient than this simulation. |
| @@ -719,7 +718,7 @@ It uses two numbers to control its garbage-collection cycles: · 355 unmodified lines |
| 719 | the <em>garbage-collector pause</em> and | 718 | the <em>garbage-collector pause</em> and |
| 720 | the <em>garbage-collector step multiplier</em>. | 719 | the <em>garbage-collector step multiplier</em>. |
| 721 | Both use percentage points as units | 720 | Both use percentage points as units |
| 722 | (so that a value of 100 means an internal value of 1). | 721 | (e.g., a value of 100 means an internal value of 1). |
| 723 | | 722 | |
| 724 | | 723 | |
| 725 | <p> | 724 | <p> |
| @@ -733,14 +732,15 @@ to double before starting a new cycle. · 7 unmodified lines |
| 733 | | 732 | |
| 734 | | 733 | |
| 735 | <p> | 734 | <p> |
| 736 | The step multiplier | 735 | The garbage-collector step multiplier |
| 737 | controls the relative speed of the collector relative to | 736 | controls the relative speed of the collector relative to |
| 738 | memory allocation. | 737 | memory allocation. |
| 739 | Larger values make the collector more aggressive but also increase | 738 | Larger values make the collector more aggressive but also increase |
| 740 | the size of each incremental step. | 739 | the size of each incremental step. |
| 741 | Values smaller than 100 make the collector too slow and | 740 | Values smaller than 100 make the collector too slow and |
| 742 | can result in the collector never finishing a cycle. | 741 | can result in the collector never finishing a cycle. |
| 743 | The default, 200, means that the collector runs at "twice" | 742 | The default is 200, |
| | 743 | which means that the collector runs at "twice" |
| 744 | the speed of memory allocation. | 744 | the speed of memory allocation. |
| 745 | | 745 | |
| 746 | | 746 | |
| @@ -767,7 +767,7 @@ the collector directly (e.g., stop and restart it). · 20 unmodified lines |
| 767 | | 767 | |
| 768 | <p> | 768 | <p> |
| 769 | Using the C API, | 769 | Using the C API, |
| 770 | you can set garbage-collector metamethods for userdata (see <a href="#2.4">§2.4</a>). | 770 | you can set garbage-collector metamethods for full userdata (see <a href="#2.4">§2.4</a>). |
| 771 | These metamethods are also called <em>finalizers</em>. | 771 | These metamethods are also called <em>finalizers</em>. |
| 772 | Finalizers allow you to coordinate Lua's garbage collection | 772 | Finalizers allow you to coordinate Lua's garbage collection |
| 773 | with external resource management | 773 | with external resource management |
| @@ -777,12 +777,12 @@ or freeing your own memory). · 3 unmodified lines |
| 777 | | 777 | |
| 778 | <p> | 778 | <p> |
| 779 | For a userdata to be finalized when collected, | 779 | For a userdata to be finalized when collected, |
| 780 | you must <em>mark</em> it first. | 780 | you must <em>mark</em> it for finalization. |
| 781 | | 781 | |
| 782 | You mark a userdata for finalization when you set its metatable | 782 | You mark a userdata for finalization when you set its metatable |
| 783 | and the metatable has a field indexed by the string "<code>__gc</code>". | 783 | and the metatable has a field indexed by the string "<code>__gc</code>". |
| 784 | Note that if you set a metatable without a <code>__gc</code> field | 784 | Note that if you set a metatable without a <code>__gc</code> field |
| 785 | and later create that field, | 785 | and later create that field in the metatable, |
| 786 | the userdata will not be marked for finalization. | 786 | the userdata will not be marked for finalization. |
| 787 | However, after a userdata is marked, | 787 | However, after a userdata is marked, |
| 788 | you can freely change the <code>__gc</code> field of its metatable. | 788 | you can freely change the <code>__gc</code> field of its metatable. |
| @@ -826,7 +826,7 @@ A <em>weak table</em> is a table whose elements are · 37 unmodified lines |
| 826 | A weak reference is ignored by the garbage collector. | 826 | A weak reference is ignored by the garbage collector. |
| 827 | In other words, | 827 | In other words, |
| 828 | if the only references to an object are weak references, | 828 | if the only references to an object are weak references, |
| 829 | then the garbage collector will collect this object. | 829 | then the garbage collector will collect that object. |
| 830 | | 830 | |
| 831 | | 831 | |
| 832 | <p> | 832 | <p> |
| @@ -873,11 +873,11 @@ Lua treats strings and light C functions as non-object values. · 40 unmodified lines |
| 873 | | 873 | |
| 874 | | 874 | |
| 875 | <p> | 875 | <p> |
| 876 | Userdata marked for finalization has a special behavior in weak tables. | 876 | Userdata marked for finalization have a special behavior in weak tables. |
| 877 | When a marked userdata is a value in a weak table, | 877 | When a marked userdata is a value in a weak table, |
| 878 | it is removed from the table the first time it is collected, | 878 | it is removed from the table the first time it is collected, |
| 879 | before running its finalizer. | 879 | before running its finalizer. |
| 880 | When it is a key, however, | 880 | However, when it is a key, |
| 881 | it is removed from the table only when it is really freed, | 881 | it is removed from the table only when it is really freed, |
| 882 | after running its finalizer. | 882 | after running its finalizer. |
| 883 | This behavior allows the finalizer to access values | 883 | This behavior allows the finalizer to access values |
| @@ -901,15 +901,16 @@ a yield function. · 17 unmodified lines |
| 901 | | 901 | |
| 902 | | 902 | |
| 903 | <p> | 903 | <p> |
| 904 | You create a coroutine with a call to <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. | 904 | You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. |
| 905 | Its sole argument is a function | 905 | Its sole argument is a function |
| 906 | that is the main function of the coroutine. | 906 | that is the main function of the coroutine. |
| 907 | The <code>create</code> function only creates a new coroutine and | 907 | The <code>create</code> function only creates a new coroutine and |
| 908 | returns a handle to it (an object of type <em>thread</em>); | 908 | returns a handle to it (an object of type <em>thread</em>); |
| 909 | it does not start the coroutine execution. | 909 | it does not start the coroutine. |
| 910 | | 910 | |
| 911 | | 911 | |
| 912 | <p> | 912 | <p> |
| | 913 | You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. |
| 913 | When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | 914 | When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, |
| 914 | passing as its first argument | 915 | passing as its first argument |
| 915 | a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | 916 | a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, |
| @@ -962,7 +963,7 @@ any error is propagated to the caller. · 46 unmodified lines |
| 962 | | 963 | |
| 963 | | 964 | |
| 964 | <p> | 965 | <p> |
| 965 | As an example, | 966 | As an example of how coroutines work, |
| 966 | consider the following code: | 967 | consider the following code: |
| 967 | | 968 | |
| 968 | <pre> | 969 | <pre> |
| @@ -3659,6 +3660,11 @@ the value at the given index; · 2690 unmodified lines |
| 3659 | this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>. | 3660 | this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>. |
| 3660 | | 3661 | |
| 3661 | | 3662 | |
| | 3663 | <p> |
| | 3664 | See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying |
| | 3665 | the table during its traversal. |
| | 3666 | |
| | 3667 | |
| 3662 | | 3668 | |
| 3663 | | 3669 | |
| 3664 | | 3670 | |
| @@ -6972,7 +6978,7 @@ and "<code>userdata</code>". · 3307 unmodified lines |
| 6972 | <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> | 6978 | <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> |
| 6973 | A global variable (not a function) that | 6979 | A global variable (not a function) that |
| 6974 | holds a string containing the current interpreter version. | 6980 | holds a string containing the current interpreter version. |
| 6975 | The current contents of this variable is "<code>Lua 5.1</code>". | 6981 | The current contents of this variable is "<code>Lua 5.2</code>". |
| 6976 | | 6982 | |
| 6977 | | 6983 | |
| 6978 | | 6984 | |
| @@ -7665,9 +7671,9 @@ Here are some examples: · 686 unmodified lines |
| 7665 | end) | 7671 | end) |
| 7666 | --> x="4+5 = 9" | 7672 | --> x="4+5 = 9" |
| 7667 | | 7673 | |
| 7668 | local t = {name="lua", version="5.1"} | 7674 | local t = {name="lua", version="5.2"} |
| 7669 | x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) | 7675 | x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) |
| 7670 | --> x="lua-5.1.tar.gz" | 7676 | --> x="lua-5.2.tar.gz" |
| 7671 | </pre> | 7677 | </pre> |
| 7672 | | 7678 | |
| 7673 | | 7679 | |
| @@ -8385,8 +8391,6 @@ Returns the hyperbolic tangent of <code>x</code>. · 711 unmodified lines |
| 8385 | <p> | 8391 | <p> |
| 8386 | This library provides bitwise operations. | 8392 | This library provides bitwise operations. |
| 8387 | It provides all its functions inside the table <a name="pdf-bit32"><code>bit32</code></a>. | 8393 | It provides all its functions inside the table <a name="pdf-bit32"><code>bit32</code></a>. |
| 8388 | It uses upper case for function names to avoid collision with | | |
| 8389 | some reserved words (<b>and</b>, <b>or</b>, and <b>not</b>). | | |
| 8390 | | 8394 | |
| 8391 | | 8395 | |
| 8392 | <p> | 8396 | <p> |
| @@ -8398,12 +8402,34 @@ the remainder of its division by <em>2<sup>32</sup></em> · 5 unmodified lines |
| 8398 | and truncated to an integer (in some unspecified way), | 8402 | and truncated to an integer (in some unspecified way), |
| 8399 | so that its final value falls in the range <em>[0,2<sup>32</sup> - 1]</em>. | 8403 | so that its final value falls in the range <em>[0,2<sup>32</sup> - 1]</em>. |
| 8400 | Similarly, all results are in the range <em>[0,2<sup>32</sup> - 1]</em>. | 8404 | Similarly, all results are in the range <em>[0,2<sup>32</sup> - 1]</em>. |
| 8401 | Note that <code>bit32.NOT(0)</code> is <code>0xFFFFFFFF</code>, | 8405 | Note that <code>bit32.bnot(0)</code> is <code>0xFFFFFFFF</code>, |
| 8402 | which is different from <code>-1</code>. | 8406 | which is different from <code>-1</code>. |
| 8403 | | 8407 | |
| 8404 | | 8408 | |
| 8405 | <p> | 8409 | <p> |
| 8406 | <hr><h3><a name="pdf-bit32.AND"><code>bit32.AND (···)</code></a></h3> | 8410 | <hr><h3><a name="pdf-bit32.arshift"><code>bit32.arshift (x, disp)</code></a></h3> |
| | 8411 | |
| | 8412 | |
| | 8413 | <p> |
| | 8414 | Returns the number <code>x</code> shifted <code>disp</code> bits to the right. |
| | 8415 | The number <code>disp</code> may be any representable integer. |
| | 8416 | Negative displacements shift to the left. |
| | 8417 | |
| | 8418 | |
| | 8419 | <p> |
| | 8420 | This shift operation is what is called arithmetic shift. |
| | 8421 | Vacant bits on the left are filled |
| | 8422 | with copies of the higher bit of <code>x</code>; |
| | 8423 | vacant bits on the right are filled with zeros. |
| | 8424 | In particular, |
| | 8425 | displacements with absolute values higher than 31 |
| | 8426 | result in zero or <code>0xFFFFFFFF</code> (all original bits are shifted out). |
| | 8427 | |
| | 8428 | |
| | 8429 | |
| | 8430 | |
| | 8431 | <p> |
| | 8432 | <hr><h3><a name="pdf-bit32.band"><code>bit32.band (···)</code></a></h3> |
| 8407 | | 8433 | |
| 8408 | | 8434 | |
| 8409 | <p> | 8435 | <p> |
| @@ -8413,7 +8439,7 @@ Returns the bitwise and of its operands. · 3 unmodified lines |
| 8413 | | 8439 | |
| 8414 | | 8440 | |
| 8415 | <p> | 8441 | <p> |
| 8416 | <hr><h3><a name="pdf-bit32.NOT"><code>bit32.NOT (x)</code></a></h3> | 8442 | <hr><h3><a name="pdf-bit32.bnot"><code>bit32.bnot (x)</code></a></h3> |
| 8417 | | 8443 | |
| 8418 | | 8444 | |
| 8419 | <p> | 8445 | <p> |
| @@ -8422,13 +8448,13 @@ For any integer <code>x</code>, · 2 unmodified lines |
| 8422 | the following identity holds: | 8448 | the following identity holds: |
| 8423 | | 8449 | |
| 8424 | <pre> | 8450 | <pre> |
| 8425 | assert(bit32.NOT(x) == (-1 - x) % 2^32) | 8451 | assert(bit32.bnot(x) == (-1 - x) % 2^32) |
| 8426 | </pre> | 8452 | </pre> |
| 8427 | | 8453 | |
| 8428 | | 8454 | |
| 8429 | | 8455 | |
| 8430 | <p> | 8456 | <p> |
| 8431 | <hr><h3><a name="pdf-bit32.OR"><code>bit32.OR (···)</code></a></h3> | 8457 | <hr><h3><a name="pdf-bit32.bor"><code>bit32.bor (···)</code></a></h3> |
| 8432 | | 8458 | |
| 8433 | | 8459 | |
| 8434 | <p> | 8460 | <p> |
| @@ -8438,7 +8464,7 @@ Returns the bitwise or of its operands. · 3 unmodified lines |
| 8438 | | 8464 | |
| 8439 | | 8465 | |
| 8440 | <p> | 8466 | <p> |
| 8441 | <hr><h3><a name="pdf-bit32.TEST"><code>bit32.TEST (···)</code></a></h3> | 8467 | <hr><h3><a name="pdf-bit32.btest"><code>bit32.btest (···)</code></a></h3> |
| 8442 | | 8468 | |
| 8443 | | 8469 | |
| 8444 | <p> | 8470 | <p> |
| @@ -8449,7 +8475,7 @@ whether the bitwise and of its operands is different from zero. · 4 unmodified lines |
| 8449 | | 8475 | |
| 8450 | | 8476 | |
| 8451 | <p> | 8477 | <p> |
| 8452 | <hr><h3><a name="pdf-bit32.XOR"><code>bit32.XOR (···)</code></a></h3> | 8478 | <hr><h3><a name="pdf-bit32.bxor"><code>bit32.bxor (···)</code></a></h3> |
| 8453 | | 8479 | |
| 8454 | | 8480 | |
| 8455 | <p> | 8481 | <p> |
| @@ -8459,7 +8485,7 @@ Returns the bitwise exclusive or of its operands. · 3 unmodified lines |
| 8459 | | 8485 | |
| 8460 | | 8486 | |
| 8461 | <p> | 8487 | <p> |
| 8462 | <hr><h3><a name="pdf-bit32.ROL"><code>bit32.ROL (x, disp)</code></a></h3> | 8488 | <hr><h3><a name="pdf-bit32.lrotate"><code>bit32.lrotate (x, disp)</code></a></h3> |
| 8463 | | 8489 | |
| 8464 | | 8490 | |
| 8465 | <p> | 8491 | <p> |
| @@ -8472,7 +8498,7 @@ For any valid displacement, · 6 unmodified lines |
| 8472 | the following identity holds: | 8498 | the following identity holds: |
| 8473 | | 8499 | |
| 8474 | <pre> | 8500 | <pre> |
| 8475 | assert(bit32.ROL(x, disp) == bit32.ROL(x, disp % 32)) | 8501 | assert(bit32.lrotate(x, disp) == bit32.lrotate(x, disp % 32)) |
| 8476 | </pre><p> | 8502 | </pre><p> |
| 8477 | In particular, | 8503 | In particular, |
| 8478 | negative displacements rotate to the right. | 8504 | negative displacements rotate to the right. |
| @@ -8481,75 +8507,53 @@ negative displacements rotate to the right. · 2 unmodified lines |
| 8481 | | 8507 | |
| 8482 | | 8508 | |
| 8483 | <p> | 8509 | <p> |
| 8484 | <hr><h3><a name="pdf-bit32.ROR"><code>bit32.ROR (x, disp)</code></a></h3> | 8510 | <hr><h3><a name="pdf-bit32.lshift"><code>bit32.lshift (x, disp)</code></a></h3> |
| 8485 | | 8511 | |
| 8486 | | 8512 | |
| 8487 | <p> | 8513 | <p> |
| 8488 | Returns the number <code>x</code> rotated <code>disp</code> bits to the right. | 8514 | Returns the number <code>x</code> shifted <code>disp</code> bits to the left. |
| 8489 | The number <code>disp</code> may be any representable integer. | 8515 | The number <code>disp</code> may be any representable integer. |
| | 8516 | Negative displacements shift to the right. |
| | 8517 | In any direction, vacant bits are filled with zeros. |
| | 8518 | In particular, |
| | 8519 | displacements with absolute values higher than 31 |
| | 8520 | result in zero (all bits are shifted out). |
| 8490 | | 8521 | |
| 8491 | | 8522 | |
| 8492 | <p> | 8523 | <p> |
| 8493 | For any valid displacement, | 8524 | For positive displacements, |
| 8494 | the following identity holds: | 8525 | the following equality holds: |
| 8495 | | 8526 | |
| 8496 | <pre> | 8527 | <pre> |
| 8497 | assert(bit32.ROR(x, disp) == bit32.ROR(x, disp % 32)) | 8528 | assert(bit32.lshift(b, disp) == (b * 2^disp) % 2^32) |
| 8498 | </pre><p> | 8529 | </pre> |
| 8499 | In particular, | | |
| 8500 | negative displacements rotate to the left. | | |
| 8501 | | | |
| 8502 | | 8530 | |
| 8503 | | 8531 | |
| 8504 | | 8532 | |
| 8505 | <p> | 8533 | <p> |
| 8506 | <hr><h3><a name="pdf-bit32.SAR"><code>bit32.SAR (x, disp)</code></a></h3> | 8534 | <hr><h3><a name="pdf-bit32.rrotate"><code>bit32.rrotate (x, disp)</code></a></h3> |
| 8507 | | 8535 | |
| 8508 | | 8536 | |
| 8509 | <p> | 8537 | <p> |
| 8510 | Returns the number <code>x</code> shifted <code>disp</code> bits to the right. | 8538 | Returns the number <code>x</code> rotated <code>disp</code> bits to the right. |
| 8511 | The number <code>disp</code> may be any representable integer. | 8539 | The number <code>disp</code> may be any representable integer. |
| 8512 | Negative displacements shift to the left. | | |
| 8513 | | | |
| 8514 | | | |
| 8515 | <p> | | |
| 8516 | This shift operation is what is called arithmetic shift. | | |
| 8517 | Vacant bits on the left are filled | | |
| 8518 | with copies of the higher bit of <code>x</code>; | | |
| 8519 | vacant bits on the right are filled with zeros. | | |
| 8520 | In particular, | | |
| 8521 | displacements with absolute values higher than 31 | | |
| 8522 | result in zero or <code>0xFFFFFFFF</code> (all bits are shifted out). | | |
| 8523 | | | |
| 8524 | | | |
| 8525 | | 8540 | |
| 8526 | | 8541 | |
| 8527 | <p> | 8542 | <p> |
| 8528 | <hr><h3><a name="pdf-bit32.SHL"><code>bit32.SHL (x, disp)</code></a></h3> | 8543 | For any valid displacement, |
| 8529 | | 8544 | the following identity holds: |
| 8530 | | 8545 | |
| 8531 | <p> | 8546 | <pre> |
| 8532 | Returns the number <code>x</code> shifted <code>disp</code> bits to the left. | 8547 | assert(bit32.rrotate(x, disp) == bit32.rrotate(x, disp % 32)) |
| 8533 | The number <code>disp</code> may be any representable integer. | 8548 | </pre><p> |
| 8534 | Negative displacements shift to the right. | | |
| 8535 | In any direction, vacant bits are filled with zeros. | | |
| 8536 | In particular, | 8549 | In particular, |
| 8537 | displacements with absolute values higher than 31 | 8550 | negative displacements rotate to the left. |
| 8538 | result in zero (all bits are shifted out). | | |
| 8539 | | | |
| 8540 | | | |
| 8541 | <p> | | |
| 8542 | For positive displacements, | | |
| 8543 | the following equality holds: | | |
| 8544 | | 8551 | |
| 8545 | <pre> | | |
| 8546 | assert(bit32.SHL(b, disp) == (b * 2^disp) % 2^32) | | |
| 8547 | </pre> | | |
| 8548 | | 8552 | |
| 8549 | | 8553 | |
| 8550 | | 8554 | |
| 8551 | <p> | 8555 | <p> |
| 8552 | <hr><h3><a name="pdf-bit32.SHR"><code>bit32.SHR (x, disp)</code></a></h3> | 8556 | <hr><h3><a name="pdf-bit32.rshift"><code>bit32.rshift (x, disp)</code></a></h3> |
| 8553 | | 8557 | |
| 8554 | | 8558 | |
| 8555 | <p> | 8559 | <p> |
| @@ -8567,7 +8571,7 @@ For positive displacements, · 11 unmodified lines |
| 8567 | the following equality holds: | 8571 | the following equality holds: |
| 8568 | | 8572 | |
| 8569 | <pre> | 8573 | <pre> |
| 8570 | assert(bit32.SHR(b, disp) == math.floor(b % 2^32 / 2^disp)) | 8574 | assert(bit32.rshift(b, disp) == math.floor(b % 2^32 / 2^disp)) |
| 8571 | </pre> | 8575 | </pre> |
| 8572 | | 8576 | |
| 8573 | <p> | 8577 | <p> |
| @@ -9925,7 +9929,7 @@ Here is the complete syntax of Lua in extended BNF. · 1351 unmodified lines |
| 9925 | <HR> | 9929 | <HR> |
| 9926 | <SMALL> | 9930 | <SMALL> |
| 9927 | Last update: | 9931 | Last update: |
| 9928 | Wed Nov 17 14:49:08 BRST 2010 | 9932 | Mon Nov 22 16:36:19 BRST 2010 |
| 9929 | </SMALL> | 9933 | </SMALL> |
| 9930 | <!-- | 9934 | <!-- |
| 9931 | Last change: revised for Lua 5.2.0 (alpha) | 9935 | Last change: revised for Lua 5.2.0 (alpha) |