comparison docs/manual.html @ 243:c912f6de2053

more work on the manual git-svn-id: https://luan-java.googlecode.com/svn/trunk@244 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 08 Oct 2014 06:22:04 +0000
parents b5a926c481a5
children
comparison
equal deleted inserted replaced
242:b5a926c481a5 243:c912f6de2053
64 <LI><A HREF="#3.3.3">3.3.3 &ndash; Assignment</A> 64 <LI><A HREF="#3.3.3">3.3.3 &ndash; Assignment</A>
65 <LI><A HREF="#3.3.4">3.3.4 &ndash; Control Structures</A> 65 <LI><A HREF="#3.3.4">3.3.4 &ndash; Control Structures</A>
66 <LI><A HREF="#3.3.5">3.3.5 &ndash; For Statement</A> 66 <LI><A HREF="#3.3.5">3.3.5 &ndash; For Statement</A>
67 <LI><A HREF="#3.3.6">3.3.6 &ndash; Function Calls as Statements</A> 67 <LI><A HREF="#3.3.6">3.3.6 &ndash; Function Calls as Statements</A>
68 <LI><A HREF="#3.3.7">3.3.7 &ndash; Local Declarations</A> 68 <LI><A HREF="#3.3.7">3.3.7 &ndash; Local Declarations</A>
69 <LI><A HREF="#3.3.8">3.3.8 &ndash; Or/And Statements</A>
69 </UL> 70 </UL>
70 <LI><A HREF="#3.4">3.4 &ndash; Expressions</A> 71 <LI><A HREF="#3.4">3.4 &ndash; Expressions</A>
71 <UL> 72 <UL>
72 <LI><A HREF="#3.4.1">3.4.1 &ndash; Arithmetic Operators</A> 73 <LI><A HREF="#3.4.1">3.4.1 &ndash; Arithmetic Operators</A>
73 <LI><A HREF="#3.4.2">3.4.2 &ndash; Coercion</A> 74 <LI><A HREF="#3.4.2">3.4.2 &ndash; Coercion</A>
82 </UL> 83 </UL>
83 <LI><A HREF="#3.5">3.5 &ndash; Visibility Rules</A> 84 <LI><A HREF="#3.5">3.5 &ndash; Visibility Rules</A>
84 </UL> 85 </UL>
85 <P> 86 <P>
86 <LI><A HREF="#4">4 &ndash; The Application Program Interface</A> 87 <LI><A HREF="#4">4 &ndash; The Application Program Interface</A>
87 <UL>
88 <LI><A HREF="#4.1">4.1 &ndash; The Stack</A>
89 <LI><A HREF="#4.2">4.2 &ndash; Stack Size</A>
90 <LI><A HREF="#4.3">4.3 &ndash; Valid and Acceptable Indices</A>
91 <LI><A HREF="#4.4">4.4 &ndash; C Closures</A>
92 <LI><A HREF="#4.5">4.5 &ndash; Registry</A>
93 <LI><A HREF="#4.6">4.6 &ndash; Error Handling in C</A>
94 <LI><A HREF="#4.7">4.7 &ndash; Handling Yields in C</A>
95 <LI><A HREF="#4.8">4.8 &ndash; Functions and Types</A>
96 <LI><A HREF="#4.9">4.9 &ndash; The Debug Interface</A>
97 </UL>
98 <P> 88 <P>
99 <LI><A HREF="#5">5 &ndash; The Auxiliary Library</A> 89 <LI><A HREF="#5">5 &ndash; The Auxiliary Library</A>
100 <UL>
101 <LI><A HREF="#5.1">5.1 &ndash; Functions and Types</A>
102 </UL>
103 <P> 90 <P>
104 <LI><A HREF="#6">6 &ndash; Standard Libraries</A> 91 <LI><A HREF="#6">6 &ndash; Standard Libraries</A>
105 <UL> 92 <UL>
106 <LI><A HREF="#6.1">6.1 &ndash; Basic Functions</A> 93 <LI><A HREF="#6.1">6.1 &ndash; Basic Functions</A>
107 <LI><A HREF="#6.2">6.2 &ndash; Coroutine Manipulation</A> 94 <LI><A HREF="#6.2">6.2 &ndash; Coroutine Manipulation</A>
131 </UL> 118 </UL>
132 119
133 120
134 <h1>1 &ndash; <a name="1">Introduction</a></h1> 121 <h1>1 &ndash; <a name="1">Introduction</a></h1>
135 122
136 <p> 123 <p>Luan is a high level programming language based on <a href="http://www.lua.org">Lua</a>. A great strength of Lua is its simplicity and Luan takes this even further, being even simpler than Lua. The goal is to provide a simple programming language for the casual programmer with as few concepts as possible so that programmers can quickly learn the language and then easily understand any code written in Luan.
137 Lua is an extension programming language designed to support 124
138 general procedural programming with data description 125 <p>Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers. By importing the <em>Java</em> package, one can directly call Java from Luan.
139 facilities. 126
140 It also offers good support for object-oriented programming, 127 <p>Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding feature to Luan, but rather by providing a complete set of libraries.
141 functional programming, and data-driven programming.
142 Lua is intended to be used as a powerful, lightweight,
143 embeddable scripting language for any program that needs one.
144 Lua is implemented as a library, written in <em>clean C</em>,
145 the common subset of Standard&nbsp;C and C++.
146
147
148 <p>
149 Being an extension language, Lua has no notion of a "main" program:
150 it only works <em>embedded</em> in a host client,
151 called the <em>embedding program</em> or simply the <em>host</em>.
152 The host program can invoke functions to execute a piece of Lua code,
153 can write and read Lua variables,
154 and can register C&nbsp;functions to be called by Lua code.
155 Through the use of C&nbsp;functions, Lua can be augmented to cope with
156 a wide range of different domains,
157 thus creating customized programming languages sharing a syntactical framework.
158 The Lua distribution includes a sample host program called <code>lua</code>,
159 which uses the Lua library to offer a complete, standalone Lua interpreter,
160 for interactive or batch use.
161
162
163 <p>
164 Lua is free software,
165 and is provided as usual with no guarantees,
166 as stated in its license.
167 The implementation described in this manual is available
168 at Lua's official web site, <code>www.lua.org</code>.
169
170
171 <p>
172 Like any other reference manual,
173 this document is dry in places.
174 For a discussion of the decisions behind the design of Lua,
175 see the technical papers available at Lua's web site.
176 For a detailed introduction to programming in Lua,
177 see Roberto's book, <em>Programming in Lua</em>.
178 128
179 <h1>2 &ndash; <a name="2">Basic Concepts</a></h1> 129 <h1>2 &ndash; <a name="2">Basic Concepts</a></h1>
180 130
181 <p> 131 <p>
182 This section describes the basic concepts of the language. 132 This section describes the basic concepts of the language.
184 134
185 135
186 <h2>2.1 &ndash; <a name="2.1">Values and Types</a></h2> 136 <h2>2.1 &ndash; <a name="2.1">Values and Types</a></h2>
187 137
188 <p> 138 <p>
189 Lua is a <em>dynamically typed language</em>. 139 Luan is a <em>dynamically typed language</em>.
190 This means that 140 This means that
191 variables do not have types; only values do. 141 variables do not have types; only values do.
192 There are no type definitions in the language. 142 There are no type definitions in the language.
193 All values carry their own type. 143 All values carry their own type.
194 144
195 145
196 <p> 146 <p>
197 All values in Lua are <em>first-class values</em>. 147 All values in Luan are <em>first-class values</em>.
198 This means that all values can be stored in variables, 148 This means that all values can be stored in variables,
199 passed as arguments to other functions, and returned as results. 149 passed as arguments to other functions, and returned as results.
200 150
201 151
202 <p> 152 <p>
203 There are eight basic types in Lua: 153 There are eight basic types in Lua:
204 <em>nil</em>, <em>boolean</em>, <em>number</em>, 154 <em>nil</em>, <em>boolean</em>, <em>number</em>,
205 <em>string</em>, <em>function</em>, <em>userdata</em>, 155 <em>string</em>, <em>binary</em>, <em>function</em>, <em>userdata</em>,
206 <em>thread</em>, and <em>table</em>. 156 and <em>table</em>.
207 <em>Nil</em> is the type of the value <b>nil</b>, 157 <em>Nil</em> is the type of the value <b>nil</b>,
208 whose main property is to be different from any other value; 158 whose main property is to be different from any other value;
209 it usually represents the absence of a useful value. 159 it usually represents the absence of a useful value.
160 <em>Nil</em> is implemented as the Java value <em>null</em>.
210 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>. 161 <em>Boolean</em> is the type of the values <b>false</b> and <b>true</b>.
211 Both <b>nil</b> and <b>false</b> make a condition false; 162 <em>Boolean</em> is implemented as the Java class <em>Boolean</em>.
212 any other value makes it true.
213 <em>Number</em> represents real (double-precision floating-point) numbers. 163 <em>Number</em> represents real (double-precision floating-point) numbers.
214 Operations on numbers follow the same rules of 164 <em>Number</em> is implemented as the Java class <em>Number</em>. Any Java subclass of <em>Number</em> is allowed and this is invisible to the Luan user. Operations on numbers follow the same rules of
215 the underlying C&nbsp;implementation, 165 the underlying Java&nbsp;implementation.
216 which, in turn, usually follows the IEEE 754 standard. 166
217 (It is easy to build Lua interpreters that use other 167 <em>String</em> is implemented as the Java class <em>String</em>.
218 internal representations for numbers, 168 <em>Binary</em> is implemented as the Java type <em>byte[]</em>.
219 such as single-precision floats or long integers; 169
220 see file <code>luaconf.h</code>.) 170
221 <em>String</em> represents immutable sequences of bytes. 171 <p>
222 172 Luan can call (and manipulate) functions written in Luan and
223 Lua is 8-bit clean: 173 functions written in Java
224 strings can contain any 8-bit value,
225 including embedded zeros ('<code>\0</code>').
226
227
228 <p>
229 Lua can call (and manipulate) functions written in Lua and
230 functions written in C
231 (see <a href="#3.4.9">&sect;3.4.9</a>). 174 (see <a href="#3.4.9">&sect;3.4.9</a>).
232 175
233 176
234 <p> 177 <p>
235 The type <em>userdata</em> is provided to allow arbitrary C&nbsp;data to 178 The type <em>userdata</em> is provided to allow arbitrary Java&nbsp;objects to
236 be stored in Lua variables. 179 be stored in Lua variables.
237 A userdata value is a pointer to a block of raw memory. 180 A userdata value is a Java object that isn't of the standard Luan types.
238 There are two kinds of userdata: 181
239 full userdata, where the block of memory is managed by Lua, 182
240 and light userdata, where the block of memory is managed by the host. 183 <p>
241 Userdata has no predefined operations in Lua, 184 Lua has a type <em>thread</em> that Luan lacks because Luan does not have the Lua concept of coroutines.
242 except assignment and identity test.
243 By using <em>metatables</em>,
244 the programmer can define operations for full userdata values
245 (see <a href="#2.4">&sect;2.4</a>).
246 Userdata values cannot be created or modified in Lua,
247 only through the C&nbsp;API.
248 This guarantees the integrity of data owned by the host program.
249
250
251 <p>
252 The type <em>thread</em> represents independent threads of execution
253 and it is used to implement coroutines (see <a href="#2.6">&sect;2.6</a>).
254 Do not confuse Lua threads with operating-system threads.
255 Lua supports coroutines on all systems,
256 even those that do not support threads.
257 185
258 186
259 <p> 187 <p>
260 The type <em>table</em> implements associative arrays, 188 The type <em>table</em> implements associative arrays,
261 that is, arrays that can be indexed not only with numbers, 189 that is, arrays that can be indexed not only with numbers,
262 but with any Lua value except <b>nil</b> and NaN 190 but with any Lua value except <b>nil</b>.
263 (<em>Not a Number</em>, a special numeric value used to represent
264 undefined or unrepresentable results, such as <code>0/0</code>).
265 Tables can be <em>heterogeneous</em>; 191 Tables can be <em>heterogeneous</em>;
266 that is, they can contain values of all types (except <b>nil</b>). 192 that is, they can contain values of all types (except <b>nil</b>).
267 Any key with value <b>nil</b> is not considered part of the table. 193 Any key with value <b>nil</b> is not considered part of the table.
268 Conversely, any key that is not part of a table has 194 Conversely, any key that is not part of a table has
269 an associated value <b>nil</b>. 195 an associated value <b>nil</b>.
304 if and only if <code>i</code> and <code>j</code> are raw equal 230 if and only if <code>i</code> and <code>j</code> are raw equal
305 (that is, equal without metamethods). 231 (that is, equal without metamethods).
306 232
307 233
308 <p> 234 <p>
309 Tables, functions, threads, and (full) userdata values are <em>objects</em>: 235 Tables, functions, and userdata values are <em>objects</em>:
310 variables do not actually <em>contain</em> these values, 236 variables do not actually <em>contain</em> these values,
311 only <em>references</em> to them. 237 only <em>references</em> to them.
312 Assignment, parameter passing, and function returns 238 Assignment, parameter passing, and function returns
313 always manipulate references to such values; 239 always manipulate references to such values;
314 these operations do not imply any kind of copy. 240 these operations do not imply any kind of copy.
348 Any table used as the value of <code>_ENV</code> is called an <em>environment</em>. 274 Any table used as the value of <code>_ENV</code> is called an <em>environment</em>.
349 275
350 276
351 <p> 277 <p>
352 Lua keeps a distinguished environment called the <em>global environment</em>. 278 Lua keeps a distinguished environment called the <em>global environment</em>.
353 This value is kept at a special index in the C registry (see <a href="#4.5">&sect;4.5</a>). 279 This value is kept in the Luan state implemented in Java.
354 In Lua, the variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value. 280 In Luan, the variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value.
355 281
356 282
357 <p> 283 <p>
358 When Lua compiles a chunk, 284 When Lua compiles a chunk,
359 it initializes the value of its <code>_ENV</code> upvalue 285 it initializes the value of its <code>_ENV</code> to an empty table.
360 with the global environment (see <a href="#pdf-load"><code>load</code></a>). 286 The values in the global environment become local variables of the chunk.
361 Therefore, by default, 287 All standard libraries are loaded in the global environment
362 global variables in Lua code refer to entries in the global environment. 288 and so they become available as local variables.
363 Moreover, all standard libraries are loaded in the global environment 289 You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>load_file</code></a>)
364 and several functions there operate on that environment. 290 to load a chunk with a specific environment instead of starting empty.
365 You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>) 291
366 to load a chunk with a different environment. 292
367 (In C, you have to load the chunk and then change the value 293 <p>
368 of its first upvalue.) 294 If you change the values in the global environment,
369
370
371 <p>
372 If you change the global environment in the registry
373 (through C code or the debug library),
374 all chunks loaded after the change will get the new environment. 295 all chunks loaded after the change will get the new environment.
375 Previously loaded chunks are not affected, however, 296 Previously loaded chunks are not affected, however,
376 as each has its own reference to the environment in its <code>_ENV</code> variable. 297 as each has its own references to the values in its local variables.
377 Moreover, the variable <a href="#pdf-_G"><code>_G</code></a> 298 Moreover, the variable <a href="#pdf-_G"><code>_G</code></a>
378 (which is stored in the original global environment) 299 (which is stored in the original global environment)
379 is never updated by Lua. 300 is never updated by Lua.
380 301
381 302
383 304
384 305
385 <h2>2.3 &ndash; <a name="2.3">Error Handling</a></h2> 306 <h2>2.3 &ndash; <a name="2.3">Error Handling</a></h2>
386 307
387 <p> 308 <p>
388 Because Lua is an embedded extension language, 309 Luan error handling is quite different from Lua.
389 all Lua actions start from C&nbsp;code in the host program 310
390 calling a function from the Lua library (see <a href="#lua_pcall"><code>lua_pcall</code></a>). 311
391 Whenever an error occurs during 312 <p>
392 the compilation or execution of a Lua chunk, 313 Luan code can explicitly generate an error by calling the
393 control returns to the host,
394 which can take appropriate measures
395 (such as printing an error message).
396
397
398 <p>
399 Lua code can explicitly generate an error by calling the
400 <a href="#pdf-error"><code>error</code></a> function. 314 <a href="#pdf-error"><code>error</code></a> function.
401 If you need to catch errors in Lua, 315 Unlike Lua, Luan has <code>try-catch</code> blocks for catching errors. This means that there is no need for Lua's <code>pcall</code> and <code>xpcall</code> functions.
402 you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a>
403 to call a given function in <em>protected mode</em>.
404 316
405 317
406 <p> 318 <p>
407 Whenever there is an error, 319 Whenever there is an error,
408 an <em>error object</em> (also called an <em>error message</em>) 320 an <em>error object</em> (also called an <em>error message</em>)
410 Lua itself only generates errors where the error object is a string, 322 Lua itself only generates errors where the error object is a string,
411 but programs may generate errors with 323 but programs may generate errors with
412 any value for the error object. 324 any value for the error object.
413 325
414 326
415 <p>
416 When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>,
417 you may give a <em>message handler</em>
418 to be called in case of errors.
419 This function is called with the original error message
420 and returns a new error message.
421 It is called before the error unwinds the stack,
422 so that it can gather more information about the error,
423 for instance by inspecting the stack and creating a stack traceback.
424 This message handler is still protected by the protected call;
425 so, an error inside the message handler
426 will call the message handler again.
427 If this loop goes on, Lua breaks it and returns an appropriate message.
428
429
430
431 327
432 328
433 <h2>2.4 &ndash; <a name="2.4">Metatables and Metamethods</a></h2> 329 <h2>2.4 &ndash; <a name="2.4">Metatables and Metamethods</a></h2>
434 330
435 <p> 331 <p>
436 Every value in Lua can have a <em>metatable</em>. 332 Every table in Luan can have a <em>metatable</em>.
437 This <em>metatable</em> is an ordinary Lua table 333 This <em>metatable</em> is an ordinary Luan table
438 that defines the behavior of the original value 334 that defines the behavior of the original table
439 under certain special operations. 335 under certain special operations.
440 You can change several aspects of the behavior 336 You can change several aspects of the behavior
441 of operations over a value by setting specific fields in its metatable. 337 of operations over a table by setting specific fields in its metatable.
442 For instance, when a non-numeric value is the operand of an addition, 338 For instance, when a table is the operand of an addition,
443 Lua checks for a function in the field "<code>__add</code>" of the value's metatable. 339 Luan checks for a function in the field "<code>__add</code>" of the table's metatable.
444 If it finds one, 340 If it finds one,
445 Lua calls this function to perform the addition. 341 Luan calls this function to perform the addition.
446 342
343 <p>
344 Inside Luan's implementation, there is a global metatable that applies to all objects. This metatable is not exposed to Luan users but can be used to change the behavior of objects other than tables.
447 345
448 <p> 346 <p>
449 The keys in a metatable are derived from the <em>event</em> names; 347 The keys in a metatable are derived from the <em>event</em> names;
450 the corresponding values are called <em>metamethods</em>. 348 the corresponding values are called <em>metamethods</em>.
451 In the previous example, the event is <code>"add"</code> 349 In the previous example, the event is <code>"add"</code>
452 and the metamethod is the function that performs the addition. 350 and the metamethod is the function that performs the addition.
453 351
454 352
455 <p> 353 <p>
456 You can query the metatable of any value 354 You can query the metatable of any value
457 using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function. 355 using the <a href="#pdf-getmetatable"><code>get_metatable</code></a> function.
458 356
459 357
460 <p> 358 <p>
461 You can replace the metatable of tables 359 You can replace the metatable of tables
462 using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function. 360 using the <a href="#pdf-setmetatable"><code>set_metatable</code></a> function.
463 You cannot change the metatable of other types from Lua 361
464 (except by using the debug library); 362
465 you must use the C&nbsp;API for that. 363 <p>
466 364 Tables have individual metatables
467 365 (although multiple tables can share their metatables).
468 <p> 366 By default, a table has no metatable.
469 Tables and full userdata have individual metatables 367
470 (although multiple tables and userdata can share their metatables). 368
471 Values of all other types share one single metatable per type; 369 <p>
472 that is, there is one single metatable for all numbers, 370 A metatable controls how a table behaves in arithmetic operations,
473 one for all strings, etc.
474 By default, a value has no metatable,
475 but the string library sets a metatable for the string type (see <a href="#6.4">&sect;6.4</a>).
476
477
478 <p>
479 A metatable controls how an object behaves in arithmetic operations,
480 order comparisons, concatenation, length operation, and indexing. 371 order comparisons, concatenation, length operation, and indexing.
481 A metatable also can define a function to be called 372 When Luan performs one of these operations over a table,
482 when a userdata or a table is garbage collected. 373 it checks whether this table has a metatable with the corresponding event.
483 When Lua performs one of these operations over a value,
484 it checks whether this value has a metatable with the corresponding event.
485 If so, the value associated with that key (the metamethod) 374 If so, the value associated with that key (the metamethod)
486 controls how Lua will perform the operation. 375 controls how Luan will perform the operation.
487 376
488 377
489 <p> 378 <p>
490 Metatables control the operations listed next. 379 Metatables control the operations listed next.
491 Each operation is identified by its corresponding name. 380 Each operation is identified by its corresponding name.
494 for instance, the key for operation "add" is the 383 for instance, the key for operation "add" is the
495 string "<code>__add</code>". 384 string "<code>__add</code>".
496 385
497 386
498 <p> 387 <p>
499 The semantics of these operations is better explained by a Lua function 388 The semantics of these operations is better explained by a Luan function
500 describing how the interpreter executes the operation. 389 describing how the interpreter executes the operation.
501 The code shown here in Lua is only illustrative; 390 The code shown here in Lua is only illustrative;
502 the real behavior is hard coded in the interpreter 391 the real behavior is hard coded in the interpreter
503 and it is much more efficient than this simulation. 392 and it is much more efficient than this simulation.
504 All functions used in these descriptions 393 All functions used in these descriptions
505 (<a href="#pdf-rawget"><code>rawget</code></a>, <a href="#pdf-tonumber"><code>tonumber</code></a>, etc.) 394 (<a href="#pdf-rawget"><code>raw_get</code></a>, <a href="#pdf-tonumber"><code>to_number</code></a>, etc.)
506 are described in <a href="#6.1">&sect;6.1</a>. 395 are described in <a href="#6.1">&sect;6.1</a>.
507 In particular, to retrieve the metamethod of a given object, 396 In particular, to retrieve the metamethod of a given object,
508 we use the expression 397 we use the expression
509 398
510 <pre> 399 <pre>
511 metatable(obj)[event] 400 metatable(obj)[event]
512 </pre><p> 401 </pre><p>
513 This should be read as 402 This should be read as
514 403
515 <pre> 404 <pre>
516 rawget(getmetatable(obj) or {}, event) 405 raw_get(get_metatable(obj) or {}, event)
517 </pre><p> 406 </pre><p>
518 This means that the access to a metamethod does not invoke other metamethods, 407 This means that the access to a metamethod does not invoke other metamethods,
519 and access to objects with no metatables does not fail 408 and access to tables with no metatables does not fail
520 (it simply results in <b>nil</b>). 409 (it simply results in <b>nil</b>).
521 410
522 411 <p>
523 <p>
524 For the unary <code>-</code> and <code>#</code> operators,
525 the metamethod is called with a dummy second argument.
526 This extra argument is only to simplify Lua's internals;
527 it may be removed in future versions and therefore it is not present
528 in the following code.
529 (For most uses this extra argument is irrelevant.)
530
531 412
532 413
533 <ul> 414 <ul>
534 415
535 <li><b>"add": </b> 416 <li><b>"add": </b>
536 the <code>+</code> operation. 417 the <code>+</code> operation.
537 418
538 419
539 420
540 <p> 421 <p>
541 The function <code>getbinhandler</code> below defines how Lua chooses a handler 422 The function <code>get_bin_handler</code> below defines how Luan chooses a handler
542 for a binary operation. 423 for a binary operation.
543 First, Lua tries the first operand. 424 First, Luan tries the first operand.
544 If its type does not define a handler for the operation, 425 If its type does not define a handler for the operation,
545 then Lua tries the second operand. 426 then Lua tries the second operand.
546 427
547 <pre> 428 <pre>
548 function getbinhandler (op1, op2, event) 429 function get_bin_handler (op1, op2, event)
549 return metatable(op1)[event] or metatable(op2)[event] 430 return metatable(op1)[event] or metatable(op2)[event]
550 end 431 end
551 </pre><p> 432 </pre><p>
552 By using this function, 433 By using this function,
553 the behavior of the <code>op1 + op2</code> is 434 the behavior of the <code>op1 + op2</code> is
554 435
555 <pre> 436 <pre>
556 function add_event (op1, op2) 437 function add_event (op1, op2)
557 local o1, o2 = tonumber(op1), tonumber(op2) 438 local o1, o2 = to_number(op1), to_number(op2)
558 if o1 and o2 then -- both operands are numeric? 439 if o1 and o2 then -- both operands are numeric?
559 return o1 + o2 -- '+' here is the primitive 'add' 440 return o1 + o2 -- '+' here is the primitive 'add'
560 else -- at least one of the operands is not numeric 441 else -- at least one of the operands is not numeric
561 local h = getbinhandler(op1, op2, "__add") 442 local h = get_bin_handler(op1, op2, "__add")
562 if h then 443 if h then
563 -- call the handler with both operands 444 -- call the handler with both operands
564 return (h(op1, op2)) 445 return (h(op1, op2))
565 else -- no handler available: default behavior 446 else -- no handler available: default behavior
566 error(&middot;&middot;&middot;) 447 error(&middot;&middot;&middot;)
580 the <code>*</code> operation. 461 the <code>*</code> operation.
581 462
582 Behavior similar to the "add" operation. 463 Behavior similar to the "add" operation.
583 </li> 464 </li>
584 465
585 <li><b>"div": </b> 466 <li><b>"span": </b>
586 the <code>/</code> operation. 467 the <code>/</code> operation.
587 468
588 Behavior similar to the "add" operation. 469 Behavior similar to the "add" operation.
589 </li> 470 </li>
590 471
608 the unary <code>-</code> operation. 489 the unary <code>-</code> operation.
609 490
610 491
611 <pre> 492 <pre>
612 function unm_event (op) 493 function unm_event (op)
613 local o = tonumber(op) 494 local o = to_number(op)
614 if o then -- operand is numeric? 495 if o then -- operand is numeric?
615 return -o -- '-' here is the primitive 'unm' 496 return -o -- '-' here is the primitive 'unm'
616 else -- the operand is not numeric. 497 else -- the operand is not numeric.
617 -- Try to get a handler from the operand 498 -- Try to get a handler from the operand
618 local h = metatable(op).__unm 499 local h = metatable(op).__unm
635 function concat_event (op1, op2) 516 function concat_event (op1, op2)
636 if (type(op1) == "string" or type(op1) == "number") and 517 if (type(op1) == "string" or type(op1) == "number") and
637 (type(op2) == "string" or type(op2) == "number") then 518 (type(op2) == "string" or type(op2) == "number") then
638 return op1 .. op2 -- primitive string concatenation 519 return op1 .. op2 -- primitive string concatenation
639 else 520 else
640 local h = getbinhandler(op1, op2, "__concat") 521 local h = get_bin_handler(op1, op2, "__concat")
641 if h then 522 if h then
642 return (h(op1, op2)) 523 return (h(op1, op2))
643 else 524 else
644 error(&middot;&middot;&middot;) 525 error(&middot;&middot;&middot;)
645 end 526 end
672 </li> 553 </li>
673 554
674 <li><b>"eq": </b> 555 <li><b>"eq": </b>
675 the <code>==</code> operation. 556 the <code>==</code> operation.
676 557
677 The function <code>getequalhandler</code> defines how Lua chooses a metamethod 558 The function <code>get_equal_handler</code> defines how Luan chooses a metamethod
678 for equality. 559 for equality.
679 A metamethod is selected only when both values 560 A metamethod is selected only when both values
680 being compared have the same type 561 being compared have the same type
681 and the same metamethod for the selected operation, 562 and the same metamethod for the selected operation,
682 and the values are either tables or full userdata. 563 and the values are either tables or full userdata.
683 564
684 <pre> 565 <pre>
685 function getequalhandler (op1, op2) 566 function get_equal_handler (op1, op2)
686 if type(op1) ~= type(op2) or 567 if type(op1) ~= type(op2) or
687 (type(op1) ~= "table" and type(op1) ~= "userdata") then 568 (type(op1) ~= "table" and type(op1) ~= "userdata") then
688 return nil -- different values 569 return nil -- different values
689 end 570 end
690 local mm1 = metatable(op1).__eq 571 local mm1 = metatable(op1).__eq
698 function eq_event (op1, op2) 579 function eq_event (op1, op2)
699 if op1 == op2 then -- primitive equal? 580 if op1 == op2 then -- primitive equal?
700 return true -- values are equal 581 return true -- values are equal
701 end 582 end
702 -- try metamethod 583 -- try metamethod
703 local h = getequalhandler(op1, op2) 584 local h = get_equal_handler(op1, op2)
704 if h then 585 if h then
705 return not not h(op1, op2) 586 return to_boolean(h(op1, op2))
706 else 587 else
707 return false 588 return false
708 end 589 end
709 end 590 end
710 </pre><p> 591 </pre><p>
720 if type(op1) == "number" and type(op2) == "number" then 601 if type(op1) == "number" and type(op2) == "number" then
721 return op1 &lt; op2 -- numeric comparison 602 return op1 &lt; op2 -- numeric comparison
722 elseif type(op1) == "string" and type(op2) == "string" then 603 elseif type(op1) == "string" and type(op2) == "string" then
723 return op1 &lt; op2 -- lexicographic comparison 604 return op1 &lt; op2 -- lexicographic comparison
724 else 605 else
725 local h = getbinhandler(op1, op2, "__lt") 606 local h = get_bin_handler(op1, op2, "__lt")
726 if h then 607 if h then
727 return not not h(op1, op2) 608 return to_boolean(h(op1, op2))
728 else 609 else
729 error(&middot;&middot;&middot;) 610 error(&middot;&middot;&middot;)
730 end 611 end
731 end 612 end
732 end 613 end
743 if type(op1) == "number" and type(op2) == "number" then 624 if type(op1) == "number" and type(op2) == "number" then
744 return op1 &lt;= op2 -- numeric comparison 625 return op1 &lt;= op2 -- numeric comparison
745 elseif type(op1) == "string" and type(op2) == "string" then 626 elseif type(op1) == "string" and type(op2) == "string" then
746 return op1 &lt;= op2 -- lexicographic comparison 627 return op1 &lt;= op2 -- lexicographic comparison
747 else 628 else
748 local h = getbinhandler(op1, op2, "__le") 629 local h = get_bin_handler(op1, op2, "__le")
749 if h then 630 if h then
750 return not not h(op1, op2) 631 return to_boolean(h(op1, op2))
751 else 632 else
752 h = getbinhandler(op1, op2, "__lt") 633 h = get_bin_handler(op1, op2, "__lt")
753 if h then 634 if h then
754 return not h(op2, op1) 635 return not to_boolean(h(op2, op1))
755 else 636 else
756 error(&middot;&middot;&middot;) 637 error(&middot;&middot;&middot;)
757 end 638 end
758 end 639 end
759 end 640 end
760 end 641 end
761 </pre><p> 642 </pre><p>
762 Note that, in the absence of a "le" metamethod, 643 Note that, in the absence of a "le" metamethod,
763 Lua tries the "lt", assuming that <code>a &lt;= b</code> is 644 Luan tries the "lt", assuming that <code>a &lt;= b</code> is
764 equivalent to <code>not (b &lt; a)</code>. 645 equivalent to <code>not (b &lt; a)</code>.
765 646
766 647
767 <p> 648 <p>
768 As with the other comparison operators, 649 As with the other comparison operators,
777 no key is ever present, 658 no key is ever present,
778 so the metamethod is always tried.) 659 so the metamethod is always tried.)
779 660
780 661
781 <pre> 662 <pre>
782 function gettable_event (table, key) 663 function get_table_event (table, key)
783 local h 664 local h
784 if type(table) == "table" then 665 if type(table) == "table" then
785 local v = rawget(table, key) 666 local v = raw_get(table, key)
786 -- if key is present, return raw value 667 -- if key is present, return raw value
787 if v ~= nil then return v end 668 if v ~= nil then return v end
788 h = metatable(table).__index 669 h = metatable(table).__index
789 if h == nil then return nil end 670 if h == nil then return nil end
790 else 671 else
806 Note that the metamethod is tried only 687 Note that the metamethod is tried only
807 when <code>key</code> is not present in <code>table</code>. 688 when <code>key</code> is not present in <code>table</code>.
808 689
809 690
810 <pre> 691 <pre>
811 function settable_event (table, key, value) 692 function set_table_event (table, key, value)
812 local h 693 local h
813 if type(table) == "table" then 694 if type(table) == "table" then
814 local v = rawget(table, key) 695 local v = raw_get(table, key)
815 -- if key is present, do raw assignment 696 -- if key is present, do raw assignment
816 if v ~= nil then rawset(table, key, value); return end 697 if v ~= nil then raw_set(table, key, value); return end
817 h = metatable(table).__newindex 698 h = metatable(table).__newindex
818 if h == nil then rawset(table, key, value); return end 699 if h == nil then raw_set(table, key, value); return end
819 else 700 else
820 h = metatable(table).__newindex 701 h = metatable(table).__newindex
821 if h == nil then 702 if h == nil then
822 error(&middot;&middot;&middot;) 703 error(&middot;&middot;&middot;)
823 end 704 end
829 end 710 end
830 </pre><p> 711 </pre><p>
831 </li> 712 </li>
832 713
833 <li><b>"call": </b> 714 <li><b>"call": </b>
834 called when Lua calls a value. 715 called when Luan calls a value.
835 716
836 717
837 <pre> 718 <pre>
838 function function_event (func, ...) 719 function function_event (func, ...)
839 if type(func) == "function" then 720 if type(func) == "function" then
856 737
857 738
858 <h2>2.5 &ndash; <a name="2.5">Garbage Collection</a></h2> 739 <h2>2.5 &ndash; <a name="2.5">Garbage Collection</a></h2>
859 740
860 <p> 741 <p>
861 Lua performs automatic memory management. 742 Luan uses Java's garbage collection, so there is very little to say on this subject. So this section is just a place holder to replace the long explanation of Lua's garbage collection which isn't needed by Luan.
862 This means that 743
863 you have to worry neither about allocating memory for new objects 744 <p>
864 nor about freeing it when the objects are no longer needed. 745 Lua has <em>weak tables</em> which is a good concept but is not yet implemented in Luan. It will be added when there is a need.
865 Lua manages memory automatically by running
866 a <em>garbage collector</em> to collect all <em>dead objects</em>
867 (that is, objects that are no longer accessible from Lua).
868 All memory used by Lua is subject to automatic management:
869 strings, tables, userdata, functions, threads, internal structures, etc.
870
871
872 <p>
873 Lua implements an incremental mark-and-sweep collector.
874 It uses two numbers to control its garbage-collection cycles:
875 the <em>garbage-collector pause</em> and
876 the <em>garbage-collector step multiplier</em>.
877 Both use percentage points as units
878 (e.g., a value of 100 means an internal value of 1).
879
880
881 <p>
882 The garbage-collector pause
883 controls how long the collector waits before starting a new cycle.
884 Larger values make the collector less aggressive.
885 Values smaller than 100 mean the collector will not wait to
886 start a new cycle.
887 A value of 200 means that the collector waits for the total memory in use
888 to double before starting a new cycle.
889
890
891 <p>
892 The garbage-collector step multiplier
893 controls the relative speed of the collector relative to
894 memory allocation.
895 Larger values make the collector more aggressive but also increase
896 the size of each incremental step.
897 Values smaller than 100 make the collector too slow and
898 can result in the collector never finishing a cycle.
899 The default is 200,
900 which means that the collector runs at "twice"
901 the speed of memory allocation.
902
903
904 <p>
905 If you set the step multiplier to a very large number
906 (larger than 10% of the maximum number of
907 bytes that the program may use),
908 the collector behaves like a stop-the-world collector.
909 If you then set the pause to 200,
910 the collector behaves as in old Lua versions,
911 doing a complete collection every time Lua doubles its
912 memory usage.
913
914
915 <p>
916 You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
917 or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
918 You can also use these functions to control
919 the collector directly (e.g., stop and restart it).
920
921
922 <p>
923 As an experimental feature in Lua 5.2,
924 you can change the collector's operation mode
925 from incremental to <em>generational</em>.
926 A <em>generational collector</em> assumes that most objects die young,
927 and therefore it traverses only young (recently created) objects.
928 This behavior can reduce the time used by the collector,
929 but also increases memory usage (as old dead objects may accumulate).
930 To mitigate this second problem,
931 from time to time the generational collector performs a full collection.
932 Remember that this is an experimental feature;
933 you are welcome to try it,
934 but check your gains.
935
936
937
938 <h3>2.5.1 &ndash; <a name="2.5.1">Garbage-Collection Metamethods</a></h3>
939
940 <p>
941 You can set garbage-collector metamethods for tables
942 and, using the C&nbsp;API,
943 for full userdata (see <a href="#2.4">&sect;2.4</a>).
944 These metamethods are also called <em>finalizers</em>.
945 Finalizers allow you to coordinate Lua's garbage collection
946 with external resource management
947 (such as closing files, network or database connections,
948 or freeing your own memory).
949
950
951 <p>
952 For an object (table or userdata) to be finalized when collected,
953 you must <em>mark</em> it for finalization.
954
955 You mark an object for finalization when you set its metatable
956 and the metatable has a field indexed by the string "<code>__gc</code>".
957 Note that if you set a metatable without a <code>__gc</code> field
958 and later create that field in the metatable,
959 the object will not be marked for finalization.
960 However, after an object is marked,
961 you can freely change the <code>__gc</code> field of its metatable.
962
963
964 <p>
965 When a marked object becomes garbage,
966 it is not collected immediately by the garbage collector.
967 Instead, Lua puts it in a list.
968 After the collection,
969 Lua does the equivalent of the following function
970 for each object in that list:
971
972 <pre>
973 function gc_event (obj)
974 local h = metatable(obj).__gc
975 if type(h) == "function" then
976 h(obj)
977 end
978 end
979 </pre>
980
981 <p>
982 At the end of each garbage-collection cycle,
983 the finalizers for objects are called in
984 the reverse order that they were marked for collection,
985 among those collected in that cycle;
986 that is, the first finalizer to be called is the one associated
987 with the object marked last in the program.
988 The execution of each finalizer may occur at any point during
989 the execution of the regular code.
990
991
992 <p>
993 Because the object being collected must still be used by the finalizer,
994 it (and other objects accessible only through it)
995 must be <em>resurrected</em> by Lua.
996 Usually, this resurrection is transient,
997 and the object memory is freed in the next garbage-collection cycle.
998 However, if the finalizer stores the object in some global place
999 (e.g., a global variable),
1000 then there is a permanent resurrection.
1001 In any case,
1002 the object memory is freed only when it becomes completely inaccessible;
1003 its finalizer will never be called twice.
1004
1005
1006 <p>
1007 When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
1008 Lua calls the finalizers of all objects marked for finalization,
1009 following the reverse order that they were marked.
1010 If any finalizer marks new objects for collection during that phase,
1011 these new objects will not be finalized.
1012
1013
1014
1015
1016
1017 <h3>2.5.2 &ndash; <a name="2.5.2">Weak Tables</a></h3>
1018
1019 <p>
1020 A <em>weak table</em> is a table whose elements are
1021 <em>weak references</em>.
1022 A weak reference is ignored by the garbage collector.
1023 In other words,
1024 if the only references to an object are weak references,
1025 then the garbage collector will collect that object.
1026
1027
1028 <p>
1029 A weak table can have weak keys, weak values, or both.
1030 A table with weak keys allows the collection of its keys,
1031 but prevents the collection of its values.
1032 A table with both weak keys and weak values allows the collection of
1033 both keys and values.
1034 In any case, if either the key or the value is collected,
1035 the whole pair is removed from the table.
1036 The weakness of a table is controlled by the
1037 <code>__mode</code> field of its metatable.
1038 If the <code>__mode</code> field is a string containing the character&nbsp;'<code>k</code>',
1039 the keys in the table are weak.
1040 If <code>__mode</code> contains '<code>v</code>',
1041 the values in the table are weak.
1042
1043
1044 <p>
1045 A table with weak keys and strong values
1046 is also called an <em>ephemeron table</em>.
1047 In an ephemeron table,
1048 a value is considered reachable only if its key is reachable.
1049 In particular,
1050 if the only reference to a key comes through its value,
1051 the pair is removed.
1052
1053
1054 <p>
1055 Any change in the weakness of a table may take effect only
1056 at the next collect cycle.
1057 In particular, if you change the weakness to a stronger mode,
1058 Lua may still collect some items from that table
1059 before the change takes effect.
1060
1061
1062 <p>
1063 Only objects that have an explicit construction
1064 are removed from weak tables.
1065 Values, such as numbers and light C functions,
1066 are not subject to garbage collection,
1067 and therefore are not removed from weak tables
1068 (unless its associated value is collected).
1069 Although strings are subject to garbage collection,
1070 they do not have an explicit construction,
1071 and therefore are not removed from weak tables.
1072
1073
1074 <p>
1075 Resurrected objects
1076 (that is, objects being finalized
1077 and objects accessible only through objects being finalized)
1078 have a special behavior in weak tables.
1079 They are removed from weak values before running their finalizers,
1080 but are removed from weak keys only in the next collection
1081 after running their finalizers, when such objects are actually freed.
1082 This behavior allows the finalizer to access properties
1083 associated with the object through weak tables.
1084
1085
1086 <p>
1087 If a weak table is among the resurrected objects in a collection cycle,
1088 it may not be properly cleared until the next cycle.
1089
1090
1091
1092
1093 746
1094 747
1095 748
1096 <h2>2.6 &ndash; <a name="2.6">Coroutines</a></h2> 749 <h2>2.6 &ndash; <a name="2.6">Coroutines</a></h2>
1097 750
1098 <p> 751 <p>
1099 Lua supports coroutines, 752 Unlike Lua, Luan does not support coroutines. Yes coroutines are cool, but they are not simple, so in the name of simplicity, Luan does without them.
1100 also called <em>collaborative multithreading</em>.
1101 A coroutine in Lua represents an independent thread of execution.
1102 Unlike threads in multithread systems, however,
1103 a coroutine only suspends its execution by explicitly calling
1104 a yield function.
1105
1106
1107 <p>
1108 You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>.
1109 Its sole argument is a function
1110 that is the main function of the coroutine.
1111 The <code>create</code> function only creates a new coroutine and
1112 returns a handle to it (an object of type <em>thread</em>);
1113 it does not start the coroutine.
1114
1115
1116 <p>
1117 You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
1118 When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
1119 passing as its first argument
1120 a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
1121 the coroutine starts its execution,
1122 at the first line of its main function.
1123 Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed on
1124 to the coroutine main function.
1125 After the coroutine starts running,
1126 it runs until it terminates or <em>yields</em>.
1127
1128
1129 <p>
1130 A coroutine can terminate its execution in two ways:
1131 normally, when its main function returns
1132 (explicitly or implicitly, after the last instruction);
1133 and abnormally, if there is an unprotected error.
1134 In the first case, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>,
1135 plus any values returned by the coroutine main function.
1136 In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b>
1137 plus an error message.
1138
1139
1140 <p>
1141 A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
1142 When a coroutine yields,
1143 the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately,
1144 even if the yield happens inside nested function calls
1145 (that is, not in the main function,
1146 but in a function directly or indirectly called by the main function).
1147 In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>,
1148 plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
1149 The next time you resume the same coroutine,
1150 it continues its execution from the point where it yielded,
1151 with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra
1152 arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
1153
1154
1155 <p>
1156 Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
1157 the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine,
1158 but instead of returning the coroutine itself,
1159 it returns a function that, when called, resumes the coroutine.
1160 Any arguments passed to this function
1161 go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
1162 <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>,
1163 except the first one (the boolean error code).
1164 Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
1165 <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors;
1166 any error is propagated to the caller.
1167
1168
1169 <p>
1170 As an example of how coroutines work,
1171 consider the following code:
1172
1173 <pre>
1174 function foo (a)
1175 print("foo", a)
1176 return coroutine.yield(2*a)
1177 end
1178
1179 co = coroutine.create(function (a,b)
1180 print("co-body", a, b)
1181 local r = foo(a+1)
1182 print("co-body", r)
1183 local r, s = coroutine.yield(a+b, a-b)
1184 print("co-body", r, s)
1185 return b, "end"
1186 end)
1187
1188 print("main", coroutine.resume(co, 1, 10))
1189 print("main", coroutine.resume(co, "r"))
1190 print("main", coroutine.resume(co, "x", "y"))
1191 print("main", coroutine.resume(co, "x", "y"))
1192 </pre><p>
1193 When you run it, it produces the following output:
1194
1195 <pre>
1196 co-body 1 10
1197 foo 2
1198 main true 4
1199 co-body r
1200 main true 11 -9
1201 co-body x y
1202 main true 10 end
1203 main false cannot resume dead coroutine
1204 </pre>
1205
1206 <p>
1207 You can also create and manipulate coroutines through the C API:
1208 see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>,
1209 and <a href="#lua_yield"><code>lua_yield</code></a>.
1210 753
1211 754
1212 755
1213 756
1214 757
1215 <h1>3 &ndash; <a name="3">The Language</a></h1> 758 <h1>3 &ndash; <a name="3">The Language</a></h1>
1216 759
1217 <p> 760 <p>
1218 This section describes the lexis, the syntax, and the semantics of Lua. 761 This section describes the lexis, the syntax, and the semantics of Luan.
1219 In other words, 762 In other words,
1220 this section describes 763 this section describes
1221 which tokens are valid, 764 which tokens are valid,
1222 how they can be combined, 765 how they can be combined,
1223 and what their combinations mean. 766 and what their combinations mean.
1229 {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and 772 {<em>a</em>}&nbsp;means&nbsp;0 or more <em>a</em>'s, and
1230 [<em>a</em>]&nbsp;means an optional <em>a</em>. 773 [<em>a</em>]&nbsp;means an optional <em>a</em>.
1231 Non-terminals are shown like non-terminal, 774 Non-terminals are shown like non-terminal,
1232 keywords are shown like <b>kword</b>, 775 keywords are shown like <b>kword</b>,
1233 and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;. 776 and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;.
1234 The complete syntax of Lua can be found in <a href="#9">&sect;9</a> 777 The complete syntax of Luan can be found in <a href="#9">&sect;9</a>
1235 at the end of this manual. 778 at the end of this manual.
1236 779
1237 780
1238 781
1239 <h2>3.1 &ndash; <a name="3.1">Lexical Conventions</a></h2> 782 <h2>3.1 &ndash; <a name="3.1">Lexical Conventions</a></h2>
1240 783
1241 <p> 784 <p>
1242 Lua is a free-form language. 785 Luan ignores spaces and comments
1243 It ignores spaces (including new lines) and comments
1244 between lexical elements (tokens), 786 between lexical elements (tokens),
1245 except as delimiters between names and keywords. 787 except as delimiters between names and keywords.
788
789 But unlike Lua, Luan generally treats the newline character as a statement separator. This is how most languages work. If a newline is preceded by a backslash, then it is treated like a space. Also, inside of parenthesis (...), brackets [...], and braces {...}, a newline is treated like a space. This allows the Luan parser to catch mistakes more easily.
790
791 <p>
792 In interactive mode, Luan allows an expression on a line which is then evaluated and printed. This means that entering <em>1+1</em> on an interactive line will produce <em>2</em>.
1246 793
1247 794
1248 <p> 795 <p>
1249 <em>Names</em> 796 <em>Names</em>
1250 (also called <em>identifiers</em>) 797 (also called <em>identifiers</em>)
1264 false for function goto if in 811 false for function goto if in
1265 local nil not or repeat return 812 local nil not or repeat return
1266 then true until while 813 then true until while
1267 </pre> 814 </pre>
1268 815
816 The following <em>keywords</em> are also reserved in Luan but not in Lua:
817
818 <pre>
819 catch import try
820 </pre>
821
822
823
1269 <p> 824 <p>
1270 Lua is a case-sensitive language: 825 Lua is a case-sensitive language:
1271 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code> 826 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
1272 are two different, valid names. 827 are two different, valid names.
1273 As a convention, names starting with an underscore followed by 828 As a convention, names starting with an underscore followed by
1279 The following strings denote other tokens: 834 The following strings denote other tokens:
1280 835
1281 <pre> 836 <pre>
1282 + - * / % ^ # 837 + - * / % ^ #
1283 == ~= &lt;= &gt;= &lt; &gt; = 838 == ~= &lt;= &gt;= &lt; &gt; =
1284 ( ) { } [ ] :: 839 ( ) { } [ ]
1285 ; : , . .. ... 840 ; : , . .. ...
1286 </pre> 841 </pre>
1287 842
1288 <p> 843 <p>
1289 <em>Literal strings</em> 844 <em>Literal strings</em>
1342 (carriage return, newline, carriage return followed by newline, 897 (carriage return, newline, carriage return followed by newline,
1343 or newline followed by carriage return) 898 or newline followed by carriage return)
1344 is converted to a simple newline. 899 is converted to a simple newline.
1345 900
1346 901
1347 <p>
1348 Any byte in a literal string not
1349 explicitly affected by the previous rules represents itself.
1350 However, Lua opens files for parsing in text mode,
1351 and the system file functions may have problems with
1352 some control characters.
1353 So, it is safer to represent
1354 non-text data as a quoted literal with
1355 explicit escape sequences for non-text characters.
1356
1357 902
1358 <p> 903 <p>
1359 For convenience, 904 For convenience,
1360 when the opening long bracket is immediately followed by a newline, 905 when the opening long bracket is immediately followed by a newline,
1361 the newline is not included in the string. 906 the newline is not included in the string.
1442 <pre> 987 <pre>
1443 var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; 988 var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo;
1444 </pre><p> 989 </pre><p>
1445 The meaning of accesses to table fields can be changed via metatables. 990 The meaning of accesses to table fields can be changed via metatables.
1446 An access to an indexed variable <code>t[i]</code> is equivalent to 991 An access to an indexed variable <code>t[i]</code> is equivalent to
1447 a call <code>gettable_event(t,i)</code>. 992 a call <code>get_table_event(t,i)</code>.
1448 (See <a href="#2.4">&sect;2.4</a> for a complete description of the 993 (See <a href="#2.4">&sect;2.4</a> for a complete description of the
1449 <code>gettable_event</code> function. 994 <code>get_table_event</code> function.
1450 This function is not defined or callable in Lua. 995 This function is not defined or callable in Lua.
1451 We use it here only for explanatory purposes.) 996 We use it here only for explanatory purposes.)
1452 997
1453 998
1454 <p> 999 <p>
1496 1041
1497 <pre> 1042 <pre>
1498 stat ::= &lsquo;<b>;</b>&rsquo; 1043 stat ::= &lsquo;<b>;</b>&rsquo;
1499 </pre> 1044 </pre>
1500 1045
1501 <p>
1502 Function calls and assignments
1503 can start with an open parenthesis.
1504 This possibility leads to an ambiguity in Lua's grammar.
1505 Consider the following fragment:
1506
1507 <pre>
1508 a = b + c
1509 (print or io.write)('done')
1510 </pre><p>
1511 The grammar could see it in two ways:
1512
1513 <pre>
1514 a = b + c(print or io.write)('done')
1515
1516 a = b + c; (print or io.write)('done')
1517 </pre><p>
1518 The current parser always sees such constructions
1519 in the first way,
1520 interpreting the open parenthesis
1521 as the start of the arguments to a call.
1522 To avoid this ambiguity,
1523 it is a good practice to always precede with a semicolon
1524 statements that start with a parenthesis:
1525
1526 <pre>
1527 ;(print or io.write)('done')
1528 </pre>
1529 1046
1530 <p> 1047 <p>
1531 A block can be explicitly delimited to produce a single statement: 1048 A block can be explicitly delimited to produce a single statement:
1532 1049
1533 <pre> 1050 <pre>
1534 stat ::= <b>do</b> block <b>end</b> 1051 stat ::= <b>do</b> block <b>end</b>
1535 </pre><p> 1052 </pre><p>
1536 Explicit blocks are useful 1053 Explicit blocks are useful
1537 to control the scope of variable declarations. 1054 to control the scope of variable declarations.
1538 Explicit blocks are also sometimes used to
1539 add a <b>return</b> statement in the middle
1540 of another block (see <a href="#3.3.4">&sect;3.3.4</a>).
1541 1055
1542 1056
1543 1057
1544 1058
1545 1059
1570 A chunk can be stored in a file or in a string inside the host program. 1084 A chunk can be stored in a file or in a string inside the host program.
1571 To execute a chunk, 1085 To execute a chunk,
1572 Lua first precompiles the chunk into instructions for a virtual machine, 1086 Lua first precompiles the chunk into instructions for a virtual machine,
1573 and then it executes the compiled code 1087 and then it executes the compiled code
1574 with an interpreter for the virtual machine. 1088 with an interpreter for the virtual machine.
1575
1576
1577 <p>
1578 Chunks can also be precompiled into binary form;
1579 see program <code>luac</code> for details.
1580 Programs in source and compiled forms are interchangeable;
1581 Lua automatically detects the file type and acts accordingly.
1582 1089
1583 1090
1584 1091
1585 1092
1586 1093
1673 <pre> 1180 <pre>
1674 stat ::= <b>while</b> exp <b>do</b> block <b>end</b> 1181 stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
1675 stat ::= <b>repeat</b> block <b>until</b> exp 1182 stat ::= <b>repeat</b> block <b>until</b> exp
1676 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> 1183 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
1677 </pre><p> 1184 </pre><p>
1678 Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">&sect;3.3.5</a>). 1185 Lua also has a <b>for</b> statement (see <a href="#3.3.5">&sect;3.3.5</a>).
1679 1186
1680 1187
1681 <p> 1188 <p>
1682 The condition expression of a 1189 The condition expression of a
1683 control structure can return any value. 1190 control structure must return a boolean.
1684 Both <b>false</b> and <b>nil</b> are considered false. 1191 This is unlike Lua and is intended to catch programming errors more quickly.
1685 All values different from <b>nil</b> and <b>false</b> are considered true
1686 (in particular, the number 0 and the empty string are also true).
1687 1192
1688 1193
1689 <p> 1194 <p>
1690 In the <b>repeat</b>&ndash;<b>until</b> loop, 1195 In the <b>repeat</b>&ndash;<b>until</b> loop,
1691 the inner block does not end at the <b>until</b> keyword, 1196 the inner block does not end at the <b>until</b> keyword,
1692 but only after the condition. 1197 but only after the condition.
1693 So, the condition can refer to local variables 1198 So, the condition can refer to local variables
1694 declared inside the loop block. 1199 declared inside the loop block.
1695 1200
1696 1201
1697 <p>
1698 The <b>goto</b> statement transfers the program control to a label.
1699 For syntactical reasons,
1700 labels in Lua are considered statements too:
1701
1702
1703
1704 <pre>
1705 stat ::= <b>goto</b> Name
1706 stat ::= label
1707 label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
1708 </pre>
1709
1710 <p>
1711 A label is visible in the entire block where it is defined,
1712 except
1713 inside nested blocks where a label with the same name is defined and
1714 inside nested functions.
1715 A goto may jump to any visible label as long as it does not
1716 enter into the scope of a local variable.
1717
1718
1719 <p>
1720 Labels and empty statements are called <em>void statements</em>,
1721 as they perform no actions.
1722 1202
1723 1203
1724 <p> 1204 <p>
1725 The <b>break</b> statement terminates the execution of a 1205 The <b>break</b> statement terminates the execution of a
1726 <b>while</b>, <b>repeat</b>, or <b>for</b> loop, 1206 <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
1742 1222
1743 <pre> 1223 <pre>
1744 stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;] 1224 stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
1745 </pre> 1225 </pre>
1746 1226
1747 <p>
1748 The <b>return</b> statement can only be written
1749 as the last statement of a block.
1750 If it is really necessary to <b>return</b> in the middle of a block,
1751 then an explicit inner block can be used,
1752 as in the idiom <code>do return end</code>,
1753 because now <b>return</b> is the last statement in its (inner) block.
1754
1755 1227
1756 1228
1757 1229
1758 1230
1759 <h3>3.3.5 &ndash; <a name="3.3.5">For Statement</a></h3> 1231 <h3>3.3.5 &ndash; <a name="3.3.5">For Statement</a></h3>
1760 1232
1761 <p> 1233
1762 1234 <p>
1763 The <b>for</b> statement has two forms: 1235 The <b>for</b> statement works over functions,
1764 one numeric and one generic.
1765
1766
1767 <p>
1768 The numeric <b>for</b> loop repeats a block of code while a
1769 control variable runs through an arithmetic progression.
1770 It has the following syntax:
1771
1772 <pre>
1773 stat ::= <b>for</b> Name &lsquo;<b>=</b>&rsquo; exp &lsquo;<b>,</b>&rsquo; exp [&lsquo;<b>,</b>&rsquo; exp] <b>do</b> block <b>end</b>
1774 </pre><p>
1775 The <em>block</em> is repeated for <em>name</em> starting at the value of
1776 the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
1777 third <em>exp</em>.
1778 More precisely, a <b>for</b> statement like
1779
1780 <pre>
1781 for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end
1782 </pre><p>
1783 is equivalent to the code:
1784
1785 <pre>
1786 do
1787 local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
1788 if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
1789 while (<em>step</em> &gt; 0 and <em>var</em> &lt;= <em>limit</em>) or (<em>step</em> &lt;= 0 and <em>var</em> &gt;= <em>limit</em>) do
1790 local v = <em>var</em>
1791 <em>block</em>
1792 <em>var</em> = <em>var</em> + <em>step</em>
1793 end
1794 end
1795 </pre><p>
1796 Note the following:
1797
1798 <ul>
1799
1800 <li>
1801 All three control expressions are evaluated only once,
1802 before the loop starts.
1803 They must all result in numbers.
1804 </li>
1805
1806 <li>
1807 <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
1808 The names shown here are for explanatory purposes only.
1809 </li>
1810
1811 <li>
1812 If the third expression (the step) is absent,
1813 then a step of&nbsp;1 is used.
1814 </li>
1815
1816 <li>
1817 You can use <b>break</b> to exit a <b>for</b> loop.
1818 </li>
1819
1820 <li>
1821 The loop variable <code>v</code> is local to the loop;
1822 you cannot use its value after the <b>for</b> ends or is broken.
1823 If you need this value,
1824 assign it to another variable before breaking or exiting the loop.
1825 </li>
1826
1827 </ul>
1828
1829 <p>
1830 The generic <b>for</b> statement works over functions,
1831 called <em>iterators</em>. 1236 called <em>iterators</em>.
1832 On each iteration, the iterator function is called to produce a new value, 1237 On each iteration, the iterator function is called to produce a new value,
1833 stopping when this new value is <b>nil</b>. 1238 stopping when this new value is <b>nil</b>.
1834 The generic <b>for</b> loop has the following syntax: 1239 The <b>for</b> loop has the following syntax:
1835 1240
1836 <pre> 1241 <pre>
1837 stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> 1242 stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
1838 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name} 1243 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
1839 </pre><p> 1244 </pre><p>
1840 A <b>for</b> statement like 1245 A <b>for</b> statement like
1841 1246
1842 <pre> 1247 <pre>
1843 for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>explist</em> do <em>block</em> end 1248 for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>expression</em> do <em>block</em> end
1844 </pre><p> 1249 </pre><p>
1845 is equivalent to the code: 1250 is equivalent to the code:
1846 1251
1847 <pre> 1252 <pre>
1848 do 1253 do
1849 local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em> 1254 local <em>f</em> = <em>expression</em>
1850 while true do 1255 while true do
1851 local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>) 1256 local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>()
1852 if <em>var_1</em> == nil then break end 1257 if <em>var_1</em> == nil then break end
1853 <em>var</em> = <em>var_1</em>
1854 <em>block</em> 1258 <em>block</em>
1855 end 1259 end
1856 end 1260 end
1857 </pre><p> 1261 </pre><p>
1858 Note the following: 1262 Note the following:
1859 1263
1860 <ul> 1264 <ul>
1861 1265
1862 <li> 1266 <li>
1863 <code><em>explist</em></code> is evaluated only once. 1267 <code><em>expression</em></code> is evaluated only once.
1864 Its results are an <em>iterator</em> function, 1268 Its result is an <em>iterator</em> function.
1865 a <em>state</em>,
1866 and an initial value for the first <em>iterator variable</em>.
1867 </li> 1269 </li>
1868 1270
1869 <li> 1271 <li>
1870 <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables. 1272 <code><em>f</em></code> is an invisible variable.
1871 The names are here for explanatory purposes only. 1273 The name is here for explanatory purposes only.
1872 </li> 1274 </li>
1873 1275
1874 <li> 1276 <li>
1875 You can use <b>break</b> to exit a <b>for</b> loop. 1277 You can use <b>break</b> to exit a <b>for</b> loop.
1876 </li> 1278 </li>
1882 then assign them to other variables before breaking or exiting the loop. 1284 then assign them to other variables before breaking or exiting the loop.
1883 </li> 1285 </li>
1884 1286
1885 </ul> 1287 </ul>
1886 1288
1887 1289 <p>
1290 Lua also has a numeric <b>for</b> statement which Luan does not support. Instead, Luan offers the <em>range</em> function (inspired by Python) which does the same thing without adding to the syntax of the language.
1888 1291
1889 1292
1890 <h3>3.3.6 &ndash; <a name="3.3.6">Function Calls as Statements</a></h3><p> 1293 <h3>3.3.6 &ndash; <a name="3.3.6">Function Calls as Statements</a></h3><p>
1891 To allow possible side-effects, 1294 To allow possible side-effects,
1892 function calls can be executed as statements: 1295 function calls can be executed as statements:
1910 </pre><p> 1313 </pre><p>
1911 If present, an initial assignment has the same semantics 1314 If present, an initial assignment has the same semantics
1912 of a multiple assignment (see <a href="#3.3.3">&sect;3.3.3</a>). 1315 of a multiple assignment (see <a href="#3.3.3">&sect;3.3.3</a>).
1913 Otherwise, all variables are initialized with <b>nil</b>. 1316 Otherwise, all variables are initialized with <b>nil</b>.
1914 1317
1915
1916 <p> 1318 <p>
1917 A chunk is also a block (see <a href="#3.3.2">&sect;3.3.2</a>), 1319 A chunk is also a block (see <a href="#3.3.2">&sect;3.3.2</a>),
1918 and so local variables can be declared in a chunk outside any explicit block. 1320 and so local variables can be declared in a chunk outside any explicit block.
1919 1321
1920 1322
1921 <p> 1323 <p>
1922 The visibility rules for local variables are explained in <a href="#3.5">&sect;3.5</a>. 1324 The visibility rules for local variables are explained in <a href="#3.5">&sect;3.5</a>.
1923 1325
1924 1326
1925 1327
1328 <h3>3.3.8 &ndash; <a name="3.3.8">Or/And Statements</a></h3><p>
1329
1330 <p>
1331 An <b>or</b> or <b>and</b> expression is also considered a statement. This is new for Luan and doesn't exist in Lua.
1332
1333 <p>For example, consider a function <em>do_something</em> that returns a boolean indicating whether it succeeded or failed. You can then do:
1334
1335 <pre>
1336 do_something() or error "didn't work"
1337 </pre>
1338
1339
1926 1340
1927 1341
1928 1342
1929 1343
1930 <h2>3.4 &ndash; <a name="3.4">Expressions</a></h2> 1344 <h2>3.4 &ndash; <a name="3.4">Expressions</a></h2>
1931 1345
1932 <p> 1346 <p>
1933 The basic expressions in Lua are the following: 1347 The basic expressions in Luan are the following:
1934 1348
1935 <pre> 1349 <pre>
1936 exp ::= prefixexp 1350 exp ::= prefixexp
1937 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> 1351 exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
1938 exp ::= Number 1352 exp ::= Number
2012 or <b>nil</b> if <code>f</code> does not return any values.) 1426 or <b>nil</b> if <code>f</code> does not return any values.)
2013 1427
2014 1428
2015 1429
2016 <h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p> 1430 <h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>
2017 Lua supports the usual arithmetic operators: 1431 Luan supports the usual arithmetic operators:
2018 the binary <code>+</code> (addition), 1432 the binary <code>+</code> (addition),
2019 <code>-</code> (subtraction), <code>*</code> (multiplication), 1433 <code>-</code> (subtraction), <code>*</code> (multiplication),
2020 <code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation); 1434 <code>/</code> (division), <code>%</code> (modulo), and <code>^</code> (exponentiation);
2021 and unary <code>-</code> (mathematical negation). 1435 and unary <code>-</code> (mathematical negation).
2022 If the operands are numbers, or strings that can be converted to 1436 If the operands are numbers, or strings that can be converted to
2025 Exponentiation works for any exponent. 1439 Exponentiation works for any exponent.
2026 For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>. 1440 For instance, <code>x^(-0.5)</code> computes the inverse of the square root of <code>x</code>.
2027 Modulo is defined as 1441 Modulo is defined as
2028 1442
2029 <pre> 1443 <pre>
2030 a % b == a - math.floor(a/b)*b 1444 a % b == a - Math.floor(a/b)*b
2031 </pre><p> 1445 </pre><p>
2032 That is, it is the remainder of a division that rounds 1446 That is, it is the remainder of a division that rounds
2033 the quotient towards minus infinity. 1447 the quotient towards minus infinity.
2034 1448
2035 1449
2037 1451
2038 1452
2039 <h3>3.4.2 &ndash; <a name="3.4.2">Coercion</a></h3> 1453 <h3>3.4.2 &ndash; <a name="3.4.2">Coercion</a></h3>
2040 1454
2041 <p> 1455 <p>
2042 Lua provides automatic conversion between 1456 Luan provides automatic conversion between
2043 string and number values at run time. 1457 string and number values at run time.
2044 Any arithmetic operation applied to a string tries to convert 1458 Any arithmetic operation applied to a string tries to convert
2045 this string to a number, following the rules of the Lua lexer. 1459 this string to a number, following the rules of the Lua lexer.
2046 (The string may have leading and trailing spaces and a sign.) 1460 (The string may have leading and trailing spaces and a sign.)
2047 Conversely, whenever a number is used where a string is expected, 1461 Conversely, whenever a number is used where a string is expected,
2053 1467
2054 1468
2055 1469
2056 1470
2057 <h3>3.4.3 &ndash; <a name="3.4.3">Relational Operators</a></h3><p> 1471 <h3>3.4.3 &ndash; <a name="3.4.3">Relational Operators</a></h3><p>
2058 The relational operators in Lua are 1472 The relational operators in Luan are
2059 1473
2060 <pre> 1474 <pre>
2061 == ~= &lt; &gt; &lt;= &gt;= 1475 == ~= &lt; &gt; &lt;= &gt;=
2062 </pre><p> 1476 </pre><p>
2063 These operators always result in <b>false</b> or <b>true</b>. 1477 These operators always result in <b>false</b> or <b>true</b>.
2078 Closures with any detectable difference 1492 Closures with any detectable difference
2079 (different behavior, different definition) are always different. 1493 (different behavior, different definition) are always different.
2080 1494
2081 1495
2082 <p> 1496 <p>
2083 You can change the way that Lua compares tables and userdata 1497 You can change the way that Luan compares tables
2084 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>). 1498 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
2085 1499
2086 1500
2087 <p> 1501 <p>
2088 The conversion rules of <a href="#3.4.2">&sect;3.4.2</a> 1502 The conversion rules of <a href="#3.4.2">&sect;3.4.2</a>
2147 1561
2148 1562
2149 1563
2150 1564
2151 <h3>3.4.5 &ndash; <a name="3.4.5">Concatenation</a></h3><p> 1565 <h3>3.4.5 &ndash; <a name="3.4.5">Concatenation</a></h3><p>
2152 The string concatenation operator in Lua is 1566 The string concatenation operator in Luan is
2153 denoted by two dots ('<code>..</code>'). 1567 denoted by two dots ('<code>..</code>').
2154 If both operands are strings or numbers, then they are converted to 1568 If both operands are strings or numbers, then they are converted to
2155 strings according to the rules mentioned in <a href="#3.4.2">&sect;3.4.2</a>. 1569 strings according to the rules mentioned in <a href="#3.4.2">&sect;3.4.2</a>.
2156 Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>). 1570 Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>).
2157 1571
2196 1610
2197 1611
2198 1612
2199 1613
2200 <h3>3.4.7 &ndash; <a name="3.4.7">Precedence</a></h3><p> 1614 <h3>3.4.7 &ndash; <a name="3.4.7">Precedence</a></h3><p>
2201 Operator precedence in Lua follows the table below, 1615 Operator precedence in Luan follows the table below,
2202 from lower to higher priority: 1616 from lower to higher priority:
2203 1617
2204 <pre> 1618 <pre>
2205 or 1619 or
2206 and 1620 and
2279 1693
2280 1694
2281 1695
2282 1696
2283 <h3>3.4.9 &ndash; <a name="3.4.9">Function Calls</a></h3><p> 1697 <h3>3.4.9 &ndash; <a name="3.4.9">Function Calls</a></h3><p>
2284 A function call in Lua has the following syntax: 1698 A function call in Luan has the following syntax:
2285 1699
2286 <pre> 1700 <pre>
2287 functioncall ::= prefixexp args 1701 functioncall ::= prefixexp args
2288 </pre><p> 1702 </pre><p>
2289 In a function call, 1703 In a function call,
2296 followed by the original call arguments 1710 followed by the original call arguments
2297 (see <a href="#2.4">&sect;2.4</a>). 1711 (see <a href="#2.4">&sect;2.4</a>).
2298 1712
2299 1713
2300 <p> 1714 <p>
2301 The form 1715 Lua supports a special function call for "methods" like <em>obj:fn(args)</em> . Luan does not support this.
2302
2303 <pre>
2304 functioncall ::= prefixexp &lsquo;<b>:</b>&rsquo; Name args
2305 </pre><p>
2306 can be used to call "methods".
2307 A call <code>v:name(<em>args</em>)</code>
2308 is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
2309 except that <code>v</code> is evaluated only once.
2310
2311 1716
2312 <p> 1717 <p>
2313 Arguments have the following syntax: 1718 Arguments have the following syntax:
2314 1719
2315 <pre> 1720 <pre>
2328 1733
2329 1734
2330 <p> 1735 <p>
2331 A call of the form <code>return <em>functioncall</em></code> is called 1736 A call of the form <code>return <em>functioncall</em></code> is called
2332 a <em>tail call</em>. 1737 a <em>tail call</em>.
2333 Lua implements <em>proper tail calls</em> 1738 Luan implements <em>proper tail calls</em>
2334 (or <em>proper tail recursion</em>): 1739 (or <em>proper tail recursion</em>):
2335 in a tail call, 1740 in a tail call,
2336 the called function reuses the stack entry of the calling function. 1741 the called function reuses the stack entry of the calling function.
2337 Therefore, there is no limit on the number of nested tail calls that 1742 Therefore, there is no limit on the number of nested tail calls that
2338 a program can execute. 1743 a program can execute.
2369 The following syntactic sugar simplifies function definitions: 1774 The following syntactic sugar simplifies function definitions:
2370 1775
2371 <pre> 1776 <pre>
2372 stat ::= <b>function</b> funcname funcbody 1777 stat ::= <b>function</b> funcname funcbody
2373 stat ::= <b>local</b> <b>function</b> Name funcbody 1778 stat ::= <b>local</b> <b>function</b> Name funcbody
2374 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name] 1779 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name}
2375 </pre><p> 1780 </pre><p>
2376 The statement 1781 The statement
2377 1782
2378 <pre> 1783 <pre>
2379 function f () <em>body</em> end 1784 function f () <em>body</em> end
2413 1818
2414 1819
2415 <p> 1820 <p>
2416 A function definition is an executable expression, 1821 A function definition is an executable expression,
2417 whose value has type <em>function</em>. 1822 whose value has type <em>function</em>.
2418 When Lua precompiles a chunk, 1823 When Luan precompiles a chunk,
2419 all its function bodies are precompiled too. 1824 all its function bodies are precompiled too.
2420 Then, whenever Lua executes the function definition, 1825 Then, whenever Luan executes the function definition,
2421 the function is <em>instantiated</em> (or <em>closed</em>). 1826 the function is <em>instantiated</em> (or <em>closed</em>).
2422 This function instance (or <em>closure</em>) 1827 This function instance (or <em>closure</em>)
2423 is the final value of the expression. 1828 is the final value of the expression.
2424 1829
2425 1830
2481 If control reaches the end of a function 1886 If control reaches the end of a function
2482 without encountering a <b>return</b> statement, 1887 without encountering a <b>return</b> statement,
2483 then the function returns with no results. 1888 then the function returns with no results.
2484 1889
2485 1890
2486 <p>
2487
2488 There is a system-dependent limit on the number of values
2489 that a function may return.
2490 This limit is guaranteed to be larger than 1000.
2491
2492
2493 <p>
2494 The <em>colon</em> syntax
2495 is used for defining <em>methods</em>,
2496 that is, functions that have an implicit extra parameter <code>self</code>.
2497 Thus, the statement
2498
2499 <pre>
2500 function t.a.b.c:f (<em>params</em>) <em>body</em> end
2501 </pre><p>
2502 is syntactic sugar for
2503
2504 <pre>
2505 t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
2506 </pre>
2507
2508
2509
2510
2511 1891
2512 1892
2513 <h2>3.5 &ndash; <a name="3.5">Visibility Rules</a></h2> 1893 <h2>3.5 &ndash; <a name="3.5">Visibility Rules</a></h2>
2514 1894
2515 <p> 1895 <p>
2516 1896
2517 Lua is a lexically scoped language. 1897 Luan is a lexically scoped language.
2518 The scope of a local variable begins at the first statement after 1898 The scope of a local variable begins at the first statement after
2519 its declaration and lasts until the last non-void statement 1899 its declaration and lasts until the last non-void statement
2520 of the innermost block that includes the declaration. 1900 of the innermost block that includes the declaration.
2521 Consider the following example: 1901 Consider the following example:
2522 1902
2573 1953
2574 1954
2575 <h1>4 &ndash; <a name="4">The Application Program Interface</a></h1> 1955 <h1>4 &ndash; <a name="4">The Application Program Interface</a></h1>
2576 1956
2577 <p> 1957 <p>
2578 1958 In the Lua documentation,
2579 This section describes the C&nbsp;API for Lua, that is, 1959 this section described the C&nbsp;API for Lua.
2580 the set of C&nbsp;functions available to the host program to communicate 1960 Obviously this is not relevant for Luan.
2581 with Lua. 1961 The implementation of Luan is radically different from Lua and will be documented eventually in Javadoc.
2582 All API functions and related types and constants 1962 So this section is just a placeholder so that Luan documentation can match Lua's documentation.
2583 are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
2584
2585
2586 <p>
2587 Even when we use the term "function",
2588 any facility in the API may be provided as a macro instead.
2589 Except where stated otherwise,
2590 all such macros use each of their arguments exactly once
2591 (except for the first argument, which is always a Lua state),
2592 and so do not generate any hidden side-effects.
2593
2594
2595 <p>
2596 As in most C&nbsp;libraries,
2597 the Lua API functions do not check their arguments for validity or consistency.
2598 However, you can change this behavior by compiling Lua
2599 with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
2600
2601
2602
2603 <h2>4.1 &ndash; <a name="4.1">The Stack</a></h2>
2604
2605 <p>
2606 Lua uses a <em>virtual stack</em> to pass values to and from C.
2607 Each element in this stack represents a Lua value
2608 (<b>nil</b>, number, string, etc.).
2609
2610
2611 <p>
2612 Whenever Lua calls C, the called function gets a new stack,
2613 which is independent of previous stacks and of stacks of
2614 C&nbsp;functions that are still active.
2615 This stack initially contains any arguments to the C&nbsp;function
2616 and it is where the C&nbsp;function pushes its results
2617 to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
2618
2619
2620 <p>
2621 For convenience,
2622 most query operations in the API do not follow a strict stack discipline.
2623 Instead, they can refer to any element in the stack
2624 by using an <em>index</em>:
2625 A positive index represents an absolute stack position
2626 (starting at&nbsp;1);
2627 a negative index represents an offset relative to the top of the stack.
2628 More specifically, if the stack has <em>n</em> elements,
2629 then index&nbsp;1 represents the first element
2630 (that is, the element that was pushed onto the stack first)
2631 and
2632 index&nbsp;<em>n</em> represents the last element;
2633 index&nbsp;-1 also represents the last element
2634 (that is, the element at the&nbsp;top)
2635 and index <em>-n</em> represents the first element.
2636
2637
2638
2639
2640
2641 <h2>4.2 &ndash; <a name="4.2">Stack Size</a></h2>
2642
2643 <p>
2644 When you interact with the Lua API,
2645 you are responsible for ensuring consistency.
2646 In particular,
2647 <em>you are responsible for controlling stack overflow</em>.
2648 You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
2649 to ensure that the stack has extra slots when pushing new elements.
2650
2651
2652 <p>
2653 Whenever Lua calls C,
2654 it ensures that the stack has at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
2655 <code>LUA_MINSTACK</code> is defined as 20,
2656 so that usually you do not have to worry about stack space
2657 unless your code has loops pushing elements onto the stack.
2658
2659
2660 <p>
2661 When you call a Lua function
2662 without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
2663 Lua ensures that the stack has enough size for all results,
2664 but it does not ensure any extra space.
2665 So, before pushing anything in the stack after such a call
2666 you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2667
2668
2669
2670
2671
2672 <h2>4.3 &ndash; <a name="4.3">Valid and Acceptable Indices</a></h2>
2673
2674 <p>
2675 Any function in the API that receives stack indices
2676 works only with <em>valid indices</em> or <em>acceptable indices</em>.
2677
2678
2679 <p>
2680 A <em>valid index</em> is an index that refers to a
2681 real position within the stack, that is,
2682 its position lies between&nbsp;1 and the stack top
2683 (<code>1 &le; abs(index) &le; top</code>).
2684
2685 Usually, functions that can modify the value at an index
2686 require valid indices.
2687
2688
2689 <p>
2690 Unless otherwise noted,
2691 any function that accepts valid indices also accepts <em>pseudo-indices</em>,
2692 which represent some Lua values that are accessible to C&nbsp;code
2693 but which are not in the stack.
2694 Pseudo-indices are used to access the registry
2695 and the upvalues of a C&nbsp;function (see <a href="#4.4">&sect;4.4</a>).
2696
2697
2698 <p>
2699 Functions that do not need a specific stack position,
2700 but only a value in the stack (e.g., query functions),
2701 can be called with acceptable indices.
2702 An <em>acceptable index</em> can be any valid index,
2703 including the pseudo-indices,
2704 but it also can be any positive index after the stack top
2705 within the space allocated for the stack,
2706 that is, indices up to the stack size.
2707 (Note that 0 is never an acceptable index.)
2708 Except when noted otherwise,
2709 functions in the API work with acceptable indices.
2710
2711
2712 <p>
2713 Acceptable indices serve to avoid extra tests
2714 against the stack top when querying the stack.
2715 For instance, a C&nbsp;function can query its third argument
2716 without the need to first check whether there is a third argument,
2717 that is, without the need to check whether 3 is a valid index.
2718
2719
2720 <p>
2721 For functions that can be called with acceptable indices,
2722 any non-valid index is treated as if it
2723 contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
2724 which behaves like a nil value.
2725
2726
2727
2728
2729
2730 <h2>4.4 &ndash; <a name="4.4">C Closures</a></h2>
2731
2732 <p>
2733 When a C&nbsp;function is created,
2734 it is possible to associate some values with it,
2735 thus creating a <em>C&nbsp;closure</em>
2736 (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
2737 these values are called <em>upvalues</em> and are
2738 accessible to the function whenever it is called.
2739
2740
2741 <p>
2742 Whenever a C&nbsp;function is called,
2743 its upvalues are located at specific pseudo-indices.
2744 These pseudo-indices are produced by the macro
2745 <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
2746 The first value associated with a function is at position
2747 <code>lua_upvalueindex(1)</code>, and so on.
2748 Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
2749 where <em>n</em> is greater than the number of upvalues of the
2750 current function (but not greater than 256),
2751 produces an acceptable but invalid index.
2752
2753
2754
2755
2756
2757 <h2>4.5 &ndash; <a name="4.5">Registry</a></h2>
2758
2759 <p>
2760 Lua provides a <em>registry</em>,
2761 a predefined table that can be used by any C&nbsp;code to
2762 store whatever Lua values it needs to store.
2763 The registry table is always located at pseudo-index
2764 <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>,
2765 which is a valid index.
2766 Any C&nbsp;library can store data into this table,
2767 but it should take care to choose keys
2768 that are different from those used
2769 by other libraries, to avoid collisions.
2770 Typically, you should use as key a string containing your library name,
2771 or a light userdata with the address of a C&nbsp;object in your code,
2772 or any Lua object created by your code.
2773 As with global names,
2774 string keys starting with an underscore followed by
2775 uppercase letters are reserved for Lua.
2776
2777
2778 <p>
2779 The integer keys in the registry are used by the reference mechanism,
2780 implemented by the auxiliary library,
2781 and by some predefined values.
2782 Therefore, integer keys should not be used for other purposes.
2783
2784
2785 <p>
2786 When you create a new Lua state,
2787 its registry comes with some predefined values.
2788 These predefined values are indexed with integer keys
2789 defined as constants in <code>lua.h</code>.
2790 The following constants are defined:
2791
2792 <ul>
2793 <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has
2794 the main thread of the state.
2795 (The main thread is the one created together with the state.)
2796 </li>
2797
2798 <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has
2799 the global environment.
2800 </li>
2801 </ul>
2802
2803
2804
2805
2806 <h2>4.6 &ndash; <a name="4.6">Error Handling in C</a></h2>
2807
2808 <p>
2809 Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
2810 (You can also choose to use exceptions if you compile Lua as C++;
2811 search for <code>LUAI_THROW</code> in the source code.)
2812 When Lua faces any error
2813 (such as a memory allocation error, type errors, syntax errors,
2814 and runtime errors)
2815 it <em>raises</em> an error;
2816 that is, it does a long jump.
2817 A <em>protected environment</em> uses <code>setjmp</code>
2818 to set a recovery point;
2819 any error jumps to the most recent active recovery point.
2820
2821
2822 <p>
2823 If an error happens outside any protected environment,
2824 Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
2825 and then calls <code>abort</code>,
2826 thus exiting the host application.
2827 Your panic function can avoid this exit by
2828 never returning
2829 (e.g., doing a long jump to your own recovery point outside Lua).
2830
2831
2832 <p>
2833 The panic function runs as if it were a message handler (see <a href="#2.3">&sect;2.3</a>);
2834 in particular, the error message is at the top of the stack.
2835 However, there is no guarantees about stack space.
2836 To push anything on the stack,
2837 the panic function should first check the available space (see <a href="#4.2">&sect;4.2</a>).
2838
2839
2840 <p>
2841 Most functions in the API can throw an error,
2842 for instance due to a memory allocation error.
2843 The documentation for each function indicates whether
2844 it can throw errors.
2845
2846
2847 <p>
2848 Inside a C&nbsp;function you can throw an error by calling <a href="#lua_error"><code>lua_error</code></a>.
2849
2850
2851
2852
2853
2854 <h2>4.7 &ndash; <a name="4.7">Handling Yields in C</a></h2>
2855
2856 <p>
2857 Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2858 Therefore, if a function <code>foo</code> calls an API function
2859 and this API function yields
2860 (directly or indirectly by calling another function that yields),
2861 Lua cannot return to <code>foo</code> any more,
2862 because the <code>longjmp</code> removes its frame from the C stack.
2863
2864
2865 <p>
2866 To avoid this kind of problem,
2867 Lua raises an error whenever it tries to yield across an API call,
2868 except for three functions:
2869 <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>.
2870 All those functions receive a <em>continuation function</em>
2871 (as a parameter called <code>k</code>) to continue execution after a yield.
2872
2873
2874 <p>
2875 We need to set some terminology to explain continuations.
2876 We have a C function called from Lua which we will call
2877 the <em>original function</em>.
2878 This original function then calls one of those three functions in the C API,
2879 which we will call the <em>callee function</em>,
2880 that then yields the current thread.
2881 (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
2882 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>
2883 and the function called by them yields.)
2884
2885
2886 <p>
2887 Suppose the running thread yields while executing the callee function.
2888 After the thread resumes,
2889 it eventually will finish running the callee function.
2890 However,
2891 the callee function cannot return to the original function,
2892 because its frame in the C stack was destroyed by the yield.
2893 Instead, Lua calls a <em>continuation function</em>,
2894 which was given as an argument to the callee function.
2895 As the name implies,
2896 the continuation function should continue the task
2897 of the original function.
2898
2899
2900 <p>
2901 Lua treats the continuation function as if it were the original function.
2902 The continuation function receives the same Lua stack
2903 from the original function,
2904 in the same state it would be if the callee function had returned.
2905 (For instance,
2906 after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
2907 removed from the stack and replaced by the results from the call.)
2908 It also has the same upvalues.
2909 Whatever it returns is handled by Lua as if it were the return
2910 of the original function.
2911
2912
2913 <p>
2914 The only difference in the Lua state between the original function
2915 and its continuation is the result of a call to <a href="#lua_getctx"><code>lua_getctx</code></a>.
2916
2917
2918
2919
2920
2921 <h2>4.8 &ndash; <a name="4.8">Functions and Types</a></h2>
2922
2923 <p>
2924 Here we list all functions and types from the C&nbsp;API in
2925 alphabetical order.
2926 Each function has an indicator like this:
2927 <span class="apii">[-o, +p, <em>x</em>]</span>
2928
2929
2930 <p>
2931 The first field, <code>o</code>,
2932 is how many elements the function pops from the stack.
2933 The second field, <code>p</code>,
2934 is how many elements the function pushes onto the stack.
2935 (Any function always pushes its results after popping its arguments.)
2936 A field in the form <code>x|y</code> means the function can push (or pop)
2937 <code>x</code> or <code>y</code> elements,
2938 depending on the situation;
2939 an interrogation mark '<code>?</code>' means that
2940 we cannot know how many elements the function pops/pushes
2941 by looking only at its arguments
2942 (e.g., they may depend on what is on the stack).
2943 The third field, <code>x</code>,
2944 tells whether the function may throw errors:
2945 '<code>-</code>' means the function never throws any error;
2946 '<code>e</code>' means the function may throw errors;
2947 '<code>v</code>' means the function may throw an error on purpose.
2948
2949
2950
2951 <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
2952 <span class="apii">[-0, +0, &ndash;]</span>
2953 <pre>int lua_absindex (lua_State *L, int idx);</pre>
2954
2955 <p>
2956 Converts the acceptable index <code>idx</code> into an absolute index
2957 (that is, one that does not depend on the stack top).
2958
2959
2960
2961
2962
2963 <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
2964 <pre>typedef void * (*lua_Alloc) (void *ud,
2965 void *ptr,
2966 size_t osize,
2967 size_t nsize);</pre>
2968
2969 <p>
2970 The type of the memory-allocation function used by Lua states.
2971 The allocator function must provide a
2972 functionality similar to <code>realloc</code>,
2973 but not exactly the same.
2974 Its arguments are
2975 <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
2976 <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
2977 <code>osize</code>, the original size of the block or some code about what
2978 is being allocated;
2979 <code>nsize</code>, the new size of the block.
2980
2981
2982 <p>
2983 When <code>ptr</code> is not <code>NULL</code>,
2984 <code>osize</code> is the size of the block pointed by <code>ptr</code>,
2985 that is, the size given when it was allocated or reallocated.
2986
2987
2988 <p>
2989 When <code>ptr</code> is <code>NULL</code>,
2990 <code>osize</code> encodes the kind of object that Lua is allocating.
2991 <code>osize</code> is any of
2992 <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>,
2993 <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)
2994 Lua is creating a new object of that type.
2995 When <code>osize</code> is some other value,
2996 Lua is allocating memory for something else.
2997
2998
2999 <p>
3000 Lua assumes the following behavior from the allocator function:
3001
3002
3003 <p>
3004 When <code>nsize</code> is zero,
3005 the allocator should behave like <code>free</code>
3006 and return <code>NULL</code>.
3007
3008
3009 <p>
3010 When <code>nsize</code> is not zero,
3011 the allocator should behave like <code>realloc</code>.
3012 The allocator returns <code>NULL</code>
3013 if and only if it cannot fulfill the request.
3014 Lua assumes that the allocator never fails when
3015 <code>osize &gt;= nsize</code>.
3016
3017
3018 <p>
3019 Here is a simple implementation for the allocator function.
3020 It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
3021
3022 <pre>
3023 static void *l_alloc (void *ud, void *ptr, size_t osize,
3024 size_t nsize) {
3025 (void)ud; (void)osize; /* not used */
3026 if (nsize == 0) {
3027 free(ptr);
3028 return NULL;
3029 }
3030 else
3031 return realloc(ptr, nsize);
3032 }
3033 </pre><p>
3034 Note that Standard&nbsp;C ensures
3035 that <code>free(NULL)</code> has no effect and that
3036 <code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>.
3037 This code assumes that <code>realloc</code> does not fail when shrinking a block.
3038 (Although Standard&nbsp;C does not ensure this behavior,
3039 it seems to be a safe assumption.)
3040
3041
3042
3043
3044
3045 <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
3046 <span class="apii">[-(2|1), +1, <em>e</em>]</span>
3047 <pre>void lua_arith (lua_State *L, int op);</pre>
3048
3049 <p>
3050 Performs an arithmetic operation over the two values
3051 (or one, in the case of negation)
3052 at the top of the stack,
3053 with the value at the top being the second operand,
3054 pops these values, and pushes the result of the operation.
3055 The function follows the semantics of the corresponding Lua operator
3056 (that is, it may call metamethods).
3057
3058
3059 <p>
3060 The value of <code>op</code> must be one of the following constants:
3061
3062 <ul>
3063
3064 <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
3065 <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
3066 <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
3067 <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs division (<code>/</code>)</li>
3068 <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
3069 <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
3070 <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li>
3071
3072 </ul>
3073
3074
3075
3076
3077 <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
3078 <span class="apii">[-0, +0, &ndash;]</span>
3079 <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
3080
3081 <p>
3082 Sets a new panic function and returns the old one (see <a href="#4.6">&sect;4.6</a>).
3083
3084
3085
3086
3087
3088 <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
3089 <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
3090 <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
3091
3092 <p>
3093 Calls a function.
3094
3095
3096 <p>
3097 To call a function you must use the following protocol:
3098 first, the function to be called is pushed onto the stack;
3099 then, the arguments to the function are pushed
3100 in direct order;
3101 that is, the first argument is pushed first.
3102 Finally you call <a href="#lua_call"><code>lua_call</code></a>;
3103 <code>nargs</code> is the number of arguments that you pushed onto the stack.
3104 All arguments and the function value are popped from the stack
3105 when the function is called.
3106 The function results are pushed onto the stack when the function returns.
3107 The number of results is adjusted to <code>nresults</code>,
3108 unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
3109 In this case, all results from the function are pushed.
3110 Lua takes care that the returned values fit into the stack space.
3111 The function results are pushed onto the stack in direct order
3112 (the first result is pushed first),
3113 so that after the call the last result is on the top of the stack.
3114
3115
3116 <p>
3117 Any error inside the called function is propagated upwards
3118 (with a <code>longjmp</code>).
3119
3120
3121 <p>
3122 The following example shows how the host program can do the
3123 equivalent to this Lua code:
3124
3125 <pre>
3126 a = f("how", t.x, 14)
3127 </pre><p>
3128 Here it is in&nbsp;C:
3129
3130 <pre>
3131 lua_getglobal(L, "f"); /* function to be called */
3132 lua_pushstring(L, "how"); /* 1st argument */
3133 lua_getglobal(L, "t"); /* table to be indexed */
3134 lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
3135 lua_remove(L, -2); /* remove 't' from the stack */
3136 lua_pushinteger(L, 14); /* 3rd argument */
3137 lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
3138 lua_setglobal(L, "a"); /* set global 'a' */
3139 </pre><p>
3140 Note that the code above is "balanced":
3141 at its end, the stack is back to its original configuration.
3142 This is considered good programming practice.
3143
3144
3145
3146
3147
3148 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
3149 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
3150 <pre>void lua_callk (lua_State *L, int nargs, int nresults, int ctx,
3151 lua_CFunction k);</pre>
3152
3153 <p>
3154 This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
3155 but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
3156
3157
3158
3159
3160
3161 <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
3162 <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
3163
3164 <p>
3165 Type for C&nbsp;functions.
3166
3167
3168 <p>
3169 In order to communicate properly with Lua,
3170 a C&nbsp;function must use the following protocol,
3171 which defines the way parameters and results are passed:
3172 a C&nbsp;function receives its arguments from Lua in its stack
3173 in direct order (the first argument is pushed first).
3174 So, when the function starts,
3175 <code>lua_gettop(L)</code> returns the number of arguments received by the function.
3176 The first argument (if any) is at index 1
3177 and its last argument is at index <code>lua_gettop(L)</code>.
3178 To return values to Lua, a C&nbsp;function just pushes them onto the stack,
3179 in direct order (the first result is pushed first),
3180 and returns the number of results.
3181 Any other value in the stack below the results will be properly
3182 discarded by Lua.
3183 Like a Lua function, a C&nbsp;function called by Lua can also return
3184 many results.
3185
3186
3187 <p>
3188 As an example, the following function receives a variable number
3189 of numerical arguments and returns their average and sum:
3190
3191 <pre>
3192 static int foo (lua_State *L) {
3193 int n = lua_gettop(L); /* number of arguments */
3194 lua_Number sum = 0;
3195 int i;
3196 for (i = 1; i &lt;= n; i++) {
3197 if (!lua_isnumber(L, i)) {
3198 lua_pushstring(L, "incorrect argument");
3199 lua_error(L);
3200 }
3201 sum += lua_tonumber(L, i);
3202 }
3203 lua_pushnumber(L, sum/n); /* first result */
3204 lua_pushnumber(L, sum); /* second result */
3205 return 2; /* number of results */
3206 }
3207 </pre>
3208
3209
3210
3211
3212 <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
3213 <span class="apii">[-0, +0, &ndash;]</span>
3214 <pre>int lua_checkstack (lua_State *L, int extra);</pre>
3215
3216 <p>
3217 Ensures that there are at least <code>extra</code> free stack slots in the stack.
3218 It returns false if it cannot fulfill the request,
3219 because it would cause the stack to be larger than a fixed maximum size
3220 (typically at least a few thousand elements) or
3221 because it cannot allocate memory for the new stack size.
3222 This function never shrinks the stack;
3223 if the stack is already larger than the new size,
3224 it is left unchanged.
3225
3226
3227
3228
3229
3230 <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
3231 <span class="apii">[-0, +0, &ndash;]</span>
3232 <pre>void lua_close (lua_State *L);</pre>
3233
3234 <p>
3235 Destroys all objects in the given Lua state
3236 (calling the corresponding garbage-collection metamethods, if any)
3237 and frees all dynamic memory used by this state.
3238 On several platforms, you may not need to call this function,
3239 because all resources are naturally released when the host program ends.
3240 On the other hand, long-running programs that create multiple states,
3241 such as daemons or web servers,
3242 might need to close states as soon as they are not needed.
3243
3244
3245
3246
3247
3248 <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
3249 <span class="apii">[-0, +0, <em>e</em>]</span>
3250 <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre>
3251
3252 <p>
3253 Compares two Lua values.
3254 Returns 1 if the value at index <code>index1</code> satisfies <code>op</code>
3255 when compared with the value at index <code>index2</code>,
3256 following the semantics of the corresponding Lua operator
3257 (that is, it may call metamethods).
3258 Otherwise returns&nbsp;0.
3259 Also returns&nbsp;0 if any of the indices is non valid.
3260
3261
3262 <p>
3263 The value of <code>op</code> must be one of the following constants:
3264
3265 <ul>
3266
3267 <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li>
3268 <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code>&lt;</code>)</li>
3269 <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code>&lt;=</code>)</li>
3270
3271 </ul>
3272
3273
3274
3275
3276 <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
3277 <span class="apii">[-n, +1, <em>e</em>]</span>
3278 <pre>void lua_concat (lua_State *L, int n);</pre>
3279
3280 <p>
3281 Concatenates the <code>n</code> values at the top of the stack,
3282 pops them, and leaves the result at the top.
3283 If <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack
3284 (that is, the function does nothing);
3285 if <code>n</code> is 0, the result is the empty string.
3286 Concatenation is performed following the usual semantics of Lua
3287 (see <a href="#3.4.5">&sect;3.4.5</a>).
3288
3289
3290
3291
3292
3293 <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
3294 <span class="apii">[-0, +0, &ndash;]</span>
3295 <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
3296
3297 <p>
3298 Moves the element at index <code>fromidx</code>
3299 into the valid index <code>toidx</code>
3300 without shifting any element
3301 (therefore replacing the value at that position).
3302
3303
3304
3305
3306
3307 <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
3308 <span class="apii">[-0, +1, <em>e</em>]</span>
3309 <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
3310
3311 <p>
3312 Creates a new empty table and pushes it onto the stack.
3313 Parameter <code>narr</code> is a hint for how many elements the table
3314 will have as a sequence;
3315 parameter <code>nrec</code> is a hint for how many other elements
3316 the table will have.
3317 Lua may use these hints to preallocate memory for the new table.
3318 This pre-allocation is useful for performance when you know in advance
3319 how many elements the table will have.
3320 Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
3321
3322
3323
3324
3325
3326 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
3327 <span class="apii">[-0, +0, <em>e</em>]</span>
3328 <pre>int lua_dump (lua_State *L, lua_Writer writer, void *data);</pre>
3329
3330 <p>
3331 Dumps a function as a binary chunk.
3332 Receives a Lua function on the top of the stack
3333 and produces a binary chunk that,
3334 if loaded again,
3335 results in a function equivalent to the one dumped.
3336 As it produces parts of the chunk,
3337 <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
3338 with the given <code>data</code>
3339 to write them.
3340
3341
3342 <p>
3343 The value returned is the error code returned by the last
3344 call to the writer;
3345 0&nbsp;means no errors.
3346
3347
3348 <p>
3349 This function does not pop the Lua function from the stack.
3350
3351
3352
3353
3354
3355 <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
3356 <span class="apii">[-1, +0, <em>v</em>]</span>
3357 <pre>int lua_error (lua_State *L);</pre>
3358
3359 <p>
3360 Generates a Lua error.
3361 The error message (which can actually be a Lua value of any type)
3362 must be on the stack top.
3363 This function does a long jump,
3364 and therefore never returns
3365 (see <a href="#luaL_error"><code>luaL_error</code></a>).
3366
3367
3368
3369
3370
3371 <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
3372 <span class="apii">[-0, +0, <em>e</em>]</span>
3373 <pre>int lua_gc (lua_State *L, int what, int data);</pre>
3374
3375 <p>
3376 Controls the garbage collector.
3377
3378
3379 <p>
3380 This function performs several tasks,
3381 according to the value of the parameter <code>what</code>:
3382
3383 <ul>
3384
3385 <li><b><code>LUA_GCSTOP</code>: </b>
3386 stops the garbage collector.
3387 </li>
3388
3389 <li><b><code>LUA_GCRESTART</code>: </b>
3390 restarts the garbage collector.
3391 </li>
3392
3393 <li><b><code>LUA_GCCOLLECT</code>: </b>
3394 performs a full garbage-collection cycle.
3395 </li>
3396
3397 <li><b><code>LUA_GCCOUNT</code>: </b>
3398 returns the current amount of memory (in Kbytes) in use by Lua.
3399 </li>
3400
3401 <li><b><code>LUA_GCCOUNTB</code>: </b>
3402 returns the remainder of dividing the current amount of bytes of
3403 memory in use by Lua by 1024.
3404 </li>
3405
3406 <li><b><code>LUA_GCSTEP</code>: </b>
3407 performs an incremental step of garbage collection.
3408 The step "size" is controlled by <code>data</code>
3409 (larger values mean more steps) in a non-specified way.
3410 If you want to control the step size
3411 you must experimentally tune the value of <code>data</code>.
3412 The function returns 1 if the step finished a
3413 garbage-collection cycle.
3414 </li>
3415
3416 <li><b><code>LUA_GCSETPAUSE</code>: </b>
3417 sets <code>data</code> as the new value
3418 for the <em>pause</em> of the collector (see <a href="#2.5">&sect;2.5</a>).
3419 The function returns the previous value of the pause.
3420 </li>
3421
3422 <li><b><code>LUA_GCSETSTEPMUL</code>: </b>
3423 sets <code>data</code> as the new value for the <em>step multiplier</em> of
3424 the collector (see <a href="#2.5">&sect;2.5</a>).
3425 The function returns the previous value of the step multiplier.
3426 </li>
3427
3428 <li><b><code>LUA_GCISRUNNING</code>: </b>
3429 returns a boolean that tells whether the collector is running
3430 (i.e., not stopped).
3431 </li>
3432
3433 <li><b><code>LUA_GCGEN</code>: </b>
3434 changes the collector to generational mode
3435 (see <a href="#2.5">&sect;2.5</a>).
3436 </li>
3437
3438 <li><b><code>LUA_GCINC</code>: </b>
3439 changes the collector to incremental mode.
3440 This is the default mode.
3441 </li>
3442
3443 </ul>
3444
3445 <p>
3446 For more details about these options,
3447 see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
3448
3449
3450
3451
3452
3453 <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
3454 <span class="apii">[-0, +0, &ndash;]</span>
3455 <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
3456
3457 <p>
3458 Returns the memory-allocation function of a given state.
3459 If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
3460 opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>.
3461
3462
3463
3464
3465
3466 <hr><h3><a name="lua_getctx"><code>lua_getctx</code></a></h3><p>
3467 <span class="apii">[-0, +0, &ndash;]</span>
3468 <pre>int lua_getctx (lua_State *L, int *ctx);</pre>
3469
3470 <p>
3471 This function is called by a continuation function (see <a href="#4.7">&sect;4.7</a>)
3472 to retrieve the status of the thread and a context information.
3473
3474
3475 <p>
3476 When called in the original function,
3477 <a href="#lua_getctx"><code>lua_getctx</code></a> always returns <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
3478 and does not change the value of its argument <code>ctx</code>.
3479 When called inside a continuation function,
3480 <a href="#lua_getctx"><code>lua_getctx</code></a> returns <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> and sets
3481 the value of <code>ctx</code> to be the context information
3482 (the value passed as the <code>ctx</code> argument
3483 to the callee together with the continuation function).
3484
3485
3486 <p>
3487 When the callee is <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3488 Lua may also call its continuation function
3489 to handle errors during the call.
3490 That is, upon an error in the function called by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3491 Lua may not return to the original function
3492 but instead may call the continuation function.
3493 In that case, a call to <a href="#lua_getctx"><code>lua_getctx</code></a> will return the error code
3494 (the value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>);
3495 the value of <code>ctx</code> will be set to the context information,
3496 as in the case of a yield.
3497
3498
3499
3500
3501
3502 <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
3503 <span class="apii">[-0, +1, <em>e</em>]</span>
3504 <pre>void lua_getfield (lua_State *L, int index, const char *k);</pre>
3505
3506 <p>
3507 Pushes onto the stack the value <code>t[k]</code>,
3508 where <code>t</code> is the value at the given index.
3509 As in Lua, this function may trigger a metamethod
3510 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
3511
3512
3513
3514
3515
3516 <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
3517 <span class="apii">[-0, +1, <em>e</em>]</span>
3518 <pre>void lua_getglobal (lua_State *L, const char *name);</pre>
3519
3520 <p>
3521 Pushes onto the stack the value of the global <code>name</code>.
3522
3523
3524
3525
3526
3527 <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
3528 <span class="apii">[-0, +(0|1), &ndash;]</span>
3529 <pre>int lua_getmetatable (lua_State *L, int index);</pre>
3530
3531 <p>
3532 Pushes onto the stack the metatable of the value at the given index.
3533 If the value does not have a metatable,
3534 the function returns&nbsp;0 and pushes nothing on the stack.
3535
3536
3537
3538
3539
3540 <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
3541 <span class="apii">[-1, +1, <em>e</em>]</span>
3542 <pre>void lua_gettable (lua_State *L, int index);</pre>
3543
3544 <p>
3545 Pushes onto the stack the value <code>t[k]</code>,
3546 where <code>t</code> is the value at the given index
3547 and <code>k</code> is the value at the top of the stack.
3548
3549
3550 <p>
3551 This function pops the key from the stack
3552 (putting the resulting value in its place).
3553 As in Lua, this function may trigger a metamethod
3554 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
3555
3556
3557
3558
3559
3560 <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
3561 <span class="apii">[-0, +0, &ndash;]</span>
3562 <pre>int lua_gettop (lua_State *L);</pre>
3563
3564 <p>
3565 Returns the index of the top element in the stack.
3566 Because indices start at&nbsp;1,
3567 this result is equal to the number of elements in the stack
3568 (and so 0&nbsp;means an empty stack).
3569
3570
3571
3572
3573
3574 <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
3575 <span class="apii">[-0, +1, &ndash;]</span>
3576 <pre>void lua_getuservalue (lua_State *L, int index);</pre>
3577
3578 <p>
3579 Pushes onto the stack the Lua value associated with the userdata
3580 at the given index.
3581 This Lua value must be a table or <b>nil</b>.
3582
3583
3584
3585
3586
3587 <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
3588 <span class="apii">[-1, +1, &ndash;]</span>
3589 <pre>void lua_insert (lua_State *L, int index);</pre>
3590
3591 <p>
3592 Moves the top element into the given valid index,
3593 shifting up the elements above this index to open space.
3594 This function cannot be called with a pseudo-index,
3595 because a pseudo-index is not an actual stack position.
3596
3597
3598
3599
3600
3601 <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
3602 <pre>typedef ptrdiff_t lua_Integer;</pre>
3603
3604 <p>
3605 The type used by the Lua API to represent signed integral values.
3606
3607
3608 <p>
3609 By default it is a <code>ptrdiff_t</code>,
3610 which is usually the largest signed integral type the machine handles
3611 "comfortably".
3612
3613
3614
3615
3616
3617 <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
3618 <span class="apii">[-0, +0, &ndash;]</span>
3619 <pre>int lua_isboolean (lua_State *L, int index);</pre>
3620
3621 <p>
3622 Returns 1 if the value at the given index is a boolean,
3623 and 0&nbsp;otherwise.
3624
3625
3626
3627
3628
3629 <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
3630 <span class="apii">[-0, +0, &ndash;]</span>
3631 <pre>int lua_iscfunction (lua_State *L, int index);</pre>
3632
3633 <p>
3634 Returns 1 if the value at the given index is a C&nbsp;function,
3635 and 0&nbsp;otherwise.
3636
3637
3638
3639
3640
3641 <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
3642 <span class="apii">[-0, +0, &ndash;]</span>
3643 <pre>int lua_isfunction (lua_State *L, int index);</pre>
3644
3645 <p>
3646 Returns 1 if the value at the given index is a function
3647 (either C or Lua), and 0&nbsp;otherwise.
3648
3649
3650
3651
3652
3653 <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
3654 <span class="apii">[-0, +0, &ndash;]</span>
3655 <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
3656
3657 <p>
3658 Returns 1 if the value at the given index is a light userdata,
3659 and 0&nbsp;otherwise.
3660
3661
3662
3663
3664
3665 <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
3666 <span class="apii">[-0, +0, &ndash;]</span>
3667 <pre>int lua_isnil (lua_State *L, int index);</pre>
3668
3669 <p>
3670 Returns 1 if the value at the given index is <b>nil</b>,
3671 and 0&nbsp;otherwise.
3672
3673
3674
3675
3676
3677 <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
3678 <span class="apii">[-0, +0, &ndash;]</span>
3679 <pre>int lua_isnone (lua_State *L, int index);</pre>
3680
3681 <p>
3682 Returns 1 if the given index is not valid,
3683 and 0&nbsp;otherwise.
3684
3685
3686
3687
3688
3689 <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
3690 <span class="apii">[-0, +0, &ndash;]</span>
3691 <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
3692
3693 <p>
3694 Returns 1 if the given index is not valid
3695 or if the value at this index is <b>nil</b>,
3696 and 0&nbsp;otherwise.
3697
3698
3699
3700
3701
3702 <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
3703 <span class="apii">[-0, +0, &ndash;]</span>
3704 <pre>int lua_isnumber (lua_State *L, int index);</pre>
3705
3706 <p>
3707 Returns 1 if the value at the given index is a number
3708 or a string convertible to a number,
3709 and 0&nbsp;otherwise.
3710
3711
3712
3713
3714
3715 <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
3716 <span class="apii">[-0, +0, &ndash;]</span>
3717 <pre>int lua_isstring (lua_State *L, int index);</pre>
3718
3719 <p>
3720 Returns 1 if the value at the given index is a string
3721 or a number (which is always convertible to a string),
3722 and 0&nbsp;otherwise.
3723
3724
3725
3726
3727
3728 <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
3729 <span class="apii">[-0, +0, &ndash;]</span>
3730 <pre>int lua_istable (lua_State *L, int index);</pre>
3731
3732 <p>
3733 Returns 1 if the value at the given index is a table,
3734 and 0&nbsp;otherwise.
3735
3736
3737
3738
3739
3740 <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
3741 <span class="apii">[-0, +0, &ndash;]</span>
3742 <pre>int lua_isthread (lua_State *L, int index);</pre>
3743
3744 <p>
3745 Returns 1 if the value at the given index is a thread,
3746 and 0&nbsp;otherwise.
3747
3748
3749
3750
3751
3752 <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
3753 <span class="apii">[-0, +0, &ndash;]</span>
3754 <pre>int lua_isuserdata (lua_State *L, int index);</pre>
3755
3756 <p>
3757 Returns 1 if the value at the given index is a userdata
3758 (either full or light), and 0&nbsp;otherwise.
3759
3760
3761
3762
3763
3764 <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
3765 <span class="apii">[-0, +1, <em>e</em>]</span>
3766 <pre>void lua_len (lua_State *L, int index);</pre>
3767
3768 <p>
3769 Returns the "length" of the value at the given index;
3770 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.6">&sect;3.4.6</a>).
3771 The result is pushed on the stack.
3772
3773
3774
3775
3776
3777 <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
3778 <span class="apii">[-0, +1, &ndash;]</span>
3779 <pre>int lua_load (lua_State *L,
3780 lua_Reader reader,
3781 void *data,
3782 const char *source,
3783 const char *mode);</pre>
3784
3785 <p>
3786 Loads a Lua chunk (without running it).
3787 If there are no errors,
3788 <code>lua_load</code> pushes the compiled chunk as a Lua
3789 function on top of the stack.
3790 Otherwise, it pushes an error message.
3791
3792
3793 <p>
3794 The return values of <code>lua_load</code> are:
3795
3796 <ul>
3797
3798 <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
3799
3800 <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
3801 syntax error during precompilation;</li>
3802
3803 <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3804 memory allocation error;</li>
3805
3806 <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3807 error while running a <code>__gc</code> metamethod.
3808 (This error has no relation with the chunk being loaded.
3809 It is generated by the garbage collector.)
3810 </li>
3811
3812 </ul>
3813
3814 <p>
3815 The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
3816 to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
3817 The <code>data</code> argument is an opaque value passed to the reader function.
3818
3819
3820 <p>
3821 The <code>source</code> argument gives a name to the chunk,
3822 which is used for error messages and in debug information (see <a href="#4.9">&sect;4.9</a>).
3823
3824
3825 <p>
3826 <code>lua_load</code> automatically detects whether the chunk is text or binary
3827 and loads it accordingly (see program <code>luac</code>).
3828 The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
3829 with the addition that
3830 a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
3831
3832
3833 <p>
3834 <code>lua_load</code> uses the stack internally,
3835 so the reader function should always leave the stack
3836 unmodified when returning.
3837
3838
3839 <p>
3840 If the resulting function has one upvalue,
3841 this upvalue is set to the value of the global environment
3842 stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">&sect;4.5</a>).
3843 When loading main chunks,
3844 this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
3845
3846
3847
3848
3849
3850 <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
3851 <span class="apii">[-0, +0, &ndash;]</span>
3852 <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
3853
3854 <p>
3855 Creates a new thread running in a new, independent state.
3856 Returns <code>NULL</code> if cannot create the thread or the state
3857 (due to lack of memory).
3858 The argument <code>f</code> is the allocator function;
3859 Lua does all memory allocation for this state through this function.
3860 The second argument, <code>ud</code>, is an opaque pointer that Lua
3861 passes to the allocator in every call.
3862
3863
3864
3865
3866
3867 <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
3868 <span class="apii">[-0, +1, <em>e</em>]</span>
3869 <pre>void lua_newtable (lua_State *L);</pre>
3870
3871 <p>
3872 Creates a new empty table and pushes it onto the stack.
3873 It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
3874
3875
3876
3877
3878
3879 <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
3880 <span class="apii">[-0, +1, <em>e</em>]</span>
3881 <pre>lua_State *lua_newthread (lua_State *L);</pre>
3882
3883 <p>
3884 Creates a new thread, pushes it on the stack,
3885 and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
3886 The new thread returned by this function shares with the original thread
3887 its global environment,
3888 but has an independent execution stack.
3889
3890
3891 <p>
3892 There is no explicit function to close or to destroy a thread.
3893 Threads are subject to garbage collection,
3894 like any Lua object.
3895
3896
3897
3898
3899
3900 <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
3901 <span class="apii">[-0, +1, <em>e</em>]</span>
3902 <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>
3903
3904 <p>
3905 This function allocates a new block of memory with the given size,
3906 pushes onto the stack a new full userdata with the block address,
3907 and returns this address.
3908 The host program can freely use this memory.
3909
3910
3911
3912
3913
3914 <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
3915 <span class="apii">[-1, +(2|0), <em>e</em>]</span>
3916 <pre>int lua_next (lua_State *L, int index);</pre>
3917
3918 <p>
3919 Pops a key from the stack,
3920 and pushes a key&ndash;value pair from the table at the given index
3921 (the "next" pair after the given key).
3922 If there are no more elements in the table,
3923 then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
3924
3925
3926 <p>
3927 A typical traversal looks like this:
3928
3929 <pre>
3930 /* table is in the stack at index 't' */
3931 lua_pushnil(L); /* first key */
3932 while (lua_next(L, t) != 0) {
3933 /* uses 'key' (at index -2) and 'value' (at index -1) */
3934 printf("%s - %s\n",
3935 lua_typename(L, lua_type(L, -2)),
3936 lua_typename(L, lua_type(L, -1)));
3937 /* removes 'value'; keeps 'key' for next iteration */
3938 lua_pop(L, 1);
3939 }
3940 </pre>
3941
3942 <p>
3943 While traversing a table,
3944 do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
3945 unless you know that the key is actually a string.
3946 Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
3947 the value at the given index;
3948 this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
3949
3950
3951 <p>
3952 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
3953 the table during its traversal.
3954
3955
3956
3957
3958
3959 <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
3960 <pre>typedef double lua_Number;</pre>
3961
3962 <p>
3963 The type of numbers in Lua.
3964 By default, it is double, but that can be changed in <code>luaconf.h</code>.
3965 Through this configuration file you can change
3966 Lua to operate with another type for numbers (e.g., float or long).
3967
3968
3969
3970
3971
3972 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
3973 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
3974 <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
3975
3976 <p>
3977 Calls a function in protected mode.
3978
3979
3980 <p>
3981 Both <code>nargs</code> and <code>nresults</code> have the same meaning as
3982 in <a href="#lua_call"><code>lua_call</code></a>.
3983 If there are no errors during the call,
3984 <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
3985 However, if there is any error,
3986 <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
3987 pushes a single value on the stack (the error message),
3988 and returns an error code.
3989 Like <a href="#lua_call"><code>lua_call</code></a>,
3990 <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
3991 and its arguments from the stack.
3992
3993
3994 <p>
3995 If <code>msgh</code> is 0,
3996 then the error message returned on the stack
3997 is exactly the original error message.
3998 Otherwise, <code>msgh</code> is the stack index of a
3999 <em>message handler</em>.
4000 (In the current implementation, this index cannot be a pseudo-index.)
4001 In case of runtime errors,
4002 this function will be called with the error message
4003 and its return value will be the message
4004 returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
4005
4006
4007 <p>
4008 Typically, the message handler is used to add more debug
4009 information to the error message, such as a stack traceback.
4010 Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
4011 since by then the stack has unwound.
4012
4013
4014 <p>
4015 The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following codes
4016 (defined in <code>lua.h</code>):
4017
4018 <ul>
4019
4020 <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b>
4021 success.</li>
4022
4023 <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b>
4024 a runtime error.
4025 </li>
4026
4027 <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
4028 memory allocation error.
4029 For such errors, Lua does not call the message handler.
4030 </li>
4031
4032 <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b>
4033 error while running the message handler.
4034 </li>
4035
4036 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
4037 error while running a <code>__gc</code> metamethod.
4038 (This error typically has no relation with the function being called.
4039 It is generated by the garbage collector.)
4040 </li>
4041
4042 </ul>
4043
4044
4045
4046
4047 <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
4048 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
4049 <pre>int lua_pcallk (lua_State *L,
4050 int nargs,
4051 int nresults,
4052 int errfunc,
4053 int ctx,
4054 lua_CFunction k);</pre>
4055
4056 <p>
4057 This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
4058 but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
4059
4060
4061
4062
4063
4064 <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
4065 <span class="apii">[-n, +0, &ndash;]</span>
4066 <pre>void lua_pop (lua_State *L, int n);</pre>
4067
4068 <p>
4069 Pops <code>n</code> elements from the stack.
4070
4071
4072
4073
4074
4075 <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
4076 <span class="apii">[-0, +1, &ndash;]</span>
4077 <pre>void lua_pushboolean (lua_State *L, int b);</pre>
4078
4079 <p>
4080 Pushes a boolean value with value <code>b</code> onto the stack.
4081
4082
4083
4084
4085
4086 <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
4087 <span class="apii">[-n, +1, <em>e</em>]</span>
4088 <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
4089
4090 <p>
4091 Pushes a new C&nbsp;closure onto the stack.
4092
4093
4094 <p>
4095 When a C&nbsp;function is created,
4096 it is possible to associate some values with it,
4097 thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);
4098 these values are then accessible to the function whenever it is called.
4099 To associate values with a C&nbsp;function,
4100 first these values should be pushed onto the stack
4101 (when there are multiple values, the first value is pushed first).
4102 Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
4103 is called to create and push the C&nbsp;function onto the stack,
4104 with the argument <code>n</code> telling how many values should be
4105 associated with the function.
4106 <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
4107
4108
4109 <p>
4110 The maximum value for <code>n</code> is 255.
4111
4112
4113 <p>
4114 When <code>n</code> is zero,
4115 this function creates a <em>light C function</em>,
4116 which is just a pointer to the C&nbsp;function.
4117 In that case, it never throws a memory error.
4118
4119
4120
4121
4122
4123 <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
4124 <span class="apii">[-0, +1, &ndash;]</span>
4125 <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
4126
4127 <p>
4128 Pushes a C&nbsp;function onto the stack.
4129 This function receives a pointer to a C function
4130 and pushes onto the stack a Lua value of type <code>function</code> that,
4131 when called, invokes the corresponding C&nbsp;function.
4132
4133
4134 <p>
4135 Any function to be registered in Lua must
4136 follow the correct protocol to receive its parameters
4137 and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
4138
4139
4140 <p>
4141 <code>lua_pushcfunction</code> is defined as a macro:
4142
4143 <pre>
4144 #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0)
4145 </pre><p>
4146 Note that <code>f</code> is used twice.
4147
4148
4149
4150
4151
4152 <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
4153 <span class="apii">[-0, +1, <em>e</em>]</span>
4154 <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
4155
4156 <p>
4157 Pushes onto the stack a formatted string
4158 and returns a pointer to this string.
4159 It is similar to the ANSI&nbsp;C function <code>sprintf</code>,
4160 but has some important differences:
4161
4162 <ul>
4163
4164 <li>
4165 You do not have to allocate space for the result:
4166 the result is a Lua string and Lua takes care of memory allocation
4167 (and deallocation, through garbage collection).
4168 </li>
4169
4170 <li>
4171 The conversion specifiers are quite restricted.
4172 There are no flags, widths, or precisions.
4173 The conversion specifiers can only be
4174 '<code>%%</code>' (inserts a '<code>%</code>' in the string),
4175 '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
4176 '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
4177 '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
4178 '<code>%d</code>' (inserts an <code>int</code>), and
4179 '<code>%c</code>' (inserts an <code>int</code> as a byte).
4180 </li>
4181
4182 </ul>
4183
4184
4185
4186
4187 <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
4188 <span class="apii">[-0, +1, &ndash;]</span>
4189 <pre>void lua_pushglobaltable (lua_State *L);</pre>
4190
4191 <p>
4192 Pushes the global environment onto the stack.
4193
4194
4195
4196
4197
4198 <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
4199 <span class="apii">[-0, +1, &ndash;]</span>
4200 <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
4201
4202 <p>
4203 Pushes a number with value <code>n</code> onto the stack.
4204
4205
4206
4207
4208
4209 <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
4210 <span class="apii">[-0, +1, &ndash;]</span>
4211 <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
4212
4213 <p>
4214 Pushes a light userdata onto the stack.
4215
4216
4217 <p>
4218 Userdata represent C&nbsp;values in Lua.
4219 A <em>light userdata</em> represents a pointer, a <code>void*</code>.
4220 It is a value (like a number):
4221 you do not create it, it has no individual metatable,
4222 and it is not collected (as it was never created).
4223 A light userdata is equal to "any"
4224 light userdata with the same C&nbsp;address.
4225
4226
4227
4228
4229
4230 <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
4231 <span class="apii">[-0, +1, <em>e</em>]</span>
4232 <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre>
4233
4234 <p>
4235 This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>,
4236 but can be used only when <code>s</code> is a literal string.
4237 It automatically provides the string length.
4238
4239
4240
4241
4242
4243 <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
4244 <span class="apii">[-0, +1, <em>e</em>]</span>
4245 <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
4246
4247 <p>
4248 Pushes the string pointed to by <code>s</code> with size <code>len</code>
4249 onto the stack.
4250 Lua makes (or reuses) an internal copy of the given string,
4251 so the memory at <code>s</code> can be freed or reused immediately after
4252 the function returns.
4253 The string can contain any binary data,
4254 including embedded zeros.
4255
4256
4257 <p>
4258 Returns a pointer to the internal copy of the string.
4259
4260
4261
4262
4263
4264 <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
4265 <span class="apii">[-0, +1, &ndash;]</span>
4266 <pre>void lua_pushnil (lua_State *L);</pre>
4267
4268 <p>
4269 Pushes a nil value onto the stack.
4270
4271
4272
4273
4274
4275 <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
4276 <span class="apii">[-0, +1, &ndash;]</span>
4277 <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
4278
4279 <p>
4280 Pushes a number with value <code>n</code> onto the stack.
4281
4282
4283
4284
4285
4286 <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
4287 <span class="apii">[-0, +1, <em>e</em>]</span>
4288 <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre>
4289
4290 <p>
4291 Pushes the zero-terminated string pointed to by <code>s</code>
4292 onto the stack.
4293 Lua makes (or reuses) an internal copy of the given string,
4294 so the memory at <code>s</code> can be freed or reused immediately after
4295 the function returns.
4296
4297
4298 <p>
4299 Returns a pointer to the internal copy of the string.
4300
4301
4302 <p>
4303 If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>.
4304
4305
4306
4307
4308
4309 <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
4310 <span class="apii">[-0, +1, &ndash;]</span>
4311 <pre>int lua_pushthread (lua_State *L);</pre>
4312
4313 <p>
4314 Pushes the thread represented by <code>L</code> onto the stack.
4315 Returns 1 if this thread is the main thread of its state.
4316
4317
4318
4319
4320
4321 <hr><h3><a name="lua_pushunsigned"><code>lua_pushunsigned</code></a></h3><p>
4322 <span class="apii">[-0, +1, &ndash;]</span>
4323 <pre>void lua_pushunsigned (lua_State *L, lua_Unsigned n);</pre>
4324
4325 <p>
4326 Pushes a number with value <code>n</code> onto the stack.
4327
4328
4329
4330
4331
4332 <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
4333 <span class="apii">[-0, +1, &ndash;]</span>
4334 <pre>void lua_pushvalue (lua_State *L, int index);</pre>
4335
4336 <p>
4337 Pushes a copy of the element at the given index
4338 onto the stack.
4339
4340
4341
4342
4343
4344 <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
4345 <span class="apii">[-0, +1, <em>e</em>]</span>
4346 <pre>const char *lua_pushvfstring (lua_State *L,
4347 const char *fmt,
4348 va_list argp);</pre>
4349
4350 <p>
4351 Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
4352 instead of a variable number of arguments.
4353
4354
4355
4356
4357
4358 <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
4359 <span class="apii">[-0, +0, &ndash;]</span>
4360 <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
4361
4362 <p>
4363 Returns 1 if the two values in indices <code>index1</code> and
4364 <code>index2</code> are primitively equal
4365 (that is, without calling metamethods).
4366 Otherwise returns&nbsp;0.
4367 Also returns&nbsp;0 if any of the indices are non valid.
4368
4369
4370
4371
4372
4373 <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
4374 <span class="apii">[-1, +1, &ndash;]</span>
4375 <pre>void lua_rawget (lua_State *L, int index);</pre>
4376
4377 <p>
4378 Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
4379 (i.e., without metamethods).
4380
4381
4382
4383
4384
4385 <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
4386 <span class="apii">[-0, +1, &ndash;]</span>
4387 <pre>void lua_rawgeti (lua_State *L, int index, int n);</pre>
4388
4389 <p>
4390 Pushes onto the stack the value <code>t[n]</code>,
4391 where <code>t</code> is the table at the given index.
4392 The access is raw;
4393 that is, it does not invoke metamethods.
4394
4395
4396
4397
4398
4399 <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
4400 <span class="apii">[-0, +1, &ndash;]</span>
4401 <pre>void lua_rawgetp (lua_State *L, int index, const void *p);</pre>
4402
4403 <p>
4404 Pushes onto the stack the value <code>t[k]</code>,
4405 where <code>t</code> is the table at the given index and
4406 <code>k</code> is the pointer <code>p</code> represented as a light userdata.
4407 The access is raw;
4408 that is, it does not invoke metamethods.
4409
4410
4411
4412
4413
4414 <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
4415 <span class="apii">[-0, +0, &ndash;]</span>
4416 <pre>size_t lua_rawlen (lua_State *L, int index);</pre>
4417
4418 <p>
4419 Returns the raw "length" of the value at the given index:
4420 for strings, this is the string length;
4421 for tables, this is the result of the length operator ('<code>#</code>')
4422 with no metamethods;
4423 for userdata, this is the size of the block of memory allocated
4424 for the userdata;
4425 for other values, it is&nbsp;0.
4426
4427
4428
4429
4430
4431 <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
4432 <span class="apii">[-2, +0, <em>e</em>]</span>
4433 <pre>void lua_rawset (lua_State *L, int index);</pre>
4434
4435 <p>
4436 Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
4437 (i.e., without metamethods).
4438
4439
4440
4441
4442
4443 <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
4444 <span class="apii">[-1, +0, <em>e</em>]</span>
4445 <pre>void lua_rawseti (lua_State *L, int index, int n);</pre>
4446
4447 <p>
4448 Does the equivalent of <code>t[n] = v</code>,
4449 where <code>t</code> is the table at the given index
4450 and <code>v</code> is the value at the top of the stack.
4451
4452
4453 <p>
4454 This function pops the value from the stack.
4455 The assignment is raw;
4456 that is, it does not invoke metamethods.
4457
4458
4459
4460
4461
4462 <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
4463 <span class="apii">[-1, +0, <em>e</em>]</span>
4464 <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
4465
4466 <p>
4467 Does the equivalent of <code>t[k] = v</code>,
4468 where <code>t</code> is the table at the given index,
4469 <code>k</code> is the pointer <code>p</code> represented as a light userdata,
4470 and <code>v</code> is the value at the top of the stack.
4471
4472
4473 <p>
4474 This function pops the value from the stack.
4475 The assignment is raw;
4476 that is, it does not invoke metamethods.
4477
4478
4479
4480
4481
4482 <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
4483 <pre>typedef const char * (*lua_Reader) (lua_State *L,
4484 void *data,
4485 size_t *size);</pre>
4486
4487 <p>
4488 The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
4489 Every time it needs another piece of the chunk,
4490 <a href="#lua_load"><code>lua_load</code></a> calls the reader,
4491 passing along its <code>data</code> parameter.
4492 The reader must return a pointer to a block of memory
4493 with a new piece of the chunk
4494 and set <code>size</code> to the block size.
4495 The block must exist until the reader function is called again.
4496 To signal the end of the chunk,
4497 the reader must return <code>NULL</code> or set <code>size</code> to zero.
4498 The reader function may return pieces of any size greater than zero.
4499
4500
4501
4502
4503
4504 <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
4505 <span class="apii">[-0, +0, <em>e</em>]</span>
4506 <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre>
4507
4508 <p>
4509 Sets the C function <code>f</code> as the new value of global <code>name</code>.
4510 It is defined as a macro:
4511
4512 <pre>
4513 #define lua_register(L,n,f) \
4514 (lua_pushcfunction(L, f), lua_setglobal(L, n))
4515 </pre>
4516
4517
4518
4519
4520 <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
4521 <span class="apii">[-1, +0, &ndash;]</span>
4522 <pre>void lua_remove (lua_State *L, int index);</pre>
4523
4524 <p>
4525 Removes the element at the given valid index,
4526 shifting down the elements above this index to fill the gap.
4527 This function cannot be called with a pseudo-index,
4528 because a pseudo-index is not an actual stack position.
4529
4530
4531
4532
4533
4534 <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
4535 <span class="apii">[-1, +0, &ndash;]</span>
4536 <pre>void lua_replace (lua_State *L, int index);</pre>
4537
4538 <p>
4539 Moves the top element into the given valid index
4540 without shifting any element
4541 (therefore replacing the value at the given index),
4542 and then pops the top element.
4543
4544
4545
4546
4547
4548 <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
4549 <span class="apii">[-?, +?, &ndash;]</span>
4550 <pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre>
4551
4552 <p>
4553 Starts and resumes a coroutine in a given thread.
4554
4555
4556 <p>
4557 To start a coroutine,
4558 you push onto the thread stack the main function plus any arguments;
4559 then you call <a href="#lua_resume"><code>lua_resume</code></a>,
4560 with <code>nargs</code> being the number of arguments.
4561 This call returns when the coroutine suspends or finishes its execution.
4562 When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>,
4563 or all values returned by the body function.
4564 <a href="#lua_resume"><code>lua_resume</code></a> returns
4565 <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
4566 <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
4567 without errors,
4568 or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
4569
4570
4571 <p>
4572 In case of errors,
4573 the stack is not unwound,
4574 so you can use the debug API over it.
4575 The error message is on the top of the stack.
4576
4577
4578 <p>
4579 To resume a coroutine,
4580 you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>,
4581 put on its stack only the values to
4582 be passed as results from <code>yield</code>,
4583 and then call <a href="#lua_resume"><code>lua_resume</code></a>.
4584
4585
4586 <p>
4587 The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
4588 If there is no such coroutine,
4589 this parameter can be <code>NULL</code>.
4590
4591
4592
4593
4594
4595 <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
4596 <span class="apii">[-0, +0, &ndash;]</span>
4597 <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
4598
4599 <p>
4600 Changes the allocator function of a given state to <code>f</code>
4601 with user data <code>ud</code>.
4602
4603
4604
4605
4606
4607 <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
4608 <span class="apii">[-1, +0, <em>e</em>]</span>
4609 <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
4610
4611 <p>
4612 Does the equivalent to <code>t[k] = v</code>,
4613 where <code>t</code> is the value at the given index
4614 and <code>v</code> is the value at the top of the stack.
4615
4616
4617 <p>
4618 This function pops the value from the stack.
4619 As in Lua, this function may trigger a metamethod
4620 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4621
4622
4623
4624
4625
4626 <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
4627 <span class="apii">[-1, +0, <em>e</em>]</span>
4628 <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
4629
4630 <p>
4631 Pops a value from the stack and
4632 sets it as the new value of global <code>name</code>.
4633
4634
4635
4636
4637
4638 <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
4639 <span class="apii">[-1, +0, &ndash;]</span>
4640 <pre>void lua_setmetatable (lua_State *L, int index);</pre>
4641
4642 <p>
4643 Pops a table from the stack and
4644 sets it as the new metatable for the value at the given index.
4645
4646
4647
4648
4649
4650 <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
4651 <span class="apii">[-2, +0, <em>e</em>]</span>
4652 <pre>void lua_settable (lua_State *L, int index);</pre>
4653
4654 <p>
4655 Does the equivalent to <code>t[k] = v</code>,
4656 where <code>t</code> is the value at the given index,
4657 <code>v</code> is the value at the top of the stack,
4658 and <code>k</code> is the value just below the top.
4659
4660
4661 <p>
4662 This function pops both the key and the value from the stack.
4663 As in Lua, this function may trigger a metamethod
4664 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4665
4666
4667
4668
4669
4670 <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
4671 <span class="apii">[-?, +?, &ndash;]</span>
4672 <pre>void lua_settop (lua_State *L, int index);</pre>
4673
4674 <p>
4675 Accepts any index, or&nbsp;0,
4676 and sets the stack top to this index.
4677 If the new top is larger than the old one,
4678 then the new elements are filled with <b>nil</b>.
4679 If <code>index</code> is&nbsp;0, then all stack elements are removed.
4680
4681
4682
4683
4684
4685 <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
4686 <span class="apii">[-1, +0, &ndash;]</span>
4687 <pre>void lua_setuservalue (lua_State *L, int index);</pre>
4688
4689 <p>
4690 Pops a table or <b>nil</b> from the stack and sets it as
4691 the new value associated to the userdata at the given index.
4692
4693
4694
4695
4696
4697 <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
4698 <pre>typedef struct lua_State lua_State;</pre>
4699
4700 <p>
4701 An opaque structure that points to a thread and indirectly
4702 (through the thread) to the whole state of a Lua interpreter.
4703 The Lua library is fully reentrant:
4704 it has no global variables.
4705 All information about a state is accessible through this structure.
4706
4707
4708 <p>
4709 A pointer to this structure must be passed as the first argument to
4710 every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
4711 which creates a Lua state from scratch.
4712
4713
4714
4715
4716
4717 <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
4718 <span class="apii">[-0, +0, &ndash;]</span>
4719 <pre>int lua_status (lua_State *L);</pre>
4720
4721 <p>
4722 Returns the status of the thread <code>L</code>.
4723
4724
4725 <p>
4726 The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread,
4727 an error code if the thread finished the execution
4728 of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
4729 or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
4730
4731
4732 <p>
4733 You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
4734 You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
4735 (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
4736 (to resume a coroutine).
4737
4738
4739
4740
4741
4742 <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
4743 <span class="apii">[-0, +0, &ndash;]</span>
4744 <pre>int lua_toboolean (lua_State *L, int index);</pre>
4745
4746 <p>
4747 Converts the Lua value at the given index to a C&nbsp;boolean
4748 value (0&nbsp;or&nbsp;1).
4749 Like all tests in Lua,
4750 <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
4751 different from <b>false</b> and <b>nil</b>;
4752 otherwise it returns false.
4753 (If you want to accept only actual boolean values,
4754 use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
4755
4756
4757
4758
4759
4760 <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
4761 <span class="apii">[-0, +0, &ndash;]</span>
4762 <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
4763
4764 <p>
4765 Converts a value at the given index to a C&nbsp;function.
4766 That value must be a C&nbsp;function;
4767 otherwise, returns <code>NULL</code>.
4768
4769
4770
4771
4772
4773 <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
4774 <span class="apii">[-0, +0, &ndash;]</span>
4775 <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
4776
4777 <p>
4778 Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4779
4780
4781
4782
4783
4784 <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
4785 <span class="apii">[-0, +0, &ndash;]</span>
4786 <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
4787
4788 <p>
4789 Converts the Lua value at the given index
4790 to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
4791 The Lua value must be a number or a string convertible to a number
4792 (see <a href="#3.4.2">&sect;3.4.2</a>);
4793 otherwise, <code>lua_tointegerx</code> returns&nbsp;0.
4794
4795
4796 <p>
4797 If the number is not an integer,
4798 it is truncated in some non-specified way.
4799
4800
4801 <p>
4802 If <code>isnum</code> is not <code>NULL</code>,
4803 its referent is assigned a boolean value that
4804 indicates whether the operation succeeded.
4805
4806
4807
4808
4809
4810 <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
4811 <span class="apii">[-0, +0, <em>e</em>]</span>
4812 <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
4813
4814 <p>
4815 Converts the Lua value at the given index to a C&nbsp;string.
4816 If <code>len</code> is not <code>NULL</code>,
4817 it also sets <code>*len</code> with the string length.
4818 The Lua value must be a string or a number;
4819 otherwise, the function returns <code>NULL</code>.
4820 If the value is a number,
4821 then <code>lua_tolstring</code> also
4822 <em>changes the actual value in the stack to a string</em>.
4823 (This change confuses <a href="#lua_next"><code>lua_next</code></a>
4824 when <code>lua_tolstring</code> is applied to keys during a table traversal.)
4825
4826
4827 <p>
4828 <code>lua_tolstring</code> returns a fully aligned pointer
4829 to a string inside the Lua state.
4830 This string always has a zero ('<code>\0</code>')
4831 after its last character (as in&nbsp;C),
4832 but can contain other zeros in its body.
4833 Because Lua has garbage collection,
4834 there is no guarantee that the pointer returned by <code>lua_tolstring</code>
4835 will be valid after the corresponding value is removed from the stack.
4836
4837
4838
4839
4840
4841 <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
4842 <span class="apii">[-0, +0, &ndash;]</span>
4843 <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
4844
4845 <p>
4846 Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4847
4848
4849
4850
4851
4852 <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
4853 <span class="apii">[-0, +0, &ndash;]</span>
4854 <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
4855
4856 <p>
4857 Converts the Lua value at the given index
4858 to the C&nbsp;type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
4859 The Lua value must be a number or a string convertible to a number
4860 (see <a href="#3.4.2">&sect;3.4.2</a>);
4861 otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns&nbsp;0.
4862
4863
4864 <p>
4865 If <code>isnum</code> is not <code>NULL</code>,
4866 its referent is assigned a boolean value that
4867 indicates whether the operation succeeded.
4868
4869
4870
4871
4872
4873 <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
4874 <span class="apii">[-0, +0, &ndash;]</span>
4875 <pre>const void *lua_topointer (lua_State *L, int index);</pre>
4876
4877 <p>
4878 Converts the value at the given index to a generic
4879 C&nbsp;pointer (<code>void*</code>).
4880 The value can be a userdata, a table, a thread, or a function;
4881 otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
4882 Different objects will give different pointers.
4883 There is no way to convert the pointer back to its original value.
4884
4885
4886 <p>
4887 Typically this function is used only for debug information.
4888
4889
4890
4891
4892
4893 <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
4894 <span class="apii">[-0, +0, <em>e</em>]</span>
4895 <pre>const char *lua_tostring (lua_State *L, int index);</pre>
4896
4897 <p>
4898 Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
4899
4900
4901
4902
4903
4904 <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
4905 <span class="apii">[-0, +0, &ndash;]</span>
4906 <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
4907
4908 <p>
4909 Converts the value at the given index to a Lua thread
4910 (represented as <code>lua_State*</code>).
4911 This value must be a thread;
4912 otherwise, the function returns <code>NULL</code>.
4913
4914
4915
4916
4917
4918 <hr><h3><a name="lua_tounsigned"><code>lua_tounsigned</code></a></h3><p>
4919 <span class="apii">[-0, +0, &ndash;]</span>
4920 <pre>lua_Unsigned lua_tounsigned (lua_State *L, int index);</pre>
4921
4922 <p>
4923 Equivalent to <a href="#lua_tounsignedx"><code>lua_tounsignedx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4924
4925
4926
4927
4928
4929 <hr><h3><a name="lua_tounsignedx"><code>lua_tounsignedx</code></a></h3><p>
4930 <span class="apii">[-0, +0, &ndash;]</span>
4931 <pre>lua_Unsigned lua_tounsignedx (lua_State *L, int index, int *isnum);</pre>
4932
4933 <p>
4934 Converts the Lua value at the given index
4935 to the unsigned integral type <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>.
4936 The Lua value must be a number or a string convertible to a number
4937 (see <a href="#3.4.2">&sect;3.4.2</a>);
4938 otherwise, <code>lua_tounsignedx</code> returns&nbsp;0.
4939
4940
4941 <p>
4942 If the number is not an integer,
4943 it is truncated in some non-specified way.
4944 If the number is outside the range of representable values,
4945 it is normalized to the remainder of its division by
4946 one more than the maximum representable value.
4947
4948
4949 <p>
4950 If <code>isnum</code> is not <code>NULL</code>,
4951 its referent is assigned a boolean value that
4952 indicates whether the operation succeeded.
4953
4954
4955
4956
4957
4958 <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
4959 <span class="apii">[-0, +0, &ndash;]</span>
4960 <pre>void *lua_touserdata (lua_State *L, int index);</pre>
4961
4962 <p>
4963 If the value at the given index is a full userdata,
4964 returns its block address.
4965 If the value is a light userdata,
4966 returns its pointer.
4967 Otherwise, returns <code>NULL</code>.
4968
4969
4970
4971
4972
4973 <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
4974 <span class="apii">[-0, +0, &ndash;]</span>
4975 <pre>int lua_type (lua_State *L, int index);</pre>
4976
4977 <p>
4978 Returns the type of the value in the given valid index,
4979 or <code>LUA_TNONE</code> for a non-valid (but acceptable) index.
4980 The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
4981 defined in <code>lua.h</code>:
4982 <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>,
4983 <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
4984 <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
4985 <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
4986 <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
4987 <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
4988 <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
4989 <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
4990 and
4991 <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
4992
4993
4994
4995
4996
4997 <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
4998 <span class="apii">[-0, +0, &ndash;]</span>
4999 <pre>const char *lua_typename (lua_State *L, int tp);</pre>
5000
5001 <p>
5002 Returns the name of the type encoded by the value <code>tp</code>,
5003 which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
5004
5005
5006
5007
5008
5009 <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
5010 <pre>typedef unsigned long lua_Unsigned;</pre>
5011
5012 <p>
5013 The type used by the Lua API to represent unsigned integral values.
5014 It must have at least 32 bits.
5015
5016
5017 <p>
5018 By default it is an <code>unsigned int</code> or an <code>unsigned long</code>,
5019 whichever can hold 32-bit values.
5020
5021
5022
5023
5024
5025 <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
5026 <span class="apii">[-0, +0, &ndash;]</span>
5027 <pre>int lua_upvalueindex (int i);</pre>
5028
5029 <p>
5030 Returns the pseudo-index that represents the <code>i</code>-th upvalue of
5031 the running function (see <a href="#4.4">&sect;4.4</a>).
5032
5033
5034
5035
5036
5037 <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
5038 <span class="apii">[-0, +0, <em>v</em>]</span>
5039 <pre>const lua_Number *lua_version (lua_State *L);</pre>
5040
5041 <p>
5042 Returns the address of the version number stored in the Lua core.
5043 When called with a valid <a href="#lua_State"><code>lua_State</code></a>,
5044 returns the address of the version used to create that state.
5045 When called with <code>NULL</code>,
5046 returns the address of the version running the call.
5047
5048
5049
5050
5051
5052 <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
5053 <pre>typedef int (*lua_Writer) (lua_State *L,
5054 const void* p,
5055 size_t sz,
5056 void* ud);</pre>
5057
5058 <p>
5059 The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
5060 Every time it produces another piece of chunk,
5061 <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
5062 passing along the buffer to be written (<code>p</code>),
5063 its size (<code>sz</code>),
5064 and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
5065
5066
5067 <p>
5068 The writer returns an error code:
5069 0&nbsp;means no errors;
5070 any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
5071 calling the writer again.
5072
5073
5074
5075
5076
5077 <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
5078 <span class="apii">[-?, +?, &ndash;]</span>
5079 <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
5080
5081 <p>
5082 Exchange values between different threads of the same state.
5083
5084
5085 <p>
5086 This function pops <code>n</code> values from the stack <code>from</code>,
5087 and pushes them onto the stack <code>to</code>.
5088
5089
5090
5091
5092
5093 <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
5094 <span class="apii">[-?, +?, &ndash;]</span>
5095 <pre>int lua_yield (lua_State *L, int nresults);</pre>
5096
5097 <p>
5098 This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5099 but it has no continuation (see <a href="#4.7">&sect;4.7</a>).
5100 Therefore, when the thread resumes,
5101 it returns to the function that called
5102 the function calling <code>lua_yield</code>.
5103
5104
5105
5106
5107
5108 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
5109 <span class="apii">[-?, +?, &ndash;]</span>
5110 <pre>int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k);</pre>
5111
5112 <p>
5113 Yields a coroutine.
5114
5115
5116 <p>
5117 This function should only be called as the
5118 return expression of a C&nbsp;function, as follows:
5119
5120 <pre>
5121 return lua_yieldk (L, n, i, k);
5122 </pre><p>
5123 When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a> in that way,
5124 the running coroutine suspends its execution,
5125 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
5126 The parameter <code>nresults</code> is the number of values from the stack
5127 that are passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
5128
5129
5130 <p>
5131 When the coroutine is resumed again,
5132 Lua calls the given continuation function <code>k</code> to continue
5133 the execution of the C function that yielded (see <a href="#4.7">&sect;4.7</a>).
5134 This continuation function receives the same stack
5135 from the previous function,
5136 with the results removed and
5137 replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
5138 Moreover,
5139 the continuation function may access the value <code>ctx</code>
5140 by calling <a href="#lua_getctx"><code>lua_getctx</code></a>.
5141
5142
5143
5144
5145
5146
5147
5148 <h2>4.9 &ndash; <a name="4.9">The Debug Interface</a></h2>
5149
5150 <p>
5151 Lua has no built-in debugging facilities.
5152 Instead, it offers a special interface
5153 by means of functions and <em>hooks</em>.
5154 This interface allows the construction of different
5155 kinds of debuggers, profilers, and other tools
5156 that need "inside information" from the interpreter.
5157
5158
5159
5160 <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
5161 <pre>typedef struct lua_Debug {
5162 int event;
5163 const char *name; /* (n) */
5164 const char *namewhat; /* (n) */
5165 const char *what; /* (S) */
5166 const char *source; /* (S) */
5167 int currentline; /* (l) */
5168 int linedefined; /* (S) */
5169 int lastlinedefined; /* (S) */
5170 unsigned char nups; /* (u) number of upvalues */
5171 unsigned char nparams; /* (u) number of parameters */
5172 char isvararg; /* (u) */
5173 char istailcall; /* (t) */
5174 char short_src[LUA_IDSIZE]; /* (S) */
5175 /* private part */
5176 <em>other fields</em>
5177 } lua_Debug;</pre>
5178
5179 <p>
5180 A structure used to carry different pieces of
5181 information about a function or an activation record.
5182 <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
5183 of this structure, for later use.
5184 To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
5185 call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5186
5187
5188 <p>
5189 The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
5190
5191 <ul>
5192
5193 <li><b><code>source</code>: </b>
5194 the source of the chunk that created the function.
5195 If <code>source</code> starts with a '<code>@</code>',
5196 it means that the function was defined in a file where
5197 the file name follows the '<code>@</code>'.
5198 If <code>source</code> starts with a '<code>=</code>',
5199 the remainder of its contents describe the source in a user-dependent manner.
5200 Otherwise,
5201 the function was defined in a string where
5202 <code>source</code> is that string.
5203 </li>
5204
5205 <li><b><code>short_src</code>: </b>
5206 a "printable" version of <code>source</code>, to be used in error messages.
5207 </li>
5208
5209 <li><b><code>linedefined</code>: </b>
5210 the line number where the definition of the function starts.
5211 </li>
5212
5213 <li><b><code>lastlinedefined</code>: </b>
5214 the line number where the definition of the function ends.
5215 </li>
5216
5217 <li><b><code>what</code>: </b>
5218 the string <code>"Lua"</code> if the function is a Lua function,
5219 <code>"C"</code> if it is a C&nbsp;function,
5220 <code>"main"</code> if it is the main part of a chunk.
5221 </li>
5222
5223 <li><b><code>currentline</code>: </b>
5224 the current line where the given function is executing.
5225 When no line information is available,
5226 <code>currentline</code> is set to -1.
5227 </li>
5228
5229 <li><b><code>name</code>: </b>
5230 a reasonable name for the given function.
5231 Because functions in Lua are first-class values,
5232 they do not have a fixed name:
5233 some functions can be the value of multiple global variables,
5234 while others can be stored only in a table field.
5235 The <code>lua_getinfo</code> function checks how the function was
5236 called to find a suitable name.
5237 If it cannot find a name,
5238 then <code>name</code> is set to <code>NULL</code>.
5239 </li>
5240
5241 <li><b><code>namewhat</code>: </b>
5242 explains the <code>name</code> field.
5243 The value of <code>namewhat</code> can be
5244 <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
5245 <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
5246 according to how the function was called.
5247 (Lua uses the empty string when no other option seems to apply.)
5248 </li>
5249
5250 <li><b><code>istailcall</code>: </b>
5251 true if this function invocation was called by a tail call.
5252 In this case, the caller of this level is not in the stack.
5253 </li>
5254
5255 <li><b><code>nups</code>: </b>
5256 the number of upvalues of the function.
5257 </li>
5258
5259 <li><b><code>nparams</code>: </b>
5260 the number of fixed parameters of the function
5261 (always 0&nbsp;for C&nbsp;functions).
5262 </li>
5263
5264 <li><b><code>isvararg</code>: </b>
5265 true if the function is a vararg function
5266 (always true for C&nbsp;functions).
5267 </li>
5268
5269 </ul>
5270
5271
5272
5273
5274 <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
5275 <span class="apii">[-0, +0, &ndash;]</span>
5276 <pre>lua_Hook lua_gethook (lua_State *L);</pre>
5277
5278 <p>
5279 Returns the current hook function.
5280
5281
5282
5283
5284
5285 <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
5286 <span class="apii">[-0, +0, &ndash;]</span>
5287 <pre>int lua_gethookcount (lua_State *L);</pre>
5288
5289 <p>
5290 Returns the current hook count.
5291
5292
5293
5294
5295
5296 <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
5297 <span class="apii">[-0, +0, &ndash;]</span>
5298 <pre>int lua_gethookmask (lua_State *L);</pre>
5299
5300 <p>
5301 Returns the current hook mask.
5302
5303
5304
5305
5306
5307 <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
5308 <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span>
5309 <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
5310
5311 <p>
5312 Gets information about a specific function or function invocation.
5313
5314
5315 <p>
5316 To get information about a function invocation,
5317 the parameter <code>ar</code> must be a valid activation record that was
5318 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5319 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5320
5321
5322 <p>
5323 To get information about a function you push it onto the stack
5324 and start the <code>what</code> string with the character '<code>&gt;</code>'.
5325 (In that case,
5326 <code>lua_getinfo</code> pops the function from the top of the stack.)
5327 For instance, to know in which line a function <code>f</code> was defined,
5328 you can write the following code:
5329
5330 <pre>
5331 lua_Debug ar;
5332 lua_getglobal(L, "f"); /* get global 'f' */
5333 lua_getinfo(L, "&gt;S", &amp;ar);
5334 printf("%d\n", ar.linedefined);
5335 </pre>
5336
5337 <p>
5338 Each character in the string <code>what</code>
5339 selects some fields of the structure <code>ar</code> to be filled or
5340 a value to be pushed on the stack:
5341
5342 <ul>
5343
5344 <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>;
5345 </li>
5346
5347 <li><b>'<code>S</code>': </b>
5348 fills in the fields <code>source</code>, <code>short_src</code>,
5349 <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
5350 </li>
5351
5352 <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>;
5353 </li>
5354
5355 <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>;
5356 </li>
5357
5358 <li><b>'<code>u</code>': </b> fills in the fields
5359 <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>;
5360 </li>
5361
5362 <li><b>'<code>f</code>': </b>
5363 pushes onto the stack the function that is
5364 running at the given level;
5365 </li>
5366
5367 <li><b>'<code>L</code>': </b>
5368 pushes onto the stack a table whose indices are the
5369 numbers of the lines that are valid on the function.
5370 (A <em>valid line</em> is a line with some associated code,
5371 that is, a line where you can put a break point.
5372 Non-valid lines include empty lines and comments.)
5373 </li>
5374
5375 </ul>
5376
5377 <p>
5378 This function returns 0 on error
5379 (for instance, an invalid option in <code>what</code>).
5380
5381
5382
5383
5384
5385 <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
5386 <span class="apii">[-0, +(0|1), &ndash;]</span>
5387 <pre>const char *lua_getlocal (lua_State *L, lua_Debug *ar, int n);</pre>
5388
5389 <p>
5390 Gets information about a local variable of
5391 a given activation record or a given function.
5392
5393
5394 <p>
5395 In the first case,
5396 the parameter <code>ar</code> must be a valid activation record that was
5397 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5398 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5399 The index <code>n</code> selects which local variable to inspect;
5400 see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
5401 and names.
5402
5403
5404 <p>
5405 <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
5406 and returns its name.
5407
5408
5409 <p>
5410 In the second case, <code>ar</code> should be <code>NULL</code> and the function
5411 to be inspected must be at the top of the stack.
5412 In this case, only parameters of Lua functions are visible
5413 (as there is no information about what variables are active)
5414 and no values are pushed onto the stack.
5415
5416
5417 <p>
5418 Returns <code>NULL</code> (and pushes nothing)
5419 when the index is greater than
5420 the number of active local variables.
5421
5422
5423
5424
5425
5426 <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
5427 <span class="apii">[-0, +0, &ndash;]</span>
5428 <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
5429
5430 <p>
5431 Gets information about the interpreter runtime stack.
5432
5433
5434 <p>
5435 This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
5436 an identification of the <em>activation record</em>
5437 of the function executing at a given level.
5438 Level&nbsp;0 is the current running function,
5439 whereas level <em>n+1</em> is the function that has called level <em>n</em>
5440 (except for tail calls, which do not count on the stack).
5441 When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
5442 when called with a level greater than the stack depth,
5443 it returns 0.
5444
5445
5446
5447
5448
5449 <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
5450 <span class="apii">[-0, +(0|1), &ndash;]</span>
5451 <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
5452
5453 <p>
5454 Gets information about a closure's upvalue.
5455 (For Lua functions,
5456 upvalues are the external local variables that the function uses,
5457 and that are consequently included in its closure.)
5458 <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> gets the index <code>n</code> of an upvalue,
5459 pushes the upvalue's value onto the stack,
5460 and returns its name.
5461 <code>funcindex</code> points to the closure in the stack.
5462 (Upvalues have no particular order,
5463 as they are active through the whole function.
5464 So, they are numbered in an arbitrary order.)
5465
5466
5467 <p>
5468 Returns <code>NULL</code> (and pushes nothing)
5469 when the index is greater than the number of upvalues.
5470 For C&nbsp;functions, this function uses the empty string <code>""</code>
5471 as a name for all upvalues.
5472
5473
5474
5475
5476
5477 <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
5478 <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
5479
5480 <p>
5481 Type for debugging hook functions.
5482
5483
5484 <p>
5485 Whenever a hook is called, its <code>ar</code> argument has its field
5486 <code>event</code> set to the specific event that triggered the hook.
5487 Lua identifies these events with the following constants:
5488 <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
5489 <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
5490 and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
5491 Moreover, for line events, the field <code>currentline</code> is also set.
5492 To get the value of any other field in <code>ar</code>,
5493 the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5494
5495
5496 <p>
5497 For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>,
5498 the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
5499 in this case, there will be no corresponding return event.
5500
5501
5502 <p>
5503 While Lua is running a hook, it disables other calls to hooks.
5504 Therefore, if a hook calls back Lua to execute a function or a chunk,
5505 this execution occurs without any calls to hooks.
5506
5507
5508 <p>
5509 Hook functions cannot have continuations,
5510 that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5511 <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>.
5512
5513
5514 <p>
5515 Hook functions can yield under the following conditions:
5516 Only count and line events can yield
5517 and they cannot yield any value;
5518 to yield a hook function must finish its execution
5519 calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero.
5520
5521
5522
5523
5524
5525 <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
5526 <span class="apii">[-0, +0, &ndash;]</span>
5527 <pre>int lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
5528
5529 <p>
5530 Sets the debugging hook function.
5531
5532
5533 <p>
5534 Argument <code>f</code> is the hook function.
5535 <code>mask</code> specifies on which events the hook will be called:
5536 it is formed by a bitwise or of the constants
5537 <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
5538 <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
5539 <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
5540 and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
5541 The <code>count</code> argument is only meaningful when the mask
5542 includes <code>LUA_MASKCOUNT</code>.
5543 For each event, the hook is called as explained below:
5544
5545 <ul>
5546
5547 <li><b>The call hook: </b> is called when the interpreter calls a function.
5548 The hook is called just after Lua enters the new function,
5549 before the function gets its arguments.
5550 </li>
5551
5552 <li><b>The return hook: </b> is called when the interpreter returns from a function.
5553 The hook is called just before Lua leaves the function.
5554 There is no standard way to access the values
5555 to be returned by the function.
5556 </li>
5557
5558 <li><b>The line hook: </b> is called when the interpreter is about to
5559 start the execution of a new line of code,
5560 or when it jumps back in the code (even to the same line).
5561 (This event only happens while Lua is executing a Lua function.)
5562 </li>
5563
5564 <li><b>The count hook: </b> is called after the interpreter executes every
5565 <code>count</code> instructions.
5566 (This event only happens while Lua is executing a Lua function.)
5567 </li>
5568
5569 </ul>
5570
5571 <p>
5572 A hook is disabled by setting <code>mask</code> to zero.
5573
5574
5575
5576
5577
5578 <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
5579 <span class="apii">[-(0|1), +0, &ndash;]</span>
5580 <pre>const char *lua_setlocal (lua_State *L, lua_Debug *ar, int n);</pre>
5581
5582 <p>
5583 Sets the value of a local variable of a given activation record.
5584 Parameters <code>ar</code> and <code>n</code> are as in <a href="#lua_getlocal"><code>lua_getlocal</code></a>
5585 (see <a href="#lua_getlocal"><code>lua_getlocal</code></a>).
5586 <a href="#lua_setlocal"><code>lua_setlocal</code></a> assigns the value at the top of the stack
5587 to the variable and returns its name.
5588 It also pops the value from the stack.
5589
5590
5591 <p>
5592 Returns <code>NULL</code> (and pops nothing)
5593 when the index is greater than
5594 the number of active local variables.
5595
5596
5597
5598
5599
5600 <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
5601 <span class="apii">[-(0|1), +0, &ndash;]</span>
5602 <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
5603
5604 <p>
5605 Sets the value of a closure's upvalue.
5606 It assigns the value at the top of the stack
5607 to the upvalue and returns its name.
5608 It also pops the value from the stack.
5609 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
5610 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>).
5611
5612
5613 <p>
5614 Returns <code>NULL</code> (and pops nothing)
5615 when the index is greater than the number of upvalues.
5616
5617
5618
5619
5620
5621 <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
5622 <span class="apii">[-0, +0, &ndash;]</span>
5623 <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre>
5624
5625 <p>
5626 Returns an unique identifier for the upvalue numbered <code>n</code>
5627 from the closure at index <code>funcindex</code>.
5628 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
5629 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>)
5630 (but <code>n</code> cannot be greater than the number of upvalues).
5631
5632
5633 <p>
5634 These unique identifiers allow a program to check whether different
5635 closures share upvalues.
5636 Lua closures that share an upvalue
5637 (that is, that access a same external local variable)
5638 will return identical ids for those upvalue indices.
5639
5640
5641
5642
5643
5644 <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
5645 <span class="apii">[-0, +0, &ndash;]</span>
5646 <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
5647 int funcindex2, int n2);</pre>
5648
5649 <p>
5650 Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
5651 refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
5652
5653
5654
5655 1963
5656 1964
5657 1965
5658 1966
5659 <h1>5 &ndash; <a name="5">The Auxiliary Library</a></h1> 1967 <h1>5 &ndash; <a name="5">The Auxiliary Library</a></h1>
5660 1968
5661 <p> 1969 <p>
5662 1970 Like the previous section, this section is specific to Lua and is not relevant to Luan.
5663 The <em>auxiliary library</em> provides several convenient functions 1971 So this section is just a placeholder.
5664 to interface C with Lua.
5665 While the basic API provides the primitive functions for all
5666 interactions between C and Lua,
5667 the auxiliary library provides higher-level functions for some
5668 common tasks.
5669
5670
5671 <p>
5672 All functions and types from the auxiliary library
5673 are defined in header file <code>lauxlib.h</code> and
5674 have a prefix <code>luaL_</code>.
5675
5676
5677 <p>
5678 All functions in the auxiliary library are built on
5679 top of the basic API,
5680 and so they provide nothing that cannot be done with that API.
5681 Nevertheless, the use of the auxiliary library ensures
5682 more consistency to your code.
5683
5684
5685 <p>
5686 Several functions in the auxiliary library use internally some
5687 extra stack slots.
5688 When a function in the auxiliary library uses less than five slots,
5689 it does not check the stack size;
5690 it simply assumes that there are enough slots.
5691
5692
5693 <p>
5694 Several functions in the auxiliary library are used to
5695 check C&nbsp;function arguments.
5696 Because the error message is formatted for arguments
5697 (e.g., "<code>bad argument #1</code>"),
5698 you should not use these functions for other stack values.
5699
5700
5701 <p>
5702 Functions called <code>luaL_check*</code>
5703 always throw an error if the check is not satisfied.
5704
5705
5706
5707 <h2>5.1 &ndash; <a name="5.1">Functions and Types</a></h2>
5708
5709 <p>
5710 Here we list all functions and types from the auxiliary library
5711 in alphabetical order.
5712
5713
5714
5715 <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
5716 <span class="apii">[-?, +?, <em>e</em>]</span>
5717 <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
5718
5719 <p>
5720 Adds the byte <code>c</code> to the buffer <code>B</code>
5721 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5722
5723
5724
5725
5726
5727 <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
5728 <span class="apii">[-?, +?, <em>e</em>]</span>
5729 <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
5730
5731 <p>
5732 Adds the string pointed to by <code>s</code> with length <code>l</code> to
5733 the buffer <code>B</code>
5734 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5735 The string can contain embedded zeros.
5736
5737
5738
5739
5740
5741 <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
5742 <span class="apii">[-?, +?, <em>e</em>]</span>
5743 <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
5744
5745 <p>
5746 Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
5747 a string of length <code>n</code> previously copied to the
5748 buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
5749
5750
5751
5752
5753
5754 <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
5755 <span class="apii">[-?, +?, <em>e</em>]</span>
5756 <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
5757
5758 <p>
5759 Adds the zero-terminated string pointed to by <code>s</code>
5760 to the buffer <code>B</code>
5761 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5762 The string cannot contain embedded zeros.
5763
5764
5765
5766
5767
5768 <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
5769 <span class="apii">[-1, +?, <em>e</em>]</span>
5770 <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
5771
5772 <p>
5773 Adds the value at the top of the stack
5774 to the buffer <code>B</code>
5775 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5776 Pops the value.
5777
5778
5779 <p>
5780 This is the only function on string buffers that can (and must)
5781 be called with an extra element on the stack,
5782 which is the value to be added to the buffer.
5783
5784
5785
5786
5787
5788 <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
5789 <span class="apii">[-0, +0, <em>v</em>]</span>
5790 <pre>void luaL_argcheck (lua_State *L,
5791 int cond,
5792 int arg,
5793 const char *extramsg);</pre>
5794
5795 <p>
5796 Checks whether <code>cond</code> is true.
5797 If not, raises an error with a standard message.
5798
5799
5800
5801
5802
5803 <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
5804 <span class="apii">[-0, +0, <em>v</em>]</span>
5805 <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre>
5806
5807 <p>
5808 Raises an error with a standard message
5809 that includes <code>extramsg</code> as a comment.
5810
5811
5812 <p>
5813 This function never returns,
5814 but it is an idiom to use it in C&nbsp;functions
5815 as <code>return luaL_argerror(<em>args</em>)</code>.
5816
5817
5818
5819
5820
5821 <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
5822 <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
5823
5824 <p>
5825 Type for a <em>string buffer</em>.
5826
5827
5828 <p>
5829 A string buffer allows C&nbsp;code to build Lua strings piecemeal.
5830 Its pattern of use is as follows:
5831
5832 <ul>
5833
5834 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5835
5836 <li>Then initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>
5837
5838 <li>
5839 Then add string pieces to the buffer calling any of
5840 the <code>luaL_add*</code> functions.
5841 </li>
5842
5843 <li>
5844 Finish by calling <code>luaL_pushresult(&amp;b)</code>.
5845 This call leaves the final string on the top of the stack.
5846 </li>
5847
5848 </ul>
5849
5850 <p>
5851 If you know beforehand the total size of the resulting string,
5852 you can use the buffer like this:
5853
5854 <ul>
5855
5856 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5857
5858 <li>Then initialize it and preallocate a space of
5859 size <code>sz</code> with a call <code>luaL_buffinitsize(L, &amp;b, sz)</code>.</li>
5860
5861 <li>Then copy the string into that space.</li>
5862
5863 <li>
5864 Finish by calling <code>luaL_pushresultsize(&amp;b, sz)</code>,
5865 where <code>sz</code> is the total size of the resulting string
5866 copied into that space.
5867 </li>
5868
5869 </ul>
5870
5871 <p>
5872 During its normal operation,
5873 a string buffer uses a variable number of stack slots.
5874 So, while using a buffer, you cannot assume that you know where
5875 the top of the stack is.
5876 You can use the stack between successive calls to buffer operations
5877 as long as that use is balanced;
5878 that is,
5879 when you call a buffer operation,
5880 the stack is at the same level
5881 it was immediately after the previous buffer operation.
5882 (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
5883 After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
5884 level when the buffer was initialized,
5885 plus the final string on its top.
5886
5887
5888
5889
5890
5891 <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
5892 <span class="apii">[-0, +0, &ndash;]</span>
5893 <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
5894
5895 <p>
5896 Initializes a buffer <code>B</code>.
5897 This function does not allocate any space;
5898 the buffer must be declared as a variable
5899 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5900
5901
5902
5903
5904
5905 <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
5906 <span class="apii">[-?, +?, <em>e</em>]</span>
5907 <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre>
5908
5909 <p>
5910 Equivalent to the sequence
5911 <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>.
5912
5913
5914
5915
5916
5917 <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
5918 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
5919 <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
5920
5921 <p>
5922 Calls a metamethod.
5923
5924
5925 <p>
5926 If the object at index <code>obj</code> has a metatable and this
5927 metatable has a field <code>e</code>,
5928 this function calls this field passing the object as its only argument.
5929 In this case this function returns true and pushes onto the
5930 stack the value returned by the call.
5931 If there is no metatable or no metamethod,
5932 this function returns false (without pushing any value on the stack).
5933
5934
5935
5936
5937
5938 <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
5939 <span class="apii">[-0, +0, <em>v</em>]</span>
5940 <pre>void luaL_checkany (lua_State *L, int arg);</pre>
5941
5942 <p>
5943 Checks whether the function has an argument
5944 of any type (including <b>nil</b>) at position <code>arg</code>.
5945
5946
5947
5948
5949
5950 <hr><h3><a name="luaL_checkint"><code>luaL_checkint</code></a></h3><p>
5951 <span class="apii">[-0, +0, <em>v</em>]</span>
5952 <pre>int luaL_checkint (lua_State *L, int arg);</pre>
5953
5954 <p>
5955 Checks whether the function argument <code>arg</code> is a number
5956 and returns this number cast to an <code>int</code>.
5957
5958
5959
5960
5961
5962 <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
5963 <span class="apii">[-0, +0, <em>v</em>]</span>
5964 <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre>
5965
5966 <p>
5967 Checks whether the function argument <code>arg</code> is a number
5968 and returns this number cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
5969
5970
5971
5972
5973
5974 <hr><h3><a name="luaL_checklong"><code>luaL_checklong</code></a></h3><p>
5975 <span class="apii">[-0, +0, <em>v</em>]</span>
5976 <pre>long luaL_checklong (lua_State *L, int arg);</pre>
5977
5978 <p>
5979 Checks whether the function argument <code>arg</code> is a number
5980 and returns this number cast to a <code>long</code>.
5981
5982
5983
5984
5985
5986 <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
5987 <span class="apii">[-0, +0, <em>v</em>]</span>
5988 <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre>
5989
5990 <p>
5991 Checks whether the function argument <code>arg</code> is a string
5992 and returns this string;
5993 if <code>l</code> is not <code>NULL</code> fills <code>*l</code>
5994 with the string's length.
5995
5996
5997 <p>
5998 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
5999 so all conversions and caveats of that function apply here.
6000
6001
6002
6003
6004
6005 <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
6006 <span class="apii">[-0, +0, <em>v</em>]</span>
6007 <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre>
6008
6009 <p>
6010 Checks whether the function argument <code>arg</code> is a number
6011 and returns this number.
6012
6013
6014
6015
6016
6017 <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
6018 <span class="apii">[-0, +0, <em>v</em>]</span>
6019 <pre>int luaL_checkoption (lua_State *L,
6020 int arg,
6021 const char *def,
6022 const char *const lst[]);</pre>
6023
6024 <p>
6025 Checks whether the function argument <code>arg</code> is a string and
6026 searches for this string in the array <code>lst</code>
6027 (which must be NULL-terminated).
6028 Returns the index in the array where the string was found.
6029 Raises an error if the argument is not a string or
6030 if the string cannot be found.
6031
6032
6033 <p>
6034 If <code>def</code> is not <code>NULL</code>,
6035 the function uses <code>def</code> as a default value when
6036 there is no argument <code>arg</code> or when this argument is <b>nil</b>.
6037
6038
6039 <p>
6040 This is a useful function for mapping strings to C&nbsp;enums.
6041 (The usual convention in Lua libraries is
6042 to use strings instead of numbers to select options.)
6043
6044
6045
6046
6047
6048 <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
6049 <span class="apii">[-0, +0, <em>v</em>]</span>
6050 <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
6051
6052 <p>
6053 Grows the stack size to <code>top + sz</code> elements,
6054 raising an error if the stack cannot grow to that size.
6055 <code>msg</code> is an additional text to go into the error message
6056 (or <code>NULL</code> for no additional text).
6057
6058
6059
6060
6061
6062 <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
6063 <span class="apii">[-0, +0, <em>v</em>]</span>
6064 <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre>
6065
6066 <p>
6067 Checks whether the function argument <code>arg</code> is a string
6068 and returns this string.
6069
6070
6071 <p>
6072 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
6073 so all conversions and caveats of that function apply here.
6074
6075
6076
6077
6078
6079 <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
6080 <span class="apii">[-0, +0, <em>v</em>]</span>
6081 <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre>
6082
6083 <p>
6084 Checks whether the function argument <code>arg</code> has type <code>t</code>.
6085 See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
6086
6087
6088
6089
6090
6091 <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
6092 <span class="apii">[-0, +0, <em>v</em>]</span>
6093 <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre>
6094
6095 <p>
6096 Checks whether the function argument <code>arg</code> is a userdata
6097 of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and
6098 returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
6099
6100
6101
6102
6103
6104 <hr><h3><a name="luaL_checkunsigned"><code>luaL_checkunsigned</code></a></h3><p>
6105 <span class="apii">[-0, +0, <em>v</em>]</span>
6106 <pre>lua_Unsigned luaL_checkunsigned (lua_State *L, int arg);</pre>
6107
6108 <p>
6109 Checks whether the function argument <code>arg</code> is a number
6110 and returns this number cast to a <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>.
6111
6112
6113
6114
6115
6116 <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
6117 <span class="apii">[-0, +0, &ndash;]</span>
6118 <pre>void luaL_checkversion (lua_State *L);</pre>
6119
6120 <p>
6121 Checks whether the core running the call,
6122 the core that created the Lua state,
6123 and the code making the call are all using the same version of Lua.
6124 Also checks whether the core running the call
6125 and the core that created the Lua state
6126 are using the same address space.
6127
6128
6129
6130
6131
6132 <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
6133 <span class="apii">[-0, +?, <em>e</em>]</span>
6134 <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
6135
6136 <p>
6137 Loads and runs the given file.
6138 It is defined as the following macro:
6139
6140 <pre>
6141 (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
6142 </pre><p>
6143 It returns false if there are no errors
6144 or true in case of errors.
6145
6146
6147
6148
6149
6150 <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
6151 <span class="apii">[-0, +?, &ndash;]</span>
6152 <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
6153
6154 <p>
6155 Loads and runs the given string.
6156 It is defined as the following macro:
6157
6158 <pre>
6159 (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
6160 </pre><p>
6161 It returns false if there are no errors
6162 or true in case of errors.
6163
6164
6165
6166
6167
6168 <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
6169 <span class="apii">[-0, +0, <em>v</em>]</span>
6170 <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
6171
6172 <p>
6173 Raises an error.
6174 The error message format is given by <code>fmt</code>
6175 plus any extra arguments,
6176 following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
6177 It also adds at the beginning of the message the file name and
6178 the line number where the error occurred,
6179 if this information is available.
6180
6181
6182 <p>
6183 This function never returns,
6184 but it is an idiom to use it in C&nbsp;functions
6185 as <code>return luaL_error(<em>args</em>)</code>.
6186
6187
6188
6189
6190
6191 <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
6192 <span class="apii">[-0, +3, <em>e</em>]</span>
6193 <pre>int luaL_execresult (lua_State *L, int stat);</pre>
6194
6195 <p>
6196 This function produces the return values for
6197 process-related functions in the standard library
6198 (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
6199
6200
6201
6202
6203
6204 <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
6205 <span class="apii">[-0, +(1|3), <em>e</em>]</span>
6206 <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre>
6207
6208 <p>
6209 This function produces the return values for
6210 file-related functions in the standard library
6211 (<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.).
6212
6213
6214
6215
6216
6217 <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
6218 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
6219 <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
6220
6221 <p>
6222 Pushes onto the stack the field <code>e</code> from the metatable
6223 of the object at index <code>obj</code>.
6224 If the object does not have a metatable,
6225 or if the metatable does not have this field,
6226 returns false and pushes nothing.
6227
6228
6229
6230
6231
6232 <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
6233 <span class="apii">[-0, +1, &ndash;]</span>
6234 <pre>void luaL_getmetatable (lua_State *L, const char *tname);</pre>
6235
6236 <p>
6237 Pushes onto the stack the metatable associated with name <code>tname</code>
6238 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6239
6240
6241
6242
6243
6244 <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
6245 <span class="apii">[-0, +1, <em>e</em>]</span>
6246 <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
6247
6248 <p>
6249 Ensures that the value <code>t[fname]</code>,
6250 where <code>t</code> is the value at index <code>idx</code>,
6251 is a table,
6252 and pushes that table onto the stack.
6253 Returns true if it finds a previous table there
6254 and false if it creates a new table.
6255
6256
6257
6258
6259
6260 <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
6261 <span class="apii">[-0, +1, <em>e</em>]</span>
6262 <pre>const char *luaL_gsub (lua_State *L,
6263 const char *s,
6264 const char *p,
6265 const char *r);</pre>
6266
6267 <p>
6268 Creates a copy of string <code>s</code> by replacing
6269 any occurrence of the string <code>p</code>
6270 with the string <code>r</code>.
6271 Pushes the resulting string on the stack and returns it.
6272
6273
6274
6275
6276
6277 <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
6278 <span class="apii">[-0, +0, <em>e</em>]</span>
6279 <pre>int luaL_len (lua_State *L, int index);</pre>
6280
6281 <p>
6282 Returns the "length" of the value at the given index
6283 as a number;
6284 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.6">&sect;3.4.6</a>).
6285 Raises an error if the result of the operation is not a number.
6286 (This case only can happen through metamethods.)
6287
6288
6289
6290
6291
6292 <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
6293 <span class="apii">[-0, +1, &ndash;]</span>
6294 <pre>int luaL_loadbuffer (lua_State *L,
6295 const char *buff,
6296 size_t sz,
6297 const char *name);</pre>
6298
6299 <p>
6300 Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
6301
6302
6303
6304
6305
6306 <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
6307 <span class="apii">[-0, +1, &ndash;]</span>
6308 <pre>int luaL_loadbufferx (lua_State *L,
6309 const char *buff,
6310 size_t sz,
6311 const char *name,
6312 const char *mode);</pre>
6313
6314 <p>
6315 Loads a buffer as a Lua chunk.
6316 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
6317 buffer pointed to by <code>buff</code> with size <code>sz</code>.
6318
6319
6320 <p>
6321 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
6322 <code>name</code> is the chunk name,
6323 used for debug information and error messages.
6324 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
6325
6326
6327
6328
6329
6330 <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
6331 <span class="apii">[-0, +1, <em>e</em>]</span>
6332 <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
6333
6334 <p>
6335 Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
6336
6337
6338
6339
6340
6341 <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
6342 <span class="apii">[-0, +1, <em>e</em>]</span>
6343 <pre>int luaL_loadfilex (lua_State *L, const char *filename,
6344 const char *mode);</pre>
6345
6346 <p>
6347 Loads a file as a Lua chunk.
6348 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
6349 named <code>filename</code>.
6350 If <code>filename</code> is <code>NULL</code>,
6351 then it loads from the standard input.
6352 The first line in the file is ignored if it starts with a <code>#</code>.
6353
6354
6355 <p>
6356 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
6357
6358
6359 <p>
6360 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
6361 but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
6362 if it cannot open/read the file or the file has a wrong mode.
6363
6364
6365 <p>
6366 As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
6367 it does not run it.
6368
6369
6370
6371
6372
6373 <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
6374 <span class="apii">[-0, +1, &ndash;]</span>
6375 <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
6376
6377 <p>
6378 Loads a string as a Lua chunk.
6379 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
6380 the zero-terminated string <code>s</code>.
6381
6382
6383 <p>
6384 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
6385
6386
6387 <p>
6388 Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
6389 it does not run it.
6390
6391
6392
6393
6394
6395 <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
6396 <span class="apii">[-0, +1, <em>e</em>]</span>
6397 <pre>void luaL_newlib (lua_State *L, const luaL_Reg *l);</pre>
6398
6399 <p>
6400 Creates a new table and registers there
6401 the functions in list <code>l</code>.
6402 It is implemented as the following macro:
6403
6404 <pre>
6405 (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
6406 </pre>
6407
6408
6409
6410
6411 <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
6412 <span class="apii">[-0, +1, <em>e</em>]</span>
6413 <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre>
6414
6415 <p>
6416 Creates a new table with a size optimized
6417 to store all entries in the array <code>l</code>
6418 (but does not actually store them).
6419 It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
6420 (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
6421
6422
6423 <p>
6424 It is implemented as a macro.
6425 The array <code>l</code> must be the actual array,
6426 not a pointer to it.
6427
6428
6429
6430
6431
6432 <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
6433 <span class="apii">[-0, +1, <em>e</em>]</span>
6434 <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
6435
6436 <p>
6437 If the registry already has the key <code>tname</code>,
6438 returns 0.
6439 Otherwise,
6440 creates a new table to be used as a metatable for userdata,
6441 adds it to the registry with key <code>tname</code>,
6442 and returns 1.
6443
6444
6445 <p>
6446 In both cases pushes onto the stack the final value associated
6447 with <code>tname</code> in the registry.
6448
6449
6450
6451
6452
6453 <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
6454 <span class="apii">[-0, +0, &ndash;]</span>
6455 <pre>lua_State *luaL_newstate (void);</pre>
6456
6457 <p>
6458 Creates a new Lua state.
6459 It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
6460 allocator based on the standard&nbsp;C <code>realloc</code> function
6461 and then sets a panic function (see <a href="#4.6">&sect;4.6</a>) that prints
6462 an error message to the standard error output in case of fatal
6463 errors.
6464
6465
6466 <p>
6467 Returns the new state,
6468 or <code>NULL</code> if there is a memory allocation error.
6469
6470
6471
6472
6473
6474 <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
6475 <span class="apii">[-0, +0, <em>e</em>]</span>
6476 <pre>void luaL_openlibs (lua_State *L);</pre>
6477
6478 <p>
6479 Opens all standard Lua libraries into the given state.
6480
6481
6482
6483
6484
6485 <hr><h3><a name="luaL_optint"><code>luaL_optint</code></a></h3><p>
6486 <span class="apii">[-0, +0, <em>v</em>]</span>
6487 <pre>int luaL_optint (lua_State *L, int arg, int d);</pre>
6488
6489 <p>
6490 If the function argument <code>arg</code> is a number,
6491 returns this number cast to an <code>int</code>.
6492 If this argument is absent or is <b>nil</b>,
6493 returns <code>d</code>.
6494 Otherwise, raises an error.
6495
6496
6497
6498
6499
6500 <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
6501 <span class="apii">[-0, +0, <em>v</em>]</span>
6502 <pre>lua_Integer luaL_optinteger (lua_State *L,
6503 int arg,
6504 lua_Integer d);</pre>
6505
6506 <p>
6507 If the function argument <code>arg</code> is a number,
6508 returns this number cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
6509 If this argument is absent or is <b>nil</b>,
6510 returns <code>d</code>.
6511 Otherwise, raises an error.
6512
6513
6514
6515
6516
6517 <hr><h3><a name="luaL_optlong"><code>luaL_optlong</code></a></h3><p>
6518 <span class="apii">[-0, +0, <em>v</em>]</span>
6519 <pre>long luaL_optlong (lua_State *L, int arg, long d);</pre>
6520
6521 <p>
6522 If the function argument <code>arg</code> is a number,
6523 returns this number cast to a <code>long</code>.
6524 If this argument is absent or is <b>nil</b>,
6525 returns <code>d</code>.
6526 Otherwise, raises an error.
6527
6528
6529
6530
6531
6532 <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
6533 <span class="apii">[-0, +0, <em>v</em>]</span>
6534 <pre>const char *luaL_optlstring (lua_State *L,
6535 int arg,
6536 const char *d,
6537 size_t *l);</pre>
6538
6539 <p>
6540 If the function argument <code>arg</code> is a string,
6541 returns this string.
6542 If this argument is absent or is <b>nil</b>,
6543 returns <code>d</code>.
6544 Otherwise, raises an error.
6545
6546
6547 <p>
6548 If <code>l</code> is not <code>NULL</code>,
6549 fills the position <code>*l</code> with the result's length.
6550
6551
6552
6553
6554
6555 <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
6556 <span class="apii">[-0, +0, <em>v</em>]</span>
6557 <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre>
6558
6559 <p>
6560 If the function argument <code>arg</code> is a number,
6561 returns this number.
6562 If this argument is absent or is <b>nil</b>,
6563 returns <code>d</code>.
6564 Otherwise, raises an error.
6565
6566
6567
6568
6569
6570 <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
6571 <span class="apii">[-0, +0, <em>v</em>]</span>
6572 <pre>const char *luaL_optstring (lua_State *L,
6573 int arg,
6574 const char *d);</pre>
6575
6576 <p>
6577 If the function argument <code>arg</code> is a string,
6578 returns this string.
6579 If this argument is absent or is <b>nil</b>,
6580 returns <code>d</code>.
6581 Otherwise, raises an error.
6582
6583
6584
6585
6586
6587 <hr><h3><a name="luaL_optunsigned"><code>luaL_optunsigned</code></a></h3><p>
6588 <span class="apii">[-0, +0, <em>v</em>]</span>
6589 <pre>lua_Unsigned luaL_optunsigned (lua_State *L,
6590 int arg,
6591 lua_Unsigned u);</pre>
6592
6593 <p>
6594 If the function argument <code>arg</code> is a number,
6595 returns this number cast to a <a href="#lua_Unsigned"><code>lua_Unsigned</code></a>.
6596 If this argument is absent or is <b>nil</b>,
6597 returns <code>u</code>.
6598 Otherwise, raises an error.
6599
6600
6601
6602
6603
6604 <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
6605 <span class="apii">[-?, +?, <em>e</em>]</span>
6606 <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
6607
6608 <p>
6609 Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
6610 with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
6611
6612
6613
6614
6615
6616 <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
6617 <span class="apii">[-?, +?, <em>e</em>]</span>
6618 <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre>
6619
6620 <p>
6621 Returns an address to a space of size <code>sz</code>
6622 where you can copy a string to be added to buffer <code>B</code>
6623 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6624 After copying the string into this space you must call
6625 <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
6626 it to the buffer.
6627
6628
6629
6630
6631
6632 <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
6633 <span class="apii">[-?, +1, <em>e</em>]</span>
6634 <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
6635
6636 <p>
6637 Finishes the use of buffer <code>B</code> leaving the final string on
6638 the top of the stack.
6639
6640
6641
6642
6643
6644 <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
6645 <span class="apii">[-?, +1, <em>e</em>]</span>
6646 <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre>
6647
6648 <p>
6649 Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>.
6650
6651
6652
6653
6654
6655 <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
6656 <span class="apii">[-1, +0, <em>e</em>]</span>
6657 <pre>int luaL_ref (lua_State *L, int t);</pre>
6658
6659 <p>
6660 Creates and returns a <em>reference</em>,
6661 in the table at index <code>t</code>,
6662 for the object at the top of the stack (and pops the object).
6663
6664
6665 <p>
6666 A reference is a unique integer key.
6667 As long as you do not manually add integer keys into table <code>t</code>,
6668 <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
6669 You can retrieve an object referred by reference <code>r</code>
6670 by calling <code>lua_rawgeti(L, t, r)</code>.
6671 Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object.
6672
6673
6674 <p>
6675 If the object at the top of the stack is <b>nil</b>,
6676 <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
6677 The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
6678 from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
6679
6680
6681
6682
6683
6684 <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
6685 <pre>typedef struct luaL_Reg {
6686 const char *name;
6687 lua_CFunction func;
6688 } luaL_Reg;</pre>
6689
6690 <p>
6691 Type for arrays of functions to be registered by
6692 <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
6693 <code>name</code> is the function name and <code>func</code> is a pointer to
6694 the function.
6695 Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with an sentinel entry
6696 in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
6697
6698
6699
6700
6701
6702 <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
6703 <span class="apii">[-0, +1, <em>e</em>]</span>
6704 <pre>void luaL_requiref (lua_State *L, const char *modname,
6705 lua_CFunction openf, int glb);</pre>
6706
6707 <p>
6708 Calls function <code>openf</code> with string <code>modname</code> as an argument
6709 and sets the call result in <code>package.loaded[modname]</code>,
6710 as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
6711
6712
6713 <p>
6714 If <code>glb</code> is true,
6715 also stores the result into global <code>modname</code>.
6716
6717
6718 <p>
6719 Leaves a copy of that result on the stack.
6720
6721
6722
6723
6724
6725 <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
6726 <span class="apii">[-nup, +0, <em>e</em>]</span>
6727 <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre>
6728
6729 <p>
6730 Registers all functions in the array <code>l</code>
6731 (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
6732 (below optional upvalues, see next).
6733
6734
6735 <p>
6736 When <code>nup</code> is not zero,
6737 all functions are created sharing <code>nup</code> upvalues,
6738 which must be previously pushed on the stack
6739 on top of the library table.
6740 These values are popped from the stack after the registration.
6741
6742
6743
6744
6745
6746 <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
6747 <span class="apii">[-0, +0, &ndash;]</span>
6748 <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre>
6749
6750 <p>
6751 Sets the metatable of the object at the top of the stack
6752 as the metatable associated with name <code>tname</code>
6753 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6754
6755
6756
6757
6758
6759 <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
6760 <span class="apii">[-0, +0, <em>e</em>]</span>
6761 <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre>
6762
6763 <p>
6764 This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
6765 except that, when the test fails,
6766 it returns <code>NULL</code> instead of throwing an error.
6767
6768
6769
6770
6771
6772 <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
6773 <span class="apii">[-0, +1, <em>e</em>]</span>
6774 <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre>
6775
6776 <p>
6777 Converts any Lua value at the given index to a C&nbsp;string
6778 in a reasonable format.
6779 The resulting string is pushed onto the stack and also
6780 returned by the function.
6781 If <code>len</code> is not <code>NULL</code>,
6782 the function also sets <code>*len</code> with the string length.
6783
6784
6785 <p>
6786 If the value has a metatable with a <code>"__tostring"</code> field,
6787 then <code>luaL_tolstring</code> calls the corresponding metamethod
6788 with the value as argument,
6789 and uses the result of the call as its result.
6790
6791
6792
6793
6794
6795 <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
6796 <span class="apii">[-0, +1, <em>e</em>]</span>
6797 <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
6798 int level);</pre>
6799
6800 <p>
6801 Creates and pushes a traceback of the stack <code>L1</code>.
6802 If <code>msg</code> is not <code>NULL</code> it is appended
6803 at the beginning of the traceback.
6804 The <code>level</code> parameter tells at which level
6805 to start the traceback.
6806
6807
6808
6809
6810
6811 <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
6812 <span class="apii">[-0, +0, &ndash;]</span>
6813 <pre>const char *luaL_typename (lua_State *L, int index);</pre>
6814
6815 <p>
6816 Returns the name of the type of the value at the given index.
6817
6818
6819
6820
6821
6822 <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
6823 <span class="apii">[-0, +0, &ndash;]</span>
6824 <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
6825
6826 <p>
6827 Releases reference <code>ref</code> from the table at index <code>t</code>
6828 (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
6829 The entry is removed from the table,
6830 so that the referred object can be collected.
6831 The reference <code>ref</code> is also freed to be used again.
6832
6833
6834 <p>
6835 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>,
6836 <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
6837
6838
6839
6840
6841
6842 <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
6843 <span class="apii">[-0, +1, <em>e</em>]</span>
6844 <pre>void luaL_where (lua_State *L, int lvl);</pre>
6845
6846 <p>
6847 Pushes onto the stack a string identifying the current position
6848 of the control at level <code>lvl</code> in the call stack.
6849 Typically this string has the following format:
6850
6851 <pre>
6852 <em>chunkname</em>:<em>currentline</em>:
6853 </pre><p>
6854 Level&nbsp;0 is the running function,
6855 level&nbsp;1 is the function that called the running function,
6856 etc.
6857
6858
6859 <p>
6860 This function is used to build a prefix for error messages.
6861
6862
6863 1972
6864 1973
6865 1974
6866 1975
6867 1976