Mercurial Hosting > luan
comparison docs/manual.html @ 239:aa7bc9c1df22
add unedited Lua manual
git-svn-id: https://luan-java.googlecode.com/svn/trunk@240 21e917c8-12df-6dd8-5cb6-c86387c605b9
| author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
|---|---|
| date | Mon, 06 Oct 2014 22:03:16 +0000 |
| parents | |
| children | bcc3911285a2 |
comparison
equal
deleted
inserted
replaced
| 238:55b4b077e5cc | 239:aa7bc9c1df22 |
|---|---|
| 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | |
| 2 <html> | |
| 3 | |
| 4 <head> | |
| 5 <title>Lua 5.2 Reference Manual</title> | |
| 6 <link rel="stylesheet" type="text/css" href="http://www.lua.org/lua.css"> | |
| 7 <link rel="stylesheet" type="text/css" href="http://www.lua.org/manual/manual.css"> | |
| 8 <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1"> | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 | |
| 13 <hr> | |
| 14 <h1> | |
| 15 <a href="../../home.html"><img src="../../images/logo.gif" alt="" border="0"></a> | |
| 16 Lua 5.2 Reference Manual | |
| 17 </h1> | |
| 18 | |
| 19 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes | |
| 20 <p> | |
| 21 <small> | |
| 22 Copyright © 2011–2013 Lua.org, PUC-Rio. | |
| 23 Freely available under the terms of the | |
| 24 <a href="../../license.html">Lua license</a>. | |
| 25 </small> | |
| 26 <hr> | |
| 27 <p> | |
| 28 | |
| 29 <a href="contents.html#contents">contents</A> | |
| 30 · | |
| 31 <a href="contents.html#index">index</A> | |
| 32 · | |
| 33 <a href="../">other versions</A> | |
| 34 | |
| 35 <!-- ====================================================================== --> | |
| 36 <p> | |
| 37 | |
| 38 <!-- $Id: manual.of,v 1.103 2013/03/14 18:51:56 roberto Exp $ --> | |
| 39 | |
| 40 | |
| 41 <H2><A NAME="contents">Contents</A></H2> | |
| 42 <UL style="padding: 0"> | |
| 43 <LI><A HREF="manual.html#1">1 – Introduction</A> | |
| 44 <P> | |
| 45 <LI><A HREF="manual.html#2">2 – Basic Concepts</A> | |
| 46 <UL> | |
| 47 <LI><A HREF="manual.html#2.1">2.1 – Values and Types</A> | |
| 48 <LI><A HREF="manual.html#2.2">2.2 – Environments and the Global Environment</A> | |
| 49 <LI><A HREF="manual.html#2.3">2.3 – Error Handling</A> | |
| 50 <LI><A HREF="manual.html#2.4">2.4 – Metatables and Metamethods</A> | |
| 51 <LI><A HREF="manual.html#2.5">2.5 – Garbage Collection</A> | |
| 52 <UL> | |
| 53 <LI><A HREF="manual.html#2.5.1">2.5.1 – Garbage-Collection Metamethods</A> | |
| 54 <LI><A HREF="manual.html#2.5.2">2.5.2 – Weak Tables</A> | |
| 55 </UL> | |
| 56 <LI><A HREF="manual.html#2.6">2.6 – Coroutines</A> | |
| 57 </UL> | |
| 58 <P> | |
| 59 <LI><A HREF="manual.html#3">3 – The Language</A> | |
| 60 <UL> | |
| 61 <LI><A HREF="manual.html#3.1">3.1 – Lexical Conventions</A> | |
| 62 <LI><A HREF="manual.html#3.2">3.2 – Variables</A> | |
| 63 <LI><A HREF="manual.html#3.3">3.3 – Statements</A> | |
| 64 <UL> | |
| 65 <LI><A HREF="manual.html#3.3.1">3.3.1 – Blocks</A> | |
| 66 <LI><A HREF="manual.html#3.3.2">3.3.2 – Chunks</A> | |
| 67 <LI><A HREF="manual.html#3.3.3">3.3.3 – Assignment</A> | |
| 68 <LI><A HREF="manual.html#3.3.4">3.3.4 – Control Structures</A> | |
| 69 <LI><A HREF="manual.html#3.3.5">3.3.5 – For Statement</A> | |
| 70 <LI><A HREF="manual.html#3.3.6">3.3.6 – Function Calls as Statements</A> | |
| 71 <LI><A HREF="manual.html#3.3.7">3.3.7 – Local Declarations</A> | |
| 72 </UL> | |
| 73 <LI><A HREF="manual.html#3.4">3.4 – Expressions</A> | |
| 74 <UL> | |
| 75 <LI><A HREF="manual.html#3.4.1">3.4.1 – Arithmetic Operators</A> | |
| 76 <LI><A HREF="manual.html#3.4.2">3.4.2 – Coercion</A> | |
| 77 <LI><A HREF="manual.html#3.4.3">3.4.3 – Relational Operators</A> | |
| 78 <LI><A HREF="manual.html#3.4.4">3.4.4 – Logical Operators</A> | |
| 79 <LI><A HREF="manual.html#3.4.5">3.4.5 – Concatenation</A> | |
| 80 <LI><A HREF="manual.html#3.4.6">3.4.6 – The Length Operator</A> | |
| 81 <LI><A HREF="manual.html#3.4.7">3.4.7 – Precedence</A> | |
| 82 <LI><A HREF="manual.html#3.4.8">3.4.8 – Table Constructors</A> | |
| 83 <LI><A HREF="manual.html#3.4.9">3.4.9 – Function Calls</A> | |
| 84 <LI><A HREF="manual.html#3.4.10">3.4.10 – Function Definitions</A> | |
| 85 </UL> | |
| 86 <LI><A HREF="manual.html#3.5">3.5 – Visibility Rules</A> | |
| 87 </UL> | |
| 88 <P> | |
| 89 <LI><A HREF="manual.html#4">4 – The Application Program Interface</A> | |
| 90 <UL> | |
| 91 <LI><A HREF="manual.html#4.1">4.1 – The Stack</A> | |
| 92 <LI><A HREF="manual.html#4.2">4.2 – Stack Size</A> | |
| 93 <LI><A HREF="manual.html#4.3">4.3 – Valid and Acceptable Indices</A> | |
| 94 <LI><A HREF="manual.html#4.4">4.4 – C Closures</A> | |
| 95 <LI><A HREF="manual.html#4.5">4.5 – Registry</A> | |
| 96 <LI><A HREF="manual.html#4.6">4.6 – Error Handling in C</A> | |
| 97 <LI><A HREF="manual.html#4.7">4.7 – Handling Yields in C</A> | |
| 98 <LI><A HREF="manual.html#4.8">4.8 – Functions and Types</A> | |
| 99 <LI><A HREF="manual.html#4.9">4.9 – The Debug Interface</A> | |
| 100 </UL> | |
| 101 <P> | |
| 102 <LI><A HREF="manual.html#5">5 – The Auxiliary Library</A> | |
| 103 <UL> | |
| 104 <LI><A HREF="manual.html#5.1">5.1 – Functions and Types</A> | |
| 105 </UL> | |
| 106 <P> | |
| 107 <LI><A HREF="manual.html#6">6 – Standard Libraries</A> | |
| 108 <UL> | |
| 109 <LI><A HREF="manual.html#6.1">6.1 – Basic Functions</A> | |
| 110 <LI><A HREF="manual.html#6.2">6.2 – Coroutine Manipulation</A> | |
| 111 <LI><A HREF="manual.html#6.3">6.3 – Modules</A> | |
| 112 <LI><A HREF="manual.html#6.4">6.4 – String Manipulation</A> | |
| 113 <UL> | |
| 114 <LI><A HREF="manual.html#6.4.1">6.4.1 – Patterns</A> | |
| 115 </UL> | |
| 116 <LI><A HREF="manual.html#6.5">6.5 – Table Manipulation</A> | |
| 117 <LI><A HREF="manual.html#6.6">6.6 – Mathematical Functions</A> | |
| 118 <LI><A HREF="manual.html#6.7">6.7 – Bitwise Operations</A> | |
| 119 <LI><A HREF="manual.html#6.8">6.8 – Input and Output Facilities</A> | |
| 120 <LI><A HREF="manual.html#6.9">6.9 – Operating System Facilities</A> | |
| 121 <LI><A HREF="manual.html#6.10">6.10 – The Debug Library</A> | |
| 122 </UL> | |
| 123 <P> | |
| 124 <LI><A HREF="manual.html#7">7 – Lua Standalone</A> | |
| 125 <P> | |
| 126 <LI><A HREF="manual.html#8">8 – Incompatibilities with the Previous Version</A> | |
| 127 <UL> | |
| 128 <LI><A HREF="manual.html#8.1">8.1 – Changes in the Language</A> | |
| 129 <LI><A HREF="manual.html#8.2">8.2 – Changes in the Libraries</A> | |
| 130 <LI><A HREF="manual.html#8.3">8.3 – Changes in the API</A> | |
| 131 </UL> | |
| 132 <P> | |
| 133 <LI><A HREF="manual.html#9">9 – The Complete Syntax of Lua</A> | |
| 134 </UL> | |
| 135 | |
| 136 | |
| 137 <h1>1 – <a name="1">Introduction</a></h1> | |
| 138 | |
| 139 <p> | |
| 140 Lua is an extension programming language designed to support | |
| 141 general procedural programming with data description | |
| 142 facilities. | |
| 143 It also offers good support for object-oriented programming, | |
| 144 functional programming, and data-driven programming. | |
| 145 Lua is intended to be used as a powerful, lightweight, | |
| 146 embeddable scripting language for any program that needs one. | |
| 147 Lua is implemented as a library, written in <em>clean C</em>, | |
| 148 the common subset of Standard C and C++. | |
| 149 | |
| 150 | |
| 151 <p> | |
| 152 Being an extension language, Lua has no notion of a "main" program: | |
| 153 it only works <em>embedded</em> in a host client, | |
| 154 called the <em>embedding program</em> or simply the <em>host</em>. | |
| 155 The host program can invoke functions to execute a piece of Lua code, | |
| 156 can write and read Lua variables, | |
| 157 and can register C functions to be called by Lua code. | |
| 158 Through the use of C functions, Lua can be augmented to cope with | |
| 159 a wide range of different domains, | |
| 160 thus creating customized programming languages sharing a syntactical framework. | |
| 161 The Lua distribution includes a sample host program called <code>lua</code>, | |
| 162 which uses the Lua library to offer a complete, standalone Lua interpreter, | |
| 163 for interactive or batch use. | |
| 164 | |
| 165 | |
| 166 <p> | |
| 167 Lua is free software, | |
| 168 and is provided as usual with no guarantees, | |
| 169 as stated in its license. | |
| 170 The implementation described in this manual is available | |
| 171 at Lua's official web site, <code>www.lua.org</code>. | |
| 172 | |
| 173 | |
| 174 <p> | |
| 175 Like any other reference manual, | |
| 176 this document is dry in places. | |
| 177 For a discussion of the decisions behind the design of Lua, | |
| 178 see the technical papers available at Lua's web site. | |
| 179 For a detailed introduction to programming in Lua, | |
| 180 see Roberto's book, <em>Programming in Lua</em>. | |
| 181 | |
| 182 | |
| 183 | |
| 184 <h1>2 – <a name="2">Basic Concepts</a></h1> | |
| 185 | |
| 186 <p> | |
| 187 This section describes the basic concepts of the language. | |
| 188 | |
| 189 | |
| 190 | |
| 191 <h2>2.1 – <a name="2.1">Values and Types</a></h2> | |
| 192 | |
| 193 <p> | |
| 194 Lua is a <em>dynamically typed language</em>. | |
| 195 This means that | |
| 196 variables do not have types; only values do. | |
| 197 There are no type definitions in the language. | |
| 198 All values carry their own type. | |
| 199 | |
| 200 | |
| 201 <p> | |
| 202 All values in Lua are <em>first-class values</em>. | |
| 203 This means that all values can be stored in variables, | |
| 204 passed as arguments to other functions, and returned as results. | |
| 205 | |
| 206 | |
| 207 <p> | |
| 208 There are eight basic types in Lua: | |
| 209 <em>nil</em>, <em>boolean</em>, <em>number</em>, | |
| 210 <em>string</em>, <em>function</em>, <em>userdata</em>, | |
| 211 <em>thread</em>, and <em>table</em>. | |
| 212 <em>Nil</em> is the type of the value <b>nil</b>, | |
| 213 whose main property is to be different from any other value; | |
| 214 it usually represents the absence of a useful value. | |
| 215 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>. | |
| 216 Both <b>nil</b> and <b>false</b> make a condition false; | |
| 217 any other value makes it true. | |
| 218 <em>Number</em> represents real (double-precision floating-point) numbers. | |
| 219 Operations on numbers follow the same rules of | |
| 220 the underlying C implementation, | |
| 221 which, in turn, usually follows the IEEE 754 standard. | |
| 222 (It is easy to build Lua interpreters that use other | |
| 223 internal representations for numbers, | |
| 224 such as single-precision floats or long integers; | |
| 225 see file <code>luaconf.h</code>.) | |
| 226 <em>String</em> represents immutable sequences of bytes. | |
| 227 | |
| 228 Lua is 8-bit clean: | |
| 229 strings can contain any 8-bit value, | |
| 230 including embedded zeros ('<code>\0</code>'). | |
| 231 | |
| 232 | |
| 233 <p> | |
| 234 Lua can call (and manipulate) functions written in Lua and | |
| 235 functions written in C | |
| 236 (see <a href="#3.4.9">§3.4.9</a>). | |
| 237 | |
| 238 | |
| 239 <p> | |
| 240 The type <em>userdata</em> is provided to allow arbitrary C data to | |
| 241 be stored in Lua variables. | |
| 242 A userdata value is a pointer to a block of raw memory. | |
| 243 There are two kinds of userdata: | |
| 244 full userdata, where the block of memory is managed by Lua, | |
| 245 and light userdata, where the block of memory is managed by the host. | |
| 246 Userdata has no predefined operations in Lua, | |
| 247 except assignment and identity test. | |
| 248 By using <em>metatables</em>, | |
| 249 the programmer can define operations for full userdata values | |
| 250 (see <a href="#2.4">§2.4</a>). | |
| 251 Userdata values cannot be created or modified in Lua, | |
| 252 only through the C API. | |
| 253 This guarantees the integrity of data owned by the host program. | |
| 254 | |
| 255 | |
| 256 <p> | |
| 257 The type <em>thread</em> represents independent threads of execution | |
| 258 and it is used to implement coroutines (see <a href="#2.6">§2.6</a>). | |
| 259 Do not confuse Lua threads with operating-system threads. | |
| 260 Lua supports coroutines on all systems, | |
| 261 even those that do not support threads. | |
| 262 | |
| 263 | |
| 264 <p> | |
| 265 The type <em>table</em> implements associative arrays, | |
| 266 that is, arrays that can be indexed not only with numbers, | |
| 267 but with any Lua value except <b>nil</b> and NaN | |
| 268 (<em>Not a Number</em>, a special numeric value used to represent | |
| 269 undefined or unrepresentable results, such as <code>0/0</code>). | |
| 270 Tables can be <em>heterogeneous</em>; | |
| 271 that is, they can contain values of all types (except <b>nil</b>). | |
| 272 Any key with value <b>nil</b> is not considered part of the table. | |
| 273 Conversely, any key that is not part of a table has | |
| 274 an associated value <b>nil</b>. | |
| 275 | |
| 276 | |
| 277 <p> | |
| 278 Tables are the sole data structuring mechanism in Lua; | |
| 279 they can be used to represent ordinary arrays, sequences, | |
| 280 symbol tables, sets, records, graphs, trees, etc. | |
| 281 To represent records, Lua uses the field name as an index. | |
| 282 The language supports this representation by | |
| 283 providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>. | |
| 284 There are several convenient ways to create tables in Lua | |
| 285 (see <a href="#3.4.8">§3.4.8</a>). | |
| 286 | |
| 287 | |
| 288 <p> | |
| 289 We use the term <em>sequence</em> to denote a table where | |
| 290 the set of all positive numeric keys is equal to <em>{1..n}</em> | |
| 291 for some integer <em>n</em>, | |
| 292 which is called the length of the sequence (see <a href="#3.4.6">§3.4.6</a>). | |
| 293 | |
| 294 | |
| 295 <p> | |
| 296 Like indices, | |
| 297 the values of table fields can be of any type. | |
| 298 In particular, | |
| 299 because functions are first-class values, | |
| 300 table fields can contain functions. | |
| 301 Thus tables can also carry <em>methods</em> (see <a href="#3.4.10">§3.4.10</a>). | |
| 302 | |
| 303 | |
| 304 <p> | |
| 305 The indexing of tables follows | |
| 306 the definition of raw equality in the language. | |
| 307 The expressions <code>a[i]</code> and <code>a[j]</code> | |
| 308 denote the same table element | |
| 309 if and only if <code>i</code> and <code>j</code> are raw equal | |
| 310 (that is, equal without metamethods). | |
| 311 | |
| 312 | |
| 313 <p> | |
| 314 Tables, functions, threads, and (full) userdata values are <em>objects</em>: | |
| 315 variables do not actually <em>contain</em> these values, | |
| 316 only <em>references</em> to them. | |
| 317 Assignment, parameter passing, and function returns | |
| 318 always manipulate references to such values; | |
| 319 these operations do not imply any kind of copy. | |
| 320 | |
| 321 | |
| 322 <p> | |
| 323 The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type | |
| 324 of a given value (see <a href="#6.1">§6.1</a>). | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 <h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2> | |
| 331 | |
| 332 <p> | |
| 333 As will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>, | |
| 334 any reference to a global name <code>var</code> is syntactically translated | |
| 335 to <code>_ENV.var</code>. | |
| 336 Moreover, every chunk is compiled in the scope of | |
| 337 an external local variable called <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>), | |
| 338 so <code>_ENV</code> itself is never a global name in a chunk. | |
| 339 | |
| 340 | |
| 341 <p> | |
| 342 Despite the existence of this external <code>_ENV</code> variable and | |
| 343 the translation of global names, | |
| 344 <code>_ENV</code> is a completely regular name. | |
| 345 In particular, | |
| 346 you can define new variables and parameters with that name. | |
| 347 Each reference to a global name uses the <code>_ENV</code> that is | |
| 348 visible at that point in the program, | |
| 349 following the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>). | |
| 350 | |
| 351 | |
| 352 <p> | |
| 353 Any table used as the value of <code>_ENV</code> is called an <em>environment</em>. | |
| 354 | |
| 355 | |
| 356 <p> | |
| 357 Lua keeps a distinguished environment called the <em>global environment</em>. | |
| 358 This value is kept at a special index in the C registry (see <a href="#4.5">§4.5</a>). | |
| 359 In Lua, the variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value. | |
| 360 | |
| 361 | |
| 362 <p> | |
| 363 When Lua compiles a chunk, | |
| 364 it initializes the value of its <code>_ENV</code> upvalue | |
| 365 with the global environment (see <a href="#pdf-load"><code>load</code></a>). | |
| 366 Therefore, by default, | |
| 367 global variables in Lua code refer to entries in the global environment. | |
| 368 Moreover, all standard libraries are loaded in the global environment | |
| 369 and several functions there operate on that environment. | |
| 370 You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>) | |
| 371 to load a chunk with a different environment. | |
| 372 (In C, you have to load the chunk and then change the value | |
| 373 of its first upvalue.) | |
| 374 | |
| 375 | |
| 376 <p> | |
| 377 If you change the global environment in the registry | |
| 378 (through C code or the debug library), | |
| 379 all chunks loaded after the change will get the new environment. | |
| 380 Previously loaded chunks are not affected, however, | |
| 381 as each has its own reference to the environment in its <code>_ENV</code> variable. | |
| 382 Moreover, the variable <a href="#pdf-_G"><code>_G</code></a> | |
| 383 (which is stored in the original global environment) | |
| 384 is never updated by Lua. | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 <h2>2.3 – <a name="2.3">Error Handling</a></h2> | |
| 391 | |
| 392 <p> | |
| 393 Because Lua is an embedded extension language, | |
| 394 all Lua actions start from C code in the host program | |
| 395 calling a function from the Lua library (see <a href="#lua_pcall"><code>lua_pcall</code></a>). | |
| 396 Whenever an error occurs during | |
| 397 the compilation or execution of a Lua chunk, | |
| 398 control returns to the host, | |
| 399 which can take appropriate measures | |
| 400 (such as printing an error message). | |
| 401 | |
| 402 | |
| 403 <p> | |
| 404 Lua code can explicitly generate an error by calling the | |
| 405 <a href="#pdf-error"><code>error</code></a> function. | |
| 406 If you need to catch errors in Lua, | |
| 407 you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a> | |
| 408 to call a given function in <em>protected mode</em>. | |
| 409 | |
| 410 | |
| 411 <p> | |
| 412 Whenever there is an error, | |
| 413 an <em>error object</em> (also called an <em>error message</em>) | |
| 414 is propagated with information about the error. | |
| 415 Lua itself only generates errors where the error object is a string, | |
| 416 but programs may generate errors with | |
| 417 any value for the error object. | |
| 418 | |
| 419 | |
| 420 <p> | |
| 421 When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>, | |
| 422 you may give a <em>message handler</em> | |
| 423 to be called in case of errors. | |
| 424 This function is called with the original error message | |
| 425 and returns a new error message. | |
| 426 It is called before the error unwinds the stack, | |
| 427 so that it can gather more information about the error, | |
| 428 for instance by inspecting the stack and creating a stack traceback. | |
| 429 This message handler is still protected by the protected call; | |
| 430 so, an error inside the message handler | |
| 431 will call the message handler again. | |
| 432 If this loop goes on, Lua breaks it and returns an appropriate message. | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 <h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2> | |
| 439 | |
| 440 <p> | |
| 441 Every value in Lua can have a <em>metatable</em>. | |
| 442 This <em>metatable</em> is an ordinary Lua table | |
| 443 that defines the behavior of the original value | |
| 444 under certain special operations. | |
| 445 You can change several aspects of the behavior | |
| 446 of operations over a value by setting specific fields in its metatable. | |
| 447 For instance, when a non-numeric value is the operand of an addition, | |
| 448 Lua checks for a function in the field "<code>__add</code>" of the value's metatable. | |
| 449 If it finds one, | |
| 450 Lua calls this function to perform the addition. | |
| 451 | |
| 452 | |
| 453 <p> | |
| 454 The keys in a metatable are derived from the <em>event</em> names; | |
| 455 the corresponding values are called <em>metamethods</em>. | |
| 456 In the previous example, the event is <code>"add"</code> | |
| 457 and the metamethod is the function that performs the addition. | |
| 458 | |
| 459 | |
| 460 <p> | |
| 461 You can query the metatable of any value | |
| 462 using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. | |
| 463 | |
| 464 | |
| 465 <p> | |
| 466 You can replace the metatable of tables | |
| 467 using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. | |
| 468 You cannot change the metatable of other types from Lua | |
| 469 (except by using the debug library); | |
| 470 you must use the C API for that. | |
| 471 | |
| 472 | |
| 473 <p> | |
| 474 Tables and full userdata have individual metatables | |
| 475 (although multiple tables and userdata can share their metatables). | |
| 476 Values of all other types share one single metatable per type; | |
| 477 that is, there is one single metatable for all numbers, | |
| 478 one for all strings, etc. | |
| 479 By default, a value has no metatable, | |
| 480 but the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>). | |
| 481 | |
| 482 | |
| 483 <p> | |
| 484 A metatable controls how an object behaves in arithmetic operations, | |
| 485 order comparisons, concatenation, length operation, and indexing. | |
| 486 A metatable also can define a function to be called | |
| 487 when a userdata or a table is garbage collected. | |
| 488 When Lua performs one of these operations over a value, | |
| 489 it checks whether this value has a metatable with the corresponding event. | |
| 490 If so, the value associated with that key (the metamethod) | |
| 491 controls how Lua will perform the operation. | |
| 492 | |
| 493 | |
| 494 <p> | |
| 495 Metatables control the operations listed next. | |
| 496 Each operation is identified by its corresponding name. | |
| 497 The key for each operation is a string with its name prefixed by | |
| 498 two underscores, '<code>__</code>'; | |
| 499 for instance, the key for operation "add" is the | |
| 500 string "<code>__add</code>". | |
| 501 | |
| 502 | |
| 503 <p> | |
| 504 The semantics of these operations is better explained by a Lua function | |
| 505 describing how the interpreter executes the operation. | |
| 506 The code shown here in Lua is only illustrative; | |
| 507 the real behavior is hard coded in the interpreter | |
| 508 and it is much more efficient than this simulation. | |
| 509 All functions used in these descriptions | |
| 510 (<a href="#pdf-rawget"><code>rawget</code></a>, <a href="#pdf-tonumber"><code>tonumber</code></a>, etc.) | |
| 511 are described in <a href="#6.1">§6.1</a>. | |
| 512 In particular, to retrieve the metamethod of a given object, | |
| 513 we use the expression | |
| 514 | |
| 515 <pre> | |
| 516 metatable(obj)[event] | |
| 517 </pre><p> | |
| 518 This should be read as | |
| 519 | |
| 520 <pre> | |
| 521 rawget(getmetatable(obj) or {}, event) | |
| 522 </pre><p> | |
| 523 This means that the access to a metamethod does not invoke other metamethods, | |
| 524 and access to objects with no metatables does not fail | |
| 525 (it simply results in <b>nil</b>). | |
| 526 | |
| 527 | |
| 528 <p> | |
| 529 For the unary <code>-</code> and <code>#</code> operators, | |
| 530 the metamethod is called with a dummy second argument. | |
| 531 This extra argument is only to simplify Lua's internals; | |
| 532 it may be removed in future versions and therefore it is not present | |
| 533 in the following code. | |
| 534 (For most uses this extra argument is irrelevant.) | |
| 535 | |
| 536 | |
| 537 | |
| 538 <ul> | |
| 539 | |
| 540 <li><b>"add": </b> | |
| 541 the <code>+</code> operation. | |
| 542 | |
| 543 | |
| 544 | |
| 545 <p> | |
| 546 The function <code>getbinhandler</code> below defines how Lua chooses a handler | |
| 547 for a binary operation. | |
| 548 First, Lua tries the first operand. | |
| 549 If its type does not define a handler for the operation, | |
| 550 then Lua tries the second operand. | |
| 551 | |
| 552 <pre> | |
| 553 function getbinhandler (op1, op2, event) | |
| 554 return metatable(op1)[event] or metatable(op2)[event] | |
| 555 end | |
| 556 </pre><p> | |
| 557 By using this function, | |
| 558 the behavior of the <code>op1 + op2</code> is | |
| 559 | |
| 560 <pre> | |
| 561 function add_event (op1, op2) | |
| 562 local o1, o2 = tonumber(op1), tonumber(op2) | |
| 563 if o1 and o2 then -- both operands are numeric? | |
| 564 return o1 + o2 -- '+' here is the primitive 'add' | |
| 565 else -- at least one of the operands is not numeric | |
| 566 local h = getbinhandler(op1, op2, "__add") | |
| 567 if h then | |
| 568 -- call the handler with both operands | |
| 569 return (h(op1, op2)) | |
| 570 else -- no handler available: default behavior | |
| 571 error(···) | |
| 572 end | |
| 573 end | |
| 574 end | |
| 575 </pre><p> | |
| 576 </li> | |
| 577 | |
| 578 <li><b>"sub": </b> | |
| 579 the <code>-</code> operation. | |
| 580 | |
| 581 Behavior similar to the "add" operation. | |
| 582 </li> | |
| 583 | |
| 584 <li><b>"mul": </b> | |
| 585 the <code>*</code> operation. | |
| 586 | |
| 587 Behavior similar to the "add" operation. | |
| 588 </li> | |
| 589 | |
| 590 <li><b>"div": </b> | |
| 591 the <code>/</code> operation. | |
| 592 | |
| 593 Behavior similar to the "add" operation. | |
| 594 </li> | |
| 595 | |
| 596 <li><b>"mod": </b> | |
| 597 the <code>%</code> operation. | |
| 598 | |
| 599 Behavior similar to the "add" operation, | |
| 600 with the operation | |
| 601 <code>o1 - floor(o1/o2)*o2</code> as the primitive operation. | |
| 602 </li> | |
| 603 | |
| 604 <li><b>"pow": </b> | |
| 605 the <code>^</code> (exponentiation) operation. | |
| 606 | |
| 607 Behavior similar to the "add" operation, | |
| 608 with the function <code>pow</code> (from the C math library) | |
| 609 as the primitive operation. | |
| 610 </li> | |
| 611 | |
| 612 <li><b>"unm": </b> | |
| 613 the unary <code>-</code> operation. | |
| 614 | |
| 615 | |
| 616 <pre> | |
| 617 function unm_event (op) | |
| 618 local o = tonumber(op) | |
| 619 if o then -- operand is numeric? | |
| 620 return -o -- '-' here is the primitive 'unm' | |
| 621 else -- the operand is not numeric. | |
| 622 -- Try to get a handler from the operand | |
| 623 local h = metatable(op).__unm | |
| 624 if h then | |
| 625 -- call the handler with the operand | |
| 626 return (h(op)) | |
| 627 else -- no handler available: default behavior | |
| 628 error(···) | |
| 629 end | |
| 630 end | |
| 631 end | |
| 632 </pre><p> | |
| 633 </li> | |
| 634 | |
| 635 <li><b>"concat": </b> | |
| 636 the <code>..</code> (concatenation) operation. | |
| 637 | |
| 638 | |
| 639 <pre> | |
| 640 function concat_event (op1, op2) | |
| 641 if (type(op1) == "string" or type(op1) == "number") and | |
| 642 (type(op2) == "string" or type(op2) == "number") then | |
| 643 return op1 .. op2 -- primitive string concatenation | |
| 644 else | |
| 645 local h = getbinhandler(op1, op2, "__concat") | |
| 646 if h then | |
| 647 return (h(op1, op2)) | |
| 648 else | |
| 649 error(···) | |
| 650 end | |
| 651 end | |
| 652 end | |
| 653 </pre><p> | |
| 654 </li> | |
| 655 | |
| 656 <li><b>"len": </b> | |
| 657 the <code>#</code> operation. | |
| 658 | |
| 659 | |
| 660 <pre> | |
| 661 function len_event (op) | |
| 662 if type(op) == "string" then | |
| 663 return strlen(op) -- primitive string length | |
| 664 else | |
| 665 local h = metatable(op).__len | |
| 666 if h then | |
| 667 return (h(op)) -- call handler with the operand | |
| 668 elseif type(op) == "table" then | |
| 669 return #op -- primitive table length | |
| 670 else -- no handler available: error | |
| 671 error(···) | |
| 672 end | |
| 673 end | |
| 674 end | |
| 675 </pre><p> | |
| 676 See <a href="#3.4.6">§3.4.6</a> for a description of the length of a table. | |
| 677 </li> | |
| 678 | |
| 679 <li><b>"eq": </b> | |
| 680 the <code>==</code> operation. | |
| 681 | |
| 682 The function <code>getequalhandler</code> defines how Lua chooses a metamethod | |
| 683 for equality. | |
| 684 A metamethod is selected only when both values | |
| 685 being compared have the same type | |
| 686 and the same metamethod for the selected operation, | |
| 687 and the values are either tables or full userdata. | |
| 688 | |
| 689 <pre> | |
| 690 function getequalhandler (op1, op2) | |
| 691 if type(op1) ~= type(op2) or | |
| 692 (type(op1) ~= "table" and type(op1) ~= "userdata") then | |
| 693 return nil -- different values | |
| 694 end | |
| 695 local mm1 = metatable(op1).__eq | |
| 696 local mm2 = metatable(op2).__eq | |
| 697 if mm1 == mm2 then return mm1 else return nil end | |
| 698 end | |
| 699 </pre><p> | |
| 700 The "eq" event is defined as follows: | |
| 701 | |
| 702 <pre> | |
| 703 function eq_event (op1, op2) | |
| 704 if op1 == op2 then -- primitive equal? | |
| 705 return true -- values are equal | |
| 706 end | |
| 707 -- try metamethod | |
| 708 local h = getequalhandler(op1, op2) | |
| 709 if h then | |
| 710 return not not h(op1, op2) | |
| 711 else | |
| 712 return false | |
| 713 end | |
| 714 end | |
| 715 </pre><p> | |
| 716 Note that the result is always a boolean. | |
| 717 </li> | |
| 718 | |
| 719 <li><b>"lt": </b> | |
| 720 the <code><</code> operation. | |
| 721 | |
| 722 | |
| 723 <pre> | |
| 724 function lt_event (op1, op2) | |
| 725 if type(op1) == "number" and type(op2) == "number" then | |
| 726 return op1 < op2 -- numeric comparison | |
| 727 elseif type(op1) == "string" and type(op2) == "string" then | |
| 728 return op1 < op2 -- lexicographic comparison | |
| 729 else | |
| 730 local h = getbinhandler(op1, op2, "__lt") | |
| 731 if h then | |
| 732 return not not h(op1, op2) | |
| 733 else | |
| 734 error(···) | |
| 735 end | |
| 736 end | |
| 737 end | |
| 738 </pre><p> | |
| 739 Note that the result is always a boolean. | |
| 740 </li> | |
| 741 | |
| 742 <li><b>"le": </b> | |
| 743 the <code><=</code> operation. | |
| 744 | |
| 745 | |
| 746 <pre> | |
| 747 function le_event (op1, op2) | |
| 748 if type(op1) == "number" and type(op2) == "number" then | |
| 749 return op1 <= op2 -- numeric comparison | |
| 750 elseif type(op1) == "string" and type(op2) == "string" then | |
| 751 return op1 <= op2 -- lexicographic comparison | |
| 752 else | |
| 753 local h = getbinhandler(op1, op2, "__le") | |
| 754 if h then | |
| 755 return not not h(op1, op2) | |
| 756 else | |
| 757 h = getbinhandler(op1, op2, "__lt") | |
| 758 if h then | |
| 759 return not h(op2, op1) | |
| 760 else | |
| 761 error(···) | |
| 762 end | |
| 763 end | |
| 764 end | |
| 765 end | |
| 766 </pre><p> | |
| 767 Note that, in the absence of a "le" metamethod, | |
| 768 Lua tries the "lt", assuming that <code>a <= b</code> is | |
| 769 equivalent to <code>not (b < a)</code>. | |
| 770 | |
| 771 | |
| 772 <p> | |
| 773 As with the other comparison operators, | |
| 774 the result is always a boolean. | |
| 775 </li> | |
| 776 | |
| 777 <li><b>"index": </b> | |
| 778 The indexing access <code>table[key]</code>. | |
| 779 Note that the metamethod is tried only | |
| 780 when <code>key</code> is not present in <code>table</code>. | |
| 781 (When <code>table</code> is not a table, | |
| 782 no key is ever present, | |
| 783 so the metamethod is always tried.) | |
| 784 | |
| 785 | |
| 786 <pre> | |
| 787 function gettable_event (table, key) | |
| 788 local h | |
| 789 if type(table) == "table" then | |
| 790 local v = rawget(table, key) | |
| 791 -- if key is present, return raw value | |
| 792 if v ~= nil then return v end | |
| 793 h = metatable(table).__index | |
| 794 if h == nil then return nil end | |
| 795 else | |
| 796 h = metatable(table).__index | |
| 797 if h == nil then | |
| 798 error(···) | |
| 799 end | |
| 800 end | |
| 801 if type(h) == "function" then | |
| 802 return (h(table, key)) -- call the handler | |
| 803 else return h[key] -- or repeat operation on it | |
| 804 end | |
| 805 end | |
| 806 </pre><p> | |
| 807 </li> | |
| 808 | |
| 809 <li><b>"newindex": </b> | |
| 810 The indexing assignment <code>table[key] = value</code>. | |
| 811 Note that the metamethod is tried only | |
| 812 when <code>key</code> is not present in <code>table</code>. | |
| 813 | |
| 814 | |
| 815 <pre> | |
| 816 function settable_event (table, key, value) | |
| 817 local h | |
| 818 if type(table) == "table" then | |
| 819 local v = rawget(table, key) | |
| 820 -- if key is present, do raw assignment | |
| 821 if v ~= nil then rawset(table, key, value); return end | |
| 822 h = metatable(table).__newindex | |
| 823 if h == nil then rawset(table, key, value); return end | |
| 824 else | |
| 825 h = metatable(table).__newindex | |
| 826 if h == nil then | |
| 827 error(···) | |
| 828 end | |
| 829 end | |
| 830 if type(h) == "function" then | |
| 831 h(table, key,value) -- call the handler | |
| 832 else h[key] = value -- or repeat operation on it | |
| 833 end | |
| 834 end | |
| 835 </pre><p> | |
| 836 </li> | |
| 837 | |
| 838 <li><b>"call": </b> | |
| 839 called when Lua calls a value. | |
| 840 | |
| 841 | |
| 842 <pre> | |
| 843 function function_event (func, ...) | |
| 844 if type(func) == "function" then | |
| 845 return func(...) -- primitive call | |
| 846 else | |
| 847 local h = metatable(func).__call | |
| 848 if h then | |
| 849 return h(func, ...) | |
| 850 else | |
| 851 error(···) | |
| 852 end | |
| 853 end | |
| 854 end | |
| 855 </pre><p> | |
| 856 </li> | |
| 857 | |
| 858 </ul> | |
| 859 | |
| 860 | |
| 861 | |
| 862 | |
| 863 <h2>2.5 – <a name="2.5">Garbage Collection</a></h2> | |
| 864 | |
| 865 <p> | |
| 866 Lua performs automatic memory management. | |
| 867 This means that | |
| 868 you have to worry neither about allocating memory for new objects | |
| 869 nor about freeing it when the objects are no longer needed. | |
| 870 Lua manages memory automatically by running | |
| 871 a <em>garbage collector</em> to collect all <em>dead objects</em> | |
| 872 (that is, objects that are no longer accessible from Lua). | |
| 873 All memory used by Lua is subject to automatic management: | |
| 874 strings, tables, userdata, functions, threads, internal structures, etc. | |
| 875 | |
| 876 | |
| 877 <p> | |
| 878 Lua implements an incremental mark-and-sweep collector. | |
| 879 It uses two numbers to control its garbage-collection cycles: | |
| 880 the <em>garbage-collector pause</em> and | |
| 881 the <em>garbage-collector step multiplier</em>. | |
| 882 Both use percentage points as units | |
| 883 (e.g., a value of 100 means an internal value of 1). | |
| 884 | |
| 885 | |
| 886 <p> | |
| 887 The garbage-collector pause | |
| 888 controls how long the collector waits before starting a new cycle. | |
| 889 Larger values make the collector less aggressive. | |
| 890 Values smaller than 100 mean the collector will not wait to | |
| 891 start a new cycle. | |
| 892 A value of 200 means that the collector waits for the total memory in use | |
| 893 to double before starting a new cycle. | |
| 894 | |
| 895 | |
| 896 <p> | |
| 897 The garbage-collector step multiplier | |
| 898 controls the relative speed of the collector relative to | |
| 899 memory allocation. | |
| 900 Larger values make the collector more aggressive but also increase | |
| 901 the size of each incremental step. | |
| 902 Values smaller than 100 make the collector too slow and | |
| 903 can result in the collector never finishing a cycle. | |
| 904 The default is 200, | |
| 905 which means that the collector runs at "twice" | |
| 906 the speed of memory allocation. | |
| 907 | |
| 908 | |
| 909 <p> | |
| 910 If you set the step multiplier to a very large number | |
| 911 (larger than 10% of the maximum number of | |
| 912 bytes that the program may use), | |
| 913 the collector behaves like a stop-the-world collector. | |
| 914 If you then set the pause to 200, | |
| 915 the collector behaves as in old Lua versions, | |
| 916 doing a complete collection every time Lua doubles its | |
| 917 memory usage. | |
| 918 | |
| 919 | |
| 920 <p> | |
| 921 You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C | |
| 922 or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua. | |
| 923 You can also use these functions to control | |
| 924 the collector directly (e.g., stop and restart it). | |
| 925 | |
| 926 | |
| 927 <p> | |
| 928 As an experimental feature in Lua 5.2, | |
| 929 you can change the collector's operation mode | |
| 930 from incremental to <em>generational</em>. | |
| 931 A <em>generational collector</em> assumes that most objects die young, | |
| 932 and therefore it traverses only young (recently created) objects. | |
| 933 This behavior can reduce the time used by the collector, | |
| 934 but also increases memory usage (as old dead objects may accumulate). | |
| 935 To mitigate this second problem, | |
| 936 from time to time the generational collector performs a full collection. | |
| 937 Remember that this is an experimental feature; | |
| 938 you are welcome to try it, | |
| 939 but check your gains. | |
| 940 | |
| 941 | |
| 942 | |
| 943 <h3>2.5.1 – <a name="2.5.1">Garbage-Collection Metamethods</a></h3> | |
| 944 | |
| 945 <p> | |
| 946 You can set garbage-collector metamethods for tables | |
| 947 and, using the C API, | |
| 948 for full userdata (see <a href="#2.4">§2.4</a>). | |
| 949 These metamethods are also called <em>finalizers</em>. | |
| 950 Finalizers allow you to coordinate Lua's garbage collection | |
| 951 with external resource management | |
| 952 (such as closing files, network or database connections, | |
| 953 or freeing your own memory). | |
| 954 | |
| 955 | |
| 956 <p> | |
| 957 For an object (table or userdata) to be finalized when collected, | |
| 958 you must <em>mark</em> it for finalization. | |
| 959 | |
| 960 You mark an object for finalization when you set its metatable | |
| 961 and the metatable has a field indexed by the string "<code>__gc</code>". | |
| 962 Note that if you set a metatable without a <code>__gc</code> field | |
| 963 and later create that field in the metatable, | |
| 964 the object will not be marked for finalization. | |
| 965 However, after an object is marked, | |
| 966 you can freely change the <code>__gc</code> field of its metatable. | |
| 967 | |
| 968 | |
| 969 <p> | |
| 970 When a marked object becomes garbage, | |
| 971 it is not collected immediately by the garbage collector. | |
| 972 Instead, Lua puts it in a list. | |
| 973 After the collection, | |
| 974 Lua does the equivalent of the following function | |
| 975 for each object in that list: | |
| 976 | |
| 977 <pre> | |
| 978 function gc_event (obj) | |
| 979 local h = metatable(obj).__gc | |
| 980 if type(h) == "function" then | |
| 981 h(obj) | |
| 982 end | |
| 983 end | |
| 984 </pre> | |
| 985 | |
| 986 <p> | |
| 987 At the end of each garbage-collection cycle, | |
| 988 the finalizers for objects are called in | |
| 989 the reverse order that they were marked for collection, | |
| 990 among those collected in that cycle; | |
| 991 that is, the first finalizer to be called is the one associated | |
| 992 with the object marked last in the program. | |
| 993 The execution of each finalizer may occur at any point during | |
| 994 the execution of the regular code. | |
| 995 | |
| 996 | |
| 997 <p> | |
| 998 Because the object being collected must still be used by the finalizer, | |
| 999 it (and other objects accessible only through it) | |
| 1000 must be <em>resurrected</em> by Lua. | |
| 1001 Usually, this resurrection is transient, | |
| 1002 and the object memory is freed in the next garbage-collection cycle. | |
| 1003 However, if the finalizer stores the object in some global place | |
| 1004 (e.g., a global variable), | |
| 1005 then there is a permanent resurrection. | |
| 1006 In any case, | |
| 1007 the object memory is freed only when it becomes completely inaccessible; | |
| 1008 its finalizer will never be called twice. | |
| 1009 | |
| 1010 | |
| 1011 <p> | |
| 1012 When you close a state (see <a href="#lua_close"><code>lua_close</code></a>), | |
| 1013 Lua calls the finalizers of all objects marked for finalization, | |
| 1014 following the reverse order that they were marked. | |
| 1015 If any finalizer marks new objects for collection during that phase, | |
| 1016 these new objects will not be finalized. | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 <h3>2.5.2 – <a name="2.5.2">Weak Tables</a></h3> | |
| 1023 | |
| 1024 <p> | |
| 1025 A <em>weak table</em> is a table whose elements are | |
| 1026 <em>weak references</em>. | |
| 1027 A weak reference is ignored by the garbage collector. | |
| 1028 In other words, | |
| 1029 if the only references to an object are weak references, | |
| 1030 then the garbage collector will collect that object. | |
| 1031 | |
| 1032 | |
| 1033 <p> | |
| 1034 A weak table can have weak keys, weak values, or both. | |
| 1035 A table with weak keys allows the collection of its keys, | |
| 1036 but prevents the collection of its values. | |
| 1037 A table with both weak keys and weak values allows the collection of | |
| 1038 both keys and values. | |
| 1039 In any case, if either the key or the value is collected, | |
| 1040 the whole pair is removed from the table. | |
| 1041 The weakness of a table is controlled by the | |
| 1042 <code>__mode</code> field of its metatable. | |
| 1043 If the <code>__mode</code> field is a string containing the character '<code>k</code>', | |
| 1044 the keys in the table are weak. | |
| 1045 If <code>__mode</code> contains '<code>v</code>', | |
| 1046 the values in the table are weak. | |
| 1047 | |
| 1048 | |
| 1049 <p> | |
| 1050 A table with weak keys and strong values | |
| 1051 is also called an <em>ephemeron table</em>. | |
| 1052 In an ephemeron table, | |
| 1053 a value is considered reachable only if its key is reachable. | |
| 1054 In particular, | |
| 1055 if the only reference to a key comes through its value, | |
| 1056 the pair is removed. | |
| 1057 | |
| 1058 | |
| 1059 <p> | |
| 1060 Any change in the weakness of a table may take effect only | |
| 1061 at the next collect cycle. | |
| 1062 In particular, if you change the weakness to a stronger mode, | |
| 1063 Lua may still collect some items from that table | |
| 1064 before the change takes effect. | |
| 1065 | |
| 1066 | |
| 1067 <p> | |
| 1068 Only objects that have an explicit construction | |
| 1069 are removed from weak tables. | |
| 1070 Values, such as numbers and light C functions, | |
| 1071 are not subject to garbage collection, | |
| 1072 and therefore are not removed from weak tables | |
| 1073 (unless its associated value is collected). | |
| 1074 Although strings are subject to garbage collection, | |
| 1075 they do not have an explicit construction, | |
| 1076 and therefore are not removed from weak tables. | |
| 1077 | |
| 1078 | |
| 1079 <p> | |
| 1080 Resurrected objects | |
| 1081 (that is, objects being finalized | |
| 1082 and objects accessible only through objects being finalized) | |
| 1083 have a special behavior in weak tables. | |
| 1084 They are removed from weak values before running their finalizers, | |
| 1085 but are removed from weak keys only in the next collection | |
| 1086 after running their finalizers, when such objects are actually freed. | |
| 1087 This behavior allows the finalizer to access properties | |
| 1088 associated with the object through weak tables. | |
| 1089 | |
| 1090 | |
| 1091 <p> | |
| 1092 If a weak table is among the resurrected objects in a collection cycle, | |
| 1093 it may not be properly cleared until the next cycle. | |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 <h2>2.6 – <a name="2.6">Coroutines</a></h2> | |
| 1102 | |
| 1103 <p> | |
| 1104 Lua supports coroutines, | |
| 1105 also called <em>collaborative multithreading</em>. | |
| 1106 A coroutine in Lua represents an independent thread of execution. | |
| 1107 Unlike threads in multithread systems, however, | |
| 1108 a coroutine only suspends its execution by explicitly calling | |
| 1109 a yield function. | |
| 1110 | |
| 1111 | |
| 1112 <p> | |
| 1113 You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>. | |
| 1114 Its sole argument is a function | |
| 1115 that is the main function of the coroutine. | |
| 1116 The <code>create</code> function only creates a new coroutine and | |
| 1117 returns a handle to it (an object of type <em>thread</em>); | |
| 1118 it does not start the coroutine. | |
| 1119 | |
| 1120 | |
| 1121 <p> | |
| 1122 You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | |
| 1123 When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | |
| 1124 passing as its first argument | |
| 1125 a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | |
| 1126 the coroutine starts its execution, | |
| 1127 at the first line of its main function. | |
| 1128 Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed on | |
| 1129 to the coroutine main function. | |
| 1130 After the coroutine starts running, | |
| 1131 it runs until it terminates or <em>yields</em>. | |
| 1132 | |
| 1133 | |
| 1134 <p> | |
| 1135 A coroutine can terminate its execution in two ways: | |
| 1136 normally, when its main function returns | |
| 1137 (explicitly or implicitly, after the last instruction); | |
| 1138 and abnormally, if there is an unprotected error. | |
| 1139 In the first case, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>, | |
| 1140 plus any values returned by the coroutine main function. | |
| 1141 In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b> | |
| 1142 plus an error message. | |
| 1143 | |
| 1144 | |
| 1145 <p> | |
| 1146 A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. | |
| 1147 When a coroutine yields, | |
| 1148 the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately, | |
| 1149 even if the yield happens inside nested function calls | |
| 1150 (that is, not in the main function, | |
| 1151 but in a function directly or indirectly called by the main function). | |
| 1152 In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>, | |
| 1153 plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>. | |
| 1154 The next time you resume the same coroutine, | |
| 1155 it continues its execution from the point where it yielded, | |
| 1156 with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra | |
| 1157 arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | |
| 1158 | |
| 1159 | |
| 1160 <p> | |
| 1161 Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>, | |
| 1162 the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine, | |
| 1163 but instead of returning the coroutine itself, | |
| 1164 it returns a function that, when called, resumes the coroutine. | |
| 1165 Any arguments passed to this function | |
| 1166 go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>. | |
| 1167 <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> returns all the values returned by <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | |
| 1168 except the first one (the boolean error code). | |
| 1169 Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>, | |
| 1170 <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors; | |
| 1171 any error is propagated to the caller. | |
| 1172 | |
| 1173 | |
| 1174 <p> | |
| 1175 As an example of how coroutines work, | |
| 1176 consider the following code: | |
| 1177 | |
| 1178 <pre> | |
| 1179 function foo (a) | |
| 1180 print("foo", a) | |
| 1181 return coroutine.yield(2*a) | |
| 1182 end | |
| 1183 | |
| 1184 co = coroutine.create(function (a,b) | |
| 1185 print("co-body", a, b) | |
| 1186 local r = foo(a+1) | |
| 1187 print("co-body", r) | |
| 1188 local r, s = coroutine.yield(a+b, a-b) | |
| 1189 print("co-body", r, s) | |
| 1190 return b, "end" | |
| 1191 end) | |
| 1192 | |
| 1193 print("main", coroutine.resume(co, 1, 10)) | |
| 1194 print("main", coroutine.resume(co, "r")) | |
| 1195 print("main", coroutine.resume(co, "x", "y")) | |
| 1196 print("main", coroutine.resume(co, "x", "y")) | |
| 1197 </pre><p> | |
| 1198 When you run it, it produces the following output: | |
| 1199 | |
| 1200 <pre> | |
| 1201 co-body 1 10 | |
| 1202 foo 2 | |
| 1203 main true 4 | |
| 1204 co-body r | |
| 1205 main true 11 -9 | |
| 1206 co-body x y | |
| 1207 main true 10 end | |
| 1208 main false cannot resume dead coroutine | |
| 1209 </pre> | |
| 1210 | |
| 1211 <p> | |
| 1212 You can also create and manipulate coroutines through the C API: | |
| 1213 see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>, | |
| 1214 and <a href="#lua_yield"><code>lua_yield</code></a>. | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 <h1>3 – <a name="3">The Language</a></h1> | |
| 1221 | |
| 1222 <p> | |
| 1223 This section describes the lexis, the syntax, and the semantics of Lua. | |
| 1224 In other words, | |
| 1225 this section describes | |
| 1226 which tokens are valid, | |
| 1227 how they can be combined, | |
| 1228 and what their combinations mean. | |
| 1229 | |
| 1230 | |
| 1231 <p> | |
| 1232 Language constructs will be explained using the usual extended BNF notation, | |
| 1233 in which | |
| 1234 {<em>a</em>} means 0 or more <em>a</em>'s, and | |
| 1235 [<em>a</em>] means an optional <em>a</em>. | |
| 1236 Non-terminals are shown like non-terminal, | |
| 1237 keywords are shown like <b>kword</b>, | |
| 1238 and other terminal symbols are shown like ‘<b>=</b>’. | |
| 1239 The complete syntax of Lua can be found in <a href="#9">§9</a> | |
| 1240 at the end of this manual. | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 <h2>3.1 – <a name="3.1">Lexical Conventions</a></h2> | |
| 1245 | |
| 1246 <p> | |
| 1247 Lua is a free-form language. | |
| 1248 It ignores spaces (including new lines) and comments | |
| 1249 between lexical elements (tokens), | |
| 1250 except as delimiters between names and keywords. | |
| 1251 | |
| 1252 | |
| 1253 <p> | |
| 1254 <em>Names</em> | |
| 1255 (also called <em>identifiers</em>) | |
| 1256 in Lua can be any string of letters, | |
| 1257 digits, and underscores, | |
| 1258 not beginning with a digit. | |
| 1259 Identifiers are used to name variables, table fields, and labels. | |
| 1260 | |
| 1261 | |
| 1262 <p> | |
| 1263 The following <em>keywords</em> are reserved | |
| 1264 and cannot be used as names: | |
| 1265 | |
| 1266 | |
| 1267 <pre> | |
| 1268 and break do else elseif end | |
| 1269 false for function goto if in | |
| 1270 local nil not or repeat return | |
| 1271 then true until while | |
| 1272 </pre> | |
| 1273 | |
| 1274 <p> | |
| 1275 Lua is a case-sensitive language: | |
| 1276 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> | |
| 1277 are two different, valid names. | |
| 1278 As a convention, names starting with an underscore followed by | |
| 1279 uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>) | |
| 1280 are reserved for variables used by Lua. | |
| 1281 | |
| 1282 | |
| 1283 <p> | |
| 1284 The following strings denote other tokens: | |
| 1285 | |
| 1286 <pre> | |
| 1287 + - * / % ^ # | |
| 1288 == ~= <= >= < > = | |
| 1289 ( ) { } [ ] :: | |
| 1290 ; : , . .. ... | |
| 1291 </pre> | |
| 1292 | |
| 1293 <p> | |
| 1294 <em>Literal strings</em> | |
| 1295 can be delimited by matching single or double quotes, | |
| 1296 and can contain the following C-like escape sequences: | |
| 1297 '<code>\a</code>' (bell), | |
| 1298 '<code>\b</code>' (backspace), | |
| 1299 '<code>\f</code>' (form feed), | |
| 1300 '<code>\n</code>' (newline), | |
| 1301 '<code>\r</code>' (carriage return), | |
| 1302 '<code>\t</code>' (horizontal tab), | |
| 1303 '<code>\v</code>' (vertical tab), | |
| 1304 '<code>\\</code>' (backslash), | |
| 1305 '<code>\"</code>' (quotation mark [double quote]), | |
| 1306 and '<code>\'</code>' (apostrophe [single quote]). | |
| 1307 A backslash followed by a real newline | |
| 1308 results in a newline in the string. | |
| 1309 The escape sequence '<code>\z</code>' skips the following span | |
| 1310 of white-space characters, | |
| 1311 including line breaks; | |
| 1312 it is particularly useful to break and indent a long literal string | |
| 1313 into multiple lines without adding the newlines and spaces | |
| 1314 into the string contents. | |
| 1315 | |
| 1316 | |
| 1317 <p> | |
| 1318 A byte in a literal string can also be specified by its numerical value. | |
| 1319 This can be done with the escape sequence <code>\x<em>XX</em></code>, | |
| 1320 where <em>XX</em> is a sequence of exactly two hexadecimal digits, | |
| 1321 or with the escape sequence <code>\<em>ddd</em></code>, | |
| 1322 where <em>ddd</em> is a sequence of up to three decimal digits. | |
| 1323 (Note that if a decimal escape is to be followed by a digit, | |
| 1324 it must be expressed using exactly three digits.) | |
| 1325 Strings in Lua can contain any 8-bit value, including embedded zeros, | |
| 1326 which can be specified as '<code>\0</code>'. | |
| 1327 | |
| 1328 | |
| 1329 <p> | |
| 1330 Literal strings can also be defined using a long format | |
| 1331 enclosed by <em>long brackets</em>. | |
| 1332 We define an <em>opening long bracket of level <em>n</em></em> as an opening | |
| 1333 square bracket followed by <em>n</em> equal signs followed by another | |
| 1334 opening square bracket. | |
| 1335 So, an opening long bracket of level 0 is written as <code>[[</code>, | |
| 1336 an opening long bracket of level 1 is written as <code>[=[</code>, | |
| 1337 and so on. | |
| 1338 A <em>closing long bracket</em> is defined similarly; | |
| 1339 for instance, a closing long bracket of level 4 is written as <code>]====]</code>. | |
| 1340 A <em>long literal</em> starts with an opening long bracket of any level and | |
| 1341 ends at the first closing long bracket of the same level. | |
| 1342 It can contain any text except a closing bracket of the proper level. | |
| 1343 Literals in this bracketed form can run for several lines, | |
| 1344 do not interpret any escape sequences, | |
| 1345 and ignore long brackets of any other level. | |
| 1346 Any kind of end-of-line sequence | |
| 1347 (carriage return, newline, carriage return followed by newline, | |
| 1348 or newline followed by carriage return) | |
| 1349 is converted to a simple newline. | |
| 1350 | |
| 1351 | |
| 1352 <p> | |
| 1353 Any byte in a literal string not | |
| 1354 explicitly affected by the previous rules represents itself. | |
| 1355 However, Lua opens files for parsing in text mode, | |
| 1356 and the system file functions may have problems with | |
| 1357 some control characters. | |
| 1358 So, it is safer to represent | |
| 1359 non-text data as a quoted literal with | |
| 1360 explicit escape sequences for non-text characters. | |
| 1361 | |
| 1362 | |
| 1363 <p> | |
| 1364 For convenience, | |
| 1365 when the opening long bracket is immediately followed by a newline, | |
| 1366 the newline is not included in the string. | |
| 1367 As an example, in a system using ASCII | |
| 1368 (in which '<code>a</code>' is coded as 97, | |
| 1369 newline is coded as 10, and '<code>1</code>' is coded as 49), | |
| 1370 the five literal strings below denote the same string: | |
| 1371 | |
| 1372 <pre> | |
| 1373 a = 'alo\n123"' | |
| 1374 a = "alo\n123\"" | |
| 1375 a = '\97lo\10\04923"' | |
| 1376 a = [[alo | |
| 1377 123"]] | |
| 1378 a = [==[ | |
| 1379 alo | |
| 1380 123"]==] | |
| 1381 </pre> | |
| 1382 | |
| 1383 <p> | |
| 1384 A <em>numerical constant</em> can be written with an optional fractional part | |
| 1385 and an optional decimal exponent, | |
| 1386 marked by a letter '<code>e</code>' or '<code>E</code>'. | |
| 1387 Lua also accepts hexadecimal constants, | |
| 1388 which start with <code>0x</code> or <code>0X</code>. | |
| 1389 Hexadecimal constants also accept an optional fractional part | |
| 1390 plus an optional binary exponent, | |
| 1391 marked by a letter '<code>p</code>' or '<code>P</code>'. | |
| 1392 Examples of valid numerical constants are | |
| 1393 | |
| 1394 <pre> | |
| 1395 3 3.0 3.1416 314.16e-2 0.31416E1 | |
| 1396 0xff 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1 | |
| 1397 </pre> | |
| 1398 | |
| 1399 <p> | |
| 1400 A <em>comment</em> starts with a double hyphen (<code>--</code>) | |
| 1401 anywhere outside a string. | |
| 1402 If the text immediately after <code>--</code> is not an opening long bracket, | |
| 1403 the comment is a <em>short comment</em>, | |
| 1404 which runs until the end of the line. | |
| 1405 Otherwise, it is a <em>long comment</em>, | |
| 1406 which runs until the corresponding closing long bracket. | |
| 1407 Long comments are frequently used to disable code temporarily. | |
| 1408 | |
| 1409 | |
| 1410 | |
| 1411 | |
| 1412 | |
| 1413 <h2>3.2 – <a name="3.2">Variables</a></h2> | |
| 1414 | |
| 1415 <p> | |
| 1416 Variables are places that store values. | |
| 1417 There are three kinds of variables in Lua: | |
| 1418 global variables, local variables, and table fields. | |
| 1419 | |
| 1420 | |
| 1421 <p> | |
| 1422 A single name can denote a global variable or a local variable | |
| 1423 (or a function's formal parameter, | |
| 1424 which is a particular kind of local variable): | |
| 1425 | |
| 1426 <pre> | |
| 1427 var ::= Name | |
| 1428 </pre><p> | |
| 1429 Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>. | |
| 1430 | |
| 1431 | |
| 1432 <p> | |
| 1433 Any variable name is assumed to be global unless explicitly declared | |
| 1434 as a local (see <a href="#3.3.7">§3.3.7</a>). | |
| 1435 Local variables are <em>lexically scoped</em>: | |
| 1436 local variables can be freely accessed by functions | |
| 1437 defined inside their scope (see <a href="#3.5">§3.5</a>). | |
| 1438 | |
| 1439 | |
| 1440 <p> | |
| 1441 Before the first assignment to a variable, its value is <b>nil</b>. | |
| 1442 | |
| 1443 | |
| 1444 <p> | |
| 1445 Square brackets are used to index a table: | |
| 1446 | |
| 1447 <pre> | |
| 1448 var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | |
| 1449 </pre><p> | |
| 1450 The meaning of accesses to table fields can be changed via metatables. | |
| 1451 An access to an indexed variable <code>t[i]</code> is equivalent to | |
| 1452 a call <code>gettable_event(t,i)</code>. | |
| 1453 (See <a href="#2.4">§2.4</a> for a complete description of the | |
| 1454 <code>gettable_event</code> function. | |
| 1455 This function is not defined or callable in Lua. | |
| 1456 We use it here only for explanatory purposes.) | |
| 1457 | |
| 1458 | |
| 1459 <p> | |
| 1460 The syntax <code>var.Name</code> is just syntactic sugar for | |
| 1461 <code>var["Name"]</code>: | |
| 1462 | |
| 1463 <pre> | |
| 1464 var ::= prefixexp ‘<b>.</b>’ Name | |
| 1465 </pre> | |
| 1466 | |
| 1467 <p> | |
| 1468 An access to a global variable <code>x</code> | |
| 1469 is equivalent to <code>_ENV.x</code>. | |
| 1470 Due to the way that chunks are compiled, | |
| 1471 <code>_ENV</code> is never a global name (see <a href="#2.2">§2.2</a>). | |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | |
| 1477 <h2>3.3 – <a name="3.3">Statements</a></h2> | |
| 1478 | |
| 1479 <p> | |
| 1480 Lua supports an almost conventional set of statements, | |
| 1481 similar to those in Pascal or C. | |
| 1482 This set includes | |
| 1483 assignments, control structures, function calls, | |
| 1484 and variable declarations. | |
| 1485 | |
| 1486 | |
| 1487 | |
| 1488 <h3>3.3.1 – <a name="3.3.1">Blocks</a></h3> | |
| 1489 | |
| 1490 <p> | |
| 1491 A block is a list of statements, | |
| 1492 which are executed sequentially: | |
| 1493 | |
| 1494 <pre> | |
| 1495 block ::= {stat} | |
| 1496 </pre><p> | |
| 1497 Lua has <em>empty statements</em> | |
| 1498 that allow you to separate statements with semicolons, | |
| 1499 start a block with a semicolon | |
| 1500 or write two semicolons in sequence: | |
| 1501 | |
| 1502 <pre> | |
| 1503 stat ::= ‘<b>;</b>’ | |
| 1504 </pre> | |
| 1505 | |
| 1506 <p> | |
| 1507 Function calls and assignments | |
| 1508 can start with an open parenthesis. | |
| 1509 This possibility leads to an ambiguity in Lua's grammar. | |
| 1510 Consider the following fragment: | |
| 1511 | |
| 1512 <pre> | |
| 1513 a = b + c | |
| 1514 (print or io.write)('done') | |
| 1515 </pre><p> | |
| 1516 The grammar could see it in two ways: | |
| 1517 | |
| 1518 <pre> | |
| 1519 a = b + c(print or io.write)('done') | |
| 1520 | |
| 1521 a = b + c; (print or io.write)('done') | |
| 1522 </pre><p> | |
| 1523 The current parser always sees such constructions | |
| 1524 in the first way, | |
| 1525 interpreting the open parenthesis | |
| 1526 as the start of the arguments to a call. | |
| 1527 To avoid this ambiguity, | |
| 1528 it is a good practice to always precede with a semicolon | |
| 1529 statements that start with a parenthesis: | |
| 1530 | |
| 1531 <pre> | |
| 1532 ;(print or io.write)('done') | |
| 1533 </pre> | |
| 1534 | |
| 1535 <p> | |
| 1536 A block can be explicitly delimited to produce a single statement: | |
| 1537 | |
| 1538 <pre> | |
| 1539 stat ::= <b>do</b> block <b>end</b> | |
| 1540 </pre><p> | |
| 1541 Explicit blocks are useful | |
| 1542 to control the scope of variable declarations. | |
| 1543 Explicit blocks are also sometimes used to | |
| 1544 add a <b>return</b> statement in the middle | |
| 1545 of another block (see <a href="#3.3.4">§3.3.4</a>). | |
| 1546 | |
| 1547 | |
| 1548 | |
| 1549 | |
| 1550 | |
| 1551 <h3>3.3.2 – <a name="3.3.2">Chunks</a></h3> | |
| 1552 | |
| 1553 <p> | |
| 1554 The unit of compilation of Lua is called a <em>chunk</em>. | |
| 1555 Syntactically, | |
| 1556 a chunk is simply a block: | |
| 1557 | |
| 1558 <pre> | |
| 1559 chunk ::= block | |
| 1560 </pre> | |
| 1561 | |
| 1562 <p> | |
| 1563 Lua handles a chunk as the body of an anonymous function | |
| 1564 with a variable number of arguments | |
| 1565 (see <a href="#3.4.10">§3.4.10</a>). | |
| 1566 As such, chunks can define local variables, | |
| 1567 receive arguments, and return values. | |
| 1568 Moreover, such anonymous function is compiled as in the | |
| 1569 scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>). | |
| 1570 The resulting function always has <code>_ENV</code> as its only upvalue, | |
| 1571 even if it does not use that variable. | |
| 1572 | |
| 1573 | |
| 1574 <p> | |
| 1575 A chunk can be stored in a file or in a string inside the host program. | |
| 1576 To execute a chunk, | |
| 1577 Lua first precompiles the chunk into instructions for a virtual machine, | |
| 1578 and then it executes the compiled code | |
| 1579 with an interpreter for the virtual machine. | |
| 1580 | |
| 1581 | |
| 1582 <p> | |
| 1583 Chunks can also be precompiled into binary form; | |
| 1584 see program <code>luac</code> for details. | |
| 1585 Programs in source and compiled forms are interchangeable; | |
| 1586 Lua automatically detects the file type and acts accordingly. | |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | |
| 1591 | |
| 1592 | |
| 1593 <h3>3.3.3 – <a name="3.3.3">Assignment</a></h3> | |
| 1594 | |
| 1595 <p> | |
| 1596 Lua allows multiple assignments. | |
| 1597 Therefore, the syntax for assignment | |
| 1598 defines a list of variables on the left side | |
| 1599 and a list of expressions on the right side. | |
| 1600 The elements in both lists are separated by commas: | |
| 1601 | |
| 1602 <pre> | |
| 1603 stat ::= varlist ‘<b>=</b>’ explist | |
| 1604 varlist ::= var {‘<b>,</b>’ var} | |
| 1605 explist ::= exp {‘<b>,</b>’ exp} | |
| 1606 </pre><p> | |
| 1607 Expressions are discussed in <a href="#3.4">§3.4</a>. | |
| 1608 | |
| 1609 | |
| 1610 <p> | |
| 1611 Before the assignment, | |
| 1612 the list of values is <em>adjusted</em> to the length of | |
| 1613 the list of variables. | |
| 1614 If there are more values than needed, | |
| 1615 the excess values are thrown away. | |
| 1616 If there are fewer values than needed, | |
| 1617 the list is extended with as many <b>nil</b>'s as needed. | |
| 1618 If the list of expressions ends with a function call, | |
| 1619 then all values returned by that call enter the list of values, | |
| 1620 before the adjustment | |
| 1621 (except when the call is enclosed in parentheses; see <a href="#3.4">§3.4</a>). | |
| 1622 | |
| 1623 | |
| 1624 <p> | |
| 1625 The assignment statement first evaluates all its expressions | |
| 1626 and only then are the assignments performed. | |
| 1627 Thus the code | |
| 1628 | |
| 1629 <pre> | |
| 1630 i = 3 | |
| 1631 i, a[i] = i+1, 20 | |
| 1632 </pre><p> | |
| 1633 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code> | |
| 1634 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3) | |
| 1635 before it is assigned 4. | |
| 1636 Similarly, the line | |
| 1637 | |
| 1638 <pre> | |
| 1639 x, y = y, x | |
| 1640 </pre><p> | |
| 1641 exchanges the values of <code>x</code> and <code>y</code>, | |
| 1642 and | |
| 1643 | |
| 1644 <pre> | |
| 1645 x, y, z = y, z, x | |
| 1646 </pre><p> | |
| 1647 cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>. | |
| 1648 | |
| 1649 | |
| 1650 <p> | |
| 1651 The meaning of assignments to global variables | |
| 1652 and table fields can be changed via metatables. | |
| 1653 An assignment to an indexed variable <code>t[i] = val</code> is equivalent to | |
| 1654 <code>settable_event(t,i,val)</code>. | |
| 1655 (See <a href="#2.4">§2.4</a> for a complete description of the | |
| 1656 <code>settable_event</code> function. | |
| 1657 This function is not defined or callable in Lua. | |
| 1658 We use it here only for explanatory purposes.) | |
| 1659 | |
| 1660 | |
| 1661 <p> | |
| 1662 An assignment to a global variable <code>x = val</code> | |
| 1663 is equivalent to the assignment | |
| 1664 <code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>). | |
| 1665 | |
| 1666 | |
| 1667 | |
| 1668 | |
| 1669 | |
| 1670 <h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p> | |
| 1671 The control structures | |
| 1672 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and | |
| 1673 familiar syntax: | |
| 1674 | |
| 1675 | |
| 1676 | |
| 1677 | |
| 1678 <pre> | |
| 1679 stat ::= <b>while</b> exp <b>do</b> block <b>end</b> | |
| 1680 stat ::= <b>repeat</b> block <b>until</b> exp | |
| 1681 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | |
| 1682 </pre><p> | |
| 1683 Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>). | |
| 1684 | |
| 1685 | |
| 1686 <p> | |
| 1687 The condition expression of a | |
| 1688 control structure can return any value. | |
| 1689 Both <b>false</b> and <b>nil</b> are considered false. | |
| 1690 All values different from <b>nil</b> and <b>false</b> are considered true | |
| 1691 (in particular, the number 0 and the empty string are also true). | |
| 1692 | |
| 1693 | |
| 1694 <p> | |
| 1695 In the <b>repeat</b>–<b>until</b> loop, | |
| 1696 the inner block does not end at the <b>until</b> keyword, | |
| 1697 but only after the condition. | |
| 1698 So, the condition can refer to local variables | |
| 1699 declared inside the loop block. | |
| 1700 | |
| 1701 | |
| 1702 <p> | |
| 1703 The <b>goto</b> statement transfers the program control to a label. | |
| 1704 For syntactical reasons, | |
| 1705 labels in Lua are considered statements too: | |
| 1706 | |
| 1707 | |
| 1708 | |
| 1709 <pre> | |
| 1710 stat ::= <b>goto</b> Name | |
| 1711 stat ::= label | |
| 1712 label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ | |
| 1713 </pre> | |
| 1714 | |
| 1715 <p> | |
| 1716 A label is visible in the entire block where it is defined, | |
| 1717 except | |
| 1718 inside nested blocks where a label with the same name is defined and | |
| 1719 inside nested functions. | |
| 1720 A goto may jump to any visible label as long as it does not | |
| 1721 enter into the scope of a local variable. | |
| 1722 | |
| 1723 | |
| 1724 <p> | |
| 1725 Labels and empty statements are called <em>void statements</em>, | |
| 1726 as they perform no actions. | |
| 1727 | |
| 1728 | |
| 1729 <p> | |
| 1730 The <b>break</b> statement terminates the execution of a | |
| 1731 <b>while</b>, <b>repeat</b>, or <b>for</b> loop, | |
| 1732 skipping to the next statement after the loop: | |
| 1733 | |
| 1734 | |
| 1735 <pre> | |
| 1736 stat ::= <b>break</b> | |
| 1737 </pre><p> | |
| 1738 A <b>break</b> ends the innermost enclosing loop. | |
| 1739 | |
| 1740 | |
| 1741 <p> | |
| 1742 The <b>return</b> statement is used to return values | |
| 1743 from a function or a chunk (which is a function in disguise). | |
| 1744 | |
| 1745 Functions can return more than one value, | |
| 1746 so the syntax for the <b>return</b> statement is | |
| 1747 | |
| 1748 <pre> | |
| 1749 stat ::= <b>return</b> [explist] [‘<b>;</b>’] | |
| 1750 </pre> | |
| 1751 | |
| 1752 <p> | |
| 1753 The <b>return</b> statement can only be written | |
| 1754 as the last statement of a block. | |
| 1755 If it is really necessary to <b>return</b> in the middle of a block, | |
| 1756 then an explicit inner block can be used, | |
| 1757 as in the idiom <code>do return end</code>, | |
| 1758 because now <b>return</b> is the last statement in its (inner) block. | |
| 1759 | |
| 1760 | |
| 1761 | |
| 1762 | |
| 1763 | |
| 1764 <h3>3.3.5 – <a name="3.3.5">For Statement</a></h3> | |
| 1765 | |
| 1766 <p> | |
| 1767 | |
| 1768 The <b>for</b> statement has two forms: | |
| 1769 one numeric and one generic. | |
| 1770 | |
| 1771 | |
| 1772 <p> | |
| 1773 The numeric <b>for</b> loop repeats a block of code while a | |
| 1774 control variable runs through an arithmetic progression. | |
| 1775 It has the following syntax: | |
| 1776 | |
| 1777 <pre> | |
| 1778 stat ::= <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | |
| 1779 </pre><p> | |
| 1780 The <em>block</em> is repeated for <em>name</em> starting at the value of | |
| 1781 the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the | |
| 1782 third <em>exp</em>. | |
| 1783 More precisely, a <b>for</b> statement like | |
| 1784 | |
| 1785 <pre> | |
| 1786 for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end | |
| 1787 </pre><p> | |
| 1788 is equivalent to the code: | |
| 1789 | |
| 1790 <pre> | |
| 1791 do | |
| 1792 local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>) | |
| 1793 if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end | |
| 1794 while (<em>step</em> > 0 and <em>var</em> <= <em>limit</em>) or (<em>step</em> <= 0 and <em>var</em> >= <em>limit</em>) do | |
| 1795 local v = <em>var</em> | |
| 1796 <em>block</em> | |
| 1797 <em>var</em> = <em>var</em> + <em>step</em> | |
| 1798 end | |
| 1799 end | |
| 1800 </pre><p> | |
| 1801 Note the following: | |
| 1802 | |
| 1803 <ul> | |
| 1804 | |
| 1805 <li> | |
| 1806 All three control expressions are evaluated only once, | |
| 1807 before the loop starts. | |
| 1808 They must all result in numbers. | |
| 1809 </li> | |
| 1810 | |
| 1811 <li> | |
| 1812 <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables. | |
| 1813 The names shown here are for explanatory purposes only. | |
| 1814 </li> | |
| 1815 | |
| 1816 <li> | |
| 1817 If the third expression (the step) is absent, | |
| 1818 then a step of 1 is used. | |
| 1819 </li> | |
| 1820 | |
| 1821 <li> | |
| 1822 You can use <b>break</b> to exit a <b>for</b> loop. | |
| 1823 </li> | |
| 1824 | |
| 1825 <li> | |
| 1826 The loop variable <code>v</code> is local to the loop; | |
| 1827 you cannot use its value after the <b>for</b> ends or is broken. | |
| 1828 If you need this value, | |
| 1829 assign it to another variable before breaking or exiting the loop. | |
| 1830 </li> | |
| 1831 | |
| 1832 </ul> | |
| 1833 | |
| 1834 <p> | |
| 1835 The generic <b>for</b> statement works over functions, | |
| 1836 called <em>iterators</em>. | |
| 1837 On each iteration, the iterator function is called to produce a new value, | |
| 1838 stopping when this new value is <b>nil</b>. | |
| 1839 The generic <b>for</b> loop has the following syntax: | |
| 1840 | |
| 1841 <pre> | |
| 1842 stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | |
| 1843 namelist ::= Name {‘<b>,</b>’ Name} | |
| 1844 </pre><p> | |
| 1845 A <b>for</b> statement like | |
| 1846 | |
| 1847 <pre> | |
| 1848 for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>block</em> end | |
| 1849 </pre><p> | |
| 1850 is equivalent to the code: | |
| 1851 | |
| 1852 <pre> | |
| 1853 do | |
| 1854 local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em> | |
| 1855 while true do | |
| 1856 local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>) | |
| 1857 if <em>var_1</em> == nil then break end | |
| 1858 <em>var</em> = <em>var_1</em> | |
| 1859 <em>block</em> | |
| 1860 end | |
| 1861 end | |
| 1862 </pre><p> | |
| 1863 Note the following: | |
| 1864 | |
| 1865 <ul> | |
| 1866 | |
| 1867 <li> | |
| 1868 <code><em>explist</em></code> is evaluated only once. | |
| 1869 Its results are an <em>iterator</em> function, | |
| 1870 a <em>state</em>, | |
| 1871 and an initial value for the first <em>iterator variable</em>. | |
| 1872 </li> | |
| 1873 | |
| 1874 <li> | |
| 1875 <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables. | |
| 1876 The names are here for explanatory purposes only. | |
| 1877 </li> | |
| 1878 | |
| 1879 <li> | |
| 1880 You can use <b>break</b> to exit a <b>for</b> loop. | |
| 1881 </li> | |
| 1882 | |
| 1883 <li> | |
| 1884 The loop variables <code><em>var_i</em></code> are local to the loop; | |
| 1885 you cannot use their values after the <b>for</b> ends. | |
| 1886 If you need these values, | |
| 1887 then assign them to other variables before breaking or exiting the loop. | |
| 1888 </li> | |
| 1889 | |
| 1890 </ul> | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 <h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p> | |
| 1896 To allow possible side-effects, | |
| 1897 function calls can be executed as statements: | |
| 1898 | |
| 1899 <pre> | |
| 1900 stat ::= functioncall | |
| 1901 </pre><p> | |
| 1902 In this case, all returned values are thrown away. | |
| 1903 Function calls are explained in <a href="#3.4.9">§3.4.9</a>. | |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | |
| 1908 | |
| 1909 <h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p> | |
| 1910 Local variables can be declared anywhere inside a block. | |
| 1911 The declaration can include an initial assignment: | |
| 1912 | |
| 1913 <pre> | |
| 1914 stat ::= <b>local</b> namelist [‘<b>=</b>’ explist] | |
| 1915 </pre><p> | |
| 1916 If present, an initial assignment has the same semantics | |
| 1917 of a multiple assignment (see <a href="#3.3.3">§3.3.3</a>). | |
| 1918 Otherwise, all variables are initialized with <b>nil</b>. | |
| 1919 | |
| 1920 | |
| 1921 <p> | |
| 1922 A chunk is also a block (see <a href="#3.3.2">§3.3.2</a>), | |
| 1923 and so local variables can be declared in a chunk outside any explicit block. | |
| 1924 | |
| 1925 | |
| 1926 <p> | |
| 1927 The visibility rules for local variables are explained in <a href="#3.5">§3.5</a>. | |
| 1928 | |
| 1929 | |
| 1930 | |
| 1931 | |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 <h2>3.4 – <a name="3.4">Expressions</a></h2> | |
| 1936 | |
| 1937 <p> | |
| 1938 The basic expressions in Lua are the following: | |
| 1939 | |
| 1940 <pre> | |
| 1941 exp ::= prefixexp | |
| 1942 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | |
| 1943 exp ::= Number | |
| 1944 exp ::= String | |
| 1945 exp ::= functiondef | |
| 1946 exp ::= tableconstructor | |
| 1947 exp ::= ‘<b>...</b>’ | |
| 1948 exp ::= exp binop exp | |
| 1949 exp ::= unop exp | |
| 1950 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | |
| 1951 </pre> | |
| 1952 | |
| 1953 <p> | |
| 1954 Numbers and literal strings are explained in <a href="#3.1">§3.1</a>; | |
| 1955 variables are explained in <a href="#3.2">§3.2</a>; | |
| 1956 function definitions are explained in <a href="#3.4.10">§3.4.10</a>; | |
| 1957 function calls are explained in <a href="#3.4.9">§3.4.9</a>; | |
| 1958 table constructors are explained in <a href="#3.4.8">§3.4.8</a>. | |
| 1959 Vararg expressions, | |
| 1960 denoted by three dots ('<code>...</code>'), can only be used when | |
| 1961 directly inside a vararg function; | |
| 1962 they are explained in <a href="#3.4.10">§3.4.10</a>. | |
| 1963 | |
| 1964 | |
| 1965 <p> | |
| 1966 Binary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>), | |
| 1967 relational operators (see <a href="#3.4.3">§3.4.3</a>), logical operators (see <a href="#3.4.4">§3.4.4</a>), | |
| 1968 and the concatenation operator (see <a href="#3.4.5">§3.4.5</a>). | |
| 1969 Unary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>), | |
| 1970 the unary <b>not</b> (see <a href="#3.4.4">§3.4.4</a>), | |
| 1971 and the unary <em>length operator</em> (see <a href="#3.4.6">§3.4.6</a>). | |
| 1972 | |
| 1973 | |
| 1974 <p> | |
| 1975 Both function calls and vararg expressions can result in multiple values. | |
| 1976 If a function call is used as a statement (see <a href="#3.3.6">§3.3.6</a>), | |
| 1977 then its return list is adjusted to zero elements, | |
| 1978 thus discarding all returned values. | |
| 1979 If an expression is used as the last (or the only) element | |
| 1980 of a list of expressions, | |
| 1981 then no adjustment is made | |
| 1982 (unless the expression is enclosed in parentheses). | |
| 1983 In all other contexts, | |
| 1984 Lua adjusts the result list to one element, | |
| 1985 either discarding all values except the first one | |
| 1986 or adding a single <b>nil</b> if there are no values. | |
| 1987 | |
| 1988 | |
| 1989 <p> | |
| 1990 Here are some examples: | |
| 1991 | |
| 1992 <pre> | |
| 1993 f() -- adjusted to 0 results | |
| 1994 g(f(), x) -- f() is adjusted to 1 result | |
| 1995 g(x, f()) -- g gets x plus all results from f() | |
| 1996 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) | |
| 1997 a,b = ... -- a gets the first vararg parameter, b gets | |
| 1998 -- the second (both a and b can get nil if there | |
| 1999 -- is no corresponding vararg parameter) | |
| 2000 | |
| 2001 a,b,c = x, f() -- f() is adjusted to 2 results | |
| 2002 a,b,c = f() -- f() is adjusted to 3 results | |
| 2003 return f() -- returns all results from f() | |
| 2004 return ... -- returns all received vararg parameters | |
| 2005 return x,y,f() -- returns x, y, and all results from f() | |
| 2006 {f()} -- creates a list with all results from f() | |
| 2007 {...} -- creates a list with all vararg parameters | |
| 2008 {f(), nil} -- f() is adjusted to 1 result | |
| 2009 </pre> | |
| 2010 | |
| 2011 <p> | |
| 2012 Any expression enclosed in parentheses always results in only one value. | |
| 2013 Thus, | |
| 2014 <code>(f(x,y,z))</code> is always a single value, | |
| 2015 even if <code>f</code> returns several values. | |
| 2016 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code> | |
| 2017 or <b>nil</b> if <code>f</code> does not return any values.) | |
| 2018 | |
| 2019 | |
| 2020 | |
| 2021 <h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p> | |
| 2022 Lua supports the usual arithmetic operators: | |
| 2023 the binary <code>+</code> (addition), | |
| 2024 <code>-</code> (subtraction), <code>*</code> (multiplication), | |
| 2025 <code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation); | |
| 2026 and unary <code>-</code> (mathematical negation). | |
| 2027 If the operands are numbers, or strings that can be converted to | |
| 2028 numbers (see <a href="#3.4.2">§3.4.2</a>), | |
| 2029 then all operations have the usual meaning. | |
| 2030 Exponentiation works for any exponent. | |
| 2031 For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>. | |
| 2032 Modulo is defined as | |
| 2033 | |
| 2034 <pre> | |
| 2035 a % b == a - math.floor(a/b)*b | |
| 2036 </pre><p> | |
| 2037 That is, it is the remainder of a division that rounds | |
| 2038 the quotient towards minus infinity. | |
| 2039 | |
| 2040 | |
| 2041 | |
| 2042 | |
| 2043 | |
| 2044 <h3>3.4.2 – <a name="3.4.2">Coercion</a></h3> | |
| 2045 | |
| 2046 <p> | |
| 2047 Lua provides automatic conversion between | |
| 2048 string and number values at run time. | |
| 2049 Any arithmetic operation applied to a string tries to convert | |
| 2050 this string to a number, following the rules of the Lua lexer. | |
| 2051 (The string may have leading and trailing spaces and a sign.) | |
| 2052 Conversely, whenever a number is used where a string is expected, | |
| 2053 the number is converted to a string, in a reasonable format. | |
| 2054 For complete control over how numbers are converted to strings, | |
| 2055 use the <code>format</code> function from the string library | |
| 2056 (see <a href="#pdf-string.format"><code>string.format</code></a>). | |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 <h3>3.4.3 – <a name="3.4.3">Relational Operators</a></h3><p> | |
| 2063 The relational operators in Lua are | |
| 2064 | |
| 2065 <pre> | |
| 2066 == ~= < > <= >= | |
| 2067 </pre><p> | |
| 2068 These operators always result in <b>false</b> or <b>true</b>. | |
| 2069 | |
| 2070 | |
| 2071 <p> | |
| 2072 Equality (<code>==</code>) first compares the type of its operands. | |
| 2073 If the types are different, then the result is <b>false</b>. | |
| 2074 Otherwise, the values of the operands are compared. | |
| 2075 Numbers and strings are compared in the usual way. | |
| 2076 Tables, userdata, and threads | |
| 2077 are compared by reference: | |
| 2078 two objects are considered equal only if they are the same object. | |
| 2079 Every time you create a new object | |
| 2080 (a table, userdata, or thread), | |
| 2081 this new object is different from any previously existing object. | |
| 2082 Closures with the same reference are always equal. | |
| 2083 Closures with any detectable difference | |
| 2084 (different behavior, different definition) are always different. | |
| 2085 | |
| 2086 | |
| 2087 <p> | |
| 2088 You can change the way that Lua compares tables and userdata | |
| 2089 by using the "eq" metamethod (see <a href="#2.4">§2.4</a>). | |
| 2090 | |
| 2091 | |
| 2092 <p> | |
| 2093 The conversion rules of <a href="#3.4.2">§3.4.2</a> | |
| 2094 do not apply to equality comparisons. | |
| 2095 Thus, <code>"0"==0</code> evaluates to <b>false</b>, | |
| 2096 and <code>t[0]</code> and <code>t["0"]</code> denote different | |
| 2097 entries in a table. | |
| 2098 | |
| 2099 | |
| 2100 <p> | |
| 2101 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>). | |
| 2102 | |
| 2103 | |
| 2104 <p> | |
| 2105 The order operators work as follows. | |
| 2106 If both arguments are numbers, then they are compared as such. | |
| 2107 Otherwise, if both arguments are strings, | |
| 2108 then their values are compared according to the current locale. | |
| 2109 Otherwise, Lua tries to call the "lt" or the "le" | |
| 2110 metamethod (see <a href="#2.4">§2.4</a>). | |
| 2111 A comparison <code>a > b</code> is translated to <code>b < a</code> | |
| 2112 and <code>a >= b</code> is translated to <code>b <= a</code>. | |
| 2113 | |
| 2114 | |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 <h3>3.4.4 – <a name="3.4.4">Logical Operators</a></h3><p> | |
| 2119 The logical operators in Lua are | |
| 2120 <b>and</b>, <b>or</b>, and <b>not</b>. | |
| 2121 Like the control structures (see <a href="#3.3.4">§3.3.4</a>), | |
| 2122 all logical operators consider both <b>false</b> and <b>nil</b> as false | |
| 2123 and anything else as true. | |
| 2124 | |
| 2125 | |
| 2126 <p> | |
| 2127 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>. | |
| 2128 The conjunction operator <b>and</b> returns its first argument | |
| 2129 if this value is <b>false</b> or <b>nil</b>; | |
| 2130 otherwise, <b>and</b> returns its second argument. | |
| 2131 The disjunction operator <b>or</b> returns its first argument | |
| 2132 if this value is different from <b>nil</b> and <b>false</b>; | |
| 2133 otherwise, <b>or</b> returns its second argument. | |
| 2134 Both <b>and</b> and <b>or</b> use short-cut evaluation; | |
| 2135 that is, | |
| 2136 the second operand is evaluated only if necessary. | |
| 2137 Here are some examples: | |
| 2138 | |
| 2139 <pre> | |
| 2140 10 or 20 --> 10 | |
| 2141 10 or error() --> 10 | |
| 2142 nil or "a" --> "a" | |
| 2143 nil and 10 --> nil | |
| 2144 false and error() --> false | |
| 2145 false and nil --> false | |
| 2146 false or nil --> nil | |
| 2147 10 and 20 --> 20 | |
| 2148 </pre><p> | |
| 2149 (In this manual, | |
| 2150 <code>--></code> indicates the result of the preceding expression.) | |
| 2151 | |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 <h3>3.4.5 – <a name="3.4.5">Concatenation</a></h3><p> | |
| 2157 The string concatenation operator in Lua is | |
| 2158 denoted by two dots ('<code>..</code>'). | |
| 2159 If both operands are strings or numbers, then they are converted to | |
| 2160 strings according to the rules mentioned in <a href="#3.4.2">§3.4.2</a>. | |
| 2161 Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">§2.4</a>). | |
| 2162 | |
| 2163 | |
| 2164 | |
| 2165 | |
| 2166 | |
| 2167 <h3>3.4.6 – <a name="3.4.6">The Length Operator</a></h3> | |
| 2168 | |
| 2169 <p> | |
| 2170 The length operator is denoted by the unary prefix operator <code>#</code>. | |
| 2171 The length of a string is its number of bytes | |
| 2172 (that is, the usual meaning of string length when each | |
| 2173 character is one byte). | |
| 2174 | |
| 2175 | |
| 2176 <p> | |
| 2177 A program can modify the behavior of the length operator for | |
| 2178 any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">§2.4</a>). | |
| 2179 | |
| 2180 | |
| 2181 <p> | |
| 2182 Unless a <code>__len</code> metamethod is given, | |
| 2183 the length of a table <code>t</code> is only defined if the | |
| 2184 table is a <em>sequence</em>, | |
| 2185 that is, | |
| 2186 the set of its positive numeric keys is equal to <em>{1..n}</em> | |
| 2187 for some integer <em>n</em>. | |
| 2188 In that case, <em>n</em> is its length. | |
| 2189 Note that a table like | |
| 2190 | |
| 2191 <pre> | |
| 2192 {10, 20, nil, 40} | |
| 2193 </pre><p> | |
| 2194 is not a sequence, because it has the key <code>4</code> | |
| 2195 but does not have the key <code>3</code>. | |
| 2196 (So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal | |
| 2197 to the set of positive numeric keys of that table.) | |
| 2198 Note, however, that non-numeric keys do not interfere | |
| 2199 with whether a table is a sequence. | |
| 2200 | |
| 2201 | |
| 2202 | |
| 2203 | |
| 2204 | |
| 2205 <h3>3.4.7 – <a name="3.4.7">Precedence</a></h3><p> | |
| 2206 Operator precedence in Lua follows the table below, | |
| 2207 from lower to higher priority: | |
| 2208 | |
| 2209 <pre> | |
| 2210 or | |
| 2211 and | |
| 2212 < > <= >= ~= == | |
| 2213 .. | |
| 2214 + - | |
| 2215 * / % | |
| 2216 not # - (unary) | |
| 2217 ^ | |
| 2218 </pre><p> | |
| 2219 As usual, | |
| 2220 you can use parentheses to change the precedences of an expression. | |
| 2221 The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>') | |
| 2222 operators are right associative. | |
| 2223 All other binary operators are left associative. | |
| 2224 | |
| 2225 | |
| 2226 | |
| 2227 | |
| 2228 | |
| 2229 <h3>3.4.8 – <a name="3.4.8">Table Constructors</a></h3><p> | |
| 2230 Table constructors are expressions that create tables. | |
| 2231 Every time a constructor is evaluated, a new table is created. | |
| 2232 A constructor can be used to create an empty table | |
| 2233 or to create a table and initialize some of its fields. | |
| 2234 The general syntax for constructors is | |
| 2235 | |
| 2236 <pre> | |
| 2237 tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ | |
| 2238 fieldlist ::= field {fieldsep field} [fieldsep] | |
| 2239 field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | |
| 2240 fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | |
| 2241 </pre> | |
| 2242 | |
| 2243 <p> | |
| 2244 Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry | |
| 2245 with key <code>exp1</code> and value <code>exp2</code>. | |
| 2246 A field of the form <code>name = exp</code> is equivalent to | |
| 2247 <code>["name"] = exp</code>. | |
| 2248 Finally, fields of the form <code>exp</code> are equivalent to | |
| 2249 <code>[i] = exp</code>, where <code>i</code> are consecutive numerical integers, | |
| 2250 starting with 1. | |
| 2251 Fields in the other formats do not affect this counting. | |
| 2252 For example, | |
| 2253 | |
| 2254 <pre> | |
| 2255 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } | |
| 2256 </pre><p> | |
| 2257 is equivalent to | |
| 2258 | |
| 2259 <pre> | |
| 2260 do | |
| 2261 local t = {} | |
| 2262 t[f(1)] = g | |
| 2263 t[1] = "x" -- 1st exp | |
| 2264 t[2] = "y" -- 2nd exp | |
| 2265 t.x = 1 -- t["x"] = 1 | |
| 2266 t[3] = f(x) -- 3rd exp | |
| 2267 t[30] = 23 | |
| 2268 t[4] = 45 -- 4th exp | |
| 2269 a = t | |
| 2270 end | |
| 2271 </pre> | |
| 2272 | |
| 2273 <p> | |
| 2274 If the last field in the list has the form <code>exp</code> | |
| 2275 and the expression is a function call or a vararg expression, | |
| 2276 then all values returned by this expression enter the list consecutively | |
| 2277 (see <a href="#3.4.9">§3.4.9</a>). | |
| 2278 | |
| 2279 | |
| 2280 <p> | |
| 2281 The field list can have an optional trailing separator, | |
| 2282 as a convenience for machine-generated code. | |
| 2283 | |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | |
| 2288 <h3>3.4.9 – <a name="3.4.9">Function Calls</a></h3><p> | |
| 2289 A function call in Lua has the following syntax: | |
| 2290 | |
| 2291 <pre> | |
| 2292 functioncall ::= prefixexp args | |
| 2293 </pre><p> | |
| 2294 In a function call, | |
| 2295 first prefixexp and args are evaluated. | |
| 2296 If the value of prefixexp has type <em>function</em>, | |
| 2297 then this function is called | |
| 2298 with the given arguments. | |
| 2299 Otherwise, the prefixexp "call" metamethod is called, | |
| 2300 having as first parameter the value of prefixexp, | |
| 2301 followed by the original call arguments | |
| 2302 (see <a href="#2.4">§2.4</a>). | |
| 2303 | |
| 2304 | |
| 2305 <p> | |
| 2306 The form | |
| 2307 | |
| 2308 <pre> | |
| 2309 functioncall ::= prefixexp ‘<b>:</b>’ Name args | |
| 2310 </pre><p> | |
| 2311 can be used to call "methods". | |
| 2312 A call <code>v:name(<em>args</em>)</code> | |
| 2313 is syntactic sugar for <code>v.name(v,<em>args</em>)</code>, | |
| 2314 except that <code>v</code> is evaluated only once. | |
| 2315 | |
| 2316 | |
| 2317 <p> | |
| 2318 Arguments have the following syntax: | |
| 2319 | |
| 2320 <pre> | |
| 2321 args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | |
| 2322 args ::= tableconstructor | |
| 2323 args ::= String | |
| 2324 </pre><p> | |
| 2325 All argument expressions are evaluated before the call. | |
| 2326 A call of the form <code>f{<em>fields</em>}</code> is | |
| 2327 syntactic sugar for <code>f({<em>fields</em>})</code>; | |
| 2328 that is, the argument list is a single new table. | |
| 2329 A call of the form <code>f'<em>string</em>'</code> | |
| 2330 (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>) | |
| 2331 is syntactic sugar for <code>f('<em>string</em>')</code>; | |
| 2332 that is, the argument list is a single literal string. | |
| 2333 | |
| 2334 | |
| 2335 <p> | |
| 2336 A call of the form <code>return <em>functioncall</em></code> is called | |
| 2337 a <em>tail call</em>. | |
| 2338 Lua implements <em>proper tail calls</em> | |
| 2339 (or <em>proper tail recursion</em>): | |
| 2340 in a tail call, | |
| 2341 the called function reuses the stack entry of the calling function. | |
| 2342 Therefore, there is no limit on the number of nested tail calls that | |
| 2343 a program can execute. | |
| 2344 However, a tail call erases any debug information about the | |
| 2345 calling function. | |
| 2346 Note that a tail call only happens with a particular syntax, | |
| 2347 where the <b>return</b> has one single function call as argument; | |
| 2348 this syntax makes the calling function return exactly | |
| 2349 the returns of the called function. | |
| 2350 So, none of the following examples are tail calls: | |
| 2351 | |
| 2352 <pre> | |
| 2353 return (f(x)) -- results adjusted to 1 | |
| 2354 return 2 * f(x) | |
| 2355 return x, f(x) -- additional results | |
| 2356 f(x); return -- results discarded | |
| 2357 return x or f(x) -- results adjusted to 1 | |
| 2358 </pre> | |
| 2359 | |
| 2360 | |
| 2361 | |
| 2362 | |
| 2363 <h3>3.4.10 – <a name="3.4.10">Function Definitions</a></h3> | |
| 2364 | |
| 2365 <p> | |
| 2366 The syntax for function definition is | |
| 2367 | |
| 2368 <pre> | |
| 2369 functiondef ::= <b>function</b> funcbody | |
| 2370 funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> | |
| 2371 </pre> | |
| 2372 | |
| 2373 <p> | |
| 2374 The following syntactic sugar simplifies function definitions: | |
| 2375 | |
| 2376 <pre> | |
| 2377 stat ::= <b>function</b> funcname funcbody | |
| 2378 stat ::= <b>local</b> <b>function</b> Name funcbody | |
| 2379 funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | |
| 2380 </pre><p> | |
| 2381 The statement | |
| 2382 | |
| 2383 <pre> | |
| 2384 function f () <em>body</em> end | |
| 2385 </pre><p> | |
| 2386 translates to | |
| 2387 | |
| 2388 <pre> | |
| 2389 f = function () <em>body</em> end | |
| 2390 </pre><p> | |
| 2391 The statement | |
| 2392 | |
| 2393 <pre> | |
| 2394 function t.a.b.c.f () <em>body</em> end | |
| 2395 </pre><p> | |
| 2396 translates to | |
| 2397 | |
| 2398 <pre> | |
| 2399 t.a.b.c.f = function () <em>body</em> end | |
| 2400 </pre><p> | |
| 2401 The statement | |
| 2402 | |
| 2403 <pre> | |
| 2404 local function f () <em>body</em> end | |
| 2405 </pre><p> | |
| 2406 translates to | |
| 2407 | |
| 2408 <pre> | |
| 2409 local f; f = function () <em>body</em> end | |
| 2410 </pre><p> | |
| 2411 not to | |
| 2412 | |
| 2413 <pre> | |
| 2414 local f = function () <em>body</em> end | |
| 2415 </pre><p> | |
| 2416 (This only makes a difference when the body of the function | |
| 2417 contains references to <code>f</code>.) | |
| 2418 | |
| 2419 | |
| 2420 <p> | |
| 2421 A function definition is an executable expression, | |
| 2422 whose value has type <em>function</em>. | |
| 2423 When Lua precompiles a chunk, | |
| 2424 all its function bodies are precompiled too. | |
| 2425 Then, whenever Lua executes the function definition, | |
| 2426 the function is <em>instantiated</em> (or <em>closed</em>). | |
| 2427 This function instance (or <em>closure</em>) | |
| 2428 is the final value of the expression. | |
| 2429 | |
| 2430 | |
| 2431 <p> | |
| 2432 Parameters act as local variables that are | |
| 2433 initialized with the argument values: | |
| 2434 | |
| 2435 <pre> | |
| 2436 parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ | |
| 2437 </pre><p> | |
| 2438 When a function is called, | |
| 2439 the list of arguments is adjusted to | |
| 2440 the length of the list of parameters, | |
| 2441 unless the function is a <em>vararg function</em>, | |
| 2442 which is indicated by three dots ('<code>...</code>') | |
| 2443 at the end of its parameter list. | |
| 2444 A vararg function does not adjust its argument list; | |
| 2445 instead, it collects all extra arguments and supplies them | |
| 2446 to the function through a <em>vararg expression</em>, | |
| 2447 which is also written as three dots. | |
| 2448 The value of this expression is a list of all actual extra arguments, | |
| 2449 similar to a function with multiple results. | |
| 2450 If a vararg expression is used inside another expression | |
| 2451 or in the middle of a list of expressions, | |
| 2452 then its return list is adjusted to one element. | |
| 2453 If the expression is used as the last element of a list of expressions, | |
| 2454 then no adjustment is made | |
| 2455 (unless that last expression is enclosed in parentheses). | |
| 2456 | |
| 2457 | |
| 2458 <p> | |
| 2459 As an example, consider the following definitions: | |
| 2460 | |
| 2461 <pre> | |
| 2462 function f(a, b) end | |
| 2463 function g(a, b, ...) end | |
| 2464 function r() return 1,2,3 end | |
| 2465 </pre><p> | |
| 2466 Then, we have the following mapping from arguments to parameters and | |
| 2467 to the vararg expression: | |
| 2468 | |
| 2469 <pre> | |
| 2470 CALL PARAMETERS | |
| 2471 | |
| 2472 f(3) a=3, b=nil | |
| 2473 f(3, 4) a=3, b=4 | |
| 2474 f(3, 4, 5) a=3, b=4 | |
| 2475 f(r(), 10) a=1, b=10 | |
| 2476 f(r()) a=1, b=2 | |
| 2477 | |
| 2478 g(3) a=3, b=nil, ... --> (nothing) | |
| 2479 g(3, 4) a=3, b=4, ... --> (nothing) | |
| 2480 g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 | |
| 2481 g(5, r()) a=5, b=1, ... --> 2 3 | |
| 2482 </pre> | |
| 2483 | |
| 2484 <p> | |
| 2485 Results are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>). | |
| 2486 If control reaches the end of a function | |
| 2487 without encountering a <b>return</b> statement, | |
| 2488 then the function returns with no results. | |
| 2489 | |
| 2490 | |
| 2491 <p> | |
| 2492 | |
| 2493 There is a system-dependent limit on the number of values | |
| 2494 that a function may return. | |
| 2495 This limit is guaranteed to be larger than 1000. | |
| 2496 | |
| 2497 | |
| 2498 <p> | |
| 2499 The <em>colon</em> syntax | |
| 2500 is used for defining <em>methods</em>, | |
| 2501 that is, functions that have an implicit extra parameter <code>self</code>. | |
| 2502 Thus, the statement | |
| 2503 | |
| 2504 <pre> | |
| 2505 function t.a.b.c:f (<em>params</em>) <em>body</em> end | |
| 2506 </pre><p> | |
| 2507 is syntactic sugar for | |
| 2508 | |
| 2509 <pre> | |
| 2510 t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end | |
| 2511 </pre> | |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 <h2>3.5 – <a name="3.5">Visibility Rules</a></h2> | |
| 2519 | |
| 2520 <p> | |
| 2521 | |
| 2522 Lua is a lexically scoped language. | |
| 2523 The scope of a local variable begins at the first statement after | |
| 2524 its declaration and lasts until the last non-void statement | |
| 2525 of the innermost block that includes the declaration. | |
| 2526 Consider the following example: | |
| 2527 | |
| 2528 <pre> | |
| 2529 x = 10 -- global variable | |
| 2530 do -- new block | |
| 2531 local x = x -- new 'x', with value 10 | |
| 2532 print(x) --> 10 | |
| 2533 x = x+1 | |
| 2534 do -- another block | |
| 2535 local x = x+1 -- another 'x' | |
| 2536 print(x) --> 12 | |
| 2537 end | |
| 2538 print(x) --> 11 | |
| 2539 end | |
| 2540 print(x) --> 10 (the global one) | |
| 2541 </pre> | |
| 2542 | |
| 2543 <p> | |
| 2544 Notice that, in a declaration like <code>local x = x</code>, | |
| 2545 the new <code>x</code> being declared is not in scope yet, | |
| 2546 and so the second <code>x</code> refers to the outside variable. | |
| 2547 | |
| 2548 | |
| 2549 <p> | |
| 2550 Because of the lexical scoping rules, | |
| 2551 local variables can be freely accessed by functions | |
| 2552 defined inside their scope. | |
| 2553 A local variable used by an inner function is called | |
| 2554 an <em>upvalue</em>, or <em>external local variable</em>, | |
| 2555 inside the inner function. | |
| 2556 | |
| 2557 | |
| 2558 <p> | |
| 2559 Notice that each execution of a <b>local</b> statement | |
| 2560 defines new local variables. | |
| 2561 Consider the following example: | |
| 2562 | |
| 2563 <pre> | |
| 2564 a = {} | |
| 2565 local x = 20 | |
| 2566 for i=1,10 do | |
| 2567 local y = 0 | |
| 2568 a[i] = function () y=y+1; return x+y end | |
| 2569 end | |
| 2570 </pre><p> | |
| 2571 The loop creates ten closures | |
| 2572 (that is, ten instances of the anonymous function). | |
| 2573 Each of these closures uses a different <code>y</code> variable, | |
| 2574 while all of them share the same <code>x</code>. | |
| 2575 | |
| 2576 | |
| 2577 | |
| 2578 | |
| 2579 | |
| 2580 <h1>4 – <a name="4">The Application Program Interface</a></h1> | |
| 2581 | |
| 2582 <p> | |
| 2583 | |
| 2584 This section describes the C API for Lua, that is, | |
| 2585 the set of C functions available to the host program to communicate | |
| 2586 with Lua. | |
| 2587 All API functions and related types and constants | |
| 2588 are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>. | |
| 2589 | |
| 2590 | |
| 2591 <p> | |
| 2592 Even when we use the term "function", | |
| 2593 any facility in the API may be provided as a macro instead. | |
| 2594 Except where stated otherwise, | |
| 2595 all such macros use each of their arguments exactly once | |
| 2596 (except for the first argument, which is always a Lua state), | |
| 2597 and so do not generate any hidden side-effects. | |
| 2598 | |
| 2599 | |
| 2600 <p> | |
| 2601 As in most C libraries, | |
| 2602 the Lua API functions do not check their arguments for validity or consistency. | |
| 2603 However, you can change this behavior by compiling Lua | |
| 2604 with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined. | |
| 2605 | |
| 2606 | |
| 2607 | |
| 2608 <h2>4.1 – <a name="4.1">The Stack</a></h2> | |
| 2609 | |
| 2610 <p> | |
| 2611 Lua uses a <em>virtual stack</em> to pass values to and from C. | |
| 2612 Each element in this stack represents a Lua value | |
| 2613 (<b>nil</b>, number, string, etc.). | |
| 2614 | |
| 2615 | |
| 2616 <p> | |
| 2617 Whenever Lua calls C, the called function gets a new stack, | |
| 2618 which is independent of previous stacks and of stacks of | |
| 2619 C functions that are still active. | |
| 2620 This stack initially contains any arguments to the C function | |
| 2621 and it is where the C function pushes its results | |
| 2622 to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | |
| 2623 | |
| 2624 | |
| 2625 <p> | |
| 2626 For convenience, | |
| 2627 most query operations in the API do not follow a strict stack discipline. | |
| 2628 Instead, they can refer to any element in the stack | |
| 2629 by using an <em>index</em>: | |
| 2630 A positive index represents an absolute stack position | |
| 2631 (starting at 1); | |
| 2632 a negative index represents an offset relative to the top of the stack. | |
| 2633 More specifically, if the stack has <em>n</em> elements, | |
| 2634 then index 1 represents the first element | |
| 2635 (that is, the element that was pushed onto the stack first) | |
| 2636 and | |
| 2637 index <em>n</em> represents the last element; | |
| 2638 index -1 also represents the last element | |
| 2639 (that is, the element at the top) | |
| 2640 and index <em>-n</em> represents the first element. | |
| 2641 | |
| 2642 | |
| 2643 | |
| 2644 | |
| 2645 | |
| 2646 <h2>4.2 – <a name="4.2">Stack Size</a></h2> | |
| 2647 | |
| 2648 <p> | |
| 2649 When you interact with the Lua API, | |
| 2650 you are responsible for ensuring consistency. | |
| 2651 In particular, | |
| 2652 <em>you are responsible for controlling stack overflow</em>. | |
| 2653 You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a> | |
| 2654 to ensure that the stack has extra slots when pushing new elements. | |
| 2655 | |
| 2656 | |
| 2657 <p> | |
| 2658 Whenever Lua calls C, | |
| 2659 it ensures that the stack has at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots. | |
| 2660 <code>LUA_MINSTACK</code> is defined as 20, | |
| 2661 so that usually you do not have to worry about stack space | |
| 2662 unless your code has loops pushing elements onto the stack. | |
| 2663 | |
| 2664 | |
| 2665 <p> | |
| 2666 When you call a Lua function | |
| 2667 without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>), | |
| 2668 Lua ensures that the stack has enough size for all results, | |
| 2669 but it does not ensure any extra space. | |
| 2670 So, before pushing anything in the stack after such a call | |
| 2671 you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>. | |
| 2672 | |
| 2673 | |
| 2674 | |
| 2675 | |
| 2676 | |
| 2677 <h2>4.3 – <a name="4.3">Valid and Acceptable Indices</a></h2> | |
| 2678 | |
| 2679 <p> | |
| 2680 Any function in the API that receives stack indices | |
| 2681 works only with <em>valid indices</em> or <em>acceptable indices</em>. | |
| 2682 | |
| 2683 | |
| 2684 <p> | |
| 2685 A <em>valid index</em> is an index that refers to a | |
| 2686 real position within the stack, that is, | |
| 2687 its position lies between 1 and the stack top | |
| 2688 (<code>1 ≤ abs(index) ≤ top</code>). | |
| 2689 | |
| 2690 Usually, functions that can modify the value at an index | |
| 2691 require valid indices. | |
| 2692 | |
| 2693 | |
| 2694 <p> | |
| 2695 Unless otherwise noted, | |
| 2696 any function that accepts valid indices also accepts <em>pseudo-indices</em>, | |
| 2697 which represent some Lua values that are accessible to C code | |
| 2698 but which are not in the stack. | |
| 2699 Pseudo-indices are used to access the registry | |
| 2700 and the upvalues of a C function (see <a href="#4.4">§4.4</a>). | |
| 2701 | |
| 2702 | |
| 2703 <p> | |
| 2704 Functions that do not need a specific stack position, | |
| 2705 but only a value in the stack (e.g., query functions), | |
| 2706 can be called with acceptable indices. | |
| 2707 An <em>acceptable index</em> can be any valid index, | |
| 2708 including the pseudo-indices, | |
| 2709 but it also can be any positive index after the stack top | |
| 2710 within the space allocated for the stack, | |
| 2711 that is, indices up to the stack size. | |
| 2712 (Note that 0 is never an acceptable index.) | |
| 2713 Except when noted otherwise, | |
| 2714 functions in the API work with acceptable indices. | |
| 2715 | |
| 2716 | |
| 2717 <p> | |
| 2718 Acceptable indices serve to avoid extra tests | |
| 2719 against the stack top when querying the stack. | |
| 2720 For instance, a C function can query its third argument | |
| 2721 without the need to first check whether there is a third argument, | |
| 2722 that is, without the need to check whether 3 is a valid index. | |
| 2723 | |
| 2724 | |
| 2725 <p> | |
| 2726 For functions that can be called with acceptable indices, | |
| 2727 any non-valid index is treated as if it | |
| 2728 contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>, | |
| 2729 which behaves like a nil value. | |
| 2730 | |
| 2731 | |
| 2732 | |
| 2733 | |
| 2734 | |
| 2735 <h2>4.4 – <a name="4.4">C Closures</a></h2> | |
| 2736 | |
| 2737 <p> | |
| 2738 When a C function is created, | |
| 2739 it is possible to associate some values with it, | |
| 2740 thus creating a <em>C closure</em> | |
| 2741 (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>); | |
| 2742 these values are called <em>upvalues</em> and are | |
| 2743 accessible to the function whenever it is called. | |
| 2744 | |
| 2745 | |
| 2746 <p> | |
| 2747 Whenever a C function is called, | |
| 2748 its upvalues are located at specific pseudo-indices. | |
| 2749 These pseudo-indices are produced by the macro | |
| 2750 <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>. | |
| 2751 The first value associated with a function is at position | |
| 2752 <code>lua_upvalueindex(1)</code>, and so on. | |
| 2753 Any access to <code>lua_upvalueindex(<em>n</em>)</code>, | |
| 2754 where <em>n</em> is greater than the number of upvalues of the | |
| 2755 current function (but not greater than 256), | |
| 2756 produces an acceptable but invalid index. | |
| 2757 | |
| 2758 | |
| 2759 | |
| 2760 | |
| 2761 | |
| 2762 <h2>4.5 – <a name="4.5">Registry</a></h2> | |
| 2763 | |
| 2764 <p> | |
| 2765 Lua provides a <em>registry</em>, | |
| 2766 a predefined table that can be used by any C code to | |
| 2767 store whatever Lua values it needs to store. | |
| 2768 The registry table is always located at pseudo-index | |
| 2769 <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>, | |
| 2770 which is a valid index. | |
| 2771 Any C library can store data into this table, | |
| 2772 but it should take care to choose keys | |
| 2773 that are different from those used | |
| 2774 by other libraries, to avoid collisions. | |
| 2775 Typically, you should use as key a string containing your library name, | |
| 2776 or a light userdata with the address of a C object in your code, | |
| 2777 or any Lua object created by your code. | |
| 2778 As with global names, | |
| 2779 string keys starting with an underscore followed by | |
| 2780 uppercase letters are reserved for Lua. | |
| 2781 | |
| 2782 | |
| 2783 <p> | |
| 2784 The integer keys in the registry are used by the reference mechanism, | |
| 2785 implemented by the auxiliary library, | |
| 2786 and by some predefined values. | |
| 2787 Therefore, integer keys should not be used for other purposes. | |
| 2788 | |
| 2789 | |
| 2790 <p> | |
| 2791 When you create a new Lua state, | |
| 2792 its registry comes with some predefined values. | |
| 2793 These predefined values are indexed with integer keys | |
| 2794 defined as constants in <code>lua.h</code>. | |
| 2795 The following constants are defined: | |
| 2796 | |
| 2797 <ul> | |
| 2798 <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has | |
| 2799 the main thread of the state. | |
| 2800 (The main thread is the one created together with the state.) | |
| 2801 </li> | |
| 2802 | |
| 2803 <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has | |
| 2804 the global environment. | |
| 2805 </li> | |
| 2806 </ul> | |
| 2807 | |
| 2808 | |
| 2809 | |
| 2810 | |
| 2811 <h2>4.6 – <a name="4.6">Error Handling in C</a></h2> | |
| 2812 | |
| 2813 <p> | |
| 2814 Internally, Lua uses the C <code>longjmp</code> facility to handle errors. | |
| 2815 (You can also choose to use exceptions if you compile Lua as C++; | |
| 2816 search for <code>LUAI_THROW</code> in the source code.) | |
| 2817 When Lua faces any error | |
| 2818 (such as a memory allocation error, type errors, syntax errors, | |
| 2819 and runtime errors) | |
| 2820 it <em>raises</em> an error; | |
| 2821 that is, it does a long jump. | |
| 2822 A <em>protected environment</em> uses <code>setjmp</code> | |
| 2823 to set a recovery point; | |
| 2824 any error jumps to the most recent active recovery point. | |
| 2825 | |
| 2826 | |
| 2827 <p> | |
| 2828 If an error happens outside any protected environment, | |
| 2829 Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>) | |
| 2830 and then calls <code>abort</code>, | |
| 2831 thus exiting the host application. | |
| 2832 Your panic function can avoid this exit by | |
| 2833 never returning | |
| 2834 (e.g., doing a long jump to your own recovery point outside Lua). | |
| 2835 | |
| 2836 | |
| 2837 <p> | |
| 2838 The panic function runs as if it were a message handler (see <a href="#2.3">§2.3</a>); | |
| 2839 in particular, the error message is at the top of the stack. | |
| 2840 However, there is no guarantees about stack space. | |
| 2841 To push anything on the stack, | |
| 2842 the panic function should first check the available space (see <a href="#4.2">§4.2</a>). | |
| 2843 | |
| 2844 | |
| 2845 <p> | |
| 2846 Most functions in the API can throw an error, | |
| 2847 for instance due to a memory allocation error. | |
| 2848 The documentation for each function indicates whether | |
| 2849 it can throw errors. | |
| 2850 | |
| 2851 | |
| 2852 <p> | |
| 2853 Inside a C function you can throw an error by calling <a href="#lua_error"><code>lua_error</code></a>. | |
| 2854 | |
| 2855 | |
| 2856 | |
| 2857 | |
| 2858 | |
| 2859 <h2>4.7 – <a name="4.7">Handling Yields in C</a></h2> | |
| 2860 | |
| 2861 <p> | |
| 2862 Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine. | |
| 2863 Therefore, if a function <code>foo</code> calls an API function | |
| 2864 and this API function yields | |
| 2865 (directly or indirectly by calling another function that yields), | |
| 2866 Lua cannot return to <code>foo</code> any more, | |
| 2867 because the <code>longjmp</code> removes its frame from the C stack. | |
| 2868 | |
| 2869 | |
| 2870 <p> | |
| 2871 To avoid this kind of problem, | |
| 2872 Lua raises an error whenever it tries to yield across an API call, | |
| 2873 except for three functions: | |
| 2874 <a href="#lua_yieldk"><code>lua_yieldk</code></a>, <a href="#lua_callk"><code>lua_callk</code></a>, and <a href="#lua_pcallk"><code>lua_pcallk</code></a>. | |
| 2875 All those functions receive a <em>continuation function</em> | |
| 2876 (as a parameter called <code>k</code>) to continue execution after a yield. | |
| 2877 | |
| 2878 | |
| 2879 <p> | |
| 2880 We need to set some terminology to explain continuations. | |
| 2881 We have a C function called from Lua which we will call | |
| 2882 the <em>original function</em>. | |
| 2883 This original function then calls one of those three functions in the C API, | |
| 2884 which we will call the <em>callee function</em>, | |
| 2885 that then yields the current thread. | |
| 2886 (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | |
| 2887 or when the callee function is either <a href="#lua_callk"><code>lua_callk</code></a> or <a href="#lua_pcallk"><code>lua_pcallk</code></a> | |
| 2888 and the function called by them yields.) | |
| 2889 | |
| 2890 | |
| 2891 <p> | |
| 2892 Suppose the running thread yields while executing the callee function. | |
| 2893 After the thread resumes, | |
| 2894 it eventually will finish running the callee function. | |
| 2895 However, | |
| 2896 the callee function cannot return to the original function, | |
| 2897 because its frame in the C stack was destroyed by the yield. | |
| 2898 Instead, Lua calls a <em>continuation function</em>, | |
| 2899 which was given as an argument to the callee function. | |
| 2900 As the name implies, | |
| 2901 the continuation function should continue the task | |
| 2902 of the original function. | |
| 2903 | |
| 2904 | |
| 2905 <p> | |
| 2906 Lua treats the continuation function as if it were the original function. | |
| 2907 The continuation function receives the same Lua stack | |
| 2908 from the original function, | |
| 2909 in the same state it would be if the callee function had returned. | |
| 2910 (For instance, | |
| 2911 after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are | |
| 2912 removed from the stack and replaced by the results from the call.) | |
| 2913 It also has the same upvalues. | |
| 2914 Whatever it returns is handled by Lua as if it were the return | |
| 2915 of the original function. | |
| 2916 | |
| 2917 | |
| 2918 <p> | |
| 2919 The only difference in the Lua state between the original function | |
| 2920 and its continuation is the result of a call to <a href="#lua_getctx"><code>lua_getctx</code></a>. | |
| 2921 | |
| 2922 | |
| 2923 | |
| 2924 | |
| 2925 | |
| 2926 <h2>4.8 – <a name="4.8">Functions and Types</a></h2> | |
| 2927 | |
| 2928 <p> | |
| 2929 Here we list all functions and types from the C API in | |
| 2930 alphabetical order. | |
| 2931 Each function has an indicator like this: | |
| 2932 <span class="apii">[-o, +p, <em>x</em>]</span> | |
| 2933 | |
| 2934 | |
| 2935 <p> | |
| 2936 The first field, <code>o</code>, | |
| 2937 is how many elements the function pops from the stack. | |
| 2938 The second field, <code>p</code>, | |
| 2939 is how many elements the function pushes onto the stack. | |
| 2940 (Any function always pushes its results after popping its arguments.) | |
| 2941 A field in the form <code>x|y</code> means the function can push (or pop) | |
| 2942 <code>x</code> or <code>y</code> elements, | |
| 2943 depending on the situation; | |
| 2944 an interrogation mark '<code>?</code>' means that | |
| 2945 we cannot know how many elements the function pops/pushes | |
| 2946 by looking only at its arguments | |
| 2947 (e.g., they may depend on what is on the stack). | |
| 2948 The third field, <code>x</code>, | |
| 2949 tells whether the function may throw errors: | |
| 2950 '<code>-</code>' means the function never throws any error; | |
| 2951 '<code>e</code>' means the function may throw errors; | |
| 2952 '<code>v</code>' means the function may throw an error on purpose. | |
| 2953 | |
| 2954 | |
| 2955 | |
| 2956 <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p> | |
| 2957 <span class="apii">[-0, +0, –]</span> | |
| 2958 <pre>int lua_absindex (lua_State *L, int idx);</pre> | |
| 2959 | |
| 2960 <p> | |
| 2961 Converts the acceptable index <code>idx</code> into an absolute index | |
| 2962 (that is, one that does not depend on the stack top). | |
| 2963 | |
| 2964 | |
| 2965 | |
| 2966 | |
| 2967 | |
| 2968 <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3> | |
| 2969 <pre>typedef void * (*lua_Alloc) (void *ud, | |
| 2970 void *ptr, | |
| 2971 size_t osize, | |
| 2972 size_t nsize);</pre> | |
| 2973 | |
| 2974 <p> | |
| 2975 The type of the memory-allocation function used by Lua states. | |
| 2976 The allocator function must provide a | |
| 2977 functionality similar to <code>realloc</code>, | |
| 2978 but not exactly the same. | |
| 2979 Its arguments are | |
| 2980 <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>; | |
| 2981 <code>ptr</code>, a pointer to the block being allocated/reallocated/freed; | |
| 2982 <code>osize</code>, the original size of the block or some code about what | |
| 2983 is being allocated; | |
| 2984 <code>nsize</code>, the new size of the block. | |
| 2985 | |
| 2986 | |
| 2987 <p> | |
| 2988 When <code>ptr</code> is not <code>NULL</code>, | |
| 2989 <code>osize</code> is the size of the block pointed by <code>ptr</code>, | |
| 2990 that is, the size given when it was allocated or reallocated. | |
| 2991 | |
| 2992 | |
| 2993 <p> | |
| 2994 When <code>ptr</code> is <code>NULL</code>, | |
| 2995 <code>osize</code> encodes the kind of object that Lua is allocating. | |
| 2996 <code>osize</code> is any of | |
| 2997 <a href="#pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, <a href="#pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, <a href="#pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>, | |
| 2998 <a href="#pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, or <a href="#pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a> when (and only when) | |
| 2999 Lua is creating a new object of that type. | |
| 3000 When <code>osize</code> is some other value, | |
| 3001 Lua is allocating memory for something else. | |
| 3002 | |
| 3003 | |
| 3004 <p> | |
| 3005 Lua assumes the following behavior from the allocator function: | |
| 3006 | |
| 3007 | |
| 3008 <p> | |
| 3009 When <code>nsize</code> is zero, | |
| 3010 the allocator should behave like <code>free</code> | |
| 3011 and return <code>NULL</code>. | |
| 3012 | |
| 3013 | |
| 3014 <p> | |
| 3015 When <code>nsize</code> is not zero, | |
| 3016 the allocator should behave like <code>realloc</code>. | |
| 3017 The allocator returns <code>NULL</code> | |
| 3018 if and only if it cannot fulfill the request. | |
| 3019 Lua assumes that the allocator never fails when | |
| 3020 <code>osize >= nsize</code>. | |
| 3021 | |
| 3022 | |
| 3023 <p> | |
| 3024 Here is a simple implementation for the allocator function. | |
| 3025 It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>. | |
| 3026 | |
| 3027 <pre> | |
| 3028 static void *l_alloc (void *ud, void *ptr, size_t osize, | |
| 3029 size_t nsize) { | |
| 3030 (void)ud; (void)osize; /* not used */ | |
| 3031 if (nsize == 0) { | |
| 3032 free(ptr); | |
| 3033 return NULL; | |
| 3034 } | |
| 3035 else | |
| 3036 return realloc(ptr, nsize); | |
| 3037 } | |
| 3038 </pre><p> | |
| 3039 Note that Standard C ensures | |
| 3040 that <code>free(NULL)</code> has no effect and that | |
| 3041 <code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>. | |
| 3042 This code assumes that <code>realloc</code> does not fail when shrinking a block. | |
| 3043 (Although Standard C does not ensure this behavior, | |
| 3044 it seems to be a safe assumption.) | |
| 3045 | |
| 3046 | |
| 3047 | |
| 3048 | |
| 3049 | |
| 3050 <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p> | |
| 3051 <span class="apii">[-(2|1), +1, <em>e</em>]</span> | |
| 3052 <pre>void lua_arith (lua_State *L, int op);</pre> | |
| 3053 | |
| 3054 <p> | |
| 3055 Performs an arithmetic operation over the two values | |
| 3056 (or one, in the case of negation) | |
| 3057 at the top of the stack, | |
| 3058 with the value at the top being the second operand, | |
| 3059 pops these values, and pushes the result of the operation. | |
| 3060 The function follows the semantics of the corresponding Lua operator | |
| 3061 (that is, it may call metamethods). | |
| 3062 | |
| 3063 | |
| 3064 <p> | |
| 3065 The value of <code>op</code> must be one of the following constants: | |
| 3066 | |
| 3067 <ul> | |
| 3068 | |
| 3069 <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li> | |
| 3070 <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li> | |
| 3071 <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li> | |
| 3072 <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs division (<code>/</code>)</li> | |
| 3073 <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li> | |
| 3074 <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li> | |
| 3075 <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li> | |
| 3076 | |
| 3077 </ul> | |
| 3078 | |
| 3079 | |
| 3080 | |
| 3081 | |
| 3082 <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p> | |
| 3083 <span class="apii">[-0, +0, –]</span> | |
| 3084 <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre> | |
| 3085 | |
| 3086 <p> | |
| 3087 Sets a new panic function and returns the old one (see <a href="#4.6">§4.6</a>). | |
| 3088 | |
| 3089 | |
| 3090 | |
| 3091 | |
| 3092 | |
| 3093 <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p> | |
| 3094 <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span> | |
| 3095 <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre> | |
| 3096 | |
| 3097 <p> | |
| 3098 Calls a function. | |
| 3099 | |
| 3100 | |
| 3101 <p> | |
| 3102 To call a function you must use the following protocol: | |
| 3103 first, the function to be called is pushed onto the stack; | |
| 3104 then, the arguments to the function are pushed | |
| 3105 in direct order; | |
| 3106 that is, the first argument is pushed first. | |
| 3107 Finally you call <a href="#lua_call"><code>lua_call</code></a>; | |
| 3108 <code>nargs</code> is the number of arguments that you pushed onto the stack. | |
| 3109 All arguments and the function value are popped from the stack | |
| 3110 when the function is called. | |
| 3111 The function results are pushed onto the stack when the function returns. | |
| 3112 The number of results is adjusted to <code>nresults</code>, | |
| 3113 unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>. | |
| 3114 In this case, all results from the function are pushed. | |
| 3115 Lua takes care that the returned values fit into the stack space. | |
| 3116 The function results are pushed onto the stack in direct order | |
| 3117 (the first result is pushed first), | |
| 3118 so that after the call the last result is on the top of the stack. | |
| 3119 | |
| 3120 | |
| 3121 <p> | |
| 3122 Any error inside the called function is propagated upwards | |
| 3123 (with a <code>longjmp</code>). | |
| 3124 | |
| 3125 | |
| 3126 <p> | |
| 3127 The following example shows how the host program can do the | |
| 3128 equivalent to this Lua code: | |
| 3129 | |
| 3130 <pre> | |
| 3131 a = f("how", t.x, 14) | |
| 3132 </pre><p> | |
| 3133 Here it is in C: | |
| 3134 | |
| 3135 <pre> | |
| 3136 lua_getglobal(L, "f"); /* function to be called */ | |
| 3137 lua_pushstring(L, "how"); /* 1st argument */ | |
| 3138 lua_getglobal(L, "t"); /* table to be indexed */ | |
| 3139 lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ | |
| 3140 lua_remove(L, -2); /* remove 't' from the stack */ | |
| 3141 lua_pushinteger(L, 14); /* 3rd argument */ | |
| 3142 lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ | |
| 3143 lua_setglobal(L, "a"); /* set global 'a' */ | |
| 3144 </pre><p> | |
| 3145 Note that the code above is "balanced": | |
| 3146 at its end, the stack is back to its original configuration. | |
| 3147 This is considered good programming practice. | |
| 3148 | |
| 3149 | |
| 3150 | |
| 3151 | |
| 3152 | |
| 3153 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p> | |
| 3154 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span> | |
| 3155 <pre>void lua_callk (lua_State *L, int nargs, int nresults, int ctx, | |
| 3156 lua_CFunction k);</pre> | |
| 3157 | |
| 3158 <p> | |
| 3159 This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>, | |
| 3160 but allows the called function to yield (see <a href="#4.7">§4.7</a>). | |
| 3161 | |
| 3162 | |
| 3163 | |
| 3164 | |
| 3165 | |
| 3166 <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3> | |
| 3167 <pre>typedef int (*lua_CFunction) (lua_State *L);</pre> | |
| 3168 | |
| 3169 <p> | |
| 3170 Type for C functions. | |
| 3171 | |
| 3172 | |
| 3173 <p> | |
| 3174 In order to communicate properly with Lua, | |
| 3175 a C function must use the following protocol, | |
| 3176 which defines the way parameters and results are passed: | |
| 3177 a C function receives its arguments from Lua in its stack | |
| 3178 in direct order (the first argument is pushed first). | |
| 3179 So, when the function starts, | |
| 3180 <code>lua_gettop(L)</code> returns the number of arguments received by the function. | |
| 3181 The first argument (if any) is at index 1 | |
| 3182 and its last argument is at index <code>lua_gettop(L)</code>. | |
| 3183 To return values to Lua, a C function just pushes them onto the stack, | |
| 3184 in direct order (the first result is pushed first), | |
| 3185 and returns the number of results. | |
| 3186 Any other value in the stack below the results will be properly | |
| 3187 discarded by Lua. | |
| 3188 Like a Lua function, a C function called by Lua can also return | |
| 3189 many results. | |
| 3190 | |
| 3191 | |
| 3192 <p> | |
| 3193 As an example, the following function receives a variable number | |
| 3194 of numerical arguments and returns their average and sum: | |
| 3195 | |
| 3196 <pre> | |
| 3197 static int foo (lua_State *L) { | |
| 3198 int n = lua_gettop(L); /* number of arguments */ | |
| 3199 lua_Number sum = 0; | |
| 3200 int i; | |
| 3201 for (i = 1; i <= n; i++) { | |
| 3202 if (!lua_isnumber(L, i)) { | |
| 3203 lua_pushstring(L, "incorrect argument"); | |
| 3204 lua_error(L); | |
| 3205 } | |
| 3206 sum += lua_tonumber(L, i); | |
| 3207 } | |
| 3208 lua_pushnumber(L, sum/n); /* first result */ | |
| 3209 lua_pushnumber(L, sum); /* second result */ | |
| 3210 return 2; /* number of results */ | |
| 3211 } | |
| 3212 </pre> | |
| 3213 | |
| 3214 | |
| 3215 | |
| 3216 | |
| 3217 <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p> | |
| 3218 <span class="apii">[-0, +0, –]</span> | |
| 3219 <pre>int lua_checkstack (lua_State *L, int extra);</pre> | |
| 3220 | |
| 3221 <p> | |
| 3222 Ensures that there are at least <code>extra</code> free stack slots in the stack. | |
| 3223 It returns false if it cannot fulfill the request, | |
| 3224 because it would cause the stack to be larger than a fixed maximum size | |
| 3225 (typically at least a few thousand elements) or | |
| 3226 because it cannot allocate memory for the new stack size. | |
| 3227 This function never shrinks the stack; | |
| 3228 if the stack is already larger than the new size, | |
| 3229 it is left unchanged. | |
| 3230 | |
| 3231 | |
| 3232 | |
| 3233 | |
| 3234 | |
| 3235 <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p> | |
| 3236 <span class="apii">[-0, +0, –]</span> | |
| 3237 <pre>void lua_close (lua_State *L);</pre> | |
| 3238 | |
| 3239 <p> | |
| 3240 Destroys all objects in the given Lua state | |
| 3241 (calling the corresponding garbage-collection metamethods, if any) | |
| 3242 and frees all dynamic memory used by this state. | |
| 3243 On several platforms, you may not need to call this function, | |
| 3244 because all resources are naturally released when the host program ends. | |
| 3245 On the other hand, long-running programs that create multiple states, | |
| 3246 such as daemons or web servers, | |
| 3247 might need to close states as soon as they are not needed. | |
| 3248 | |
| 3249 | |
| 3250 | |
| 3251 | |
| 3252 | |
| 3253 <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p> | |
| 3254 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 3255 <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre> | |
| 3256 | |
| 3257 <p> | |
| 3258 Compares two Lua values. | |
| 3259 Returns 1 if the value at index <code>index1</code> satisfies <code>op</code> | |
| 3260 when compared with the value at index <code>index2</code>, | |
| 3261 following the semantics of the corresponding Lua operator | |
| 3262 (that is, it may call metamethods). | |
| 3263 Otherwise returns 0. | |
| 3264 Also returns 0 if any of the indices is non valid. | |
| 3265 | |
| 3266 | |
| 3267 <p> | |
| 3268 The value of <code>op</code> must be one of the following constants: | |
| 3269 | |
| 3270 <ul> | |
| 3271 | |
| 3272 <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li> | |
| 3273 <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code><</code>)</li> | |
| 3274 <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code><=</code>)</li> | |
| 3275 | |
| 3276 </ul> | |
| 3277 | |
| 3278 | |
| 3279 | |
| 3280 | |
| 3281 <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p> | |
| 3282 <span class="apii">[-n, +1, <em>e</em>]</span> | |
| 3283 <pre>void lua_concat (lua_State *L, int n);</pre> | |
| 3284 | |
| 3285 <p> | |
| 3286 Concatenates the <code>n</code> values at the top of the stack, | |
| 3287 pops them, and leaves the result at the top. | |
| 3288 If <code>n</code> is 1, the result is the single value on the stack | |
| 3289 (that is, the function does nothing); | |
| 3290 if <code>n</code> is 0, the result is the empty string. | |
| 3291 Concatenation is performed following the usual semantics of Lua | |
| 3292 (see <a href="#3.4.5">§3.4.5</a>). | |
| 3293 | |
| 3294 | |
| 3295 | |
| 3296 | |
| 3297 | |
| 3298 <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p> | |
| 3299 <span class="apii">[-0, +0, –]</span> | |
| 3300 <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre> | |
| 3301 | |
| 3302 <p> | |
| 3303 Moves the element at index <code>fromidx</code> | |
| 3304 into the valid index <code>toidx</code> | |
| 3305 without shifting any element | |
| 3306 (therefore replacing the value at that position). | |
| 3307 | |
| 3308 | |
| 3309 | |
| 3310 | |
| 3311 | |
| 3312 <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p> | |
| 3313 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3314 <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre> | |
| 3315 | |
| 3316 <p> | |
| 3317 Creates a new empty table and pushes it onto the stack. | |
| 3318 Parameter <code>narr</code> is a hint for how many elements the table | |
| 3319 will have as a sequence; | |
| 3320 parameter <code>nrec</code> is a hint for how many other elements | |
| 3321 the table will have. | |
| 3322 Lua may use these hints to preallocate memory for the new table. | |
| 3323 This pre-allocation is useful for performance when you know in advance | |
| 3324 how many elements the table will have. | |
| 3325 Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>. | |
| 3326 | |
| 3327 | |
| 3328 | |
| 3329 | |
| 3330 | |
| 3331 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p> | |
| 3332 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 3333 <pre>int lua_dump (lua_State *L, lua_Writer writer, void *data);</pre> | |
| 3334 | |
| 3335 <p> | |
| 3336 Dumps a function as a binary chunk. | |
| 3337 Receives a Lua function on the top of the stack | |
| 3338 and produces a binary chunk that, | |
| 3339 if loaded again, | |
| 3340 results in a function equivalent to the one dumped. | |
| 3341 As it produces parts of the chunk, | |
| 3342 <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>) | |
| 3343 with the given <code>data</code> | |
| 3344 to write them. | |
| 3345 | |
| 3346 | |
| 3347 <p> | |
| 3348 The value returned is the error code returned by the last | |
| 3349 call to the writer; | |
| 3350 0 means no errors. | |
| 3351 | |
| 3352 | |
| 3353 <p> | |
| 3354 This function does not pop the Lua function from the stack. | |
| 3355 | |
| 3356 | |
| 3357 | |
| 3358 | |
| 3359 | |
| 3360 <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p> | |
| 3361 <span class="apii">[-1, +0, <em>v</em>]</span> | |
| 3362 <pre>int lua_error (lua_State *L);</pre> | |
| 3363 | |
| 3364 <p> | |
| 3365 Generates a Lua error. | |
| 3366 The error message (which can actually be a Lua value of any type) | |
| 3367 must be on the stack top. | |
| 3368 This function does a long jump, | |
| 3369 and therefore never returns | |
| 3370 (see <a href="#luaL_error"><code>luaL_error</code></a>). | |
| 3371 | |
| 3372 | |
| 3373 | |
| 3374 | |
| 3375 | |
| 3376 <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p> | |
| 3377 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 3378 <pre>int lua_gc (lua_State *L, int what, int data);</pre> | |
| 3379 | |
| 3380 <p> | |
| 3381 Controls the garbage collector. | |
| 3382 | |
| 3383 | |
| 3384 <p> | |
| 3385 This function performs several tasks, | |
| 3386 according to the value of the parameter <code>what</code>: | |
| 3387 | |
| 3388 <ul> | |
| 3389 | |
| 3390 <li><b><code>LUA_GCSTOP</code>: </b> | |
| 3391 stops the garbage collector. | |
| 3392 </li> | |
| 3393 | |
| 3394 <li><b><code>LUA_GCRESTART</code>: </b> | |
| 3395 restarts the garbage collector. | |
| 3396 </li> | |
| 3397 | |
| 3398 <li><b><code>LUA_GCCOLLECT</code>: </b> | |
| 3399 performs a full garbage-collection cycle. | |
| 3400 </li> | |
| 3401 | |
| 3402 <li><b><code>LUA_GCCOUNT</code>: </b> | |
| 3403 returns the current amount of memory (in Kbytes) in use by Lua. | |
| 3404 </li> | |
| 3405 | |
| 3406 <li><b><code>LUA_GCCOUNTB</code>: </b> | |
| 3407 returns the remainder of dividing the current amount of bytes of | |
| 3408 memory in use by Lua by 1024. | |
| 3409 </li> | |
| 3410 | |
| 3411 <li><b><code>LUA_GCSTEP</code>: </b> | |
| 3412 performs an incremental step of garbage collection. | |
| 3413 The step "size" is controlled by <code>data</code> | |
| 3414 (larger values mean more steps) in a non-specified way. | |
| 3415 If you want to control the step size | |
| 3416 you must experimentally tune the value of <code>data</code>. | |
| 3417 The function returns 1 if the step finished a | |
| 3418 garbage-collection cycle. | |
| 3419 </li> | |
| 3420 | |
| 3421 <li><b><code>LUA_GCSETPAUSE</code>: </b> | |
| 3422 sets <code>data</code> as the new value | |
| 3423 for the <em>pause</em> of the collector (see <a href="#2.5">§2.5</a>). | |
| 3424 The function returns the previous value of the pause. | |
| 3425 </li> | |
| 3426 | |
| 3427 <li><b><code>LUA_GCSETSTEPMUL</code>: </b> | |
| 3428 sets <code>data</code> as the new value for the <em>step multiplier</em> of | |
| 3429 the collector (see <a href="#2.5">§2.5</a>). | |
| 3430 The function returns the previous value of the step multiplier. | |
| 3431 </li> | |
| 3432 | |
| 3433 <li><b><code>LUA_GCISRUNNING</code>: </b> | |
| 3434 returns a boolean that tells whether the collector is running | |
| 3435 (i.e., not stopped). | |
| 3436 </li> | |
| 3437 | |
| 3438 <li><b><code>LUA_GCGEN</code>: </b> | |
| 3439 changes the collector to generational mode | |
| 3440 (see <a href="#2.5">§2.5</a>). | |
| 3441 </li> | |
| 3442 | |
| 3443 <li><b><code>LUA_GCINC</code>: </b> | |
| 3444 changes the collector to incremental mode. | |
| 3445 This is the default mode. | |
| 3446 </li> | |
| 3447 | |
| 3448 </ul> | |
| 3449 | |
| 3450 <p> | |
| 3451 For more details about these options, | |
| 3452 see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>. | |
| 3453 | |
| 3454 | |
| 3455 | |
| 3456 | |
| 3457 | |
| 3458 <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p> | |
| 3459 <span class="apii">[-0, +0, –]</span> | |
| 3460 <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre> | |
| 3461 | |
| 3462 <p> | |
| 3463 Returns the memory-allocation function of a given state. | |
| 3464 If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the | |
| 3465 opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>. | |
| 3466 | |
| 3467 | |
| 3468 | |
| 3469 | |
| 3470 | |
| 3471 <hr><h3><a name="lua_getctx"><code>lua_getctx</code></a></h3><p> | |
| 3472 <span class="apii">[-0, +0, –]</span> | |
| 3473 <pre>int lua_getctx (lua_State *L, int *ctx);</pre> | |
| 3474 | |
| 3475 <p> | |
| 3476 This function is called by a continuation function (see <a href="#4.7">§4.7</a>) | |
| 3477 to retrieve the status of the thread and a context information. | |
| 3478 | |
| 3479 | |
| 3480 <p> | |
| 3481 When called in the original function, | |
| 3482 <a href="#lua_getctx"><code>lua_getctx</code></a> always returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> | |
| 3483 and does not change the value of its argument <code>ctx</code>. | |
| 3484 When called inside a continuation function, | |
| 3485 <a href="#lua_getctx"><code>lua_getctx</code></a> returns <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> and sets | |
| 3486 the value of <code>ctx</code> to be the context information | |
| 3487 (the value passed as the <code>ctx</code> argument | |
| 3488 to the callee together with the continuation function). | |
| 3489 | |
| 3490 | |
| 3491 <p> | |
| 3492 When the callee is <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | |
| 3493 Lua may also call its continuation function | |
| 3494 to handle errors during the call. | |
| 3495 That is, upon an error in the function called by <a href="#lua_pcallk"><code>lua_pcallk</code></a>, | |
| 3496 Lua may not return to the original function | |
| 3497 but instead may call the continuation function. | |
| 3498 In that case, a call to <a href="#lua_getctx"><code>lua_getctx</code></a> will return the error code | |
| 3499 (the value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>); | |
| 3500 the value of <code>ctx</code> will be set to the context information, | |
| 3501 as in the case of a yield. | |
| 3502 | |
| 3503 | |
| 3504 | |
| 3505 | |
| 3506 | |
| 3507 <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p> | |
| 3508 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3509 <pre>void lua_getfield (lua_State *L, int index, const char *k);</pre> | |
| 3510 | |
| 3511 <p> | |
| 3512 Pushes onto the stack the value <code>t[k]</code>, | |
| 3513 where <code>t</code> is the value at the given index. | |
| 3514 As in Lua, this function may trigger a metamethod | |
| 3515 for the "index" event (see <a href="#2.4">§2.4</a>). | |
| 3516 | |
| 3517 | |
| 3518 | |
| 3519 | |
| 3520 | |
| 3521 <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p> | |
| 3522 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3523 <pre>void lua_getglobal (lua_State *L, const char *name);</pre> | |
| 3524 | |
| 3525 <p> | |
| 3526 Pushes onto the stack the value of the global <code>name</code>. | |
| 3527 | |
| 3528 | |
| 3529 | |
| 3530 | |
| 3531 | |
| 3532 <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p> | |
| 3533 <span class="apii">[-0, +(0|1), –]</span> | |
| 3534 <pre>int lua_getmetatable (lua_State *L, int index);</pre> | |
| 3535 | |
| 3536 <p> | |
| 3537 Pushes onto the stack the metatable of the value at the given index. | |
| 3538 If the value does not have a metatable, | |
| 3539 the function returns 0 and pushes nothing on the stack. | |
| 3540 | |
| 3541 | |
| 3542 | |
| 3543 | |
| 3544 | |
| 3545 <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p> | |
| 3546 <span class="apii">[-1, +1, <em>e</em>]</span> | |
| 3547 <pre>void lua_gettable (lua_State *L, int index);</pre> | |
| 3548 | |
| 3549 <p> | |
| 3550 Pushes onto the stack the value <code>t[k]</code>, | |
| 3551 where <code>t</code> is the value at the given index | |
| 3552 and <code>k</code> is the value at the top of the stack. | |
| 3553 | |
| 3554 | |
| 3555 <p> | |
| 3556 This function pops the key from the stack | |
| 3557 (putting the resulting value in its place). | |
| 3558 As in Lua, this function may trigger a metamethod | |
| 3559 for the "index" event (see <a href="#2.4">§2.4</a>). | |
| 3560 | |
| 3561 | |
| 3562 | |
| 3563 | |
| 3564 | |
| 3565 <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p> | |
| 3566 <span class="apii">[-0, +0, –]</span> | |
| 3567 <pre>int lua_gettop (lua_State *L);</pre> | |
| 3568 | |
| 3569 <p> | |
| 3570 Returns the index of the top element in the stack. | |
| 3571 Because indices start at 1, | |
| 3572 this result is equal to the number of elements in the stack | |
| 3573 (and so 0 means an empty stack). | |
| 3574 | |
| 3575 | |
| 3576 | |
| 3577 | |
| 3578 | |
| 3579 <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p> | |
| 3580 <span class="apii">[-0, +1, –]</span> | |
| 3581 <pre>void lua_getuservalue (lua_State *L, int index);</pre> | |
| 3582 | |
| 3583 <p> | |
| 3584 Pushes onto the stack the Lua value associated with the userdata | |
| 3585 at the given index. | |
| 3586 This Lua value must be a table or <b>nil</b>. | |
| 3587 | |
| 3588 | |
| 3589 | |
| 3590 | |
| 3591 | |
| 3592 <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p> | |
| 3593 <span class="apii">[-1, +1, –]</span> | |
| 3594 <pre>void lua_insert (lua_State *L, int index);</pre> | |
| 3595 | |
| 3596 <p> | |
| 3597 Moves the top element into the given valid index, | |
| 3598 shifting up the elements above this index to open space. | |
| 3599 This function cannot be called with a pseudo-index, | |
| 3600 because a pseudo-index is not an actual stack position. | |
| 3601 | |
| 3602 | |
| 3603 | |
| 3604 | |
| 3605 | |
| 3606 <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3> | |
| 3607 <pre>typedef ptrdiff_t lua_Integer;</pre> | |
| 3608 | |
| 3609 <p> | |
| 3610 The type used by the Lua API to represent signed integral values. | |
| 3611 | |
| 3612 | |
| 3613 <p> | |
| 3614 By default it is a <code>ptrdiff_t</code>, | |
| 3615 which is usually the largest signed integral type the machine handles | |
| 3616 "comfortably". | |
| 3617 | |
| 3618 | |
| 3619 | |
| 3620 | |
| 3621 | |
| 3622 <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p> | |
| 3623 <span class="apii">[-0, +0, –]</span> | |
| 3624 <pre>int lua_isboolean (lua_State *L, int index);</pre> | |
| 3625 | |
| 3626 <p> | |
| 3627 Returns 1 if the value at the given index is a boolean, | |
| 3628 and 0 otherwise. | |
| 3629 | |
| 3630 | |
| 3631 | |
| 3632 | |
| 3633 | |
| 3634 <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p> | |
| 3635 <span class="apii">[-0, +0, –]</span> | |
| 3636 <pre>int lua_iscfunction (lua_State *L, int index);</pre> | |
| 3637 | |
| 3638 <p> | |
| 3639 Returns 1 if the value at the given index is a C function, | |
| 3640 and 0 otherwise. | |
| 3641 | |
| 3642 | |
| 3643 | |
| 3644 | |
| 3645 | |
| 3646 <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p> | |
| 3647 <span class="apii">[-0, +0, –]</span> | |
| 3648 <pre>int lua_isfunction (lua_State *L, int index);</pre> | |
| 3649 | |
| 3650 <p> | |
| 3651 Returns 1 if the value at the given index is a function | |
| 3652 (either C or Lua), and 0 otherwise. | |
| 3653 | |
| 3654 | |
| 3655 | |
| 3656 | |
| 3657 | |
| 3658 <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p> | |
| 3659 <span class="apii">[-0, +0, –]</span> | |
| 3660 <pre>int lua_islightuserdata (lua_State *L, int index);</pre> | |
| 3661 | |
| 3662 <p> | |
| 3663 Returns 1 if the value at the given index is a light userdata, | |
| 3664 and 0 otherwise. | |
| 3665 | |
| 3666 | |
| 3667 | |
| 3668 | |
| 3669 | |
| 3670 <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p> | |
| 3671 <span class="apii">[-0, +0, –]</span> | |
| 3672 <pre>int lua_isnil (lua_State *L, int index);</pre> | |
| 3673 | |
| 3674 <p> | |
| 3675 Returns 1 if the value at the given index is <b>nil</b>, | |
| 3676 and 0 otherwise. | |
| 3677 | |
| 3678 | |
| 3679 | |
| 3680 | |
| 3681 | |
| 3682 <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p> | |
| 3683 <span class="apii">[-0, +0, –]</span> | |
| 3684 <pre>int lua_isnone (lua_State *L, int index);</pre> | |
| 3685 | |
| 3686 <p> | |
| 3687 Returns 1 if the given index is not valid, | |
| 3688 and 0 otherwise. | |
| 3689 | |
| 3690 | |
| 3691 | |
| 3692 | |
| 3693 | |
| 3694 <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p> | |
| 3695 <span class="apii">[-0, +0, –]</span> | |
| 3696 <pre>int lua_isnoneornil (lua_State *L, int index);</pre> | |
| 3697 | |
| 3698 <p> | |
| 3699 Returns 1 if the given index is not valid | |
| 3700 or if the value at this index is <b>nil</b>, | |
| 3701 and 0 otherwise. | |
| 3702 | |
| 3703 | |
| 3704 | |
| 3705 | |
| 3706 | |
| 3707 <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p> | |
| 3708 <span class="apii">[-0, +0, –]</span> | |
| 3709 <pre>int lua_isnumber (lua_State *L, int index);</pre> | |
| 3710 | |
| 3711 <p> | |
| 3712 Returns 1 if the value at the given index is a number | |
| 3713 or a string convertible to a number, | |
| 3714 and 0 otherwise. | |
| 3715 | |
| 3716 | |
| 3717 | |
| 3718 | |
| 3719 | |
| 3720 <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p> | |
| 3721 <span class="apii">[-0, +0, –]</span> | |
| 3722 <pre>int lua_isstring (lua_State *L, int index);</pre> | |
| 3723 | |
| 3724 <p> | |
| 3725 Returns 1 if the value at the given index is a string | |
| 3726 or a number (which is always convertible to a string), | |
| 3727 and 0 otherwise. | |
| 3728 | |
| 3729 | |
| 3730 | |
| 3731 | |
| 3732 | |
| 3733 <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p> | |
| 3734 <span class="apii">[-0, +0, –]</span> | |
| 3735 <pre>int lua_istable (lua_State *L, int index);</pre> | |
| 3736 | |
| 3737 <p> | |
| 3738 Returns 1 if the value at the given index is a table, | |
| 3739 and 0 otherwise. | |
| 3740 | |
| 3741 | |
| 3742 | |
| 3743 | |
| 3744 | |
| 3745 <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p> | |
| 3746 <span class="apii">[-0, +0, –]</span> | |
| 3747 <pre>int lua_isthread (lua_State *L, int index);</pre> | |
| 3748 | |
| 3749 <p> | |
| 3750 Returns 1 if the value at the given index is a thread, | |
| 3751 and 0 otherwise. | |
| 3752 | |
| 3753 | |
| 3754 | |
| 3755 | |
| 3756 | |
| 3757 <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p> | |
| 3758 <span class="apii">[-0, +0, –]</span> | |
| 3759 <pre>int lua_isuserdata (lua_State *L, int index);</pre> | |
| 3760 | |
| 3761 <p> | |
| 3762 Returns 1 if the value at the given index is a userdata | |
| 3763 (either full or light), and 0 otherwise. | |
| 3764 | |
| 3765 | |
| 3766 | |
| 3767 | |
| 3768 | |
| 3769 <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p> | |
| 3770 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3771 <pre>void lua_len (lua_State *L, int index);</pre> | |
| 3772 | |
| 3773 <p> | |
| 3774 Returns the "length" of the value at the given index; | |
| 3775 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.6">§3.4.6</a>). | |
| 3776 The result is pushed on the stack. | |
| 3777 | |
| 3778 | |
| 3779 | |
| 3780 | |
| 3781 | |
| 3782 <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p> | |
| 3783 <span class="apii">[-0, +1, –]</span> | |
| 3784 <pre>int lua_load (lua_State *L, | |
| 3785 lua_Reader reader, | |
| 3786 void *data, | |
| 3787 const char *source, | |
| 3788 const char *mode);</pre> | |
| 3789 | |
| 3790 <p> | |
| 3791 Loads a Lua chunk (without running it). | |
| 3792 If there are no errors, | |
| 3793 <code>lua_load</code> pushes the compiled chunk as a Lua | |
| 3794 function on top of the stack. | |
| 3795 Otherwise, it pushes an error message. | |
| 3796 | |
| 3797 | |
| 3798 <p> | |
| 3799 The return values of <code>lua_load</code> are: | |
| 3800 | |
| 3801 <ul> | |
| 3802 | |
| 3803 <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li> | |
| 3804 | |
| 3805 <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b> | |
| 3806 syntax error during precompilation;</li> | |
| 3807 | |
| 3808 <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b> | |
| 3809 memory allocation error;</li> | |
| 3810 | |
| 3811 <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b> | |
| 3812 error while running a <code>__gc</code> metamethod. | |
| 3813 (This error has no relation with the chunk being loaded. | |
| 3814 It is generated by the garbage collector.) | |
| 3815 </li> | |
| 3816 | |
| 3817 </ul> | |
| 3818 | |
| 3819 <p> | |
| 3820 The <code>lua_load</code> function uses a user-supplied <code>reader</code> function | |
| 3821 to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>). | |
| 3822 The <code>data</code> argument is an opaque value passed to the reader function. | |
| 3823 | |
| 3824 | |
| 3825 <p> | |
| 3826 The <code>source</code> argument gives a name to the chunk, | |
| 3827 which is used for error messages and in debug information (see <a href="#4.9">§4.9</a>). | |
| 3828 | |
| 3829 | |
| 3830 <p> | |
| 3831 <code>lua_load</code> automatically detects whether the chunk is text or binary | |
| 3832 and loads it accordingly (see program <code>luac</code>). | |
| 3833 The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>, | |
| 3834 with the addition that | |
| 3835 a <code>NULL</code> value is equivalent to the string "<code>bt</code>". | |
| 3836 | |
| 3837 | |
| 3838 <p> | |
| 3839 <code>lua_load</code> uses the stack internally, | |
| 3840 so the reader function should always leave the stack | |
| 3841 unmodified when returning. | |
| 3842 | |
| 3843 | |
| 3844 <p> | |
| 3845 If the resulting function has one upvalue, | |
| 3846 this upvalue is set to the value of the global environment | |
| 3847 stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">§4.5</a>). | |
| 3848 When loading main chunks, | |
| 3849 this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). | |
| 3850 | |
| 3851 | |
| 3852 | |
| 3853 | |
| 3854 | |
| 3855 <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p> | |
| 3856 <span class="apii">[-0, +0, –]</span> | |
| 3857 <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre> | |
| 3858 | |
| 3859 <p> | |
| 3860 Creates a new thread running in a new, independent state. | |
| 3861 Returns <code>NULL</code> if cannot create the thread or the state | |
| 3862 (due to lack of memory). | |
| 3863 The argument <code>f</code> is the allocator function; | |
| 3864 Lua does all memory allocation for this state through this function. | |
| 3865 The second argument, <code>ud</code>, is an opaque pointer that Lua | |
| 3866 passes to the allocator in every call. | |
| 3867 | |
| 3868 | |
| 3869 | |
| 3870 | |
| 3871 | |
| 3872 <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p> | |
| 3873 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3874 <pre>void lua_newtable (lua_State *L);</pre> | |
| 3875 | |
| 3876 <p> | |
| 3877 Creates a new empty table and pushes it onto the stack. | |
| 3878 It is equivalent to <code>lua_createtable(L, 0, 0)</code>. | |
| 3879 | |
| 3880 | |
| 3881 | |
| 3882 | |
| 3883 | |
| 3884 <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p> | |
| 3885 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3886 <pre>lua_State *lua_newthread (lua_State *L);</pre> | |
| 3887 | |
| 3888 <p> | |
| 3889 Creates a new thread, pushes it on the stack, | |
| 3890 and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread. | |
| 3891 The new thread returned by this function shares with the original thread | |
| 3892 its global environment, | |
| 3893 but has an independent execution stack. | |
| 3894 | |
| 3895 | |
| 3896 <p> | |
| 3897 There is no explicit function to close or to destroy a thread. | |
| 3898 Threads are subject to garbage collection, | |
| 3899 like any Lua object. | |
| 3900 | |
| 3901 | |
| 3902 | |
| 3903 | |
| 3904 | |
| 3905 <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p> | |
| 3906 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 3907 <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre> | |
| 3908 | |
| 3909 <p> | |
| 3910 This function allocates a new block of memory with the given size, | |
| 3911 pushes onto the stack a new full userdata with the block address, | |
| 3912 and returns this address. | |
| 3913 The host program can freely use this memory. | |
| 3914 | |
| 3915 | |
| 3916 | |
| 3917 | |
| 3918 | |
| 3919 <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p> | |
| 3920 <span class="apii">[-1, +(2|0), <em>e</em>]</span> | |
| 3921 <pre>int lua_next (lua_State *L, int index);</pre> | |
| 3922 | |
| 3923 <p> | |
| 3924 Pops a key from the stack, | |
| 3925 and pushes a key–value pair from the table at the given index | |
| 3926 (the "next" pair after the given key). | |
| 3927 If there are no more elements in the table, | |
| 3928 then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing). | |
| 3929 | |
| 3930 | |
| 3931 <p> | |
| 3932 A typical traversal looks like this: | |
| 3933 | |
| 3934 <pre> | |
| 3935 /* table is in the stack at index 't' */ | |
| 3936 lua_pushnil(L); /* first key */ | |
| 3937 while (lua_next(L, t) != 0) { | |
| 3938 /* uses 'key' (at index -2) and 'value' (at index -1) */ | |
| 3939 printf("%s - %s\n", | |
| 3940 lua_typename(L, lua_type(L, -2)), | |
| 3941 lua_typename(L, lua_type(L, -1))); | |
| 3942 /* removes 'value'; keeps 'key' for next iteration */ | |
| 3943 lua_pop(L, 1); | |
| 3944 } | |
| 3945 </pre> | |
| 3946 | |
| 3947 <p> | |
| 3948 While traversing a table, | |
| 3949 do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key, | |
| 3950 unless you know that the key is actually a string. | |
| 3951 Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change | |
| 3952 the value at the given index; | |
| 3953 this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>. | |
| 3954 | |
| 3955 | |
| 3956 <p> | |
| 3957 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying | |
| 3958 the table during its traversal. | |
| 3959 | |
| 3960 | |
| 3961 | |
| 3962 | |
| 3963 | |
| 3964 <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3> | |
| 3965 <pre>typedef double lua_Number;</pre> | |
| 3966 | |
| 3967 <p> | |
| 3968 The type of numbers in Lua. | |
| 3969 By default, it is double, but that can be changed in <code>luaconf.h</code>. | |
| 3970 Through this configuration file you can change | |
| 3971 Lua to operate with another type for numbers (e.g., float or long). | |
| 3972 | |
| 3973 | |
| 3974 | |
| 3975 | |
| 3976 | |
| 3977 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p> | |
| 3978 <span class="apii">[-(nargs + 1), +(nresults|1), –]</span> | |
| 3979 <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre> | |
| 3980 | |
| 3981 <p> | |
| 3982 Calls a function in protected mode. | |
| 3983 | |
| 3984 | |
| 3985 <p> | |
| 3986 Both <code>nargs</code> and <code>nresults</code> have the same meaning as | |
| 3987 in <a href="#lua_call"><code>lua_call</code></a>. | |
| 3988 If there are no errors during the call, | |
| 3989 <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>. | |
| 3990 However, if there is any error, | |
| 3991 <a href="#lua_pcall"><code>lua_pcall</code></a> catches it, | |
| 3992 pushes a single value on the stack (the error message), | |
| 3993 and returns an error code. | |
| 3994 Like <a href="#lua_call"><code>lua_call</code></a>, | |
| 3995 <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function | |
| 3996 and its arguments from the stack. | |
| 3997 | |
| 3998 | |
| 3999 <p> | |
| 4000 If <code>msgh</code> is 0, | |
| 4001 then the error message returned on the stack | |
| 4002 is exactly the original error message. | |
| 4003 Otherwise, <code>msgh</code> is the stack index of a | |
| 4004 <em>message handler</em>. | |
| 4005 (In the current implementation, this index cannot be a pseudo-index.) | |
| 4006 In case of runtime errors, | |
| 4007 this function will be called with the error message | |
| 4008 and its return value will be the message | |
| 4009 returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>. | |
| 4010 | |
| 4011 | |
| 4012 <p> | |
| 4013 Typically, the message handler is used to add more debug | |
| 4014 information to the error message, such as a stack traceback. | |
| 4015 Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>, | |
| 4016 since by then the stack has unwound. | |
| 4017 | |
| 4018 | |
| 4019 <p> | |
| 4020 The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following codes | |
| 4021 (defined in <code>lua.h</code>): | |
| 4022 | |
| 4023 <ul> | |
| 4024 | |
| 4025 <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b> | |
| 4026 success.</li> | |
| 4027 | |
| 4028 <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b> | |
| 4029 a runtime error. | |
| 4030 </li> | |
| 4031 | |
| 4032 <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b> | |
| 4033 memory allocation error. | |
| 4034 For such errors, Lua does not call the message handler. | |
| 4035 </li> | |
| 4036 | |
| 4037 <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b> | |
| 4038 error while running the message handler. | |
| 4039 </li> | |
| 4040 | |
| 4041 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b> | |
| 4042 error while running a <code>__gc</code> metamethod. | |
| 4043 (This error typically has no relation with the function being called. | |
| 4044 It is generated by the garbage collector.) | |
| 4045 </li> | |
| 4046 | |
| 4047 </ul> | |
| 4048 | |
| 4049 | |
| 4050 | |
| 4051 | |
| 4052 <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p> | |
| 4053 <span class="apii">[-(nargs + 1), +(nresults|1), –]</span> | |
| 4054 <pre>int lua_pcallk (lua_State *L, | |
| 4055 int nargs, | |
| 4056 int nresults, | |
| 4057 int errfunc, | |
| 4058 int ctx, | |
| 4059 lua_CFunction k);</pre> | |
| 4060 | |
| 4061 <p> | |
| 4062 This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>, | |
| 4063 but allows the called function to yield (see <a href="#4.7">§4.7</a>). | |
| 4064 | |
| 4065 | |
| 4066 | |
| 4067 | |
| 4068 | |
| 4069 <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p> | |
| 4070 <span class="apii">[-n, +0, –]</span> | |
| 4071 <pre>void lua_pop (lua_State *L, int n);</pre> | |
| 4072 | |
| 4073 <p> | |
| 4074 Pops <code>n</code> elements from the stack. | |
| 4075 | |
| 4076 | |
| 4077 | |
| 4078 | |
| 4079 | |
| 4080 <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p> | |
| 4081 <span class="apii">[-0, +1, –]</span> | |
| 4082 <pre>void lua_pushboolean (lua_State *L, int b);</pre> | |
| 4083 | |
| 4084 <p> | |
| 4085 Pushes a boolean value with value <code>b</code> onto the stack. | |
| 4086 | |
| 4087 | |
| 4088 | |
| 4089 | |
| 4090 | |
| 4091 <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p> | |
| 4092 <span class="apii">[-n, +1, <em>e</em>]</span> | |
| 4093 <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre> | |
| 4094 | |
| 4095 <p> | |
| 4096 Pushes a new C closure onto the stack. | |
| 4097 | |
| 4098 | |
| 4099 <p> | |
| 4100 When a C function is created, | |
| 4101 it is possible to associate some values with it, | |
| 4102 thus creating a C closure (see <a href="#4.4">§4.4</a>); | |
| 4103 these values are then accessible to the function whenever it is called. | |
| 4104 To associate values with a C function, | |
| 4105 first these values should be pushed onto the stack | |
| 4106 (when there are multiple values, the first value is pushed first). | |
| 4107 Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> | |
| 4108 is called to create and push the C function onto the stack, | |
| 4109 with the argument <code>n</code> telling how many values should be | |
| 4110 associated with the function. | |
| 4111 <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack. | |
| 4112 | |
| 4113 | |
| 4114 <p> | |
| 4115 The maximum value for <code>n</code> is 255. | |
| 4116 | |
| 4117 | |
| 4118 <p> | |
| 4119 When <code>n</code> is zero, | |
| 4120 this function creates a <em>light C function</em>, | |
| 4121 which is just a pointer to the C function. | |
| 4122 In that case, it never throws a memory error. | |
| 4123 | |
| 4124 | |
| 4125 | |
| 4126 | |
| 4127 | |
| 4128 <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p> | |
| 4129 <span class="apii">[-0, +1, –]</span> | |
| 4130 <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre> | |
| 4131 | |
| 4132 <p> | |
| 4133 Pushes a C function onto the stack. | |
| 4134 This function receives a pointer to a C function | |
| 4135 and pushes onto the stack a Lua value of type <code>function</code> that, | |
| 4136 when called, invokes the corresponding C function. | |
| 4137 | |
| 4138 | |
| 4139 <p> | |
| 4140 Any function to be registered in Lua must | |
| 4141 follow the correct protocol to receive its parameters | |
| 4142 and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | |
| 4143 | |
| 4144 | |
| 4145 <p> | |
| 4146 <code>lua_pushcfunction</code> is defined as a macro: | |
| 4147 | |
| 4148 <pre> | |
| 4149 #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0) | |
| 4150 </pre><p> | |
| 4151 Note that <code>f</code> is used twice. | |
| 4152 | |
| 4153 | |
| 4154 | |
| 4155 | |
| 4156 | |
| 4157 <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p> | |
| 4158 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 4159 <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre> | |
| 4160 | |
| 4161 <p> | |
| 4162 Pushes onto the stack a formatted string | |
| 4163 and returns a pointer to this string. | |
| 4164 It is similar to the ANSI C function <code>sprintf</code>, | |
| 4165 but has some important differences: | |
| 4166 | |
| 4167 <ul> | |
| 4168 | |
| 4169 <li> | |
| 4170 You do not have to allocate space for the result: | |
| 4171 the result is a Lua string and Lua takes care of memory allocation | |
| 4172 (and deallocation, through garbage collection). | |
| 4173 </li> | |
| 4174 | |
| 4175 <li> | |
| 4176 The conversion specifiers are quite restricted. | |
| 4177 There are no flags, widths, or precisions. | |
| 4178 The conversion specifiers can only be | |
| 4179 '<code>%%</code>' (inserts a '<code>%</code>' in the string), | |
| 4180 '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions), | |
| 4181 '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>), | |
| 4182 '<code>%p</code>' (inserts a pointer as a hexadecimal numeral), | |
| 4183 '<code>%d</code>' (inserts an <code>int</code>), and | |
| 4184 '<code>%c</code>' (inserts an <code>int</code> as a byte). | |
| 4185 </li> | |
| 4186 | |
| 4187 </ul> | |
| 4188 | |
| 4189 | |
| 4190 | |
| 4191 | |
| 4192 <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p> | |
| 4193 <span class="apii">[-0, +1, –]</span> | |
| 4194 <pre>void lua_pushglobaltable (lua_State *L);</pre> | |
| 4195 | |
| 4196 <p> | |
| 4197 Pushes the global environment onto the stack. | |
| 4198 | |
| 4199 | |
| 4200 | |
| 4201 | |
| 4202 | |
| 4203 <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p> | |
| 4204 <span class="apii">[-0, +1, –]</span> | |
| 4205 <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre> | |
| 4206 | |
| 4207 <p> | |
| 4208 Pushes a number with value <code>n</code> onto the stack. | |
| 4209 | |
| 4210 | |
| 4211 | |
| 4212 | |
| 4213 | |
| 4214 <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p> | |
| 4215 <span class="apii">[-0, +1, –]</span> | |
| 4216 <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre> | |
| 4217 | |
| 4218 <p> | |
| 4219 Pushes a light userdata onto the stack. | |
| 4220 | |
| 4221 | |
| 4222 <p> | |
| 4223 Userdata represent C values in Lua. | |
| 4224 A <em>light userdata</em> represents a pointer, a <code>void*</code>. | |
| 4225 It is a value (like a number): | |
| 4226 you do not create it, it has no individual metatable, | |
| 4227 and it is not collected (as it was never created). | |
| 4228 A light userdata is equal to "any" | |
| 4229 light userdata with the same C address. | |
| 4230 | |
| 4231 | |
| 4232 | |
| 4233 | |
| 4234 | |
| 4235 <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p> | |
| 4236 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 4237 <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre> | |
| 4238 | |
| 4239 <p> | |
| 4240 This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>, | |
| 4241 but can be used only when <code>s</code> is a literal string. | |
| 4242 It automatically provides the string length. | |
| 4243 | |
| 4244 | |
| 4245 | |
| 4246 | |
| 4247 | |
| 4248 <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p> | |
| 4249 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 4250 <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre> | |
| 4251 | |
| 4252 <p> | |
| 4253 Pushes the string pointed to by <code>s</code> with size <code>len</code> | |
| 4254 onto the stack. | |
| 4255 Lua makes (or reuses) an internal copy of the given string, | |
| 4256 so the memory at <code>s</code> can be freed or reused immediately after | |
| 4257 the function returns. | |
| 4258 The string can contain any binary data, | |
| 4259 including embedded zeros. | |
| 4260 | |
| 4261 | |
| 4262 <p> | |
| 4263 Returns a pointer to the internal copy of the string. | |
| 4264 | |
| 4265 | |
| 4266 | |
| 4267 | |
| 4268 | |
| 4269 <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p> | |
| 4270 <span class="apii">[-0, +1, –]</span> | |
| 4271 <pre>void lua_pushnil (lua_State *L);</pre> | |
| 4272 | |
| 4273 <p> | |
| 4274 Pushes a nil value onto the stack. | |
| 4275 | |
| 4276 | |
| 4277 | |
| 4278 | |
| 4279 | |
| 4280 <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p> | |
| 4281 <span class="apii">[-0, +1, –]</span> | |
| 4282 <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre> | |
| 4283 | |
| 4284 <p> | |
| 4285 Pushes a number with value <code>n</code> onto the stack. | |
| 4286 | |
| 4287 | |
| 4288 | |
| 4289 | |
| 4290 | |
| 4291 <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p> | |
| 4292 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 4293 <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre> | |
| 4294 | |
| 4295 <p> | |
| 4296 Pushes the zero-terminated string pointed to by <code>s</code> | |
| 4297 onto the stack. | |
| 4298 Lua makes (or reuses) an internal copy of the given string, | |
| 4299 so the memory at <code>s</code> can be freed or reused immediately after | |
| 4300 the function returns. | |
| 4301 | |
| 4302 | |
| 4303 <p> | |
| 4304 Returns a pointer to the internal copy of the string. | |
| 4305 | |
| 4306 | |
| 4307 <p> | |
| 4308 If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>. | |
| 4309 | |
| 4310 | |
| 4311 | |
| 4312 | |
| 4313 | |
| 4314 <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p> | |
| 4315 <span class="apii">[-0, +1, –]</span> | |
| 4316 <pre>int lua_pushthread (lua_State *L);</pre> | |
| 4317 | |
| 4318 <p> | |
| 4319 Pushes the thread represented by <code>L</code> onto the stack. | |
| 4320 Returns 1 if this thread is the main thread of its state. | |
| 4321 | |
| 4322 | |
| 4323 | |
| 4324 | |
| 4325 | |
| 4326 <hr><h3><a name="lua_pushunsigned"><code>lua_pushunsigned</code></a></h3><p> | |
| 4327 <span class="apii">[-0, +1, –]</span> | |
| 4328 <pre>void lua_pushunsigned (lua_State *L, lua_Unsigned n);</pre> | |
| 4329 | |
| 4330 <p> | |
| 4331 Pushes a number with value <code>n</code> onto the stack. | |
| 4332 | |
| 4333 | |
| 4334 | |
| 4335 | |
| 4336 | |
| 4337 <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p> | |
| 4338 <span class="apii">[-0, +1, –]</span> | |
| 4339 <pre>void lua_pushvalue (lua_State *L, int index);</pre> | |
| 4340 | |
| 4341 <p> | |
| 4342 Pushes a copy of the element at the given index | |
| 4343 onto the stack. | |
| 4344 | |
| 4345 | |
| 4346 | |
| 4347 | |
| 4348 | |
| 4349 <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p> | |
| 4350 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 4351 <pre>const char *lua_pushvfstring (lua_State *L, | |
| 4352 const char *fmt, | |
| 4353 va_list argp);</pre> | |
| 4354 | |
| 4355 <p> | |
| 4356 Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code> | |
| 4357 instead of a variable number of arguments. | |
| 4358 | |
| 4359 | |
| 4360 | |
| 4361 | |
| 4362 | |
| 4363 <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p> | |
| 4364 <span class="apii">[-0, +0, –]</span> | |
| 4365 <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre> | |
| 4366 | |
| 4367 <p> | |
| 4368 Returns 1 if the two values in indices <code>index1</code> and | |
| 4369 <code>index2</code> are primitively equal | |
| 4370 (that is, without calling metamethods). | |
| 4371 Otherwise returns 0. | |
| 4372 Also returns 0 if any of the indices are non valid. | |
| 4373 | |
| 4374 | |
| 4375 | |
| 4376 | |
| 4377 | |
| 4378 <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p> | |
| 4379 <span class="apii">[-1, +1, –]</span> | |
| 4380 <pre>void lua_rawget (lua_State *L, int index);</pre> | |
| 4381 | |
| 4382 <p> | |
| 4383 Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access | |
| 4384 (i.e., without metamethods). | |
| 4385 | |
| 4386 | |
| 4387 | |
| 4388 | |
| 4389 | |
| 4390 <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p> | |
| 4391 <span class="apii">[-0, +1, –]</span> | |
| 4392 <pre>void lua_rawgeti (lua_State *L, int index, int n);</pre> | |
| 4393 | |
| 4394 <p> | |
| 4395 Pushes onto the stack the value <code>t[n]</code>, | |
| 4396 where <code>t</code> is the table at the given index. | |
| 4397 The access is raw; | |
| 4398 that is, it does not invoke metamethods. | |
| 4399 | |
| 4400 | |
| 4401 | |
| 4402 | |
| 4403 | |
| 4404 <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p> | |
| 4405 <span class="apii">[-0, +1, –]</span> | |
| 4406 <pre>void lua_rawgetp (lua_State *L, int index, const void *p);</pre> | |
| 4407 | |
| 4408 <p> | |
| 4409 Pushes onto the stack the value <code>t[k]</code>, | |
| 4410 where <code>t</code> is the table at the given index and | |
| 4411 <code>k</code> is the pointer <code>p</code> represented as a light userdata. | |
| 4412 The access is raw; | |
| 4413 that is, it does not invoke metamethods. | |
| 4414 | |
| 4415 | |
| 4416 | |
| 4417 | |
| 4418 | |
| 4419 <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p> | |
| 4420 <span class="apii">[-0, +0, –]</span> | |
| 4421 <pre>size_t lua_rawlen (lua_State *L, int index);</pre> | |
| 4422 | |
| 4423 <p> | |
| 4424 Returns the raw "length" of the value at the given index: | |
| 4425 for strings, this is the string length; | |
| 4426 for tables, this is the result of the length operator ('<code>#</code>') | |
| 4427 with no metamethods; | |
| 4428 for userdata, this is the size of the block of memory allocated | |
| 4429 for the userdata; | |
| 4430 for other values, it is 0. | |
| 4431 | |
| 4432 | |
| 4433 | |
| 4434 | |
| 4435 | |
| 4436 <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p> | |
| 4437 <span class="apii">[-2, +0, <em>e</em>]</span> | |
| 4438 <pre>void lua_rawset (lua_State *L, int index);</pre> | |
| 4439 | |
| 4440 <p> | |
| 4441 Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment | |
| 4442 (i.e., without metamethods). | |
| 4443 | |
| 4444 | |
| 4445 | |
| 4446 | |
| 4447 | |
| 4448 <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p> | |
| 4449 <span class="apii">[-1, +0, <em>e</em>]</span> | |
| 4450 <pre>void lua_rawseti (lua_State *L, int index, int n);</pre> | |
| 4451 | |
| 4452 <p> | |
| 4453 Does the equivalent of <code>t[n] = v</code>, | |
| 4454 where <code>t</code> is the table at the given index | |
| 4455 and <code>v</code> is the value at the top of the stack. | |
| 4456 | |
| 4457 | |
| 4458 <p> | |
| 4459 This function pops the value from the stack. | |
| 4460 The assignment is raw; | |
| 4461 that is, it does not invoke metamethods. | |
| 4462 | |
| 4463 | |
| 4464 | |
| 4465 | |
| 4466 | |
| 4467 <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p> | |
| 4468 <span class="apii">[-1, +0, <em>e</em>]</span> | |
| 4469 <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre> | |
| 4470 | |
| 4471 <p> | |
| 4472 Does the equivalent of <code>t[k] = v</code>, | |
| 4473 where <code>t</code> is the table at the given index, | |
| 4474 <code>k</code> is the pointer <code>p</code> represented as a light userdata, | |
| 4475 and <code>v</code> is the value at the top of the stack. | |
| 4476 | |
| 4477 | |
| 4478 <p> | |
| 4479 This function pops the value from the stack. | |
| 4480 The assignment is raw; | |
| 4481 that is, it does not invoke metamethods. | |
| 4482 | |
| 4483 | |
| 4484 | |
| 4485 | |
| 4486 | |
| 4487 <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3> | |
| 4488 <pre>typedef const char * (*lua_Reader) (lua_State *L, | |
| 4489 void *data, | |
| 4490 size_t *size);</pre> | |
| 4491 | |
| 4492 <p> | |
| 4493 The reader function used by <a href="#lua_load"><code>lua_load</code></a>. | |
| 4494 Every time it needs another piece of the chunk, | |
| 4495 <a href="#lua_load"><code>lua_load</code></a> calls the reader, | |
| 4496 passing along its <code>data</code> parameter. | |
| 4497 The reader must return a pointer to a block of memory | |
| 4498 with a new piece of the chunk | |
| 4499 and set <code>size</code> to the block size. | |
| 4500 The block must exist until the reader function is called again. | |
| 4501 To signal the end of the chunk, | |
| 4502 the reader must return <code>NULL</code> or set <code>size</code> to zero. | |
| 4503 The reader function may return pieces of any size greater than zero. | |
| 4504 | |
| 4505 | |
| 4506 | |
| 4507 | |
| 4508 | |
| 4509 <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p> | |
| 4510 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 4511 <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre> | |
| 4512 | |
| 4513 <p> | |
| 4514 Sets the C function <code>f</code> as the new value of global <code>name</code>. | |
| 4515 It is defined as a macro: | |
| 4516 | |
| 4517 <pre> | |
| 4518 #define lua_register(L,n,f) \ | |
| 4519 (lua_pushcfunction(L, f), lua_setglobal(L, n)) | |
| 4520 </pre> | |
| 4521 | |
| 4522 | |
| 4523 | |
| 4524 | |
| 4525 <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p> | |
| 4526 <span class="apii">[-1, +0, –]</span> | |
| 4527 <pre>void lua_remove (lua_State *L, int index);</pre> | |
| 4528 | |
| 4529 <p> | |
| 4530 Removes the element at the given valid index, | |
| 4531 shifting down the elements above this index to fill the gap. | |
| 4532 This function cannot be called with a pseudo-index, | |
| 4533 because a pseudo-index is not an actual stack position. | |
| 4534 | |
| 4535 | |
| 4536 | |
| 4537 | |
| 4538 | |
| 4539 <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p> | |
| 4540 <span class="apii">[-1, +0, –]</span> | |
| 4541 <pre>void lua_replace (lua_State *L, int index);</pre> | |
| 4542 | |
| 4543 <p> | |
| 4544 Moves the top element into the given valid index | |
| 4545 without shifting any element | |
| 4546 (therefore replacing the value at the given index), | |
| 4547 and then pops the top element. | |
| 4548 | |
| 4549 | |
| 4550 | |
| 4551 | |
| 4552 | |
| 4553 <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p> | |
| 4554 <span class="apii">[-?, +?, –]</span> | |
| 4555 <pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre> | |
| 4556 | |
| 4557 <p> | |
| 4558 Starts and resumes a coroutine in a given thread. | |
| 4559 | |
| 4560 | |
| 4561 <p> | |
| 4562 To start a coroutine, | |
| 4563 you push onto the thread stack the main function plus any arguments; | |
| 4564 then you call <a href="#lua_resume"><code>lua_resume</code></a>, | |
| 4565 with <code>nargs</code> being the number of arguments. | |
| 4566 This call returns when the coroutine suspends or finishes its execution. | |
| 4567 When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>, | |
| 4568 or all values returned by the body function. | |
| 4569 <a href="#lua_resume"><code>lua_resume</code></a> returns | |
| 4570 <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields, | |
| 4571 <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution | |
| 4572 without errors, | |
| 4573 or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>). | |
| 4574 | |
| 4575 | |
| 4576 <p> | |
| 4577 In case of errors, | |
| 4578 the stack is not unwound, | |
| 4579 so you can use the debug API over it. | |
| 4580 The error message is on the top of the stack. | |
| 4581 | |
| 4582 | |
| 4583 <p> | |
| 4584 To resume a coroutine, | |
| 4585 you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>, | |
| 4586 put on its stack only the values to | |
| 4587 be passed as results from <code>yield</code>, | |
| 4588 and then call <a href="#lua_resume"><code>lua_resume</code></a>. | |
| 4589 | |
| 4590 | |
| 4591 <p> | |
| 4592 The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>. | |
| 4593 If there is no such coroutine, | |
| 4594 this parameter can be <code>NULL</code>. | |
| 4595 | |
| 4596 | |
| 4597 | |
| 4598 | |
| 4599 | |
| 4600 <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p> | |
| 4601 <span class="apii">[-0, +0, –]</span> | |
| 4602 <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre> | |
| 4603 | |
| 4604 <p> | |
| 4605 Changes the allocator function of a given state to <code>f</code> | |
| 4606 with user data <code>ud</code>. | |
| 4607 | |
| 4608 | |
| 4609 | |
| 4610 | |
| 4611 | |
| 4612 <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p> | |
| 4613 <span class="apii">[-1, +0, <em>e</em>]</span> | |
| 4614 <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre> | |
| 4615 | |
| 4616 <p> | |
| 4617 Does the equivalent to <code>t[k] = v</code>, | |
| 4618 where <code>t</code> is the value at the given index | |
| 4619 and <code>v</code> is the value at the top of the stack. | |
| 4620 | |
| 4621 | |
| 4622 <p> | |
| 4623 This function pops the value from the stack. | |
| 4624 As in Lua, this function may trigger a metamethod | |
| 4625 for the "newindex" event (see <a href="#2.4">§2.4</a>). | |
| 4626 | |
| 4627 | |
| 4628 | |
| 4629 | |
| 4630 | |
| 4631 <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p> | |
| 4632 <span class="apii">[-1, +0, <em>e</em>]</span> | |
| 4633 <pre>void lua_setglobal (lua_State *L, const char *name);</pre> | |
| 4634 | |
| 4635 <p> | |
| 4636 Pops a value from the stack and | |
| 4637 sets it as the new value of global <code>name</code>. | |
| 4638 | |
| 4639 | |
| 4640 | |
| 4641 | |
| 4642 | |
| 4643 <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p> | |
| 4644 <span class="apii">[-1, +0, –]</span> | |
| 4645 <pre>void lua_setmetatable (lua_State *L, int index);</pre> | |
| 4646 | |
| 4647 <p> | |
| 4648 Pops a table from the stack and | |
| 4649 sets it as the new metatable for the value at the given index. | |
| 4650 | |
| 4651 | |
| 4652 | |
| 4653 | |
| 4654 | |
| 4655 <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p> | |
| 4656 <span class="apii">[-2, +0, <em>e</em>]</span> | |
| 4657 <pre>void lua_settable (lua_State *L, int index);</pre> | |
| 4658 | |
| 4659 <p> | |
| 4660 Does the equivalent to <code>t[k] = v</code>, | |
| 4661 where <code>t</code> is the value at the given index, | |
| 4662 <code>v</code> is the value at the top of the stack, | |
| 4663 and <code>k</code> is the value just below the top. | |
| 4664 | |
| 4665 | |
| 4666 <p> | |
| 4667 This function pops both the key and the value from the stack. | |
| 4668 As in Lua, this function may trigger a metamethod | |
| 4669 for the "newindex" event (see <a href="#2.4">§2.4</a>). | |
| 4670 | |
| 4671 | |
| 4672 | |
| 4673 | |
| 4674 | |
| 4675 <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p> | |
| 4676 <span class="apii">[-?, +?, –]</span> | |
| 4677 <pre>void lua_settop (lua_State *L, int index);</pre> | |
| 4678 | |
| 4679 <p> | |
| 4680 Accepts any index, or 0, | |
| 4681 and sets the stack top to this index. | |
| 4682 If the new top is larger than the old one, | |
| 4683 then the new elements are filled with <b>nil</b>. | |
| 4684 If <code>index</code> is 0, then all stack elements are removed. | |
| 4685 | |
| 4686 | |
| 4687 | |
| 4688 | |
| 4689 | |
| 4690 <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p> | |
| 4691 <span class="apii">[-1, +0, –]</span> | |
| 4692 <pre>void lua_setuservalue (lua_State *L, int index);</pre> | |
| 4693 | |
| 4694 <p> | |
| 4695 Pops a table or <b>nil</b> from the stack and sets it as | |
| 4696 the new value associated to the userdata at the given index. | |
| 4697 | |
| 4698 | |
| 4699 | |
| 4700 | |
| 4701 | |
| 4702 <hr><h3><a name="lua_State"><code>lua_State</code></a></h3> | |
| 4703 <pre>typedef struct lua_State lua_State;</pre> | |
| 4704 | |
| 4705 <p> | |
| 4706 An opaque structure that points to a thread and indirectly | |
| 4707 (through the thread) to the whole state of a Lua interpreter. | |
| 4708 The Lua library is fully reentrant: | |
| 4709 it has no global variables. | |
| 4710 All information about a state is accessible through this structure. | |
| 4711 | |
| 4712 | |
| 4713 <p> | |
| 4714 A pointer to this structure must be passed as the first argument to | |
| 4715 every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>, | |
| 4716 which creates a Lua state from scratch. | |
| 4717 | |
| 4718 | |
| 4719 | |
| 4720 | |
| 4721 | |
| 4722 <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p> | |
| 4723 <span class="apii">[-0, +0, –]</span> | |
| 4724 <pre>int lua_status (lua_State *L);</pre> | |
| 4725 | |
| 4726 <p> | |
| 4727 Returns the status of the thread <code>L</code>. | |
| 4728 | |
| 4729 | |
| 4730 <p> | |
| 4731 The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread, | |
| 4732 an error code if the thread finished the execution | |
| 4733 of a <a href="#lua_resume"><code>lua_resume</code></a> with an error, | |
| 4734 or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended. | |
| 4735 | |
| 4736 | |
| 4737 <p> | |
| 4738 You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>. | |
| 4739 You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> | |
| 4740 (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> | |
| 4741 (to resume a coroutine). | |
| 4742 | |
| 4743 | |
| 4744 | |
| 4745 | |
| 4746 | |
| 4747 <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p> | |
| 4748 <span class="apii">[-0, +0, –]</span> | |
| 4749 <pre>int lua_toboolean (lua_State *L, int index);</pre> | |
| 4750 | |
| 4751 <p> | |
| 4752 Converts the Lua value at the given index to a C boolean | |
| 4753 value (0 or 1). | |
| 4754 Like all tests in Lua, | |
| 4755 <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value | |
| 4756 different from <b>false</b> and <b>nil</b>; | |
| 4757 otherwise it returns false. | |
| 4758 (If you want to accept only actual boolean values, | |
| 4759 use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.) | |
| 4760 | |
| 4761 | |
| 4762 | |
| 4763 | |
| 4764 | |
| 4765 <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p> | |
| 4766 <span class="apii">[-0, +0, –]</span> | |
| 4767 <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre> | |
| 4768 | |
| 4769 <p> | |
| 4770 Converts a value at the given index to a C function. | |
| 4771 That value must be a C function; | |
| 4772 otherwise, returns <code>NULL</code>. | |
| 4773 | |
| 4774 | |
| 4775 | |
| 4776 | |
| 4777 | |
| 4778 <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p> | |
| 4779 <span class="apii">[-0, +0, –]</span> | |
| 4780 <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre> | |
| 4781 | |
| 4782 <p> | |
| 4783 Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>. | |
| 4784 | |
| 4785 | |
| 4786 | |
| 4787 | |
| 4788 | |
| 4789 <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p> | |
| 4790 <span class="apii">[-0, +0, –]</span> | |
| 4791 <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre> | |
| 4792 | |
| 4793 <p> | |
| 4794 Converts the Lua value at the given index | |
| 4795 to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>. | |
| 4796 The Lua value must be a number or a string convertible to a number | |
| 4797 (see <a href="#3.4.2">§3.4.2</a>); | |
| 4798 otherwise, <code>lua_tointegerx</code> returns 0. | |
| 4799 | |
| 4800 | |
| 4801 <p> | |
| 4802 If the number is not an integer, | |
| 4803 it is truncated in some non-specified way. | |
| 4804 | |
| 4805 | |
| 4806 <p> | |
| 4807 If <code>isnum</code> is not <code>NULL</code>, | |
| 4808 its referent is assigned a boolean value that | |
| 4809 indicates whether the operation succeeded. | |
| 4810 | |
| 4811 | |
| 4812 | |
| 4813 | |
| 4814 | |
| 4815 <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p> | |
| 4816 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 4817 <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre> | |
| 4818 | |
| 4819 <p> | |
| 4820 Converts the Lua value at the given index to a C string. | |
| 4821 If <code>len</code> is not <code>NULL</code>, | |
| 4822 it also sets <code>*len</code> with the string length. | |
| 4823 The Lua value must be a string or a number; | |
| 4824 otherwise, the function returns <code>NULL</code>. | |
| 4825 If the value is a number, | |
| 4826 then <code>lua_tolstring</code> also | |
| 4827 <em>changes the actual value in the stack to a string</em>. | |
| 4828 (This change confuses <a href="#lua_next"><code>lua_next</code></a> | |
| 4829 when <code>lua_tolstring</code> is applied to keys during a table traversal.) | |
| 4830 | |
| 4831 | |
| 4832 <p> | |
| 4833 <code>lua_tolstring</code> returns a fully aligned pointer | |
| 4834 to a string inside the Lua state. | |
| 4835 This string always has a zero ('<code>\0</code>') | |
| 4836 after its last character (as in C), | |
| 4837 but can contain other zeros in its body. | |
| 4838 Because Lua has garbage collection, | |
| 4839 there is no guarantee that the pointer returned by <code>lua_tolstring</code> | |
| 4840 will be valid after the corresponding value is removed from the stack. | |
| 4841 | |
| 4842 | |
| 4843 | |
| 4844 | |
| 4845 | |
| 4846 <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p> | |
| 4847 <span class="apii">[-0, +0, –]</span> | |
| 4848 <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre> | |
| 4849 | |
| 4850 <p> | |
| 4851 Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>. | |
| 4852 | |
| 4853 | |
| 4854 | |
| 4855 | |
| 4856 | |
| 4857 <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p> | |
| 4858 <span class="apii">[-0, +0, –]</span> | |
| 4859 <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre> | |
| 4860 | |
| 4861 <p> | |
| 4862 Converts the Lua value at the given index | |
| 4863 to the C type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>). | |
| 4864 The Lua value must be a number or a string convertible to a number | |
| 4865 (see <a href="#3.4.2">§3.4.2</a>); | |
| 4866 otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns 0. | |
| 4867 | |
| 4868 | |
| 4869 <p> | |
| 4870 If <code>isnum</code> is not <code>NULL</code>, | |
| 4871 its referent is assigned a boolean value that | |
| 4872 indicates whether the operation succeeded. | |
| 4873 | |
| 4874 | |
| 4875 | |
| 4876 | |
| 4877 | |
| 4878 <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p> | |
| 4879 <span class="apii">[-0, +0, –]</span> | |
| 4880 <pre>const void *lua_topointer (lua_State *L, int index);</pre> | |
| 4881 | |
| 4882 <p> | |
| 4883 Converts the value at the given index to a generic | |
| 4884 C pointer (<code>void*</code>). | |
| 4885 The value can be a userdata, a table, a thread, or a function; | |
| 4886 otherwise, <code>lua_topointer</code> returns <code>NULL</code>. | |
| 4887 Different objects will give different pointers. | |
| 4888 There is no way to convert the pointer back to its original value. | |
| 4889 | |
| 4890 | |
| 4891 <p> | |
| 4892 Typically this function is used only for debug information. | |
| 4893 | |
| 4894 | |
| 4895 | |
| 4896 | |
| 4897 | |
| 4898 <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p> | |
| 4899 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 4900 <pre>const char *lua_tostring (lua_State *L, int index);</pre> | |
| 4901 | |
| 4902 <p> | |
| 4903 Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>. | |
| 4904 | |
| 4905 | |
| 4906 | |
| 4907 | |
| 4908 | |
| 4909 <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p> | |
| 4910 <span class="apii">[-0, +0, –]</span> | |
| 4911 <pre>lua_State *lua_tothread (lua_State *L, int index);</pre> | |
| 4912 | |
| 4913 <p> | |
| 4914 Converts the value at the given index to a Lua thread | |
| 4915 (represented as <code>lua_State*</code>). | |
| 4916 This value must be a thread; | |
| 4917 otherwise, the function returns <code>NULL</code>. | |
| 4918 | |
| 4919 | |
| 4920 | |
| 4921 | |
| 4922 | |
| 4923 <hr><h3><a name="lua_tounsigned"><code>lua_tounsigned</code></a></h3><p> | |
| 4924 <span class="apii">[-0, +0, –]</span> | |
| 4925 <pre>lua_Unsigned lua_tounsigned (lua_State *L, int index);</pre> | |
| 4926 | |
| 4927 <p> | |
| 4928 Equivalent to <a href="#lua_tounsignedx"><code>lua_tounsignedx</code></a> with <code>isnum</code> equal to <code>NULL</code>. | |
| 4929 | |
| 4930 | |
| 4931 | |
| 4932 | |
| 4933 | |
| 4934 <hr><h3><a name="lua_tounsignedx"><code>lua_tounsignedx</code></a></h3><p> | |
| 4935 <span class="apii">[-0, +0, –]</span> | |
| 4936 <pre>lua_Unsigned lua_tounsignedx (lua_State *L, int index, int *isnum);</pre> | |
| 4937 | |
| 4938 <p> | |
| 4939 Converts the Lua value at the given index | |
| 4940 to the unsigned integral type <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>. | |
| 4941 The Lua value must be a number or a string convertible to a number | |
| 4942 (see <a href="#3.4.2">§3.4.2</a>); | |
| 4943 otherwise, <code>lua_tounsignedx</code> returns 0. | |
| 4944 | |
| 4945 | |
| 4946 <p> | |
| 4947 If the number is not an integer, | |
| 4948 it is truncated in some non-specified way. | |
| 4949 If the number is outside the range of representable values, | |
| 4950 it is normalized to the remainder of its division by | |
| 4951 one more than the maximum representable value. | |
| 4952 | |
| 4953 | |
| 4954 <p> | |
| 4955 If <code>isnum</code> is not <code>NULL</code>, | |
| 4956 its referent is assigned a boolean value that | |
| 4957 indicates whether the operation succeeded. | |
| 4958 | |
| 4959 | |
| 4960 | |
| 4961 | |
| 4962 | |
| 4963 <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p> | |
| 4964 <span class="apii">[-0, +0, –]</span> | |
| 4965 <pre>void *lua_touserdata (lua_State *L, int index);</pre> | |
| 4966 | |
| 4967 <p> | |
| 4968 If the value at the given index is a full userdata, | |
| 4969 returns its block address. | |
| 4970 If the value is a light userdata, | |
| 4971 returns its pointer. | |
| 4972 Otherwise, returns <code>NULL</code>. | |
| 4973 | |
| 4974 | |
| 4975 | |
| 4976 | |
| 4977 | |
| 4978 <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p> | |
| 4979 <span class="apii">[-0, +0, –]</span> | |
| 4980 <pre>int lua_type (lua_State *L, int index);</pre> | |
| 4981 | |
| 4982 <p> | |
| 4983 Returns the type of the value in the given valid index, | |
| 4984 or <code>LUA_TNONE</code> for a non-valid (but acceptable) index. | |
| 4985 The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants | |
| 4986 defined in <code>lua.h</code>: | |
| 4987 <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>, | |
| 4988 <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>, | |
| 4989 <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>, | |
| 4990 <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, | |
| 4991 <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, | |
| 4992 <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>, | |
| 4993 <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, | |
| 4994 <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>, | |
| 4995 and | |
| 4996 <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>. | |
| 4997 | |
| 4998 | |
| 4999 | |
| 5000 | |
| 5001 | |
| 5002 <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p> | |
| 5003 <span class="apii">[-0, +0, –]</span> | |
| 5004 <pre>const char *lua_typename (lua_State *L, int tp);</pre> | |
| 5005 | |
| 5006 <p> | |
| 5007 Returns the name of the type encoded by the value <code>tp</code>, | |
| 5008 which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>. | |
| 5009 | |
| 5010 | |
| 5011 | |
| 5012 | |
| 5013 | |
| 5014 <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3> | |
| 5015 <pre>typedef unsigned long lua_Unsigned;</pre> | |
| 5016 | |
| 5017 <p> | |
| 5018 The type used by the Lua API to represent unsigned integral values. | |
| 5019 It must have at least 32 bits. | |
| 5020 | |
| 5021 | |
| 5022 <p> | |
| 5023 By default it is an <code>unsigned int</code> or an <code>unsigned long</code>, | |
| 5024 whichever can hold 32-bit values. | |
| 5025 | |
| 5026 | |
| 5027 | |
| 5028 | |
| 5029 | |
| 5030 <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p> | |
| 5031 <span class="apii">[-0, +0, –]</span> | |
| 5032 <pre>int lua_upvalueindex (int i);</pre> | |
| 5033 | |
| 5034 <p> | |
| 5035 Returns the pseudo-index that represents the <code>i</code>-th upvalue of | |
| 5036 the running function (see <a href="#4.4">§4.4</a>). | |
| 5037 | |
| 5038 | |
| 5039 | |
| 5040 | |
| 5041 | |
| 5042 <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p> | |
| 5043 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5044 <pre>const lua_Number *lua_version (lua_State *L);</pre> | |
| 5045 | |
| 5046 <p> | |
| 5047 Returns the address of the version number stored in the Lua core. | |
| 5048 When called with a valid <a href="#lua_State"><code>lua_State</code></a>, | |
| 5049 returns the address of the version used to create that state. | |
| 5050 When called with <code>NULL</code>, | |
| 5051 returns the address of the version running the call. | |
| 5052 | |
| 5053 | |
| 5054 | |
| 5055 | |
| 5056 | |
| 5057 <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3> | |
| 5058 <pre>typedef int (*lua_Writer) (lua_State *L, | |
| 5059 const void* p, | |
| 5060 size_t sz, | |
| 5061 void* ud);</pre> | |
| 5062 | |
| 5063 <p> | |
| 5064 The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>. | |
| 5065 Every time it produces another piece of chunk, | |
| 5066 <a href="#lua_dump"><code>lua_dump</code></a> calls the writer, | |
| 5067 passing along the buffer to be written (<code>p</code>), | |
| 5068 its size (<code>sz</code>), | |
| 5069 and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>. | |
| 5070 | |
| 5071 | |
| 5072 <p> | |
| 5073 The writer returns an error code: | |
| 5074 0 means no errors; | |
| 5075 any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from | |
| 5076 calling the writer again. | |
| 5077 | |
| 5078 | |
| 5079 | |
| 5080 | |
| 5081 | |
| 5082 <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p> | |
| 5083 <span class="apii">[-?, +?, –]</span> | |
| 5084 <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre> | |
| 5085 | |
| 5086 <p> | |
| 5087 Exchange values between different threads of the same state. | |
| 5088 | |
| 5089 | |
| 5090 <p> | |
| 5091 This function pops <code>n</code> values from the stack <code>from</code>, | |
| 5092 and pushes them onto the stack <code>to</code>. | |
| 5093 | |
| 5094 | |
| 5095 | |
| 5096 | |
| 5097 | |
| 5098 <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p> | |
| 5099 <span class="apii">[-?, +?, –]</span> | |
| 5100 <pre>int lua_yield (lua_State *L, int nresults);</pre> | |
| 5101 | |
| 5102 <p> | |
| 5103 This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | |
| 5104 but it has no continuation (see <a href="#4.7">§4.7</a>). | |
| 5105 Therefore, when the thread resumes, | |
| 5106 it returns to the function that called | |
| 5107 the function calling <code>lua_yield</code>. | |
| 5108 | |
| 5109 | |
| 5110 | |
| 5111 | |
| 5112 | |
| 5113 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p> | |
| 5114 <span class="apii">[-?, +?, –]</span> | |
| 5115 <pre>int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k);</pre> | |
| 5116 | |
| 5117 <p> | |
| 5118 Yields a coroutine. | |
| 5119 | |
| 5120 | |
| 5121 <p> | |
| 5122 This function should only be called as the | |
| 5123 return expression of a C function, as follows: | |
| 5124 | |
| 5125 <pre> | |
| 5126 return lua_yieldk (L, n, i, k); | |
| 5127 </pre><p> | |
| 5128 When a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way, | |
| 5129 the running coroutine suspends its execution, | |
| 5130 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns. | |
| 5131 The parameter <code>nresults</code> is the number of values from the stack | |
| 5132 that are passed as results to <a href="#lua_resume"><code>lua_resume</code></a>. | |
| 5133 | |
| 5134 | |
| 5135 <p> | |
| 5136 When the coroutine is resumed again, | |
| 5137 Lua calls the given continuation function <code>k</code> to continue | |
| 5138 the execution of the C function that yielded (see <a href="#4.7">§4.7</a>). | |
| 5139 This continuation function receives the same stack | |
| 5140 from the previous function, | |
| 5141 with the results removed and | |
| 5142 replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>. | |
| 5143 Moreover, | |
| 5144 the continuation function may access the value <code>ctx</code> | |
| 5145 by calling <a href="#lua_getctx"><code>lua_getctx</code></a>. | |
| 5146 | |
| 5147 | |
| 5148 | |
| 5149 | |
| 5150 | |
| 5151 | |
| 5152 | |
| 5153 <h2>4.9 – <a name="4.9">The Debug Interface</a></h2> | |
| 5154 | |
| 5155 <p> | |
| 5156 Lua has no built-in debugging facilities. | |
| 5157 Instead, it offers a special interface | |
| 5158 by means of functions and <em>hooks</em>. | |
| 5159 This interface allows the construction of different | |
| 5160 kinds of debuggers, profilers, and other tools | |
| 5161 that need "inside information" from the interpreter. | |
| 5162 | |
| 5163 | |
| 5164 | |
| 5165 <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3> | |
| 5166 <pre>typedef struct lua_Debug { | |
| 5167 int event; | |
| 5168 const char *name; /* (n) */ | |
| 5169 const char *namewhat; /* (n) */ | |
| 5170 const char *what; /* (S) */ | |
| 5171 const char *source; /* (S) */ | |
| 5172 int currentline; /* (l) */ | |
| 5173 int linedefined; /* (S) */ | |
| 5174 int lastlinedefined; /* (S) */ | |
| 5175 unsigned char nups; /* (u) number of upvalues */ | |
| 5176 unsigned char nparams; /* (u) number of parameters */ | |
| 5177 char isvararg; /* (u) */ | |
| 5178 char istailcall; /* (t) */ | |
| 5179 char short_src[LUA_IDSIZE]; /* (S) */ | |
| 5180 /* private part */ | |
| 5181 <em>other fields</em> | |
| 5182 } lua_Debug;</pre> | |
| 5183 | |
| 5184 <p> | |
| 5185 A structure used to carry different pieces of | |
| 5186 information about a function or an activation record. | |
| 5187 <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part | |
| 5188 of this structure, for later use. | |
| 5189 To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information, | |
| 5190 call <a href="#lua_getinfo"><code>lua_getinfo</code></a>. | |
| 5191 | |
| 5192 | |
| 5193 <p> | |
| 5194 The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning: | |
| 5195 | |
| 5196 <ul> | |
| 5197 | |
| 5198 <li><b><code>source</code>: </b> | |
| 5199 the source of the chunk that created the function. | |
| 5200 If <code>source</code> starts with a '<code>@</code>', | |
| 5201 it means that the function was defined in a file where | |
| 5202 the file name follows the '<code>@</code>'. | |
| 5203 If <code>source</code> starts with a '<code>=</code>', | |
| 5204 the remainder of its contents describe the source in a user-dependent manner. | |
| 5205 Otherwise, | |
| 5206 the function was defined in a string where | |
| 5207 <code>source</code> is that string. | |
| 5208 </li> | |
| 5209 | |
| 5210 <li><b><code>short_src</code>: </b> | |
| 5211 a "printable" version of <code>source</code>, to be used in error messages. | |
| 5212 </li> | |
| 5213 | |
| 5214 <li><b><code>linedefined</code>: </b> | |
| 5215 the line number where the definition of the function starts. | |
| 5216 </li> | |
| 5217 | |
| 5218 <li><b><code>lastlinedefined</code>: </b> | |
| 5219 the line number where the definition of the function ends. | |
| 5220 </li> | |
| 5221 | |
| 5222 <li><b><code>what</code>: </b> | |
| 5223 the string <code>"Lua"</code> if the function is a Lua function, | |
| 5224 <code>"C"</code> if it is a C function, | |
| 5225 <code>"main"</code> if it is the main part of a chunk. | |
| 5226 </li> | |
| 5227 | |
| 5228 <li><b><code>currentline</code>: </b> | |
| 5229 the current line where the given function is executing. | |
| 5230 When no line information is available, | |
| 5231 <code>currentline</code> is set to -1. | |
| 5232 </li> | |
| 5233 | |
| 5234 <li><b><code>name</code>: </b> | |
| 5235 a reasonable name for the given function. | |
| 5236 Because functions in Lua are first-class values, | |
| 5237 they do not have a fixed name: | |
| 5238 some functions can be the value of multiple global variables, | |
| 5239 while others can be stored only in a table field. | |
| 5240 The <code>lua_getinfo</code> function checks how the function was | |
| 5241 called to find a suitable name. | |
| 5242 If it cannot find a name, | |
| 5243 then <code>name</code> is set to <code>NULL</code>. | |
| 5244 </li> | |
| 5245 | |
| 5246 <li><b><code>namewhat</code>: </b> | |
| 5247 explains the <code>name</code> field. | |
| 5248 The value of <code>namewhat</code> can be | |
| 5249 <code>"global"</code>, <code>"local"</code>, <code>"method"</code>, | |
| 5250 <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string), | |
| 5251 according to how the function was called. | |
| 5252 (Lua uses the empty string when no other option seems to apply.) | |
| 5253 </li> | |
| 5254 | |
| 5255 <li><b><code>istailcall</code>: </b> | |
| 5256 true if this function invocation was called by a tail call. | |
| 5257 In this case, the caller of this level is not in the stack. | |
| 5258 </li> | |
| 5259 | |
| 5260 <li><b><code>nups</code>: </b> | |
| 5261 the number of upvalues of the function. | |
| 5262 </li> | |
| 5263 | |
| 5264 <li><b><code>nparams</code>: </b> | |
| 5265 the number of fixed parameters of the function | |
| 5266 (always 0 for C functions). | |
| 5267 </li> | |
| 5268 | |
| 5269 <li><b><code>isvararg</code>: </b> | |
| 5270 true if the function is a vararg function | |
| 5271 (always true for C functions). | |
| 5272 </li> | |
| 5273 | |
| 5274 </ul> | |
| 5275 | |
| 5276 | |
| 5277 | |
| 5278 | |
| 5279 <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p> | |
| 5280 <span class="apii">[-0, +0, –]</span> | |
| 5281 <pre>lua_Hook lua_gethook (lua_State *L);</pre> | |
| 5282 | |
| 5283 <p> | |
| 5284 Returns the current hook function. | |
| 5285 | |
| 5286 | |
| 5287 | |
| 5288 | |
| 5289 | |
| 5290 <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p> | |
| 5291 <span class="apii">[-0, +0, –]</span> | |
| 5292 <pre>int lua_gethookcount (lua_State *L);</pre> | |
| 5293 | |
| 5294 <p> | |
| 5295 Returns the current hook count. | |
| 5296 | |
| 5297 | |
| 5298 | |
| 5299 | |
| 5300 | |
| 5301 <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p> | |
| 5302 <span class="apii">[-0, +0, –]</span> | |
| 5303 <pre>int lua_gethookmask (lua_State *L);</pre> | |
| 5304 | |
| 5305 <p> | |
| 5306 Returns the current hook mask. | |
| 5307 | |
| 5308 | |
| 5309 | |
| 5310 | |
| 5311 | |
| 5312 <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p> | |
| 5313 <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span> | |
| 5314 <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre> | |
| 5315 | |
| 5316 <p> | |
| 5317 Gets information about a specific function or function invocation. | |
| 5318 | |
| 5319 | |
| 5320 <p> | |
| 5321 To get information about a function invocation, | |
| 5322 the parameter <code>ar</code> must be a valid activation record that was | |
| 5323 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or | |
| 5324 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). | |
| 5325 | |
| 5326 | |
| 5327 <p> | |
| 5328 To get information about a function you push it onto the stack | |
| 5329 and start the <code>what</code> string with the character '<code>></code>'. | |
| 5330 (In that case, | |
| 5331 <code>lua_getinfo</code> pops the function from the top of the stack.) | |
| 5332 For instance, to know in which line a function <code>f</code> was defined, | |
| 5333 you can write the following code: | |
| 5334 | |
| 5335 <pre> | |
| 5336 lua_Debug ar; | |
| 5337 lua_getglobal(L, "f"); /* get global 'f' */ | |
| 5338 lua_getinfo(L, ">S", &ar); | |
| 5339 printf("%d\n", ar.linedefined); | |
| 5340 </pre> | |
| 5341 | |
| 5342 <p> | |
| 5343 Each character in the string <code>what</code> | |
| 5344 selects some fields of the structure <code>ar</code> to be filled or | |
| 5345 a value to be pushed on the stack: | |
| 5346 | |
| 5347 <ul> | |
| 5348 | |
| 5349 <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>; | |
| 5350 </li> | |
| 5351 | |
| 5352 <li><b>'<code>S</code>': </b> | |
| 5353 fills in the fields <code>source</code>, <code>short_src</code>, | |
| 5354 <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>; | |
| 5355 </li> | |
| 5356 | |
| 5357 <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>; | |
| 5358 </li> | |
| 5359 | |
| 5360 <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>; | |
| 5361 </li> | |
| 5362 | |
| 5363 <li><b>'<code>u</code>': </b> fills in the fields | |
| 5364 <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>; | |
| 5365 </li> | |
| 5366 | |
| 5367 <li><b>'<code>f</code>': </b> | |
| 5368 pushes onto the stack the function that is | |
| 5369 running at the given level; | |
| 5370 </li> | |
| 5371 | |
| 5372 <li><b>'<code>L</code>': </b> | |
| 5373 pushes onto the stack a table whose indices are the | |
| 5374 numbers of the lines that are valid on the function. | |
| 5375 (A <em>valid line</em> is a line with some associated code, | |
| 5376 that is, a line where you can put a break point. | |
| 5377 Non-valid lines include empty lines and comments.) | |
| 5378 </li> | |
| 5379 | |
| 5380 </ul> | |
| 5381 | |
| 5382 <p> | |
| 5383 This function returns 0 on error | |
| 5384 (for instance, an invalid option in <code>what</code>). | |
| 5385 | |
| 5386 | |
| 5387 | |
| 5388 | |
| 5389 | |
| 5390 <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p> | |
| 5391 <span class="apii">[-0, +(0|1), –]</span> | |
| 5392 <pre>const char *lua_getlocal (lua_State *L, lua_Debug *ar, int n);</pre> | |
| 5393 | |
| 5394 <p> | |
| 5395 Gets information about a local variable of | |
| 5396 a given activation record or a given function. | |
| 5397 | |
| 5398 | |
| 5399 <p> | |
| 5400 In the first case, | |
| 5401 the parameter <code>ar</code> must be a valid activation record that was | |
| 5402 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or | |
| 5403 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>). | |
| 5404 The index <code>n</code> selects which local variable to inspect; | |
| 5405 see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices | |
| 5406 and names. | |
| 5407 | |
| 5408 | |
| 5409 <p> | |
| 5410 <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack | |
| 5411 and returns its name. | |
| 5412 | |
| 5413 | |
| 5414 <p> | |
| 5415 In the second case, <code>ar</code> should be <code>NULL</code> and the function | |
| 5416 to be inspected must be at the top of the stack. | |
| 5417 In this case, only parameters of Lua functions are visible | |
| 5418 (as there is no information about what variables are active) | |
| 5419 and no values are pushed onto the stack. | |
| 5420 | |
| 5421 | |
| 5422 <p> | |
| 5423 Returns <code>NULL</code> (and pushes nothing) | |
| 5424 when the index is greater than | |
| 5425 the number of active local variables. | |
| 5426 | |
| 5427 | |
| 5428 | |
| 5429 | |
| 5430 | |
| 5431 <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p> | |
| 5432 <span class="apii">[-0, +0, –]</span> | |
| 5433 <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre> | |
| 5434 | |
| 5435 <p> | |
| 5436 Gets information about the interpreter runtime stack. | |
| 5437 | |
| 5438 | |
| 5439 <p> | |
| 5440 This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with | |
| 5441 an identification of the <em>activation record</em> | |
| 5442 of the function executing at a given level. | |
| 5443 Level 0 is the current running function, | |
| 5444 whereas level <em>n+1</em> is the function that has called level <em>n</em> | |
| 5445 (except for tail calls, which do not count on the stack). | |
| 5446 When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1; | |
| 5447 when called with a level greater than the stack depth, | |
| 5448 it returns 0. | |
| 5449 | |
| 5450 | |
| 5451 | |
| 5452 | |
| 5453 | |
| 5454 <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p> | |
| 5455 <span class="apii">[-0, +(0|1), –]</span> | |
| 5456 <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre> | |
| 5457 | |
| 5458 <p> | |
| 5459 Gets information about a closure's upvalue. | |
| 5460 (For Lua functions, | |
| 5461 upvalues are the external local variables that the function uses, | |
| 5462 and that are consequently included in its closure.) | |
| 5463 <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> gets the index <code>n</code> of an upvalue, | |
| 5464 pushes the upvalue's value onto the stack, | |
| 5465 and returns its name. | |
| 5466 <code>funcindex</code> points to the closure in the stack. | |
| 5467 (Upvalues have no particular order, | |
| 5468 as they are active through the whole function. | |
| 5469 So, they are numbered in an arbitrary order.) | |
| 5470 | |
| 5471 | |
| 5472 <p> | |
| 5473 Returns <code>NULL</code> (and pushes nothing) | |
| 5474 when the index is greater than the number of upvalues. | |
| 5475 For C functions, this function uses the empty string <code>""</code> | |
| 5476 as a name for all upvalues. | |
| 5477 | |
| 5478 | |
| 5479 | |
| 5480 | |
| 5481 | |
| 5482 <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3> | |
| 5483 <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre> | |
| 5484 | |
| 5485 <p> | |
| 5486 Type for debugging hook functions. | |
| 5487 | |
| 5488 | |
| 5489 <p> | |
| 5490 Whenever a hook is called, its <code>ar</code> argument has its field | |
| 5491 <code>event</code> set to the specific event that triggered the hook. | |
| 5492 Lua identifies these events with the following constants: | |
| 5493 <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>, | |
| 5494 <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>, | |
| 5495 and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>. | |
| 5496 Moreover, for line events, the field <code>currentline</code> is also set. | |
| 5497 To get the value of any other field in <code>ar</code>, | |
| 5498 the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>. | |
| 5499 | |
| 5500 | |
| 5501 <p> | |
| 5502 For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>, | |
| 5503 the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call; | |
| 5504 in this case, there will be no corresponding return event. | |
| 5505 | |
| 5506 | |
| 5507 <p> | |
| 5508 While Lua is running a hook, it disables other calls to hooks. | |
| 5509 Therefore, if a hook calls back Lua to execute a function or a chunk, | |
| 5510 this execution occurs without any calls to hooks. | |
| 5511 | |
| 5512 | |
| 5513 <p> | |
| 5514 Hook functions cannot have continuations, | |
| 5515 that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>, | |
| 5516 <a href="#lua_pcallk"><code>lua_pcallk</code></a>, or <a href="#lua_callk"><code>lua_callk</code></a> with a non-null <code>k</code>. | |
| 5517 | |
| 5518 | |
| 5519 <p> | |
| 5520 Hook functions can yield under the following conditions: | |
| 5521 Only count and line events can yield | |
| 5522 and they cannot yield any value; | |
| 5523 to yield a hook function must finish its execution | |
| 5524 calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero. | |
| 5525 | |
| 5526 | |
| 5527 | |
| 5528 | |
| 5529 | |
| 5530 <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p> | |
| 5531 <span class="apii">[-0, +0, –]</span> | |
| 5532 <pre>int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre> | |
| 5533 | |
| 5534 <p> | |
| 5535 Sets the debugging hook function. | |
| 5536 | |
| 5537 | |
| 5538 <p> | |
| 5539 Argument <code>f</code> is the hook function. | |
| 5540 <code>mask</code> specifies on which events the hook will be called: | |
| 5541 it is formed by a bitwise or of the constants | |
| 5542 <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>, | |
| 5543 <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>, | |
| 5544 <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>, | |
| 5545 and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>. | |
| 5546 The <code>count</code> argument is only meaningful when the mask | |
| 5547 includes <code>LUA_MASKCOUNT</code>. | |
| 5548 For each event, the hook is called as explained below: | |
| 5549 | |
| 5550 <ul> | |
| 5551 | |
| 5552 <li><b>The call hook: </b> is called when the interpreter calls a function. | |
| 5553 The hook is called just after Lua enters the new function, | |
| 5554 before the function gets its arguments. | |
| 5555 </li> | |
| 5556 | |
| 5557 <li><b>The return hook: </b> is called when the interpreter returns from a function. | |
| 5558 The hook is called just before Lua leaves the function. | |
| 5559 There is no standard way to access the values | |
| 5560 to be returned by the function. | |
| 5561 </li> | |
| 5562 | |
| 5563 <li><b>The line hook: </b> is called when the interpreter is about to | |
| 5564 start the execution of a new line of code, | |
| 5565 or when it jumps back in the code (even to the same line). | |
| 5566 (This event only happens while Lua is executing a Lua function.) | |
| 5567 </li> | |
| 5568 | |
| 5569 <li><b>The count hook: </b> is called after the interpreter executes every | |
| 5570 <code>count</code> instructions. | |
| 5571 (This event only happens while Lua is executing a Lua function.) | |
| 5572 </li> | |
| 5573 | |
| 5574 </ul> | |
| 5575 | |
| 5576 <p> | |
| 5577 A hook is disabled by setting <code>mask</code> to zero. | |
| 5578 | |
| 5579 | |
| 5580 | |
| 5581 | |
| 5582 | |
| 5583 <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p> | |
| 5584 <span class="apii">[-(0|1), +0, –]</span> | |
| 5585 <pre>const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);</pre> | |
| 5586 | |
| 5587 <p> | |
| 5588 Sets the value of a local variable of a given activation record. | |
| 5589 Parameters <code>ar</code> and <code>n</code> are as in <a href="#lua_getlocal"><code>lua_getlocal</code></a> | |
| 5590 (see <a href="#lua_getlocal"><code>lua_getlocal</code></a>). | |
| 5591 <a href="#lua_setlocal"><code>lua_setlocal</code></a> assigns the value at the top of the stack | |
| 5592 to the variable and returns its name. | |
| 5593 It also pops the value from the stack. | |
| 5594 | |
| 5595 | |
| 5596 <p> | |
| 5597 Returns <code>NULL</code> (and pops nothing) | |
| 5598 when the index is greater than | |
| 5599 the number of active local variables. | |
| 5600 | |
| 5601 | |
| 5602 | |
| 5603 | |
| 5604 | |
| 5605 <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p> | |
| 5606 <span class="apii">[-(0|1), +0, –]</span> | |
| 5607 <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre> | |
| 5608 | |
| 5609 <p> | |
| 5610 Sets the value of a closure's upvalue. | |
| 5611 It assigns the value at the top of the stack | |
| 5612 to the upvalue and returns its name. | |
| 5613 It also pops the value from the stack. | |
| 5614 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> | |
| 5615 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>). | |
| 5616 | |
| 5617 | |
| 5618 <p> | |
| 5619 Returns <code>NULL</code> (and pops nothing) | |
| 5620 when the index is greater than the number of upvalues. | |
| 5621 | |
| 5622 | |
| 5623 | |
| 5624 | |
| 5625 | |
| 5626 <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p> | |
| 5627 <span class="apii">[-0, +0, –]</span> | |
| 5628 <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre> | |
| 5629 | |
| 5630 <p> | |
| 5631 Returns an unique identifier for the upvalue numbered <code>n</code> | |
| 5632 from the closure at index <code>funcindex</code>. | |
| 5633 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> | |
| 5634 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>) | |
| 5635 (but <code>n</code> cannot be greater than the number of upvalues). | |
| 5636 | |
| 5637 | |
| 5638 <p> | |
| 5639 These unique identifiers allow a program to check whether different | |
| 5640 closures share upvalues. | |
| 5641 Lua closures that share an upvalue | |
| 5642 (that is, that access a same external local variable) | |
| 5643 will return identical ids for those upvalue indices. | |
| 5644 | |
| 5645 | |
| 5646 | |
| 5647 | |
| 5648 | |
| 5649 <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p> | |
| 5650 <span class="apii">[-0, +0, –]</span> | |
| 5651 <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, | |
| 5652 int funcindex2, int n2);</pre> | |
| 5653 | |
| 5654 <p> | |
| 5655 Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code> | |
| 5656 refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>. | |
| 5657 | |
| 5658 | |
| 5659 | |
| 5660 | |
| 5661 | |
| 5662 | |
| 5663 | |
| 5664 <h1>5 – <a name="5">The Auxiliary Library</a></h1> | |
| 5665 | |
| 5666 <p> | |
| 5667 | |
| 5668 The <em>auxiliary library</em> provides several convenient functions | |
| 5669 to interface C with Lua. | |
| 5670 While the basic API provides the primitive functions for all | |
| 5671 interactions between C and Lua, | |
| 5672 the auxiliary library provides higher-level functions for some | |
| 5673 common tasks. | |
| 5674 | |
| 5675 | |
| 5676 <p> | |
| 5677 All functions and types from the auxiliary library | |
| 5678 are defined in header file <code>lauxlib.h</code> and | |
| 5679 have a prefix <code>luaL_</code>. | |
| 5680 | |
| 5681 | |
| 5682 <p> | |
| 5683 All functions in the auxiliary library are built on | |
| 5684 top of the basic API, | |
| 5685 and so they provide nothing that cannot be done with that API. | |
| 5686 Nevertheless, the use of the auxiliary library ensures | |
| 5687 more consistency to your code. | |
| 5688 | |
| 5689 | |
| 5690 <p> | |
| 5691 Several functions in the auxiliary library use internally some | |
| 5692 extra stack slots. | |
| 5693 When a function in the auxiliary library uses less than five slots, | |
| 5694 it does not check the stack size; | |
| 5695 it simply assumes that there are enough slots. | |
| 5696 | |
| 5697 | |
| 5698 <p> | |
| 5699 Several functions in the auxiliary library are used to | |
| 5700 check C function arguments. | |
| 5701 Because the error message is formatted for arguments | |
| 5702 (e.g., "<code>bad argument #1</code>"), | |
| 5703 you should not use these functions for other stack values. | |
| 5704 | |
| 5705 | |
| 5706 <p> | |
| 5707 Functions called <code>luaL_check*</code> | |
| 5708 always throw an error if the check is not satisfied. | |
| 5709 | |
| 5710 | |
| 5711 | |
| 5712 <h2>5.1 – <a name="5.1">Functions and Types</a></h2> | |
| 5713 | |
| 5714 <p> | |
| 5715 Here we list all functions and types from the auxiliary library | |
| 5716 in alphabetical order. | |
| 5717 | |
| 5718 | |
| 5719 | |
| 5720 <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p> | |
| 5721 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 5722 <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre> | |
| 5723 | |
| 5724 <p> | |
| 5725 Adds the byte <code>c</code> to the buffer <code>B</code> | |
| 5726 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | |
| 5727 | |
| 5728 | |
| 5729 | |
| 5730 | |
| 5731 | |
| 5732 <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p> | |
| 5733 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 5734 <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre> | |
| 5735 | |
| 5736 <p> | |
| 5737 Adds the string pointed to by <code>s</code> with length <code>l</code> to | |
| 5738 the buffer <code>B</code> | |
| 5739 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | |
| 5740 The string can contain embedded zeros. | |
| 5741 | |
| 5742 | |
| 5743 | |
| 5744 | |
| 5745 | |
| 5746 <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p> | |
| 5747 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 5748 <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre> | |
| 5749 | |
| 5750 <p> | |
| 5751 Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>) | |
| 5752 a string of length <code>n</code> previously copied to the | |
| 5753 buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>). | |
| 5754 | |
| 5755 | |
| 5756 | |
| 5757 | |
| 5758 | |
| 5759 <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p> | |
| 5760 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 5761 <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre> | |
| 5762 | |
| 5763 <p> | |
| 5764 Adds the zero-terminated string pointed to by <code>s</code> | |
| 5765 to the buffer <code>B</code> | |
| 5766 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | |
| 5767 The string cannot contain embedded zeros. | |
| 5768 | |
| 5769 | |
| 5770 | |
| 5771 | |
| 5772 | |
| 5773 <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p> | |
| 5774 <span class="apii">[-1, +?, <em>e</em>]</span> | |
| 5775 <pre>void luaL_addvalue (luaL_Buffer *B);</pre> | |
| 5776 | |
| 5777 <p> | |
| 5778 Adds the value at the top of the stack | |
| 5779 to the buffer <code>B</code> | |
| 5780 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | |
| 5781 Pops the value. | |
| 5782 | |
| 5783 | |
| 5784 <p> | |
| 5785 This is the only function on string buffers that can (and must) | |
| 5786 be called with an extra element on the stack, | |
| 5787 which is the value to be added to the buffer. | |
| 5788 | |
| 5789 | |
| 5790 | |
| 5791 | |
| 5792 | |
| 5793 <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p> | |
| 5794 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5795 <pre>void luaL_argcheck (lua_State *L, | |
| 5796 int cond, | |
| 5797 int arg, | |
| 5798 const char *extramsg);</pre> | |
| 5799 | |
| 5800 <p> | |
| 5801 Checks whether <code>cond</code> is true. | |
| 5802 If not, raises an error with a standard message. | |
| 5803 | |
| 5804 | |
| 5805 | |
| 5806 | |
| 5807 | |
| 5808 <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p> | |
| 5809 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5810 <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre> | |
| 5811 | |
| 5812 <p> | |
| 5813 Raises an error with a standard message | |
| 5814 that includes <code>extramsg</code> as a comment. | |
| 5815 | |
| 5816 | |
| 5817 <p> | |
| 5818 This function never returns, | |
| 5819 but it is an idiom to use it in C functions | |
| 5820 as <code>return luaL_argerror(<em>args</em>)</code>. | |
| 5821 | |
| 5822 | |
| 5823 | |
| 5824 | |
| 5825 | |
| 5826 <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3> | |
| 5827 <pre>typedef struct luaL_Buffer luaL_Buffer;</pre> | |
| 5828 | |
| 5829 <p> | |
| 5830 Type for a <em>string buffer</em>. | |
| 5831 | |
| 5832 | |
| 5833 <p> | |
| 5834 A string buffer allows C code to build Lua strings piecemeal. | |
| 5835 Its pattern of use is as follows: | |
| 5836 | |
| 5837 <ul> | |
| 5838 | |
| 5839 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> | |
| 5840 | |
| 5841 <li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li> | |
| 5842 | |
| 5843 <li> | |
| 5844 Then add string pieces to the buffer calling any of | |
| 5845 the <code>luaL_add*</code> functions. | |
| 5846 </li> | |
| 5847 | |
| 5848 <li> | |
| 5849 Finish by calling <code>luaL_pushresult(&b)</code>. | |
| 5850 This call leaves the final string on the top of the stack. | |
| 5851 </li> | |
| 5852 | |
| 5853 </ul> | |
| 5854 | |
| 5855 <p> | |
| 5856 If you know beforehand the total size of the resulting string, | |
| 5857 you can use the buffer like this: | |
| 5858 | |
| 5859 <ul> | |
| 5860 | |
| 5861 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li> | |
| 5862 | |
| 5863 <li>Then initialize it and preallocate a space of | |
| 5864 size <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li> | |
| 5865 | |
| 5866 <li>Then copy the string into that space.</li> | |
| 5867 | |
| 5868 <li> | |
| 5869 Finish by calling <code>luaL_pushresultsize(&b, sz)</code>, | |
| 5870 where <code>sz</code> is the total size of the resulting string | |
| 5871 copied into that space. | |
| 5872 </li> | |
| 5873 | |
| 5874 </ul> | |
| 5875 | |
| 5876 <p> | |
| 5877 During its normal operation, | |
| 5878 a string buffer uses a variable number of stack slots. | |
| 5879 So, while using a buffer, you cannot assume that you know where | |
| 5880 the top of the stack is. | |
| 5881 You can use the stack between successive calls to buffer operations | |
| 5882 as long as that use is balanced; | |
| 5883 that is, | |
| 5884 when you call a buffer operation, | |
| 5885 the stack is at the same level | |
| 5886 it was immediately after the previous buffer operation. | |
| 5887 (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.) | |
| 5888 After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its | |
| 5889 level when the buffer was initialized, | |
| 5890 plus the final string on its top. | |
| 5891 | |
| 5892 | |
| 5893 | |
| 5894 | |
| 5895 | |
| 5896 <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p> | |
| 5897 <span class="apii">[-0, +0, –]</span> | |
| 5898 <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre> | |
| 5899 | |
| 5900 <p> | |
| 5901 Initializes a buffer <code>B</code>. | |
| 5902 This function does not allocate any space; | |
| 5903 the buffer must be declared as a variable | |
| 5904 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | |
| 5905 | |
| 5906 | |
| 5907 | |
| 5908 | |
| 5909 | |
| 5910 <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p> | |
| 5911 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 5912 <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre> | |
| 5913 | |
| 5914 <p> | |
| 5915 Equivalent to the sequence | |
| 5916 <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>. | |
| 5917 | |
| 5918 | |
| 5919 | |
| 5920 | |
| 5921 | |
| 5922 <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p> | |
| 5923 <span class="apii">[-0, +(0|1), <em>e</em>]</span> | |
| 5924 <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre> | |
| 5925 | |
| 5926 <p> | |
| 5927 Calls a metamethod. | |
| 5928 | |
| 5929 | |
| 5930 <p> | |
| 5931 If the object at index <code>obj</code> has a metatable and this | |
| 5932 metatable has a field <code>e</code>, | |
| 5933 this function calls this field passing the object as its only argument. | |
| 5934 In this case this function returns true and pushes onto the | |
| 5935 stack the value returned by the call. | |
| 5936 If there is no metatable or no metamethod, | |
| 5937 this function returns false (without pushing any value on the stack). | |
| 5938 | |
| 5939 | |
| 5940 | |
| 5941 | |
| 5942 | |
| 5943 <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p> | |
| 5944 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5945 <pre>void luaL_checkany (lua_State *L, int arg);</pre> | |
| 5946 | |
| 5947 <p> | |
| 5948 Checks whether the function has an argument | |
| 5949 of any type (including <b>nil</b>) at position <code>arg</code>. | |
| 5950 | |
| 5951 | |
| 5952 | |
| 5953 | |
| 5954 | |
| 5955 <hr><h3><a name="luaL_checkint"><code>luaL_checkint</code></a></h3><p> | |
| 5956 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5957 <pre>int luaL_checkint (lua_State *L, int arg);</pre> | |
| 5958 | |
| 5959 <p> | |
| 5960 Checks whether the function argument <code>arg</code> is a number | |
| 5961 and returns this number cast to an <code>int</code>. | |
| 5962 | |
| 5963 | |
| 5964 | |
| 5965 | |
| 5966 | |
| 5967 <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p> | |
| 5968 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5969 <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre> | |
| 5970 | |
| 5971 <p> | |
| 5972 Checks whether the function argument <code>arg</code> is a number | |
| 5973 and returns this number cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>. | |
| 5974 | |
| 5975 | |
| 5976 | |
| 5977 | |
| 5978 | |
| 5979 <hr><h3><a name="luaL_checklong"><code>luaL_checklong</code></a></h3><p> | |
| 5980 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5981 <pre>long luaL_checklong (lua_State *L, int arg);</pre> | |
| 5982 | |
| 5983 <p> | |
| 5984 Checks whether the function argument <code>arg</code> is a number | |
| 5985 and returns this number cast to a <code>long</code>. | |
| 5986 | |
| 5987 | |
| 5988 | |
| 5989 | |
| 5990 | |
| 5991 <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p> | |
| 5992 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 5993 <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre> | |
| 5994 | |
| 5995 <p> | |
| 5996 Checks whether the function argument <code>arg</code> is a string | |
| 5997 and returns this string; | |
| 5998 if <code>l</code> is not <code>NULL</code> fills <code>*l</code> | |
| 5999 with the string's length. | |
| 6000 | |
| 6001 | |
| 6002 <p> | |
| 6003 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, | |
| 6004 so all conversions and caveats of that function apply here. | |
| 6005 | |
| 6006 | |
| 6007 | |
| 6008 | |
| 6009 | |
| 6010 <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p> | |
| 6011 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6012 <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre> | |
| 6013 | |
| 6014 <p> | |
| 6015 Checks whether the function argument <code>arg</code> is a number | |
| 6016 and returns this number. | |
| 6017 | |
| 6018 | |
| 6019 | |
| 6020 | |
| 6021 | |
| 6022 <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p> | |
| 6023 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6024 <pre>int luaL_checkoption (lua_State *L, | |
| 6025 int arg, | |
| 6026 const char *def, | |
| 6027 const char *const lst[]);</pre> | |
| 6028 | |
| 6029 <p> | |
| 6030 Checks whether the function argument <code>arg</code> is a string and | |
| 6031 searches for this string in the array <code>lst</code> | |
| 6032 (which must be NULL-terminated). | |
| 6033 Returns the index in the array where the string was found. | |
| 6034 Raises an error if the argument is not a string or | |
| 6035 if the string cannot be found. | |
| 6036 | |
| 6037 | |
| 6038 <p> | |
| 6039 If <code>def</code> is not <code>NULL</code>, | |
| 6040 the function uses <code>def</code> as a default value when | |
| 6041 there is no argument <code>arg</code> or when this argument is <b>nil</b>. | |
| 6042 | |
| 6043 | |
| 6044 <p> | |
| 6045 This is a useful function for mapping strings to C enums. | |
| 6046 (The usual convention in Lua libraries is | |
| 6047 to use strings instead of numbers to select options.) | |
| 6048 | |
| 6049 | |
| 6050 | |
| 6051 | |
| 6052 | |
| 6053 <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p> | |
| 6054 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6055 <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre> | |
| 6056 | |
| 6057 <p> | |
| 6058 Grows the stack size to <code>top + sz</code> elements, | |
| 6059 raising an error if the stack cannot grow to that size. | |
| 6060 <code>msg</code> is an additional text to go into the error message | |
| 6061 (or <code>NULL</code> for no additional text). | |
| 6062 | |
| 6063 | |
| 6064 | |
| 6065 | |
| 6066 | |
| 6067 <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p> | |
| 6068 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6069 <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre> | |
| 6070 | |
| 6071 <p> | |
| 6072 Checks whether the function argument <code>arg</code> is a string | |
| 6073 and returns this string. | |
| 6074 | |
| 6075 | |
| 6076 <p> | |
| 6077 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result, | |
| 6078 so all conversions and caveats of that function apply here. | |
| 6079 | |
| 6080 | |
| 6081 | |
| 6082 | |
| 6083 | |
| 6084 <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p> | |
| 6085 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6086 <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre> | |
| 6087 | |
| 6088 <p> | |
| 6089 Checks whether the function argument <code>arg</code> has type <code>t</code>. | |
| 6090 See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>. | |
| 6091 | |
| 6092 | |
| 6093 | |
| 6094 | |
| 6095 | |
| 6096 <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p> | |
| 6097 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6098 <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre> | |
| 6099 | |
| 6100 <p> | |
| 6101 Checks whether the function argument <code>arg</code> is a userdata | |
| 6102 of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and | |
| 6103 returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>). | |
| 6104 | |
| 6105 | |
| 6106 | |
| 6107 | |
| 6108 | |
| 6109 <hr><h3><a name="luaL_checkunsigned"><code>luaL_checkunsigned</code></a></h3><p> | |
| 6110 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6111 <pre>lua_Unsigned luaL_checkunsigned (lua_State *L, int arg);</pre> | |
| 6112 | |
| 6113 <p> | |
| 6114 Checks whether the function argument <code>arg</code> is a number | |
| 6115 and returns this number cast to a <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>. | |
| 6116 | |
| 6117 | |
| 6118 | |
| 6119 | |
| 6120 | |
| 6121 <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p> | |
| 6122 <span class="apii">[-0, +0, –]</span> | |
| 6123 <pre>void luaL_checkversion (lua_State *L);</pre> | |
| 6124 | |
| 6125 <p> | |
| 6126 Checks whether the core running the call, | |
| 6127 the core that created the Lua state, | |
| 6128 and the code making the call are all using the same version of Lua. | |
| 6129 Also checks whether the core running the call | |
| 6130 and the core that created the Lua state | |
| 6131 are using the same address space. | |
| 6132 | |
| 6133 | |
| 6134 | |
| 6135 | |
| 6136 | |
| 6137 <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p> | |
| 6138 <span class="apii">[-0, +?, <em>e</em>]</span> | |
| 6139 <pre>int luaL_dofile (lua_State *L, const char *filename);</pre> | |
| 6140 | |
| 6141 <p> | |
| 6142 Loads and runs the given file. | |
| 6143 It is defined as the following macro: | |
| 6144 | |
| 6145 <pre> | |
| 6146 (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0)) | |
| 6147 </pre><p> | |
| 6148 It returns false if there are no errors | |
| 6149 or true in case of errors. | |
| 6150 | |
| 6151 | |
| 6152 | |
| 6153 | |
| 6154 | |
| 6155 <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p> | |
| 6156 <span class="apii">[-0, +?, –]</span> | |
| 6157 <pre>int luaL_dostring (lua_State *L, const char *str);</pre> | |
| 6158 | |
| 6159 <p> | |
| 6160 Loads and runs the given string. | |
| 6161 It is defined as the following macro: | |
| 6162 | |
| 6163 <pre> | |
| 6164 (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0)) | |
| 6165 </pre><p> | |
| 6166 It returns false if there are no errors | |
| 6167 or true in case of errors. | |
| 6168 | |
| 6169 | |
| 6170 | |
| 6171 | |
| 6172 | |
| 6173 <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p> | |
| 6174 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6175 <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre> | |
| 6176 | |
| 6177 <p> | |
| 6178 Raises an error. | |
| 6179 The error message format is given by <code>fmt</code> | |
| 6180 plus any extra arguments, | |
| 6181 following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>. | |
| 6182 It also adds at the beginning of the message the file name and | |
| 6183 the line number where the error occurred, | |
| 6184 if this information is available. | |
| 6185 | |
| 6186 | |
| 6187 <p> | |
| 6188 This function never returns, | |
| 6189 but it is an idiom to use it in C functions | |
| 6190 as <code>return luaL_error(<em>args</em>)</code>. | |
| 6191 | |
| 6192 | |
| 6193 | |
| 6194 | |
| 6195 | |
| 6196 <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p> | |
| 6197 <span class="apii">[-0, +3, <em>e</em>]</span> | |
| 6198 <pre>int luaL_execresult (lua_State *L, int stat);</pre> | |
| 6199 | |
| 6200 <p> | |
| 6201 This function produces the return values for | |
| 6202 process-related functions in the standard library | |
| 6203 (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>). | |
| 6204 | |
| 6205 | |
| 6206 | |
| 6207 | |
| 6208 | |
| 6209 <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p> | |
| 6210 <span class="apii">[-0, +(1|3), <em>e</em>]</span> | |
| 6211 <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre> | |
| 6212 | |
| 6213 <p> | |
| 6214 This function produces the return values for | |
| 6215 file-related functions in the standard library | |
| 6216 (<a href="#pdf-io.open"><code>io.open</code></a>, <a href="#pdf-os.rename"><code>os.rename</code></a>, <a href="#pdf-file:seek"><code>file:seek</code></a>, etc.). | |
| 6217 | |
| 6218 | |
| 6219 | |
| 6220 | |
| 6221 | |
| 6222 <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p> | |
| 6223 <span class="apii">[-0, +(0|1), <em>e</em>]</span> | |
| 6224 <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre> | |
| 6225 | |
| 6226 <p> | |
| 6227 Pushes onto the stack the field <code>e</code> from the metatable | |
| 6228 of the object at index <code>obj</code>. | |
| 6229 If the object does not have a metatable, | |
| 6230 or if the metatable does not have this field, | |
| 6231 returns false and pushes nothing. | |
| 6232 | |
| 6233 | |
| 6234 | |
| 6235 | |
| 6236 | |
| 6237 <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p> | |
| 6238 <span class="apii">[-0, +1, –]</span> | |
| 6239 <pre>void luaL_getmetatable (lua_State *L, const char *tname);</pre> | |
| 6240 | |
| 6241 <p> | |
| 6242 Pushes onto the stack the metatable associated with name <code>tname</code> | |
| 6243 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). | |
| 6244 | |
| 6245 | |
| 6246 | |
| 6247 | |
| 6248 | |
| 6249 <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p> | |
| 6250 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6251 <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre> | |
| 6252 | |
| 6253 <p> | |
| 6254 Ensures that the value <code>t[fname]</code>, | |
| 6255 where <code>t</code> is the value at index <code>idx</code>, | |
| 6256 is a table, | |
| 6257 and pushes that table onto the stack. | |
| 6258 Returns true if it finds a previous table there | |
| 6259 and false if it creates a new table. | |
| 6260 | |
| 6261 | |
| 6262 | |
| 6263 | |
| 6264 | |
| 6265 <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p> | |
| 6266 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6267 <pre>const char *luaL_gsub (lua_State *L, | |
| 6268 const char *s, | |
| 6269 const char *p, | |
| 6270 const char *r);</pre> | |
| 6271 | |
| 6272 <p> | |
| 6273 Creates a copy of string <code>s</code> by replacing | |
| 6274 any occurrence of the string <code>p</code> | |
| 6275 with the string <code>r</code>. | |
| 6276 Pushes the resulting string on the stack and returns it. | |
| 6277 | |
| 6278 | |
| 6279 | |
| 6280 | |
| 6281 | |
| 6282 <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p> | |
| 6283 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 6284 <pre>int luaL_len (lua_State *L, int index);</pre> | |
| 6285 | |
| 6286 <p> | |
| 6287 Returns the "length" of the value at the given index | |
| 6288 as a number; | |
| 6289 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.6">§3.4.6</a>). | |
| 6290 Raises an error if the result of the operation is not a number. | |
| 6291 (This case only can happen through metamethods.) | |
| 6292 | |
| 6293 | |
| 6294 | |
| 6295 | |
| 6296 | |
| 6297 <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p> | |
| 6298 <span class="apii">[-0, +1, –]</span> | |
| 6299 <pre>int luaL_loadbuffer (lua_State *L, | |
| 6300 const char *buff, | |
| 6301 size_t sz, | |
| 6302 const char *name);</pre> | |
| 6303 | |
| 6304 <p> | |
| 6305 Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>. | |
| 6306 | |
| 6307 | |
| 6308 | |
| 6309 | |
| 6310 | |
| 6311 <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p> | |
| 6312 <span class="apii">[-0, +1, –]</span> | |
| 6313 <pre>int luaL_loadbufferx (lua_State *L, | |
| 6314 const char *buff, | |
| 6315 size_t sz, | |
| 6316 const char *name, | |
| 6317 const char *mode);</pre> | |
| 6318 | |
| 6319 <p> | |
| 6320 Loads a buffer as a Lua chunk. | |
| 6321 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the | |
| 6322 buffer pointed to by <code>buff</code> with size <code>sz</code>. | |
| 6323 | |
| 6324 | |
| 6325 <p> | |
| 6326 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. | |
| 6327 <code>name</code> is the chunk name, | |
| 6328 used for debug information and error messages. | |
| 6329 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>. | |
| 6330 | |
| 6331 | |
| 6332 | |
| 6333 | |
| 6334 | |
| 6335 <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p> | |
| 6336 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6337 <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre> | |
| 6338 | |
| 6339 <p> | |
| 6340 Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>. | |
| 6341 | |
| 6342 | |
| 6343 | |
| 6344 | |
| 6345 | |
| 6346 <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p> | |
| 6347 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6348 <pre>int luaL_loadfilex (lua_State *L, const char *filename, | |
| 6349 const char *mode);</pre> | |
| 6350 | |
| 6351 <p> | |
| 6352 Loads a file as a Lua chunk. | |
| 6353 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file | |
| 6354 named <code>filename</code>. | |
| 6355 If <code>filename</code> is <code>NULL</code>, | |
| 6356 then it loads from the standard input. | |
| 6357 The first line in the file is ignored if it starts with a <code>#</code>. | |
| 6358 | |
| 6359 | |
| 6360 <p> | |
| 6361 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>. | |
| 6362 | |
| 6363 | |
| 6364 <p> | |
| 6365 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>, | |
| 6366 but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a> | |
| 6367 if it cannot open/read the file or the file has a wrong mode. | |
| 6368 | |
| 6369 | |
| 6370 <p> | |
| 6371 As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; | |
| 6372 it does not run it. | |
| 6373 | |
| 6374 | |
| 6375 | |
| 6376 | |
| 6377 | |
| 6378 <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p> | |
| 6379 <span class="apii">[-0, +1, –]</span> | |
| 6380 <pre>int luaL_loadstring (lua_State *L, const char *s);</pre> | |
| 6381 | |
| 6382 <p> | |
| 6383 Loads a string as a Lua chunk. | |
| 6384 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in | |
| 6385 the zero-terminated string <code>s</code>. | |
| 6386 | |
| 6387 | |
| 6388 <p> | |
| 6389 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>. | |
| 6390 | |
| 6391 | |
| 6392 <p> | |
| 6393 Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk; | |
| 6394 it does not run it. | |
| 6395 | |
| 6396 | |
| 6397 | |
| 6398 | |
| 6399 | |
| 6400 <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p> | |
| 6401 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6402 <pre>void luaL_newlib (lua_State *L, const luaL_Reg *l);</pre> | |
| 6403 | |
| 6404 <p> | |
| 6405 Creates a new table and registers there | |
| 6406 the functions in list <code>l</code>. | |
| 6407 It is implemented as the following macro: | |
| 6408 | |
| 6409 <pre> | |
| 6410 (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)) | |
| 6411 </pre> | |
| 6412 | |
| 6413 | |
| 6414 | |
| 6415 | |
| 6416 <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p> | |
| 6417 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6418 <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre> | |
| 6419 | |
| 6420 <p> | |
| 6421 Creates a new table with a size optimized | |
| 6422 to store all entries in the array <code>l</code> | |
| 6423 (but does not actually store them). | |
| 6424 It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> | |
| 6425 (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>). | |
| 6426 | |
| 6427 | |
| 6428 <p> | |
| 6429 It is implemented as a macro. | |
| 6430 The array <code>l</code> must be the actual array, | |
| 6431 not a pointer to it. | |
| 6432 | |
| 6433 | |
| 6434 | |
| 6435 | |
| 6436 | |
| 6437 <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p> | |
| 6438 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6439 <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre> | |
| 6440 | |
| 6441 <p> | |
| 6442 If the registry already has the key <code>tname</code>, | |
| 6443 returns 0. | |
| 6444 Otherwise, | |
| 6445 creates a new table to be used as a metatable for userdata, | |
| 6446 adds it to the registry with key <code>tname</code>, | |
| 6447 and returns 1. | |
| 6448 | |
| 6449 | |
| 6450 <p> | |
| 6451 In both cases pushes onto the stack the final value associated | |
| 6452 with <code>tname</code> in the registry. | |
| 6453 | |
| 6454 | |
| 6455 | |
| 6456 | |
| 6457 | |
| 6458 <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p> | |
| 6459 <span class="apii">[-0, +0, –]</span> | |
| 6460 <pre>lua_State *luaL_newstate (void);</pre> | |
| 6461 | |
| 6462 <p> | |
| 6463 Creates a new Lua state. | |
| 6464 It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an | |
| 6465 allocator based on the standard C <code>realloc</code> function | |
| 6466 and then sets a panic function (see <a href="#4.6">§4.6</a>) that prints | |
| 6467 an error message to the standard error output in case of fatal | |
| 6468 errors. | |
| 6469 | |
| 6470 | |
| 6471 <p> | |
| 6472 Returns the new state, | |
| 6473 or <code>NULL</code> if there is a memory allocation error. | |
| 6474 | |
| 6475 | |
| 6476 | |
| 6477 | |
| 6478 | |
| 6479 <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p> | |
| 6480 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 6481 <pre>void luaL_openlibs (lua_State *L);</pre> | |
| 6482 | |
| 6483 <p> | |
| 6484 Opens all standard Lua libraries into the given state. | |
| 6485 | |
| 6486 | |
| 6487 | |
| 6488 | |
| 6489 | |
| 6490 <hr><h3><a name="luaL_optint"><code>luaL_optint</code></a></h3><p> | |
| 6491 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6492 <pre>int luaL_optint (lua_State *L, int arg, int d);</pre> | |
| 6493 | |
| 6494 <p> | |
| 6495 If the function argument <code>arg</code> is a number, | |
| 6496 returns this number cast to an <code>int</code>. | |
| 6497 If this argument is absent or is <b>nil</b>, | |
| 6498 returns <code>d</code>. | |
| 6499 Otherwise, raises an error. | |
| 6500 | |
| 6501 | |
| 6502 | |
| 6503 | |
| 6504 | |
| 6505 <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p> | |
| 6506 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6507 <pre>lua_Integer luaL_optinteger (lua_State *L, | |
| 6508 int arg, | |
| 6509 lua_Integer d);</pre> | |
| 6510 | |
| 6511 <p> | |
| 6512 If the function argument <code>arg</code> is a number, | |
| 6513 returns this number cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>. | |
| 6514 If this argument is absent or is <b>nil</b>, | |
| 6515 returns <code>d</code>. | |
| 6516 Otherwise, raises an error. | |
| 6517 | |
| 6518 | |
| 6519 | |
| 6520 | |
| 6521 | |
| 6522 <hr><h3><a name="luaL_optlong"><code>luaL_optlong</code></a></h3><p> | |
| 6523 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6524 <pre>long luaL_optlong (lua_State *L, int arg, long d);</pre> | |
| 6525 | |
| 6526 <p> | |
| 6527 If the function argument <code>arg</code> is a number, | |
| 6528 returns this number cast to a <code>long</code>. | |
| 6529 If this argument is absent or is <b>nil</b>, | |
| 6530 returns <code>d</code>. | |
| 6531 Otherwise, raises an error. | |
| 6532 | |
| 6533 | |
| 6534 | |
| 6535 | |
| 6536 | |
| 6537 <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p> | |
| 6538 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6539 <pre>const char *luaL_optlstring (lua_State *L, | |
| 6540 int arg, | |
| 6541 const char *d, | |
| 6542 size_t *l);</pre> | |
| 6543 | |
| 6544 <p> | |
| 6545 If the function argument <code>arg</code> is a string, | |
| 6546 returns this string. | |
| 6547 If this argument is absent or is <b>nil</b>, | |
| 6548 returns <code>d</code>. | |
| 6549 Otherwise, raises an error. | |
| 6550 | |
| 6551 | |
| 6552 <p> | |
| 6553 If <code>l</code> is not <code>NULL</code>, | |
| 6554 fills the position <code>*l</code> with the result's length. | |
| 6555 | |
| 6556 | |
| 6557 | |
| 6558 | |
| 6559 | |
| 6560 <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p> | |
| 6561 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6562 <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre> | |
| 6563 | |
| 6564 <p> | |
| 6565 If the function argument <code>arg</code> is a number, | |
| 6566 returns this number. | |
| 6567 If this argument is absent or is <b>nil</b>, | |
| 6568 returns <code>d</code>. | |
| 6569 Otherwise, raises an error. | |
| 6570 | |
| 6571 | |
| 6572 | |
| 6573 | |
| 6574 | |
| 6575 <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p> | |
| 6576 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6577 <pre>const char *luaL_optstring (lua_State *L, | |
| 6578 int arg, | |
| 6579 const char *d);</pre> | |
| 6580 | |
| 6581 <p> | |
| 6582 If the function argument <code>arg</code> is a string, | |
| 6583 returns this string. | |
| 6584 If this argument is absent or is <b>nil</b>, | |
| 6585 returns <code>d</code>. | |
| 6586 Otherwise, raises an error. | |
| 6587 | |
| 6588 | |
| 6589 | |
| 6590 | |
| 6591 | |
| 6592 <hr><h3><a name="luaL_optunsigned"><code>luaL_optunsigned</code></a></h3><p> | |
| 6593 <span class="apii">[-0, +0, <em>v</em>]</span> | |
| 6594 <pre>lua_Unsigned luaL_optunsigned (lua_State *L, | |
| 6595 int arg, | |
| 6596 lua_Unsigned u);</pre> | |
| 6597 | |
| 6598 <p> | |
| 6599 If the function argument <code>arg</code> is a number, | |
| 6600 returns this number cast to a <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>. | |
| 6601 If this argument is absent or is <b>nil</b>, | |
| 6602 returns <code>u</code>. | |
| 6603 Otherwise, raises an error. | |
| 6604 | |
| 6605 | |
| 6606 | |
| 6607 | |
| 6608 | |
| 6609 <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p> | |
| 6610 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 6611 <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre> | |
| 6612 | |
| 6613 <p> | |
| 6614 Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a> | |
| 6615 with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>. | |
| 6616 | |
| 6617 | |
| 6618 | |
| 6619 | |
| 6620 | |
| 6621 <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p> | |
| 6622 <span class="apii">[-?, +?, <em>e</em>]</span> | |
| 6623 <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre> | |
| 6624 | |
| 6625 <p> | |
| 6626 Returns an address to a space of size <code>sz</code> | |
| 6627 where you can copy a string to be added to buffer <code>B</code> | |
| 6628 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>). | |
| 6629 After copying the string into this space you must call | |
| 6630 <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add | |
| 6631 it to the buffer. | |
| 6632 | |
| 6633 | |
| 6634 | |
| 6635 | |
| 6636 | |
| 6637 <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p> | |
| 6638 <span class="apii">[-?, +1, <em>e</em>]</span> | |
| 6639 <pre>void luaL_pushresult (luaL_Buffer *B);</pre> | |
| 6640 | |
| 6641 <p> | |
| 6642 Finishes the use of buffer <code>B</code> leaving the final string on | |
| 6643 the top of the stack. | |
| 6644 | |
| 6645 | |
| 6646 | |
| 6647 | |
| 6648 | |
| 6649 <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p> | |
| 6650 <span class="apii">[-?, +1, <em>e</em>]</span> | |
| 6651 <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre> | |
| 6652 | |
| 6653 <p> | |
| 6654 Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>. | |
| 6655 | |
| 6656 | |
| 6657 | |
| 6658 | |
| 6659 | |
| 6660 <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p> | |
| 6661 <span class="apii">[-1, +0, <em>e</em>]</span> | |
| 6662 <pre>int luaL_ref (lua_State *L, int t);</pre> | |
| 6663 | |
| 6664 <p> | |
| 6665 Creates and returns a <em>reference</em>, | |
| 6666 in the table at index <code>t</code>, | |
| 6667 for the object at the top of the stack (and pops the object). | |
| 6668 | |
| 6669 | |
| 6670 <p> | |
| 6671 A reference is a unique integer key. | |
| 6672 As long as you do not manually add integer keys into table <code>t</code>, | |
| 6673 <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns. | |
| 6674 You can retrieve an object referred by reference <code>r</code> | |
| 6675 by calling <code>lua_rawgeti(L, t, r)</code>. | |
| 6676 Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object. | |
| 6677 | |
| 6678 | |
| 6679 <p> | |
| 6680 If the object at the top of the stack is <b>nil</b>, | |
| 6681 <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>. | |
| 6682 The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different | |
| 6683 from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>. | |
| 6684 | |
| 6685 | |
| 6686 | |
| 6687 | |
| 6688 | |
| 6689 <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3> | |
| 6690 <pre>typedef struct luaL_Reg { | |
| 6691 const char *name; | |
| 6692 lua_CFunction func; | |
| 6693 } luaL_Reg;</pre> | |
| 6694 | |
| 6695 <p> | |
| 6696 Type for arrays of functions to be registered by | |
| 6697 <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>. | |
| 6698 <code>name</code> is the function name and <code>func</code> is a pointer to | |
| 6699 the function. | |
| 6700 Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with an sentinel entry | |
| 6701 in which both <code>name</code> and <code>func</code> are <code>NULL</code>. | |
| 6702 | |
| 6703 | |
| 6704 | |
| 6705 | |
| 6706 | |
| 6707 <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p> | |
| 6708 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6709 <pre>void luaL_requiref (lua_State *L, const char *modname, | |
| 6710 lua_CFunction openf, int glb);</pre> | |
| 6711 | |
| 6712 <p> | |
| 6713 Calls function <code>openf</code> with string <code>modname</code> as an argument | |
| 6714 and sets the call result in <code>package.loaded[modname]</code>, | |
| 6715 as if that function has been called through <a href="#pdf-require"><code>require</code></a>. | |
| 6716 | |
| 6717 | |
| 6718 <p> | |
| 6719 If <code>glb</code> is true, | |
| 6720 also stores the result into global <code>modname</code>. | |
| 6721 | |
| 6722 | |
| 6723 <p> | |
| 6724 Leaves a copy of that result on the stack. | |
| 6725 | |
| 6726 | |
| 6727 | |
| 6728 | |
| 6729 | |
| 6730 <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p> | |
| 6731 <span class="apii">[-nup, +0, <em>e</em>]</span> | |
| 6732 <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre> | |
| 6733 | |
| 6734 <p> | |
| 6735 Registers all functions in the array <code>l</code> | |
| 6736 (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack | |
| 6737 (below optional upvalues, see next). | |
| 6738 | |
| 6739 | |
| 6740 <p> | |
| 6741 When <code>nup</code> is not zero, | |
| 6742 all functions are created sharing <code>nup</code> upvalues, | |
| 6743 which must be previously pushed on the stack | |
| 6744 on top of the library table. | |
| 6745 These values are popped from the stack after the registration. | |
| 6746 | |
| 6747 | |
| 6748 | |
| 6749 | |
| 6750 | |
| 6751 <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p> | |
| 6752 <span class="apii">[-0, +0, –]</span> | |
| 6753 <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre> | |
| 6754 | |
| 6755 <p> | |
| 6756 Sets the metatable of the object at the top of the stack | |
| 6757 as the metatable associated with name <code>tname</code> | |
| 6758 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>). | |
| 6759 | |
| 6760 | |
| 6761 | |
| 6762 | |
| 6763 | |
| 6764 <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p> | |
| 6765 <span class="apii">[-0, +0, <em>e</em>]</span> | |
| 6766 <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre> | |
| 6767 | |
| 6768 <p> | |
| 6769 This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>, | |
| 6770 except that, when the test fails, | |
| 6771 it returns <code>NULL</code> instead of throwing an error. | |
| 6772 | |
| 6773 | |
| 6774 | |
| 6775 | |
| 6776 | |
| 6777 <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p> | |
| 6778 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6779 <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre> | |
| 6780 | |
| 6781 <p> | |
| 6782 Converts any Lua value at the given index to a C string | |
| 6783 in a reasonable format. | |
| 6784 The resulting string is pushed onto the stack and also | |
| 6785 returned by the function. | |
| 6786 If <code>len</code> is not <code>NULL</code>, | |
| 6787 the function also sets <code>*len</code> with the string length. | |
| 6788 | |
| 6789 | |
| 6790 <p> | |
| 6791 If the value has a metatable with a <code>"__tostring"</code> field, | |
| 6792 then <code>luaL_tolstring</code> calls the corresponding metamethod | |
| 6793 with the value as argument, | |
| 6794 and uses the result of the call as its result. | |
| 6795 | |
| 6796 | |
| 6797 | |
| 6798 | |
| 6799 | |
| 6800 <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p> | |
| 6801 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6802 <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, | |
| 6803 int level);</pre> | |
| 6804 | |
| 6805 <p> | |
| 6806 Creates and pushes a traceback of the stack <code>L1</code>. | |
| 6807 If <code>msg</code> is not <code>NULL</code> it is appended | |
| 6808 at the beginning of the traceback. | |
| 6809 The <code>level</code> parameter tells at which level | |
| 6810 to start the traceback. | |
| 6811 | |
| 6812 | |
| 6813 | |
| 6814 | |
| 6815 | |
| 6816 <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p> | |
| 6817 <span class="apii">[-0, +0, –]</span> | |
| 6818 <pre>const char *luaL_typename (lua_State *L, int index);</pre> | |
| 6819 | |
| 6820 <p> | |
| 6821 Returns the name of the type of the value at the given index. | |
| 6822 | |
| 6823 | |
| 6824 | |
| 6825 | |
| 6826 | |
| 6827 <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p> | |
| 6828 <span class="apii">[-0, +0, –]</span> | |
| 6829 <pre>void luaL_unref (lua_State *L, int t, int ref);</pre> | |
| 6830 | |
| 6831 <p> | |
| 6832 Releases reference <code>ref</code> from the table at index <code>t</code> | |
| 6833 (see <a href="#luaL_ref"><code>luaL_ref</code></a>). | |
| 6834 The entry is removed from the table, | |
| 6835 so that the referred object can be collected. | |
| 6836 The reference <code>ref</code> is also freed to be used again. | |
| 6837 | |
| 6838 | |
| 6839 <p> | |
| 6840 If <code>ref</code> is <a href="#pdf-LUA_NOREF"><code>LUA_NOREF</code></a> or <a href="#pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>, | |
| 6841 <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing. | |
| 6842 | |
| 6843 | |
| 6844 | |
| 6845 | |
| 6846 | |
| 6847 <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p> | |
| 6848 <span class="apii">[-0, +1, <em>e</em>]</span> | |
| 6849 <pre>void luaL_where (lua_State *L, int lvl);</pre> | |
| 6850 | |
| 6851 <p> | |
| 6852 Pushes onto the stack a string identifying the current position | |
| 6853 of the control at level <code>lvl</code> in the call stack. | |
| 6854 Typically this string has the following format: | |
| 6855 | |
| 6856 <pre> | |
| 6857 <em>chunkname</em>:<em>currentline</em>: | |
| 6858 </pre><p> | |
| 6859 Level 0 is the running function, | |
| 6860 level 1 is the function that called the running function, | |
| 6861 etc. | |
| 6862 | |
| 6863 | |
| 6864 <p> | |
| 6865 This function is used to build a prefix for error messages. | |
| 6866 | |
| 6867 | |
| 6868 | |
| 6869 | |
| 6870 | |
| 6871 | |
| 6872 | |
| 6873 <h1>6 – <a name="6">Standard Libraries</a></h1> | |
| 6874 | |
| 6875 <p> | |
| 6876 The standard Lua libraries provide useful functions | |
| 6877 that are implemented directly through the C API. | |
| 6878 Some of these functions provide essential services to the language | |
| 6879 (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>); | |
| 6880 others provide access to "outside" services (e.g., I/O); | |
| 6881 and others could be implemented in Lua itself, | |
| 6882 but are quite useful or have critical performance requirements that | |
| 6883 deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>). | |
| 6884 | |
| 6885 | |
| 6886 <p> | |
| 6887 All libraries are implemented through the official C API | |
| 6888 and are provided as separate C modules. | |
| 6889 Currently, Lua has the following standard libraries: | |
| 6890 | |
| 6891 <ul> | |
| 6892 | |
| 6893 <li>basic library (<a href="#6.1">§6.1</a>);</li> | |
| 6894 | |
| 6895 <li>coroutine library (<a href="#6.2">§6.2</a>);</li> | |
| 6896 | |
| 6897 <li>package library (<a href="#6.3">§6.3</a>);</li> | |
| 6898 | |
| 6899 <li>string manipulation (<a href="#6.4">§6.4</a>);</li> | |
| 6900 | |
| 6901 <li>table manipulation (<a href="#6.5">§6.5</a>);</li> | |
| 6902 | |
| 6903 <li>mathematical functions (<a href="#6.6">§6.6</a>) (sin, log, etc.);</li> | |
| 6904 | |
| 6905 <li>bitwise operations (<a href="#6.7">§6.7</a>);</li> | |
| 6906 | |
| 6907 <li>input and output (<a href="#6.8">§6.8</a>);</li> | |
| 6908 | |
| 6909 <li>operating system facilities (<a href="#6.9">§6.9</a>);</li> | |
| 6910 | |
| 6911 <li>debug facilities (<a href="#6.10">§6.10</a>).</li> | |
| 6912 | |
| 6913 </ul><p> | |
| 6914 Except for the basic and the package libraries, | |
| 6915 each library provides all its functions as fields of a global table | |
| 6916 or as methods of its objects. | |
| 6917 | |
| 6918 | |
| 6919 <p> | |
| 6920 To have access to these libraries, | |
| 6921 the C host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function, | |
| 6922 which opens all standard libraries. | |
| 6923 Alternatively, | |
| 6924 the host program can open them individually by using | |
| 6925 <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call | |
| 6926 <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library), | |
| 6927 <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library), | |
| 6928 <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library), | |
| 6929 <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library), | |
| 6930 <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library), | |
| 6931 <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library), | |
| 6932 <a name="pdf-luaopen_bit32"><code>luaopen_bit32</code></a> (for the bit library), | |
| 6933 <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library), | |
| 6934 <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the Operating System library), | |
| 6935 and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library). | |
| 6936 These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>. | |
| 6937 | |
| 6938 | |
| 6939 | |
| 6940 <h2>6.1 – <a name="6.1">Basic Functions</a></h2> | |
| 6941 | |
| 6942 <p> | |
| 6943 The basic library provides core functions to Lua. | |
| 6944 If you do not include this library in your application, | |
| 6945 you should check carefully whether you need to provide | |
| 6946 implementations for some of its facilities. | |
| 6947 | |
| 6948 | |
| 6949 <p> | |
| 6950 <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3> | |
| 6951 Issues an error when | |
| 6952 the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>); | |
| 6953 otherwise, returns all its arguments. | |
| 6954 <code>message</code> is an error message; | |
| 6955 when absent, it defaults to "assertion failed!" | |
| 6956 | |
| 6957 | |
| 6958 | |
| 6959 | |
| 6960 <p> | |
| 6961 <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3> | |
| 6962 | |
| 6963 | |
| 6964 <p> | |
| 6965 This function is a generic interface to the garbage collector. | |
| 6966 It performs different functions according to its first argument, <code>opt</code>: | |
| 6967 | |
| 6968 <ul> | |
| 6969 | |
| 6970 <li><b>"<code>collect</code>": </b> | |
| 6971 performs a full garbage-collection cycle. | |
| 6972 This is the default option. | |
| 6973 </li> | |
| 6974 | |
| 6975 <li><b>"<code>stop</code>": </b> | |
| 6976 stops automatic execution of the garbage collector. | |
| 6977 The collector will run only when explicitly invoked, | |
| 6978 until a call to restart it. | |
| 6979 </li> | |
| 6980 | |
| 6981 <li><b>"<code>restart</code>": </b> | |
| 6982 restarts automatic execution of the garbage collector. | |
| 6983 </li> | |
| 6984 | |
| 6985 <li><b>"<code>count</code>": </b> | |
| 6986 returns the total memory in use by Lua (in Kbytes) and | |
| 6987 a second value with the total memory in bytes modulo 1024. | |
| 6988 The first value has a fractional part, | |
| 6989 so the following equality is always true: | |
| 6990 | |
| 6991 <pre> | |
| 6992 k, b = collectgarbage("count") | |
| 6993 assert(k*1024 == math.floor(k)*1024 + b) | |
| 6994 </pre><p> | |
| 6995 (The second result is useful when Lua is compiled | |
| 6996 with a non floating-point type for numbers.) | |
| 6997 </li> | |
| 6998 | |
| 6999 <li><b>"<code>step</code>": </b> | |
| 7000 performs a garbage-collection step. | |
| 7001 The step "size" is controlled by <code>arg</code> | |
| 7002 (larger values mean more steps) in a non-specified way. | |
| 7003 If you want to control the step size | |
| 7004 you must experimentally tune the value of <code>arg</code>. | |
| 7005 Returns <b>true</b> if the step finished a collection cycle. | |
| 7006 </li> | |
| 7007 | |
| 7008 <li><b>"<code>setpause</code>": </b> | |
| 7009 sets <code>arg</code> as the new value for the <em>pause</em> of | |
| 7010 the collector (see <a href="#2.5">§2.5</a>). | |
| 7011 Returns the previous value for <em>pause</em>. | |
| 7012 </li> | |
| 7013 | |
| 7014 <li><b>"<code>setstepmul</code>": </b> | |
| 7015 sets <code>arg</code> as the new value for the <em>step multiplier</em> of | |
| 7016 the collector (see <a href="#2.5">§2.5</a>). | |
| 7017 Returns the previous value for <em>step</em>. | |
| 7018 </li> | |
| 7019 | |
| 7020 <li><b>"<code>isrunning</code>": </b> | |
| 7021 returns a boolean that tells whether the collector is running | |
| 7022 (i.e., not stopped). | |
| 7023 </li> | |
| 7024 | |
| 7025 <li><b>"<code>generational</code>": </b> | |
| 7026 changes the collector to generational mode. | |
| 7027 This is an experimental feature (see <a href="#2.5">§2.5</a>). | |
| 7028 </li> | |
| 7029 | |
| 7030 <li><b>"<code>incremental</code>": </b> | |
| 7031 changes the collector to incremental mode. | |
| 7032 This is the default mode. | |
| 7033 </li> | |
| 7034 | |
| 7035 </ul> | |
| 7036 | |
| 7037 | |
| 7038 | |
| 7039 <p> | |
| 7040 <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3> | |
| 7041 Opens the named file and executes its contents as a Lua chunk. | |
| 7042 When called without arguments, | |
| 7043 <code>dofile</code> executes the contents of the standard input (<code>stdin</code>). | |
| 7044 Returns all values returned by the chunk. | |
| 7045 In case of errors, <code>dofile</code> propagates the error | |
| 7046 to its caller (that is, <code>dofile</code> does not run in protected mode). | |
| 7047 | |
| 7048 | |
| 7049 | |
| 7050 | |
| 7051 <p> | |
| 7052 <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3> | |
| 7053 Terminates the last protected function called | |
| 7054 and returns <code>message</code> as the error message. | |
| 7055 Function <code>error</code> never returns. | |
| 7056 | |
| 7057 | |
| 7058 <p> | |
| 7059 Usually, <code>error</code> adds some information about the error position | |
| 7060 at the beginning of the message, if the message is a string. | |
| 7061 The <code>level</code> argument specifies how to get the error position. | |
| 7062 With level 1 (the default), the error position is where the | |
| 7063 <code>error</code> function was called. | |
| 7064 Level 2 points the error to where the function | |
| 7065 that called <code>error</code> was called; and so on. | |
| 7066 Passing a level 0 avoids the addition of error position information | |
| 7067 to the message. | |
| 7068 | |
| 7069 | |
| 7070 | |
| 7071 | |
| 7072 <p> | |
| 7073 <hr><h3><a name="pdf-_G"><code>_G</code></a></h3> | |
| 7074 A global variable (not a function) that | |
| 7075 holds the global environment (see <a href="#2.2">§2.2</a>). | |
| 7076 Lua itself does not use this variable; | |
| 7077 changing its value does not affect any environment, | |
| 7078 nor vice-versa. | |
| 7079 | |
| 7080 | |
| 7081 | |
| 7082 | |
| 7083 <p> | |
| 7084 <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3> | |
| 7085 | |
| 7086 | |
| 7087 <p> | |
| 7088 If <code>object</code> does not have a metatable, returns <b>nil</b>. | |
| 7089 Otherwise, | |
| 7090 if the object's metatable has a <code>"__metatable"</code> field, | |
| 7091 returns the associated value. | |
| 7092 Otherwise, returns the metatable of the given object. | |
| 7093 | |
| 7094 | |
| 7095 | |
| 7096 | |
| 7097 <p> | |
| 7098 <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3> | |
| 7099 | |
| 7100 | |
| 7101 <p> | |
| 7102 If <code>t</code> has a metamethod <code>__ipairs</code>, | |
| 7103 calls it with <code>t</code> as argument and returns the first three | |
| 7104 results from the call. | |
| 7105 | |
| 7106 | |
| 7107 <p> | |
| 7108 Otherwise, | |
| 7109 returns three values: an iterator function, the table <code>t</code>, and 0, | |
| 7110 so that the construction | |
| 7111 | |
| 7112 <pre> | |
| 7113 for i,v in ipairs(t) do <em>body</em> end | |
| 7114 </pre><p> | |
| 7115 will iterate over the pairs (<code>1,t[1]</code>), (<code>2,t[2]</code>), ..., | |
| 7116 up to the first integer key absent from the table. | |
| 7117 | |
| 7118 | |
| 7119 | |
| 7120 | |
| 7121 <p> | |
| 7122 <hr><h3><a name="pdf-load"><code>load (ld [, source [, mode [, env]]])</code></a></h3> | |
| 7123 | |
| 7124 | |
| 7125 <p> | |
| 7126 Loads a chunk. | |
| 7127 | |
| 7128 | |
| 7129 <p> | |
| 7130 If <code>ld</code> is a string, the chunk is this string. | |
| 7131 If <code>ld</code> is a function, | |
| 7132 <code>load</code> calls it repeatedly to get the chunk pieces. | |
| 7133 Each call to <code>ld</code> must return a string that concatenates | |
| 7134 with previous results. | |
| 7135 A return of an empty string, <b>nil</b>, or no value signals the end of the chunk. | |
| 7136 | |
| 7137 | |
| 7138 <p> | |
| 7139 If there are no syntactic errors, | |
| 7140 returns the compiled chunk as a function; | |
| 7141 otherwise, returns <b>nil</b> plus the error message. | |
| 7142 | |
| 7143 | |
| 7144 <p> | |
| 7145 If the resulting function has upvalues, | |
| 7146 the first upvalue is set to the value of <code>env</code>, | |
| 7147 if that parameter is given, | |
| 7148 or to the value of the global environment. | |
| 7149 (When you load a main chunk, | |
| 7150 the resulting function will always have exactly one upvalue, | |
| 7151 the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>). | |
| 7152 When you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>), | |
| 7153 the resulting function can have arbitrary upvalues.) | |
| 7154 | |
| 7155 | |
| 7156 <p> | |
| 7157 <code>source</code> is used as the source of the chunk for error messages | |
| 7158 and debug information (see <a href="#4.9">§4.9</a>). | |
| 7159 When absent, | |
| 7160 it defaults to <code>ld</code>, if <code>ld</code> is a string, | |
| 7161 or to "<code>=(load)</code>" otherwise. | |
| 7162 | |
| 7163 | |
| 7164 <p> | |
| 7165 The string <code>mode</code> controls whether the chunk can be text or binary | |
| 7166 (that is, a precompiled chunk). | |
| 7167 It may be the string "<code>b</code>" (only binary chunks), | |
| 7168 "<code>t</code>" (only text chunks), | |
| 7169 or "<code>bt</code>" (both binary and text). | |
| 7170 The default is "<code>bt</code>". | |
| 7171 | |
| 7172 | |
| 7173 | |
| 7174 | |
| 7175 <p> | |
| 7176 <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3> | |
| 7177 | |
| 7178 | |
| 7179 <p> | |
| 7180 Similar to <a href="#pdf-load"><code>load</code></a>, | |
| 7181 but gets the chunk from file <code>filename</code> | |
| 7182 or from the standard input, | |
| 7183 if no file name is given. | |
| 7184 | |
| 7185 | |
| 7186 | |
| 7187 | |
| 7188 <p> | |
| 7189 <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3> | |
| 7190 | |
| 7191 | |
| 7192 <p> | |
| 7193 Allows a program to traverse all fields of a table. | |
| 7194 Its first argument is a table and its second argument | |
| 7195 is an index in this table. | |
| 7196 <code>next</code> returns the next index of the table | |
| 7197 and its associated value. | |
| 7198 When called with <b>nil</b> as its second argument, | |
| 7199 <code>next</code> returns an initial index | |
| 7200 and its associated value. | |
| 7201 When called with the last index, | |
| 7202 or with <b>nil</b> in an empty table, | |
| 7203 <code>next</code> returns <b>nil</b>. | |
| 7204 If the second argument is absent, then it is interpreted as <b>nil</b>. | |
| 7205 In particular, | |
| 7206 you can use <code>next(t)</code> to check whether a table is empty. | |
| 7207 | |
| 7208 | |
| 7209 <p> | |
| 7210 The order in which the indices are enumerated is not specified, | |
| 7211 <em>even for numeric indices</em>. | |
| 7212 (To traverse a table in numeric order, | |
| 7213 use a numerical <b>for</b>.) | |
| 7214 | |
| 7215 | |
| 7216 <p> | |
| 7217 The behavior of <code>next</code> is undefined if, | |
| 7218 during the traversal, | |
| 7219 you assign any value to a non-existent field in the table. | |
| 7220 You may however modify existing fields. | |
| 7221 In particular, you may clear existing fields. | |
| 7222 | |
| 7223 | |
| 7224 | |
| 7225 | |
| 7226 <p> | |
| 7227 <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3> | |
| 7228 | |
| 7229 | |
| 7230 <p> | |
| 7231 If <code>t</code> has a metamethod <code>__pairs</code>, | |
| 7232 calls it with <code>t</code> as argument and returns the first three | |
| 7233 results from the call. | |
| 7234 | |
| 7235 | |
| 7236 <p> | |
| 7237 Otherwise, | |
| 7238 returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>, | |
| 7239 so that the construction | |
| 7240 | |
| 7241 <pre> | |
| 7242 for k,v in pairs(t) do <em>body</em> end | |
| 7243 </pre><p> | |
| 7244 will iterate over all key–value pairs of table <code>t</code>. | |
| 7245 | |
| 7246 | |
| 7247 <p> | |
| 7248 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying | |
| 7249 the table during its traversal. | |
| 7250 | |
| 7251 | |
| 7252 | |
| 7253 | |
| 7254 <p> | |
| 7255 <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3> | |
| 7256 | |
| 7257 | |
| 7258 <p> | |
| 7259 Calls function <code>f</code> with | |
| 7260 the given arguments in <em>protected mode</em>. | |
| 7261 This means that any error inside <code>f</code> is not propagated; | |
| 7262 instead, <code>pcall</code> catches the error | |
| 7263 and returns a status code. | |
| 7264 Its first result is the status code (a boolean), | |
| 7265 which is true if the call succeeds without errors. | |
| 7266 In such case, <code>pcall</code> also returns all results from the call, | |
| 7267 after this first result. | |
| 7268 In case of any error, <code>pcall</code> returns <b>false</b> plus the error message. | |
| 7269 | |
| 7270 | |
| 7271 | |
| 7272 | |
| 7273 <p> | |
| 7274 <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3> | |
| 7275 Receives any number of arguments | |
| 7276 and prints their values to <code>stdout</code>, | |
| 7277 using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string. | |
| 7278 <code>print</code> is not intended for formatted output, | |
| 7279 but only as a quick way to show a value, | |
| 7280 for instance for debugging. | |
| 7281 For complete control over the output, | |
| 7282 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>. | |
| 7283 | |
| 7284 | |
| 7285 | |
| 7286 | |
| 7287 <p> | |
| 7288 <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3> | |
| 7289 Checks whether <code>v1</code> is equal to <code>v2</code>, | |
| 7290 without invoking any metamethod. | |
| 7291 Returns a boolean. | |
| 7292 | |
| 7293 | |
| 7294 | |
| 7295 | |
| 7296 <p> | |
| 7297 <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3> | |
| 7298 Gets the real value of <code>table[index]</code>, | |
| 7299 without invoking any metamethod. | |
| 7300 <code>table</code> must be a table; | |
| 7301 <code>index</code> may be any value. | |
| 7302 | |
| 7303 | |
| 7304 | |
| 7305 | |
| 7306 <p> | |
| 7307 <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3> | |
| 7308 Returns the length of the object <code>v</code>, | |
| 7309 which must be a table or a string, | |
| 7310 without invoking any metamethod. | |
| 7311 Returns an integer number. | |
| 7312 | |
| 7313 | |
| 7314 | |
| 7315 | |
| 7316 <p> | |
| 7317 <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3> | |
| 7318 Sets the real value of <code>table[index]</code> to <code>value</code>, | |
| 7319 without invoking any metamethod. | |
| 7320 <code>table</code> must be a table, | |
| 7321 <code>index</code> any value different from <b>nil</b> and NaN, | |
| 7322 and <code>value</code> any Lua value. | |
| 7323 | |
| 7324 | |
| 7325 <p> | |
| 7326 This function returns <code>table</code>. | |
| 7327 | |
| 7328 | |
| 7329 | |
| 7330 | |
| 7331 <p> | |
| 7332 <hr><h3><a name="pdf-select"><code>select (index, ···)</code></a></h3> | |
| 7333 | |
| 7334 | |
| 7335 <p> | |
| 7336 If <code>index</code> is a number, | |
| 7337 returns all arguments after argument number <code>index</code>; | |
| 7338 a negative number indexes from the end (-1 is the last argument). | |
| 7339 Otherwise, <code>index</code> must be the string <code>"#"</code>, | |
| 7340 and <code>select</code> returns the total number of extra arguments it received. | |
| 7341 | |
| 7342 | |
| 7343 | |
| 7344 | |
| 7345 <p> | |
| 7346 <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3> | |
| 7347 | |
| 7348 | |
| 7349 <p> | |
| 7350 Sets the metatable for the given table. | |
| 7351 (You cannot change the metatable of other types from Lua, only from C.) | |
| 7352 If <code>metatable</code> is <b>nil</b>, | |
| 7353 removes the metatable of the given table. | |
| 7354 If the original metatable has a <code>"__metatable"</code> field, | |
| 7355 raises an error. | |
| 7356 | |
| 7357 | |
| 7358 <p> | |
| 7359 This function returns <code>table</code>. | |
| 7360 | |
| 7361 | |
| 7362 | |
| 7363 | |
| 7364 <p> | |
| 7365 <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3> | |
| 7366 | |
| 7367 | |
| 7368 <p> | |
| 7369 When called with no <code>base</code>, | |
| 7370 <code>tonumber</code> tries to convert its argument to a number. | |
| 7371 If the argument is already a number or | |
| 7372 a string convertible to a number (see <a href="#3.4.2">§3.4.2</a>), | |
| 7373 then <code>tonumber</code> returns this number; | |
| 7374 otherwise, it returns <b>nil</b>. | |
| 7375 | |
| 7376 | |
| 7377 <p> | |
| 7378 When called with <code>base</code>, | |
| 7379 then <code>e</code> should be a string to be interpreted as | |
| 7380 an integer numeral in that base. | |
| 7381 The base may be any integer between 2 and 36, inclusive. | |
| 7382 In bases above 10, the letter '<code>A</code>' (in either upper or lower case) | |
| 7383 represents 10, '<code>B</code>' represents 11, and so forth, | |
| 7384 with '<code>Z</code>' representing 35. | |
| 7385 If the string <code>e</code> is not a valid numeral in the given base, | |
| 7386 the function returns <b>nil</b>. | |
| 7387 | |
| 7388 | |
| 7389 | |
| 7390 | |
| 7391 <p> | |
| 7392 <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3> | |
| 7393 Receives a value of any type and | |
| 7394 converts it to a string in a reasonable format. | |
| 7395 (For complete control of how numbers are converted, | |
| 7396 use <a href="#pdf-string.format"><code>string.format</code></a>.) | |
| 7397 | |
| 7398 | |
| 7399 <p> | |
| 7400 If the metatable of <code>v</code> has a <code>"__tostring"</code> field, | |
| 7401 then <code>tostring</code> calls the corresponding value | |
| 7402 with <code>v</code> as argument, | |
| 7403 and uses the result of the call as its result. | |
| 7404 | |
| 7405 | |
| 7406 | |
| 7407 | |
| 7408 <p> | |
| 7409 <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3> | |
| 7410 Returns the type of its only argument, coded as a string. | |
| 7411 The possible results of this function are | |
| 7412 "<code>nil</code>" (a string, not the value <b>nil</b>), | |
| 7413 "<code>number</code>", | |
| 7414 "<code>string</code>", | |
| 7415 "<code>boolean</code>", | |
| 7416 "<code>table</code>", | |
| 7417 "<code>function</code>", | |
| 7418 "<code>thread</code>", | |
| 7419 and "<code>userdata</code>". | |
| 7420 | |
| 7421 | |
| 7422 | |
| 7423 | |
| 7424 <p> | |
| 7425 <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3> | |
| 7426 A global variable (not a function) that | |
| 7427 holds a string containing the current interpreter version. | |
| 7428 The current contents of this variable is "<code>Lua 5.2</code>". | |
| 7429 | |
| 7430 | |
| 7431 | |
| 7432 | |
| 7433 <p> | |
| 7434 <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></h3> | |
| 7435 | |
| 7436 | |
| 7437 <p> | |
| 7438 This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>, | |
| 7439 except that it sets a new message handler <code>msgh</code>. | |
| 7440 | |
| 7441 | |
| 7442 | |
| 7443 | |
| 7444 | |
| 7445 | |
| 7446 | |
| 7447 <h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2> | |
| 7448 | |
| 7449 <p> | |
| 7450 The operations related to coroutines comprise a sub-library of | |
| 7451 the basic library and come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>. | |
| 7452 See <a href="#2.6">§2.6</a> for a general description of coroutines. | |
| 7453 | |
| 7454 | |
| 7455 <p> | |
| 7456 <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3> | |
| 7457 | |
| 7458 | |
| 7459 <p> | |
| 7460 Creates a new coroutine, with body <code>f</code>. | |
| 7461 <code>f</code> must be a Lua function. | |
| 7462 Returns this new coroutine, | |
| 7463 an object with type <code>"thread"</code>. | |
| 7464 | |
| 7465 | |
| 7466 | |
| 7467 | |
| 7468 <p> | |
| 7469 <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3> | |
| 7470 | |
| 7471 | |
| 7472 <p> | |
| 7473 Starts or continues the execution of coroutine <code>co</code>. | |
| 7474 The first time you resume a coroutine, | |
| 7475 it starts running its body. | |
| 7476 The values <code>val1</code>, ... are passed | |
| 7477 as the arguments to the body function. | |
| 7478 If the coroutine has yielded, | |
| 7479 <code>resume</code> restarts it; | |
| 7480 the values <code>val1</code>, ... are passed | |
| 7481 as the results from the yield. | |
| 7482 | |
| 7483 | |
| 7484 <p> | |
| 7485 If the coroutine runs without any errors, | |
| 7486 <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code> | |
| 7487 (if the coroutine yields) or any values returned by the body function | |
| 7488 (if the coroutine terminates). | |
| 7489 If there is any error, | |
| 7490 <code>resume</code> returns <b>false</b> plus the error message. | |
| 7491 | |
| 7492 | |
| 7493 | |
| 7494 | |
| 7495 <p> | |
| 7496 <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3> | |
| 7497 | |
| 7498 | |
| 7499 <p> | |
| 7500 Returns the running coroutine plus a boolean, | |
| 7501 true when the running coroutine is the main one. | |
| 7502 | |
| 7503 | |
| 7504 | |
| 7505 | |
| 7506 <p> | |
| 7507 <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3> | |
| 7508 | |
| 7509 | |
| 7510 <p> | |
| 7511 Returns the status of coroutine <code>co</code>, as a string: | |
| 7512 <code>"running"</code>, | |
| 7513 if the coroutine is running (that is, it called <code>status</code>); | |
| 7514 <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>, | |
| 7515 or if it has not started running yet; | |
| 7516 <code>"normal"</code> if the coroutine is active but not running | |
| 7517 (that is, it has resumed another coroutine); | |
| 7518 and <code>"dead"</code> if the coroutine has finished its body function, | |
| 7519 or if it has stopped with an error. | |
| 7520 | |
| 7521 | |
| 7522 | |
| 7523 | |
| 7524 <p> | |
| 7525 <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3> | |
| 7526 | |
| 7527 | |
| 7528 <p> | |
| 7529 Creates a new coroutine, with body <code>f</code>. | |
| 7530 <code>f</code> must be a Lua function. | |
| 7531 Returns a function that resumes the coroutine each time it is called. | |
| 7532 Any arguments passed to the function behave as the | |
| 7533 extra arguments to <code>resume</code>. | |
| 7534 Returns the same values returned by <code>resume</code>, | |
| 7535 except the first boolean. | |
| 7536 In case of error, propagates the error. | |
| 7537 | |
| 7538 | |
| 7539 | |
| 7540 | |
| 7541 <p> | |
| 7542 <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></h3> | |
| 7543 | |
| 7544 | |
| 7545 <p> | |
| 7546 Suspends the execution of the calling coroutine. | |
| 7547 Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>. | |
| 7548 | |
| 7549 | |
| 7550 | |
| 7551 | |
| 7552 | |
| 7553 | |
| 7554 | |
| 7555 <h2>6.3 – <a name="6.3">Modules</a></h2> | |
| 7556 | |
| 7557 <p> | |
| 7558 The package library provides basic | |
| 7559 facilities for loading modules in Lua. | |
| 7560 It exports one function directly in the global environment: | |
| 7561 <a href="#pdf-require"><code>require</code></a>. | |
| 7562 Everything else is exported in a table <a name="pdf-package"><code>package</code></a>. | |
| 7563 | |
| 7564 | |
| 7565 <p> | |
| 7566 <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3> | |
| 7567 | |
| 7568 | |
| 7569 <p> | |
| 7570 Loads the given module. | |
| 7571 The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table | |
| 7572 to determine whether <code>modname</code> is already loaded. | |
| 7573 If it is, then <code>require</code> returns the value stored | |
| 7574 at <code>package.loaded[modname]</code>. | |
| 7575 Otherwise, it tries to find a <em>loader</em> for the module. | |
| 7576 | |
| 7577 | |
| 7578 <p> | |
| 7579 To find a loader, | |
| 7580 <code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</code></a> sequence. | |
| 7581 By changing this sequence, | |
| 7582 we can change how <code>require</code> looks for a module. | |
| 7583 The following explanation is based on the default configuration | |
| 7584 for <a href="#pdf-package.searchers"><code>package.searchers</code></a>. | |
| 7585 | |
| 7586 | |
| 7587 <p> | |
| 7588 First <code>require</code> queries <code>package.preload[modname]</code>. | |
| 7589 If it has a value, | |
| 7590 this value (which should be a function) is the loader. | |
| 7591 Otherwise <code>require</code> searches for a Lua loader using the | |
| 7592 path stored in <a href="#pdf-package.path"><code>package.path</code></a>. | |
| 7593 If that also fails, it searches for a C loader using the | |
| 7594 path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>. | |
| 7595 If that also fails, | |
| 7596 it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>). | |
| 7597 | |
| 7598 | |
| 7599 <p> | |
| 7600 Once a loader is found, | |
| 7601 <code>require</code> calls the loader with two arguments: | |
| 7602 <code>modname</code> and an extra value dependent on how it got the loader. | |
| 7603 (If the loader came from a file, | |
| 7604 this extra value is the file name.) | |
| 7605 If the loader returns any non-nil value, | |
| 7606 <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>. | |
| 7607 If the loader does not return a non-nil value and | |
| 7608 has not assigned any value to <code>package.loaded[modname]</code>, | |
| 7609 then <code>require</code> assigns <b>true</b> to this entry. | |
| 7610 In any case, <code>require</code> returns the | |
| 7611 final value of <code>package.loaded[modname]</code>. | |
| 7612 | |
| 7613 | |
| 7614 <p> | |
| 7615 If there is any error loading or running the module, | |
| 7616 or if it cannot find any loader for the module, | |
| 7617 then <code>require</code> raises an error. | |
| 7618 | |
| 7619 | |
| 7620 | |
| 7621 | |
| 7622 <p> | |
| 7623 <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3> | |
| 7624 | |
| 7625 | |
| 7626 <p> | |
| 7627 A string describing some compile-time configurations for packages. | |
| 7628 This string is a sequence of lines: | |
| 7629 | |
| 7630 <ul> | |
| 7631 | |
| 7632 <li>The first line is the directory separator string. | |
| 7633 Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li> | |
| 7634 | |
| 7635 <li>The second line is the character that separates templates in a path. | |
| 7636 Default is '<code>;</code>'.</li> | |
| 7637 | |
| 7638 <li>The third line is the string that marks the | |
| 7639 substitution points in a template. | |
| 7640 Default is '<code>?</code>'.</li> | |
| 7641 | |
| 7642 <li>The fourth line is a string that, in a path in Windows, | |
| 7643 is replaced by the executable's directory. | |
| 7644 Default is '<code>!</code>'.</li> | |
| 7645 | |
| 7646 <li>The fifth line is a mark to ignore all text before it | |
| 7647 when building the <code>luaopen_</code> function name. | |
| 7648 Default is '<code>-</code>'.</li> | |
| 7649 | |
| 7650 </ul> | |
| 7651 | |
| 7652 | |
| 7653 | |
| 7654 <p> | |
| 7655 <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3> | |
| 7656 | |
| 7657 | |
| 7658 <p> | |
| 7659 The path used by <a href="#pdf-require"><code>require</code></a> to search for a C loader. | |
| 7660 | |
| 7661 | |
| 7662 <p> | |
| 7663 Lua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way | |
| 7664 it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>, | |
| 7665 using the environment variable <a name="pdf-LUA_CPATH_5_2"><code>LUA_CPATH_5_2</code></a> | |
| 7666 or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a> | |
| 7667 or a default path defined in <code>luaconf.h</code>. | |
| 7668 | |
| 7669 | |
| 7670 | |
| 7671 | |
| 7672 <p> | |
| 7673 <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3> | |
| 7674 | |
| 7675 | |
| 7676 <p> | |
| 7677 A table used by <a href="#pdf-require"><code>require</code></a> to control which | |
| 7678 modules are already loaded. | |
| 7679 When you require a module <code>modname</code> and | |
| 7680 <code>package.loaded[modname]</code> is not false, | |
| 7681 <a href="#pdf-require"><code>require</code></a> simply returns the value stored there. | |
| 7682 | |
| 7683 | |
| 7684 <p> | |
| 7685 This variable is only a reference to the real table; | |
| 7686 assignments to this variable do not change the | |
| 7687 table used by <a href="#pdf-require"><code>require</code></a>. | |
| 7688 | |
| 7689 | |
| 7690 | |
| 7691 | |
| 7692 <p> | |
| 7693 <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3> | |
| 7694 | |
| 7695 | |
| 7696 <p> | |
| 7697 Dynamically links the host program with the C library <code>libname</code>. | |
| 7698 | |
| 7699 | |
| 7700 <p> | |
| 7701 If <code>funcname</code> is "<code>*</code>", | |
| 7702 then it only links with the library, | |
| 7703 making the symbols exported by the library | |
| 7704 available to other dynamically linked libraries. | |
| 7705 Otherwise, | |
| 7706 it looks for a function <code>funcname</code> inside the library | |
| 7707 and returns this function as a C function. | |
| 7708 So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype | |
| 7709 (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>). | |
| 7710 | |
| 7711 | |
| 7712 <p> | |
| 7713 This is a low-level function. | |
| 7714 It completely bypasses the package and module system. | |
| 7715 Unlike <a href="#pdf-require"><code>require</code></a>, | |
| 7716 it does not perform any path searching and | |
| 7717 does not automatically adds extensions. | |
| 7718 <code>libname</code> must be the complete file name of the C library, | |
| 7719 including if necessary a path and an extension. | |
| 7720 <code>funcname</code> must be the exact name exported by the C library | |
| 7721 (which may depend on the C compiler and linker used). | |
| 7722 | |
| 7723 | |
| 7724 <p> | |
| 7725 This function is not supported by Standard C. | |
| 7726 As such, it is only available on some platforms | |
| 7727 (Windows, Linux, Mac OS X, Solaris, BSD, | |
| 7728 plus other Unix systems that support the <code>dlfcn</code> standard). | |
| 7729 | |
| 7730 | |
| 7731 | |
| 7732 | |
| 7733 <p> | |
| 7734 <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3> | |
| 7735 | |
| 7736 | |
| 7737 <p> | |
| 7738 The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader. | |
| 7739 | |
| 7740 | |
| 7741 <p> | |
| 7742 At start-up, Lua initializes this variable with | |
| 7743 the value of the environment variable <a name="pdf-LUA_PATH_5_2"><code>LUA_PATH_5_2</code></a> or | |
| 7744 the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or | |
| 7745 with a default path defined in <code>luaconf.h</code>, | |
| 7746 if those environment variables are not defined. | |
| 7747 Any "<code>;;</code>" in the value of the environment variable | |
| 7748 is replaced by the default path. | |
| 7749 | |
| 7750 | |
| 7751 | |
| 7752 | |
| 7753 <p> | |
| 7754 <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3> | |
| 7755 | |
| 7756 | |
| 7757 <p> | |
| 7758 A table to store loaders for specific modules | |
| 7759 (see <a href="#pdf-require"><code>require</code></a>). | |
| 7760 | |
| 7761 | |
| 7762 <p> | |
| 7763 This variable is only a reference to the real table; | |
| 7764 assignments to this variable do not change the | |
| 7765 table used by <a href="#pdf-require"><code>require</code></a>. | |
| 7766 | |
| 7767 | |
| 7768 | |
| 7769 | |
| 7770 <p> | |
| 7771 <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3> | |
| 7772 | |
| 7773 | |
| 7774 <p> | |
| 7775 A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules. | |
| 7776 | |
| 7777 | |
| 7778 <p> | |
| 7779 Each entry in this table is a <em>searcher function</em>. | |
| 7780 When looking for a module, | |
| 7781 <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order, | |
| 7782 with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its | |
| 7783 sole parameter. | |
| 7784 The function can return another function (the module <em>loader</em>) | |
| 7785 plus an extra value that will be passed to that loader, | |
| 7786 or a string explaining why it did not find that module | |
| 7787 (or <b>nil</b> if it has nothing to say). | |
| 7788 | |
| 7789 | |
| 7790 <p> | |
| 7791 Lua initializes this table with four searcher functions. | |
| 7792 | |
| 7793 | |
| 7794 <p> | |
| 7795 The first searcher simply looks for a loader in the | |
| 7796 <a href="#pdf-package.preload"><code>package.preload</code></a> table. | |
| 7797 | |
| 7798 | |
| 7799 <p> | |
| 7800 The second searcher looks for a loader as a Lua library, | |
| 7801 using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>. | |
| 7802 The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. | |
| 7803 | |
| 7804 | |
| 7805 <p> | |
| 7806 The third searcher looks for a loader as a C library, | |
| 7807 using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>. | |
| 7808 Again, | |
| 7809 the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. | |
| 7810 For instance, | |
| 7811 if the C path is the string | |
| 7812 | |
| 7813 <pre> | |
| 7814 "./?.so;./?.dll;/usr/local/?/init.so" | |
| 7815 </pre><p> | |
| 7816 the searcher for module <code>foo</code> | |
| 7817 will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>, | |
| 7818 and <code>/usr/local/foo/init.so</code>, in that order. | |
| 7819 Once it finds a C library, | |
| 7820 this searcher first uses a dynamic link facility to link the | |
| 7821 application with the library. | |
| 7822 Then it tries to find a C function inside the library to | |
| 7823 be used as the loader. | |
| 7824 The name of this C function is the string "<code>luaopen_</code>" | |
| 7825 concatenated with a copy of the module name where each dot | |
| 7826 is replaced by an underscore. | |
| 7827 Moreover, if the module name has a hyphen, | |
| 7828 its prefix up to (and including) the first hyphen is removed. | |
| 7829 For instance, if the module name is <code>a.v1-b.c</code>, | |
| 7830 the function name will be <code>luaopen_b_c</code>. | |
| 7831 | |
| 7832 | |
| 7833 <p> | |
| 7834 The fourth searcher tries an <em>all-in-one loader</em>. | |
| 7835 It searches the C path for a library for | |
| 7836 the root name of the given module. | |
| 7837 For instance, when requiring <code>a.b.c</code>, | |
| 7838 it will search for a C library for <code>a</code>. | |
| 7839 If found, it looks into it for an open function for | |
| 7840 the submodule; | |
| 7841 in our example, that would be <code>luaopen_a_b_c</code>. | |
| 7842 With this facility, a package can pack several C submodules | |
| 7843 into one single library, | |
| 7844 with each submodule keeping its original open function. | |
| 7845 | |
| 7846 | |
| 7847 <p> | |
| 7848 All searchers except the first one (preload) return as the extra value | |
| 7849 the file name where the module was found, | |
| 7850 as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>. | |
| 7851 The first searcher returns no extra value. | |
| 7852 | |
| 7853 | |
| 7854 | |
| 7855 | |
| 7856 <p> | |
| 7857 <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3> | |
| 7858 | |
| 7859 | |
| 7860 <p> | |
| 7861 Searches for the given <code>name</code> in the given <code>path</code>. | |
| 7862 | |
| 7863 | |
| 7864 <p> | |
| 7865 A path is a string containing a sequence of | |
| 7866 <em>templates</em> separated by semicolons. | |
| 7867 For each template, | |
| 7868 the function replaces each interrogation mark (if any) | |
| 7869 in the template with a copy of <code>name</code> | |
| 7870 wherein all occurrences of <code>sep</code> | |
| 7871 (a dot, by default) | |
| 7872 were replaced by <code>rep</code> | |
| 7873 (the system's directory separator, by default), | |
| 7874 and then tries to open the resulting file name. | |
| 7875 | |
| 7876 | |
| 7877 <p> | |
| 7878 For instance, if the path is the string | |
| 7879 | |
| 7880 <pre> | |
| 7881 "./?.lua;./?.lc;/usr/local/?/init.lua" | |
| 7882 </pre><p> | |
| 7883 the search for the name <code>foo.a</code> | |
| 7884 will try to open the files | |
| 7885 <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and | |
| 7886 <code>/usr/local/foo/a/init.lua</code>, in that order. | |
| 7887 | |
| 7888 | |
| 7889 <p> | |
| 7890 Returns the resulting name of the first file that it can | |
| 7891 open in read mode (after closing the file), | |
| 7892 or <b>nil</b> plus an error message if none succeeds. | |
| 7893 (This error message lists all file names it tried to open.) | |
| 7894 | |
| 7895 | |
| 7896 | |
| 7897 | |
| 7898 | |
| 7899 | |
| 7900 | |
| 7901 <h2>6.4 – <a name="6.4">String Manipulation</a></h2> | |
| 7902 | |
| 7903 <p> | |
| 7904 This library provides generic functions for string manipulation, | |
| 7905 such as finding and extracting substrings, and pattern matching. | |
| 7906 When indexing a string in Lua, the first character is at position 1 | |
| 7907 (not at 0, as in C). | |
| 7908 Indices are allowed to be negative and are interpreted as indexing backwards, | |
| 7909 from the end of the string. | |
| 7910 Thus, the last character is at position -1, and so on. | |
| 7911 | |
| 7912 | |
| 7913 <p> | |
| 7914 The string library provides all its functions inside the table | |
| 7915 <a name="pdf-string"><code>string</code></a>. | |
| 7916 It also sets a metatable for strings | |
| 7917 where the <code>__index</code> field points to the <code>string</code> table. | |
| 7918 Therefore, you can use the string functions in object-oriented style. | |
| 7919 For instance, <code>string.byte(s,i)</code> | |
| 7920 can be written as <code>s:byte(i)</code>. | |
| 7921 | |
| 7922 | |
| 7923 <p> | |
| 7924 The string library assumes one-byte character encodings. | |
| 7925 | |
| 7926 | |
| 7927 <p> | |
| 7928 <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3> | |
| 7929 Returns the internal numerical codes of the characters <code>s[i]</code>, | |
| 7930 <code>s[i+1]</code>, ..., <code>s[j]</code>. | |
| 7931 The default value for <code>i</code> is 1; | |
| 7932 the default value for <code>j</code> is <code>i</code>. | |
| 7933 These indices are corrected | |
| 7934 following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>. | |
| 7935 | |
| 7936 | |
| 7937 <p> | |
| 7938 Numerical codes are not necessarily portable across platforms. | |
| 7939 | |
| 7940 | |
| 7941 | |
| 7942 | |
| 7943 <p> | |
| 7944 <hr><h3><a name="pdf-string.char"><code>string.char (···)</code></a></h3> | |
| 7945 Receives zero or more integers. | |
| 7946 Returns a string with length equal to the number of arguments, | |
| 7947 in which each character has the internal numerical code equal | |
| 7948 to its corresponding argument. | |
| 7949 | |
| 7950 | |
| 7951 <p> | |
| 7952 Numerical codes are not necessarily portable across platforms. | |
| 7953 | |
| 7954 | |
| 7955 | |
| 7956 | |
| 7957 <p> | |
| 7958 <hr><h3><a name="pdf-string.dump"><code>string.dump (function)</code></a></h3> | |
| 7959 | |
| 7960 | |
| 7961 <p> | |
| 7962 Returns a string containing a binary representation of the given function, | |
| 7963 so that a later <a href="#pdf-load"><code>load</code></a> on this string returns | |
| 7964 a copy of the function (but with new upvalues). | |
| 7965 | |
| 7966 | |
| 7967 | |
| 7968 | |
| 7969 <p> | |
| 7970 <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3> | |
| 7971 | |
| 7972 | |
| 7973 <p> | |
| 7974 Looks for the first match of | |
| 7975 <code>pattern</code> in the string <code>s</code>. | |
| 7976 If it finds a match, then <code>find</code> returns the indices of <code>s</code> | |
| 7977 where this occurrence starts and ends; | |
| 7978 otherwise, it returns <b>nil</b>. | |
| 7979 A third, optional numerical argument <code>init</code> specifies | |
| 7980 where to start the search; | |
| 7981 its default value is 1 and can be negative. | |
| 7982 A value of <b>true</b> as a fourth, optional argument <code>plain</code> | |
| 7983 turns off the pattern matching facilities, | |
| 7984 so the function does a plain "find substring" operation, | |
| 7985 with no characters in <code>pattern</code> being considered magic. | |
| 7986 Note that if <code>plain</code> is given, then <code>init</code> must be given as well. | |
| 7987 | |
| 7988 | |
| 7989 <p> | |
| 7990 If the pattern has captures, | |
| 7991 then in a successful match | |
| 7992 the captured values are also returned, | |
| 7993 after the two indices. | |
| 7994 | |
| 7995 | |
| 7996 | |
| 7997 | |
| 7998 <p> | |
| 7999 <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, ···)</code></a></h3> | |
| 8000 | |
| 8001 | |
| 8002 <p> | |
| 8003 Returns a formatted version of its variable number of arguments | |
| 8004 following the description given in its first argument (which must be a string). | |
| 8005 The format string follows the same rules as the ANSI C function <code>sprintf</code>. | |
| 8006 The only differences are that the options/modifiers | |
| 8007 <code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, <code>n</code>, | |
| 8008 and <code>p</code> are not supported | |
| 8009 and that there is an extra option, <code>q</code>. | |
| 8010 The <code>q</code> option formats a string between double quotes, | |
| 8011 using escape sequences when necessary to ensure that | |
| 8012 it can safely be read back by the Lua interpreter. | |
| 8013 For instance, the call | |
| 8014 | |
| 8015 <pre> | |
| 8016 string.format('%q', 'a string with "quotes" and \n new line') | |
| 8017 </pre><p> | |
| 8018 may produce the string: | |
| 8019 | |
| 8020 <pre> | |
| 8021 "a string with \"quotes\" and \ | |
| 8022 new line" | |
| 8023 </pre> | |
| 8024 | |
| 8025 <p> | |
| 8026 Options | |
| 8027 <code>A</code> and <code>a</code> (when available), | |
| 8028 <code>E</code>, <code>e</code>, <code>f</code>, | |
| 8029 <code>G</code>, and <code>g</code> all expect a number as argument. | |
| 8030 Options <code>c</code>, <code>d</code>, | |
| 8031 <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code> | |
| 8032 also expect a number, | |
| 8033 but the range of that number may be limited by | |
| 8034 the underlying C implementation. | |
| 8035 For options <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>, | |
| 8036 the number cannot be negative. | |
| 8037 Option <code>q</code> expects a string; | |
| 8038 option <code>s</code> expects a string without embedded zeros. | |
| 8039 If the argument to option <code>s</code> is not a string, | |
| 8040 it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>. | |
| 8041 | |
| 8042 | |
| 8043 | |
| 8044 | |
| 8045 <p> | |
| 8046 <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3> | |
| 8047 Returns an iterator function that, | |
| 8048 each time it is called, | |
| 8049 returns the next captures from <code>pattern</code> over the string <code>s</code>. | |
| 8050 If <code>pattern</code> specifies no captures, | |
| 8051 then the whole match is produced in each call. | |
| 8052 | |
| 8053 | |
| 8054 <p> | |
| 8055 As an example, the following loop | |
| 8056 will iterate over all the words from string <code>s</code>, | |
| 8057 printing one per line: | |
| 8058 | |
| 8059 <pre> | |
| 8060 s = "hello world from Lua" | |
| 8061 for w in string.gmatch(s, "%a+") do | |
| 8062 print(w) | |
| 8063 end | |
| 8064 </pre><p> | |
| 8065 The next example collects all pairs <code>key=value</code> from the | |
| 8066 given string into a table: | |
| 8067 | |
| 8068 <pre> | |
| 8069 t = {} | |
| 8070 s = "from=world, to=Lua" | |
| 8071 for k, v in string.gmatch(s, "(%w+)=(%w+)") do | |
| 8072 t[k] = v | |
| 8073 end | |
| 8074 </pre> | |
| 8075 | |
| 8076 <p> | |
| 8077 For this function, a caret '<code>^</code>' at the start of a pattern does not | |
| 8078 work as an anchor, as this would prevent the iteration. | |
| 8079 | |
| 8080 | |
| 8081 | |
| 8082 | |
| 8083 <p> | |
| 8084 <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3> | |
| 8085 Returns a copy of <code>s</code> | |
| 8086 in which all (or the first <code>n</code>, if given) | |
| 8087 occurrences of the <code>pattern</code> have been | |
| 8088 replaced by a replacement string specified by <code>repl</code>, | |
| 8089 which can be a string, a table, or a function. | |
| 8090 <code>gsub</code> also returns, as its second value, | |
| 8091 the total number of matches that occurred. | |
| 8092 The name <code>gsub</code> comes from <em>Global SUBstitution</em>. | |
| 8093 | |
| 8094 | |
| 8095 <p> | |
| 8096 If <code>repl</code> is a string, then its value is used for replacement. | |
| 8097 The character <code>%</code> works as an escape character: | |
| 8098 any sequence in <code>repl</code> of the form <code>%<em>d</em></code>, | |
| 8099 with <em>d</em> between 1 and 9, | |
| 8100 stands for the value of the <em>d</em>-th captured substring. | |
| 8101 The sequence <code>%0</code> stands for the whole match. | |
| 8102 The sequence <code>%%</code> stands for a single <code>%</code>. | |
| 8103 | |
| 8104 | |
| 8105 <p> | |
| 8106 If <code>repl</code> is a table, then the table is queried for every match, | |
| 8107 using the first capture as the key. | |
| 8108 | |
| 8109 | |
| 8110 <p> | |
| 8111 If <code>repl</code> is a function, then this function is called every time a | |
| 8112 match occurs, with all captured substrings passed as arguments, | |
| 8113 in order. | |
| 8114 | |
| 8115 | |
| 8116 <p> | |
| 8117 In any case, | |
| 8118 if the pattern specifies no captures, | |
| 8119 then it behaves as if the whole pattern was inside a capture. | |
| 8120 | |
| 8121 | |
| 8122 <p> | |
| 8123 If the value returned by the table query or by the function call | |
| 8124 is a string or a number, | |
| 8125 then it is used as the replacement string; | |
| 8126 otherwise, if it is <b>false</b> or <b>nil</b>, | |
| 8127 then there is no replacement | |
| 8128 (that is, the original match is kept in the string). | |
| 8129 | |
| 8130 | |
| 8131 <p> | |
| 8132 Here are some examples: | |
| 8133 | |
| 8134 <pre> | |
| 8135 x = string.gsub("hello world", "(%w+)", "%1 %1") | |
| 8136 --> x="hello hello world world" | |
| 8137 | |
| 8138 x = string.gsub("hello world", "%w+", "%0 %0", 1) | |
| 8139 --> x="hello hello world" | |
| 8140 | |
| 8141 x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") | |
| 8142 --> x="world hello Lua from" | |
| 8143 | |
| 8144 x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) | |
| 8145 --> x="home = /home/roberto, user = roberto" | |
| 8146 | |
| 8147 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) | |
| 8148 return load(s)() | |
| 8149 end) | |
| 8150 --> x="4+5 = 9" | |
| 8151 | |
| 8152 local t = {name="lua", version="5.2"} | |
| 8153 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) | |
| 8154 --> x="lua-5.2.tar.gz" | |
| 8155 </pre> | |
| 8156 | |
| 8157 | |
| 8158 | |
| 8159 <p> | |
| 8160 <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3> | |
| 8161 Receives a string and returns its length. | |
| 8162 The empty string <code>""</code> has length 0. | |
| 8163 Embedded zeros are counted, | |
| 8164 so <code>"a\000bc\000"</code> has length 5. | |
| 8165 | |
| 8166 | |
| 8167 | |
| 8168 | |
| 8169 <p> | |
| 8170 <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3> | |
| 8171 Receives a string and returns a copy of this string with all | |
| 8172 uppercase letters changed to lowercase. | |
| 8173 All other characters are left unchanged. | |
| 8174 The definition of what an uppercase letter is depends on the current locale. | |
| 8175 | |
| 8176 | |
| 8177 | |
| 8178 | |
| 8179 <p> | |
| 8180 <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3> | |
| 8181 Looks for the first <em>match</em> of | |
| 8182 <code>pattern</code> in the string <code>s</code>. | |
| 8183 If it finds one, then <code>match</code> returns | |
| 8184 the captures from the pattern; | |
| 8185 otherwise it returns <b>nil</b>. | |
| 8186 If <code>pattern</code> specifies no captures, | |
| 8187 then the whole match is returned. | |
| 8188 A third, optional numerical argument <code>init</code> specifies | |
| 8189 where to start the search; | |
| 8190 its default value is 1 and can be negative. | |
| 8191 | |
| 8192 | |
| 8193 | |
| 8194 | |
| 8195 <p> | |
| 8196 <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3> | |
| 8197 Returns a string that is the concatenation of <code>n</code> copies of | |
| 8198 the string <code>s</code> separated by the string <code>sep</code>. | |
| 8199 The default value for <code>sep</code> is the empty string | |
| 8200 (that is, no separator). | |
| 8201 | |
| 8202 | |
| 8203 | |
| 8204 | |
| 8205 <p> | |
| 8206 <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3> | |
| 8207 Returns a string that is the string <code>s</code> reversed. | |
| 8208 | |
| 8209 | |
| 8210 | |
| 8211 | |
| 8212 <p> | |
| 8213 <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3> | |
| 8214 Returns the substring of <code>s</code> that | |
| 8215 starts at <code>i</code> and continues until <code>j</code>; | |
| 8216 <code>i</code> and <code>j</code> can be negative. | |
| 8217 If <code>j</code> is absent, then it is assumed to be equal to -1 | |
| 8218 (which is the same as the string length). | |
| 8219 In particular, | |
| 8220 the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code> | |
| 8221 with length <code>j</code>, | |
| 8222 and <code>string.sub(s, -i)</code> returns a suffix of <code>s</code> | |
| 8223 with length <code>i</code>. | |
| 8224 | |
| 8225 | |
| 8226 <p> | |
| 8227 If, after the translation of negative indices, | |
| 8228 <code>i</code> is less than 1, | |
| 8229 it is corrected to 1. | |
| 8230 If <code>j</code> is greater than the string length, | |
| 8231 it is corrected to that length. | |
| 8232 If, after these corrections, | |
| 8233 <code>i</code> is greater than <code>j</code>, | |
| 8234 the function returns the empty string. | |
| 8235 | |
| 8236 | |
| 8237 | |
| 8238 | |
| 8239 <p> | |
| 8240 <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3> | |
| 8241 Receives a string and returns a copy of this string with all | |
| 8242 lowercase letters changed to uppercase. | |
| 8243 All other characters are left unchanged. | |
| 8244 The definition of what a lowercase letter is depends on the current locale. | |
| 8245 | |
| 8246 | |
| 8247 | |
| 8248 <h3>6.4.1 – <a name="6.4.1">Patterns</a></h3> | |
| 8249 | |
| 8250 | |
| 8251 <h4>Character Class:</h4><p> | |
| 8252 A <em>character class</em> is used to represent a set of characters. | |
| 8253 The following combinations are allowed in describing a character class: | |
| 8254 | |
| 8255 <ul> | |
| 8256 | |
| 8257 <li><b><em>x</em>: </b> | |
| 8258 (where <em>x</em> is not one of the <em>magic characters</em> | |
| 8259 <code>^$()%.[]*+-?</code>) | |
| 8260 represents the character <em>x</em> itself. | |
| 8261 </li> | |
| 8262 | |
| 8263 <li><b><code>.</code>: </b> (a dot) represents all characters.</li> | |
| 8264 | |
| 8265 <li><b><code>%a</code>: </b> represents all letters.</li> | |
| 8266 | |
| 8267 <li><b><code>%c</code>: </b> represents all control characters.</li> | |
| 8268 | |
| 8269 <li><b><code>%d</code>: </b> represents all digits.</li> | |
| 8270 | |
| 8271 <li><b><code>%g</code>: </b> represents all printable characters except space.</li> | |
| 8272 | |
| 8273 <li><b><code>%l</code>: </b> represents all lowercase letters.</li> | |
| 8274 | |
| 8275 <li><b><code>%p</code>: </b> represents all punctuation characters.</li> | |
| 8276 | |
| 8277 <li><b><code>%s</code>: </b> represents all space characters.</li> | |
| 8278 | |
| 8279 <li><b><code>%u</code>: </b> represents all uppercase letters.</li> | |
| 8280 | |
| 8281 <li><b><code>%w</code>: </b> represents all alphanumeric characters.</li> | |
| 8282 | |
| 8283 <li><b><code>%x</code>: </b> represents all hexadecimal digits.</li> | |
| 8284 | |
| 8285 <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character) | |
| 8286 represents the character <em>x</em>. | |
| 8287 This is the standard way to escape the magic characters. | |
| 8288 Any punctuation character (even the non magic) | |
| 8289 can be preceded by a '<code>%</code>' | |
| 8290 when used to represent itself in a pattern. | |
| 8291 </li> | |
| 8292 | |
| 8293 <li><b><code>[<em>set</em>]</code>: </b> | |
| 8294 represents the class which is the union of all | |
| 8295 characters in <em>set</em>. | |
| 8296 A range of characters can be specified by | |
| 8297 separating the end characters of the range, | |
| 8298 in ascending order, with a '<code>-</code>', | |
| 8299 All classes <code>%</code><em>x</em> described above can also be used as | |
| 8300 components in <em>set</em>. | |
| 8301 All other characters in <em>set</em> represent themselves. | |
| 8302 For example, <code>[%w_]</code> (or <code>[_%w]</code>) | |
| 8303 represents all alphanumeric characters plus the underscore, | |
| 8304 <code>[0-7]</code> represents the octal digits, | |
| 8305 and <code>[0-7%l%-]</code> represents the octal digits plus | |
| 8306 the lowercase letters plus the '<code>-</code>' character. | |
| 8307 | |
| 8308 | |
| 8309 <p> | |
| 8310 The interaction between ranges and classes is not defined. | |
| 8311 Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code> | |
| 8312 have no meaning. | |
| 8313 </li> | |
| 8314 | |
| 8315 <li><b><code>[^<em>set</em>]</code>: </b> | |
| 8316 represents the complement of <em>set</em>, | |
| 8317 where <em>set</em> is interpreted as above. | |
| 8318 </li> | |
| 8319 | |
| 8320 </ul><p> | |
| 8321 For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.), | |
| 8322 the corresponding uppercase letter represents the complement of the class. | |
| 8323 For instance, <code>%S</code> represents all non-space characters. | |
| 8324 | |
| 8325 | |
| 8326 <p> | |
| 8327 The definitions of letter, space, and other character groups | |
| 8328 depend on the current locale. | |
| 8329 In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>. | |
| 8330 | |
| 8331 | |
| 8332 | |
| 8333 | |
| 8334 | |
| 8335 <h4>Pattern Item:</h4><p> | |
| 8336 A <em>pattern item</em> can be | |
| 8337 | |
| 8338 <ul> | |
| 8339 | |
| 8340 <li> | |
| 8341 a single character class, | |
| 8342 which matches any single character in the class; | |
| 8343 </li> | |
| 8344 | |
| 8345 <li> | |
| 8346 a single character class followed by '<code>*</code>', | |
| 8347 which matches 0 or more repetitions of characters in the class. | |
| 8348 These repetition items will always match the longest possible sequence; | |
| 8349 </li> | |
| 8350 | |
| 8351 <li> | |
| 8352 a single character class followed by '<code>+</code>', | |
| 8353 which matches 1 or more repetitions of characters in the class. | |
| 8354 These repetition items will always match the longest possible sequence; | |
| 8355 </li> | |
| 8356 | |
| 8357 <li> | |
| 8358 a single character class followed by '<code>-</code>', | |
| 8359 which also matches 0 or more repetitions of characters in the class. | |
| 8360 Unlike '<code>*</code>', | |
| 8361 these repetition items will always match the shortest possible sequence; | |
| 8362 </li> | |
| 8363 | |
| 8364 <li> | |
| 8365 a single character class followed by '<code>?</code>', | |
| 8366 which matches 0 or 1 occurrence of a character in the class; | |
| 8367 </li> | |
| 8368 | |
| 8369 <li> | |
| 8370 <code>%<em>n</em></code>, for <em>n</em> between 1 and 9; | |
| 8371 such item matches a substring equal to the <em>n</em>-th captured string | |
| 8372 (see below); | |
| 8373 </li> | |
| 8374 | |
| 8375 <li> | |
| 8376 <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters; | |
| 8377 such item matches strings that start with <em>x</em>, end with <em>y</em>, | |
| 8378 and where the <em>x</em> and <em>y</em> are <em>balanced</em>. | |
| 8379 This means that, if one reads the string from left to right, | |
| 8380 counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>, | |
| 8381 the ending <em>y</em> is the first <em>y</em> where the count reaches 0. | |
| 8382 For instance, the item <code>%b()</code> matches expressions with | |
| 8383 balanced parentheses. | |
| 8384 </li> | |
| 8385 | |
| 8386 <li> | |
| 8387 <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>; | |
| 8388 such item matches an empty string at any position such that | |
| 8389 the next character belongs to <em>set</em> | |
| 8390 and the previous character does not belong to <em>set</em>. | |
| 8391 The set <em>set</em> is interpreted as previously described. | |
| 8392 The beginning and the end of the subject are handled as if | |
| 8393 they were the character '<code>\0</code>'. | |
| 8394 </li> | |
| 8395 | |
| 8396 </ul> | |
| 8397 | |
| 8398 | |
| 8399 | |
| 8400 | |
| 8401 <h4>Pattern:</h4><p> | |
| 8402 A <em>pattern</em> is a sequence of pattern items. | |
| 8403 A caret '<code>^</code>' at the beginning of a pattern anchors the match at the | |
| 8404 beginning of the subject string. | |
| 8405 A '<code>$</code>' at the end of a pattern anchors the match at the | |
| 8406 end of the subject string. | |
| 8407 At other positions, | |
| 8408 '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves. | |
| 8409 | |
| 8410 | |
| 8411 | |
| 8412 | |
| 8413 | |
| 8414 <h4>Captures:</h4><p> | |
| 8415 A pattern can contain sub-patterns enclosed in parentheses; | |
| 8416 they describe <em>captures</em>. | |
| 8417 When a match succeeds, the substrings of the subject string | |
| 8418 that match captures are stored (<em>captured</em>) for future use. | |
| 8419 Captures are numbered according to their left parentheses. | |
| 8420 For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>, | |
| 8421 the part of the string matching <code>"a*(.)%w(%s*)"</code> is | |
| 8422 stored as the first capture (and therefore has number 1); | |
| 8423 the character matching "<code>.</code>" is captured with number 2, | |
| 8424 and the part matching "<code>%s*</code>" has number 3. | |
| 8425 | |
| 8426 | |
| 8427 <p> | |
| 8428 As a special case, the empty capture <code>()</code> captures | |
| 8429 the current string position (a number). | |
| 8430 For instance, if we apply the pattern <code>"()aa()"</code> on the | |
| 8431 string <code>"flaaap"</code>, there will be two captures: 3 and 5. | |
| 8432 | |
| 8433 | |
| 8434 | |
| 8435 | |
| 8436 | |
| 8437 | |
| 8438 | |
| 8439 | |
| 8440 | |
| 8441 | |
| 8442 | |
| 8443 <h2>6.5 – <a name="6.5">Table Manipulation</a></h2> | |
| 8444 | |
| 8445 <p> | |
| 8446 This library provides generic functions for table manipulation. | |
| 8447 It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>. | |
| 8448 | |
| 8449 | |
| 8450 <p> | |
| 8451 Remember that, whenever an operation needs the length of a table, | |
| 8452 the table should be a proper sequence | |
| 8453 or have a <code>__len</code> metamethod (see <a href="#3.4.6">§3.4.6</a>). | |
| 8454 All functions ignore non-numeric keys | |
| 8455 in tables given as arguments. | |
| 8456 | |
| 8457 | |
| 8458 <p> | |
| 8459 For performance reasons, | |
| 8460 all table accesses (get/set) performed by these functions are raw. | |
| 8461 | |
| 8462 | |
| 8463 <p> | |
| 8464 <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3> | |
| 8465 | |
| 8466 | |
| 8467 <p> | |
| 8468 Given a list where all elements are strings or numbers, | |
| 8469 returns the string <code>list[i]..sep..list[i+1] ··· sep..list[j]</code>. | |
| 8470 The default value for <code>sep</code> is the empty string, | |
| 8471 the default for <code>i</code> is 1, | |
| 8472 and the default for <code>j</code> is <code>#list</code>. | |
| 8473 If <code>i</code> is greater than <code>j</code>, returns the empty string. | |
| 8474 | |
| 8475 | |
| 8476 | |
| 8477 | |
| 8478 <p> | |
| 8479 <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3> | |
| 8480 | |
| 8481 | |
| 8482 <p> | |
| 8483 Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>, | |
| 8484 shifting up the elements | |
| 8485 <code>list[pos], list[pos+1], ···, list[#list]</code>. | |
| 8486 The default value for <code>pos</code> is <code>#list+1</code>, | |
| 8487 so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end | |
| 8488 of list <code>t</code>. | |
| 8489 | |
| 8490 | |
| 8491 | |
| 8492 | |
| 8493 <p> | |
| 8494 <hr><h3><a name="pdf-table.pack"><code>table.pack (···)</code></a></h3> | |
| 8495 | |
| 8496 | |
| 8497 <p> | |
| 8498 Returns a new table with all parameters stored into keys 1, 2, etc. | |
| 8499 and with a field "<code>n</code>" with the total number of parameters. | |
| 8500 Note that the resulting table may not be a sequence. | |
| 8501 | |
| 8502 | |
| 8503 | |
| 8504 | |
| 8505 <p> | |
| 8506 <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3> | |
| 8507 | |
| 8508 | |
| 8509 <p> | |
| 8510 Removes from <code>list</code> the element at position <code>pos</code>, | |
| 8511 returning the value of the removed element. | |
| 8512 When <code>pos</code> is an integer between 1 and <code>#list</code>, | |
| 8513 it shifts down the elements | |
| 8514 <code>list[pos+1], list[pos+2], ···, list[#list]</code> | |
| 8515 and erases element <code>list[#list]</code>; | |
| 8516 The index <code>pos</code> can also be 0 when <code>#list</code> is 0, | |
| 8517 or <code>#list + 1</code>; | |
| 8518 in those cases, the function erases the element <code>list[pos]</code>. | |
| 8519 | |
| 8520 | |
| 8521 <p> | |
| 8522 The default value for <code>pos</code> is <code>#list</code>, | |
| 8523 so that a call <code>table.remove(t)</code> removes the last element | |
| 8524 of list <code>t</code>. | |
| 8525 | |
| 8526 | |
| 8527 | |
| 8528 | |
| 8529 <p> | |
| 8530 <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3> | |
| 8531 | |
| 8532 | |
| 8533 <p> | |
| 8534 Sorts list elements in a given order, <em>in-place</em>, | |
| 8535 from <code>list[1]</code> to <code>list[#list]</code>. | |
| 8536 If <code>comp</code> is given, | |
| 8537 then it must be a function that receives two list elements | |
| 8538 and returns true when the first element must come | |
| 8539 before the second in the final order | |
| 8540 (so that <code>not comp(list[i+1],list[i])</code> will be true after the sort). | |
| 8541 If <code>comp</code> is not given, | |
| 8542 then the standard Lua operator <code><</code> is used instead. | |
| 8543 | |
| 8544 | |
| 8545 <p> | |
| 8546 The sort algorithm is not stable; | |
| 8547 that is, elements considered equal by the given order | |
| 8548 may have their relative positions changed by the sort. | |
| 8549 | |
| 8550 | |
| 8551 | |
| 8552 | |
| 8553 <p> | |
| 8554 <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3> | |
| 8555 | |
| 8556 | |
| 8557 <p> | |
| 8558 Returns the elements from the given table. | |
| 8559 This function is equivalent to | |
| 8560 | |
| 8561 <pre> | |
| 8562 return list[i], list[i+1], ···, list[j] | |
| 8563 </pre><p> | |
| 8564 By default, <code>i</code> is 1 and <code>j</code> is <code>#list</code>. | |
| 8565 | |
| 8566 | |
| 8567 | |
| 8568 | |
| 8569 | |
| 8570 | |
| 8571 | |
| 8572 <h2>6.6 – <a name="6.6">Mathematical Functions</a></h2> | |
| 8573 | |
| 8574 <p> | |
| 8575 This library is an interface to the standard C math library. | |
| 8576 It provides all its functions inside the table <a name="pdf-math"><code>math</code></a>. | |
| 8577 | |
| 8578 | |
| 8579 <p> | |
| 8580 <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3> | |
| 8581 | |
| 8582 | |
| 8583 <p> | |
| 8584 Returns the absolute value of <code>x</code>. | |
| 8585 | |
| 8586 | |
| 8587 | |
| 8588 | |
| 8589 <p> | |
| 8590 <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3> | |
| 8591 | |
| 8592 | |
| 8593 <p> | |
| 8594 Returns the arc cosine of <code>x</code> (in radians). | |
| 8595 | |
| 8596 | |
| 8597 | |
| 8598 | |
| 8599 <p> | |
| 8600 <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3> | |
| 8601 | |
| 8602 | |
| 8603 <p> | |
| 8604 Returns the arc sine of <code>x</code> (in radians). | |
| 8605 | |
| 8606 | |
| 8607 | |
| 8608 | |
| 8609 <p> | |
| 8610 <hr><h3><a name="pdf-math.atan"><code>math.atan (x)</code></a></h3> | |
| 8611 | |
| 8612 | |
| 8613 <p> | |
| 8614 Returns the arc tangent of <code>x</code> (in radians). | |
| 8615 | |
| 8616 | |
| 8617 | |
| 8618 | |
| 8619 <p> | |
| 8620 <hr><h3><a name="pdf-math.atan2"><code>math.atan2 (y, x)</code></a></h3> | |
| 8621 | |
| 8622 | |
| 8623 <p> | |
| 8624 Returns the arc tangent of <code>y/x</code> (in radians), | |
| 8625 but uses the signs of both parameters to find the | |
| 8626 quadrant of the result. | |
| 8627 (It also handles correctly the case of <code>x</code> being zero.) | |
| 8628 | |
| 8629 | |
| 8630 | |
| 8631 | |
| 8632 <p> | |
| 8633 <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3> | |
| 8634 | |
| 8635 | |
| 8636 <p> | |
| 8637 Returns the smallest integer larger than or equal to <code>x</code>. | |
| 8638 | |
| 8639 | |
| 8640 | |
| 8641 | |
| 8642 <p> | |
| 8643 <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3> | |
| 8644 | |
| 8645 | |
| 8646 <p> | |
| 8647 Returns the cosine of <code>x</code> (assumed to be in radians). | |
| 8648 | |
| 8649 | |
| 8650 | |
| 8651 | |
| 8652 <p> | |
| 8653 <hr><h3><a name="pdf-math.cosh"><code>math.cosh (x)</code></a></h3> | |
| 8654 | |
| 8655 | |
| 8656 <p> | |
| 8657 Returns the hyperbolic cosine of <code>x</code>. | |
| 8658 | |
| 8659 | |
| 8660 | |
| 8661 | |
| 8662 <p> | |
| 8663 <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3> | |
| 8664 | |
| 8665 | |
| 8666 <p> | |
| 8667 Returns the angle <code>x</code> (given in radians) in degrees. | |
| 8668 | |
| 8669 | |
| 8670 | |
| 8671 | |
| 8672 <p> | |
| 8673 <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3> | |
| 8674 | |
| 8675 | |
| 8676 <p> | |
| 8677 Returns the value <em>e<sup>x</sup></em>. | |
| 8678 | |
| 8679 | |
| 8680 | |
| 8681 | |
| 8682 <p> | |
| 8683 <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3> | |
| 8684 | |
| 8685 | |
| 8686 <p> | |
| 8687 Returns the largest integer smaller than or equal to <code>x</code>. | |
| 8688 | |
| 8689 | |
| 8690 | |
| 8691 | |
| 8692 <p> | |
| 8693 <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3> | |
| 8694 | |
| 8695 | |
| 8696 <p> | |
| 8697 Returns the remainder of the division of <code>x</code> by <code>y</code> | |
| 8698 that rounds the quotient towards zero. | |
| 8699 | |
| 8700 | |
| 8701 | |
| 8702 | |
| 8703 <p> | |
| 8704 <hr><h3><a name="pdf-math.frexp"><code>math.frexp (x)</code></a></h3> | |
| 8705 | |
| 8706 | |
| 8707 <p> | |
| 8708 Returns <code>m</code> and <code>e</code> such that <em>x = m2<sup>e</sup></em>, | |
| 8709 <code>e</code> is an integer and the absolute value of <code>m</code> is | |
| 8710 in the range <em>[0.5, 1)</em> | |
| 8711 (or zero when <code>x</code> is zero). | |
| 8712 | |
| 8713 | |
| 8714 | |
| 8715 | |
| 8716 <p> | |
| 8717 <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3> | |
| 8718 | |
| 8719 | |
| 8720 <p> | |
| 8721 The value <code>HUGE_VAL</code>, | |
| 8722 a value larger than or equal to any other numerical value. | |
| 8723 | |
| 8724 | |
| 8725 | |
| 8726 | |
| 8727 <p> | |
| 8728 <hr><h3><a name="pdf-math.ldexp"><code>math.ldexp (m, e)</code></a></h3> | |
| 8729 | |
| 8730 | |
| 8731 <p> | |
| 8732 Returns <em>m2<sup>e</sup></em> (<code>e</code> should be an integer). | |
| 8733 | |
| 8734 | |
| 8735 | |
| 8736 | |
| 8737 <p> | |
| 8738 <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3> | |
| 8739 | |
| 8740 | |
| 8741 <p> | |
| 8742 Returns the logarithm of <code>x</code> in the given base. | |
| 8743 The default for <code>base</code> is <em>e</em> | |
| 8744 (so that the function returns the natural logarithm of <code>x</code>). | |
| 8745 | |
| 8746 | |
| 8747 | |
| 8748 | |
| 8749 <p> | |
| 8750 <hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3> | |
| 8751 | |
| 8752 | |
| 8753 <p> | |
| 8754 Returns the maximum value among its arguments. | |
| 8755 | |
| 8756 | |
| 8757 | |
| 8758 | |
| 8759 <p> | |
| 8760 <hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3> | |
| 8761 | |
| 8762 | |
| 8763 <p> | |
| 8764 Returns the minimum value among its arguments. | |
| 8765 | |
| 8766 | |
| 8767 | |
| 8768 | |
| 8769 <p> | |
| 8770 <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3> | |
| 8771 | |
| 8772 | |
| 8773 <p> | |
| 8774 Returns two numbers, | |
| 8775 the integral part of <code>x</code> and the fractional part of <code>x</code>. | |
| 8776 | |
| 8777 | |
| 8778 | |
| 8779 | |
| 8780 <p> | |
| 8781 <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3> | |
| 8782 | |
| 8783 | |
| 8784 <p> | |
| 8785 The value of <em>π</em>. | |
| 8786 | |
| 8787 | |
| 8788 | |
| 8789 | |
| 8790 <p> | |
| 8791 <hr><h3><a name="pdf-math.pow"><code>math.pow (x, y)</code></a></h3> | |
| 8792 | |
| 8793 | |
| 8794 <p> | |
| 8795 Returns <em>x<sup>y</sup></em>. | |
| 8796 (You can also use the expression <code>x^y</code> to compute this value.) | |
| 8797 | |
| 8798 | |
| 8799 | |
| 8800 | |
| 8801 <p> | |
| 8802 <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3> | |
| 8803 | |
| 8804 | |
| 8805 <p> | |
| 8806 Returns the angle <code>x</code> (given in degrees) in radians. | |
| 8807 | |
| 8808 | |
| 8809 | |
| 8810 | |
| 8811 <p> | |
| 8812 <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3> | |
| 8813 | |
| 8814 | |
| 8815 <p> | |
| 8816 This function is an interface to the simple | |
| 8817 pseudo-random generator function <code>rand</code> provided by Standard C. | |
| 8818 (No guarantees can be given for its statistical properties.) | |
| 8819 | |
| 8820 | |
| 8821 <p> | |
| 8822 When called without arguments, | |
| 8823 returns a uniform pseudo-random real number | |
| 8824 in the range <em>[0,1)</em>. | |
| 8825 When called with an integer number <code>m</code>, | |
| 8826 <code>math.random</code> returns | |
| 8827 a uniform pseudo-random integer in the range <em>[1, m]</em>. | |
| 8828 When called with two integer numbers <code>m</code> and <code>n</code>, | |
| 8829 <code>math.random</code> returns a uniform pseudo-random | |
| 8830 integer in the range <em>[m, n]</em>. | |
| 8831 | |
| 8832 | |
| 8833 | |
| 8834 | |
| 8835 <p> | |
| 8836 <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3> | |
| 8837 | |
| 8838 | |
| 8839 <p> | |
| 8840 Sets <code>x</code> as the "seed" | |
| 8841 for the pseudo-random generator: | |
| 8842 equal seeds produce equal sequences of numbers. | |
| 8843 | |
| 8844 | |
| 8845 | |
| 8846 | |
| 8847 <p> | |
| 8848 <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3> | |
| 8849 | |
| 8850 | |
| 8851 <p> | |
| 8852 Returns the sine of <code>x</code> (assumed to be in radians). | |
| 8853 | |
| 8854 | |
| 8855 | |
| 8856 | |
| 8857 <p> | |
| 8858 <hr><h3><a name="pdf-math.sinh"><code>math.sinh (x)</code></a></h3> | |
| 8859 | |
| 8860 | |
| 8861 <p> | |
| 8862 Returns the hyperbolic sine of <code>x</code>. | |
| 8863 | |
| 8864 | |
| 8865 | |
| 8866 | |
| 8867 <p> | |
| 8868 <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3> | |
| 8869 | |
| 8870 | |
| 8871 <p> | |
| 8872 Returns the square root of <code>x</code>. | |
| 8873 (You can also use the expression <code>x^0.5</code> to compute this value.) | |
| 8874 | |
| 8875 | |
| 8876 | |
| 8877 | |
| 8878 <p> | |
| 8879 <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3> | |
| 8880 | |
| 8881 | |
| 8882 <p> | |
| 8883 Returns the tangent of <code>x</code> (assumed to be in radians). | |
| 8884 | |
| 8885 | |
| 8886 | |
| 8887 | |
| 8888 <p> | |
| 8889 <hr><h3><a name="pdf-math.tanh"><code>math.tanh (x)</code></a></h3> | |
| 8890 | |
| 8891 | |
| 8892 <p> | |
| 8893 Returns the hyperbolic tangent of <code>x</code>. | |
| 8894 | |
| 8895 | |
| 8896 | |
| 8897 | |
| 8898 | |
| 8899 | |
| 8900 | |
| 8901 <h2>6.7 – <a name="6.7">Bitwise Operations</a></h2> | |
| 8902 | |
| 8903 <p> | |
| 8904 This library provides bitwise operations. | |
| 8905 It provides all its functions inside the table <a name="pdf-bit32"><code>bit32</code></a>. | |
| 8906 | |
| 8907 | |
| 8908 <p> | |
| 8909 Unless otherwise stated, | |
| 8910 all functions accept numeric arguments in the range | |
| 8911 <em>(-2<sup>51</sup>,+2<sup>51</sup>)</em>; | |
| 8912 each argument is normalized to | |
| 8913 the remainder of its division by <em>2<sup>32</sup></em> | |
| 8914 and truncated to an integer (in some unspecified way), | |
| 8915 so that its final value falls in the range <em>[0,2<sup>32</sup> - 1]</em>. | |
| 8916 Similarly, all results are in the range <em>[0,2<sup>32</sup> - 1]</em>. | |
| 8917 Note that <code>bit32.bnot(0)</code> is <code>0xFFFFFFFF</code>, | |
| 8918 which is different from <code>-1</code>. | |
| 8919 | |
| 8920 | |
| 8921 <p> | |
| 8922 <hr><h3><a name="pdf-bit32.arshift"><code>bit32.arshift (x, disp)</code></a></h3> | |
| 8923 | |
| 8924 | |
| 8925 <p> | |
| 8926 Returns the number <code>x</code> shifted <code>disp</code> bits to the right. | |
| 8927 The number <code>disp</code> may be any representable integer. | |
| 8928 Negative displacements shift to the left. | |
| 8929 | |
| 8930 | |
| 8931 <p> | |
| 8932 This shift operation is what is called arithmetic shift. | |
| 8933 Vacant bits on the left are filled | |
| 8934 with copies of the higher bit of <code>x</code>; | |
| 8935 vacant bits on the right are filled with zeros. | |
| 8936 In particular, | |
| 8937 displacements with absolute values higher than 31 | |
| 8938 result in zero or <code>0xFFFFFFFF</code> (all original bits are shifted out). | |
| 8939 | |
| 8940 | |
| 8941 | |
| 8942 | |
| 8943 <p> | |
| 8944 <hr><h3><a name="pdf-bit32.band"><code>bit32.band (···)</code></a></h3> | |
| 8945 | |
| 8946 | |
| 8947 <p> | |
| 8948 Returns the bitwise <em>and</em> of its operands. | |
| 8949 | |
| 8950 | |
| 8951 | |
| 8952 | |
| 8953 <p> | |
| 8954 <hr><h3><a name="pdf-bit32.bnot"><code>bit32.bnot (x)</code></a></h3> | |
| 8955 | |
| 8956 | |
| 8957 <p> | |
| 8958 Returns the bitwise negation of <code>x</code>. | |
| 8959 For any integer <code>x</code>, | |
| 8960 the following identity holds: | |
| 8961 | |
| 8962 <pre> | |
| 8963 assert(bit32.bnot(x) == (-1 - x) % 2^32) | |
| 8964 </pre> | |
| 8965 | |
| 8966 | |
| 8967 | |
| 8968 <p> | |
| 8969 <hr><h3><a name="pdf-bit32.bor"><code>bit32.bor (···)</code></a></h3> | |
| 8970 | |
| 8971 | |
| 8972 <p> | |
| 8973 Returns the bitwise <em>or</em> of its operands. | |
| 8974 | |
| 8975 | |
| 8976 | |
| 8977 | |
| 8978 <p> | |
| 8979 <hr><h3><a name="pdf-bit32.btest"><code>bit32.btest (···)</code></a></h3> | |
| 8980 | |
| 8981 | |
| 8982 <p> | |
| 8983 Returns a boolean signaling | |
| 8984 whether the bitwise <em>and</em> of its operands is different from zero. | |
| 8985 | |
| 8986 | |
| 8987 | |
| 8988 | |
| 8989 <p> | |
| 8990 <hr><h3><a name="pdf-bit32.bxor"><code>bit32.bxor (···)</code></a></h3> | |
| 8991 | |
| 8992 | |
| 8993 <p> | |
| 8994 Returns the bitwise <em>exclusive or</em> of its operands. | |
| 8995 | |
| 8996 | |
| 8997 | |
| 8998 | |
| 8999 <p> | |
| 9000 <hr><h3><a name="pdf-bit32.extract"><code>bit32.extract (n, field [, width])</code></a></h3> | |
| 9001 | |
| 9002 | |
| 9003 <p> | |
| 9004 Returns the unsigned number formed by the bits | |
| 9005 <code>field</code> to <code>field + width - 1</code> from <code>n</code>. | |
| 9006 Bits are numbered from 0 (least significant) to 31 (most significant). | |
| 9007 All accessed bits must be in the range <em>[0, 31]</em>. | |
| 9008 | |
| 9009 | |
| 9010 <p> | |
| 9011 The default for <code>width</code> is 1. | |
| 9012 | |
| 9013 | |
| 9014 | |
| 9015 | |
| 9016 <p> | |
| 9017 <hr><h3><a name="pdf-bit32.replace"><code>bit32.replace (n, v, field [, width])</code></a></h3> | |
| 9018 | |
| 9019 | |
| 9020 <p> | |
| 9021 Returns a copy of <code>n</code> with | |
| 9022 the bits <code>field</code> to <code>field + width - 1</code> | |
| 9023 replaced by the value <code>v</code>. | |
| 9024 See <a href="#pdf-bit32.extract"><code>bit32.extract</code></a> for details about <code>field</code> and <code>width</code>. | |
| 9025 | |
| 9026 | |
| 9027 | |
| 9028 | |
| 9029 <p> | |
| 9030 <hr><h3><a name="pdf-bit32.lrotate"><code>bit32.lrotate (x, disp)</code></a></h3> | |
| 9031 | |
| 9032 | |
| 9033 <p> | |
| 9034 Returns the number <code>x</code> rotated <code>disp</code> bits to the left. | |
| 9035 The number <code>disp</code> may be any representable integer. | |
| 9036 | |
| 9037 | |
| 9038 <p> | |
| 9039 For any valid displacement, | |
| 9040 the following identity holds: | |
| 9041 | |
| 9042 <pre> | |
| 9043 assert(bit32.lrotate(x, disp) == bit32.lrotate(x, disp % 32)) | |
| 9044 </pre><p> | |
| 9045 In particular, | |
| 9046 negative displacements rotate to the right. | |
| 9047 | |
| 9048 | |
| 9049 | |
| 9050 | |
| 9051 <p> | |
| 9052 <hr><h3><a name="pdf-bit32.lshift"><code>bit32.lshift (x, disp)</code></a></h3> | |
| 9053 | |
| 9054 | |
| 9055 <p> | |
| 9056 Returns the number <code>x</code> shifted <code>disp</code> bits to the left. | |
| 9057 The number <code>disp</code> may be any representable integer. | |
| 9058 Negative displacements shift to the right. | |
| 9059 In any direction, vacant bits are filled with zeros. | |
| 9060 In particular, | |
| 9061 displacements with absolute values higher than 31 | |
| 9062 result in zero (all bits are shifted out). | |
| 9063 | |
| 9064 | |
| 9065 <p> | |
| 9066 For positive displacements, | |
| 9067 the following equality holds: | |
| 9068 | |
| 9069 <pre> | |
| 9070 assert(bit32.lshift(b, disp) == (b * 2^disp) % 2^32) | |
| 9071 </pre> | |
| 9072 | |
| 9073 | |
| 9074 | |
| 9075 <p> | |
| 9076 <hr><h3><a name="pdf-bit32.rrotate"><code>bit32.rrotate (x, disp)</code></a></h3> | |
| 9077 | |
| 9078 | |
| 9079 <p> | |
| 9080 Returns the number <code>x</code> rotated <code>disp</code> bits to the right. | |
| 9081 The number <code>disp</code> may be any representable integer. | |
| 9082 | |
| 9083 | |
| 9084 <p> | |
| 9085 For any valid displacement, | |
| 9086 the following identity holds: | |
| 9087 | |
| 9088 <pre> | |
| 9089 assert(bit32.rrotate(x, disp) == bit32.rrotate(x, disp % 32)) | |
| 9090 </pre><p> | |
| 9091 In particular, | |
| 9092 negative displacements rotate to the left. | |
| 9093 | |
| 9094 | |
| 9095 | |
| 9096 | |
| 9097 <p> | |
| 9098 <hr><h3><a name="pdf-bit32.rshift"><code>bit32.rshift (x, disp)</code></a></h3> | |
| 9099 | |
| 9100 | |
| 9101 <p> | |
| 9102 Returns the number <code>x</code> shifted <code>disp</code> bits to the right. | |
| 9103 The number <code>disp</code> may be any representable integer. | |
| 9104 Negative displacements shift to the left. | |
| 9105 In any direction, vacant bits are filled with zeros. | |
| 9106 In particular, | |
| 9107 displacements with absolute values higher than 31 | |
| 9108 result in zero (all bits are shifted out). | |
| 9109 | |
| 9110 | |
| 9111 <p> | |
| 9112 For positive displacements, | |
| 9113 the following equality holds: | |
| 9114 | |
| 9115 <pre> | |
| 9116 assert(bit32.rshift(b, disp) == math.floor(b % 2^32 / 2^disp)) | |
| 9117 </pre> | |
| 9118 | |
| 9119 <p> | |
| 9120 This shift operation is what is called logical shift. | |
| 9121 | |
| 9122 | |
| 9123 | |
| 9124 | |
| 9125 | |
| 9126 | |
| 9127 | |
| 9128 <h2>6.8 – <a name="6.8">Input and Output Facilities</a></h2> | |
| 9129 | |
| 9130 <p> | |
| 9131 The I/O library provides two different styles for file manipulation. | |
| 9132 The first one uses implicit file descriptors; | |
| 9133 that is, there are operations to set a default input file and a | |
| 9134 default output file, | |
| 9135 and all input/output operations are over these default files. | |
| 9136 The second style uses explicit file descriptors. | |
| 9137 | |
| 9138 | |
| 9139 <p> | |
| 9140 When using implicit file descriptors, | |
| 9141 all operations are supplied by table <a name="pdf-io"><code>io</code></a>. | |
| 9142 When using explicit file descriptors, | |
| 9143 the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file descriptor | |
| 9144 and then all operations are supplied as methods of the file descriptor. | |
| 9145 | |
| 9146 | |
| 9147 <p> | |
| 9148 The table <code>io</code> also provides | |
| 9149 three predefined file descriptors with their usual meanings from C: | |
| 9150 <a name="pdf-io.stdin"><code>io.stdin</code></a>, <a name="pdf-io.stdout"><code>io.stdout</code></a>, and <a name="pdf-io.stderr"><code>io.stderr</code></a>. | |
| 9151 The I/O library never closes these files. | |
| 9152 | |
| 9153 | |
| 9154 <p> | |
| 9155 Unless otherwise stated, | |
| 9156 all I/O functions return <b>nil</b> on failure | |
| 9157 (plus an error message as a second result and | |
| 9158 a system-dependent error code as a third result) | |
| 9159 and some value different from <b>nil</b> on success. | |
| 9160 On non-Posix systems, | |
| 9161 the computation of the error message and error code | |
| 9162 in case of errors | |
| 9163 may be not thread safe, | |
| 9164 because they rely on the global C variable <code>errno</code>. | |
| 9165 | |
| 9166 | |
| 9167 <p> | |
| 9168 <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3> | |
| 9169 | |
| 9170 | |
| 9171 <p> | |
| 9172 Equivalent to <code>file:close()</code>. | |
| 9173 Without a <code>file</code>, closes the default output file. | |
| 9174 | |
| 9175 | |
| 9176 | |
| 9177 | |
| 9178 <p> | |
| 9179 <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3> | |
| 9180 | |
| 9181 | |
| 9182 <p> | |
| 9183 Equivalent to <code>io.output():flush()</code>. | |
| 9184 | |
| 9185 | |
| 9186 | |
| 9187 | |
| 9188 <p> | |
| 9189 <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3> | |
| 9190 | |
| 9191 | |
| 9192 <p> | |
| 9193 When called with a file name, it opens the named file (in text mode), | |
| 9194 and sets its handle as the default input file. | |
| 9195 When called with a file handle, | |
| 9196 it simply sets this file handle as the default input file. | |
| 9197 When called without parameters, | |
| 9198 it returns the current default input file. | |
| 9199 | |
| 9200 | |
| 9201 <p> | |
| 9202 In case of errors this function raises the error, | |
| 9203 instead of returning an error code. | |
| 9204 | |
| 9205 | |
| 9206 | |
| 9207 | |
| 9208 <p> | |
| 9209 <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename ···])</code></a></h3> | |
| 9210 | |
| 9211 | |
| 9212 <p> | |
| 9213 Opens the given file name in read mode | |
| 9214 and returns an iterator function that | |
| 9215 works like <code>file:lines(···)</code> over the opened file. | |
| 9216 When the iterator function detects the end of file, | |
| 9217 it returns <b>nil</b> (to finish the loop) and automatically closes the file. | |
| 9218 | |
| 9219 | |
| 9220 <p> | |
| 9221 The call <code>io.lines()</code> (with no file name) is equivalent | |
| 9222 to <code>io.input():lines()</code>; | |
| 9223 that is, it iterates over the lines of the default input file. | |
| 9224 In this case it does not close the file when the loop ends. | |
| 9225 | |
| 9226 | |
| 9227 <p> | |
| 9228 In case of errors this function raises the error, | |
| 9229 instead of returning an error code. | |
| 9230 | |
| 9231 | |
| 9232 | |
| 9233 | |
| 9234 <p> | |
| 9235 <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3> | |
| 9236 | |
| 9237 | |
| 9238 <p> | |
| 9239 This function opens a file, | |
| 9240 in the mode specified in the string <code>mode</code>. | |
| 9241 It returns a new file handle, | |
| 9242 or, in case of errors, <b>nil</b> plus an error message. | |
| 9243 | |
| 9244 | |
| 9245 <p> | |
| 9246 The <code>mode</code> string can be any of the following: | |
| 9247 | |
| 9248 <ul> | |
| 9249 <li><b>"<code>r</code>": </b> read mode (the default);</li> | |
| 9250 <li><b>"<code>w</code>": </b> write mode;</li> | |
| 9251 <li><b>"<code>a</code>": </b> append mode;</li> | |
| 9252 <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li> | |
| 9253 <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li> | |
| 9254 <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved, | |
| 9255 writing is only allowed at the end of file.</li> | |
| 9256 </ul><p> | |
| 9257 The <code>mode</code> string can also have a '<code>b</code>' at the end, | |
| 9258 which is needed in some systems to open the file in binary mode. | |
| 9259 | |
| 9260 | |
| 9261 | |
| 9262 | |
| 9263 <p> | |
| 9264 <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3> | |
| 9265 | |
| 9266 | |
| 9267 <p> | |
| 9268 Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file. | |
| 9269 | |
| 9270 | |
| 9271 | |
| 9272 | |
| 9273 <p> | |
| 9274 <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3> | |
| 9275 | |
| 9276 | |
| 9277 <p> | |
| 9278 This function is system dependent and is not available | |
| 9279 on all platforms. | |
| 9280 | |
| 9281 | |
| 9282 <p> | |
| 9283 Starts program <code>prog</code> in a separated process and returns | |
| 9284 a file handle that you can use to read data from this program | |
| 9285 (if <code>mode</code> is <code>"r"</code>, the default) | |
| 9286 or to write data to this program | |
| 9287 (if <code>mode</code> is <code>"w"</code>). | |
| 9288 | |
| 9289 | |
| 9290 | |
| 9291 | |
| 9292 <p> | |
| 9293 <hr><h3><a name="pdf-io.read"><code>io.read (···)</code></a></h3> | |
| 9294 | |
| 9295 | |
| 9296 <p> | |
| 9297 Equivalent to <code>io.input():read(···)</code>. | |
| 9298 | |
| 9299 | |
| 9300 | |
| 9301 | |
| 9302 <p> | |
| 9303 <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3> | |
| 9304 | |
| 9305 | |
| 9306 <p> | |
| 9307 Returns a handle for a temporary file. | |
| 9308 This file is opened in update mode | |
| 9309 and it is automatically removed when the program ends. | |
| 9310 | |
| 9311 | |
| 9312 | |
| 9313 | |
| 9314 <p> | |
| 9315 <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3> | |
| 9316 | |
| 9317 | |
| 9318 <p> | |
| 9319 Checks whether <code>obj</code> is a valid file handle. | |
| 9320 Returns the string <code>"file"</code> if <code>obj</code> is an open file handle, | |
| 9321 <code>"closed file"</code> if <code>obj</code> is a closed file handle, | |
| 9322 or <b>nil</b> if <code>obj</code> is not a file handle. | |
| 9323 | |
| 9324 | |
| 9325 | |
| 9326 | |
| 9327 <p> | |
| 9328 <hr><h3><a name="pdf-io.write"><code>io.write (···)</code></a></h3> | |
| 9329 | |
| 9330 | |
| 9331 <p> | |
| 9332 Equivalent to <code>io.output():write(···)</code>. | |
| 9333 | |
| 9334 | |
| 9335 | |
| 9336 | |
| 9337 <p> | |
| 9338 <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3> | |
| 9339 | |
| 9340 | |
| 9341 <p> | |
| 9342 Closes <code>file</code>. | |
| 9343 Note that files are automatically closed when | |
| 9344 their handles are garbage collected, | |
| 9345 but that takes an unpredictable amount of time to happen. | |
| 9346 | |
| 9347 | |
| 9348 <p> | |
| 9349 When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>, | |
| 9350 <a href="#pdf-file:close"><code>file:close</code></a> returns the same values | |
| 9351 returned by <a href="#pdf-os.execute"><code>os.execute</code></a>. | |
| 9352 | |
| 9353 | |
| 9354 | |
| 9355 | |
| 9356 <p> | |
| 9357 <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3> | |
| 9358 | |
| 9359 | |
| 9360 <p> | |
| 9361 Saves any written data to <code>file</code>. | |
| 9362 | |
| 9363 | |
| 9364 | |
| 9365 | |
| 9366 <p> | |
| 9367 <hr><h3><a name="pdf-file:lines"><code>file:lines (···)</code></a></h3> | |
| 9368 | |
| 9369 | |
| 9370 <p> | |
| 9371 Returns an iterator function that, | |
| 9372 each time it is called, | |
| 9373 reads the file according to the given formats. | |
| 9374 When no format is given, | |
| 9375 uses "*l" as a default. | |
| 9376 As an example, the construction | |
| 9377 | |
| 9378 <pre> | |
| 9379 for c in file:lines(1) do <em>body</em> end | |
| 9380 </pre><p> | |
| 9381 will iterate over all characters of the file, | |
| 9382 starting at the current position. | |
| 9383 Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file | |
| 9384 when the loop ends. | |
| 9385 | |
| 9386 | |
| 9387 <p> | |
| 9388 In case of errors this function raises the error, | |
| 9389 instead of returning an error code. | |
| 9390 | |
| 9391 | |
| 9392 | |
| 9393 | |
| 9394 <p> | |
| 9395 <hr><h3><a name="pdf-file:read"><code>file:read (···)</code></a></h3> | |
| 9396 | |
| 9397 | |
| 9398 <p> | |
| 9399 Reads the file <code>file</code>, | |
| 9400 according to the given formats, which specify what to read. | |
| 9401 For each format, | |
| 9402 the function returns a string (or a number) with the characters read, | |
| 9403 or <b>nil</b> if it cannot read data with the specified format. | |
| 9404 When called without formats, | |
| 9405 it uses a default format that reads the next line | |
| 9406 (see below). | |
| 9407 | |
| 9408 | |
| 9409 <p> | |
| 9410 The available formats are | |
| 9411 | |
| 9412 <ul> | |
| 9413 | |
| 9414 <li><b>"<code>*n</code>": </b> | |
| 9415 reads a number; | |
| 9416 this is the only format that returns a number instead of a string. | |
| 9417 </li> | |
| 9418 | |
| 9419 <li><b>"<code>*a</code>": </b> | |
| 9420 reads the whole file, starting at the current position. | |
| 9421 On end of file, it returns the empty string. | |
| 9422 </li> | |
| 9423 | |
| 9424 <li><b>"<code>*l</code>": </b> | |
| 9425 reads the next line skipping the end of line, | |
| 9426 returning <b>nil</b> on end of file. | |
| 9427 This is the default format. | |
| 9428 </li> | |
| 9429 | |
| 9430 <li><b>"<code>*L</code>": </b> | |
| 9431 reads the next line keeping the end of line (if present), | |
| 9432 returning <b>nil</b> on end of file. | |
| 9433 </li> | |
| 9434 | |
| 9435 <li><b><em>number</em>: </b> | |
| 9436 reads a string with up to this number of bytes, | |
| 9437 returning <b>nil</b> on end of file. | |
| 9438 If number is zero, | |
| 9439 it reads nothing and returns an empty string, | |
| 9440 or <b>nil</b> on end of file. | |
| 9441 </li> | |
| 9442 | |
| 9443 </ul> | |
| 9444 | |
| 9445 | |
| 9446 | |
| 9447 <p> | |
| 9448 <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3> | |
| 9449 | |
| 9450 | |
| 9451 <p> | |
| 9452 Sets and gets the file position, | |
| 9453 measured from the beginning of the file, | |
| 9454 to the position given by <code>offset</code> plus a base | |
| 9455 specified by the string <code>whence</code>, as follows: | |
| 9456 | |
| 9457 <ul> | |
| 9458 <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li> | |
| 9459 <li><b>"<code>cur</code>": </b> base is current position;</li> | |
| 9460 <li><b>"<code>end</code>": </b> base is end of file;</li> | |
| 9461 </ul><p> | |
| 9462 In case of success, <code>seek</code> returns the final file position, | |
| 9463 measured in bytes from the beginning of the file. | |
| 9464 If <code>seek</code> fails, it returns <b>nil</b>, | |
| 9465 plus a string describing the error. | |
| 9466 | |
| 9467 | |
| 9468 <p> | |
| 9469 The default value for <code>whence</code> is <code>"cur"</code>, | |
| 9470 and for <code>offset</code> is 0. | |
| 9471 Therefore, the call <code>file:seek()</code> returns the current | |
| 9472 file position, without changing it; | |
| 9473 the call <code>file:seek("set")</code> sets the position to the | |
| 9474 beginning of the file (and returns 0); | |
| 9475 and the call <code>file:seek("end")</code> sets the position to the | |
| 9476 end of the file, and returns its size. | |
| 9477 | |
| 9478 | |
| 9479 | |
| 9480 | |
| 9481 <p> | |
| 9482 <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3> | |
| 9483 | |
| 9484 | |
| 9485 <p> | |
| 9486 Sets the buffering mode for an output file. | |
| 9487 There are three available modes: | |
| 9488 | |
| 9489 <ul> | |
| 9490 | |
| 9491 <li><b>"<code>no</code>": </b> | |
| 9492 no buffering; the result of any output operation appears immediately. | |
| 9493 </li> | |
| 9494 | |
| 9495 <li><b>"<code>full</code>": </b> | |
| 9496 full buffering; output operation is performed only | |
| 9497 when the buffer is full or when | |
| 9498 you explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>). | |
| 9499 </li> | |
| 9500 | |
| 9501 <li><b>"<code>line</code>": </b> | |
| 9502 line buffering; output is buffered until a newline is output | |
| 9503 or there is any input from some special files | |
| 9504 (such as a terminal device). | |
| 9505 </li> | |
| 9506 | |
| 9507 </ul><p> | |
| 9508 For the last two cases, <code>size</code> | |
| 9509 specifies the size of the buffer, in bytes. | |
| 9510 The default is an appropriate size. | |
| 9511 | |
| 9512 | |
| 9513 | |
| 9514 | |
| 9515 <p> | |
| 9516 <hr><h3><a name="pdf-file:write"><code>file:write (···)</code></a></h3> | |
| 9517 | |
| 9518 | |
| 9519 <p> | |
| 9520 Writes the value of each of its arguments to <code>file</code>. | |
| 9521 The arguments must be strings or numbers. | |
| 9522 | |
| 9523 | |
| 9524 <p> | |
| 9525 In case of success, this function returns <code>file</code>. | |
| 9526 Otherwise it returns <b>nil</b> plus a string describing the error. | |
| 9527 | |
| 9528 | |
| 9529 | |
| 9530 | |
| 9531 | |
| 9532 | |
| 9533 | |
| 9534 <h2>6.9 – <a name="6.9">Operating System Facilities</a></h2> | |
| 9535 | |
| 9536 <p> | |
| 9537 This library is implemented through table <a name="pdf-os"><code>os</code></a>. | |
| 9538 | |
| 9539 | |
| 9540 <p> | |
| 9541 <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3> | |
| 9542 | |
| 9543 | |
| 9544 <p> | |
| 9545 Returns an approximation of the amount in seconds of CPU time | |
| 9546 used by the program. | |
| 9547 | |
| 9548 | |
| 9549 | |
| 9550 | |
| 9551 <p> | |
| 9552 <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3> | |
| 9553 | |
| 9554 | |
| 9555 <p> | |
| 9556 Returns a string or a table containing date and time, | |
| 9557 formatted according to the given string <code>format</code>. | |
| 9558 | |
| 9559 | |
| 9560 <p> | |
| 9561 If the <code>time</code> argument is present, | |
| 9562 this is the time to be formatted | |
| 9563 (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value). | |
| 9564 Otherwise, <code>date</code> formats the current time. | |
| 9565 | |
| 9566 | |
| 9567 <p> | |
| 9568 If <code>format</code> starts with '<code>!</code>', | |
| 9569 then the date is formatted in Coordinated Universal Time. | |
| 9570 After this optional character, | |
| 9571 if <code>format</code> is the string "<code>*t</code>", | |
| 9572 then <code>date</code> returns a table with the following fields: | |
| 9573 <code>year</code> (four digits), <code>month</code> (1–12), <code>day</code> (1–31), | |
| 9574 <code>hour</code> (0–23), <code>min</code> (0–59), <code>sec</code> (0–61), | |
| 9575 <code>wday</code> (weekday, Sunday is 1), | |
| 9576 <code>yday</code> (day of the year), | |
| 9577 and <code>isdst</code> (daylight saving flag, a boolean). | |
| 9578 This last field may be absent | |
| 9579 if the information is not available. | |
| 9580 | |
| 9581 | |
| 9582 <p> | |
| 9583 If <code>format</code> is not "<code>*t</code>", | |
| 9584 then <code>date</code> returns the date as a string, | |
| 9585 formatted according to the same rules as the ANSI C function <code>strftime</code>. | |
| 9586 | |
| 9587 | |
| 9588 <p> | |
| 9589 When called without arguments, | |
| 9590 <code>date</code> returns a reasonable date and time representation that depends on | |
| 9591 the host system and on the current locale | |
| 9592 (that is, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>). | |
| 9593 | |
| 9594 | |
| 9595 <p> | |
| 9596 On non-Posix systems, | |
| 9597 this function may be not thread safe | |
| 9598 because of its reliance on C function <code>gmtime</code> and C function <code>localtime</code>. | |
| 9599 | |
| 9600 | |
| 9601 | |
| 9602 | |
| 9603 <p> | |
| 9604 <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3> | |
| 9605 | |
| 9606 | |
| 9607 <p> | |
| 9608 Returns the number of seconds from time <code>t1</code> to time <code>t2</code>. | |
| 9609 In POSIX, Windows, and some other systems, | |
| 9610 this value is exactly <code>t2</code><em>-</em><code>t1</code>. | |
| 9611 | |
| 9612 | |
| 9613 | |
| 9614 | |
| 9615 <p> | |
| 9616 <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3> | |
| 9617 | |
| 9618 | |
| 9619 <p> | |
| 9620 This function is equivalent to the ANSI C function <code>system</code>. | |
| 9621 It passes <code>command</code> to be executed by an operating system shell. | |
| 9622 Its first result is <b>true</b> | |
| 9623 if the command terminated successfully, | |
| 9624 or <b>nil</b> otherwise. | |
| 9625 After this first result | |
| 9626 the function returns a string and a number, | |
| 9627 as follows: | |
| 9628 | |
| 9629 <ul> | |
| 9630 | |
| 9631 <li><b>"<code>exit</code>": </b> | |
| 9632 the command terminated normally; | |
| 9633 the following number is the exit status of the command. | |
| 9634 </li> | |
| 9635 | |
| 9636 <li><b>"<code>signal</code>": </b> | |
| 9637 the command was terminated by a signal; | |
| 9638 the following number is the signal that terminated the command. | |
| 9639 </li> | |
| 9640 | |
| 9641 </ul> | |
| 9642 | |
| 9643 <p> | |
| 9644 When called without a <code>command</code>, | |
| 9645 <code>os.execute</code> returns a boolean that is true if a shell is available. | |
| 9646 | |
| 9647 | |
| 9648 | |
| 9649 | |
| 9650 <p> | |
| 9651 <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close])</code></a></h3> | |
| 9652 | |
| 9653 | |
| 9654 <p> | |
| 9655 Calls the ANSI C function <code>exit</code> to terminate the host program. | |
| 9656 If <code>code</code> is <b>true</b>, | |
| 9657 the returned status is <code>EXIT_SUCCESS</code>; | |
| 9658 if <code>code</code> is <b>false</b>, | |
| 9659 the returned status is <code>EXIT_FAILURE</code>; | |
| 9660 if <code>code</code> is a number, | |
| 9661 the returned status is this number. | |
| 9662 The default value for <code>code</code> is <b>true</b>. | |
| 9663 | |
| 9664 | |
| 9665 <p> | |
| 9666 If the optional second argument <code>close</code> is true, | |
| 9667 closes the Lua state before exiting. | |
| 9668 | |
| 9669 | |
| 9670 | |
| 9671 | |
| 9672 <p> | |
| 9673 <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3> | |
| 9674 | |
| 9675 | |
| 9676 <p> | |
| 9677 Returns the value of the process environment variable <code>varname</code>, | |
| 9678 or <b>nil</b> if the variable is not defined. | |
| 9679 | |
| 9680 | |
| 9681 | |
| 9682 | |
| 9683 <p> | |
| 9684 <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3> | |
| 9685 | |
| 9686 | |
| 9687 <p> | |
| 9688 Deletes the file (or empty directory, on POSIX systems) | |
| 9689 with the given name. | |
| 9690 If this function fails, it returns <b>nil</b>, | |
| 9691 plus a string describing the error and the error code. | |
| 9692 | |
| 9693 | |
| 9694 | |
| 9695 | |
| 9696 <p> | |
| 9697 <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3> | |
| 9698 | |
| 9699 | |
| 9700 <p> | |
| 9701 Renames file or directory named <code>oldname</code> to <code>newname</code>. | |
| 9702 If this function fails, it returns <b>nil</b>, | |
| 9703 plus a string describing the error and the error code. | |
| 9704 | |
| 9705 | |
| 9706 | |
| 9707 | |
| 9708 <p> | |
| 9709 <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3> | |
| 9710 | |
| 9711 | |
| 9712 <p> | |
| 9713 Sets the current locale of the program. | |
| 9714 <code>locale</code> is a system-dependent string specifying a locale; | |
| 9715 <code>category</code> is an optional string describing which category to change: | |
| 9716 <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>, | |
| 9717 <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>; | |
| 9718 the default category is <code>"all"</code>. | |
| 9719 The function returns the name of the new locale, | |
| 9720 or <b>nil</b> if the request cannot be honored. | |
| 9721 | |
| 9722 | |
| 9723 <p> | |
| 9724 If <code>locale</code> is the empty string, | |
| 9725 the current locale is set to an implementation-defined native locale. | |
| 9726 If <code>locale</code> is the string "<code>C</code>", | |
| 9727 the current locale is set to the standard C locale. | |
| 9728 | |
| 9729 | |
| 9730 <p> | |
| 9731 When called with <b>nil</b> as the first argument, | |
| 9732 this function only returns the name of the current locale | |
| 9733 for the given category. | |
| 9734 | |
| 9735 | |
| 9736 <p> | |
| 9737 This function may be not thread safe | |
| 9738 because of its reliance on C function <code>setlocale</code>. | |
| 9739 | |
| 9740 | |
| 9741 | |
| 9742 | |
| 9743 <p> | |
| 9744 <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3> | |
| 9745 | |
| 9746 | |
| 9747 <p> | |
| 9748 Returns the current time when called without arguments, | |
| 9749 or a time representing the date and time specified by the given table. | |
| 9750 This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>, | |
| 9751 and may have fields | |
| 9752 <code>hour</code> (default is 12), | |
| 9753 <code>min</code> (default is 0), | |
| 9754 <code>sec</code> (default is 0), | |
| 9755 and <code>isdst</code> (default is <b>nil</b>). | |
| 9756 For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function. | |
| 9757 | |
| 9758 | |
| 9759 <p> | |
| 9760 The returned value is a number, whose meaning depends on your system. | |
| 9761 In POSIX, Windows, and some other systems, | |
| 9762 this number counts the number | |
| 9763 of seconds since some given start time (the "epoch"). | |
| 9764 In other systems, the meaning is not specified, | |
| 9765 and the number returned by <code>time</code> can be used only as an argument to | |
| 9766 <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>. | |
| 9767 | |
| 9768 | |
| 9769 | |
| 9770 | |
| 9771 <p> | |
| 9772 <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3> | |
| 9773 | |
| 9774 | |
| 9775 <p> | |
| 9776 Returns a string with a file name that can | |
| 9777 be used for a temporary file. | |
| 9778 The file must be explicitly opened before its use | |
| 9779 and explicitly removed when no longer needed. | |
| 9780 | |
| 9781 | |
| 9782 <p> | |
| 9783 On POSIX systems, | |
| 9784 this function also creates a file with that name, | |
| 9785 to avoid security risks. | |
| 9786 (Someone else might create the file with wrong permissions | |
| 9787 in the time between getting the name and creating the file.) | |
| 9788 You still have to open the file to use it | |
| 9789 and to remove it (even if you do not use it). | |
| 9790 | |
| 9791 | |
| 9792 <p> | |
| 9793 When possible, | |
| 9794 you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>, | |
| 9795 which automatically removes the file when the program ends. | |
| 9796 | |
| 9797 | |
| 9798 | |
| 9799 | |
| 9800 | |
| 9801 | |
| 9802 | |
| 9803 <h2>6.10 – <a name="6.10">The Debug Library</a></h2> | |
| 9804 | |
| 9805 <p> | |
| 9806 This library provides | |
| 9807 the functionality of the debug interface (<a href="#4.9">§4.9</a>) to Lua programs. | |
| 9808 You should exert care when using this library. | |
| 9809 Several of its functions | |
| 9810 violate basic assumptions about Lua code | |
| 9811 (e.g., that variables local to a function | |
| 9812 cannot be accessed from outside; | |
| 9813 that userdata metatables cannot be changed by Lua code; | |
| 9814 that Lua programs do not crash) | |
| 9815 and therefore can compromise otherwise secure code. | |
| 9816 Moreover, some functions in this library may be slow. | |
| 9817 | |
| 9818 | |
| 9819 <p> | |
| 9820 All functions in this library are provided | |
| 9821 inside the <a name="pdf-debug"><code>debug</code></a> table. | |
| 9822 All functions that operate over a thread | |
| 9823 have an optional first argument which is the | |
| 9824 thread to operate over. | |
| 9825 The default is always the current thread. | |
| 9826 | |
| 9827 | |
| 9828 <p> | |
| 9829 <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3> | |
| 9830 | |
| 9831 | |
| 9832 <p> | |
| 9833 Enters an interactive mode with the user, | |
| 9834 running each string that the user enters. | |
| 9835 Using simple commands and other debug facilities, | |
| 9836 the user can inspect global and local variables, | |
| 9837 change their values, evaluate expressions, and so on. | |
| 9838 A line containing only the word <code>cont</code> finishes this function, | |
| 9839 so that the caller continues its execution. | |
| 9840 | |
| 9841 | |
| 9842 <p> | |
| 9843 Note that commands for <code>debug.debug</code> are not lexically nested | |
| 9844 within any function and so have no direct access to local variables. | |
| 9845 | |
| 9846 | |
| 9847 | |
| 9848 | |
| 9849 <p> | |
| 9850 <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3> | |
| 9851 | |
| 9852 | |
| 9853 <p> | |
| 9854 Returns the current hook settings of the thread, as three values: | |
| 9855 the current hook function, the current hook mask, | |
| 9856 and the current hook count | |
| 9857 (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function). | |
| 9858 | |
| 9859 | |
| 9860 | |
| 9861 | |
| 9862 <p> | |
| 9863 <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3> | |
| 9864 | |
| 9865 | |
| 9866 <p> | |
| 9867 Returns a table with information about a function. | |
| 9868 You can give the function directly | |
| 9869 or you can give a number as the value of <code>f</code>, | |
| 9870 which means the function running at level <code>f</code> of the call stack | |
| 9871 of the given thread: | |
| 9872 level 0 is the current function (<code>getinfo</code> itself); | |
| 9873 level 1 is the function that called <code>getinfo</code> | |
| 9874 (except for tail calls, which do not count on the stack); | |
| 9875 and so on. | |
| 9876 If <code>f</code> is a number larger than the number of active functions, | |
| 9877 then <code>getinfo</code> returns <b>nil</b>. | |
| 9878 | |
| 9879 | |
| 9880 <p> | |
| 9881 The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>, | |
| 9882 with the string <code>what</code> describing which fields to fill in. | |
| 9883 The default for <code>what</code> is to get all information available, | |
| 9884 except the table of valid lines. | |
| 9885 If present, | |
| 9886 the option '<code>f</code>' | |
| 9887 adds a field named <code>func</code> with the function itself. | |
| 9888 If present, | |
| 9889 the option '<code>L</code>' | |
| 9890 adds a field named <code>activelines</code> with the table of | |
| 9891 valid lines. | |
| 9892 | |
| 9893 | |
| 9894 <p> | |
| 9895 For instance, the expression <code>debug.getinfo(1,"n").name</code> returns | |
| 9896 a table with a name for the current function, | |
| 9897 if a reasonable name can be found, | |
| 9898 and the expression <code>debug.getinfo(print)</code> | |
| 9899 returns a table with all available information | |
| 9900 about the <a href="#pdf-print"><code>print</code></a> function. | |
| 9901 | |
| 9902 | |
| 9903 | |
| 9904 | |
| 9905 <p> | |
| 9906 <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3> | |
| 9907 | |
| 9908 | |
| 9909 <p> | |
| 9910 This function returns the name and the value of the local variable | |
| 9911 with index <code>local</code> of the function at level <code>f</code> of the stack. | |
| 9912 This function accesses not only explicit local variables, | |
| 9913 but also parameters, temporaries, etc. | |
| 9914 | |
| 9915 | |
| 9916 <p> | |
| 9917 The first parameter or local variable has index 1, and so on, | |
| 9918 until the last active variable. | |
| 9919 Negative indices refer to vararg parameters; | |
| 9920 -1 is the first vararg parameter. | |
| 9921 The function returns <b>nil</b> if there is no variable with the given index, | |
| 9922 and raises an error when called with a level out of range. | |
| 9923 (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.) | |
| 9924 | |
| 9925 | |
| 9926 <p> | |
| 9927 Variable names starting with '<code>(</code>' (open parenthesis) | |
| 9928 represent internal variables | |
| 9929 (loop control variables, temporaries, varargs, and C function locals). | |
| 9930 | |
| 9931 | |
| 9932 <p> | |
| 9933 The parameter <code>f</code> may also be a function. | |
| 9934 In that case, <code>getlocal</code> returns only the name of function parameters. | |
| 9935 | |
| 9936 | |
| 9937 | |
| 9938 | |
| 9939 <p> | |
| 9940 <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3> | |
| 9941 | |
| 9942 | |
| 9943 <p> | |
| 9944 Returns the metatable of the given <code>value</code> | |
| 9945 or <b>nil</b> if it does not have a metatable. | |
| 9946 | |
| 9947 | |
| 9948 | |
| 9949 | |
| 9950 <p> | |
| 9951 <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3> | |
| 9952 | |
| 9953 | |
| 9954 <p> | |
| 9955 Returns the registry table (see <a href="#4.5">§4.5</a>). | |
| 9956 | |
| 9957 | |
| 9958 | |
| 9959 | |
| 9960 <p> | |
| 9961 <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3> | |
| 9962 | |
| 9963 | |
| 9964 <p> | |
| 9965 This function returns the name and the value of the upvalue | |
| 9966 with index <code>up</code> of the function <code>f</code>. | |
| 9967 The function returns <b>nil</b> if there is no upvalue with the given index. | |
| 9968 | |
| 9969 | |
| 9970 | |
| 9971 | |
| 9972 <p> | |
| 9973 <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3> | |
| 9974 | |
| 9975 | |
| 9976 <p> | |
| 9977 Returns the Lua value associated to <code>u</code>. | |
| 9978 If <code>u</code> is not a userdata, | |
| 9979 returns <b>nil</b>. | |
| 9980 | |
| 9981 | |
| 9982 | |
| 9983 | |
| 9984 <p> | |
| 9985 <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3> | |
| 9986 | |
| 9987 | |
| 9988 <p> | |
| 9989 Sets the given function as a hook. | |
| 9990 The string <code>mask</code> and the number <code>count</code> describe | |
| 9991 when the hook will be called. | |
| 9992 The string mask may have the following characters, | |
| 9993 with the given meaning: | |
| 9994 | |
| 9995 <ul> | |
| 9996 <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li> | |
| 9997 <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li> | |
| 9998 <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li> | |
| 9999 </ul><p> | |
| 10000 With a <code>count</code> different from zero, | |
| 10001 the hook is called after every <code>count</code> instructions. | |
| 10002 | |
| 10003 | |
| 10004 <p> | |
| 10005 When called without arguments, | |
| 10006 <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook. | |
| 10007 | |
| 10008 | |
| 10009 <p> | |
| 10010 When the hook is called, its first parameter is a string | |
| 10011 describing the event that has triggered its call: | |
| 10012 <code>"call"</code> (or <code>"tail call"</code>), | |
| 10013 <code>"return"</code>, | |
| 10014 <code>"line"</code>, and <code>"count"</code>. | |
| 10015 For line events, | |
| 10016 the hook also gets the new line number as its second parameter. | |
| 10017 Inside a hook, | |
| 10018 you can call <code>getinfo</code> with level 2 to get more information about | |
| 10019 the running function | |
| 10020 (level 0 is the <code>getinfo</code> function, | |
| 10021 and level 1 is the hook function). | |
| 10022 | |
| 10023 | |
| 10024 | |
| 10025 | |
| 10026 <p> | |
| 10027 <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3> | |
| 10028 | |
| 10029 | |
| 10030 <p> | |
| 10031 This function assigns the value <code>value</code> to the local variable | |
| 10032 with index <code>local</code> of the function at level <code>level</code> of the stack. | |
| 10033 The function returns <b>nil</b> if there is no local | |
| 10034 variable with the given index, | |
| 10035 and raises an error when called with a <code>level</code> out of range. | |
| 10036 (You can call <code>getinfo</code> to check whether the level is valid.) | |
| 10037 Otherwise, it returns the name of the local variable. | |
| 10038 | |
| 10039 | |
| 10040 <p> | |
| 10041 See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about | |
| 10042 variable indices and names. | |
| 10043 | |
| 10044 | |
| 10045 | |
| 10046 | |
| 10047 <p> | |
| 10048 <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3> | |
| 10049 | |
| 10050 | |
| 10051 <p> | |
| 10052 Sets the metatable for the given <code>value</code> to the given <code>table</code> | |
| 10053 (which can be <b>nil</b>). | |
| 10054 Returns <code>value</code>. | |
| 10055 | |
| 10056 | |
| 10057 | |
| 10058 | |
| 10059 <p> | |
| 10060 <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3> | |
| 10061 | |
| 10062 | |
| 10063 <p> | |
| 10064 This function assigns the value <code>value</code> to the upvalue | |
| 10065 with index <code>up</code> of the function <code>f</code>. | |
| 10066 The function returns <b>nil</b> if there is no upvalue | |
| 10067 with the given index. | |
| 10068 Otherwise, it returns the name of the upvalue. | |
| 10069 | |
| 10070 | |
| 10071 | |
| 10072 | |
| 10073 <p> | |
| 10074 <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3> | |
| 10075 | |
| 10076 | |
| 10077 <p> | |
| 10078 Sets the given <code>value</code> as | |
| 10079 the Lua value associated to the given <code>udata</code>. | |
| 10080 <code>value</code> must be a table or <b>nil</b>; | |
| 10081 <code>udata</code> must be a full userdata. | |
| 10082 | |
| 10083 | |
| 10084 <p> | |
| 10085 Returns <code>udata</code>. | |
| 10086 | |
| 10087 | |
| 10088 | |
| 10089 | |
| 10090 <p> | |
| 10091 <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3> | |
| 10092 | |
| 10093 | |
| 10094 <p> | |
| 10095 If <code>message</code> is present but is neither a string nor <b>nil</b>, | |
| 10096 this function returns <code>message</code> without further processing. | |
| 10097 Otherwise, | |
| 10098 it returns a string with a traceback of the call stack. | |
| 10099 An optional <code>message</code> string is appended | |
| 10100 at the beginning of the traceback. | |
| 10101 An optional <code>level</code> number tells at which level | |
| 10102 to start the traceback | |
| 10103 (default is 1, the function calling <code>traceback</code>). | |
| 10104 | |
| 10105 | |
| 10106 | |
| 10107 | |
| 10108 <p> | |
| 10109 <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3> | |
| 10110 | |
| 10111 | |
| 10112 <p> | |
| 10113 Returns an unique identifier (as a light userdata) | |
| 10114 for the upvalue numbered <code>n</code> | |
| 10115 from the given function. | |
| 10116 | |
| 10117 | |
| 10118 <p> | |
| 10119 These unique identifiers allow a program to check whether different | |
| 10120 closures share upvalues. | |
| 10121 Lua closures that share an upvalue | |
| 10122 (that is, that access a same external local variable) | |
| 10123 will return identical ids for those upvalue indices. | |
| 10124 | |
| 10125 | |
| 10126 | |
| 10127 | |
| 10128 <p> | |
| 10129 <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3> | |
| 10130 | |
| 10131 | |
| 10132 <p> | |
| 10133 Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code> | |
| 10134 refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>. | |
| 10135 | |
| 10136 | |
| 10137 | |
| 10138 | |
| 10139 | |
| 10140 | |
| 10141 | |
| 10142 <h1>7 – <a name="7">Lua Standalone</a></h1> | |
| 10143 | |
| 10144 <p> | |
| 10145 Although Lua has been designed as an extension language, | |
| 10146 to be embedded in a host C program, | |
| 10147 it is also frequently used as a standalone language. | |
| 10148 An interpreter for Lua as a standalone language, | |
| 10149 called simply <code>lua</code>, | |
| 10150 is provided with the standard distribution. | |
| 10151 The standalone interpreter includes | |
| 10152 all standard libraries, including the debug library. | |
| 10153 Its usage is: | |
| 10154 | |
| 10155 <pre> | |
| 10156 lua [options] [script [args]] | |
| 10157 </pre><p> | |
| 10158 The options are: | |
| 10159 | |
| 10160 <ul> | |
| 10161 <li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li> | |
| 10162 <li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em>;</li> | |
| 10163 <li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li> | |
| 10164 <li><b><code>-v</code>: </b> prints version information;</li> | |
| 10165 <li><b><code>-E</code>: </b> ignores environment variables;</li> | |
| 10166 <li><b><code>--</code>: </b> stops handling options;</li> | |
| 10167 <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li> | |
| 10168 </ul><p> | |
| 10169 After handling its options, <code>lua</code> runs the given <em>script</em>, | |
| 10170 passing to it the given <em>args</em> as string arguments. | |
| 10171 When called without arguments, | |
| 10172 <code>lua</code> behaves as <code>lua -v -i</code> | |
| 10173 when the standard input (<code>stdin</code>) is a terminal, | |
| 10174 and as <code>lua -</code> otherwise. | |
| 10175 | |
| 10176 | |
| 10177 <p> | |
| 10178 When called without option <code>-E</code>, | |
| 10179 the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a> | |
| 10180 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined) | |
| 10181 before running any argument. | |
| 10182 If the variable content has the format <code>@<em>filename</em></code>, | |
| 10183 then <code>lua</code> executes the file. | |
| 10184 Otherwise, <code>lua</code> executes the string itself. | |
| 10185 | |
| 10186 | |
| 10187 <p> | |
| 10188 When called with option <code>-E</code>, | |
| 10189 besides ignoring <code>LUA_INIT</code>, | |
| 10190 Lua also ignores | |
| 10191 the values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>, | |
| 10192 setting the values of | |
| 10193 <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a> | |
| 10194 with the default paths defined in <code>luaconf.h</code>. | |
| 10195 | |
| 10196 | |
| 10197 <p> | |
| 10198 All options are handled in order, except <code>-i</code> and <code>-E</code>. | |
| 10199 For instance, an invocation like | |
| 10200 | |
| 10201 <pre> | |
| 10202 $ lua -e'a=1' -e 'print(a)' script.lua | |
| 10203 </pre><p> | |
| 10204 will first set <code>a</code> to 1, then print the value of <code>a</code>, | |
| 10205 and finally run the file <code>script.lua</code> with no arguments. | |
| 10206 (Here <code>$</code> is the shell prompt. Your prompt may be different.) | |
| 10207 | |
| 10208 | |
| 10209 <p> | |
| 10210 Before starting to run the script, | |
| 10211 <code>lua</code> collects all arguments in the command line | |
| 10212 in a global table called <code>arg</code>. | |
| 10213 The script name is stored at index 0, | |
| 10214 the first argument after the script name goes to index 1, | |
| 10215 and so on. | |
| 10216 Any arguments before the script name | |
| 10217 (that is, the interpreter name plus the options) | |
| 10218 go to negative indices. | |
| 10219 For instance, in the call | |
| 10220 | |
| 10221 <pre> | |
| 10222 $ lua -la b.lua t1 t2 | |
| 10223 </pre><p> | |
| 10224 the interpreter first runs the file <code>a.lua</code>, | |
| 10225 then creates a table | |
| 10226 | |
| 10227 <pre> | |
| 10228 arg = { [-2] = "lua", [-1] = "-la", | |
| 10229 [0] = "b.lua", | |
| 10230 [1] = "t1", [2] = "t2" } | |
| 10231 </pre><p> | |
| 10232 and finally runs the file <code>b.lua</code>. | |
| 10233 The script is called with <code>arg[1]</code>, <code>arg[2]</code>, ... | |
| 10234 as arguments; | |
| 10235 it can also access these arguments with the vararg expression '<code>...</code>'. | |
| 10236 | |
| 10237 | |
| 10238 <p> | |
| 10239 In interactive mode, | |
| 10240 if you write an incomplete statement, | |
| 10241 the interpreter waits for its completion | |
| 10242 by issuing a different prompt. | |
| 10243 | |
| 10244 | |
| 10245 <p> | |
| 10246 In case of unprotected errors in the script, | |
| 10247 the interpreter reports the error to the standard error stream. | |
| 10248 If the error object is a string, | |
| 10249 the interpreter adds a stack traceback to it. | |
| 10250 Otherwise, if the error object has a metamethod <code>__tostring</code>, | |
| 10251 the interpreter calls this metamethod to produce the final message. | |
| 10252 Finally, if the error object is <b>nil</b>, | |
| 10253 the interpreter does not report the error. | |
| 10254 | |
| 10255 | |
| 10256 <p> | |
| 10257 When finishing normally, | |
| 10258 the interpreter closes its main Lua state | |
| 10259 (see <a href="#lua_close"><code>lua_close</code></a>). | |
| 10260 The script can avoid this step by | |
| 10261 calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate. | |
| 10262 | |
| 10263 | |
| 10264 <p> | |
| 10265 To allow the use of Lua as a | |
| 10266 script interpreter in Unix systems, | |
| 10267 the standalone interpreter skips | |
| 10268 the first line of a chunk if it starts with <code>#</code>. | |
| 10269 Therefore, Lua scripts can be made into executable programs | |
| 10270 by using <code>chmod +x</code> and the <code>#!</code> form, | |
| 10271 as in | |
| 10272 | |
| 10273 <pre> | |
| 10274 #!/usr/local/bin/lua | |
| 10275 </pre><p> | |
| 10276 (Of course, | |
| 10277 the location of the Lua interpreter may be different in your machine. | |
| 10278 If <code>lua</code> is in your <code>PATH</code>, | |
| 10279 then | |
| 10280 | |
| 10281 <pre> | |
| 10282 #!/usr/bin/env lua | |
| 10283 </pre><p> | |
| 10284 is a more portable solution.) | |
| 10285 | |
| 10286 | |
| 10287 | |
| 10288 <h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1> | |
| 10289 | |
| 10290 <p> | |
| 10291 Here we list the incompatibilities that you may find when moving a program | |
| 10292 from Lua 5.1 to Lua 5.2. | |
| 10293 You can avoid some incompatibilities by compiling Lua with | |
| 10294 appropriate options (see file <code>luaconf.h</code>). | |
| 10295 However, | |
| 10296 all these compatibility options will be removed in the next version of Lua. | |
| 10297 Similarly, | |
| 10298 all features marked as deprecated in Lua 5.1 | |
| 10299 have been removed in Lua 5.2. | |
| 10300 | |
| 10301 | |
| 10302 | |
| 10303 <h2>8.1 – <a name="8.1">Changes in the Language</a></h2> | |
| 10304 <ul> | |
| 10305 | |
| 10306 <li> | |
| 10307 The concept of <em>environment</em> changed. | |
| 10308 Only Lua functions have environments. | |
| 10309 To set the environment of a Lua function, | |
| 10310 use the variable <code>_ENV</code> or the function <a href="#pdf-load"><code>load</code></a>. | |
| 10311 | |
| 10312 | |
| 10313 <p> | |
| 10314 C functions no longer have environments. | |
| 10315 Use an upvalue with a shared table if you need to keep | |
| 10316 shared state among several C functions. | |
| 10317 (You may use <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> to open a C library | |
| 10318 with all functions sharing a common upvalue.) | |
| 10319 | |
| 10320 | |
| 10321 <p> | |
| 10322 To manipulate the "environment" of a userdata | |
| 10323 (which is now called user value), | |
| 10324 use the new functions | |
| 10325 <a href="#lua_getuservalue"><code>lua_getuservalue</code></a> and <a href="#lua_setuservalue"><code>lua_setuservalue</code></a>. | |
| 10326 </li> | |
| 10327 | |
| 10328 <li> | |
| 10329 Lua identifiers cannot use locale-dependent letters. | |
| 10330 </li> | |
| 10331 | |
| 10332 <li> | |
| 10333 Doing a step or a full collection in the garbage collector | |
| 10334 does not restart the collector if it has been stopped. | |
| 10335 </li> | |
| 10336 | |
| 10337 <li> | |
| 10338 Weak tables with weak keys now perform like <em>ephemeron tables</em>. | |
| 10339 </li> | |
| 10340 | |
| 10341 <li> | |
| 10342 The event <em>tail return</em> in debug hooks was removed. | |
| 10343 Instead, tail calls generate a special new event, | |
| 10344 <em>tail call</em>, so that the debugger can know that | |
| 10345 there will not be a corresponding return event. | |
| 10346 </li> | |
| 10347 | |
| 10348 <li> | |
| 10349 Equality between function values has changed. | |
| 10350 Now, a function definition may not create a new value; | |
| 10351 it may reuse some previous value if there is no | |
| 10352 observable difference to the new function. | |
| 10353 </li> | |
| 10354 | |
| 10355 </ul> | |
| 10356 | |
| 10357 | |
| 10358 | |
| 10359 | |
| 10360 <h2>8.2 – <a name="8.2">Changes in the Libraries</a></h2> | |
| 10361 <ul> | |
| 10362 | |
| 10363 <li> | |
| 10364 Function <code>module</code> is deprecated. | |
| 10365 It is easy to set up a module with regular Lua code. | |
| 10366 Modules are not expected to set global variables. | |
| 10367 </li> | |
| 10368 | |
| 10369 <li> | |
| 10370 Functions <code>setfenv</code> and <code>getfenv</code> were removed, | |
| 10371 because of the changes in environments. | |
| 10372 </li> | |
| 10373 | |
| 10374 <li> | |
| 10375 Function <code>math.log10</code> is deprecated. | |
| 10376 Use <a href="#pdf-math.log"><code>math.log</code></a> with 10 as its second argument, instead. | |
| 10377 </li> | |
| 10378 | |
| 10379 <li> | |
| 10380 Function <code>loadstring</code> is deprecated. | |
| 10381 Use <code>load</code> instead; it now accepts string arguments | |
| 10382 and are exactly equivalent to <code>loadstring</code>. | |
| 10383 </li> | |
| 10384 | |
| 10385 <li> | |
| 10386 Function <code>table.maxn</code> is deprecated. | |
| 10387 Write it in Lua if you really need it. | |
| 10388 </li> | |
| 10389 | |
| 10390 <li> | |
| 10391 Function <code>os.execute</code> now returns <b>true</b> when command | |
| 10392 terminates successfully and <b>nil</b> plus error information | |
| 10393 otherwise. | |
| 10394 </li> | |
| 10395 | |
| 10396 <li> | |
| 10397 Function <code>unpack</code> was moved into the table library | |
| 10398 and therefore must be called as <a href="#pdf-table.unpack"><code>table.unpack</code></a>. | |
| 10399 </li> | |
| 10400 | |
| 10401 <li> | |
| 10402 Character class <code>%z</code> in patterns is deprecated, | |
| 10403 as now patterns may contain '<code>\0</code>' as a regular character. | |
| 10404 </li> | |
| 10405 | |
| 10406 <li> | |
| 10407 The table <code>package.loaders</code> was renamed <code>package.searchers</code>. | |
| 10408 </li> | |
| 10409 | |
| 10410 <li> | |
| 10411 Lua does not have bytecode verification anymore. | |
| 10412 So, all functions that load code | |
| 10413 (<a href="#pdf-load"><code>load</code></a> and <a href="#pdf-loadfile"><code>loadfile</code></a>) | |
| 10414 are potentially insecure when loading untrusted binary data. | |
| 10415 (Actually, those functions were already insecure because | |
| 10416 of flaws in the verification algorithm.) | |
| 10417 When in doubt, | |
| 10418 use the <code>mode</code> argument of those functions | |
| 10419 to restrict them to loading textual chunks. | |
| 10420 </li> | |
| 10421 | |
| 10422 <li> | |
| 10423 The standard paths in the official distribution may | |
| 10424 change between versions. | |
| 10425 </li> | |
| 10426 | |
| 10427 </ul> | |
| 10428 | |
| 10429 | |
| 10430 | |
| 10431 | |
| 10432 <h2>8.3 – <a name="8.3">Changes in the API</a></h2> | |
| 10433 <ul> | |
| 10434 | |
| 10435 <li> | |
| 10436 Pseudoindex <code>LUA_GLOBALSINDEX</code> was removed. | |
| 10437 You must get the global environment from the registry | |
| 10438 (see <a href="#4.5">§4.5</a>). | |
| 10439 </li> | |
| 10440 | |
| 10441 <li> | |
| 10442 Pseudoindex <code>LUA_ENVIRONINDEX</code> | |
| 10443 and functions <code>lua_getfenv</code>/<code>lua_setfenv</code> | |
| 10444 were removed, | |
| 10445 as C functions no longer have environments. | |
| 10446 </li> | |
| 10447 | |
| 10448 <li> | |
| 10449 Function <code>luaL_register</code> is deprecated. | |
| 10450 Use <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a> so that your module does not create globals. | |
| 10451 (Modules are not expected to set global variables anymore.) | |
| 10452 </li> | |
| 10453 | |
| 10454 <li> | |
| 10455 The <code>osize</code> argument to the allocation function | |
| 10456 may not be zero when creating a new block, | |
| 10457 that is, when <code>ptr</code> is <code>NULL</code> | |
| 10458 (see <a href="#lua_Alloc"><code>lua_Alloc</code></a>). | |
| 10459 Use only the test <code>ptr == NULL</code> to check whether | |
| 10460 the block is new. | |
| 10461 </li> | |
| 10462 | |
| 10463 <li> | |
| 10464 Finalizers (<code>__gc</code> metamethods) for userdata are called in the | |
| 10465 reverse order that they were marked for finalization, | |
| 10466 not that they were created (see <a href="#2.5.1">§2.5.1</a>). | |
| 10467 (Most userdata are marked immediately after they are created.) | |
| 10468 Moreover, | |
| 10469 if the metatable does not have a <code>__gc</code> field when set, | |
| 10470 the finalizer will not be called, | |
| 10471 even if it is set later. | |
| 10472 </li> | |
| 10473 | |
| 10474 <li> | |
| 10475 <code>luaL_typerror</code> was removed. | |
| 10476 Write your own version if you need it. | |
| 10477 </li> | |
| 10478 | |
| 10479 <li> | |
| 10480 Function <code>lua_cpcall</code> is deprecated. | |
| 10481 You can simply push the function with <a href="#lua_pushcfunction"><code>lua_pushcfunction</code></a> | |
| 10482 and call it with <a href="#lua_pcall"><code>lua_pcall</code></a>. | |
| 10483 </li> | |
| 10484 | |
| 10485 <li> | |
| 10486 Functions <code>lua_equal</code> and <code>lua_lessthan</code> are deprecated. | |
| 10487 Use the new <a href="#lua_compare"><code>lua_compare</code></a> with appropriate options instead. | |
| 10488 </li> | |
| 10489 | |
| 10490 <li> | |
| 10491 Function <code>lua_objlen</code> was renamed <a href="#lua_rawlen"><code>lua_rawlen</code></a>. | |
| 10492 </li> | |
| 10493 | |
| 10494 <li> | |
| 10495 Function <a href="#lua_load"><code>lua_load</code></a> has an extra parameter, <code>mode</code>. | |
| 10496 Pass <code>NULL</code> to simulate the old behavior. | |
| 10497 </li> | |
| 10498 | |
| 10499 <li> | |
| 10500 Function <a href="#lua_resume"><code>lua_resume</code></a> has an extra parameter, <code>from</code>. | |
| 10501 Pass <code>NULL</code> or the thread doing the call. | |
| 10502 </li> | |
| 10503 | |
| 10504 </ul> | |
| 10505 | |
| 10506 | |
| 10507 | |
| 10508 | |
| 10509 <h1>9 – <a name="9">The Complete Syntax of Lua</a></h1> | |
| 10510 | |
| 10511 <p> | |
| 10512 Here is the complete syntax of Lua in extended BNF. | |
| 10513 (It does not describe operator precedences.) | |
| 10514 | |
| 10515 | |
| 10516 | |
| 10517 | |
| 10518 <pre> | |
| 10519 | |
| 10520 chunk ::= block | |
| 10521 | |
| 10522 block ::= {stat} [retstat] | |
| 10523 | |
| 10524 stat ::= ‘<b>;</b>’ | | |
| 10525 varlist ‘<b>=</b>’ explist | | |
| 10526 functioncall | | |
| 10527 label | | |
| 10528 <b>break</b> | | |
| 10529 <b>goto</b> Name | | |
| 10530 <b>do</b> block <b>end</b> | | |
| 10531 <b>while</b> exp <b>do</b> block <b>end</b> | | |
| 10532 <b>repeat</b> block <b>until</b> exp | | |
| 10533 <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | | |
| 10534 <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | | |
| 10535 <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | | |
| 10536 <b>function</b> funcname funcbody | | |
| 10537 <b>local</b> <b>function</b> Name funcbody | | |
| 10538 <b>local</b> namelist [‘<b>=</b>’ explist] | |
| 10539 | |
| 10540 retstat ::= <b>return</b> [explist] [‘<b>;</b>’] | |
| 10541 | |
| 10542 label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ | |
| 10543 | |
| 10544 funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | |
| 10545 | |
| 10546 varlist ::= var {‘<b>,</b>’ var} | |
| 10547 | |
| 10548 var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name | |
| 10549 | |
| 10550 namelist ::= Name {‘<b>,</b>’ Name} | |
| 10551 | |
| 10552 explist ::= exp {‘<b>,</b>’ exp} | |
| 10553 | |
| 10554 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | ‘<b>...</b>’ | functiondef | | |
| 10555 prefixexp | tableconstructor | exp binop exp | unop exp | |
| 10556 | |
| 10557 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | |
| 10558 | |
| 10559 functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args | |
| 10560 | |
| 10561 args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | String | |
| 10562 | |
| 10563 functiondef ::= <b>function</b> funcbody | |
| 10564 | |
| 10565 funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> | |
| 10566 | |
| 10567 parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’ | |
| 10568 | |
| 10569 tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’ | |
| 10570 | |
| 10571 fieldlist ::= field {fieldsep field} [fieldsep] | |
| 10572 | |
| 10573 field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | |
| 10574 | |
| 10575 fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | |
| 10576 | |
| 10577 binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | ‘<b>..</b>’ | | |
| 10578 ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | | |
| 10579 <b>and</b> | <b>or</b> | |
| 10580 | |
| 10581 unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | |
| 10582 | |
| 10583 </pre> | |
| 10584 | |
| 10585 <p> | |
| 10586 | |
| 10587 | |
| 10588 | |
| 10589 | |
| 10590 | |
| 10591 | |
| 10592 | |
| 10593 <HR> | |
| 10594 <SMALL CLASS="footer"> | |
| 10595 Last update: | |
| 10596 Thu Mar 21 13:01:53 BRT 2013 | |
| 10597 </SMALL> | |
| 10598 <!-- | |
| 10599 Last change: revised for Lua 5.2.2 | |
| 10600 --> | |
| 10601 | |
| 10602 </body></html> | |
| 10603 |
