Mercurial Hosting > luan
comparison website/src/manual.html @ 373:571057b1666b
work on manual
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Fri, 17 Apr 2015 15:14:44 -0600 |
| parents | f08cefa4594c |
| children | 6c6c3537035e |
comparison
equal
deleted
inserted
replaced
| 372:f08cefa4594c | 373:571057b1666b |
|---|---|
| 33 | 33 |
| 34 <h2>Contents</h2> | 34 <h2>Contents</h2> |
| 35 | 35 |
| 36 <div margin-bottom="1em"><a href="#intro">Introduction</a></div> | 36 <div margin-bottom="1em"><a href="#intro">Introduction</a></div> |
| 37 | 37 |
| 38 <div margin-bottom="1em"> | |
| 39 <a href="#basic">Basic Concepts</a> | |
| 40 <ul> | |
| 41 <li><a href="#types">Values and Types</a></li> | |
| 42 <li><a href="#env">Environments</a></li> | |
| 43 <li><a href="#error">Error Handling</a></li> | |
| 44 <li><a href="#meta">Metatables and Metamethods</a></li> | |
| 45 </ul> | |
| 46 </div> | |
| 47 | |
| 38 <hr/> | 48 <hr/> |
| 39 | 49 |
| 50 | |
| 40 <h2 margin-top="1em"><a name="intro">Introduction</a></h2> | 51 <h2 margin-top="1em"><a name="intro">Introduction</a></h2> |
| 41 | 52 |
| 42 <p>Luan is a high level programming language based on <a href="http://www.lua.org">Lua</a>. A great strength of Lua is its simplicity and Luan takes this even further, being even simpler than Lua. The goal is to provide a simple programming language for the casual programmer with as few concepts as possible so that one can quickly learn the language and then easily understand any code written in Luan.</p> | 53 <p>Luan is a high level programming language based on <a href="http://www.lua.org">Lua</a>. A great strength of Lua is its simplicity and Luan takes this even further, being even simpler than Lua. The goal is to provide a simple programming language for the casual programmer with as few concepts as possible so that one can quickly learn the language and then easily understand any code written in Luan.</p> |
| 43 | 54 |
| 44 <p>Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers.</p> | 55 <p>Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers.</p> |
| 45 | 56 |
| 46 <p>Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding feature to Luan, but rather by providing a complete set of libraries.</p> | 57 <p>Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding feature to Luan, but rather by providing a complete set of libraries.</p> |
| 47 | 58 |
| 48 | 59 |
| 49 <h1>2 – <a name="2">Basic Concepts</a></h1> | 60 <h2 margin-top="1em"><a name="basic">Basic Concepts</a></h2> |
| 50 | 61 |
| 51 <p> | 62 <p>This section describes the basic concepts of the language.</p> |
| 52 This section describes the basic concepts of the language. | 63 |
| 53 | 64 <h3 margin-top="1em"><a name="types">Values and Types</a></h3> |
| 54 | 65 |
| 55 | 66 <p> |
| 56 <h2>2.1 – <a name="2.1">Values and Types</a></h2> | 67 Luan is a <i>dynamically typed language</i>. |
| 57 | |
| 58 <p> | |
| 59 Lua is a <em>dynamically typed language</em>. | |
| 60 This means that | 68 This means that |
| 61 variables do not have types; only values do. | 69 variables do not have types; only values do. |
| 62 There are no type definitions in the language. | 70 There are no type definitions in the language. |
| 63 All values carry their own type. | 71 All values carry their own type. |
| 64 | 72 |
| 65 | 73 |
| 66 <p> | 74 <p> |
| 67 All values in Lua are <em>first-class values</em>. | 75 All values in Luan are <i>first-class values</i>. |
| 68 This means that all values can be stored in variables, | 76 This means that all values can be stored in variables, |
| 69 passed as arguments to other functions, and returned as results. | 77 passed as arguments to other functions, and returned as results. |
| 70 | 78 |
| 71 | 79 |
| 72 <p> | 80 <p> |
| 73 There are eight basic types in Lua: | 81 There are eight basic types in Luan: |
| 74 <em>nil</em>, <em>boolean</em>, <em>number</em>, | 82 <i>nil</i>, <i>boolean</i>, <i>number</i>, |
| 75 <em>string</em>, <em>function</em>, <em>userdata</em>, | 83 <i>string</i>, <i>binary</i>, <i>function</i>, <i>userdata</i>, |
| 76 <em>thread</em>, and <em>table</em>. | 84 and <i>table</i>. |
| 77 <em>Nil</em> is the type of the value <b>nil</b>, | 85 <i>Nil</i> is the type of the value <b>nil</b>, |
| 78 whose main property is to be different from any other value; | 86 whose main property is to be different from any other value; |
| 79 it usually represents the absence of a useful value. | 87 it usually represents the absence of a useful value. |
| 80 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>. | 88 <i>Nil</i> is implemented as the Java value <i>null</i>. |
| 81 Both <b>nil</b> and <b>false</b> make a condition false; | 89 <i>Boolean</i> is the type of the values <b>false</b> and <b>true</b>. |
| 82 any other value makes it true. | 90 <i>Boolean</i> is implemented as the Java class <i>Boolean</i>. |
| 83 <em>Number</em> represents both | 91 <i>Number</i> represents both |
| 84 integer numbers and real (floating-point) numbers. | 92 integer numbers and real (floating-point) numbers. |
| 85 <em>String</em> represents immutable sequences of bytes. | 93 <i>Number</i> is implemented as the Java class <i>Number</i>. Any Java subclass of <i>Number</i> is allowed and this is invisible to the Luan user. Operations on numbers follow the same rules of |
| 86 | 94 the underlying Java implementation. |
| 87 Lua is 8-bit clean: | 95 |
| 88 strings can contain any 8-bit value, | 96 <i>String</i> is implemented as the Java class <i>String</i>. |
| 89 including embedded zeros ('<code>\0</code>'). | 97 <i>Binary</i> is implemented as the Java type <i>byte[]</i>. |
| 90 Lua is also encoding-agnostic; | 98 |
| 91 it makes no assumptions about the contents of a string. | 99 |
| 92 | 100 <p> |
| 93 | 101 Luan can call (and manipulate) functions written in Luan and |
| 94 <p> | 102 functions written in Java (see <a href="#3.4.10">§3.4.10</a>). |
| 95 The type <em>number</em> uses two internal representations, | 103 Both are represented by the type <i>function</i>. |
| 96 one called <em>integer</em> and the other called <em>float</em>. | 104 |
| 97 Lua has explicit rules about when each representation is used, | 105 |
| 98 but it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>). | 106 <p> |
| 99 Therefore, | 107 The type <i>userdata</i> is provided to allow arbitrary Java objects to |
| 100 the programmer may choose to mostly ignore the difference | 108 be stored in Luan variables. |
| 101 between integers and floats | 109 A userdata value is a Java object that isn't one of the standard Luan types. |
| 102 or to assume complete control over the representation of each number. | 110 Userdata has no predefined operations in Luan, |
| 103 Standard Lua uses 64-bit integers and double-precision (64-bit) floats, | |
| 104 but you can also compile Lua so that it | |
| 105 uses 32-bit integers and/or single-precision (32-bit) floats. | |
| 106 The option with 32 bits for both integers and floats | |
| 107 is particularly attractive | |
| 108 for small machines and embedded systems. | |
| 109 (See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.) | |
| 110 | |
| 111 | |
| 112 <p> | |
| 113 Lua can call (and manipulate) functions written in Lua and | |
| 114 functions written in C (see <a href="#3.4.10">§3.4.10</a>). | |
| 115 Both are represented by the type <em>function</em>. | |
| 116 | |
| 117 | |
| 118 <p> | |
| 119 The type <em>userdata</em> is provided to allow arbitrary C data to | |
| 120 be stored in Lua variables. | |
| 121 A userdata value represents a block of raw memory. | |
| 122 There are two kinds of userdata: | |
| 123 <em>full userdata</em>, | |
| 124 which is an object with a block of memory managed by Lua, | |
| 125 and <em>light userdata</em>, | |
| 126 which is simply a C pointer value. | |
| 127 Userdata has no predefined operations in Lua, | |
| 128 except assignment and identity test. | 111 except assignment and identity test. |
| 129 By using <em>metatables</em>, | 112 Userdata is useful then Java access is enabled in Luan |
| 130 the programmer can define operations for full userdata values | 113 |
| 131 (see <a href="#2.4">§2.4</a>). | 114 |
| 132 Userdata values cannot be created or modified in Lua, | 115 |
| 133 only through the C API. | 116 <p> |
| 134 This guarantees the integrity of data owned by the host program. | 117 The type <i>table</i> implements associative arrays, |
| 135 | |
| 136 | |
| 137 <p> | |
| 138 The type <em>thread</em> represents independent threads of execution | |
| 139 and it is used to implement coroutines (see <a href="#2.6">§2.6</a>). | |
| 140 Lua threads are not related to operating-system threads. | |
| 141 Lua supports coroutines on all systems, | |
| 142 even those that do not support threads natively. | |
| 143 | |
| 144 | |
| 145 <p> | |
| 146 The type <em>table</em> implements associative arrays, | |
| 147 that is, arrays that can be indexed not only with numbers, | 118 that is, arrays that can be indexed not only with numbers, |
| 148 but with any Lua value except <b>nil</b> and NaN. | 119 but with any Luan value except <b>nil</b>. |
| 149 (<em>Not a Number</em> is a special numeric value used to represent | 120 Tables can be <i>heterogeneous</i>; |
| 150 undefined or unrepresentable results, such as <code>0/0</code>.) | |
| 151 Tables can be <em>heterogeneous</em>; | |
| 152 that is, they can contain values of all types (except <b>nil</b>). | 121 that is, they can contain values of all types (except <b>nil</b>). |
| 153 Any key with value <b>nil</b> is not considered part of the table. | 122 Any key with value <b>nil</b> is not considered part of the table. |
| 154 Conversely, any key that is not part of a table has | 123 Conversely, any key that is not part of a table has |
| 155 an associated value <b>nil</b>. | 124 an associated value <b>nil</b>. |
| 156 | 125 |
| 157 | 126 |
| 158 <p> | 127 <p> |
| 159 Tables are the sole data-structuring mechanism in Lua; | 128 Tables are the sole data-structuring mechanism in Luan; |
| 160 they can be used to represent ordinary arrays, sequences, | 129 they can be used to represent ordinary arrays, sequences, |
| 161 symbol tables, sets, records, graphs, trees, etc. | 130 symbol tables, sets, records, graphs, trees, etc. |
| 162 To represent records, Lua uses the field name as an index. | 131 To represent records, Luan uses the field name as an index. |
| 163 The language supports this representation by | 132 The language supports this representation by |
| 164 providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>. | 133 providing <tt>a.name</tt> as syntactic sugar for <tt>a["name"]</tt>. |
| 165 There are several convenient ways to create tables in Lua | 134 There are several convenient ways to create tables in Luan |
| 166 (see <a href="#3.4.9">§3.4.9</a>). | 135 (see <a href="#3.4.9">§3.4.9</a>). |
| 167 | 136 |
| 168 | 137 |
| 169 <p> | 138 <p> |
| 170 We use the term <em>sequence</em> to denote a table where | 139 We use the term <i>sequence</i> to denote a table where |
| 171 the set of all positive numeric keys is equal to {1..<em>n</em>} | 140 the set of all positive numeric keys is equal to {1..<i>n</i>} |
| 172 for some non-negative integer <em>n</em>, | 141 for some non-negative integer <i>n</i>, |
| 173 which is called the length of the sequence (see <a href="#3.4.7">§3.4.7</a>). | 142 which is called the length of the sequence (see <a href="#3.4.7">§3.4.7</a>). |
| 174 | 143 |
| 175 | 144 |
| 176 <p> | 145 <p> |
| 177 Like indices, | 146 Like indices, |
| 178 the values of table fields can be of any type. | 147 the values of table fields can be of any type. |
| 179 In particular, | 148 In particular, |
| 180 because functions are first-class values, | 149 because functions are first-class values, |
| 181 table fields can contain functions. | 150 table fields can contain functions. |
| 182 Thus tables can also carry <em>methods</em> (see <a href="#3.4.11">§3.4.11</a>). | 151 Thus tables can also carry <i>methods</i> (see <a href="#3.4.11">§3.4.11</a>). |
| 183 | 152 |
| 184 | 153 |
| 185 <p> | 154 <p> |
| 186 The indexing of tables follows | 155 The indexing of tables follows |
| 187 the definition of raw equality in the language. | 156 the definition of raw equality in the language. |
| 188 The expressions <code>a[i]</code> and <code>a[j]</code> | 157 The expressions <tt>a[i]</tt> and <tt>a[j]</tt> |
| 189 denote the same table element | 158 denote the same table element |
| 190 if and only if <code>i</code> and <code>j</code> are raw equal | 159 if and only if <tt>i</tt> and <tt>j</tt> are raw equal |
| 191 (that is, equal without metamethods). | 160 (that is, equal without metamethods). |
| 192 In particular, floats with integral values | 161 In particular, floats with integral values |
| 193 are equal to their respective integers | 162 are equal to their respective integers |
| 194 (e.g., <code>1.0 == 1</code>). | 163 (e.g., <tt>1.0 == 1</tt>). |
| 195 To avoid ambiguities, | 164 |
| 196 any float with integral value used as a key | 165 |
| 197 is converted to its respective integer. | 166 <p> |
| 198 For instance, if you write <code>a[2.0] = true</code>, | 167 Luan values are <i>objects</i>: |
| 199 the actual key inserted into the table will be the | 168 variables do not actually <i>contain</i> values, |
| 200 integer <code>2</code>. | 169 only <i>references</i> to them. |
| 201 (On the other hand, | |
| 202 2 and "<code>2</code>" are different Lua values and therefore | |
| 203 denote different table entries.) | |
| 204 | |
| 205 | |
| 206 <p> | |
| 207 Tables, functions, threads, and (full) userdata values are <em>objects</em>: | |
| 208 variables do not actually <em>contain</em> these values, | |
| 209 only <em>references</em> to them. | |
| 210 Assignment, parameter passing, and function returns | 170 Assignment, parameter passing, and function returns |
| 211 always manipulate references to such values; | 171 always manipulate references to values; |
| 212 these operations do not imply any kind of copy. | 172 these operations do not imply any kind of copy. |
| 213 | 173 |
| 214 | 174 |
| 215 <p> | 175 <p> |
| 216 The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type | 176 The library function <a href="#pdf-type"><tt>Luan.type</tt></a> returns a string describing the type |
| 217 of a given value (see <a href="#6.1">§6.1</a>). | 177 of a given value (see <a href="#6.1">§6.1</a>). |
| 218 | 178 |
| 219 | 179 |
| 220 | 180 |
| 221 | 181 |
| 222 | 182 |
| 223 <h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2> | 183 <h3 margin-top="1em"><a name="env">Environments</a></h3> |
| 224 | 184 |
| 225 <p> | 185 <p> |
| 226 As will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, | 186 As will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, |
| 227 any reference to a free name | 187 any reference to a free name |
| 228 (that is, a name not bound to any declaration) <code>var</code> | 188 (that is, a name not bound to any declaration) <tt>var</tt> |
| 229 is syntactically translated to <code>_ENV.var</code>. | 189 is syntactically translated to <tt>_ENV.var</tt>. |
| 230 Moreover, every chunk is compiled in the scope of | 190 Moreover, every chunk is compiled in the scope of |
| 231 an external local variable named <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), | 191 an external local variable named <tt>_ENV</tt> (see <a href="#3.3.2">§3.3.2</a>), |
| 232 so <code>_ENV</code> itself is never a free name in a chunk. | 192 so <tt>_ENV</tt> itself is never a free name in a chunk. |
| 233 | 193 |
| 234 | 194 |
| 235 <p> | 195 <p> |
| 236 Despite the existence of this external <code>_ENV</code> variable and | 196 Despite the existence of this external <tt>_ENV</tt> variable and |
| 237 the translation of free names, | 197 the translation of free names, |
| 238 <code>_ENV</code> is a completely regular name. | 198 <tt>_ENV</tt> is a completely regular name. |
| 239 In particular, | 199 In particular, |
| 240 you can define new variables and parameters with that name. | 200 you can define new variables and parameters with that name. |
| 241 Each reference to a free name uses the <code>_ENV</code> that is | 201 Each reference to a free name uses the <tt>_ENV</tt> that is |
| 242 visible at that point in the program, | 202 visible at that point in the program, |
| 243 following the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>). | 203 following the usual visibility rules of Luan (see <a href="#3.5">§3.5</a>). |
| 244 | 204 |
| 245 | 205 |
| 246 <p> | 206 <p> |
| 247 Any table used as the value of <code>_ENV</code> is called an <em>environment</em>. | 207 Any table used as the value of <tt>_ENV</tt> is called an <i>environment</i>. |
| 248 | 208 |
| 249 | 209 |
| 250 <p> | 210 <p> |
| 251 Lua keeps a distinguished environment called the <em>global environment</em>. | 211 When Luan loads a chunk, |
| 252 This value is kept at a special index in the C registry (see <a href="#4.5">§4.5</a>). | 212 the default value for its <tt>_ENV</tt> is an empty table. |
| 253 In Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value. | 213 |
| 254 (<a href="#pdf-_G"><code>_G</code></a> is never used internally.) | 214 <p> |
| 255 | 215 Luan also provides all chunks with two other local values: <tt>require</tt> and <tt>java</tt>. These are functions used to load and access libraries and other modules. |
| 256 | 216 |
| 257 <p> | 217 |
| 258 When Lua loads a chunk, | 218 |
| 259 the default value for its <code>_ENV</code> upvalue | 219 |
| 260 is the global environment (see <a href="#pdf-load"><code>load</code></a>). | 220 <h3 margin-top="1em"><a name="error">Error Handling</a></h3> |
| 261 Therefore, by default, | 221 |
| 262 free names in Lua code refer to entries in the global environment | 222 <p> |
| 263 (and, therefore, they are also called <em>global variables</em>). | 223 Luan code can explicitly generate an error by calling the |
| 264 Moreover, all standard libraries are loaded in the global environment | 224 <a href="#pdf-error"><tt>error</tt></a> function. |
| 265 and some functions there operate on that environment. | 225 If you need to catch errors in Luan, |
| 266 You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>) | 226 you can use <a href="#pdf-pcall"><tt>pcall</tt></a> or <a href="#pdf-xpcall"><tt>try</tt></a> |
| 267 to load a chunk with a different environment. | 227 to call a given function in <i>protected mode</i>. |
| 268 (In C, you have to load the chunk and then change the value | |
| 269 of its first upvalue.) | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 <h2>2.3 – <a name="2.3">Error Handling</a></h2> | |
| 276 | |
| 277 <p> | |
| 278 Because Lua is an embedded extension language, | |
| 279 all Lua actions start from C code in the host program | |
| 280 calling a function from the Lua library. | |
| 281 (When you use Lua standalone, | |
| 282 the <code>lua</code> application is the host program.) | |
| 283 Whenever an error occurs during | |
| 284 the compilation or execution of a Lua chunk, | |
| 285 control returns to the host, | |
| 286 which can take appropriate measures | |
| 287 (such as printing an error message). | |
| 288 | |
| 289 | |
| 290 <p> | |
| 291 Lua code can explicitly generate an error by calling the | |
| 292 <a href="#pdf-error"><code>error</code></a> function. | |
| 293 If you need to catch errors in Lua, | |
| 294 you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a> | |
| 295 to call a given function in <em>protected mode</em>. | |
| 296 | 228 |
| 297 | 229 |
| 298 <p> | 230 <p> |
| 299 Whenever there is an error, | 231 Whenever there is an error, |
| 300 an <em>error object</em> (also called an <em>error message</em>) | 232 an <i>error object</i> (also called an <i>error message</i>) |
| 301 is propagated with information about the error. | 233 is propagated with information about the error. |
| 302 Lua itself only generates errors whose error object is a string, | 234 Luan itself only generates errors whose error object is a string, |
| 303 but programs may generate errors with | 235 but programs may generate errors with |
| 304 any value as the error object. | 236 any value as the error object. |
| 305 It is up to the Lua program or its host to handle such error objects. | 237 It is up to the Luan program or its host to handle such error objects. |
| 306 | 238 |
| 307 | 239 |
| 308 <p> | 240 |
| 309 When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>, | 241 |
| 310 you may give a <em>message handler</em> | 242 |
| 311 to be called in case of errors. | 243 <h3 margin-top="1em"><a name="meta">Metatables and Metamethods</a></h3> |
| 312 This function is called with the original error message | 244 |
| 313 and returns a new error message. | 245 <p> |
| 314 It is called before the error unwinds the stack, | 246 Every table in Luan can have a <i>metatable</i>. |
| 315 so that it can gather more information about the error, | 247 This <i>metatable</i> is an ordinary Luan table |
| 316 for instance by inspecting the stack and creating a stack traceback. | |
| 317 This message handler is still protected by the protected call; | |
| 318 so, an error inside the message handler | |
| 319 will call the message handler again. | |
| 320 If this loop goes on for too long, | |
| 321 Lua breaks it and returns an appropriate message. | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 <h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2> | |
| 328 | |
| 329 <p> | |
| 330 Every value in Lua can have a <em>metatable</em>. | |
| 331 This <em>metatable</em> is an ordinary Lua table | |
| 332 that defines the behavior of the original value | 248 that defines the behavior of the original value |
| 333 under certain special operations. | 249 under certain special operations. |
| 334 You can change several aspects of the behavior | 250 You can change several aspects of the behavior |
| 335 of operations over a value by setting specific fields in its metatable. | 251 of operations over a value by setting specific fields in its metatable. |
| 336 For instance, when a non-numeric value is the operand of an addition, | 252 For instance, when a table is the operand of an addition, |
| 337 Lua checks for a function in the field "<code>__add</code>" of the value's metatable. | 253 Luan checks for a function in the field "<tt>__add</tt>" of the table's metatable. |
| 338 If it finds one, | 254 If it finds one, |
| 339 Lua calls this function to perform the addition. | 255 Luan calls this function to perform the addition. |
| 340 | 256 |
| 341 | 257 |
| 342 <p> | 258 <p> |
| 343 The keys in a metatable are derived from the <em>event</em> names; | 259 The keys in a metatable are derived from the <i>event</i> names; |
| 344 the corresponding values are called <em>metamethods</em>. | 260 the corresponding values are called <ii>metamethods</i>. |
| 345 In the previous example, the event is <code>"add"</code> | 261 In the previous example, the event is <tt>"add"</tt> |
| 346 and the metamethod is the function that performs the addition. | 262 and the metamethod is the function that performs the addition. |
| 347 | 263 |
| 348 | 264 |
| 349 <p> | 265 <p> |
| 350 You can query the metatable of any value | 266 You can query the metatable of any table |
| 351 using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. | 267 using the <a href="#pdf-getmetatable"><tt>get_metatable</tt></a> function. |
| 352 | 268 |
| 353 | 269 |
| 354 <p> | 270 <p> |
| 355 You can replace the metatable of tables | 271 You can replace the metatable of tables |
| 356 using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. | 272 using the <a href="#pdf-setmetatable"><tt>set_metatable</tt></a> function. |
| 357 You cannot change the metatable of other types from Lua | 273 |
| 358 (except by using the debug library (<a href="#6.10">§6.10</a>)); | 274 |
| 359 you must use the C API for that. | 275 <p> |
| 360 | 276 A metatable controls how a table behaves in |
| 361 | |
| 362 <p> | |
| 363 Tables and full userdata have individual metatables | |
| 364 (although multiple tables and userdata can share their metatables). | |
| 365 Values of all other types share one single metatable per type; | |
| 366 that is, there is one single metatable for all numbers, | |
| 367 one for all strings, etc. | |
| 368 By default, a value has no metatable, | |
| 369 but the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>). | |
| 370 | |
| 371 | |
| 372 <p> | |
| 373 A metatable controls how an object behaves in | |
| 374 arithmetic operations, bitwise operations, | 277 arithmetic operations, bitwise operations, |
| 375 order comparisons, concatenation, length operation, calls, and indexing. | 278 order comparisons, concatenation, length operation, calls, and indexing. |
| 376 A metatable also can define a function to be called | |
| 377 when a userdata or a table is garbage collected (<a href="#2.5">§2.5</a>). | |
| 378 | 279 |
| 379 | 280 |
| 380 <p> | 281 <p> |
| 381 A detailed list of events controlled by metatables is given next. | 282 A detailed list of events controlled by metatables is given next. |
| 382 Each operation is identified by its corresponding event name. | 283 Each operation is identified by its corresponding event name. |
| 383 The key for each event is a string with its name prefixed by | 284 The key for each event is a string with its name prefixed by |
| 384 two underscores, '<code>__</code>'; | 285 two underscores, '<tt>__</tt>'; |
| 385 for instance, the key for operation "add" is the | 286 for instance, the key for operation "add" is the |
| 386 string "<code>__add</code>". | 287 string "<tt>__add</tt>". |
| 387 Note that queries for metamethods are always raw; | 288 Note that queries for metamethods are always raw; |
| 388 the access to a metamethod does not invoke other metamethods. | 289 the access to a metamethod does not invoke other metamethods. |
| 389 You can emulate how Lua queries a metamethod for an object <code>obj</code> | 290 You can emulate how Luan queries a metamethod for an object <tt>obj</tt> |
| 390 with the following code: | 291 with the following code: |
| 391 | 292 |
| 392 <pre> | 293 <tt><pre> |
| 393 rawget(getmetatable(obj) or {}, "__" .. event_name) | 294 raw_get(get_metatable(obj) or {}, "__" .. event_name) |
| 394 </pre> | 295 |
| 395 | 296 </pre></tt> |
| 396 <p> | 297 |
| 397 For the unary operators (negation, length, and bitwise not), | 298 <p> |
| 398 the metamethod is computed and called with a dummy second operand, | 299 Here are the events: |
| 399 equal to the first one. | |
| 400 This extra operand is only to simplify Lua's internals | |
| 401 (by making these operators behave like a binary operation) | |
| 402 and may be removed in future versions. | |
| 403 (For most uses this extra operand is irrelevant.) | |
| 404 | |
| 405 | |
| 406 | 300 |
| 407 <ul> | 301 <ul> |
| 408 | 302 |
| 409 <li><b>"add": </b> | 303 <li><b>"add": </b> |
| 410 the <code>+</code> operation. | 304 the <tt>+</tt> operation. |
| 411 | 305 |
| 412 If any operand for an addition is not a number | 306 If any operand for an addition is a table, |
| 413 (nor a string coercible to a number), | 307 Luan will try to call a metamethod. |
| 414 Lua will try to call a metamethod. | 308 First, Luan will check the first operand (even if it is valid). |
| 415 First, Lua will check the first operand (even if it is valid). | 309 If that operand does not define a metamethod for the "<tt>__add</tt>" event, |
| 416 If that operand does not define a metamethod for the "<code>__add</code>" event, | 310 then Luan will check the second operand. |
| 417 then Lua will check the second operand. | 311 If Luan can find a metamethod, |
| 418 If Lua can find a metamethod, | |
| 419 it calls the metamethod with the two operands as arguments, | 312 it calls the metamethod with the two operands as arguments, |
| 420 and the result of the call | 313 and the result of the call |
| 421 (adjusted to one value) | 314 (adjusted to one value) |
| 422 is the result of the operation. | 315 is the result of the operation. |
| 423 Otherwise, | 316 Otherwise, |
| 424 it raises an error. | 317 it raises an error. |
| 425 </li> | 318 </li> |
| 426 | 319 |
| 427 <li><b>"sub": </b> | 320 <li><b>"sub": </b> |
| 428 the <code>-</code> operation. | 321 the <tt>-</tt> operation. |
| 429 | 322 |
| 430 Behavior similar to the "add" operation. | 323 Behavior similar to the "add" operation. |
| 431 </li> | 324 </li> |
| 432 | 325 |
| 433 <li><b>"mul": </b> | 326 <li><b>"mul": </b> |
| 434 the <code>*</code> operation. | 327 the <tt>*</tt> operation. |
| 435 | 328 |
| 436 Behavior similar to the "add" operation. | 329 Behavior similar to the "add" operation. |
| 437 </li> | 330 </li> |
| 438 | 331 |
| 439 <li><b>"div": </b> | 332 <li><b>"div": </b> |
| 440 the <code>/</code> operation. | 333 the <tt>/</tt> operation. |
| 441 | 334 |
| 442 Behavior similar to the "add" operation. | 335 Behavior similar to the "add" operation. |
| 443 </li> | 336 </li> |
| 444 | 337 |
| 445 <li><b>"mod": </b> | 338 <li><b>"mod": </b> |
| 446 the <code>%</code> operation. | 339 the <tt>%</tt> operation. |
| 447 | 340 |
| 448 Behavior similar to the "add" operation. | 341 Behavior similar to the "add" operation. |
| 449 </li> | 342 </li> |
| 450 | 343 |
| 451 <li><b>"pow": </b> | 344 <li><b>"pow": </b> |
| 452 the <code>^</code> (exponentiation) operation. | 345 the <tt>^</tt> (exponentiation) operation. |
| 453 | 346 |
| 454 Behavior similar to the "add" operation. | 347 Behavior similar to the "add" operation. |
| 455 </li> | 348 </li> |
| 456 | 349 |
| 457 <li><b>"unm": </b> | 350 <li><b>"unm": </b> |
| 458 the <code>-</code> (unary minus) operation. | 351 the <tt>-</tt> (unary minus) operation. |
| 459 | 352 |
| 460 Behavior similar to the "add" operation. | 353 Behavior similar to the "add" operation. |
| 461 </li> | 354 </li> |
| 462 | 355 |
| 463 <li><b>"idiv": </b> | 356 <li><b>"concat": </b> |
| 464 the <code>//</code> (floor division) operation. | 357 the <tt>..</tt> (concatenation) operation. |
| 465 | 358 |
| 466 Behavior similar to the "add" operation. | 359 Behavior similar to the "add" operation. |
| 467 </li> | 360 </li> |
| 468 | 361 |
| 469 <li><b>"band": </b> | |
| 470 the <code>&</code> (bitwise and) operation. | |
| 471 | |
| 472 Behavior similar to the "add" operation, | |
| 473 except that Lua will try a metamethod | |
| 474 if any operator is neither an integer | |
| 475 nor a value coercible to an integer (see <a href="#3.4.3">§3.4.3</a>). | |
| 476 </li> | |
| 477 | |
| 478 <li><b>"bor": </b> | |
| 479 the <code>|</code> (bitwise or) operation. | |
| 480 | |
| 481 Behavior similar to the "band" operation. | |
| 482 </li> | |
| 483 | |
| 484 <li><b>"bxor": </b> | |
| 485 the <code>~</code> (bitwise exclusive or) operation. | |
| 486 | |
| 487 Behavior similar to the "band" operation. | |
| 488 </li> | |
| 489 | |
| 490 <li><b>"bnot": </b> | |
| 491 the <code>~</code> (bitwise unary not) operation. | |
| 492 | |
| 493 Behavior similar to the "band" operation. | |
| 494 </li> | |
| 495 | |
| 496 <li><b>"shl": </b> | |
| 497 the <code><<</code> (bitwise left shift) operation. | |
| 498 | |
| 499 Behavior similar to the "band" operation. | |
| 500 </li> | |
| 501 | |
| 502 <li><b>"shr": </b> | |
| 503 the <code>>></code> (bitwise right shift) operation. | |
| 504 | |
| 505 Behavior similar to the "band" operation. | |
| 506 </li> | |
| 507 | |
| 508 <li><b>"concat": </b> | |
| 509 the <code>..</code> (concatenation) operation. | |
| 510 | |
| 511 Behavior similar to the "add" operation, | |
| 512 except that Lua will try a metamethod | |
| 513 if any operator is neither a string nor a number | |
| 514 (which is always coercible to a string). | |
| 515 </li> | |
| 516 | |
| 517 <li><b>"len": </b> | 362 <li><b>"len": </b> |
| 518 the <code>#</code> (length) operation. | 363 the <tt>#</tt> (length) operation. |
| 519 | 364 |
| 520 If the object is not a string, | |
| 521 Lua will try its metamethod. | |
| 522 If there is a metamethod, | 365 If there is a metamethod, |
| 523 Lua calls it with the object as argument, | 366 Luan calls it with the object as argument, |
| 524 and the result of the call | 367 and the result of the call |
| 525 (always adjusted to one value) | 368 (always adjusted to one value) |
| 526 is the result of the operation. | 369 is the result of the operation. |
| 527 If there is no metamethod but the object is a table, | 370 If there is no metamethod but the object is a table, |
| 528 then Lua uses the table length operation (see <a href="#3.4.7">§3.4.7</a>). | 371 then Luan uses the table length operation (see <a href="#3.4.7">§3.4.7</a>). |
| 529 Otherwise, Lua raises an error. | 372 Otherwise, Luan raises an error. |
| 530 </li> | 373 </li> |
| 531 | 374 |
| 532 <li><b>"eq": </b> | 375 <li><b>"eq": </b> |
| 533 the <code>==</code> (equal) operation. | 376 the <tt>==</tt> (equal) operation. |
| 534 | 377 |
| 535 Behavior similar to the "add" operation, | 378 Behavior similar to the "add" operation, |
| 536 except that Lua will try a metamethod only when the values | 379 except that Luan will try a metamethod only when the values |
| 537 being compared are either both tables or both full userdata | 380 being compared are both tables |
| 538 and they are not primitively equal. | 381 and they are not primitively equal. |
| 539 The result of the call is always converted to a boolean. | 382 The result of the call is always converted to a boolean. |
| 540 </li> | 383 </li> |
| 541 | 384 |
| 542 <li><b>"lt": </b> | 385 <li><b>"lt": </b> |
| 543 the <code><</code> (less than) operation. | 386 the <tt><</tt> (less than) operation. |
| 544 | 387 |
| 545 Behavior similar to the "add" operation, | 388 Behavior similar to the "add" operation. |
| 546 except that Lua will try a metamethod only when the values | |
| 547 being compared are neither both numbers nor both strings. | |
| 548 The result of the call is always converted to a boolean. | 389 The result of the call is always converted to a boolean. |
| 549 </li> | 390 </li> |
| 550 | 391 |
| 551 <li><b>"le": </b> | 392 <li><b>"le": </b> |
| 552 the <code><=</code> (less equal) operation. | 393 the <tt><=</tt> (less equal) operation. |
| 553 | 394 |
| 554 Unlike other operations, | 395 Unlike other operations, |
| 555 The less-equal operation can use two different events. | 396 The less-equal operation can use two different events. |
| 556 First, Lua looks for the "<code>__le</code>" metamethod in both operands, | 397 First, Luan looks for the "<tt>__le</tt>" metamethod in both operands, |
| 557 like in the "lt" operation. | 398 like in the "lt" operation. |
| 558 If it cannot find such a metamethod, | 399 If it cannot find such a metamethod, |
| 559 then it will try the "<code>__lt</code>" event, | 400 then it will try the "<tt>__lt</tt>" event, |
| 560 assuming that <code>a <= b</code> is equivalent to <code>not (b < a)</code>. | 401 assuming that <tt>a <= b</tt> is equivalent to <tt>not (b < a)</tt>. |
| 561 As with the other comparison operators, | 402 As with the other comparison operators, |
| 562 the result is always a boolean. | 403 the result is always a boolean. |
| 563 </li> | 404 </li> |
| 564 | 405 |
| 565 <li><b>"index": </b> | 406 <li><b>"index": </b> |
| 566 The indexing access <code>table[key]</code>. | 407 The indexing access <tt>table[key]</tt>. |
| 567 | 408 |
| 568 This event happens when <code>table</code> is not a table or | 409 This event happens |
| 569 when <code>key</code> is not present in <code>table</code>. | 410 when <tt>key</tt> is not present in <tt>table</tt>. |
| 570 The metamethod is looked up in <code>table</code>. | 411 The metamethod is looked up in <tt>table</tt>. |
| 571 | 412 |
| 572 | 413 |
| 573 <p> | 414 <p> |
| 574 Despite the name, | 415 Despite the name, |
| 575 the metamethod for this event can be either a function or a table. | 416 the metamethod for this event can be either a function or a table. |
| 576 If it is a function, | 417 If it is a function, |
| 577 it is called with <code>table</code> and <code>key</code> as arguments. | 418 it is called with <tt>table</tt> and <tt>key</tt> as arguments. |
| 578 If it is a table, | 419 If it is a table, |
| 579 the final result is the result of indexing this table with <code>key</code>. | 420 the final result is the result of indexing this table with <tt>key</tt>. |
| 580 (This indexing is regular, not raw, | 421 (This indexing is regular, not raw, |
| 581 and therefore can trigger another metamethod.) | 422 and therefore can trigger another metamethod.) |
| 582 </li> | 423 </li> |
| 583 | 424 |
| 584 <li><b>"newindex": </b> | 425 <li><b>"newindex": </b> |
| 585 The indexing assignment <code>table[key] = value</code>. | 426 The indexing assignment <tt>table[key] = value</tt>. |
| 586 | 427 |
| 587 Like the index event, | 428 Like the index event, |
| 588 this event happens when <code>table</code> is not a table or | 429 this event happens when |
| 589 when <code>key</code> is not present in <code>table</code>. | 430 when <tt>key</tt> is not present in <tt>table</tt>. |
| 590 The metamethod is looked up in <code>table</code>. | 431 The metamethod is looked up in <tt>table</tt>. |
| 591 | 432 |
| 592 | 433 |
| 593 <p> | 434 <p> |
| 594 Like with indexing, | 435 Like with indexing, |
| 595 the metamethod for this event can be either a function or a table. | 436 the metamethod for this event can be either a function or a table. |
| 596 If it is a function, | 437 If it is a function, |
| 597 it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments. | 438 it is called with <tt>table</tt>, <tt>key</tt>, and <tt>value</tt> as arguments. |
| 598 If it is a table, | 439 If it is a table, |
| 599 Lua does an indexing assignment to this table with the same key and value. | 440 Luan does an indexing assignment to this table with the same key and value. |
| 600 (This assignment is regular, not raw, | 441 (This assignment is regular, not raw, |
| 601 and therefore can trigger another metamethod.) | 442 and therefore can trigger another metamethod.) |
| 602 | 443 |
| 603 | 444 |
| 604 <p> | 445 <p> |
| 605 Whenever there is a "newindex" metamethod, | 446 Whenever there is a "newindex" metamethod, |
| 606 Lua does not perform the primitive assignment. | 447 Luan does not perform the primitive assignment. |
| 607 (If necessary, | 448 (If necessary, |
| 608 the metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a> | 449 the metamethod itself can call <a href="#pdf-rawset"><tt>raw_set</tt></a> |
| 609 to do the assignment.) | 450 to do the assignment.) |
| 610 </li> | 451 </li> |
| 611 | 452 |
| 612 <li><b>"call": </b> | 453 <li><b>"call": </b> |
| 613 The call operation <code>func(args)</code>. | 454 The call operation <tt>func(args)</tt>. |
| 614 | 455 |
| 615 This event happens when Lua tries to call a non-function value | 456 This event happens when Luan tries to call a table. |
| 616 (that is, <code>func</code> is not a function). | 457 The metamethod is looked up in <tt>func</tt>. |
| 617 The metamethod is looked up in <code>func</code>. | |
| 618 If present, | 458 If present, |
| 619 the metamethod is called with <code>func</code> as its first argument, | 459 the metamethod is called with <tt>func</tt> as its first argument, |
| 620 followed by the arguments of the original call (<code>args</code>). | 460 followed by the arguments of the original call (<tt>args</tt>). |
| 621 </li> | 461 </li> |
| 622 | 462 |
| 623 </ul> | 463 </ul> |
| 624 | 464 |
| 625 | 465 |
