comparison website/src/manual.html.luan @ 386:db23f654f87d

make all of website use luan
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 23 Apr 2015 18:09:12 -0600
parents website/src/manual.html@8557581740db
children 23d075ce1e48
comparison
equal deleted inserted replaced
385:9850d788a20b 386:db23f654f87d
1 local Io = require "luan:Io"
2 local Html = require "luan:Html"
3 local Http = require "luan:web/Http"
4
5
6 function service()
7 Io.stdout = Http.response.text_writer()
8 Html.simply_html_page{
9 head = function() %>
10 <title>Luan Reference Manual</title>
11 <% end;
12 body = function() %>
13
14 <div container>
15 <div><small><a href="/">Luan</a></small></div>
16
17 <h1>Luan Reference Manual</h1>
18
19 <p>
20 <small>
21 Original copyright &copy; 2015 Lua.org, PUC-Rio.
22 Freely available under the terms of the
23 <a href="http://www.lua.org/license.html">Lua license</a>.
24 Modified for Luan.
25 </small>
26 </p>
27
28 <hr/>
29
30 <h2>Contents</h2>
31
32 <div margin-bottom="1em"><a href="#intro">Introduction</a></div>
33
34 <div margin-bottom="1em">
35 <a href="#basic">Basic Concepts</a>
36 <ul>
37 <li><a href="#types">Values and Types</a></li>
38 <li><a href="#env">Environments</a></li>
39 <li><a href="#error">Error Handling</a></li>
40 <li><a href="#meta">Metatables and Metamethods</a></li>
41 <li><a href="#gc">Garbage Collection</a></li>
42 </ul>
43 </div>
44
45 <div margin-bottom="1em">
46 <a href="#lang">The Language</a>
47 <ul>
48 <li><a href="#lex">Lexical Conventions</a></li>
49 <li><a href="#vars">Variables</a></li>
50 </ul>
51 </div>
52
53 <hr/>
54
55
56 <h2 margin-top="1em"><a name="intro">Introduction</a></h2>
57
58 <p>Luan is a high level programming language based on <a href="http://www.lua.org">Lua</a>. A great strength of Lua is its simplicity and Luan takes this even further, being even simpler than Lua. The goal is to provide a simple programming language for the casual programmer with as few concepts as possible so that one can quickly learn the language and then easily understand any code written in Luan.</p>
59
60 <p>Luan is implemented in Java and is tightly coupled with Java. So it makes a great scripting language for Java programmers.</p>
61
62 <p>Unlike Lua which is meant to be embedded, Luan is meant to be a full scripting language. This done not by adding feature to Luan, but rather by providing a complete set of libraries.</p>
63
64
65 <h2 margin-top="1em"><a name="basic">Basic Concepts</a></h2>
66
67 <p>This section describes the basic concepts of the language.</p>
68
69 <h3 margin-top="1em"><a name="types">Values and Types</a></h3>
70
71 <p>
72 Luan is a <i>dynamically typed language</i>.
73 This means that
74 variables do not have types; only values do.
75 There are no type definitions in the language.
76 All values carry their own type.
77
78
79 <p>
80 All values in Luan are <i>first-class values</i>.
81 This means that all values can be stored in variables,
82 passed as arguments to other functions, and returned as results.
83
84
85 <p>
86 There are eight basic types in Luan:
87 <i>nil</i>, <i>boolean</i>, <i>number</i>,
88 <i>string</i>, <i>binary</i>, <i>function</i>, <i>userdata</i>,
89 and <i>table</i>.
90 <i>Nil</i> is the type of the value <b>nil</b>,
91 whose main property is to be different from any other value;
92 it usually represents the absence of a useful value.
93 <i>Nil</i> is implemented as the Java value <i>null</i>.
94 <i>Boolean</i> is the type of the values <b>false</b> and <b>true</b>.
95 <i>Boolean</i> is implemented as the Java class <i>Boolean</i>.
96 <i>Number</i> represents both
97 integer numbers and real (floating-point) numbers.
98 <i>Number</i> is implemented as the Java class <i>Number</i>. Any Java subclass of <i>Number</i> is allowed and this is invisible to the Luan user. Operations on numbers follow the same rules of
99 the underlying Java implementation.
100
101 <i>String</i> is implemented as the Java class <i>String</i>.
102 <i>Binary</i> is implemented as the Java type <i>byte[]</i>.
103
104
105 <p>
106 Luan can call (and manipulate) functions written in Luan and
107 functions written in Java (see <a href="#3.4.10">&sect;3.4.10</a>).
108 Both are represented by the type <i>function</i>.
109
110
111 <p>
112 The type <i>userdata</i> is provided to allow arbitrary Java objects to
113 be stored in Luan variables.
114 A userdata value is a Java object that isn't one of the standard Luan types.
115 Userdata has no predefined operations in Luan,
116 except assignment and identity test.
117 Userdata is useful then Java access is enabled in Luan
118
119
120
121 <p>
122 The type <i>table</i> implements associative arrays,
123 that is, arrays that can be indexed not only with numbers,
124 but with any Luan value except <b>nil</b>.
125 Tables can be <i>heterogeneous</i>;
126 that is, they can contain values of all types (except <b>nil</b>).
127 Any key with value <b>nil</b> is not considered part of the table.
128 Conversely, any key that is not part of a table has
129 an associated value <b>nil</b>.
130
131
132 <p>
133 Tables are the sole data-structuring mechanism in Luan;
134 they can be used to represent ordinary arrays, sequences,
135 symbol tables, sets, records, graphs, trees, etc.
136 To represent records, Luan uses the field name as an index.
137 The language supports this representation by
138 providing <tt>a.name</tt> as syntactic sugar for <tt>a["name"]</tt>.
139 There are several convenient ways to create tables in Luan
140 (see <a href="#3.4.9">&sect;3.4.9</a>).
141
142
143 <p>
144 We use the term <i>sequence</i> to denote a table where
145 the set of all positive numeric keys is equal to {1..<i>n</i>}
146 for some non-negative integer <i>n</i>,
147 which is called the length of the sequence (see <a href="#3.4.7">&sect;3.4.7</a>).
148
149
150 <p>
151 Like indices,
152 the values of table fields can be of any type.
153 In particular,
154 because functions are first-class values,
155 table fields can contain functions.
156 Thus tables can also carry <i>methods</i> (see <a href="#3.4.11">&sect;3.4.11</a>).
157
158
159 <p>
160 The indexing of tables follows
161 the definition of raw equality in the language.
162 The expressions <tt>a[i]</tt> and <tt>a[j]</tt>
163 denote the same table element
164 if and only if <tt>i</tt> and <tt>j</tt> are raw equal
165 (that is, equal without metamethods).
166 In particular, floats with integral values
167 are equal to their respective integers
168 (e.g., <tt>1.0 == 1</tt>).
169
170
171 <p>
172 Luan values are <i>objects</i>:
173 variables do not actually <i>contain</i> values,
174 only <i>references</i> to them.
175 Assignment, parameter passing, and function returns
176 always manipulate references to values;
177 these operations do not imply any kind of copy.
178
179
180 <p>
181 The library function <a href="#pdf-type"><tt>Luan.type</tt></a> returns a string describing the type
182 of a given value (see <a href="#6.1">&sect;6.1</a>).
183
184
185
186
187
188 <h3 margin-top="1em"><a name="env">Environments</a></h3>
189
190 <p>
191 As will be discussed in <a href="#3.2">&sect;3.2</a> and <a href="#3.3.3">&sect;3.3.3</a>,
192 any reference to a free name
193 (that is, a name not bound to any declaration) <tt>var</tt>
194 is syntactically translated to <tt>_ENV.var</tt>.
195 Moreover, every chunk is compiled in the scope of
196 an external local variable named <tt>_ENV</tt> (see <a href="#3.3.2">&sect;3.3.2</a>),
197 so <tt>_ENV</tt> itself is never a free name in a chunk.
198
199
200 <p>
201 Despite the existence of this external <tt>_ENV</tt> variable and
202 the translation of free names,
203 <tt>_ENV</tt> is a completely regular name.
204 In particular,
205 you can define new variables and parameters with that name.
206 Each reference to a free name uses the <tt>_ENV</tt> that is
207 visible at that point in the program,
208 following the usual visibility rules of Luan (see <a href="#3.5">&sect;3.5</a>).
209
210
211 <p>
212 Any table used as the value of <tt>_ENV</tt> is called an <i>environment</i>.
213
214
215 <p>
216 When Luan loads a chunk,
217 the default value for its <tt>_ENV</tt> is an empty table.
218
219 <p>
220 Luan also provides all chunks with two other local values: <tt>require</tt> and <tt>java</tt>. These are functions used to load and access libraries and other modules.
221
222
223
224
225 <h3 margin-top="1em"><a name="error">Error Handling</a></h3>
226
227 <p>
228 Luan code can explicitly generate an error by calling the
229 <a href="#pdf-error"><tt>error</tt></a> function.
230 If you need to catch errors in Luan,
231 you can use <a href="#pdf-pcall"><tt>pcall</tt></a> or <a href="#pdf-xpcall"><tt>try</tt></a>
232 to call a given function in <i>protected mode</i>.
233
234
235 <p>
236 Whenever there is an error,
237 an <i>error object</i> (also called an <i>error message</i>)
238 is propagated with information about the error.
239 Luan itself only generates errors whose error object is a string,
240 but programs may generate errors with
241 any value as the error object.
242 It is up to the Luan program or its host to handle such error objects.
243
244
245
246
247
248 <h3 margin-top="1em"><a name="meta">Metatables and Metamethods</a></h3>
249
250 <p>
251 Every table in Luan can have a <i>metatable</i>.
252 This <i>metatable</i> is an ordinary Luan table
253 that defines the behavior of the original value
254 under certain special operations.
255 You can change several aspects of the behavior
256 of operations over a value by setting specific fields in its metatable.
257 For instance, when a table is the operand of an addition,
258 Luan checks for a function in the field "<tt>__add</tt>" of the table's metatable.
259 If it finds one,
260 Luan calls this function to perform the addition.
261
262
263 <p>
264 The keys in a metatable are derived from the <i>event</i> names;
265 the corresponding values are called <ii>metamethods</i>.
266 In the previous example, the event is <tt>"add"</tt>
267 and the metamethod is the function that performs the addition.
268
269
270 <p>
271 You can query the metatable of any table
272 using the <a href="#pdf-getmetatable"><tt>get_metatable</tt></a> function.
273
274
275 <p>
276 You can replace the metatable of tables
277 using the <a href="#pdf-setmetatable"><tt>set_metatable</tt></a> function.
278
279
280 <p>
281 A metatable controls how a table behaves in
282 arithmetic operations, bitwise operations,
283 order comparisons, concatenation, length operation, calls, and indexing.
284
285
286 <p>
287 A detailed list of events controlled by metatables is given next.
288 Each operation is identified by its corresponding event name.
289 The key for each event is a string with its name prefixed by
290 two underscores, '<tt>__</tt>';
291 for instance, the key for operation "add" is the
292 string "<tt>__add</tt>".
293 Note that queries for metamethods are always raw;
294 the access to a metamethod does not invoke other metamethods.
295 You can emulate how Luan queries a metamethod for an object <tt>obj</tt>
296 with the following code:
297
298 <p><tt><pre>
299 raw_get(get_metatable(obj) or {}, "__" .. event_name)
300 </pre></tt></p>
301
302 <p>
303 Here are the events:
304
305 <ul>
306
307 <li><b>"add": </b>
308 the <tt>+</tt> operation.
309
310 If any operand for an addition is a table,
311 Luan will try to call a metamethod.
312 First, Luan will check the first operand (even if it is valid).
313 If that operand does not define a metamethod for the "<tt>__add</tt>" event,
314 then Luan will check the second operand.
315 If Luan can find a metamethod,
316 it calls the metamethod with the two operands as arguments,
317 and the result of the call
318 (adjusted to one value)
319 is the result of the operation.
320 Otherwise,
321 it raises an error.
322 </li>
323
324 <li><b>"sub": </b>
325 the <tt>-</tt> operation.
326
327 Behavior similar to the "add" operation.
328 </li>
329
330 <li><b>"mul": </b>
331 the <tt>*</tt> operation.
332
333 Behavior similar to the "add" operation.
334 </li>
335
336 <li><b>"div": </b>
337 the <tt>/</tt> operation.
338
339 Behavior similar to the "add" operation.
340 </li>
341
342 <li><b>"mod": </b>
343 the <tt>%</tt> operation.
344
345 Behavior similar to the "add" operation.
346 </li>
347
348 <li><b>"pow": </b>
349 the <tt>^</tt> (exponentiation) operation.
350
351 Behavior similar to the "add" operation.
352 </li>
353
354 <li><b>"unm": </b>
355 the <tt>-</tt> (unary minus) operation.
356
357 Behavior similar to the "add" operation.
358 </li>
359
360 <li><b>"concat": </b>
361 the <tt>..</tt> (concatenation) operation.
362
363 Behavior similar to the "add" operation.
364 </li>
365
366 <li><b>"len": </b>
367 the <tt>#</tt> (length) operation.
368
369 If there is a metamethod,
370 Luan calls it with the object as argument,
371 and the result of the call
372 (always adjusted to one value)
373 is the result of the operation.
374 If there is no metamethod but the object is a table,
375 then Luan uses the table length operation (see <a href="#3.4.7">&sect;3.4.7</a>).
376 Otherwise, Luan raises an error.
377 </li>
378
379 <li><b>"eq": </b>
380 the <tt>==</tt> (equal) operation.
381
382 Behavior similar to the "add" operation,
383 except that Luan will try a metamethod only when the values
384 being compared are both tables
385 and they are not primitively equal.
386 The result of the call is always converted to a boolean.
387 </li>
388
389 <li><b>"lt": </b>
390 the <tt>&lt;</tt> (less than) operation.
391
392 Behavior similar to the "add" operation.
393 The result of the call is always converted to a boolean.
394 </li>
395
396 <li><b>"le": </b>
397 the <tt>&lt;=</tt> (less equal) operation.
398
399 Unlike other operations,
400 The less-equal operation can use two different events.
401 First, Luan looks for the "<tt>__le</tt>" metamethod in both operands,
402 like in the "lt" operation.
403 If it cannot find such a metamethod,
404 then it will try the "<tt>__lt</tt>" event,
405 assuming that <tt>a &lt;= b</tt> is equivalent to <tt>not (b &lt; a)</tt>.
406 As with the other comparison operators,
407 the result is always a boolean.
408 </li>
409
410 <li><b>"index": </b>
411 The indexing access <tt>table[key]</tt>.
412
413 This event happens
414 when <tt>key</tt> is not present in <tt>table</tt>.
415 The metamethod is looked up in <tt>table</tt>.
416
417
418 <p>
419 Despite the name,
420 the metamethod for this event can be either a function or a table.
421 If it is a function,
422 it is called with <tt>table</tt> and <tt>key</tt> as arguments.
423 If it is a table,
424 the final result is the result of indexing this table with <tt>key</tt>.
425 (This indexing is regular, not raw,
426 and therefore can trigger another metamethod.)
427 </li>
428
429 <li><b>"newindex": </b>
430 The indexing assignment <tt>table[key] = value</tt>.
431
432 Like the index event,
433 this event happens when
434 when <tt>key</tt> is not present in <tt>table</tt>.
435 The metamethod is looked up in <tt>table</tt>.
436
437
438 <p>
439 Like with indexing,
440 the metamethod for this event can be either a function or a table.
441 If it is a function,
442 it is called with <tt>table</tt>, <tt>key</tt>, and <tt>value</tt> as arguments.
443 If it is a table,
444 Luan does an indexing assignment to this table with the same key and value.
445 (This assignment is regular, not raw,
446 and therefore can trigger another metamethod.)
447
448
449 <p>
450 Whenever there is a "newindex" metamethod,
451 Luan does not perform the primitive assignment.
452 (If necessary,
453 the metamethod itself can call <a href="#pdf-rawset"><tt>raw_set</tt></a>
454 to do the assignment.)
455 </li>
456
457 <li><b>"call": </b>
458 The call operation <tt>func(args)</tt>.
459
460 This event happens when Luan tries to call a table.
461 The metamethod is looked up in <tt>func</tt>.
462 If present,
463 the metamethod is called with <tt>func</tt> as its first argument,
464 followed by the arguments of the original call (<tt>args</tt>).
465 </li>
466
467 </ul>
468
469
470
471
472 <h3 margin-top="1em"><a name="gc">Garbage Collection</a></h3>
473
474 <p>
475 Luan uses Java's garbage collection.
476
477
478
479
480
481 <h2 margin-top="1em"><a name="lang">The Language</a></h2>
482
483 <p>
484 This section describes the lexis, the syntax, and the semantics of Luan.
485 In other words,
486 this section describes
487 which tokens are valid,
488 how they can be combined,
489 and what their combinations mean.
490
491
492 <p>
493 Language constructs will be explained using the usual extended BNF notation,
494 in which
495 {<i>a</i>}&nbsp;means&nbsp;0 or more <i>a</i>'s, and
496 [<i>a</i>]&nbsp;means an optional <i>a</i>.
497 Non-terminals are shown like non-terminal,
498 keywords are shown like <b>kword</b>,
499 and other terminal symbols are shown like &lsquo;<b>=</b>&rsquo;.
500 The complete syntax of Luan can be found in <a href="#9">&sect;9</a>
501 at the end of this manual.
502
503
504
505 <h3 margin-top="1em"><a name="lex">Lexical Conventions</a></h3>
506
507 <p>
508 Luan ignores spaces and comments
509 between lexical elements (tokens),
510 except as delimiters between names and keywords.
511 Luan generally considers the end of a line to be the end of a statement. This catches errors and encourages readability. The exception to this is in paranthesis ( <i>(...)</i>, <i>[...]</i>, and <i>{...}</i> ) where the end of line is treated as white space.
512
513 <p>
514 <i>Names</i>
515 (also called <i>identifiers</i>)
516 in Luan can be any string of letters,
517 digits, and underscores,
518 not beginning with a digit.
519 Identifiers are used to name variables, table fields, and labels.
520
521
522 <p>
523 The following <i>keywords</i> are reserved
524 and cannot be used as names:
525
526
527 <p><pre>
528 and break do else elseif end
529 false for function goto if in
530 local nil not or repeat return
531 then true until while
532 </pre></p>
533
534 <p>
535 Luan is a case-sensitive language:
536 <tt>and</tt> is a reserved word, but <tt>And</tt> and <tt>AND</tt>
537 are two different, valid names.
538
539
540 <p>
541 The following strings denote other tokens:
542
543 <p><pre>
544 + - * / % ^ #
545 &amp; ~ | &lt;&lt; &gt;&gt; //
546 == ~= &lt;= &gt;= &lt; &gt; =
547 ( ) { } [ ] ::
548 ; : , . .. ...
549 </pre></p>
550
551 <p>
552 <i>Literal strings</i>
553 can be delimited by matching single or double quotes,
554 and can contain the following C-like escape sequences:
555 '<tt>\a</tt>' (bell),
556 '<tt>\b</tt>' (backspace),
557 '<tt>\f</tt>' (form feed),
558 '<tt>\n</tt>' (newline),
559 '<tt>\r</tt>' (carriage return),
560 '<tt>\t</tt>' (horizontal tab),
561 '<tt>\v</tt>' (vertical tab),
562 '<tt>\\</tt>' (backslash),
563 '<tt>\"</tt>' (quotation mark [double quote]),
564 and '<tt>\'</tt>' (apostrophe [single quote]).
565 A backslash followed by a real newline
566 results in a newline in the string.
567 The escape sequence '<tt>\z</tt>' skips the following span
568 of white-space characters,
569 including line breaks;
570 it is particularly useful to break and indent a long literal string
571 into multiple lines without adding the newlines and spaces
572 into the string contents.
573
574
575 <p>
576 Luan can specify any character in a literal string by its numerical value.
577 This can be done
578 with the escape sequence <tt>\x<i>XX</i></tt>,
579 where <i>XX</i> is a sequence of exactly two hexadecimal digits,
580 or with the escape sequence <tt>\<i>ddd</i></tt>,
581 where <i>ddd</i> is a sequence of up to three decimal digits.
582 (Note that if a decimal escape sequence is to be followed by a digit,
583 it must be expressed using exactly three digits.)
584
585
586 <p>
587 Literal strings can also be defined using a long format
588 enclosed by <i>long brackets</i>.
589 We define an <i>opening long bracket of level <i>n</i></i> as an opening
590 square bracket followed by <i>n</i> equal signs followed by another
591 opening square bracket.
592 So, an opening long bracket of level 0 is written as <tt>[[</tt>,
593 an opening long bracket of level 1 is written as <tt>[=[</tt>,
594 and so on.
595 A <i>closing long bracket</i> is defined similarly;
596 for instance,
597 a closing long bracket of level 4 is written as <tt>]====]</tt>.
598 A <i>long literal</i> starts with an opening long bracket of any level and
599 ends at the first closing long bracket of the same level.
600 It can contain any text except a closing bracket of the same level.
601 Literals in this bracketed form can run for several lines,
602 do not interpret any escape sequences,
603 and ignore long brackets of any other level.
604 Any kind of end-of-line sequence
605 (carriage return, newline, carriage return followed by newline,
606 or newline followed by carriage return)
607 is converted to a simple newline.
608
609
610 <p>
611 Any character in a literal string not
612 explicitly affected by the previous rules represents itself.
613 However, Luan opens files for parsing in text mode,
614 and the system file functions may have problems with
615 some control characters.
616 So, it is safer to represent
617 non-text data as a quoted literal with
618 explicit escape sequences for non-text characters.
619
620
621 <p>
622 For convenience,
623 when the opening long bracket is immediately followed by a newline,
624 the newline is not included in the string.
625 As an example
626 the five literal strings below denote the same string:
627
628 <p><pre>
629 a = 'alo\n123"'
630 a = "alo\n123\""
631 a = '\97lo\10\04923"'
632 a = [[alo
633 123"]]
634 a = [==[
635 alo
636 123"]==]
637 </pre></p>
638
639 <p>
640 A <i>numerical constant</i> (or <i>numeral</i>)
641 can be written with an optional fractional part
642 and an optional decimal exponent,
643 marked by a letter '<tt>e</tt>' or '<tt>E</tt>'.
644 Luan also accepts hexadecimal constants,
645 which start with <tt>0x</tt> or <tt>0X</tt>.
646 Hexadecimal constants also accept an optional fractional part
647 plus an optional binary exponent,
648 marked by a letter '<tt>p</tt>' or '<tt>P</tt>'.
649 A numeric constant with a fractional dot or an exponent
650 denotes a float;
651 otherwise it denotes an integer.
652 Examples of valid integer constants are
653
654 <p><pre>
655 3 345 0xff 0xBEBADA
656 </pre></p>
657
658 <p>
659 Examples of valid float constants are
660
661 <p><pre>
662 3.0 3.1416 314.16e-2 0.31416E1 34e1
663 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
664 </pre></p>
665
666 <p>
667 A <i>comment</i> starts with a double hyphen (<tt>--</tt>)
668 anywhere outside a string.
669 If the text immediately after <tt>--</tt> is not an opening long bracket,
670 the comment is a <i>short comment</i>,
671 which runs until the end of the line.
672 Otherwise, it is a <i>long comment</i>,
673 which runs until the corresponding closing long bracket.
674 Long comments are frequently used to disable code temporarily.
675
676
677
678
679
680 <h3 margin-top="1em"><a name="vars">Variables</a></h3>
681
682 <p>
683 Variables are places that store values.
684 There are three kinds of variables in Luan:
685 global variables, local variables, and table fields.
686
687
688 <p>
689 A single name can denote a global variable or a local variable
690 (or a function's formal parameter,
691 which is a particular kind of local variable):
692
693 <p><pre>
694 var ::= Name
695 </pre></p>
696
697 <p>
698 Name denotes identifiers, as defined in <a href="#3.1">&sect;3.1</a>.
699
700
701 <p>
702 Any variable name is assumed to be global unless explicitly declared
703 as a local (see <a href="#3.3.7">&sect;3.3.7</a>).
704 Local variables are <i>lexically scoped</i>:
705 local variables can be freely accessed by functions
706 defined inside their scope (see <a href="#3.5">&sect;3.5</a>).
707
708
709 <p>
710 Before the first assignment to a variable, its value is <b>nil</b>.
711
712
713 <p>
714 Square brackets are used to index a table:
715
716 <p><pre>
717 var ::= prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo;
718 </pre></p>
719
720 <p>
721 The meaning of accesses to table fields can be changed via metatables.
722 An access to an indexed variable <tt>t[i]</tt> is equivalent to
723 a call <tt>gettable_event(t,i)</tt>.
724 (See <a href="#2.4">&sect;2.4</a> for a complete description of the
725 <tt>gettable_event</tt> function.
726 This function is not defined or callable in Luan.
727 We use it here only for explanatory purposes.)
728
729
730 <p>
731 The syntax <tt>var.Name</tt> is just syntactic sugar for
732 <tt>var["Name"]</tt>:
733
734 <p><pre>
735 var ::= prefixexp &lsquo;<b>.</b>&rsquo; Name
736 </pre></p>
737
738 <p>
739 An access to a global variable <tt>x</tt>
740 is equivalent to <tt>_ENV.x</tt>.
741 Due to the way that chunks are compiled,
742 <tt>_ENV</tt> is never a global name (see <a href="#2.2">&sect;2.2</a>).
743
744
745
746
747
748 <h2>3.3 &ndash; <a name="3.3">Statements</a></h2>
749
750 <p>
751 Lua supports an almost conventional set of statements,
752 similar to those in Pascal or C.
753 This set includes
754 assignments, control structures, function calls,
755 and variable declarations.
756
757
758
759 <h3>3.3.1 &ndash; <a name="3.3.1">Blocks</a></h3>
760
761 <p>
762 A block is a list of statements,
763 which are executed sequentially:
764
765 <pre>
766 block ::= {stat}
767 </pre><p>
768 Lua has <em>empty statements</em>
769 that allow you to separate statements with semicolons,
770 start a block with a semicolon
771 or write two semicolons in sequence:
772
773 <pre>
774 stat ::= &lsquo;<b>;</b>&rsquo;
775 </pre>
776
777 <p>
778 Function calls and assignments
779 can start with an open parenthesis.
780 This possibility leads to an ambiguity in Lua's grammar.
781 Consider the following fragment:
782
783 <pre>
784 a = b + c
785 (print or io.write)('done')
786 </pre><p>
787 The grammar could see it in two ways:
788
789 <pre>
790 a = b + c(print or io.write)('done')
791
792 a = b + c; (print or io.write)('done')
793 </pre><p>
794 The current parser always sees such constructions
795 in the first way,
796 interpreting the open parenthesis
797 as the start of the arguments to a call.
798 To avoid this ambiguity,
799 it is a good practice to always precede with a semicolon
800 statements that start with a parenthesis:
801
802 <pre>
803 ;(print or io.write)('done')
804 </pre>
805
806 <p>
807 A block can be explicitly delimited to produce a single statement:
808
809 <pre>
810 stat ::= <b>do</b> block <b>end</b>
811 </pre><p>
812 Explicit blocks are useful
813 to control the scope of variable declarations.
814 Explicit blocks are also sometimes used to
815 add a <b>return</b> statement in the middle
816 of another block (see <a href="#3.3.4">&sect;3.3.4</a>).
817
818
819
820
821
822 <h3>3.3.2 &ndash; <a name="3.3.2">Chunks</a></h3>
823
824 <p>
825 The unit of compilation of Lua is called a <em>chunk</em>.
826 Syntactically,
827 a chunk is simply a block:
828
829 <pre>
830 chunk ::= block
831 </pre>
832
833 <p>
834 Lua handles a chunk as the body of an anonymous function
835 with a variable number of arguments
836 (see <a href="#3.4.11">&sect;3.4.11</a>).
837 As such, chunks can define local variables,
838 receive arguments, and return values.
839 Moreover, such anonymous function is compiled as in the
840 scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">&sect;2.2</a>).
841 The resulting function always has <code>_ENV</code> as its only upvalue,
842 even if it does not use that variable.
843
844
845 <p>
846 A chunk can be stored in a file or in a string inside the host program.
847 To execute a chunk,
848 Lua first <em>loads</em> it,
849 precompiling the chunk's code into instructions for a virtual machine,
850 and then Lua executes the compiled code
851 with an interpreter for the virtual machine.
852
853
854 <p>
855 Chunks can also be precompiled into binary form;
856 see program <code>luac</code> and function <a href="#pdf-string.dump"><code>string.dump</code></a> for details.
857 Programs in source and compiled forms are interchangeable;
858 Lua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>).
859
860
861
862
863
864 <h3>3.3.3 &ndash; <a name="3.3.3">Assignment</a></h3>
865
866 <p>
867 Lua allows multiple assignments.
868 Therefore, the syntax for assignment
869 defines a list of variables on the left side
870 and a list of expressions on the right side.
871 The elements in both lists are separated by commas:
872
873 <pre>
874 stat ::= varlist &lsquo;<b>=</b>&rsquo; explist
875 varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
876 explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
877 </pre><p>
878 Expressions are discussed in <a href="#3.4">&sect;3.4</a>.
879
880
881 <p>
882 Before the assignment,
883 the list of values is <em>adjusted</em> to the length of
884 the list of variables.
885 If there are more values than needed,
886 the excess values are thrown away.
887 If there are fewer values than needed,
888 the list is extended with as many <b>nil</b>'s as needed.
889 If the list of expressions ends with a function call,
890 then all values returned by that call enter the list of values,
891 before the adjustment
892 (except when the call is enclosed in parentheses; see <a href="#3.4">&sect;3.4</a>).
893
894
895 <p>
896 The assignment statement first evaluates all its expressions
897 and only then the assignments are performed.
898 Thus the code
899
900 <pre>
901 i = 3
902 i, a[i] = i+1, 20
903 </pre><p>
904 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
905 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
906 before it is assigned&nbsp;4.
907 Similarly, the line
908
909 <pre>
910 x, y = y, x
911 </pre><p>
912 exchanges the values of <code>x</code> and <code>y</code>,
913 and
914
915 <pre>
916 x, y, z = y, z, x
917 </pre><p>
918 cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
919
920
921 <p>
922 The meaning of assignments to global variables
923 and table fields can be changed via metatables.
924 An assignment to an indexed variable <code>t[i] = val</code> is equivalent to
925 <code>settable_event(t,i,val)</code>.
926 (See <a href="#2.4">&sect;2.4</a> for a complete description of the
927 <code>settable_event</code> function.
928 This function is not defined or callable in Lua.
929 We use it here only for explanatory purposes.)
930
931
932 <p>
933 An assignment to a global name <code>x = val</code>
934 is equivalent to the assignment
935 <code>_ENV.x = val</code> (see <a href="#2.2">&sect;2.2</a>).
936
937
938
939
940
941 <h3>3.3.4 &ndash; <a name="3.3.4">Control Structures</a></h3><p>
942 The control structures
943 <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
944 familiar syntax:
945
946
947
948
949 <pre>
950 stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
951 stat ::= <b>repeat</b> block <b>until</b> exp
952 stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
953 </pre><p>
954 Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">&sect;3.3.5</a>).
955
956
957 <p>
958 The condition expression of a
959 control structure can return any value.
960 Both <b>false</b> and <b>nil</b> are considered false.
961 All values different from <b>nil</b> and <b>false</b> are considered true
962 (in particular, the number 0 and the empty string are also true).
963
964
965 <p>
966 In the <b>repeat</b>&ndash;<b>until</b> loop,
967 the inner block does not end at the <b>until</b> keyword,
968 but only after the condition.
969 So, the condition can refer to local variables
970 declared inside the loop block.
971
972
973 <p>
974 The <b>goto</b> statement transfers the program control to a label.
975 For syntactical reasons,
976 labels in Lua are considered statements too:
977
978
979
980 <pre>
981 stat ::= <b>goto</b> Name
982 stat ::= label
983 label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
984 </pre>
985
986 <p>
987 A label is visible in the entire block where it is defined,
988 except
989 inside nested blocks where a label with the same name is defined and
990 inside nested functions.
991 A goto may jump to any visible label as long as it does not
992 enter into the scope of a local variable.
993
994
995 <p>
996 Labels and empty statements are called <em>void statements</em>,
997 as they perform no actions.
998
999
1000 <p>
1001 The <b>break</b> statement terminates the execution of a
1002 <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
1003 skipping to the next statement after the loop:
1004
1005
1006 <pre>
1007 stat ::= <b>break</b>
1008 </pre><p>
1009 A <b>break</b> ends the innermost enclosing loop.
1010
1011
1012 <p>
1013 The <b>return</b> statement is used to return values
1014 from a function or a chunk
1015 (which is an anonymous function).
1016
1017 Functions can return more than one value,
1018 so the syntax for the <b>return</b> statement is
1019
1020 <pre>
1021 stat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
1022 </pre>
1023
1024 <p>
1025 The <b>return</b> statement can only be written
1026 as the last statement of a block.
1027 If it is really necessary to <b>return</b> in the middle of a block,
1028 then an explicit inner block can be used,
1029 as in the idiom <code>do return end</code>,
1030 because now <b>return</b> is the last statement in its (inner) block.
1031
1032
1033
1034
1035
1036 <h3>3.3.5 &ndash; <a name="3.3.5">For Statement</a></h3>
1037
1038 <p>
1039
1040 The <b>for</b> statement has two forms:
1041 one numeric and one generic.
1042
1043
1044 <p>
1045 The numeric <b>for</b> loop repeats a block of code while a
1046 control variable runs through an arithmetic progression.
1047 It has the following syntax:
1048
1049 <pre>
1050 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>
1051 </pre><p>
1052 The <em>block</em> is repeated for <em>name</em> starting at the value of
1053 the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
1054 third <em>exp</em>.
1055 More precisely, a <b>for</b> statement like
1056
1057 <pre>
1058 for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end
1059 </pre><p>
1060 is equivalent to the code:
1061
1062 <pre>
1063 do
1064 local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
1065 if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
1066 <em>var</em> = <em>var</em> - <em>step</em>
1067 while true do
1068 <em>var</em> = <em>var</em> + <em>step</em>
1069 if (<em>step</em> &gt;= 0 and <em>var</em> &gt; <em>limit</em>) or (<em>step</em> &lt; 0 and <em>var</em> &lt; <em>limit</em>) then
1070 break
1071 end
1072 local v = <em>var</em>
1073 <em>block</em>
1074 end
1075 end
1076 </pre>
1077
1078 <p>
1079 Note the following:
1080
1081 <ul>
1082
1083 <li>
1084 All three control expressions are evaluated only once,
1085 before the loop starts.
1086 They must all result in numbers.
1087 </li>
1088
1089 <li>
1090 <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
1091 The names shown here are for explanatory purposes only.
1092 </li>
1093
1094 <li>
1095 If the third expression (the step) is absent,
1096 then a step of&nbsp;1 is used.
1097 </li>
1098
1099 <li>
1100 You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop.
1101 </li>
1102
1103 <li>
1104 The loop variable <code>v</code> is local to the loop body.
1105 If you need its value after the loop,
1106 assign it to another variable before exiting the loop.
1107 </li>
1108
1109 </ul>
1110
1111 <p>
1112 The generic <b>for</b> statement works over functions,
1113 called <em>iterators</em>.
1114 On each iteration, the iterator function is called to produce a new value,
1115 stopping when this new value is <b>nil</b>.
1116 The generic <b>for</b> loop has the following syntax:
1117
1118 <pre>
1119 stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
1120 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
1121 </pre><p>
1122 A <b>for</b> statement like
1123
1124 <pre>
1125 for <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> in <em>explist</em> do <em>block</em> end
1126 </pre><p>
1127 is equivalent to the code:
1128
1129 <pre>
1130 do
1131 local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>
1132 while true do
1133 local <em>var_1</em>, &middot;&middot;&middot;, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)
1134 if <em>var_1</em> == nil then break end
1135 <em>var</em> = <em>var_1</em>
1136 <em>block</em>
1137 end
1138 end
1139 </pre><p>
1140 Note the following:
1141
1142 <ul>
1143
1144 <li>
1145 <code><em>explist</em></code> is evaluated only once.
1146 Its results are an <em>iterator</em> function,
1147 a <em>state</em>,
1148 and an initial value for the first <em>iterator variable</em>.
1149 </li>
1150
1151 <li>
1152 <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.
1153 The names are here for explanatory purposes only.
1154 </li>
1155
1156 <li>
1157 You can use <b>break</b> to exit a <b>for</b> loop.
1158 </li>
1159
1160 <li>
1161 The loop variables <code><em>var_i</em></code> are local to the loop;
1162 you cannot use their values after the <b>for</b> ends.
1163 If you need these values,
1164 then assign them to other variables before breaking or exiting the loop.
1165 </li>
1166
1167 </ul>
1168
1169
1170
1171
1172 <h3>3.3.6 &ndash; <a name="3.3.6">Function Calls as Statements</a></h3><p>
1173 To allow possible side-effects,
1174 function calls can be executed as statements:
1175
1176 <pre>
1177 stat ::= functioncall
1178 </pre><p>
1179 In this case, all returned values are thrown away.
1180 Function calls are explained in <a href="#3.4.10">&sect;3.4.10</a>.
1181
1182
1183
1184
1185
1186 <h3>3.3.7 &ndash; <a name="3.3.7">Local Declarations</a></h3><p>
1187 Local variables can be declared anywhere inside a block.
1188 The declaration can include an initial assignment:
1189
1190 <pre>
1191 stat ::= <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
1192 </pre><p>
1193 If present, an initial assignment has the same semantics
1194 of a multiple assignment (see <a href="#3.3.3">&sect;3.3.3</a>).
1195 Otherwise, all variables are initialized with <b>nil</b>.
1196
1197
1198 <p>
1199 A chunk is also a block (see <a href="#3.3.2">&sect;3.3.2</a>),
1200 and so local variables can be declared in a chunk outside any explicit block.
1201
1202
1203 <p>
1204 The visibility rules for local variables are explained in <a href="#3.5">&sect;3.5</a>.
1205
1206
1207
1208
1209
1210
1211
1212 <h2>3.4 &ndash; <a name="3.4">Expressions</a></h2>
1213
1214 <p>
1215 The basic expressions in Lua are the following:
1216
1217 <pre>
1218 exp ::= prefixexp
1219 exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
1220 exp ::= Numeral
1221 exp ::= LiteralString
1222 exp ::= functiondef
1223 exp ::= tableconstructor
1224 exp ::= &lsquo;<b>...</b>&rsquo;
1225 exp ::= exp binop exp
1226 exp ::= unop exp
1227 prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
1228 </pre>
1229
1230 <p>
1231 Numerals and literal strings are explained in <a href="#3.1">&sect;3.1</a>;
1232 variables are explained in <a href="#3.2">&sect;3.2</a>;
1233 function definitions are explained in <a href="#3.4.11">&sect;3.4.11</a>;
1234 function calls are explained in <a href="#3.4.10">&sect;3.4.10</a>;
1235 table constructors are explained in <a href="#3.4.9">&sect;3.4.9</a>.
1236 Vararg expressions,
1237 denoted by three dots ('<code>...</code>'), can only be used when
1238 directly inside a vararg function;
1239 they are explained in <a href="#3.4.11">&sect;3.4.11</a>.
1240
1241
1242 <p>
1243 Binary operators comprise arithmetic operators (see <a href="#3.4.1">&sect;3.4.1</a>),
1244 bitwise operators (see <a href="#3.4.2">&sect;3.4.2</a>),
1245 relational operators (see <a href="#3.4.4">&sect;3.4.4</a>), logical operators (see <a href="#3.4.5">&sect;3.4.5</a>),
1246 and the concatenation operator (see <a href="#3.4.6">&sect;3.4.6</a>).
1247 Unary operators comprise the unary minus (see <a href="#3.4.1">&sect;3.4.1</a>),
1248 the unary bitwise not (see <a href="#3.4.2">&sect;3.4.2</a>),
1249 the unary logical <b>not</b> (see <a href="#3.4.5">&sect;3.4.5</a>),
1250 and the unary <em>length operator</em> (see <a href="#3.4.7">&sect;3.4.7</a>).
1251
1252
1253 <p>
1254 Both function calls and vararg expressions can result in multiple values.
1255 If a function call is used as a statement (see <a href="#3.3.6">&sect;3.3.6</a>),
1256 then its return list is adjusted to zero elements,
1257 thus discarding all returned values.
1258 If an expression is used as the last (or the only) element
1259 of a list of expressions,
1260 then no adjustment is made
1261 (unless the expression is enclosed in parentheses).
1262 In all other contexts,
1263 Lua adjusts the result list to one element,
1264 either discarding all values except the first one
1265 or adding a single <b>nil</b> if there are no values.
1266
1267
1268 <p>
1269 Here are some examples:
1270
1271 <pre>
1272 f() -- adjusted to 0 results
1273 g(f(), x) -- f() is adjusted to 1 result
1274 g(x, f()) -- g gets x plus all results from f()
1275 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
1276 a,b = ... -- a gets the first vararg parameter, b gets
1277 -- the second (both a and b can get nil if there
1278 -- is no corresponding vararg parameter)
1279
1280 a,b,c = x, f() -- f() is adjusted to 2 results
1281 a,b,c = f() -- f() is adjusted to 3 results
1282 return f() -- returns all results from f()
1283 return ... -- returns all received vararg parameters
1284 return x,y,f() -- returns x, y, and all results from f()
1285 {f()} -- creates a list with all results from f()
1286 {...} -- creates a list with all vararg parameters
1287 {f(), nil} -- f() is adjusted to 1 result
1288 </pre>
1289
1290 <p>
1291 Any expression enclosed in parentheses always results in only one value.
1292 Thus,
1293 <code>(f(x,y,z))</code> is always a single value,
1294 even if <code>f</code> returns several values.
1295 (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
1296 or <b>nil</b> if <code>f</code> does not return any values.)
1297
1298
1299
1300 <h3>3.4.1 &ndash; <a name="3.4.1">Arithmetic Operators</a></h3><p>
1301 Lua supports the following arithmetic operators:
1302
1303 <ul>
1304 <li><b><code>+</code>: </b>addition</li>
1305 <li><b><code>-</code>: </b>subtraction</li>
1306 <li><b><code>*</code>: </b>multiplication</li>
1307 <li><b><code>/</code>: </b>float division</li>
1308 <li><b><code>//</code>: </b>floor division</li>
1309 <li><b><code>%</code>: </b>modulo</li>
1310 <li><b><code>^</code>: </b>exponentiation</li>
1311 <li><b><code>-</code>: </b>unary minus</li>
1312 </ul>
1313
1314 <p>
1315 With the exception of exponentiation and float division,
1316 the arithmetic operators work as follows:
1317 If both operands are integers,
1318 the operation is performed over integers and the result is an integer.
1319 Otherwise, if both operands are numbers
1320 or strings that can be converted to
1321 numbers (see <a href="#3.4.3">&sect;3.4.3</a>),
1322 then they are converted to floats,
1323 the operation is performed following the usual rules
1324 for floating-point arithmetic
1325 (usually the IEEE 754 standard),
1326 and the result is a float.
1327
1328
1329 <p>
1330 Exponentiation and float division (<code>/</code>)
1331 always convert their operands to floats
1332 and the result is always a float.
1333 Exponentiation uses the ISO&nbsp;C function <code>pow</code>,
1334 so that it works for non-integer exponents too.
1335
1336
1337 <p>
1338 Floor division (<code>//</code>) is a division
1339 that rounds the quotient towards minus infinite,
1340 that is, the floor of the division of its operands.
1341
1342
1343 <p>
1344 Modulo is defined as the remainder of a division
1345 that rounds the quotient towards minus infinite (floor division).
1346
1347
1348 <p>
1349 In case of overflows in integer arithmetic,
1350 all operations <em>wrap around</em>,
1351 according to the usual rules of two-complement arithmetic.
1352 (In other words,
1353 they return the unique representable integer
1354 that is equal modulo <em>2<sup>64</sup></em> to the mathematical result.)
1355
1356
1357
1358 <h3>3.4.2 &ndash; <a name="3.4.2">Bitwise Operators</a></h3><p>
1359 Lua supports the following bitwise operators:
1360
1361 <ul>
1362 <li><b><code>&amp;</code>: </b>bitwise and</li>
1363 <li><b><code>&#124;</code>: </b>bitwise or</li>
1364 <li><b><code>~</code>: </b>bitwise exclusive or</li>
1365 <li><b><code>&gt;&gt;</code>: </b>right shift</li>
1366 <li><b><code>&lt;&lt;</code>: </b>left shift</li>
1367 <li><b><code>~</code>: </b>unary bitwise not</li>
1368 </ul>
1369
1370 <p>
1371 All bitwise operations convert its operands to integers
1372 (see <a href="#3.4.3">&sect;3.4.3</a>),
1373 operate on all bits of those integers,
1374 and result in an integer.
1375
1376
1377 <p>
1378 Both right and left shifts fill the vacant bits with zeros.
1379 Negative displacements shift to the other direction;
1380 displacements with absolute values equal to or higher than
1381 the number of bits in an integer
1382 result in zero (as all bits are shifted out).
1383
1384
1385
1386
1387
1388 <h3>3.4.3 &ndash; <a name="3.4.3">Coercions and Conversions</a></h3><p>
1389 Lua provides some automatic conversions between some
1390 types and representations at run time.
1391 Bitwise operators always convert float operands to integers.
1392 Exponentiation and float division
1393 always convert integer operands to floats.
1394 All other arithmetic operations applied to mixed numbers
1395 (integers and floats) convert the integer operand to a float;
1396 this is called the <em>usual rule</em>.
1397 The C API also converts both integers to floats and
1398 floats to integers, as needed.
1399 Moreover, string concatenation accepts numbers as arguments,
1400 besides strings.
1401
1402
1403 <p>
1404 Lua also converts strings to numbers,
1405 whenever a number is expected.
1406
1407
1408 <p>
1409 In a conversion from integer to float,
1410 if the integer value has an exact representation as a float,
1411 that is the result.
1412 Otherwise,
1413 the conversion gets the nearest higher or
1414 the nearest lower representable value.
1415 This kind of conversion never fails.
1416
1417
1418 <p>
1419 The conversion from float to integer
1420 checks whether the float has an exact representation as an integer
1421 (that is, the float has an integral value and
1422 it is in the range of integer representation).
1423 If it does, that representation is the result.
1424 Otherwise, the conversion fails.
1425
1426
1427 <p>
1428 The conversion from strings to numbers goes as follows:
1429 First, the string is converted to an integer or a float,
1430 following its syntax and the rules of the Lua lexer.
1431 (The string may have also leading and trailing spaces and a sign.)
1432 Then, the resulting number is converted to the required type
1433 (float or integer) according to the previous rules.
1434
1435
1436 <p>
1437 The conversion from numbers to strings uses a
1438 non-specified human-readable format.
1439 For complete control over how numbers are converted to strings,
1440 use the <code>format</code> function from the string library
1441 (see <a href="#pdf-string.format"><code>string.format</code></a>).
1442
1443
1444
1445
1446
1447 <h3>3.4.4 &ndash; <a name="3.4.4">Relational Operators</a></h3><p>
1448 Lua supports the following relational operators:
1449
1450 <ul>
1451 <li><b><code>==</code>: </b>equality</li>
1452 <li><b><code>~=</code>: </b>inequality</li>
1453 <li><b><code>&lt;</code>: </b>less than</li>
1454 <li><b><code>&gt;</code>: </b>greater than</li>
1455 <li><b><code>&lt;=</code>: </b>less or equal</li>
1456 <li><b><code>&gt;=</code>: </b>greater or equal</li>
1457 </ul><p>
1458 These operators always result in <b>false</b> or <b>true</b>.
1459
1460
1461 <p>
1462 Equality (<code>==</code>) first compares the type of its operands.
1463 If the types are different, then the result is <b>false</b>.
1464 Otherwise, the values of the operands are compared.
1465 Strings are compared in the obvious way.
1466 Numbers follow the usual rule for binary operations:
1467 if both operands are integers,
1468 they are compared as integers;
1469 otherwise, they are converted to floats
1470 and compared as such.
1471
1472
1473 <p>
1474 Tables, userdata, and threads
1475 are compared by reference:
1476 two objects are considered equal only if they are the same object.
1477 Every time you create a new object
1478 (a table, userdata, or thread),
1479 this new object is different from any previously existing object.
1480 Closures with the same reference are always equal.
1481 Closures with any detectable difference
1482 (different behavior, different definition) are always different.
1483
1484
1485 <p>
1486 You can change the way that Lua compares tables and userdata
1487 by using the "eq" metamethod (see <a href="#2.4">&sect;2.4</a>).
1488
1489
1490 <p>
1491 Equality comparisons do not convert strings to numbers
1492 or vice versa.
1493 Thus, <code>"0"==0</code> evaluates to <b>false</b>,
1494 and <code>t[0]</code> and <code>t["0"]</code> denote different
1495 entries in a table.
1496
1497
1498 <p>
1499 The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
1500
1501
1502 <p>
1503 The order operators work as follows.
1504 If both arguments are numbers,
1505 then they are compared following
1506 the usual rule for binary operations.
1507 Otherwise, if both arguments are strings,
1508 then their values are compared according to the current locale.
1509 Otherwise, Lua tries to call the "lt" or the "le"
1510 metamethod (see <a href="#2.4">&sect;2.4</a>).
1511 A comparison <code>a &gt; b</code> is translated to <code>b &lt; a</code>
1512 and <code>a &gt;= b</code> is translated to <code>b &lt;= a</code>.
1513
1514
1515
1516
1517
1518 <h3>3.4.5 &ndash; <a name="3.4.5">Logical Operators</a></h3><p>
1519 The logical operators in Lua are
1520 <b>and</b>, <b>or</b>, and <b>not</b>.
1521 Like the control structures (see <a href="#3.3.4">&sect;3.3.4</a>),
1522 all logical operators consider both <b>false</b> and <b>nil</b> as false
1523 and anything else as true.
1524
1525
1526 <p>
1527 The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
1528 The conjunction operator <b>and</b> returns its first argument
1529 if this value is <b>false</b> or <b>nil</b>;
1530 otherwise, <b>and</b> returns its second argument.
1531 The disjunction operator <b>or</b> returns its first argument
1532 if this value is different from <b>nil</b> and <b>false</b>;
1533 otherwise, <b>or</b> returns its second argument.
1534 Both <b>and</b> and <b>or</b> use short-circuit evaluation;
1535 that is,
1536 the second operand is evaluated only if necessary.
1537 Here are some examples:
1538
1539 <pre>
1540 10 or 20 --&gt; 10
1541 10 or error() --&gt; 10
1542 nil or "a" --&gt; "a"
1543 nil and 10 --&gt; nil
1544 false and error() --&gt; false
1545 false and nil --&gt; false
1546 false or nil --&gt; nil
1547 10 and 20 --&gt; 20
1548 </pre><p>
1549 (In this manual,
1550 <code>--&gt;</code> indicates the result of the preceding expression.)
1551
1552
1553
1554
1555
1556 <h3>3.4.6 &ndash; <a name="3.4.6">Concatenation</a></h3><p>
1557 The string concatenation operator in Lua is
1558 denoted by two dots ('<code>..</code>').
1559 If both operands are strings or numbers, then they are converted to
1560 strings according to the rules described in <a href="#3.4.3">&sect;3.4.3</a>.
1561 Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">&sect;2.4</a>).
1562
1563
1564
1565
1566
1567 <h3>3.4.7 &ndash; <a name="3.4.7">The Length Operator</a></h3>
1568
1569 <p>
1570 The length operator is denoted by the unary prefix operator <code>#</code>.
1571 The length of a string is its number of bytes
1572 (that is, the usual meaning of string length when each
1573 character is one byte).
1574
1575
1576 <p>
1577 A program can modify the behavior of the length operator for
1578 any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">&sect;2.4</a>).
1579
1580
1581 <p>
1582 Unless a <code>__len</code> metamethod is given,
1583 the length of a table <code>t</code> is only defined if the
1584 table is a <em>sequence</em>,
1585 that is,
1586 the set of its positive numeric keys is equal to <em>{1..n}</em>
1587 for some non-negative integer <em>n</em>.
1588 In that case, <em>n</em> is its length.
1589 Note that a table like
1590
1591 <pre>
1592 {10, 20, nil, 40}
1593 </pre><p>
1594 is not a sequence, because it has the key <code>4</code>
1595 but does not have the key <code>3</code>.
1596 (So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal
1597 to the set of positive numeric keys of that table.)
1598 Note, however, that non-numeric keys do not interfere
1599 with whether a table is a sequence.
1600
1601
1602
1603
1604
1605 <h3>3.4.8 &ndash; <a name="3.4.8">Precedence</a></h3><p>
1606 Operator precedence in Lua follows the table below,
1607 from lower to higher priority:
1608
1609 <pre>
1610 or
1611 and
1612 &lt; &gt; &lt;= &gt;= ~= ==
1613 |
1614 ~
1615 &amp;
1616 &lt;&lt; &gt;&gt;
1617 ..
1618 + -
1619 * / // %
1620 unary operators (not # - ~)
1621 ^
1622 </pre><p>
1623 As usual,
1624 you can use parentheses to change the precedences of an expression.
1625 The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
1626 operators are right associative.
1627 All other binary operators are left associative.
1628
1629
1630
1631
1632
1633 <h3>3.4.9 &ndash; <a name="3.4.9">Table Constructors</a></h3><p>
1634 Table constructors are expressions that create tables.
1635 Every time a constructor is evaluated, a new table is created.
1636 A constructor can be used to create an empty table
1637 or to create a table and initialize some of its fields.
1638 The general syntax for constructors is
1639
1640 <pre>
1641 tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
1642 fieldlist ::= field {fieldsep field} [fieldsep]
1643 field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
1644 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
1645 </pre>
1646
1647 <p>
1648 Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
1649 with key <code>exp1</code> and value <code>exp2</code>.
1650 A field of the form <code>name = exp</code> is equivalent to
1651 <code>["name"] = exp</code>.
1652 Finally, fields of the form <code>exp</code> are equivalent to
1653 <code>[i] = exp</code>, where <code>i</code> are consecutive integers
1654 starting with 1.
1655 Fields in the other formats do not affect this counting.
1656 For example,
1657
1658 <pre>
1659 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
1660 </pre><p>
1661 is equivalent to
1662
1663 <pre>
1664 do
1665 local t = {}
1666 t[f(1)] = g
1667 t[1] = "x" -- 1st exp
1668 t[2] = "y" -- 2nd exp
1669 t.x = 1 -- t["x"] = 1
1670 t[3] = f(x) -- 3rd exp
1671 t[30] = 23
1672 t[4] = 45 -- 4th exp
1673 a = t
1674 end
1675 </pre>
1676
1677 <p>
1678 The order of the assignments in a constructor is undefined.
1679 (This order would be relevant only when there are repeated keys.)
1680
1681
1682 <p>
1683 If the last field in the list has the form <code>exp</code>
1684 and the expression is a function call or a vararg expression,
1685 then all values returned by this expression enter the list consecutively
1686 (see <a href="#3.4.10">&sect;3.4.10</a>).
1687
1688
1689 <p>
1690 The field list can have an optional trailing separator,
1691 as a convenience for machine-generated code.
1692
1693
1694
1695
1696
1697 <h3>3.4.10 &ndash; <a name="3.4.10">Function Calls</a></h3><p>
1698 A function call in Lua has the following syntax:
1699
1700 <pre>
1701 functioncall ::= prefixexp args
1702 </pre><p>
1703 In a function call,
1704 first prefixexp and args are evaluated.
1705 If the value of prefixexp has type <em>function</em>,
1706 then this function is called
1707 with the given arguments.
1708 Otherwise, the prefixexp "call" metamethod is called,
1709 having as first parameter the value of prefixexp,
1710 followed by the original call arguments
1711 (see <a href="#2.4">&sect;2.4</a>).
1712
1713
1714 <p>
1715 The form
1716
1717 <pre>
1718 functioncall ::= prefixexp &lsquo;<b>:</b>&rsquo; Name args
1719 </pre><p>
1720 can be used to call "methods".
1721 A call <code>v:name(<em>args</em>)</code>
1722 is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
1723 except that <code>v</code> is evaluated only once.
1724
1725
1726 <p>
1727 Arguments have the following syntax:
1728
1729 <pre>
1730 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo;
1731 args ::= tableconstructor
1732 args ::= LiteralString
1733 </pre><p>
1734 All argument expressions are evaluated before the call.
1735 A call of the form <code>f{<em>fields</em>}</code> is
1736 syntactic sugar for <code>f({<em>fields</em>})</code>;
1737 that is, the argument list is a single new table.
1738 A call of the form <code>f'<em>string</em>'</code>
1739 (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)
1740 is syntactic sugar for <code>f('<em>string</em>')</code>;
1741 that is, the argument list is a single literal string.
1742
1743
1744 <p>
1745 A call of the form <code>return <em>functioncall</em></code> is called
1746 a <em>tail call</em>.
1747 Lua implements <em>proper tail calls</em>
1748 (or <em>proper tail recursion</em>):
1749 in a tail call,
1750 the called function reuses the stack entry of the calling function.
1751 Therefore, there is no limit on the number of nested tail calls that
1752 a program can execute.
1753 However, a tail call erases any debug information about the
1754 calling function.
1755 Note that a tail call only happens with a particular syntax,
1756 where the <b>return</b> has one single function call as argument;
1757 this syntax makes the calling function return exactly
1758 the returns of the called function.
1759 So, none of the following examples are tail calls:
1760
1761 <pre>
1762 return (f(x)) -- results adjusted to 1
1763 return 2 * f(x)
1764 return x, f(x) -- additional results
1765 f(x); return -- results discarded
1766 return x or f(x) -- results adjusted to 1
1767 </pre>
1768
1769
1770
1771
1772 <h3>3.4.11 &ndash; <a name="3.4.11">Function Definitions</a></h3>
1773
1774 <p>
1775 The syntax for function definition is
1776
1777 <pre>
1778 functiondef ::= <b>function</b> funcbody
1779 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
1780 </pre>
1781
1782 <p>
1783 The following syntactic sugar simplifies function definitions:
1784
1785 <pre>
1786 stat ::= <b>function</b> funcname funcbody
1787 stat ::= <b>local</b> <b>function</b> Name funcbody
1788 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
1789 </pre><p>
1790 The statement
1791
1792 <pre>
1793 function f () <em>body</em> end
1794 </pre><p>
1795 translates to
1796
1797 <pre>
1798 f = function () <em>body</em> end
1799 </pre><p>
1800 The statement
1801
1802 <pre>
1803 function t.a.b.c.f () <em>body</em> end
1804 </pre><p>
1805 translates to
1806
1807 <pre>
1808 t.a.b.c.f = function () <em>body</em> end
1809 </pre><p>
1810 The statement
1811
1812 <pre>
1813 local function f () <em>body</em> end
1814 </pre><p>
1815 translates to
1816
1817 <pre>
1818 local f; f = function () <em>body</em> end
1819 </pre><p>
1820 not to
1821
1822 <pre>
1823 local f = function () <em>body</em> end
1824 </pre><p>
1825 (This only makes a difference when the body of the function
1826 contains references to <code>f</code>.)
1827
1828
1829 <p>
1830 A function definition is an executable expression,
1831 whose value has type <em>function</em>.
1832 When Lua precompiles a chunk,
1833 all its function bodies are precompiled too.
1834 Then, whenever Lua executes the function definition,
1835 the function is <em>instantiated</em> (or <em>closed</em>).
1836 This function instance (or <em>closure</em>)
1837 is the final value of the expression.
1838
1839
1840 <p>
1841 Parameters act as local variables that are
1842 initialized with the argument values:
1843
1844 <pre>
1845 parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
1846 </pre><p>
1847 When a function is called,
1848 the list of arguments is adjusted to
1849 the length of the list of parameters,
1850 unless the function is a <em>vararg function</em>,
1851 which is indicated by three dots ('<code>...</code>')
1852 at the end of its parameter list.
1853 A vararg function does not adjust its argument list;
1854 instead, it collects all extra arguments and supplies them
1855 to the function through a <em>vararg expression</em>,
1856 which is also written as three dots.
1857 The value of this expression is a list of all actual extra arguments,
1858 similar to a function with multiple results.
1859 If a vararg expression is used inside another expression
1860 or in the middle of a list of expressions,
1861 then its return list is adjusted to one element.
1862 If the expression is used as the last element of a list of expressions,
1863 then no adjustment is made
1864 (unless that last expression is enclosed in parentheses).
1865
1866
1867 <p>
1868 As an example, consider the following definitions:
1869
1870 <pre>
1871 function f(a, b) end
1872 function g(a, b, ...) end
1873 function r() return 1,2,3 end
1874 </pre><p>
1875 Then, we have the following mapping from arguments to parameters and
1876 to the vararg expression:
1877
1878 <pre>
1879 CALL PARAMETERS
1880
1881 f(3) a=3, b=nil
1882 f(3, 4) a=3, b=4
1883 f(3, 4, 5) a=3, b=4
1884 f(r(), 10) a=1, b=10
1885 f(r()) a=1, b=2
1886
1887 g(3) a=3, b=nil, ... --&gt; (nothing)
1888 g(3, 4) a=3, b=4, ... --&gt; (nothing)
1889 g(3, 4, 5, 8) a=3, b=4, ... --&gt; 5 8
1890 g(5, r()) a=5, b=1, ... --&gt; 2 3
1891 </pre>
1892
1893 <p>
1894 Results are returned using the <b>return</b> statement (see <a href="#3.3.4">&sect;3.3.4</a>).
1895 If control reaches the end of a function
1896 without encountering a <b>return</b> statement,
1897 then the function returns with no results.
1898
1899
1900 <p>
1901
1902 There is a system-dependent limit on the number of values
1903 that a function may return.
1904 This limit is guaranteed to be larger than 1000.
1905
1906
1907 <p>
1908 The <em>colon</em> syntax
1909 is used for defining <em>methods</em>,
1910 that is, functions that have an implicit extra parameter <code>self</code>.
1911 Thus, the statement
1912
1913 <pre>
1914 function t.a.b.c:f (<em>params</em>) <em>body</em> end
1915 </pre><p>
1916 is syntactic sugar for
1917
1918 <pre>
1919 t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
1920 </pre>
1921
1922
1923
1924
1925
1926
1927 <h2>3.5 &ndash; <a name="3.5">Visibility Rules</a></h2>
1928
1929 <p>
1930
1931 Lua is a lexically scoped language.
1932 The scope of a local variable begins at the first statement after
1933 its declaration and lasts until the last non-void statement
1934 of the innermost block that includes the declaration.
1935 Consider the following example:
1936
1937 <pre>
1938 x = 10 -- global variable
1939 do -- new block
1940 local x = x -- new 'x', with value 10
1941 print(x) --&gt; 10
1942 x = x+1
1943 do -- another block
1944 local x = x+1 -- another 'x'
1945 print(x) --&gt; 12
1946 end
1947 print(x) --&gt; 11
1948 end
1949 print(x) --&gt; 10 (the global one)
1950 </pre>
1951
1952 <p>
1953 Notice that, in a declaration like <code>local x = x</code>,
1954 the new <code>x</code> being declared is not in scope yet,
1955 and so the second <code>x</code> refers to the outside variable.
1956
1957
1958 <p>
1959 Because of the lexical scoping rules,
1960 local variables can be freely accessed by functions
1961 defined inside their scope.
1962 A local variable used by an inner function is called
1963 an <em>upvalue</em>, or <em>external local variable</em>,
1964 inside the inner function.
1965
1966
1967 <p>
1968 Notice that each execution of a <b>local</b> statement
1969 defines new local variables.
1970 Consider the following example:
1971
1972 <pre>
1973 a = {}
1974 local x = 20
1975 for i=1,10 do
1976 local y = 0
1977 a[i] = function () y=y+1; return x+y end
1978 end
1979 </pre><p>
1980 The loop creates ten closures
1981 (that is, ten instances of the anonymous function).
1982 Each of these closures uses a different <code>y</code> variable,
1983 while all of them share the same <code>x</code>.
1984
1985
1986
1987
1988
1989 <h1>4 &ndash; <a name="4">The Application Program Interface</a></h1>
1990
1991 <p>
1992
1993 This section describes the C&nbsp;API for Lua, that is,
1994 the set of C&nbsp;functions available to the host program to communicate
1995 with Lua.
1996 All API functions and related types and constants
1997 are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
1998
1999
2000 <p>
2001 Even when we use the term "function",
2002 any facility in the API may be provided as a macro instead.
2003 Except where stated otherwise,
2004 all such macros use each of their arguments exactly once
2005 (except for the first argument, which is always a Lua state),
2006 and so do not generate any hidden side-effects.
2007
2008
2009 <p>
2010 As in most C&nbsp;libraries,
2011 the Lua API functions do not check their arguments for validity or consistency.
2012 However, you can change this behavior by compiling Lua
2013 with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
2014
2015
2016
2017 <h2>4.1 &ndash; <a name="4.1">The Stack</a></h2>
2018
2019 <p>
2020 Lua uses a <em>virtual stack</em> to pass values to and from C.
2021 Each element in this stack represents a Lua value
2022 (<b>nil</b>, number, string, etc.).
2023
2024
2025 <p>
2026 Whenever Lua calls C, the called function gets a new stack,
2027 which is independent of previous stacks and of stacks of
2028 C&nbsp;functions that are still active.
2029 This stack initially contains any arguments to the C&nbsp;function
2030 and it is where the C&nbsp;function pushes its results
2031 to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
2032
2033
2034 <p>
2035 For convenience,
2036 most query operations in the API do not follow a strict stack discipline.
2037 Instead, they can refer to any element in the stack
2038 by using an <em>index</em>:
2039 A positive index represents an absolute stack position
2040 (starting at&nbsp;1);
2041 a negative index represents an offset relative to the top of the stack.
2042 More specifically, if the stack has <em>n</em> elements,
2043 then index&nbsp;1 represents the first element
2044 (that is, the element that was pushed onto the stack first)
2045 and
2046 index&nbsp;<em>n</em> represents the last element;
2047 index&nbsp;-1 also represents the last element
2048 (that is, the element at the&nbsp;top)
2049 and index <em>-n</em> represents the first element.
2050
2051
2052
2053
2054
2055 <h2>4.2 &ndash; <a name="4.2">Stack Size</a></h2>
2056
2057 <p>
2058 When you interact with the Lua API,
2059 you are responsible for ensuring consistency.
2060 In particular,
2061 <em>you are responsible for controlling stack overflow</em>.
2062 You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
2063 to ensure that the stack has enough space for pushing new elements.
2064
2065
2066 <p>
2067 Whenever Lua calls C,
2068 it ensures that the stack has space for
2069 at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
2070 <code>LUA_MINSTACK</code> is defined as 20,
2071 so that usually you do not have to worry about stack space
2072 unless your code has loops pushing elements onto the stack.
2073
2074
2075 <p>
2076 When you call a Lua function
2077 without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
2078 Lua ensures that the stack has enough space for all results,
2079 but it does not ensure any extra space.
2080 So, before pushing anything in the stack after such a call
2081 you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2082
2083
2084
2085
2086
2087 <h2>4.3 &ndash; <a name="4.3">Valid and Acceptable Indices</a></h2>
2088
2089 <p>
2090 Any function in the API that receives stack indices
2091 works only with <em>valid indices</em> or <em>acceptable indices</em>.
2092
2093
2094 <p>
2095 A <em>valid index</em> is an index that refers to a
2096 real position within the stack, that is,
2097 its position lies between&nbsp;1 and the stack top
2098 (<code>1 &le; abs(index) &le; top</code>).
2099
2100 Usually, functions that can modify the value at an index
2101 require valid indices.
2102
2103
2104 <p>
2105 Unless otherwise noted,
2106 any function that accepts valid indices also accepts <em>pseudo-indices</em>,
2107 which represent some Lua values that are accessible to C&nbsp;code
2108 but which are not in the stack.
2109 Pseudo-indices are used to access the registry
2110 and the upvalues of a C&nbsp;function (see <a href="#4.4">&sect;4.4</a>).
2111
2112
2113 <p>
2114 Functions that do not need a specific stack position,
2115 but only a value in the stack (e.g., query functions),
2116 can be called with acceptable indices.
2117 An <em>acceptable index</em> can be any valid index,
2118 including the pseudo-indices,
2119 but it also can be any positive index after the stack top
2120 within the space allocated for the stack,
2121 that is, indices up to the stack size.
2122 (Note that 0 is never an acceptable index.)
2123 Except when noted otherwise,
2124 functions in the API work with acceptable indices.
2125
2126
2127 <p>
2128 Acceptable indices serve to avoid extra tests
2129 against the stack top when querying the stack.
2130 For instance, a C&nbsp;function can query its third argument
2131 without the need to first check whether there is a third argument,
2132 that is, without the need to check whether 3 is a valid index.
2133
2134
2135 <p>
2136 For functions that can be called with acceptable indices,
2137 any non-valid index is treated as if it
2138 contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
2139 which behaves like a nil value.
2140
2141
2142
2143
2144
2145 <h2>4.4 &ndash; <a name="4.4">C Closures</a></h2>
2146
2147 <p>
2148 When a C&nbsp;function is created,
2149 it is possible to associate some values with it,
2150 thus creating a <em>C&nbsp;closure</em>
2151 (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
2152 these values are called <em>upvalues</em> and are
2153 accessible to the function whenever it is called.
2154
2155
2156 <p>
2157 Whenever a C&nbsp;function is called,
2158 its upvalues are located at specific pseudo-indices.
2159 These pseudo-indices are produced by the macro
2160 <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
2161 The first value associated with a function is at position
2162 <code>lua_upvalueindex(1)</code>, and so on.
2163 Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
2164 where <em>n</em> is greater than the number of upvalues of the
2165 current function (but not greater than 256),
2166 produces an acceptable but invalid index.
2167
2168
2169
2170
2171
2172 <h2>4.5 &ndash; <a name="4.5">Registry</a></h2>
2173
2174 <p>
2175 Lua provides a <em>registry</em>,
2176 a predefined table that can be used by any C&nbsp;code to
2177 store whatever Lua values it needs to store.
2178 The registry table is always located at pseudo-index
2179 <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>,
2180 which is a valid index.
2181 Any C&nbsp;library can store data into this table,
2182 but it must take care to choose keys
2183 that are different from those used
2184 by other libraries, to avoid collisions.
2185 Typically, you should use as key a string containing your library name,
2186 or a light userdata with the address of a C&nbsp;object in your code,
2187 or any Lua object created by your code.
2188 As with variable names,
2189 string keys starting with an underscore followed by
2190 uppercase letters are reserved for Lua.
2191
2192
2193 <p>
2194 The integer keys in the registry are used
2195 by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>)
2196 and by some predefined values.
2197 Therefore, integer keys must not be used for other purposes.
2198
2199
2200 <p>
2201 When you create a new Lua state,
2202 its registry comes with some predefined values.
2203 These predefined values are indexed with integer keys
2204 defined as constants in <code>lua.h</code>.
2205 The following constants are defined:
2206
2207 <ul>
2208 <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has
2209 the main thread of the state.
2210 (The main thread is the one created together with the state.)
2211 </li>
2212
2213 <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has
2214 the global environment.
2215 </li>
2216 </ul>
2217
2218
2219
2220
2221 <h2>4.6 &ndash; <a name="4.6">Error Handling in C</a></h2>
2222
2223 <p>
2224 Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
2225 (Lua will use exceptions if you compile it as C++;
2226 search for <code>LUAI_THROW</code> in the source code for details.)
2227 When Lua faces any error
2228 (such as a memory allocation error, type errors, syntax errors,
2229 and runtime errors)
2230 it <em>raises</em> an error;
2231 that is, it does a long jump.
2232 A <em>protected environment</em> uses <code>setjmp</code>
2233 to set a recovery point;
2234 any error jumps to the most recent active recovery point.
2235
2236
2237 <p>
2238 If an error happens outside any protected environment,
2239 Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
2240 and then calls <code>abort</code>,
2241 thus exiting the host application.
2242 Your panic function can avoid this exit by
2243 never returning
2244 (e.g., doing a long jump to your own recovery point outside Lua).
2245
2246
2247 <p>
2248 The panic function runs as if it were a message handler (see <a href="#2.3">&sect;2.3</a>);
2249 in particular, the error message is at the top of the stack.
2250 However, there is no guarantee about stack space.
2251 To push anything on the stack,
2252 the panic function must first check the available space (see <a href="#4.2">&sect;4.2</a>).
2253
2254
2255 <p>
2256 Most functions in the API can raise an error,
2257 for instance due to a memory allocation error.
2258 The documentation for each function indicates whether
2259 it can raise errors.
2260
2261
2262 <p>
2263 Inside a C&nbsp;function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>.
2264
2265
2266
2267
2268
2269 <h2>4.7 &ndash; <a name="4.7">Handling Yields in C</a></h2>
2270
2271 <p>
2272 Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2273 Therefore, if a C function <code>foo</code> calls an API function
2274 and this API function yields
2275 (directly or indirectly by calling another function that yields),
2276 Lua cannot return to <code>foo</code> any more,
2277 because the <code>longjmp</code> removes its frame from the C stack.
2278
2279
2280 <p>
2281 To avoid this kind of problem,
2282 Lua raises an error whenever it tries to yield across an API call,
2283 except for three functions:
2284 <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>.
2285 All those functions receive a <em>continuation function</em>
2286 (as a parameter named <code>k</code>) to continue execution after a yield.
2287
2288
2289 <p>
2290 We need to set some terminology to explain continuations.
2291 We have a C function called from Lua which we will call
2292 the <em>original function</em>.
2293 This original function then calls one of those three functions in the C API,
2294 which we will call the <em>callee function</em>,
2295 that then yields the current thread.
2296 (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
2297 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>
2298 and the function called by them yields.)
2299
2300
2301 <p>
2302 Suppose the running thread yields while executing the callee function.
2303 After the thread resumes,
2304 it eventually will finish running the callee function.
2305 However,
2306 the callee function cannot return to the original function,
2307 because its frame in the C stack was destroyed by the yield.
2308 Instead, Lua calls a <em>continuation function</em>,
2309 which was given as an argument to the callee function.
2310 As the name implies,
2311 the continuation function should continue the task
2312 of the original function.
2313
2314
2315 <p>
2316 As an illustration, consider the following function:
2317
2318 <pre>
2319 int original_function (lua_State *L) {
2320 ... /* code 1 */
2321 status = lua_pcall(L, n, m, h); /* calls Lua */
2322 ... /* code 2 */
2323 }
2324 </pre><p>
2325 Now we want to allow
2326 the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
2327 First, we can rewrite our function like here:
2328
2329 <pre>
2330 int k (lua_State *L, int status, lua_KContext ctx) {
2331 ... /* code 2 */
2332 }
2333
2334 int original_function (lua_State *L) {
2335 ... /* code 1 */
2336 return k(L, lua_pcall(L, n, m, h), ctx);
2337 }
2338 </pre><p>
2339 In the above code,
2340 the new function <code>k</code> is a
2341 <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
2342 which should do all the work that the original function
2343 was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
2344 Now, we must inform Lua that it must call <code>k</code> if the Lua code
2345 being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
2346 (errors or yielding),
2347 so we rewrite the code as here,
2348 replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>:
2349
2350 <pre>
2351 int original_function (lua_State *L) {
2352 ... /* code 1 */
2353 return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
2354 }
2355 </pre><p>
2356 Note the external, explicit call to the continuation:
2357 Lua will call the continuation only if needed, that is,
2358 in case of errors or resuming after a yield.
2359 If the called function returns normally without ever yielding,
2360 <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally.
2361 (Of course, instead of calling the continuation in that case,
2362 you can do the equivalent work directly inside the original function.)
2363
2364
2365 <p>
2366 Besides the Lua state,
2367 the continuation function has two other parameters:
2368 the final status of the call plus the context value (<code>ctx</code>) that
2369 was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2370 (Lua does not use this context value;
2371 it only passes this value from the original function to the
2372 continuation function.)
2373 For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2374 the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2375 except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield
2376 (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
2377 For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
2378 the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
2379 (For these two functions,
2380 Lua will not call the continuation in case of errors,
2381 because they do not handle errors.)
2382 Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
2383 you should call the continuation function
2384 with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
2385 (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
2386 directly the continuation function,
2387 because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
2388
2389
2390 <p>
2391 Lua treats the continuation function as if it were the original function.
2392 The continuation function receives the same Lua stack
2393 from the original function,
2394 in the same state it would be if the callee function had returned.
2395 (For instance,
2396 after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
2397 removed from the stack and replaced by the results from the call.)
2398 It also has the same upvalues.
2399 Whatever it returns is handled by Lua as if it were the return
2400 of the original function.
2401
2402
2403
2404
2405
2406 <h2>4.8 &ndash; <a name="4.8">Functions and Types</a></h2>
2407
2408 <p>
2409 Here we list all functions and types from the C&nbsp;API in
2410 alphabetical order.
2411 Each function has an indicator like this:
2412 <span class="apii">[-o, +p, <em>x</em>]</span>
2413
2414
2415 <p>
2416 The first field, <code>o</code>,
2417 is how many elements the function pops from the stack.
2418 The second field, <code>p</code>,
2419 is how many elements the function pushes onto the stack.
2420 (Any function always pushes its results after popping its arguments.)
2421 A field in the form <code>x|y</code> means the function can push (or pop)
2422 <code>x</code> or <code>y</code> elements,
2423 depending on the situation;
2424 an interrogation mark '<code>?</code>' means that
2425 we cannot know how many elements the function pops/pushes
2426 by looking only at its arguments
2427 (e.g., they may depend on what is on the stack).
2428 The third field, <code>x</code>,
2429 tells whether the function may raise errors:
2430 '<code>-</code>' means the function never raises any error;
2431 '<code>e</code>' means the function may raise errors;
2432 '<code>v</code>' means the function may raise an error on purpose.
2433
2434
2435
2436 <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
2437 <span class="apii">[-0, +0, &ndash;]</span>
2438 <pre>int lua_absindex (lua_State *L, int idx);</pre>
2439
2440 <p>
2441 Converts the acceptable index <code>idx</code> into an absolute index
2442 (that is, one that does not depend on the stack top).
2443
2444
2445
2446
2447
2448 <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
2449 <pre>typedef void * (*lua_Alloc) (void *ud,
2450 void *ptr,
2451 size_t osize,
2452 size_t nsize);</pre>
2453
2454 <p>
2455 The type of the memory-allocation function used by Lua states.
2456 The allocator function must provide a
2457 functionality similar to <code>realloc</code>,
2458 but not exactly the same.
2459 Its arguments are
2460 <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
2461 <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
2462 <code>osize</code>, the original size of the block or some code about what
2463 is being allocated;
2464 and <code>nsize</code>, the new size of the block.
2465
2466
2467 <p>
2468 When <code>ptr</code> is not <code>NULL</code>,
2469 <code>osize</code> is the size of the block pointed by <code>ptr</code>,
2470 that is, the size given when it was allocated or reallocated.
2471
2472
2473 <p>
2474 When <code>ptr</code> is <code>NULL</code>,
2475 <code>osize</code> encodes the kind of object that Lua is allocating.
2476 <code>osize</code> is any of
2477 <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>,
2478 <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)
2479 Lua is creating a new object of that type.
2480 When <code>osize</code> is some other value,
2481 Lua is allocating memory for something else.
2482
2483
2484 <p>
2485 Lua assumes the following behavior from the allocator function:
2486
2487
2488 <p>
2489 When <code>nsize</code> is zero,
2490 the allocator must behave like <code>free</code>
2491 and return <code>NULL</code>.
2492
2493
2494 <p>
2495 When <code>nsize</code> is not zero,
2496 the allocator must behave like <code>realloc</code>.
2497 The allocator returns <code>NULL</code>
2498 if and only if it cannot fulfill the request.
2499 Lua assumes that the allocator never fails when
2500 <code>osize &gt;= nsize</code>.
2501
2502
2503 <p>
2504 Here is a simple implementation for the allocator function.
2505 It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
2506
2507 <pre>
2508 static void *l_alloc (void *ud, void *ptr, size_t osize,
2509 size_t nsize) {
2510 (void)ud; (void)osize; /* not used */
2511 if (nsize == 0) {
2512 free(ptr);
2513 return NULL;
2514 }
2515 else
2516 return realloc(ptr, nsize);
2517 }
2518 </pre><p>
2519 Note that Standard&nbsp;C ensures
2520 that <code>free(NULL)</code> has no effect and that
2521 <code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>.
2522 This code assumes that <code>realloc</code> does not fail when shrinking a block.
2523 (Although Standard&nbsp;C does not ensure this behavior,
2524 it seems to be a safe assumption.)
2525
2526
2527
2528
2529
2530 <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
2531 <span class="apii">[-(2|1), +1, <em>e</em>]</span>
2532 <pre>void lua_arith (lua_State *L, int op);</pre>
2533
2534 <p>
2535 Performs an arithmetic or bitwise operation over the two values
2536 (or one, in the case of negations)
2537 at the top of the stack,
2538 with the value at the top being the second operand,
2539 pops these values, and pushes the result of the operation.
2540 The function follows the semantics of the corresponding Lua operator
2541 (that is, it may call metamethods).
2542
2543
2544 <p>
2545 The value of <code>op</code> must be one of the following constants:
2546
2547 <ul>
2548
2549 <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
2550 <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
2551 <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
2552 <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li>
2553 <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li>
2554 <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
2555 <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
2556 <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li>
2557 <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise negation (<code>~</code>)</li>
2558 <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise and (<code>&amp;</code>)</li>
2559 <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise or (<code>|</code>)</li>
2560 <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive or (<code>~</code>)</li>
2561 <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code>&lt;&lt;</code>)</li>
2562 <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>&gt;&gt;</code>)</li>
2563
2564 </ul>
2565
2566
2567
2568
2569 <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
2570 <span class="apii">[-0, +0, &ndash;]</span>
2571 <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
2572
2573 <p>
2574 Sets a new panic function and returns the old one (see <a href="#4.6">&sect;4.6</a>).
2575
2576
2577
2578
2579
2580 <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
2581 <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
2582 <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
2583
2584 <p>
2585 Calls a function.
2586
2587
2588 <p>
2589 To call a function you must use the following protocol:
2590 first, the function to be called is pushed onto the stack;
2591 then, the arguments to the function are pushed
2592 in direct order;
2593 that is, the first argument is pushed first.
2594 Finally you call <a href="#lua_call"><code>lua_call</code></a>;
2595 <code>nargs</code> is the number of arguments that you pushed onto the stack.
2596 All arguments and the function value are popped from the stack
2597 when the function is called.
2598 The function results are pushed onto the stack when the function returns.
2599 The number of results is adjusted to <code>nresults</code>,
2600 unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
2601 In this case, all results from the function are pushed.
2602 Lua takes care that the returned values fit into the stack space.
2603 The function results are pushed onto the stack in direct order
2604 (the first result is pushed first),
2605 so that after the call the last result is on the top of the stack.
2606
2607
2608 <p>
2609 Any error inside the called function is propagated upwards
2610 (with a <code>longjmp</code>).
2611
2612
2613 <p>
2614 The following example shows how the host program can do the
2615 equivalent to this Lua code:
2616
2617 <pre>
2618 a = f("how", t.x, 14)
2619 </pre><p>
2620 Here it is in&nbsp;C:
2621
2622 <pre>
2623 lua_getglobal(L, "f"); /* function to be called */
2624 lua_pushliteral(L, "how"); /* 1st argument */
2625 lua_getglobal(L, "t"); /* table to be indexed */
2626 lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
2627 lua_remove(L, -2); /* remove 't' from the stack */
2628 lua_pushinteger(L, 14); /* 3rd argument */
2629 lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
2630 lua_setglobal(L, "a"); /* set global 'a' */
2631 </pre><p>
2632 Note that the code above is <em>balanced</em>:
2633 at its end, the stack is back to its original configuration.
2634 This is considered good programming practice.
2635
2636
2637
2638
2639
2640 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
2641 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
2642 <pre>void lua_callk (lua_State *L,
2643 int nargs,
2644 int nresults,
2645 lua_KContext ctx,
2646 lua_KFunction k);</pre>
2647
2648 <p>
2649 This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
2650 but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
2651
2652
2653
2654
2655
2656 <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
2657 <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
2658
2659 <p>
2660 Type for C&nbsp;functions.
2661
2662
2663 <p>
2664 In order to communicate properly with Lua,
2665 a C&nbsp;function must use the following protocol,
2666 which defines the way parameters and results are passed:
2667 a C&nbsp;function receives its arguments from Lua in its stack
2668 in direct order (the first argument is pushed first).
2669 So, when the function starts,
2670 <code>lua_gettop(L)</code> returns the number of arguments received by the function.
2671 The first argument (if any) is at index 1
2672 and its last argument is at index <code>lua_gettop(L)</code>.
2673 To return values to Lua, a C&nbsp;function just pushes them onto the stack,
2674 in direct order (the first result is pushed first),
2675 and returns the number of results.
2676 Any other value in the stack below the results will be properly
2677 discarded by Lua.
2678 Like a Lua function, a C&nbsp;function called by Lua can also return
2679 many results.
2680
2681
2682 <p>
2683 As an example, the following function receives a variable number
2684 of numerical arguments and returns their average and their sum:
2685
2686 <pre>
2687 static int foo (lua_State *L) {
2688 int n = lua_gettop(L); /* number of arguments */
2689 lua_Number sum = 0.0;
2690 int i;
2691 for (i = 1; i &lt;= n; i++) {
2692 if (!lua_isnumber(L, i)) {
2693 lua_pushliteral(L, "incorrect argument");
2694 lua_error(L);
2695 }
2696 sum += lua_tonumber(L, i);
2697 }
2698 lua_pushnumber(L, sum/n); /* first result */
2699 lua_pushnumber(L, sum); /* second result */
2700 return 2; /* number of results */
2701 }
2702 </pre>
2703
2704
2705
2706
2707 <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
2708 <span class="apii">[-0, +0, &ndash;]</span>
2709 <pre>int lua_checkstack (lua_State *L, int n);</pre>
2710
2711 <p>
2712 Ensures that the stack has space for at least <code>n</code> extra slots.
2713 It returns false if it cannot fulfill the request,
2714 either because it would cause the stack
2715 to be larger than a fixed maximum size
2716 (typically at least several thousand elements) or
2717 because it cannot allocate memory for the extra space.
2718 This function never shrinks the stack;
2719 if the stack is already larger than the new size,
2720 it is left unchanged.
2721
2722
2723
2724
2725
2726 <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
2727 <span class="apii">[-0, +0, &ndash;]</span>
2728 <pre>void lua_close (lua_State *L);</pre>
2729
2730 <p>
2731 Destroys all objects in the given Lua state
2732 (calling the corresponding garbage-collection metamethods, if any)
2733 and frees all dynamic memory used by this state.
2734 On several platforms, you may not need to call this function,
2735 because all resources are naturally released when the host program ends.
2736 On the other hand, long-running programs that create multiple states,
2737 such as daemons or web servers,
2738 will probably need to close states as soon as they are not needed.
2739
2740
2741
2742
2743
2744 <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
2745 <span class="apii">[-0, +0, <em>e</em>]</span>
2746 <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre>
2747
2748 <p>
2749 Compares two Lua values.
2750 Returns 1 if the value at index <code>index1</code> satisfies <code>op</code>
2751 when compared with the value at index <code>index2</code>,
2752 following the semantics of the corresponding Lua operator
2753 (that is, it may call metamethods).
2754 Otherwise returns&nbsp;0.
2755 Also returns&nbsp;0 if any of the indices is not valid.
2756
2757
2758 <p>
2759 The value of <code>op</code> must be one of the following constants:
2760
2761 <ul>
2762
2763 <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li>
2764 <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code>&lt;</code>)</li>
2765 <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code>&lt;=</code>)</li>
2766
2767 </ul>
2768
2769
2770
2771
2772 <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
2773 <span class="apii">[-n, +1, <em>e</em>]</span>
2774 <pre>void lua_concat (lua_State *L, int n);</pre>
2775
2776 <p>
2777 Concatenates the <code>n</code> values at the top of the stack,
2778 pops them, and leaves the result at the top.
2779 If <code>n</code>&nbsp;is&nbsp;1, the result is the single value on the stack
2780 (that is, the function does nothing);
2781 if <code>n</code> is 0, the result is the empty string.
2782 Concatenation is performed following the usual semantics of Lua
2783 (see <a href="#3.4.6">&sect;3.4.6</a>).
2784
2785
2786
2787
2788
2789 <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
2790 <span class="apii">[-0, +0, &ndash;]</span>
2791 <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
2792
2793 <p>
2794 Copies the element at index <code>fromidx</code>
2795 into the valid index <code>toidx</code>,
2796 replacing the value at that position.
2797 Values at other positions are not affected.
2798
2799
2800
2801
2802
2803 <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
2804 <span class="apii">[-0, +1, <em>e</em>]</span>
2805 <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
2806
2807 <p>
2808 Creates a new empty table and pushes it onto the stack.
2809 Parameter <code>narr</code> is a hint for how many elements the table
2810 will have as a sequence;
2811 parameter <code>nrec</code> is a hint for how many other elements
2812 the table will have.
2813 Lua may use these hints to preallocate memory for the new table.
2814 This pre-allocation is useful for performance when you know in advance
2815 how many elements the table will have.
2816 Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
2817
2818
2819
2820
2821
2822 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
2823 <span class="apii">[-0, +0, <em>e</em>]</span>
2824 <pre>int lua_dump (lua_State *L,
2825 lua_Writer writer,
2826 void *data,
2827 int strip);</pre>
2828
2829 <p>
2830 Dumps a function as a binary chunk.
2831 Receives a Lua function on the top of the stack
2832 and produces a binary chunk that,
2833 if loaded again,
2834 results in a function equivalent to the one dumped.
2835 As it produces parts of the chunk,
2836 <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
2837 with the given <code>data</code>
2838 to write them.
2839
2840
2841 <p>
2842 If <code>strip</code> is true,
2843 the binary representation is created without debug information
2844 about the function.
2845
2846
2847 <p>
2848 The value returned is the error code returned by the last
2849 call to the writer;
2850 0&nbsp;means no errors.
2851
2852
2853 <p>
2854 This function does not pop the Lua function from the stack.
2855
2856
2857
2858
2859
2860 <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
2861 <span class="apii">[-1, +0, <em>v</em>]</span>
2862 <pre>int lua_error (lua_State *L);</pre>
2863
2864 <p>
2865 Generates a Lua error,
2866 using the value at the top of the stack as the error object.
2867 This function does a long jump,
2868 and therefore never returns
2869 (see <a href="#luaL_error"><code>luaL_error</code></a>).
2870
2871
2872
2873
2874
2875 <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
2876 <span class="apii">[-0, +0, <em>e</em>]</span>
2877 <pre>int lua_gc (lua_State *L, int what, int data);</pre>
2878
2879 <p>
2880 Controls the garbage collector.
2881
2882
2883 <p>
2884 This function performs several tasks,
2885 according to the value of the parameter <code>what</code>:
2886
2887 <ul>
2888
2889 <li><b><code>LUA_GCSTOP</code>: </b>
2890 stops the garbage collector.
2891 </li>
2892
2893 <li><b><code>LUA_GCRESTART</code>: </b>
2894 restarts the garbage collector.
2895 </li>
2896
2897 <li><b><code>LUA_GCCOLLECT</code>: </b>
2898 performs a full garbage-collection cycle.
2899 </li>
2900
2901 <li><b><code>LUA_GCCOUNT</code>: </b>
2902 returns the current amount of memory (in Kbytes) in use by Lua.
2903 </li>
2904
2905 <li><b><code>LUA_GCCOUNTB</code>: </b>
2906 returns the remainder of dividing the current amount of bytes of
2907 memory in use by Lua by 1024.
2908 </li>
2909
2910 <li><b><code>LUA_GCSTEP</code>: </b>
2911 performs an incremental step of garbage collection.
2912 </li>
2913
2914 <li><b><code>LUA_GCSETPAUSE</code>: </b>
2915 sets <code>data</code> as the new value
2916 for the <em>pause</em> of the collector (see <a href="#2.5">&sect;2.5</a>)
2917 and returns the previous value of the pause.
2918 </li>
2919
2920 <li><b><code>LUA_GCSETSTEPMUL</code>: </b>
2921 sets <code>data</code> as the new value for the <em>step multiplier</em> of
2922 the collector (see <a href="#2.5">&sect;2.5</a>)
2923 and returns the previous value of the step multiplier.
2924 </li>
2925
2926 <li><b><code>LUA_GCISRUNNING</code>: </b>
2927 returns a boolean that tells whether the collector is running
2928 (i.e., not stopped).
2929 </li>
2930
2931 </ul>
2932
2933 <p>
2934 For more details about these options,
2935 see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
2936
2937
2938
2939
2940
2941 <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
2942 <span class="apii">[-0, +0, &ndash;]</span>
2943 <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
2944
2945 <p>
2946 Returns the memory-allocation function of a given state.
2947 If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
2948 opaque pointer given when the memory-allocator function was set.
2949
2950
2951
2952
2953
2954 <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
2955 <span class="apii">[-0, +1, <em>e</em>]</span>
2956 <pre>int lua_getfield (lua_State *L, int index, const char *k);</pre>
2957
2958 <p>
2959 Pushes onto the stack the value <code>t[k]</code>,
2960 where <code>t</code> is the value at the given index.
2961 As in Lua, this function may trigger a metamethod
2962 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
2963
2964
2965 <p>
2966 Returns the type of the pushed value.
2967
2968
2969
2970
2971
2972 <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
2973 <span class="apii">[-0, +0, &ndash;]</span>
2974 <pre>void *lua_getextraspace (lua_State *L);</pre>
2975
2976 <p>
2977 Returns a pointer to a raw memory area associated with the
2978 given Lua state.
2979 The application can use this area for any purpose;
2980 Lua does not use it for anything.
2981
2982
2983 <p>
2984 Each new thread has this area initialized with a copy
2985 of the area of the main thread.
2986
2987
2988 <p>
2989 By default, this area has the size of a pointer to void,
2990 but you can recompile Lua with a different size for this area.
2991 (See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.)
2992
2993
2994
2995
2996
2997 <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
2998 <span class="apii">[-0, +1, <em>e</em>]</span>
2999 <pre>int lua_getglobal (lua_State *L, const char *name);</pre>
3000
3001 <p>
3002 Pushes onto the stack the value of the global <code>name</code>.
3003 Returns the type of that value.
3004
3005
3006
3007
3008
3009 <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p>
3010 <span class="apii">[-0, +1, <em>e</em>]</span>
3011 <pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre>
3012
3013 <p>
3014 Pushes onto the stack the value <code>t[i]</code>,
3015 where <code>t</code> is the value at the given index.
3016 As in Lua, this function may trigger a metamethod
3017 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
3018
3019
3020 <p>
3021 Returns the type of the pushed value.
3022
3023
3024
3025
3026
3027 <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
3028 <span class="apii">[-0, +(0|1), &ndash;]</span>
3029 <pre>int lua_getmetatable (lua_State *L, int index);</pre>
3030
3031 <p>
3032 If the value at the given index has a metatable,
3033 the function pushes that metatable onto the stack and returns&nbsp;1.
3034 Otherwise,
3035 the function returns&nbsp;0 and pushes nothing on the stack.
3036
3037
3038
3039
3040
3041 <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
3042 <span class="apii">[-1, +1, <em>e</em>]</span>
3043 <pre>int lua_gettable (lua_State *L, int index);</pre>
3044
3045 <p>
3046 Pushes onto the stack the value <code>t[k]</code>,
3047 where <code>t</code> is the value at the given index
3048 and <code>k</code> is the value at the top of the stack.
3049
3050
3051 <p>
3052 This function pops the key from the stack,
3053 pushing the resulting value in its place.
3054 As in Lua, this function may trigger a metamethod
3055 for the "index" event (see <a href="#2.4">&sect;2.4</a>).
3056
3057
3058 <p>
3059 Returns the type of the pushed value.
3060
3061
3062
3063
3064
3065 <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
3066 <span class="apii">[-0, +0, &ndash;]</span>
3067 <pre>int lua_gettop (lua_State *L);</pre>
3068
3069 <p>
3070 Returns the index of the top element in the stack.
3071 Because indices start at&nbsp;1,
3072 this result is equal to the number of elements in the stack;
3073 in particular, 0&nbsp;means an empty stack.
3074
3075
3076
3077
3078
3079 <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
3080 <span class="apii">[-0, +1, &ndash;]</span>
3081 <pre>int lua_getuservalue (lua_State *L, int index);</pre>
3082
3083 <p>
3084 Pushes onto the stack the Lua value associated with the userdata
3085 at the given index.
3086
3087
3088 <p>
3089 Returns the type of the pushed value.
3090
3091
3092
3093
3094
3095 <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
3096 <span class="apii">[-1, +1, &ndash;]</span>
3097 <pre>void lua_insert (lua_State *L, int index);</pre>
3098
3099 <p>
3100 Moves the top element into the given valid index,
3101 shifting up the elements above this index to open space.
3102 This function cannot be called with a pseudo-index,
3103 because a pseudo-index is not an actual stack position.
3104
3105
3106
3107
3108
3109 <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
3110 <pre>typedef ... lua_Integer;</pre>
3111
3112 <p>
3113 The type of integers in Lua.
3114
3115
3116 <p>
3117 By default this type is <code>long long</code>,
3118 (usually a 64-bit two-complement integer),
3119 but that can be changed to <code>long</code> or <code>int</code>
3120 (usually a 32-bit two-complement integer).
3121 (See <code>LUA_INT</code> in <code>luaconf.h</code>.)
3122
3123
3124 <p>
3125 Lua also defines the constants
3126 <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>,
3127 with the minimum and the maximum values that fit in this type.
3128
3129
3130
3131
3132
3133 <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
3134 <span class="apii">[-0, +0, &ndash;]</span>
3135 <pre>int lua_isboolean (lua_State *L, int index);</pre>
3136
3137 <p>
3138 Returns 1 if the value at the given index is a boolean,
3139 and 0&nbsp;otherwise.
3140
3141
3142
3143
3144
3145 <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
3146 <span class="apii">[-0, +0, &ndash;]</span>
3147 <pre>int lua_iscfunction (lua_State *L, int index);</pre>
3148
3149 <p>
3150 Returns 1 if the value at the given index is a C&nbsp;function,
3151 and 0&nbsp;otherwise.
3152
3153
3154
3155
3156
3157 <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
3158 <span class="apii">[-0, +0, &ndash;]</span>
3159 <pre>int lua_isfunction (lua_State *L, int index);</pre>
3160
3161 <p>
3162 Returns 1 if the value at the given index is a function
3163 (either C or Lua), and 0&nbsp;otherwise.
3164
3165
3166
3167
3168
3169 <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p>
3170 <span class="apii">[-0, +0, &ndash;]</span>
3171 <pre>int lua_isinteger (lua_State *L, int index);</pre>
3172
3173 <p>
3174 Returns 1 if the value at the given index is an integer
3175 (that is, the value is a number and is represented as an integer),
3176 and 0&nbsp;otherwise.
3177
3178
3179
3180
3181
3182 <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
3183 <span class="apii">[-0, +0, &ndash;]</span>
3184 <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
3185
3186 <p>
3187 Returns 1 if the value at the given index is a light userdata,
3188 and 0&nbsp;otherwise.
3189
3190
3191
3192
3193
3194 <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
3195 <span class="apii">[-0, +0, &ndash;]</span>
3196 <pre>int lua_isnil (lua_State *L, int index);</pre>
3197
3198 <p>
3199 Returns 1 if the value at the given index is <b>nil</b>,
3200 and 0&nbsp;otherwise.
3201
3202
3203
3204
3205
3206 <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
3207 <span class="apii">[-0, +0, &ndash;]</span>
3208 <pre>int lua_isnone (lua_State *L, int index);</pre>
3209
3210 <p>
3211 Returns 1 if the given index is not valid,
3212 and 0&nbsp;otherwise.
3213
3214
3215
3216
3217
3218 <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
3219 <span class="apii">[-0, +0, &ndash;]</span>
3220 <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
3221
3222 <p>
3223 Returns 1 if the given index is not valid
3224 or if the value at this index is <b>nil</b>,
3225 and 0&nbsp;otherwise.
3226
3227
3228
3229
3230
3231 <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
3232 <span class="apii">[-0, +0, &ndash;]</span>
3233 <pre>int lua_isnumber (lua_State *L, int index);</pre>
3234
3235 <p>
3236 Returns 1 if the value at the given index is a number
3237 or a string convertible to a number,
3238 and 0&nbsp;otherwise.
3239
3240
3241
3242
3243
3244 <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
3245 <span class="apii">[-0, +0, &ndash;]</span>
3246 <pre>int lua_isstring (lua_State *L, int index);</pre>
3247
3248 <p>
3249 Returns 1 if the value at the given index is a string
3250 or a number (which is always convertible to a string),
3251 and 0&nbsp;otherwise.
3252
3253
3254
3255
3256
3257 <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
3258 <span class="apii">[-0, +0, &ndash;]</span>
3259 <pre>int lua_istable (lua_State *L, int index);</pre>
3260
3261 <p>
3262 Returns 1 if the value at the given index is a table,
3263 and 0&nbsp;otherwise.
3264
3265
3266
3267
3268
3269 <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
3270 <span class="apii">[-0, +0, &ndash;]</span>
3271 <pre>int lua_isthread (lua_State *L, int index);</pre>
3272
3273 <p>
3274 Returns 1 if the value at the given index is a thread,
3275 and 0&nbsp;otherwise.
3276
3277
3278
3279
3280
3281 <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
3282 <span class="apii">[-0, +0, &ndash;]</span>
3283 <pre>int lua_isuserdata (lua_State *L, int index);</pre>
3284
3285 <p>
3286 Returns 1 if the value at the given index is a userdata
3287 (either full or light), and 0&nbsp;otherwise.
3288
3289
3290
3291
3292
3293 <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
3294 <span class="apii">[-0, +0, &ndash;]</span>
3295 <pre>int lua_isyieldable (lua_State *L);</pre>
3296
3297 <p>
3298 Returns 1 if the given coroutine can yield,
3299 and 0&nbsp;otherwise.
3300
3301
3302
3303
3304
3305 <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3>
3306 <pre>typedef ... lua_KContext;</pre>
3307
3308 <p>
3309 The type for continuation-function contexts.
3310 It must be a numerical type.
3311 This type is defined as <code>intptr_t</code>
3312 when <code>intptr_t</code> is available,
3313 so that it can store pointers too.
3314 Otherwise, it is defined as <code>ptrdiff_t</code>.
3315
3316
3317
3318
3319
3320 <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
3321 <pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre>
3322
3323 <p>
3324 Type for continuation functions (see <a href="#4.7">&sect;4.7</a>).
3325
3326
3327
3328
3329
3330 <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
3331 <span class="apii">[-0, +1, <em>e</em>]</span>
3332 <pre>void lua_len (lua_State *L, int index);</pre>
3333
3334 <p>
3335 Returns the length of the value at the given index.
3336 It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>) and
3337 may trigger a metamethod for the "length" event (see <a href="#2.4">&sect;2.4</a>).
3338 The result is pushed on the stack.
3339
3340
3341
3342
3343
3344 <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
3345 <span class="apii">[-0, +1, &ndash;]</span>
3346 <pre>int lua_load (lua_State *L,
3347 lua_Reader reader,
3348 void *data,
3349 const char *chunkname,
3350 const char *mode);</pre>
3351
3352 <p>
3353 Loads a Lua chunk without running it.
3354 If there are no errors,
3355 <code>lua_load</code> pushes the compiled chunk as a Lua
3356 function on top of the stack.
3357 Otherwise, it pushes an error message.
3358
3359
3360 <p>
3361 The return values of <code>lua_load</code> are:
3362
3363 <ul>
3364
3365 <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
3366
3367 <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
3368 syntax error during precompilation;</li>
3369
3370 <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3371 memory allocation error;</li>
3372
3373 <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3374 error while running a <code>__gc</code> metamethod.
3375 (This error has no relation with the chunk being loaded.
3376 It is generated by the garbage collector.)
3377 </li>
3378
3379 </ul>
3380
3381 <p>
3382 The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
3383 to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
3384 The <code>data</code> argument is an opaque value passed to the reader function.
3385
3386
3387 <p>
3388 The <code>chunkname</code> argument gives a name to the chunk,
3389 which is used for error messages and in debug information (see <a href="#4.9">&sect;4.9</a>).
3390
3391
3392 <p>
3393 <code>lua_load</code> automatically detects whether the chunk is text or binary
3394 and loads it accordingly (see program <code>luac</code>).
3395 The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
3396 with the addition that
3397 a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
3398
3399
3400 <p>
3401 <code>lua_load</code> uses the stack internally,
3402 so the reader function must always leave the stack
3403 unmodified when returning.
3404
3405
3406 <p>
3407 If the resulting function has upvalues,
3408 its first upvalue is set to the value of the global environment
3409 stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">&sect;4.5</a>).
3410 When loading main chunks,
3411 this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
3412 Other upvalues are initialized with <b>nil</b>.
3413
3414
3415
3416
3417
3418 <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
3419 <span class="apii">[-0, +0, &ndash;]</span>
3420 <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
3421
3422 <p>
3423 Creates a new thread running in a new, independent state.
3424 Returns <code>NULL</code> if it cannot create the thread or the state
3425 (due to lack of memory).
3426 The argument <code>f</code> is the allocator function;
3427 Lua does all memory allocation for this state through this function.
3428 The second argument, <code>ud</code>, is an opaque pointer that Lua
3429 passes to the allocator in every call.
3430
3431
3432
3433
3434
3435 <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
3436 <span class="apii">[-0, +1, <em>e</em>]</span>
3437 <pre>void lua_newtable (lua_State *L);</pre>
3438
3439 <p>
3440 Creates a new empty table and pushes it onto the stack.
3441 It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
3442
3443
3444
3445
3446
3447 <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
3448 <span class="apii">[-0, +1, <em>e</em>]</span>
3449 <pre>lua_State *lua_newthread (lua_State *L);</pre>
3450
3451 <p>
3452 Creates a new thread, pushes it on the stack,
3453 and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
3454 The new thread returned by this function shares with the original thread
3455 its global environment,
3456 but has an independent execution stack.
3457
3458
3459 <p>
3460 There is no explicit function to close or to destroy a thread.
3461 Threads are subject to garbage collection,
3462 like any Lua object.
3463
3464
3465
3466
3467
3468 <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
3469 <span class="apii">[-0, +1, <em>e</em>]</span>
3470 <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>
3471
3472 <p>
3473 This function allocates a new block of memory with the given size,
3474 pushes onto the stack a new full userdata with the block address,
3475 and returns this address.
3476 The host program can freely use this memory.
3477
3478
3479
3480
3481
3482 <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
3483 <span class="apii">[-1, +(2|0), <em>e</em>]</span>
3484 <pre>int lua_next (lua_State *L, int index);</pre>
3485
3486 <p>
3487 Pops a key from the stack,
3488 and pushes a key&ndash;value pair from the table at the given index
3489 (the "next" pair after the given key).
3490 If there are no more elements in the table,
3491 then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
3492
3493
3494 <p>
3495 A typical traversal looks like this:
3496
3497 <pre>
3498 /* table is in the stack at index 't' */
3499 lua_pushnil(L); /* first key */
3500 while (lua_next(L, t) != 0) {
3501 /* uses 'key' (at index -2) and 'value' (at index -1) */
3502 printf("%s - %s\n",
3503 lua_typename(L, lua_type(L, -2)),
3504 lua_typename(L, lua_type(L, -1)));
3505 /* removes 'value'; keeps 'key' for next iteration */
3506 lua_pop(L, 1);
3507 }
3508 </pre>
3509
3510 <p>
3511 While traversing a table,
3512 do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
3513 unless you know that the key is actually a string.
3514 Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
3515 the value at the given index;
3516 this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
3517
3518
3519 <p>
3520 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
3521 the table during its traversal.
3522
3523
3524
3525
3526
3527 <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
3528 <pre>typedef double lua_Number;</pre>
3529
3530 <p>
3531 The type of floats in Lua.
3532
3533
3534 <p>
3535 By default this type is double,
3536 but that can be changed to a single float.
3537 (See <code>LUA_REAL</code> in <code>luaconf.h</code>.)
3538
3539
3540
3541
3542
3543 <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3>
3544 <pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre>
3545
3546 <p>
3547 Converts a Lua float to a Lua integer.
3548 This macro assumes that <code>n</code> has an integral value.
3549 If that value is within the range of Lua integers,
3550 it is converted to an integer and assigned to <code>*p</code>.
3551 The macro results in a boolean indicating whether the
3552 conversion was successful.
3553 (Note that this range test can be tricky to do
3554 correctly without this macro,
3555 due to roundings.)
3556
3557
3558 <p>
3559 This macro may evaluate its arguments more than once.
3560
3561
3562
3563
3564
3565 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
3566 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
3567 <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
3568
3569 <p>
3570 Calls a function in protected mode.
3571
3572
3573 <p>
3574 Both <code>nargs</code> and <code>nresults</code> have the same meaning as
3575 in <a href="#lua_call"><code>lua_call</code></a>.
3576 If there are no errors during the call,
3577 <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
3578 However, if there is any error,
3579 <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
3580 pushes a single value on the stack (the error message),
3581 and returns an error code.
3582 Like <a href="#lua_call"><code>lua_call</code></a>,
3583 <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
3584 and its arguments from the stack.
3585
3586
3587 <p>
3588 If <code>msgh</code> is 0,
3589 then the error message returned on the stack
3590 is exactly the original error message.
3591 Otherwise, <code>msgh</code> is the stack index of a
3592 <em>message handler</em>.
3593 (In the current implementation, this index cannot be a pseudo-index.)
3594 In case of runtime errors,
3595 this function will be called with the error message
3596 and its return value will be the message
3597 returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
3598
3599
3600 <p>
3601 Typically, the message handler is used to add more debug
3602 information to the error message, such as a stack traceback.
3603 Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
3604 since by then the stack has unwound.
3605
3606
3607 <p>
3608 The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
3609 (defined in <code>lua.h</code>):
3610
3611 <ul>
3612
3613 <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b>
3614 success.</li>
3615
3616 <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b>
3617 a runtime error.
3618 </li>
3619
3620 <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3621 memory allocation error.
3622 For such errors, Lua does not call the message handler.
3623 </li>
3624
3625 <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b>
3626 error while running the message handler.
3627 </li>
3628
3629 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3630 error while running a <code>__gc</code> metamethod.
3631 (This error typically has no relation with the function being called.)
3632 </li>
3633
3634 </ul>
3635
3636
3637
3638
3639 <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
3640 <span class="apii">[-(nargs + 1), +(nresults|1), &ndash;]</span>
3641 <pre>int lua_pcallk (lua_State *L,
3642 int nargs,
3643 int nresults,
3644 int msgh,
3645 lua_KContext ctx,
3646 lua_KFunction k);</pre>
3647
3648 <p>
3649 This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
3650 but allows the called function to yield (see <a href="#4.7">&sect;4.7</a>).
3651
3652
3653
3654
3655
3656 <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
3657 <span class="apii">[-n, +0, &ndash;]</span>
3658 <pre>void lua_pop (lua_State *L, int n);</pre>
3659
3660 <p>
3661 Pops <code>n</code> elements from the stack.
3662
3663
3664
3665
3666
3667 <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
3668 <span class="apii">[-0, +1, &ndash;]</span>
3669 <pre>void lua_pushboolean (lua_State *L, int b);</pre>
3670
3671 <p>
3672 Pushes a boolean value with value <code>b</code> onto the stack.
3673
3674
3675
3676
3677
3678 <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
3679 <span class="apii">[-n, +1, <em>e</em>]</span>
3680 <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
3681
3682 <p>
3683 Pushes a new C&nbsp;closure onto the stack.
3684
3685
3686 <p>
3687 When a C&nbsp;function is created,
3688 it is possible to associate some values with it,
3689 thus creating a C&nbsp;closure (see <a href="#4.4">&sect;4.4</a>);
3690 these values are then accessible to the function whenever it is called.
3691 To associate values with a C&nbsp;function,
3692 first these values must be pushed onto the stack
3693 (when there are multiple values, the first value is pushed first).
3694 Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
3695 is called to create and push the C&nbsp;function onto the stack,
3696 with the argument <code>n</code> telling how many values will be
3697 associated with the function.
3698 <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
3699
3700
3701 <p>
3702 The maximum value for <code>n</code> is 255.
3703
3704
3705 <p>
3706 When <code>n</code> is zero,
3707 this function creates a <em>light C function</em>,
3708 which is just a pointer to the C&nbsp;function.
3709 In that case, it never raises a memory error.
3710
3711
3712
3713
3714
3715 <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
3716 <span class="apii">[-0, +1, &ndash;]</span>
3717 <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
3718
3719 <p>
3720 Pushes a C&nbsp;function onto the stack.
3721 This function receives a pointer to a C function
3722 and pushes onto the stack a Lua value of type <code>function</code> that,
3723 when called, invokes the corresponding C&nbsp;function.
3724
3725
3726 <p>
3727 Any function to be registered in Lua must
3728 follow the correct protocol to receive its parameters
3729 and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
3730
3731
3732 <p>
3733 <code>lua_pushcfunction</code> is defined as a macro:
3734
3735 <pre>
3736 #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0)
3737 </pre><p>
3738 Note that <code>f</code> is used twice.
3739
3740
3741
3742
3743
3744 <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
3745 <span class="apii">[-0, +1, <em>e</em>]</span>
3746 <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
3747
3748 <p>
3749 Pushes onto the stack a formatted string
3750 and returns a pointer to this string.
3751 It is similar to the ISO&nbsp;C function <code>sprintf</code>,
3752 but has some important differences:
3753
3754 <ul>
3755
3756 <li>
3757 You do not have to allocate space for the result:
3758 the result is a Lua string and Lua takes care of memory allocation
3759 (and deallocation, through garbage collection).
3760 </li>
3761
3762 <li>
3763 The conversion specifiers are quite restricted.
3764 There are no flags, widths, or precisions.
3765 The conversion specifiers can only be
3766 '<code>%%</code>' (inserts the character '<code>%</code>'),
3767 '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
3768 '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
3769 '<code>%L</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>),
3770 '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
3771 '<code>%d</code>' (inserts an <code>int</code>),
3772 '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and
3773 '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence).
3774 </li>
3775
3776 </ul>
3777
3778
3779
3780
3781 <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
3782 <span class="apii">[-0, +1, &ndash;]</span>
3783 <pre>void lua_pushglobaltable (lua_State *L);</pre>
3784
3785 <p>
3786 Pushes the global environment onto the stack.
3787
3788
3789
3790
3791
3792 <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
3793 <span class="apii">[-0, +1, &ndash;]</span>
3794 <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
3795
3796 <p>
3797 Pushes an integer with value <code>n</code> onto the stack.
3798
3799
3800
3801
3802
3803 <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
3804 <span class="apii">[-0, +1, &ndash;]</span>
3805 <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
3806
3807 <p>
3808 Pushes a light userdata onto the stack.
3809
3810
3811 <p>
3812 Userdata represent C&nbsp;values in Lua.
3813 A <em>light userdata</em> represents a pointer, a <code>void*</code>.
3814 It is a value (like a number):
3815 you do not create it, it has no individual metatable,
3816 and it is not collected (as it was never created).
3817 A light userdata is equal to "any"
3818 light userdata with the same C&nbsp;address.
3819
3820
3821
3822
3823
3824 <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
3825 <span class="apii">[-0, +1, <em>e</em>]</span>
3826 <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre>
3827
3828 <p>
3829 This macro is equivalent to <a href="#lua_pushlstring"><code>lua_pushlstring</code></a>,
3830 but can be used only when <code>s</code> is a literal string.
3831 It automatically provides the string length.
3832
3833
3834
3835
3836
3837 <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
3838 <span class="apii">[-0, +1, <em>e</em>]</span>
3839 <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
3840
3841 <p>
3842 Pushes the string pointed to by <code>s</code> with size <code>len</code>
3843 onto the stack.
3844 Lua makes (or reuses) an internal copy of the given string,
3845 so the memory at <code>s</code> can be freed or reused immediately after
3846 the function returns.
3847 The string can contain any binary data,
3848 including embedded zeros.
3849
3850
3851 <p>
3852 Returns a pointer to the internal copy of the string.
3853
3854
3855
3856
3857
3858 <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
3859 <span class="apii">[-0, +1, &ndash;]</span>
3860 <pre>void lua_pushnil (lua_State *L);</pre>
3861
3862 <p>
3863 Pushes a nil value onto the stack.
3864
3865
3866
3867
3868
3869 <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
3870 <span class="apii">[-0, +1, &ndash;]</span>
3871 <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
3872
3873 <p>
3874 Pushes a float with value <code>n</code> onto the stack.
3875
3876
3877
3878
3879
3880 <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
3881 <span class="apii">[-0, +1, <em>e</em>]</span>
3882 <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre>
3883
3884 <p>
3885 Pushes the zero-terminated string pointed to by <code>s</code>
3886 onto the stack.
3887 Lua makes (or reuses) an internal copy of the given string,
3888 so the memory at <code>s</code> can be freed or reused immediately after
3889 the function returns.
3890
3891
3892 <p>
3893 Returns a pointer to the internal copy of the string.
3894
3895
3896 <p>
3897 If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>.
3898
3899
3900
3901
3902
3903 <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
3904 <span class="apii">[-0, +1, &ndash;]</span>
3905 <pre>int lua_pushthread (lua_State *L);</pre>
3906
3907 <p>
3908 Pushes the thread represented by <code>L</code> onto the stack.
3909 Returns 1 if this thread is the main thread of its state.
3910
3911
3912
3913
3914
3915 <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
3916 <span class="apii">[-0, +1, &ndash;]</span>
3917 <pre>void lua_pushvalue (lua_State *L, int index);</pre>
3918
3919 <p>
3920 Pushes a copy of the element at the given index
3921 onto the stack.
3922
3923
3924
3925
3926
3927 <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
3928 <span class="apii">[-0, +1, <em>e</em>]</span>
3929 <pre>const char *lua_pushvfstring (lua_State *L,
3930 const char *fmt,
3931 va_list argp);</pre>
3932
3933 <p>
3934 Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
3935 instead of a variable number of arguments.
3936
3937
3938
3939
3940
3941 <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
3942 <span class="apii">[-0, +0, &ndash;]</span>
3943 <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
3944
3945 <p>
3946 Returns 1 if the two values in indices <code>index1</code> and
3947 <code>index2</code> are primitively equal
3948 (that is, without calling metamethods).
3949 Otherwise returns&nbsp;0.
3950 Also returns&nbsp;0 if any of the indices are not valid.
3951
3952
3953
3954
3955
3956 <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
3957 <span class="apii">[-1, +1, &ndash;]</span>
3958 <pre>int lua_rawget (lua_State *L, int index);</pre>
3959
3960 <p>
3961 Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
3962 (i.e., without metamethods).
3963
3964
3965
3966
3967
3968 <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
3969 <span class="apii">[-0, +1, &ndash;]</span>
3970 <pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre>
3971
3972 <p>
3973 Pushes onto the stack the value <code>t[n]</code>,
3974 where <code>t</code> is the table at the given index.
3975 The access is raw;
3976 that is, it does not invoke metamethods.
3977
3978
3979 <p>
3980 Returns the type of the pushed value.
3981
3982
3983
3984
3985
3986 <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
3987 <span class="apii">[-0, +1, &ndash;]</span>
3988 <pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre>
3989
3990 <p>
3991 Pushes onto the stack the value <code>t[k]</code>,
3992 where <code>t</code> is the table at the given index and
3993 <code>k</code> is the pointer <code>p</code> represented as a light userdata.
3994 The access is raw;
3995 that is, it does not invoke metamethods.
3996
3997
3998 <p>
3999 Returns the type of the pushed value.
4000
4001
4002
4003
4004
4005 <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
4006 <span class="apii">[-0, +0, &ndash;]</span>
4007 <pre>size_t lua_rawlen (lua_State *L, int index);</pre>
4008
4009 <p>
4010 Returns the raw "length" of the value at the given index:
4011 for strings, this is the string length;
4012 for tables, this is the result of the length operator ('<code>#</code>')
4013 with no metamethods;
4014 for userdata, this is the size of the block of memory allocated
4015 for the userdata;
4016 for other values, it is&nbsp;0.
4017
4018
4019
4020
4021
4022 <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
4023 <span class="apii">[-2, +0, <em>e</em>]</span>
4024 <pre>void lua_rawset (lua_State *L, int index);</pre>
4025
4026 <p>
4027 Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
4028 (i.e., without metamethods).
4029
4030
4031
4032
4033
4034 <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
4035 <span class="apii">[-1, +0, <em>e</em>]</span>
4036 <pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre>
4037
4038 <p>
4039 Does the equivalent of <code>t[i] = v</code>,
4040 where <code>t</code> is the table at the given index
4041 and <code>v</code> is the value at the top of the stack.
4042
4043
4044 <p>
4045 This function pops the value from the stack.
4046 The assignment is raw;
4047 that is, it does not invoke metamethods.
4048
4049
4050
4051
4052
4053 <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
4054 <span class="apii">[-1, +0, <em>e</em>]</span>
4055 <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
4056
4057 <p>
4058 Does the equivalent of <code>t[k] = v</code>,
4059 where <code>t</code> is the table at the given index,
4060 <code>k</code> is the pointer <code>p</code> represented as a light userdata,
4061 and <code>v</code> is the value at the top of the stack.
4062
4063
4064 <p>
4065 This function pops the value from the stack.
4066 The assignment is raw;
4067 that is, it does not invoke metamethods.
4068
4069
4070
4071
4072
4073 <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
4074 <pre>typedef const char * (*lua_Reader) (lua_State *L,
4075 void *data,
4076 size_t *size);</pre>
4077
4078 <p>
4079 The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
4080 Every time it needs another piece of the chunk,
4081 <a href="#lua_load"><code>lua_load</code></a> calls the reader,
4082 passing along its <code>data</code> parameter.
4083 The reader must return a pointer to a block of memory
4084 with a new piece of the chunk
4085 and set <code>size</code> to the block size.
4086 The block must exist until the reader function is called again.
4087 To signal the end of the chunk,
4088 the reader must return <code>NULL</code> or set <code>size</code> to zero.
4089 The reader function may return pieces of any size greater than zero.
4090
4091
4092
4093
4094
4095 <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
4096 <span class="apii">[-0, +0, <em>e</em>]</span>
4097 <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre>
4098
4099 <p>
4100 Sets the C function <code>f</code> as the new value of global <code>name</code>.
4101 It is defined as a macro:
4102
4103 <pre>
4104 #define lua_register(L,n,f) \
4105 (lua_pushcfunction(L, f), lua_setglobal(L, n))
4106 </pre>
4107
4108
4109
4110
4111 <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
4112 <span class="apii">[-1, +0, &ndash;]</span>
4113 <pre>void lua_remove (lua_State *L, int index);</pre>
4114
4115 <p>
4116 Removes the element at the given valid index,
4117 shifting down the elements above this index to fill the gap.
4118 This function cannot be called with a pseudo-index,
4119 because a pseudo-index is not an actual stack position.
4120
4121
4122
4123
4124
4125 <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
4126 <span class="apii">[-1, +0, &ndash;]</span>
4127 <pre>void lua_replace (lua_State *L, int index);</pre>
4128
4129 <p>
4130 Moves the top element into the given valid index
4131 without shifting any element
4132 (therefore replacing the value at the given index),
4133 and then pops the top element.
4134
4135
4136
4137
4138
4139 <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
4140 <span class="apii">[-?, +?, &ndash;]</span>
4141 <pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre>
4142
4143 <p>
4144 Starts and resumes a coroutine in a given thread.
4145
4146
4147 <p>
4148 To start a coroutine,
4149 you push onto the thread stack the main function plus any arguments;
4150 then you call <a href="#lua_resume"><code>lua_resume</code></a>,
4151 with <code>nargs</code> being the number of arguments.
4152 This call returns when the coroutine suspends or finishes its execution.
4153 When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>,
4154 or all values returned by the body function.
4155 <a href="#lua_resume"><code>lua_resume</code></a> returns
4156 <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
4157 <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
4158 without errors,
4159 or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
4160
4161
4162 <p>
4163 In case of errors,
4164 the stack is not unwound,
4165 so you can use the debug API over it.
4166 The error message is on the top of the stack.
4167
4168
4169 <p>
4170 To resume a coroutine,
4171 you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>,
4172 put on its stack only the values to
4173 be passed as results from <code>yield</code>,
4174 and then call <a href="#lua_resume"><code>lua_resume</code></a>.
4175
4176
4177 <p>
4178 The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
4179 If there is no such coroutine,
4180 this parameter can be <code>NULL</code>.
4181
4182
4183
4184
4185
4186 <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
4187 <span class="apii">[-0, +0, &ndash;]</span>
4188 <pre>void lua_rotate (lua_State *L, int idx, int n);</pre>
4189
4190 <p>
4191 Rotates the stack elements from <code>idx</code> to the top <code>n</code> positions
4192 in the direction of the top, for a positive <code>n</code>,
4193 or <code>-n</code> positions in the direction of the bottom,
4194 for a negative <code>n</code>.
4195 The absolute value of <code>n</code> must not be greater than the size
4196 of the slice being rotated.
4197
4198
4199
4200
4201
4202 <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
4203 <span class="apii">[-0, +0, &ndash;]</span>
4204 <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
4205
4206 <p>
4207 Changes the allocator function of a given state to <code>f</code>
4208 with user data <code>ud</code>.
4209
4210
4211
4212
4213
4214 <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
4215 <span class="apii">[-1, +0, <em>e</em>]</span>
4216 <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
4217
4218 <p>
4219 Does the equivalent to <code>t[k] = v</code>,
4220 where <code>t</code> is the value at the given index
4221 and <code>v</code> is the value at the top of the stack.
4222
4223
4224 <p>
4225 This function pops the value from the stack.
4226 As in Lua, this function may trigger a metamethod
4227 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4228
4229
4230
4231
4232
4233 <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
4234 <span class="apii">[-1, +0, <em>e</em>]</span>
4235 <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
4236
4237 <p>
4238 Pops a value from the stack and
4239 sets it as the new value of global <code>name</code>.
4240
4241
4242
4243
4244
4245 <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p>
4246 <span class="apii">[-1, +0, <em>e</em>]</span>
4247 <pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre>
4248
4249 <p>
4250 Does the equivalent to <code>t[n] = v</code>,
4251 where <code>t</code> is the value at the given index
4252 and <code>v</code> is the value at the top of the stack.
4253
4254
4255 <p>
4256 This function pops the value from the stack.
4257 As in Lua, this function may trigger a metamethod
4258 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4259
4260
4261
4262
4263
4264 <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
4265 <span class="apii">[-1, +0, &ndash;]</span>
4266 <pre>void lua_setmetatable (lua_State *L, int index);</pre>
4267
4268 <p>
4269 Pops a table from the stack and
4270 sets it as the new metatable for the value at the given index.
4271
4272
4273
4274
4275
4276 <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
4277 <span class="apii">[-2, +0, <em>e</em>]</span>
4278 <pre>void lua_settable (lua_State *L, int index);</pre>
4279
4280 <p>
4281 Does the equivalent to <code>t[k] = v</code>,
4282 where <code>t</code> is the value at the given index,
4283 <code>v</code> is the value at the top of the stack,
4284 and <code>k</code> is the value just below the top.
4285
4286
4287 <p>
4288 This function pops both the key and the value from the stack.
4289 As in Lua, this function may trigger a metamethod
4290 for the "newindex" event (see <a href="#2.4">&sect;2.4</a>).
4291
4292
4293
4294
4295
4296 <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
4297 <span class="apii">[-?, +?, &ndash;]</span>
4298 <pre>void lua_settop (lua_State *L, int index);</pre>
4299
4300 <p>
4301 Accepts any index, or&nbsp;0,
4302 and sets the stack top to this index.
4303 If the new top is larger than the old one,
4304 then the new elements are filled with <b>nil</b>.
4305 If <code>index</code> is&nbsp;0, then all stack elements are removed.
4306
4307
4308
4309
4310
4311 <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
4312 <span class="apii">[-1, +0, &ndash;]</span>
4313 <pre>void lua_setuservalue (lua_State *L, int index);</pre>
4314
4315 <p>
4316 Pops a value from the stack and sets it as
4317 the new value associated to the userdata at the given index.
4318
4319
4320
4321
4322
4323 <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
4324 <pre>typedef struct lua_State lua_State;</pre>
4325
4326 <p>
4327 An opaque structure that points to a thread and indirectly
4328 (through the thread) to the whole state of a Lua interpreter.
4329 The Lua library is fully reentrant:
4330 it has no global variables.
4331 All information about a state is accessible through this structure.
4332
4333
4334 <p>
4335 A pointer to this structure must be passed as the first argument to
4336 every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
4337 which creates a Lua state from scratch.
4338
4339
4340
4341
4342
4343 <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
4344 <span class="apii">[-0, +0, &ndash;]</span>
4345 <pre>int lua_status (lua_State *L);</pre>
4346
4347 <p>
4348 Returns the status of the thread <code>L</code>.
4349
4350
4351 <p>
4352 The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread,
4353 an error code if the thread finished the execution
4354 of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
4355 or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
4356
4357
4358 <p>
4359 You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
4360 You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
4361 (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
4362 (to resume a coroutine).
4363
4364
4365
4366
4367
4368 <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p>
4369 <span class="apii">[-0, +1, &ndash;]</span>
4370 <pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre>
4371
4372 <p>
4373 Converts the zero-terminated string <code>s</code> to a number,
4374 pushes that number into the stack,
4375 and returns the total size of the string,
4376 that is, its length plus one.
4377 The conversion can result in an integer or a float,
4378 according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
4379 The string may have leading and trailing spaces and a sign.
4380 If the string is not a valid numeral,
4381 returns 0 and pushes nothing.
4382 (Note that the result can be used as a boolean,
4383 true if the conversion succeeds.)
4384
4385
4386
4387
4388
4389 <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
4390 <span class="apii">[-0, +0, &ndash;]</span>
4391 <pre>int lua_toboolean (lua_State *L, int index);</pre>
4392
4393 <p>
4394 Converts the Lua value at the given index to a C&nbsp;boolean
4395 value (0&nbsp;or&nbsp;1).
4396 Like all tests in Lua,
4397 <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
4398 different from <b>false</b> and <b>nil</b>;
4399 otherwise it returns false.
4400 (If you want to accept only actual boolean values,
4401 use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
4402
4403
4404
4405
4406
4407 <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
4408 <span class="apii">[-0, +0, &ndash;]</span>
4409 <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
4410
4411 <p>
4412 Converts a value at the given index to a C&nbsp;function.
4413 That value must be a C&nbsp;function;
4414 otherwise, returns <code>NULL</code>.
4415
4416
4417
4418
4419
4420 <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
4421 <span class="apii">[-0, +0, &ndash;]</span>
4422 <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
4423
4424 <p>
4425 Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4426
4427
4428
4429
4430
4431 <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
4432 <span class="apii">[-0, +0, &ndash;]</span>
4433 <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
4434
4435 <p>
4436 Converts the Lua value at the given index
4437 to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
4438 The Lua value must be an integer,
4439 or a number or string convertible to an integer (see <a href="#3.4.3">&sect;3.4.3</a>);
4440 otherwise, <code>lua_tointegerx</code> returns&nbsp;0.
4441
4442
4443 <p>
4444 If <code>isnum</code> is not <code>NULL</code>,
4445 its referent is assigned a boolean value that
4446 indicates whether the operation succeeded.
4447
4448
4449
4450
4451
4452 <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
4453 <span class="apii">[-0, +0, <em>e</em>]</span>
4454 <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
4455
4456 <p>
4457 Converts the Lua value at the given index to a C&nbsp;string.
4458 If <code>len</code> is not <code>NULL</code>,
4459 it also sets <code>*len</code> with the string length.
4460 The Lua value must be a string or a number;
4461 otherwise, the function returns <code>NULL</code>.
4462 If the value is a number,
4463 then <code>lua_tolstring</code> also
4464 <em>changes the actual value in the stack to a string</em>.
4465 (This change confuses <a href="#lua_next"><code>lua_next</code></a>
4466 when <code>lua_tolstring</code> is applied to keys during a table traversal.)
4467
4468
4469 <p>
4470 <code>lua_tolstring</code> returns a fully aligned pointer
4471 to a string inside the Lua state.
4472 This string always has a zero ('<code>\0</code>')
4473 after its last character (as in&nbsp;C),
4474 but can contain other zeros in its body.
4475
4476
4477 <p>
4478 Because Lua has garbage collection,
4479 there is no guarantee that the pointer returned by <code>lua_tolstring</code>
4480 will be valid after the corresponding Lua value is removed from the stack.
4481
4482
4483
4484
4485
4486 <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
4487 <span class="apii">[-0, +0, &ndash;]</span>
4488 <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
4489
4490 <p>
4491 Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4492
4493
4494
4495
4496
4497 <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
4498 <span class="apii">[-0, +0, &ndash;]</span>
4499 <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
4500
4501 <p>
4502 Converts the Lua value at the given index
4503 to the C&nbsp;type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
4504 The Lua value must be a number or a string convertible to a number
4505 (see <a href="#3.4.3">&sect;3.4.3</a>);
4506 otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns&nbsp;0.
4507
4508
4509 <p>
4510 If <code>isnum</code> is not <code>NULL</code>,
4511 its referent is assigned a boolean value that
4512 indicates whether the operation succeeded.
4513
4514
4515
4516
4517
4518 <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
4519 <span class="apii">[-0, +0, &ndash;]</span>
4520 <pre>const void *lua_topointer (lua_State *L, int index);</pre>
4521
4522 <p>
4523 Converts the value at the given index to a generic
4524 C&nbsp;pointer (<code>void*</code>).
4525 The value can be a userdata, a table, a thread, or a function;
4526 otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
4527 Different objects will give different pointers.
4528 There is no way to convert the pointer back to its original value.
4529
4530
4531 <p>
4532 Typically this function is used only for debug information.
4533
4534
4535
4536
4537
4538 <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
4539 <span class="apii">[-0, +0, <em>e</em>]</span>
4540 <pre>const char *lua_tostring (lua_State *L, int index);</pre>
4541
4542 <p>
4543 Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
4544
4545
4546
4547
4548
4549 <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
4550 <span class="apii">[-0, +0, &ndash;]</span>
4551 <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
4552
4553 <p>
4554 Converts the value at the given index to a Lua thread
4555 (represented as <code>lua_State*</code>).
4556 This value must be a thread;
4557 otherwise, the function returns <code>NULL</code>.
4558
4559
4560
4561
4562
4563 <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
4564 <span class="apii">[-0, +0, &ndash;]</span>
4565 <pre>void *lua_touserdata (lua_State *L, int index);</pre>
4566
4567 <p>
4568 If the value at the given index is a full userdata,
4569 returns its block address.
4570 If the value is a light userdata,
4571 returns its pointer.
4572 Otherwise, returns <code>NULL</code>.
4573
4574
4575
4576
4577
4578 <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
4579 <span class="apii">[-0, +0, &ndash;]</span>
4580 <pre>int lua_type (lua_State *L, int index);</pre>
4581
4582 <p>
4583 Returns the type of the value in the given valid index,
4584 or <code>LUA_TNONE</code> for a non-valid (but acceptable) index.
4585 The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
4586 defined in <code>lua.h</code>:
4587 <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a>,
4588 <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
4589 <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
4590 <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
4591 <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
4592 <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
4593 <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
4594 <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
4595 and
4596 <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
4597
4598
4599
4600
4601
4602 <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
4603 <span class="apii">[-0, +0, &ndash;]</span>
4604 <pre>const char *lua_typename (lua_State *L, int tp);</pre>
4605
4606 <p>
4607 Returns the name of the type encoded by the value <code>tp</code>,
4608 which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
4609
4610
4611
4612
4613
4614 <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
4615 <pre>typedef ... lua_Unsigned;</pre>
4616
4617 <p>
4618 The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
4619
4620
4621
4622
4623
4624 <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
4625 <span class="apii">[-0, +0, &ndash;]</span>
4626 <pre>int lua_upvalueindex (int i);</pre>
4627
4628 <p>
4629 Returns the pseudo-index that represents the <code>i</code>-th upvalue of
4630 the running function (see <a href="#4.4">&sect;4.4</a>).
4631
4632
4633
4634
4635
4636 <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
4637 <span class="apii">[-0, +0, <em>v</em>]</span>
4638 <pre>const lua_Number *lua_version (lua_State *L);</pre>
4639
4640 <p>
4641 Returns the address of the version number stored in the Lua core.
4642 When called with a valid <a href="#lua_State"><code>lua_State</code></a>,
4643 returns the address of the version used to create that state.
4644 When called with <code>NULL</code>,
4645 returns the address of the version running the call.
4646
4647
4648
4649
4650
4651 <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
4652 <pre>typedef int (*lua_Writer) (lua_State *L,
4653 const void* p,
4654 size_t sz,
4655 void* ud);</pre>
4656
4657 <p>
4658 The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
4659 Every time it produces another piece of chunk,
4660 <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
4661 passing along the buffer to be written (<code>p</code>),
4662 its size (<code>sz</code>),
4663 and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
4664
4665
4666 <p>
4667 The writer returns an error code:
4668 0&nbsp;means no errors;
4669 any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
4670 calling the writer again.
4671
4672
4673
4674
4675
4676 <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
4677 <span class="apii">[-?, +?, &ndash;]</span>
4678 <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
4679
4680 <p>
4681 Exchange values between different threads of the same state.
4682
4683
4684 <p>
4685 This function pops <code>n</code> values from the stack <code>from</code>,
4686 and pushes them onto the stack <code>to</code>.
4687
4688
4689
4690
4691
4692 <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
4693 <span class="apii">[-?, +?, <em>e</em>]</span>
4694 <pre>int lua_yield (lua_State *L, int nresults);</pre>
4695
4696 <p>
4697 This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
4698 but it has no continuation (see <a href="#4.7">&sect;4.7</a>).
4699 Therefore, when the thread resumes,
4700 it continues the function that called
4701 the function calling <code>lua_yield</code>.
4702
4703
4704
4705
4706
4707 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
4708 <span class="apii">[-?, +?, <em>e</em>]</span>
4709 <pre>int lua_yieldk (lua_State *L,
4710 int nresults,
4711 lua_KContext ctx,
4712 lua_KFunction k);</pre>
4713
4714 <p>
4715 Yields a coroutine (thread).
4716
4717
4718 <p>
4719 When a C&nbsp;function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
4720 the running coroutine suspends its execution,
4721 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
4722 The parameter <code>nresults</code> is the number of values from the stack
4723 that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
4724
4725
4726 <p>
4727 When the coroutine is resumed again,
4728 Lua calls the given continuation function <code>k</code> to continue
4729 the execution of the C function that yielded (see <a href="#4.7">&sect;4.7</a>).
4730 This continuation function receives the same stack
4731 from the previous function,
4732 with the <code>n</code> results removed and
4733 replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
4734 Moreover,
4735 the continuation function receives the value <code>ctx</code>
4736 that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
4737
4738
4739 <p>
4740 Usually, this function does not return;
4741 when the coroutine eventually resumes,
4742 it continues executing the continuation function.
4743 However, there is one special case,
4744 which is when this function is called
4745 from inside a line hook (see <a href="#4.9">&sect;4.9</a>).
4746 In that case, <code>lua_yieldk</code> should be called with no continuation
4747 (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>),
4748 and the hook should return immediately after the call.
4749 Lua will yield and,
4750 when the coroutine resumes again,
4751 it will continue the normal execution
4752 of the (Lua) function that triggered the hook.
4753
4754
4755 <p>
4756 This function can raise an error if it is called from a thread
4757 with a pending C call with no continuation function,
4758 or it is called from a thread that is not running inside a resume
4759 (e.g., the main thread).
4760
4761
4762
4763
4764
4765
4766
4767 <h2>4.9 &ndash; <a name="4.9">The Debug Interface</a></h2>
4768
4769 <p>
4770 Lua has no built-in debugging facilities.
4771 Instead, it offers a special interface
4772 by means of functions and <em>hooks</em>.
4773 This interface allows the construction of different
4774 kinds of debuggers, profilers, and other tools
4775 that need "inside information" from the interpreter.
4776
4777
4778
4779 <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
4780 <pre>typedef struct lua_Debug {
4781 int event;
4782 const char *name; /* (n) */
4783 const char *namewhat; /* (n) */
4784 const char *what; /* (S) */
4785 const char *source; /* (S) */
4786 int currentline; /* (l) */
4787 int linedefined; /* (S) */
4788 int lastlinedefined; /* (S) */
4789 unsigned char nups; /* (u) number of upvalues */
4790 unsigned char nparams; /* (u) number of parameters */
4791 char isvararg; /* (u) */
4792 char istailcall; /* (t) */
4793 char short_src[LUA_IDSIZE]; /* (S) */
4794 /* private part */
4795 <em>other fields</em>
4796 } lua_Debug;</pre>
4797
4798 <p>
4799 A structure used to carry different pieces of
4800 information about a function or an activation record.
4801 <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
4802 of this structure, for later use.
4803 To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
4804 call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
4805
4806
4807 <p>
4808 The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
4809
4810 <ul>
4811
4812 <li><b><code>source</code>: </b>
4813 the name of the chunk that created the function.
4814 If <code>source</code> starts with a '<code>@</code>',
4815 it means that the function was defined in a file where
4816 the file name follows the '<code>@</code>'.
4817 If <code>source</code> starts with a '<code>=</code>',
4818 the remainder of its contents describe the source in a user-dependent manner.
4819 Otherwise,
4820 the function was defined in a string where
4821 <code>source</code> is that string.
4822 </li>
4823
4824 <li><b><code>short_src</code>: </b>
4825 a "printable" version of <code>source</code>, to be used in error messages.
4826 </li>
4827
4828 <li><b><code>linedefined</code>: </b>
4829 the line number where the definition of the function starts.
4830 </li>
4831
4832 <li><b><code>lastlinedefined</code>: </b>
4833 the line number where the definition of the function ends.
4834 </li>
4835
4836 <li><b><code>what</code>: </b>
4837 the string <code>"Lua"</code> if the function is a Lua function,
4838 <code>"C"</code> if it is a C&nbsp;function,
4839 <code>"main"</code> if it is the main part of a chunk.
4840 </li>
4841
4842 <li><b><code>currentline</code>: </b>
4843 the current line where the given function is executing.
4844 When no line information is available,
4845 <code>currentline</code> is set to -1.
4846 </li>
4847
4848 <li><b><code>name</code>: </b>
4849 a reasonable name for the given function.
4850 Because functions in Lua are first-class values,
4851 they do not have a fixed name:
4852 some functions can be the value of multiple global variables,
4853 while others can be stored only in a table field.
4854 The <code>lua_getinfo</code> function checks how the function was
4855 called to find a suitable name.
4856 If it cannot find a name,
4857 then <code>name</code> is set to <code>NULL</code>.
4858 </li>
4859
4860 <li><b><code>namewhat</code>: </b>
4861 explains the <code>name</code> field.
4862 The value of <code>namewhat</code> can be
4863 <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
4864 <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
4865 according to how the function was called.
4866 (Lua uses the empty string when no other option seems to apply.)
4867 </li>
4868
4869 <li><b><code>istailcall</code>: </b>
4870 true if this function invocation was called by a tail call.
4871 In this case, the caller of this level is not in the stack.
4872 </li>
4873
4874 <li><b><code>nups</code>: </b>
4875 the number of upvalues of the function.
4876 </li>
4877
4878 <li><b><code>nparams</code>: </b>
4879 the number of fixed parameters of the function
4880 (always 0&nbsp;for C&nbsp;functions).
4881 </li>
4882
4883 <li><b><code>isvararg</code>: </b>
4884 true if the function is a vararg function
4885 (always true for C&nbsp;functions).
4886 </li>
4887
4888 </ul>
4889
4890
4891
4892
4893 <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
4894 <span class="apii">[-0, +0, &ndash;]</span>
4895 <pre>lua_Hook lua_gethook (lua_State *L);</pre>
4896
4897 <p>
4898 Returns the current hook function.
4899
4900
4901
4902
4903
4904 <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
4905 <span class="apii">[-0, +0, &ndash;]</span>
4906 <pre>int lua_gethookcount (lua_State *L);</pre>
4907
4908 <p>
4909 Returns the current hook count.
4910
4911
4912
4913
4914
4915 <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
4916 <span class="apii">[-0, +0, &ndash;]</span>
4917 <pre>int lua_gethookmask (lua_State *L);</pre>
4918
4919 <p>
4920 Returns the current hook mask.
4921
4922
4923
4924
4925
4926 <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
4927 <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span>
4928 <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
4929
4930 <p>
4931 Gets information about a specific function or function invocation.
4932
4933
4934 <p>
4935 To get information about a function invocation,
4936 the parameter <code>ar</code> must be a valid activation record that was
4937 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
4938 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
4939
4940
4941 <p>
4942 To get information about a function you push it onto the stack
4943 and start the <code>what</code> string with the character '<code>&gt;</code>'.
4944 (In that case,
4945 <code>lua_getinfo</code> pops the function from the top of the stack.)
4946 For instance, to know in which line a function <code>f</code> was defined,
4947 you can write the following code:
4948
4949 <pre>
4950 lua_Debug ar;
4951 lua_getglobal(L, "f"); /* get global 'f' */
4952 lua_getinfo(L, "&gt;S", &amp;ar);
4953 printf("%d\n", ar.linedefined);
4954 </pre>
4955
4956 <p>
4957 Each character in the string <code>what</code>
4958 selects some fields of the structure <code>ar</code> to be filled or
4959 a value to be pushed on the stack:
4960
4961 <ul>
4962
4963 <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>;
4964 </li>
4965
4966 <li><b>'<code>S</code>': </b>
4967 fills in the fields <code>source</code>, <code>short_src</code>,
4968 <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
4969 </li>
4970
4971 <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>;
4972 </li>
4973
4974 <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>;
4975 </li>
4976
4977 <li><b>'<code>u</code>': </b> fills in the fields
4978 <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>;
4979 </li>
4980
4981 <li><b>'<code>f</code>': </b>
4982 pushes onto the stack the function that is
4983 running at the given level;
4984 </li>
4985
4986 <li><b>'<code>L</code>': </b>
4987 pushes onto the stack a table whose indices are the
4988 numbers of the lines that are valid on the function.
4989 (A <em>valid line</em> is a line with some associated code,
4990 that is, a line where you can put a break point.
4991 Non-valid lines include empty lines and comments.)
4992
4993
4994 <p>
4995 If this option is given together with option '<code>f</code>',
4996 its table is pushed after the function.
4997 </li>
4998
4999 </ul>
5000
5001 <p>
5002 This function returns 0 on error
5003 (for instance, an invalid option in <code>what</code>).
5004
5005
5006
5007
5008
5009 <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
5010 <span class="apii">[-0, +(0|1), &ndash;]</span>
5011 <pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
5012
5013 <p>
5014 Gets information about a local variable of
5015 a given activation record or a given function.
5016
5017
5018 <p>
5019 In the first case,
5020 the parameter <code>ar</code> must be a valid activation record that was
5021 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5022 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5023 The index <code>n</code> selects which local variable to inspect;
5024 see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
5025 and names.
5026
5027
5028 <p>
5029 <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
5030 and returns its name.
5031
5032
5033 <p>
5034 In the second case, <code>ar</code> must be <code>NULL</code> and the function
5035 to be inspected must be at the top of the stack.
5036 In this case, only parameters of Lua functions are visible
5037 (as there is no information about what variables are active)
5038 and no values are pushed onto the stack.
5039
5040
5041 <p>
5042 Returns <code>NULL</code> (and pushes nothing)
5043 when the index is greater than
5044 the number of active local variables.
5045
5046
5047
5048
5049
5050 <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
5051 <span class="apii">[-0, +0, &ndash;]</span>
5052 <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
5053
5054 <p>
5055 Gets information about the interpreter runtime stack.
5056
5057
5058 <p>
5059 This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
5060 an identification of the <em>activation record</em>
5061 of the function executing at a given level.
5062 Level&nbsp;0 is the current running function,
5063 whereas level <em>n+1</em> is the function that has called level <em>n</em>
5064 (except for tail calls, which do not count on the stack).
5065 When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
5066 when called with a level greater than the stack depth,
5067 it returns 0.
5068
5069
5070
5071
5072
5073 <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
5074 <span class="apii">[-0, +(0|1), &ndash;]</span>
5075 <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
5076
5077 <p>
5078 Gets information about a closure's upvalue.
5079 (For Lua functions,
5080 upvalues are the external local variables that the function uses,
5081 and that are consequently included in its closure.)
5082 <a href="#lua_getupvalue"><code>lua_getupvalue</code></a> gets the index <code>n</code> of an upvalue,
5083 pushes the upvalue's value onto the stack,
5084 and returns its name.
5085 <code>funcindex</code> points to the closure in the stack.
5086 (Upvalues have no particular order,
5087 as they are active through the whole function.
5088 So, they are numbered in an arbitrary order.)
5089
5090
5091 <p>
5092 Returns <code>NULL</code> (and pushes nothing)
5093 when the index is greater than the number of upvalues.
5094 For C&nbsp;functions, this function uses the empty string <code>""</code>
5095 as a name for all upvalues.
5096
5097
5098
5099
5100
5101 <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
5102 <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
5103
5104 <p>
5105 Type for debugging hook functions.
5106
5107
5108 <p>
5109 Whenever a hook is called, its <code>ar</code> argument has its field
5110 <code>event</code> set to the specific event that triggered the hook.
5111 Lua identifies these events with the following constants:
5112 <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
5113 <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
5114 and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
5115 Moreover, for line events, the field <code>currentline</code> is also set.
5116 To get the value of any other field in <code>ar</code>,
5117 the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5118
5119
5120 <p>
5121 For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>,
5122 the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
5123 in this case, there will be no corresponding return event.
5124
5125
5126 <p>
5127 While Lua is running a hook, it disables other calls to hooks.
5128 Therefore, if a hook calls back Lua to execute a function or a chunk,
5129 this execution occurs without any calls to hooks.
5130
5131
5132 <p>
5133 Hook functions cannot have continuations,
5134 that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5135 <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>.
5136
5137
5138 <p>
5139 Hook functions can yield under the following conditions:
5140 Only count and line events can yield
5141 and they cannot yield any value;
5142 to yield a hook function must finish its execution
5143 calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero.
5144
5145
5146
5147
5148
5149 <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
5150 <span class="apii">[-0, +0, &ndash;]</span>
5151 <pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
5152
5153 <p>
5154 Sets the debugging hook function.
5155
5156
5157 <p>
5158 Argument <code>f</code> is the hook function.
5159 <code>mask</code> specifies on which events the hook will be called:
5160 it is formed by a bitwise or of the constants
5161 <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
5162 <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
5163 <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
5164 and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
5165 The <code>count</code> argument is only meaningful when the mask
5166 includes <code>LUA_MASKCOUNT</code>.
5167 For each event, the hook is called as explained below:
5168
5169 <ul>
5170
5171 <li><b>The call hook: </b> is called when the interpreter calls a function.
5172 The hook is called just after Lua enters the new function,
5173 before the function gets its arguments.
5174 </li>
5175
5176 <li><b>The return hook: </b> is called when the interpreter returns from a function.
5177 The hook is called just before Lua leaves the function.
5178 There is no standard way to access the values
5179 to be returned by the function.
5180 </li>
5181
5182 <li><b>The line hook: </b> is called when the interpreter is about to
5183 start the execution of a new line of code,
5184 or when it jumps back in the code (even to the same line).
5185 (This event only happens while Lua is executing a Lua function.)
5186 </li>
5187
5188 <li><b>The count hook: </b> is called after the interpreter executes every
5189 <code>count</code> instructions.
5190 (This event only happens while Lua is executing a Lua function.)
5191 </li>
5192
5193 </ul>
5194
5195 <p>
5196 A hook is disabled by setting <code>mask</code> to zero.
5197
5198
5199
5200
5201
5202 <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
5203 <span class="apii">[-(0|1), +0, &ndash;]</span>
5204 <pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
5205
5206 <p>
5207 Sets the value of a local variable of a given activation record.
5208 Parameters <code>ar</code> and <code>n</code> are as in <a href="#lua_getlocal"><code>lua_getlocal</code></a>
5209 (see <a href="#lua_getlocal"><code>lua_getlocal</code></a>).
5210 <a href="#lua_setlocal"><code>lua_setlocal</code></a> assigns the value at the top of the stack
5211 to the variable and returns its name.
5212 It also pops the value from the stack.
5213
5214
5215 <p>
5216 Returns <code>NULL</code> (and pops nothing)
5217 when the index is greater than
5218 the number of active local variables.
5219
5220
5221
5222
5223
5224 <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
5225 <span class="apii">[-(0|1), +0, &ndash;]</span>
5226 <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
5227
5228 <p>
5229 Sets the value of a closure's upvalue.
5230 It assigns the value at the top of the stack
5231 to the upvalue and returns its name.
5232 It also pops the value from the stack.
5233 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
5234 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>).
5235
5236
5237 <p>
5238 Returns <code>NULL</code> (and pops nothing)
5239 when the index is greater than the number of upvalues.
5240
5241
5242
5243
5244
5245 <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
5246 <span class="apii">[-0, +0, &ndash;]</span>
5247 <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre>
5248
5249 <p>
5250 Returns a unique identifier for the upvalue numbered <code>n</code>
5251 from the closure at index <code>funcindex</code>.
5252 Parameters <code>funcindex</code> and <code>n</code> are as in the <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>
5253 (see <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>)
5254 (but <code>n</code> cannot be greater than the number of upvalues).
5255
5256
5257 <p>
5258 These unique identifiers allow a program to check whether different
5259 closures share upvalues.
5260 Lua closures that share an upvalue
5261 (that is, that access a same external local variable)
5262 will return identical ids for those upvalue indices.
5263
5264
5265
5266
5267
5268 <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
5269 <span class="apii">[-0, +0, &ndash;]</span>
5270 <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
5271 int funcindex2, int n2);</pre>
5272
5273 <p>
5274 Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
5275 refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
5276
5277
5278
5279
5280
5281
5282
5283 <h1>5 &ndash; <a name="5">The Auxiliary Library</a></h1>
5284
5285 <p>
5286
5287 The <em>auxiliary library</em> provides several convenient functions
5288 to interface C with Lua.
5289 While the basic API provides the primitive functions for all
5290 interactions between C and Lua,
5291 the auxiliary library provides higher-level functions for some
5292 common tasks.
5293
5294
5295 <p>
5296 All functions and types from the auxiliary library
5297 are defined in header file <code>lauxlib.h</code> and
5298 have a prefix <code>luaL_</code>.
5299
5300
5301 <p>
5302 All functions in the auxiliary library are built on
5303 top of the basic API,
5304 and so they provide nothing that cannot be done with that API.
5305 Nevertheless, the use of the auxiliary library ensures
5306 more consistency to your code.
5307
5308
5309 <p>
5310 Several functions in the auxiliary library use internally some
5311 extra stack slots.
5312 When a function in the auxiliary library uses less than five slots,
5313 it does not check the stack size;
5314 it simply assumes that there are enough slots.
5315
5316
5317 <p>
5318 Several functions in the auxiliary library are used to
5319 check C&nbsp;function arguments.
5320 Because the error message is formatted for arguments
5321 (e.g., "<code>bad argument #1</code>"),
5322 you should not use these functions for other stack values.
5323
5324
5325 <p>
5326 Functions called <code>luaL_check*</code>
5327 always raise an error if the check is not satisfied.
5328
5329
5330
5331 <h2>5.1 &ndash; <a name="5.1">Functions and Types</a></h2>
5332
5333 <p>
5334 Here we list all functions and types from the auxiliary library
5335 in alphabetical order.
5336
5337
5338
5339 <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
5340 <span class="apii">[-?, +?, <em>e</em>]</span>
5341 <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
5342
5343 <p>
5344 Adds the byte <code>c</code> to the buffer <code>B</code>
5345 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5346
5347
5348
5349
5350
5351 <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
5352 <span class="apii">[-?, +?, <em>e</em>]</span>
5353 <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
5354
5355 <p>
5356 Adds the string pointed to by <code>s</code> with length <code>l</code> to
5357 the buffer <code>B</code>
5358 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5359 The string can contain embedded zeros.
5360
5361
5362
5363
5364
5365 <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
5366 <span class="apii">[-?, +?, <em>e</em>]</span>
5367 <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
5368
5369 <p>
5370 Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
5371 a string of length <code>n</code> previously copied to the
5372 buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
5373
5374
5375
5376
5377
5378 <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
5379 <span class="apii">[-?, +?, <em>e</em>]</span>
5380 <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
5381
5382 <p>
5383 Adds the zero-terminated string pointed to by <code>s</code>
5384 to the buffer <code>B</code>
5385 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5386
5387
5388
5389
5390
5391 <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
5392 <span class="apii">[-1, +?, <em>e</em>]</span>
5393 <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
5394
5395 <p>
5396 Adds the value at the top of the stack
5397 to the buffer <code>B</code>
5398 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5399 Pops the value.
5400
5401
5402 <p>
5403 This is the only function on string buffers that can (and must)
5404 be called with an extra element on the stack,
5405 which is the value to be added to the buffer.
5406
5407
5408
5409
5410
5411 <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
5412 <span class="apii">[-0, +0, <em>v</em>]</span>
5413 <pre>void luaL_argcheck (lua_State *L,
5414 int cond,
5415 int arg,
5416 const char *extramsg);</pre>
5417
5418 <p>
5419 Checks whether <code>cond</code> is true.
5420 If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>).
5421
5422
5423
5424
5425
5426 <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
5427 <span class="apii">[-0, +0, <em>v</em>]</span>
5428 <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre>
5429
5430 <p>
5431 Raises an error reporting a problem with argument <code>arg</code>
5432 of the C function that called it,
5433 using a standard message
5434 that includes <code>extramsg</code> as a comment:
5435
5436 <pre>
5437 bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>)
5438 </pre><p>
5439 This function never returns.
5440
5441
5442
5443
5444
5445 <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
5446 <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
5447
5448 <p>
5449 Type for a <em>string buffer</em>.
5450
5451
5452 <p>
5453 A string buffer allows C&nbsp;code to build Lua strings piecemeal.
5454 Its pattern of use is as follows:
5455
5456 <ul>
5457
5458 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5459
5460 <li>Then initialize it with a call <code>luaL_buffinit(L, &amp;b)</code>.</li>
5461
5462 <li>
5463 Then add string pieces to the buffer calling any of
5464 the <code>luaL_add*</code> functions.
5465 </li>
5466
5467 <li>
5468 Finish by calling <code>luaL_pushresult(&amp;b)</code>.
5469 This call leaves the final string on the top of the stack.
5470 </li>
5471
5472 </ul>
5473
5474 <p>
5475 If you know beforehand the total size of the resulting string,
5476 you can use the buffer like this:
5477
5478 <ul>
5479
5480 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
5481
5482 <li>Then initialize it and preallocate a space of
5483 size <code>sz</code> with a call <code>luaL_buffinitsize(L, &amp;b, sz)</code>.</li>
5484
5485 <li>Then copy the string into that space.</li>
5486
5487 <li>
5488 Finish by calling <code>luaL_pushresultsize(&amp;b, sz)</code>,
5489 where <code>sz</code> is the total size of the resulting string
5490 copied into that space.
5491 </li>
5492
5493 </ul>
5494
5495 <p>
5496 During its normal operation,
5497 a string buffer uses a variable number of stack slots.
5498 So, while using a buffer, you cannot assume that you know where
5499 the top of the stack is.
5500 You can use the stack between successive calls to buffer operations
5501 as long as that use is balanced;
5502 that is,
5503 when you call a buffer operation,
5504 the stack is at the same level
5505 it was immediately after the previous buffer operation.
5506 (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
5507 After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
5508 level when the buffer was initialized,
5509 plus the final string on its top.
5510
5511
5512
5513
5514
5515 <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
5516 <span class="apii">[-0, +0, &ndash;]</span>
5517 <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
5518
5519 <p>
5520 Initializes a buffer <code>B</code>.
5521 This function does not allocate any space;
5522 the buffer must be declared as a variable
5523 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5524
5525
5526
5527
5528
5529 <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
5530 <span class="apii">[-?, +?, <em>e</em>]</span>
5531 <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre>
5532
5533 <p>
5534 Equivalent to the sequence
5535 <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>.
5536
5537
5538
5539
5540
5541 <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
5542 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
5543 <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
5544
5545 <p>
5546 Calls a metamethod.
5547
5548
5549 <p>
5550 If the object at index <code>obj</code> has a metatable and this
5551 metatable has a field <code>e</code>,
5552 this function calls this field passing the object as its only argument.
5553 In this case this function returns true and pushes onto the
5554 stack the value returned by the call.
5555 If there is no metatable or no metamethod,
5556 this function returns false (without pushing any value on the stack).
5557
5558
5559
5560
5561
5562 <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
5563 <span class="apii">[-0, +0, <em>v</em>]</span>
5564 <pre>void luaL_checkany (lua_State *L, int arg);</pre>
5565
5566 <p>
5567 Checks whether the function has an argument
5568 of any type (including <b>nil</b>) at position <code>arg</code>.
5569
5570
5571
5572
5573
5574 <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
5575 <span class="apii">[-0, +0, <em>v</em>]</span>
5576 <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre>
5577
5578 <p>
5579 Checks whether the function argument <code>arg</code> is an integer
5580 (or can be converted to an integer)
5581 and returns this integer cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
5582
5583
5584
5585
5586
5587 <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
5588 <span class="apii">[-0, +0, <em>v</em>]</span>
5589 <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre>
5590
5591 <p>
5592 Checks whether the function argument <code>arg</code> is a string
5593 and returns this string;
5594 if <code>l</code> is not <code>NULL</code> fills <code>*l</code>
5595 with the string's length.
5596
5597
5598 <p>
5599 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
5600 so all conversions and caveats of that function apply here.
5601
5602
5603
5604
5605
5606 <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
5607 <span class="apii">[-0, +0, <em>v</em>]</span>
5608 <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre>
5609
5610 <p>
5611 Checks whether the function argument <code>arg</code> is a number
5612 and returns this number.
5613
5614
5615
5616
5617
5618 <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
5619 <span class="apii">[-0, +0, <em>v</em>]</span>
5620 <pre>int luaL_checkoption (lua_State *L,
5621 int arg,
5622 const char *def,
5623 const char *const lst[]);</pre>
5624
5625 <p>
5626 Checks whether the function argument <code>arg</code> is a string and
5627 searches for this string in the array <code>lst</code>
5628 (which must be NULL-terminated).
5629 Returns the index in the array where the string was found.
5630 Raises an error if the argument is not a string or
5631 if the string cannot be found.
5632
5633
5634 <p>
5635 If <code>def</code> is not <code>NULL</code>,
5636 the function uses <code>def</code> as a default value when
5637 there is no argument <code>arg</code> or when this argument is <b>nil</b>.
5638
5639
5640 <p>
5641 This is a useful function for mapping strings to C&nbsp;enums.
5642 (The usual convention in Lua libraries is
5643 to use strings instead of numbers to select options.)
5644
5645
5646
5647
5648
5649 <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
5650 <span class="apii">[-0, +0, <em>v</em>]</span>
5651 <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
5652
5653 <p>
5654 Grows the stack size to <code>top + sz</code> elements,
5655 raising an error if the stack cannot grow to that size.
5656 <code>msg</code> is an additional text to go into the error message
5657 (or <code>NULL</code> for no additional text).
5658
5659
5660
5661
5662
5663 <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
5664 <span class="apii">[-0, +0, <em>v</em>]</span>
5665 <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre>
5666
5667 <p>
5668 Checks whether the function argument <code>arg</code> is a string
5669 and returns this string.
5670
5671
5672 <p>
5673 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
5674 so all conversions and caveats of that function apply here.
5675
5676
5677
5678
5679
5680 <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
5681 <span class="apii">[-0, +0, <em>v</em>]</span>
5682 <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre>
5683
5684 <p>
5685 Checks whether the function argument <code>arg</code> has type <code>t</code>.
5686 See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
5687
5688
5689
5690
5691
5692 <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
5693 <span class="apii">[-0, +0, <em>v</em>]</span>
5694 <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre>
5695
5696 <p>
5697 Checks whether the function argument <code>arg</code> is a userdata
5698 of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and
5699 returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
5700
5701
5702
5703
5704
5705 <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
5706 <span class="apii">[-0, +0, &ndash;]</span>
5707 <pre>void luaL_checkversion (lua_State *L);</pre>
5708
5709 <p>
5710 Checks whether the core running the call,
5711 the core that created the Lua state,
5712 and the code making the call are all using the same version of Lua.
5713 Also checks whether the core running the call
5714 and the core that created the Lua state
5715 are using the same address space.
5716
5717
5718
5719
5720
5721 <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
5722 <span class="apii">[-0, +?, <em>e</em>]</span>
5723 <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
5724
5725 <p>
5726 Loads and runs the given file.
5727 It is defined as the following macro:
5728
5729 <pre>
5730 (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
5731 </pre><p>
5732 It returns false if there are no errors
5733 or true in case of errors.
5734
5735
5736
5737
5738
5739 <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
5740 <span class="apii">[-0, +?, &ndash;]</span>
5741 <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
5742
5743 <p>
5744 Loads and runs the given string.
5745 It is defined as the following macro:
5746
5747 <pre>
5748 (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
5749 </pre><p>
5750 It returns false if there are no errors
5751 or true in case of errors.
5752
5753
5754
5755
5756
5757 <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
5758 <span class="apii">[-0, +0, <em>v</em>]</span>
5759 <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
5760
5761 <p>
5762 Raises an error.
5763 The error message format is given by <code>fmt</code>
5764 plus any extra arguments,
5765 following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
5766 It also adds at the beginning of the message the file name and
5767 the line number where the error occurred,
5768 if this information is available.
5769
5770
5771 <p>
5772 This function never returns,
5773 but it is an idiom to use it in C&nbsp;functions
5774 as <code>return luaL_error(<em>args</em>)</code>.
5775
5776
5777
5778
5779
5780 <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
5781 <span class="apii">[-0, +3, <em>e</em>]</span>
5782 <pre>int luaL_execresult (lua_State *L, int stat);</pre>
5783
5784 <p>
5785 This function produces the return values for
5786 process-related functions in the standard library
5787 (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
5788
5789
5790
5791
5792
5793 <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
5794 <span class="apii">[-0, +(1|3), <em>e</em>]</span>
5795 <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre>
5796
5797 <p>
5798 This function produces the return values for
5799 file-related functions in the standard library
5800 (<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.).
5801
5802
5803
5804
5805
5806 <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
5807 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
5808 <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
5809
5810 <p>
5811 Pushes onto the stack the field <code>e</code> from the metatable
5812 of the object at index <code>obj</code> and returns the type of pushed value.
5813 If the object does not have a metatable,
5814 or if the metatable does not have this field,
5815 pushes nothing and returns <code>LUA_TNIL</code>.
5816
5817
5818
5819
5820
5821 <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
5822 <span class="apii">[-0, +1, &ndash;]</span>
5823 <pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre>
5824
5825 <p>
5826 Pushes onto the stack the metatable associated with name <code>tname</code>
5827 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
5828 If there is no metatable associated with <code>tname</code>,
5829 returns false and pushes <b>nil</b>.
5830
5831
5832
5833
5834
5835 <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
5836 <span class="apii">[-0, +1, <em>e</em>]</span>
5837 <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
5838
5839 <p>
5840 Ensures that the value <code>t[fname]</code>,
5841 where <code>t</code> is the value at index <code>idx</code>,
5842 is a table,
5843 and pushes that table onto the stack.
5844 Returns true if it finds a previous table there
5845 and false if it creates a new table.
5846
5847
5848
5849
5850
5851 <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
5852 <span class="apii">[-0, +1, <em>e</em>]</span>
5853 <pre>const char *luaL_gsub (lua_State *L,
5854 const char *s,
5855 const char *p,
5856 const char *r);</pre>
5857
5858 <p>
5859 Creates a copy of string <code>s</code> by replacing
5860 any occurrence of the string <code>p</code>
5861 with the string <code>r</code>.
5862 Pushes the resulting string on the stack and returns it.
5863
5864
5865
5866
5867
5868 <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
5869 <span class="apii">[-0, +0, <em>e</em>]</span>
5870 <pre>lua_Integer luaL_len (lua_State *L, int index);</pre>
5871
5872 <p>
5873 Returns the "length" of the value at the given index
5874 as a number;
5875 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">&sect;3.4.7</a>).
5876 Raises an error if the result of the operation is not an integer.
5877 (This case only can happen through metamethods.)
5878
5879
5880
5881
5882
5883 <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
5884 <span class="apii">[-0, +1, &ndash;]</span>
5885 <pre>int luaL_loadbuffer (lua_State *L,
5886 const char *buff,
5887 size_t sz,
5888 const char *name);</pre>
5889
5890 <p>
5891 Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
5892
5893
5894
5895
5896
5897 <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
5898 <span class="apii">[-0, +1, &ndash;]</span>
5899 <pre>int luaL_loadbufferx (lua_State *L,
5900 const char *buff,
5901 size_t sz,
5902 const char *name,
5903 const char *mode);</pre>
5904
5905 <p>
5906 Loads a buffer as a Lua chunk.
5907 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
5908 buffer pointed to by <code>buff</code> with size <code>sz</code>.
5909
5910
5911 <p>
5912 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
5913 <code>name</code> is the chunk name,
5914 used for debug information and error messages.
5915 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
5916
5917
5918
5919
5920
5921 <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
5922 <span class="apii">[-0, +1, <em>e</em>]</span>
5923 <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
5924
5925 <p>
5926 Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
5927
5928
5929
5930
5931
5932 <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
5933 <span class="apii">[-0, +1, <em>e</em>]</span>
5934 <pre>int luaL_loadfilex (lua_State *L, const char *filename,
5935 const char *mode);</pre>
5936
5937 <p>
5938 Loads a file as a Lua chunk.
5939 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
5940 named <code>filename</code>.
5941 If <code>filename</code> is <code>NULL</code>,
5942 then it loads from the standard input.
5943 The first line in the file is ignored if it starts with a <code>#</code>.
5944
5945
5946 <p>
5947 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
5948
5949
5950 <p>
5951 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
5952 but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
5953 if it cannot open/read the file or the file has a wrong mode.
5954
5955
5956 <p>
5957 As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
5958 it does not run it.
5959
5960
5961
5962
5963
5964 <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
5965 <span class="apii">[-0, +1, &ndash;]</span>
5966 <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
5967
5968 <p>
5969 Loads a string as a Lua chunk.
5970 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
5971 the zero-terminated string <code>s</code>.
5972
5973
5974 <p>
5975 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
5976
5977
5978 <p>
5979 Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
5980 it does not run it.
5981
5982
5983
5984
5985
5986 <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
5987 <span class="apii">[-0, +1, <em>e</em>]</span>
5988 <pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre>
5989
5990 <p>
5991 Creates a new table and registers there
5992 the functions in list <code>l</code>.
5993
5994
5995 <p>
5996 It is implemented as the following macro:
5997
5998 <pre>
5999 (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
6000 </pre><p>
6001 The array <code>l</code> must be the actual array,
6002 not a pointer to it.
6003
6004
6005
6006
6007
6008 <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
6009 <span class="apii">[-0, +1, <em>e</em>]</span>
6010 <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre>
6011
6012 <p>
6013 Creates a new table with a size optimized
6014 to store all entries in the array <code>l</code>
6015 (but does not actually store them).
6016 It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
6017 (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
6018
6019
6020 <p>
6021 It is implemented as a macro.
6022 The array <code>l</code> must be the actual array,
6023 not a pointer to it.
6024
6025
6026
6027
6028
6029 <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
6030 <span class="apii">[-0, +1, <em>e</em>]</span>
6031 <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
6032
6033 <p>
6034 If the registry already has the key <code>tname</code>,
6035 returns 0.
6036 Otherwise,
6037 creates a new table to be used as a metatable for userdata,
6038 adds to this new table the pair <code>__name = tname</code>,
6039 adds to the registry the pair <code>[tname] = new table</code>,
6040 and returns 1.
6041 (The entry <code>__name</code> is used by some error-reporting functions.)
6042
6043
6044 <p>
6045 In both cases pushes onto the stack the final value associated
6046 with <code>tname</code> in the registry.
6047
6048
6049
6050
6051
6052 <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
6053 <span class="apii">[-0, +0, &ndash;]</span>
6054 <pre>lua_State *luaL_newstate (void);</pre>
6055
6056 <p>
6057 Creates a new Lua state.
6058 It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
6059 allocator based on the standard&nbsp;C <code>realloc</code> function
6060 and then sets a panic function (see <a href="#4.6">&sect;4.6</a>) that prints
6061 an error message to the standard error output in case of fatal
6062 errors.
6063
6064
6065 <p>
6066 Returns the new state,
6067 or <code>NULL</code> if there is a memory allocation error.
6068
6069
6070
6071
6072
6073 <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
6074 <span class="apii">[-0, +0, <em>e</em>]</span>
6075 <pre>void luaL_openlibs (lua_State *L);</pre>
6076
6077 <p>
6078 Opens all standard Lua libraries into the given state.
6079
6080
6081
6082
6083
6084 <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
6085 <span class="apii">[-0, +0, <em>v</em>]</span>
6086 <pre>lua_Integer luaL_optinteger (lua_State *L,
6087 int arg,
6088 lua_Integer d);</pre>
6089
6090 <p>
6091 If the function argument <code>arg</code> is an integer
6092 (or convertible to an integer),
6093 returns this integer.
6094 If this argument is absent or is <b>nil</b>,
6095 returns <code>d</code>.
6096 Otherwise, raises an error.
6097
6098
6099
6100
6101
6102 <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
6103 <span class="apii">[-0, +0, <em>v</em>]</span>
6104 <pre>const char *luaL_optlstring (lua_State *L,
6105 int arg,
6106 const char *d,
6107 size_t *l);</pre>
6108
6109 <p>
6110 If the function argument <code>arg</code> is a string,
6111 returns this string.
6112 If this argument is absent or is <b>nil</b>,
6113 returns <code>d</code>.
6114 Otherwise, raises an error.
6115
6116
6117 <p>
6118 If <code>l</code> is not <code>NULL</code>,
6119 fills the position <code>*l</code> with the result's length.
6120
6121
6122
6123
6124
6125 <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
6126 <span class="apii">[-0, +0, <em>v</em>]</span>
6127 <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre>
6128
6129 <p>
6130 If the function argument <code>arg</code> is a number,
6131 returns this number.
6132 If this argument is absent or is <b>nil</b>,
6133 returns <code>d</code>.
6134 Otherwise, raises an error.
6135
6136
6137
6138
6139
6140 <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
6141 <span class="apii">[-0, +0, <em>v</em>]</span>
6142 <pre>const char *luaL_optstring (lua_State *L,
6143 int arg,
6144 const char *d);</pre>
6145
6146 <p>
6147 If the function argument <code>arg</code> is a string,
6148 returns this string.
6149 If this argument is absent or is <b>nil</b>,
6150 returns <code>d</code>.
6151 Otherwise, raises an error.
6152
6153
6154
6155
6156
6157 <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
6158 <span class="apii">[-?, +?, <em>e</em>]</span>
6159 <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
6160
6161 <p>
6162 Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
6163 with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
6164
6165
6166
6167
6168
6169 <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
6170 <span class="apii">[-?, +?, <em>e</em>]</span>
6171 <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre>
6172
6173 <p>
6174 Returns an address to a space of size <code>sz</code>
6175 where you can copy a string to be added to buffer <code>B</code>
6176 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6177 After copying the string into this space you must call
6178 <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
6179 it to the buffer.
6180
6181
6182
6183
6184
6185 <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
6186 <span class="apii">[-?, +1, <em>e</em>]</span>
6187 <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
6188
6189 <p>
6190 Finishes the use of buffer <code>B</code> leaving the final string on
6191 the top of the stack.
6192
6193
6194
6195
6196
6197 <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
6198 <span class="apii">[-?, +1, <em>e</em>]</span>
6199 <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre>
6200
6201 <p>
6202 Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>.
6203
6204
6205
6206
6207
6208 <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
6209 <span class="apii">[-1, +0, <em>e</em>]</span>
6210 <pre>int luaL_ref (lua_State *L, int t);</pre>
6211
6212 <p>
6213 Creates and returns a <em>reference</em>,
6214 in the table at index <code>t</code>,
6215 for the object at the top of the stack (and pops the object).
6216
6217
6218 <p>
6219 A reference is a unique integer key.
6220 As long as you do not manually add integer keys into table <code>t</code>,
6221 <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
6222 You can retrieve an object referred by reference <code>r</code>
6223 by calling <code>lua_rawgeti(L, t, r)</code>.
6224 Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object.
6225
6226
6227 <p>
6228 If the object at the top of the stack is <b>nil</b>,
6229 <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
6230 The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
6231 from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
6232
6233
6234
6235
6236
6237 <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
6238 <pre>typedef struct luaL_Reg {
6239 const char *name;
6240 lua_CFunction func;
6241 } luaL_Reg;</pre>
6242
6243 <p>
6244 Type for arrays of functions to be registered by
6245 <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
6246 <code>name</code> is the function name and <code>func</code> is a pointer to
6247 the function.
6248 Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry
6249 in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
6250
6251
6252
6253
6254
6255 <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
6256 <span class="apii">[-0, +1, <em>e</em>]</span>
6257 <pre>void luaL_requiref (lua_State *L, const char *modname,
6258 lua_CFunction openf, int glb);</pre>
6259
6260 <p>
6261 If <code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</code></a>,
6262 calls function <code>openf</code> with string <code>modname</code> as an argument
6263 and sets the call result in <code>package.loaded[modname]</code>,
6264 as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
6265
6266
6267 <p>
6268 If <code>glb</code> is true,
6269 also stores the module into global <code>modname</code>.
6270
6271
6272 <p>
6273 Leaves a copy of the module on the stack.
6274
6275
6276
6277
6278
6279 <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
6280 <span class="apii">[-nup, +0, <em>e</em>]</span>
6281 <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre>
6282
6283 <p>
6284 Registers all functions in the array <code>l</code>
6285 (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
6286 (below optional upvalues, see next).
6287
6288
6289 <p>
6290 When <code>nup</code> is not zero,
6291 all functions are created sharing <code>nup</code> upvalues,
6292 which must be previously pushed on the stack
6293 on top of the library table.
6294 These values are popped from the stack after the registration.
6295
6296
6297
6298
6299
6300 <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
6301 <span class="apii">[-0, +0, &ndash;]</span>
6302 <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre>
6303
6304 <p>
6305 Sets the metatable of the object at the top of the stack
6306 as the metatable associated with name <code>tname</code>
6307 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6308
6309
6310
6311
6312
6313 <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3>
6314 <pre>typedef struct luaL_Stream {
6315 FILE *f;
6316 lua_CFunction closef;
6317 } luaL_Stream;</pre>
6318
6319 <p>
6320 The standard representation for file handles,
6321 which is used by the standard I/O library.
6322
6323
6324 <p>
6325 A file handle is implemented as a full userdata,
6326 with a metatable called <code>LUA_FILEHANDLE</code>
6327 (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name).
6328 The metatable is created by the I/O library
6329 (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6330
6331
6332 <p>
6333 This userdata must start with the structure <code>luaL_Stream</code>;
6334 it can contain other data after this initial structure.
6335 Field <code>f</code> points to the corresponding C stream
6336 (or it can be <code>NULL</code> to indicate an incompletely created handle).
6337 Field <code>closef</code> points to a Lua function
6338 that will be called to close the stream
6339 when the handle is closed or collected;
6340 this function receives the file handle as its sole argument and
6341 must return either <b>true</b> (in case of success)
6342 or <b>nil</b> plus an error message (in case of error).
6343 Once Lua calls this field,
6344 the field value is changed to <code>NULL</code>
6345 to signal that the handle is closed.
6346
6347
6348
6349
6350
6351 <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
6352 <span class="apii">[-0, +0, <em>e</em>]</span>
6353 <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre>
6354
6355 <p>
6356 This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
6357 except that, when the test fails,
6358 it returns <code>NULL</code> instead of raising an error.
6359
6360
6361
6362
6363
6364 <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
6365 <span class="apii">[-0, +1, <em>e</em>]</span>
6366 <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre>
6367
6368 <p>
6369 Converts any Lua value at the given index to a C&nbsp;string
6370 in a reasonable format.
6371 The resulting string is pushed onto the stack and also
6372 returned by the function.
6373 If <code>len</code> is not <code>NULL</code>,
6374 the function also sets <code>*len</code> with the string length.
6375
6376
6377 <p>
6378 If the value has a metatable with a <code>"__tostring"</code> field,
6379 then <code>luaL_tolstring</code> calls the corresponding metamethod
6380 with the value as argument,
6381 and uses the result of the call as its result.
6382
6383
6384
6385
6386
6387 <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
6388 <span class="apii">[-0, +1, <em>e</em>]</span>
6389 <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
6390 int level);</pre>
6391
6392 <p>
6393 Creates and pushes a traceback of the stack <code>L1</code>.
6394 If <code>msg</code> is not <code>NULL</code> it is appended
6395 at the beginning of the traceback.
6396 The <code>level</code> parameter tells at which level
6397 to start the traceback.
6398
6399
6400
6401
6402
6403 <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
6404 <span class="apii">[-0, +0, &ndash;]</span>
6405 <pre>const char *luaL_typename (lua_State *L, int index);</pre>
6406
6407 <p>
6408 Returns the name of the type of the value at the given index.
6409
6410
6411
6412
6413
6414 <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
6415 <span class="apii">[-0, +0, &ndash;]</span>
6416 <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
6417
6418 <p>
6419 Releases reference <code>ref</code> from the table at index <code>t</code>
6420 (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
6421 The entry is removed from the table,
6422 so that the referred object can be collected.
6423 The reference <code>ref</code> is also freed to be used again.
6424
6425
6426 <p>
6427 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>,
6428 <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
6429
6430
6431
6432
6433
6434 <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
6435 <span class="apii">[-0, +1, <em>e</em>]</span>
6436 <pre>void luaL_where (lua_State *L, int lvl);</pre>
6437
6438 <p>
6439 Pushes onto the stack a string identifying the current position
6440 of the control at level <code>lvl</code> in the call stack.
6441 Typically this string has the following format:
6442
6443 <pre>
6444 <em>chunkname</em>:<em>currentline</em>:
6445 </pre><p>
6446 Level&nbsp;0 is the running function,
6447 level&nbsp;1 is the function that called the running function,
6448 etc.
6449
6450
6451 <p>
6452 This function is used to build a prefix for error messages.
6453
6454
6455
6456
6457
6458
6459
6460 <h1>6 &ndash; <a name="6">Standard Libraries</a></h1>
6461
6462 <p>
6463 The standard Lua libraries provide useful functions
6464 that are implemented directly through the C&nbsp;API.
6465 Some of these functions provide essential services to the language
6466 (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>);
6467 others provide access to "outside" services (e.g., I/O);
6468 and others could be implemented in Lua itself,
6469 but are quite useful or have critical performance requirements that
6470 deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>).
6471
6472
6473 <p>
6474 All libraries are implemented through the official C&nbsp;API
6475 and are provided as separate C&nbsp;modules.
6476 Currently, Lua has the following standard libraries:
6477
6478 <ul>
6479
6480 <li>basic library (<a href="#6.1">&sect;6.1</a>);</li>
6481
6482 <li>coroutine library (<a href="#6.2">&sect;6.2</a>);</li>
6483
6484 <li>package library (<a href="#6.3">&sect;6.3</a>);</li>
6485
6486 <li>string manipulation (<a href="#6.4">&sect;6.4</a>);</li>
6487
6488 <li>basic UTF-8 support (<a href="#6.5">&sect;6.5</a>);</li>
6489
6490 <li>table manipulation (<a href="#6.6">&sect;6.6</a>);</li>
6491
6492 <li>mathematical functions (<a href="#6.7">&sect;6.7</a>) (sin, log, etc.);</li>
6493
6494 <li>input and output (<a href="#6.8">&sect;6.8</a>);</li>
6495
6496 <li>operating system facilities (<a href="#6.9">&sect;6.9</a>);</li>
6497
6498 <li>debug facilities (<a href="#6.10">&sect;6.10</a>).</li>
6499
6500 </ul><p>
6501 Except for the basic and the package libraries,
6502 each library provides all its functions as fields of a global table
6503 or as methods of its objects.
6504
6505
6506 <p>
6507 To have access to these libraries,
6508 the C&nbsp;host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function,
6509 which opens all standard libraries.
6510 Alternatively,
6511 the host program can open them individually by using
6512 <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call
6513 <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library),
6514 <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library),
6515 <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library),
6516 <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library),
6517 <a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF8 library),
6518 <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library),
6519 <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library),
6520 <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library),
6521 <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library),
6522 and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library).
6523 These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>.
6524
6525
6526
6527 <h2>6.1 &ndash; <a name="6.1">Basic Functions</a></h2>
6528
6529 <p>
6530 The basic library provides core functions to Lua.
6531 If you do not include this library in your application,
6532 you should check carefully whether you need to provide
6533 implementations for some of its facilities.
6534
6535
6536 <p>
6537 <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
6538
6539
6540 <p>
6541 Calls <a href="#pdf-error"><code>error</code></a> if
6542 the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
6543 otherwise, returns all its arguments.
6544 In case of error,
6545 <code>message</code> is the error object;
6546 when absent, it defaults to "<code>assertion failed!</code>"
6547
6548
6549
6550
6551 <p>
6552 <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3>
6553
6554
6555 <p>
6556 This function is a generic interface to the garbage collector.
6557 It performs different functions according to its first argument, <code>opt</code>:
6558
6559 <ul>
6560
6561 <li><b>"<code>collect</code>": </b>
6562 performs a full garbage-collection cycle.
6563 This is the default option.
6564 </li>
6565
6566 <li><b>"<code>stop</code>": </b>
6567 stops automatic execution of the garbage collector.
6568 The collector will run only when explicitly invoked,
6569 until a call to restart it.
6570 </li>
6571
6572 <li><b>"<code>restart</code>": </b>
6573 restarts automatic execution of the garbage collector.
6574 </li>
6575
6576 <li><b>"<code>count</code>": </b>
6577 returns the total memory in use by Lua in Kbytes.
6578 The value has a fractional part,
6579 so that it multiplied by 1024
6580 gives the exact number of bytes in use by Lua
6581 (except for overflows).
6582 </li>
6583
6584 <li><b>"<code>step</code>": </b>
6585 performs a garbage-collection step.
6586 The step "size" is controlled by <code>arg</code>.
6587 With a zero value,
6588 the collector will perform one basic (indivisible) step.
6589 For non-zero values,
6590 the collector will perform as if that amount of memory
6591 (in KBytes) had been allocated by Lua.
6592 Returns <b>true</b> if the step finished a collection cycle.
6593 </li>
6594
6595 <li><b>"<code>setpause</code>": </b>
6596 sets <code>arg</code> as the new value for the <em>pause</em> of
6597 the collector (see <a href="#2.5">&sect;2.5</a>).
6598 Returns the previous value for <em>pause</em>.
6599 </li>
6600
6601 <li><b>"<code>setstepmul</code>": </b>
6602 sets <code>arg</code> as the new value for the <em>step multiplier</em> of
6603 the collector (see <a href="#2.5">&sect;2.5</a>).
6604 Returns the previous value for <em>step</em>.
6605 </li>
6606
6607 <li><b>"<code>isrunning</code>": </b>
6608 returns a boolean that tells whether the collector is running
6609 (i.e., not stopped).
6610 </li>
6611
6612 </ul>
6613
6614
6615
6616 <p>
6617 <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3>
6618 Opens the named file and executes its contents as a Lua chunk.
6619 When called without arguments,
6620 <code>dofile</code> executes the contents of the standard input (<code>stdin</code>).
6621 Returns all values returned by the chunk.
6622 In case of errors, <code>dofile</code> propagates the error
6623 to its caller (that is, <code>dofile</code> does not run in protected mode).
6624
6625
6626
6627
6628 <p>
6629 <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
6630 Terminates the last protected function called
6631 and returns <code>message</code> as the error object.
6632 Function <code>error</code> never returns.
6633
6634
6635 <p>
6636 Usually, <code>error</code> adds some information about the error position
6637 at the beginning of the message, if the message is a string.
6638 The <code>level</code> argument specifies how to get the error position.
6639 With level&nbsp;1 (the default), the error position is where the
6640 <code>error</code> function was called.
6641 Level&nbsp;2 points the error to where the function
6642 that called <code>error</code> was called; and so on.
6643 Passing a level&nbsp;0 avoids the addition of error position information
6644 to the message.
6645
6646
6647
6648
6649 <p>
6650 <hr><h3><a name="pdf-_G"><code>_G</code></a></h3>
6651 A global variable (not a function) that
6652 holds the global environment (see <a href="#2.2">&sect;2.2</a>).
6653 Lua itself does not use this variable;
6654 changing its value does not affect any environment,
6655 nor vice versa.
6656
6657
6658
6659
6660 <p>
6661 <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3>
6662
6663
6664 <p>
6665 If <code>object</code> does not have a metatable, returns <b>nil</b>.
6666 Otherwise,
6667 if the object's metatable has a <code>"__metatable"</code> field,
6668 returns the associated value.
6669 Otherwise, returns the metatable of the given object.
6670
6671
6672
6673
6674 <p>
6675 <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3>
6676
6677
6678 <p>
6679 Returns three values (an iterator function, the table <code>t</code>, and 0)
6680 so that the construction
6681
6682 <pre>
6683 for i,v in ipairs(t) do <em>body</em> end
6684 </pre><p>
6685 will iterate over the key&ndash;value pairs
6686 (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
6687 up to the first nil value.
6688
6689
6690
6691
6692 <p>
6693 <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3>
6694
6695
6696 <p>
6697 Loads a chunk.
6698
6699
6700 <p>
6701 If <code>chunk</code> is a string, the chunk is this string.
6702 If <code>chunk</code> is a function,
6703 <code>load</code> calls it repeatedly to get the chunk pieces.
6704 Each call to <code>chunk</code> must return a string that concatenates
6705 with previous results.
6706 A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
6707
6708
6709 <p>
6710 If there are no syntactic errors,
6711 returns the compiled chunk as a function;
6712 otherwise, returns <b>nil</b> plus the error message.
6713
6714
6715 <p>
6716 If the resulting function has upvalues,
6717 the first upvalue is set to the value of <code>env</code>,
6718 if that parameter is given,
6719 or to the value of the global environment.
6720 Other upvalues are initialized with <b>nil</b>.
6721 (When you load a main chunk,
6722 the resulting function will always have exactly one upvalue,
6723 the <code>_ENV</code> variable (see <a href="#2.2">&sect;2.2</a>).
6724 However,
6725 when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>),
6726 the resulting function can have an arbitrary number of upvalues.)
6727 All upvalues are fresh, that is,
6728 they are not shared with any other function.
6729
6730
6731 <p>
6732 <code>chunkname</code> is used as the name of the chunk for error messages
6733 and debug information (see <a href="#4.9">&sect;4.9</a>).
6734 When absent,
6735 it defaults to <code>chunk</code>, if <code>chunk</code> is a string,
6736 or to "<code>=(load)</code>" otherwise.
6737
6738
6739 <p>
6740 The string <code>mode</code> controls whether the chunk can be text or binary
6741 (that is, a precompiled chunk).
6742 It may be the string "<code>b</code>" (only binary chunks),
6743 "<code>t</code>" (only text chunks),
6744 or "<code>bt</code>" (both binary and text).
6745 The default is "<code>bt</code>".
6746
6747
6748 <p>
6749 Lua does not check the consistency of binary chunks.
6750 Maliciously crafted binary chunks can crash
6751 the interpreter.
6752
6753
6754
6755
6756 <p>
6757 <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3>
6758
6759
6760 <p>
6761 Similar to <a href="#pdf-load"><code>load</code></a>,
6762 but gets the chunk from file <code>filename</code>
6763 or from the standard input,
6764 if no file name is given.
6765
6766
6767
6768
6769 <p>
6770 <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
6771
6772
6773 <p>
6774 Allows a program to traverse all fields of a table.
6775 Its first argument is a table and its second argument
6776 is an index in this table.
6777 <code>next</code> returns the next index of the table
6778 and its associated value.
6779 When called with <b>nil</b> as its second argument,
6780 <code>next</code> returns an initial index
6781 and its associated value.
6782 When called with the last index,
6783 or with <b>nil</b> in an empty table,
6784 <code>next</code> returns <b>nil</b>.
6785 If the second argument is absent, then it is interpreted as <b>nil</b>.
6786 In particular,
6787 you can use <code>next(t)</code> to check whether a table is empty.
6788
6789
6790 <p>
6791 The order in which the indices are enumerated is not specified,
6792 <em>even for numeric indices</em>.
6793 (To traverse a table in numeric order,
6794 use a numerical <b>for</b>.)
6795
6796
6797 <p>
6798 The behavior of <code>next</code> is undefined if,
6799 during the traversal,
6800 you assign any value to a non-existent field in the table.
6801 You may however modify existing fields.
6802 In particular, you may clear existing fields.
6803
6804
6805
6806
6807 <p>
6808 <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3>
6809
6810
6811 <p>
6812 If <code>t</code> has a metamethod <code>__pairs</code>,
6813 calls it with <code>t</code> as argument and returns the first three
6814 results from the call.
6815
6816
6817 <p>
6818 Otherwise,
6819 returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>,
6820 so that the construction
6821
6822 <pre>
6823 for k,v in pairs(t) do <em>body</em> end
6824 </pre><p>
6825 will iterate over all key&ndash;value pairs of table <code>t</code>.
6826
6827
6828 <p>
6829 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
6830 the table during its traversal.
6831
6832
6833
6834
6835 <p>
6836 <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, &middot;&middot;&middot;])</code></a></h3>
6837
6838
6839 <p>
6840 Calls function <code>f</code> with
6841 the given arguments in <em>protected mode</em>.
6842 This means that any error inside&nbsp;<code>f</code> is not propagated;
6843 instead, <code>pcall</code> catches the error
6844 and returns a status code.
6845 Its first result is the status code (a boolean),
6846 which is true if the call succeeds without errors.
6847 In such case, <code>pcall</code> also returns all results from the call,
6848 after this first result.
6849 In case of any error, <code>pcall</code> returns <b>false</b> plus the error message.
6850
6851
6852
6853
6854 <p>
6855 <hr><h3><a name="pdf-print"><code>print (&middot;&middot;&middot;)</code></a></h3>
6856 Receives any number of arguments
6857 and prints their values to <code>stdout</code>,
6858 using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string.
6859 <code>print</code> is not intended for formatted output,
6860 but only as a quick way to show a value,
6861 for instance for debugging.
6862 For complete control over the output,
6863 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
6864
6865
6866
6867
6868 <p>
6869 <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3>
6870 Checks whether <code>v1</code> is equal to <code>v2</code>,
6871 without invoking any metamethod.
6872 Returns a boolean.
6873
6874
6875
6876
6877 <p>
6878 <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3>
6879 Gets the real value of <code>table[index]</code>,
6880 without invoking any metamethod.
6881 <code>table</code> must be a table;
6882 <code>index</code> may be any value.
6883
6884
6885
6886
6887 <p>
6888 <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3>
6889 Returns the length of the object <code>v</code>,
6890 which must be a table or a string,
6891 without invoking any metamethod.
6892 Returns an integer.
6893
6894
6895
6896
6897 <p>
6898 <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
6899 Sets the real value of <code>table[index]</code> to <code>value</code>,
6900 without invoking any metamethod.
6901 <code>table</code> must be a table,
6902 <code>index</code> any value different from <b>nil</b> and NaN,
6903 and <code>value</code> any Lua value.
6904
6905
6906 <p>
6907 This function returns <code>table</code>.
6908
6909
6910
6911
6912 <p>
6913 <hr><h3><a name="pdf-select"><code>select (index, &middot;&middot;&middot;)</code></a></h3>
6914
6915
6916 <p>
6917 If <code>index</code> is a number,
6918 returns all arguments after argument number <code>index</code>;
6919 a negative number indexes from the end (-1 is the last argument).
6920 Otherwise, <code>index</code> must be the string <code>"#"</code>,
6921 and <code>select</code> returns the total number of extra arguments it received.
6922
6923
6924
6925
6926 <p>
6927 <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3>
6928
6929
6930 <p>
6931 Sets the metatable for the given table.
6932 (You cannot change the metatable of other types from Lua, only from&nbsp;C.)
6933 If <code>metatable</code> is <b>nil</b>,
6934 removes the metatable of the given table.
6935 If the original metatable has a <code>"__metatable"</code> field,
6936 raises an error.
6937
6938
6939 <p>
6940 This function returns <code>table</code>.
6941
6942
6943
6944
6945 <p>
6946 <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
6947
6948
6949 <p>
6950 When called with no <code>base</code>,
6951 <code>tonumber</code> tries to convert its argument to a number.
6952 If the argument is already a number or
6953 a string convertible to a number,
6954 then <code>tonumber</code> returns this number;
6955 otherwise, it returns <b>nil</b>.
6956
6957
6958 <p>
6959 The conversion of strings can result in integers or floats,
6960 according to the lexical conventions of Lua (see <a href="#3.1">&sect;3.1</a>).
6961 (The string may have leading and trailing spaces and a sign.)
6962
6963
6964 <p>
6965 When called with <code>base</code>,
6966 then <code>e</code> must be a string to be interpreted as
6967 an integer numeral in that base.
6968 The base may be any integer between 2 and 36, inclusive.
6969 In bases above&nbsp;10, the letter '<code>A</code>' (in either upper or lower case)
6970 represents&nbsp;10, '<code>B</code>' represents&nbsp;11, and so forth,
6971 with '<code>Z</code>' representing 35.
6972 If the string <code>e</code> is not a valid numeral in the given base,
6973 the function returns <b>nil</b>.
6974
6975
6976
6977
6978 <p>
6979 <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3>
6980 Receives a value of any type and
6981 converts it to a string in a human-readable format.
6982 Floats always produce strings with some
6983 floating-point indication (either a decimal dot or an exponent).
6984 (For complete control of how numbers are converted,
6985 use <a href="#pdf-string.format"><code>string.format</code></a>.)
6986
6987
6988 <p>
6989 If the metatable of <code>v</code> has a <code>"__tostring"</code> field,
6990 then <code>tostring</code> calls the corresponding value
6991 with <code>v</code> as argument,
6992 and uses the result of the call as its result.
6993
6994
6995
6996
6997 <p>
6998 <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3>
6999 Returns the type of its only argument, coded as a string.
7000 The possible results of this function are
7001 "<code>nil</code>" (a string, not the value <b>nil</b>),
7002 "<code>number</code>",
7003 "<code>string</code>",
7004 "<code>boolean</code>",
7005 "<code>table</code>",
7006 "<code>function</code>",
7007 "<code>thread</code>",
7008 and "<code>userdata</code>".
7009
7010
7011
7012
7013 <p>
7014 <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3>
7015 A global variable (not a function) that
7016 holds a string containing the current interpreter version.
7017 The current value of this variable is "<code>Lua 5.3</code>".
7018
7019
7020
7021
7022 <p>
7023 <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, &middot;&middot;&middot;])</code></a></h3>
7024
7025
7026 <p>
7027 This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
7028 except that it sets a new message handler <code>msgh</code>.
7029
7030
7031
7032
7033
7034
7035
7036 <h2>6.2 &ndash; <a name="6.2">Coroutine Manipulation</a></h2>
7037
7038 <p>
7039 The operations related to coroutines comprise a sub-library of
7040 the basic library and come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>.
7041 See <a href="#2.6">&sect;2.6</a> for a general description of coroutines.
7042
7043
7044 <p>
7045 <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3>
7046
7047
7048 <p>
7049 Creates a new coroutine, with body <code>f</code>.
7050 <code>f</code> must be a Lua function.
7051 Returns this new coroutine,
7052 an object with type <code>"thread"</code>.
7053
7054
7055
7056
7057 <p>
7058 <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3>
7059
7060
7061 <p>
7062 Returns true when the running coroutine can yield.
7063
7064
7065 <p>
7066 A running coroutine is yieldable if it is not the main thread and
7067 it is not inside a non-yieldable C function.
7068
7069
7070
7071
7072 <p>
7073 <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, &middot;&middot;&middot;])</code></a></h3>
7074
7075
7076 <p>
7077 Starts or continues the execution of coroutine <code>co</code>.
7078 The first time you resume a coroutine,
7079 it starts running its body.
7080 The values <code>val1</code>, ... are passed
7081 as the arguments to the body function.
7082 If the coroutine has yielded,
7083 <code>resume</code> restarts it;
7084 the values <code>val1</code>, ... are passed
7085 as the results from the yield.
7086
7087
7088 <p>
7089 If the coroutine runs without any errors,
7090 <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>
7091 (when the coroutine yields) or any values returned by the body function
7092 (when the coroutine terminates).
7093 If there is any error,
7094 <code>resume</code> returns <b>false</b> plus the error message.
7095
7096
7097
7098
7099 <p>
7100 <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3>
7101
7102
7103 <p>
7104 Returns the running coroutine plus a boolean,
7105 true when the running coroutine is the main one.
7106
7107
7108
7109
7110 <p>
7111 <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3>
7112
7113
7114 <p>
7115 Returns the status of coroutine <code>co</code>, as a string:
7116 <code>"running"</code>,
7117 if the coroutine is running (that is, it called <code>status</code>);
7118 <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>,
7119 or if it has not started running yet;
7120 <code>"normal"</code> if the coroutine is active but not running
7121 (that is, it has resumed another coroutine);
7122 and <code>"dead"</code> if the coroutine has finished its body function,
7123 or if it has stopped with an error.
7124
7125
7126
7127
7128 <p>
7129 <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3>
7130
7131
7132 <p>
7133 Creates a new coroutine, with body <code>f</code>.
7134 <code>f</code> must be a Lua function.
7135 Returns a function that resumes the coroutine each time it is called.
7136 Any arguments passed to the function behave as the
7137 extra arguments to <code>resume</code>.
7138 Returns the same values returned by <code>resume</code>,
7139 except the first boolean.
7140 In case of error, propagates the error.
7141
7142
7143
7144
7145 <p>
7146 <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (&middot;&middot;&middot;)</code></a></h3>
7147
7148
7149 <p>
7150 Suspends the execution of the calling coroutine.
7151 Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>.
7152
7153
7154
7155
7156
7157
7158
7159 <h2>6.3 &ndash; <a name="6.3">Modules</a></h2>
7160
7161 <p>
7162 The package library provides basic
7163 facilities for loading modules in Lua.
7164 It exports one function directly in the global environment:
7165 <a href="#pdf-require"><code>require</code></a>.
7166 Everything else is exported in a table <a name="pdf-package"><code>package</code></a>.
7167
7168
7169 <p>
7170 <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
7171
7172
7173 <p>
7174 Loads the given module.
7175 The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table
7176 to determine whether <code>modname</code> is already loaded.
7177 If it is, then <code>require</code> returns the value stored
7178 at <code>package.loaded[modname]</code>.
7179 Otherwise, it tries to find a <em>loader</em> for the module.
7180
7181
7182 <p>
7183 To find a loader,
7184 <code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</code></a> sequence.
7185 By changing this sequence,
7186 we can change how <code>require</code> looks for a module.
7187 The following explanation is based on the default configuration
7188 for <a href="#pdf-package.searchers"><code>package.searchers</code></a>.
7189
7190
7191 <p>
7192 First <code>require</code> queries <code>package.preload[modname]</code>.
7193 If it has a value,
7194 this value (which must be a function) is the loader.
7195 Otherwise <code>require</code> searches for a Lua loader using the
7196 path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
7197 If that also fails, it searches for a C&nbsp;loader using the
7198 path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
7199 If that also fails,
7200 it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>).
7201
7202
7203 <p>
7204 Once a loader is found,
7205 <code>require</code> calls the loader with two arguments:
7206 <code>modname</code> and an extra value dependent on how it got the loader.
7207 (If the loader came from a file,
7208 this extra value is the file name.)
7209 If the loader returns any non-nil value,
7210 <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
7211 If the loader does not return a non-nil value and
7212 has not assigned any value to <code>package.loaded[modname]</code>,
7213 then <code>require</code> assigns <b>true</b> to this entry.
7214 In any case, <code>require</code> returns the
7215 final value of <code>package.loaded[modname]</code>.
7216
7217
7218 <p>
7219 If there is any error loading or running the module,
7220 or if it cannot find any loader for the module,
7221 then <code>require</code> raises an error.
7222
7223
7224
7225
7226 <p>
7227 <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3>
7228
7229
7230 <p>
7231 A string describing some compile-time configurations for packages.
7232 This string is a sequence of lines:
7233
7234 <ul>
7235
7236 <li>The first line is the directory separator string.
7237 Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li>
7238
7239 <li>The second line is the character that separates templates in a path.
7240 Default is '<code>;</code>'.</li>
7241
7242 <li>The third line is the string that marks the
7243 substitution points in a template.
7244 Default is '<code>?</code>'.</li>
7245
7246 <li>The fourth line is a string that, in a path in Windows,
7247 is replaced by the executable's directory.
7248 Default is '<code>!</code>'.</li>
7249
7250 <li>The fifth line is a mark to ignore all text after it
7251 when building the <code>luaopen_</code> function name.
7252 Default is '<code>-</code>'.</li>
7253
7254 </ul>
7255
7256
7257
7258 <p>
7259 <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
7260
7261
7262 <p>
7263 The path used by <a href="#pdf-require"><code>require</code></a> to search for a C&nbsp;loader.
7264
7265
7266 <p>
7267 Lua initializes the C&nbsp;path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
7268 it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
7269 using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>
7270 or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>
7271 or a default path defined in <code>luaconf.h</code>.
7272
7273
7274
7275
7276 <p>
7277 <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
7278
7279
7280 <p>
7281 A table used by <a href="#pdf-require"><code>require</code></a> to control which
7282 modules are already loaded.
7283 When you require a module <code>modname</code> and
7284 <code>package.loaded[modname]</code> is not false,
7285 <a href="#pdf-require"><code>require</code></a> simply returns the value stored there.
7286
7287
7288 <p>
7289 This variable is only a reference to the real table;
7290 assignments to this variable do not change the
7291 table used by <a href="#pdf-require"><code>require</code></a>.
7292
7293
7294
7295
7296 <p>
7297 <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
7298
7299
7300 <p>
7301 Dynamically links the host program with the C&nbsp;library <code>libname</code>.
7302
7303
7304 <p>
7305 If <code>funcname</code> is "<code>*</code>",
7306 then it only links with the library,
7307 making the symbols exported by the library
7308 available to other dynamically linked libraries.
7309 Otherwise,
7310 it looks for a function <code>funcname</code> inside the library
7311 and returns this function as a C&nbsp;function.
7312 So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype
7313 (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
7314
7315
7316 <p>
7317 This is a low-level function.
7318 It completely bypasses the package and module system.
7319 Unlike <a href="#pdf-require"><code>require</code></a>,
7320 it does not perform any path searching and
7321 does not automatically adds extensions.
7322 <code>libname</code> must be the complete file name of the C&nbsp;library,
7323 including if necessary a path and an extension.
7324 <code>funcname</code> must be the exact name exported by the C&nbsp;library
7325 (which may depend on the C&nbsp;compiler and linker used).
7326
7327
7328 <p>
7329 This function is not supported by Standard&nbsp;C.
7330 As such, it is only available on some platforms
7331 (Windows, Linux, Mac OS X, Solaris, BSD,
7332 plus other Unix systems that support the <code>dlfcn</code> standard).
7333
7334
7335
7336
7337 <p>
7338 <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
7339
7340
7341 <p>
7342 The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader.
7343
7344
7345 <p>
7346 At start-up, Lua initializes this variable with
7347 the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
7348 the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
7349 with a default path defined in <code>luaconf.h</code>,
7350 if those environment variables are not defined.
7351 Any "<code>;;</code>" in the value of the environment variable
7352 is replaced by the default path.
7353
7354
7355
7356
7357 <p>
7358 <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
7359
7360
7361 <p>
7362 A table to store loaders for specific modules
7363 (see <a href="#pdf-require"><code>require</code></a>).
7364
7365
7366 <p>
7367 This variable is only a reference to the real table;
7368 assignments to this variable do not change the
7369 table used by <a href="#pdf-require"><code>require</code></a>.
7370
7371
7372
7373
7374 <p>
7375 <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3>
7376
7377
7378 <p>
7379 A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules.
7380
7381
7382 <p>
7383 Each entry in this table is a <em>searcher function</em>.
7384 When looking for a module,
7385 <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
7386 with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
7387 sole parameter.
7388 The function can return another function (the module <em>loader</em>)
7389 plus an extra value that will be passed to that loader,
7390 or a string explaining why it did not find that module
7391 (or <b>nil</b> if it has nothing to say).
7392
7393
7394 <p>
7395 Lua initializes this table with four searcher functions.
7396
7397
7398 <p>
7399 The first searcher simply looks for a loader in the
7400 <a href="#pdf-package.preload"><code>package.preload</code></a> table.
7401
7402
7403 <p>
7404 The second searcher looks for a loader as a Lua library,
7405 using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
7406 The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
7407
7408
7409 <p>
7410 The third searcher looks for a loader as a C&nbsp;library,
7411 using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
7412 Again,
7413 the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
7414 For instance,
7415 if the C&nbsp;path is the string
7416
7417 <pre>
7418 "./?.so;./?.dll;/usr/local/?/init.so"
7419 </pre><p>
7420 the searcher for module <code>foo</code>
7421 will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,
7422 and <code>/usr/local/foo/init.so</code>, in that order.
7423 Once it finds a C&nbsp;library,
7424 this searcher first uses a dynamic link facility to link the
7425 application with the library.
7426 Then it tries to find a C&nbsp;function inside the library to
7427 be used as the loader.
7428 The name of this C&nbsp;function is the string "<code>luaopen_</code>"
7429 concatenated with a copy of the module name where each dot
7430 is replaced by an underscore.
7431 Moreover, if the module name has a hyphen,
7432 its suffix after (and including) the first hyphen is removed.
7433 For instance, if the module name is <code>a.b.c-v2.1</code>,
7434 the function name will be <code>luaopen_a_b_c</code>.
7435
7436
7437 <p>
7438 The fourth searcher tries an <em>all-in-one loader</em>.
7439 It searches the C&nbsp;path for a library for
7440 the root name of the given module.
7441 For instance, when requiring <code>a.b.c</code>,
7442 it will search for a C&nbsp;library for <code>a</code>.
7443 If found, it looks into it for an open function for
7444 the submodule;
7445 in our example, that would be <code>luaopen_a_b_c</code>.
7446 With this facility, a package can pack several C&nbsp;submodules
7447 into one single library,
7448 with each submodule keeping its original open function.
7449
7450
7451 <p>
7452 All searchers except the first one (preload) return as the extra value
7453 the file name where the module was found,
7454 as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
7455 The first searcher returns no extra value.
7456
7457
7458
7459
7460 <p>
7461 <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3>
7462
7463
7464 <p>
7465 Searches for the given <code>name</code> in the given <code>path</code>.
7466
7467
7468 <p>
7469 A path is a string containing a sequence of
7470 <em>templates</em> separated by semicolons.
7471 For each template,
7472 the function replaces each interrogation mark (if any)
7473 in the template with a copy of <code>name</code>
7474 wherein all occurrences of <code>sep</code>
7475 (a dot, by default)
7476 were replaced by <code>rep</code>
7477 (the system's directory separator, by default),
7478 and then tries to open the resulting file name.
7479
7480
7481 <p>
7482 For instance, if the path is the string
7483
7484 <pre>
7485 "./?.lua;./?.lc;/usr/local/?/init.lua"
7486 </pre><p>
7487 the search for the name <code>foo.a</code>
7488 will try to open the files
7489 <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and
7490 <code>/usr/local/foo/a/init.lua</code>, in that order.
7491
7492
7493 <p>
7494 Returns the resulting name of the first file that it can
7495 open in read mode (after closing the file),
7496 or <b>nil</b> plus an error message if none succeeds.
7497 (This error message lists all file names it tried to open.)
7498
7499
7500
7501
7502
7503
7504
7505 <h2>6.4 &ndash; <a name="6.4">String Manipulation</a></h2>
7506
7507 <p>
7508 This library provides generic functions for string manipulation,
7509 such as finding and extracting substrings, and pattern matching.
7510 When indexing a string in Lua, the first character is at position&nbsp;1
7511 (not at&nbsp;0, as in C).
7512 Indices are allowed to be negative and are interpreted as indexing backwards,
7513 from the end of the string.
7514 Thus, the last character is at position -1, and so on.
7515
7516
7517 <p>
7518 The string library provides all its functions inside the table
7519 <a name="pdf-string"><code>string</code></a>.
7520 It also sets a metatable for strings
7521 where the <code>__index</code> field points to the <code>string</code> table.
7522 Therefore, you can use the string functions in object-oriented style.
7523 For instance, <code>string.byte(s,i)</code>
7524 can be written as <code>s:byte(i)</code>.
7525
7526
7527 <p>
7528 The string library assumes one-byte character encodings.
7529
7530
7531 <p>
7532 <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
7533 Returns the internal numerical codes of the characters <code>s[i]</code>,
7534 <code>s[i+1]</code>, ..., <code>s[j]</code>.
7535 The default value for <code>i</code> is&nbsp;1;
7536 the default value for <code>j</code> is&nbsp;<code>i</code>.
7537 These indices are corrected
7538 following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>.
7539
7540
7541 <p>
7542 Numerical codes are not necessarily portable across platforms.
7543
7544
7545
7546
7547 <p>
7548 <hr><h3><a name="pdf-string.char"><code>string.char (&middot;&middot;&middot;)</code></a></h3>
7549 Receives zero or more integers.
7550 Returns a string with length equal to the number of arguments,
7551 in which each character has the internal numerical code equal
7552 to its corresponding argument.
7553
7554
7555 <p>
7556 Numerical codes are not necessarily portable across platforms.
7557
7558
7559
7560
7561 <p>
7562 <hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3>
7563
7564
7565 <p>
7566 Returns a string containing a binary representation
7567 (a <em>binary chunk</em>)
7568 of the given function,
7569 so that a later <a href="#pdf-load"><code>load</code></a> on this string returns
7570 a copy of the function (but with new upvalues).
7571 If <code>strip</code> is a true value,
7572 the binary representation is created without debug information
7573 about the function
7574 (local variable names, lines, etc.).
7575
7576
7577 <p>
7578 Functions with upvalues have only their number of upvalues saved.
7579 When (re)loaded,
7580 those upvalues receive fresh instances containing <b>nil</b>.
7581 (You can use the debug library to serialize
7582 and reload the upvalues of a function
7583 in a way adequate to your needs.)
7584
7585
7586
7587
7588 <p>
7589 <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
7590
7591
7592 <p>
7593 Looks for the first match of
7594 <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
7595 If it finds a match, then <code>find</code> returns the indices of&nbsp;<code>s</code>
7596 where this occurrence starts and ends;
7597 otherwise, it returns <b>nil</b>.
7598 A third, optional numerical argument <code>init</code> specifies
7599 where to start the search;
7600 its default value is&nbsp;1 and can be negative.
7601 A value of <b>true</b> as a fourth, optional argument <code>plain</code>
7602 turns off the pattern matching facilities,
7603 so the function does a plain "find substring" operation,
7604 with no characters in <code>pattern</code> being considered magic.
7605 Note that if <code>plain</code> is given, then <code>init</code> must be given as well.
7606
7607
7608 <p>
7609 If the pattern has captures,
7610 then in a successful match
7611 the captured values are also returned,
7612 after the two indices.
7613
7614
7615
7616
7617 <p>
7618 <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, &middot;&middot;&middot;)</code></a></h3>
7619
7620
7621 <p>
7622 Returns a formatted version of its variable number of arguments
7623 following the description given in its first argument (which must be a string).
7624 The format string follows the same rules as the ISO&nbsp;C function <code>sprintf</code>.
7625 The only differences are that the options/modifiers
7626 <code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, <code>n</code>,
7627 and <code>p</code> are not supported
7628 and that there is an extra option, <code>q</code>.
7629 The <code>q</code> option formats a string between double quotes,
7630 using escape sequences when necessary to ensure that
7631 it can safely be read back by the Lua interpreter.
7632 For instance, the call
7633
7634 <pre>
7635 string.format('%q', 'a string with "quotes" and \n new line')
7636 </pre><p>
7637 may produce the string:
7638
7639 <pre>
7640 "a string with \"quotes\" and \
7641 new line"
7642 </pre>
7643
7644 <p>
7645 Options
7646 <code>A</code> and <code>a</code> (when available),
7647 <code>E</code>, <code>e</code>, <code>f</code>,
7648 <code>G</code>, and <code>g</code> all expect a number as argument.
7649 Options <code>c</code>, <code>d</code>,
7650 <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
7651 expect an integer.
7652 Option <code>q</code> expects a string;
7653 option <code>s</code> expects a string without embedded zeros.
7654 If the argument to option <code>s</code> is not a string,
7655 it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
7656
7657
7658
7659
7660 <p>
7661 <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
7662 Returns an iterator function that,
7663 each time it is called,
7664 returns the next captures from <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>)
7665 over the string <code>s</code>.
7666 If <code>pattern</code> specifies no captures,
7667 then the whole match is produced in each call.
7668
7669
7670 <p>
7671 As an example, the following loop
7672 will iterate over all the words from string <code>s</code>,
7673 printing one per line:
7674
7675 <pre>
7676 s = "hello world from Lua"
7677 for w in string.gmatch(s, "%a+") do
7678 print(w)
7679 end
7680 </pre><p>
7681 The next example collects all pairs <code>key=value</code> from the
7682 given string into a table:
7683
7684 <pre>
7685 t = {}
7686 s = "from=world, to=Lua"
7687 for k, v in string.gmatch(s, "(%w+)=(%w+)") do
7688 t[k] = v
7689 end
7690 </pre>
7691
7692 <p>
7693 For this function, a caret '<code>^</code>' at the start of a pattern does not
7694 work as an anchor, as this would prevent the iteration.
7695
7696
7697
7698
7699 <p>
7700 <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
7701 Returns a copy of <code>s</code>
7702 in which all (or the first <code>n</code>, if given)
7703 occurrences of the <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) have been
7704 replaced by a replacement string specified by <code>repl</code>,
7705 which can be a string, a table, or a function.
7706 <code>gsub</code> also returns, as its second value,
7707 the total number of matches that occurred.
7708 The name <code>gsub</code> comes from <em>Global SUBstitution</em>.
7709
7710
7711 <p>
7712 If <code>repl</code> is a string, then its value is used for replacement.
7713 The character&nbsp;<code>%</code> works as an escape character:
7714 any sequence in <code>repl</code> of the form <code>%<em>d</em></code>,
7715 with <em>d</em> between 1 and 9,
7716 stands for the value of the <em>d</em>-th captured substring.
7717 The sequence <code>%0</code> stands for the whole match.
7718 The sequence <code>%%</code> stands for a single&nbsp;<code>%</code>.
7719
7720
7721 <p>
7722 If <code>repl</code> is a table, then the table is queried for every match,
7723 using the first capture as the key.
7724
7725
7726 <p>
7727 If <code>repl</code> is a function, then this function is called every time a
7728 match occurs, with all captured substrings passed as arguments,
7729 in order.
7730
7731
7732 <p>
7733 In any case,
7734 if the pattern specifies no captures,
7735 then it behaves as if the whole pattern was inside a capture.
7736
7737
7738 <p>
7739 If the value returned by the table query or by the function call
7740 is a string or a number,
7741 then it is used as the replacement string;
7742 otherwise, if it is <b>false</b> or <b>nil</b>,
7743 then there is no replacement
7744 (that is, the original match is kept in the string).
7745
7746
7747 <p>
7748 Here are some examples:
7749
7750 <pre>
7751 x = string.gsub("hello world", "(%w+)", "%1 %1")
7752 --&gt; x="hello hello world world"
7753
7754 x = string.gsub("hello world", "%w+", "%0 %0", 1)
7755 --&gt; x="hello hello world"
7756
7757 x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
7758 --&gt; x="world hello Lua from"
7759
7760 x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
7761 --&gt; x="home = /home/roberto, user = roberto"
7762
7763 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
7764 return load(s)()
7765 end)
7766 --&gt; x="4+5 = 9"
7767
7768 local t = {name="lua", version="5.3"}
7769 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
7770 --&gt; x="lua-5.3.tar.gz"
7771 </pre>
7772
7773
7774
7775 <p>
7776 <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3>
7777 Receives a string and returns its length.
7778 The empty string <code>""</code> has length 0.
7779 Embedded zeros are counted,
7780 so <code>"a\000bc\000"</code> has length 5.
7781
7782
7783
7784
7785 <p>
7786 <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3>
7787 Receives a string and returns a copy of this string with all
7788 uppercase letters changed to lowercase.
7789 All other characters are left unchanged.
7790 The definition of what an uppercase letter is depends on the current locale.
7791
7792
7793
7794
7795 <p>
7796 <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
7797 Looks for the first <em>match</em> of
7798 <code>pattern</code> (see <a href="#6.4.1">&sect;6.4.1</a>) in the string <code>s</code>.
7799 If it finds one, then <code>match</code> returns
7800 the captures from the pattern;
7801 otherwise it returns <b>nil</b>.
7802 If <code>pattern</code> specifies no captures,
7803 then the whole match is returned.
7804 A third, optional numerical argument <code>init</code> specifies
7805 where to start the search;
7806 its default value is&nbsp;1 and can be negative.
7807
7808
7809
7810
7811 <p>
7812 <hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, &middot;&middot;&middot;)</code></a></h3>
7813
7814
7815 <p>
7816 Returns a binary string containing the values <code>v1</code>, <code>v2</code>, etc.
7817 packed (that is, serialized in binary form)
7818 according to the format string <code>fmt</code> (see <a href="#6.4.2">&sect;6.4.2</a>).
7819
7820
7821
7822
7823 <p>
7824 <hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3>
7825
7826
7827 <p>
7828 Returns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a>
7829 with the given format.
7830 The format string cannot have the variable-length options
7831 '<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">&sect;6.4.2</a>).
7832
7833
7834
7835
7836 <p>
7837 <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
7838 Returns a string that is the concatenation of <code>n</code> copies of
7839 the string <code>s</code> separated by the string <code>sep</code>.
7840 The default value for <code>sep</code> is the empty string
7841 (that is, no separator).
7842 Returns the empty string if <code>n</code> is not positive.
7843
7844
7845
7846
7847 <p>
7848 <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3>
7849 Returns a string that is the string <code>s</code> reversed.
7850
7851
7852
7853
7854 <p>
7855 <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3>
7856 Returns the substring of <code>s</code> that
7857 starts at <code>i</code> and continues until <code>j</code>;
7858 <code>i</code> and <code>j</code> can be negative.
7859 If <code>j</code> is absent, then it is assumed to be equal to -1
7860 (which is the same as the string length).
7861 In particular,
7862 the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
7863 with length <code>j</code>,
7864 and <code>string.sub(s, -i)</code> returns a suffix of <code>s</code>
7865 with length <code>i</code>.
7866
7867
7868 <p>
7869 If, after the translation of negative indices,
7870 <code>i</code> is less than 1,
7871 it is corrected to 1.
7872 If <code>j</code> is greater than the string length,
7873 it is corrected to that length.
7874 If, after these corrections,
7875 <code>i</code> is greater than <code>j</code>,
7876 the function returns the empty string.
7877
7878
7879
7880
7881 <p>
7882 <hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3>
7883
7884
7885 <p>
7886 Returns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>)
7887 according to the format string <code>fmt</code> (see <a href="#6.4.2">&sect;6.4.2</a>).
7888 An optional <code>pos</code> marks where
7889 to start reading in <code>s</code> (default is 1).
7890 After the read values,
7891 this function also returns the index of the first unread byte in <code>s</code>.
7892
7893
7894
7895
7896 <p>
7897 <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3>
7898 Receives a string and returns a copy of this string with all
7899 lowercase letters changed to uppercase.
7900 All other characters are left unchanged.
7901 The definition of what a lowercase letter is depends on the current locale.
7902
7903
7904
7905
7906
7907 <h3>6.4.1 &ndash; <a name="6.4.1">Patterns</a></h3>
7908
7909 <p>
7910 Patterns in Lua are described by regular strings,
7911 which are interpreted as patterns by the pattern-matching functions
7912 <a href="#pdf-string.find"><code>string.find</code></a>,
7913 <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>,
7914 <a href="#pdf-string.gsub"><code>string.gsub</code></a>,
7915 and <a href="#pdf-string.match"><code>string.match</code></a>.
7916 This section describes the syntax and the meaning
7917 (that is, what they match) of these strings.
7918
7919
7920
7921 <h4>Character Class:</h4><p>
7922 A <em>character class</em> is used to represent a set of characters.
7923 The following combinations are allowed in describing a character class:
7924
7925 <ul>
7926
7927 <li><b><em>x</em>: </b>
7928 (where <em>x</em> is not one of the <em>magic characters</em>
7929 <code>^$()%.[]*+-?</code>)
7930 represents the character <em>x</em> itself.
7931 </li>
7932
7933 <li><b><code>.</code>: </b> (a dot) represents all characters.</li>
7934
7935 <li><b><code>%a</code>: </b> represents all letters.</li>
7936
7937 <li><b><code>%c</code>: </b> represents all control characters.</li>
7938
7939 <li><b><code>%d</code>: </b> represents all digits.</li>
7940
7941 <li><b><code>%g</code>: </b> represents all printable characters except space.</li>
7942
7943 <li><b><code>%l</code>: </b> represents all lowercase letters.</li>
7944
7945 <li><b><code>%p</code>: </b> represents all punctuation characters.</li>
7946
7947 <li><b><code>%s</code>: </b> represents all space characters.</li>
7948
7949 <li><b><code>%u</code>: </b> represents all uppercase letters.</li>
7950
7951 <li><b><code>%w</code>: </b> represents all alphanumeric characters.</li>
7952
7953 <li><b><code>%x</code>: </b> represents all hexadecimal digits.</li>
7954
7955 <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character)
7956 represents the character <em>x</em>.
7957 This is the standard way to escape the magic characters.
7958 Any non-alphanumeric character
7959 (including all punctuations, even the non-magical)
7960 can be preceded by a '<code>%</code>'
7961 when used to represent itself in a pattern.
7962 </li>
7963
7964 <li><b><code>[<em>set</em>]</code>: </b>
7965 represents the class which is the union of all
7966 characters in <em>set</em>.
7967 A range of characters can be specified by
7968 separating the end characters of the range,
7969 in ascending order, with a '<code>-</code>'.
7970 All classes <code>%</code><em>x</em> described above can also be used as
7971 components in <em>set</em>.
7972 All other characters in <em>set</em> represent themselves.
7973 For example, <code>[%w_]</code> (or <code>[_%w]</code>)
7974 represents all alphanumeric characters plus the underscore,
7975 <code>[0-7]</code> represents the octal digits,
7976 and <code>[0-7%l%-]</code> represents the octal digits plus
7977 the lowercase letters plus the '<code>-</code>' character.
7978
7979
7980 <p>
7981 The interaction between ranges and classes is not defined.
7982 Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>
7983 have no meaning.
7984 </li>
7985
7986 <li><b><code>[^<em>set</em>]</code>: </b>
7987 represents the complement of <em>set</em>,
7988 where <em>set</em> is interpreted as above.
7989 </li>
7990
7991 </ul><p>
7992 For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),
7993 the corresponding uppercase letter represents the complement of the class.
7994 For instance, <code>%S</code> represents all non-space characters.
7995
7996
7997 <p>
7998 The definitions of letter, space, and other character groups
7999 depend on the current locale.
8000 In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.
8001
8002
8003
8004
8005
8006 <h4>Pattern Item:</h4><p>
8007 A <em>pattern item</em> can be
8008
8009 <ul>
8010
8011 <li>
8012 a single character class,
8013 which matches any single character in the class;
8014 </li>
8015
8016 <li>
8017 a single character class followed by '<code>*</code>',
8018 which matches zero or more repetitions of characters in the class.
8019 These repetition items will always match the longest possible sequence;
8020 </li>
8021
8022 <li>
8023 a single character class followed by '<code>+</code>',
8024 which matches one or more repetitions of characters in the class.
8025 These repetition items will always match the longest possible sequence;
8026 </li>
8027
8028 <li>
8029 a single character class followed by '<code>-</code>',
8030 which also matches zero or more repetitions of characters in the class.
8031 Unlike '<code>*</code>',
8032 these repetition items will always match the shortest possible sequence;
8033 </li>
8034
8035 <li>
8036 a single character class followed by '<code>?</code>',
8037 which matches zero or one occurrence of a character in the class.
8038 It always matches one occurrence if possible;
8039 </li>
8040
8041 <li>
8042 <code>%<em>n</em></code>, for <em>n</em> between 1 and 9;
8043 such item matches a substring equal to the <em>n</em>-th captured string
8044 (see below);
8045 </li>
8046
8047 <li>
8048 <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters;
8049 such item matches strings that start with&nbsp;<em>x</em>, end with&nbsp;<em>y</em>,
8050 and where the <em>x</em> and <em>y</em> are <em>balanced</em>.
8051 This means that, if one reads the string from left to right,
8052 counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,
8053 the ending <em>y</em> is the first <em>y</em> where the count reaches 0.
8054 For instance, the item <code>%b()</code> matches expressions with
8055 balanced parentheses.
8056 </li>
8057
8058 <li>
8059 <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>;
8060 such item matches an empty string at any position such that
8061 the next character belongs to <em>set</em>
8062 and the previous character does not belong to <em>set</em>.
8063 The set <em>set</em> is interpreted as previously described.
8064 The beginning and the end of the subject are handled as if
8065 they were the character '<code>\0</code>'.
8066 </li>
8067
8068 </ul>
8069
8070
8071
8072
8073 <h4>Pattern:</h4><p>
8074 A <em>pattern</em> is a sequence of pattern items.
8075 A caret '<code>^</code>' at the beginning of a pattern anchors the match at the
8076 beginning of the subject string.
8077 A '<code>$</code>' at the end of a pattern anchors the match at the
8078 end of the subject string.
8079 At other positions,
8080 '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves.
8081
8082
8083
8084
8085
8086 <h4>Captures:</h4><p>
8087 A pattern can contain sub-patterns enclosed in parentheses;
8088 they describe <em>captures</em>.
8089 When a match succeeds, the substrings of the subject string
8090 that match captures are stored (<em>captured</em>) for future use.
8091 Captures are numbered according to their left parentheses.
8092 For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>,
8093 the part of the string matching <code>"a*(.)%w(%s*)"</code> is
8094 stored as the first capture (and therefore has number&nbsp;1);
8095 the character matching "<code>.</code>" is captured with number&nbsp;2,
8096 and the part matching "<code>%s*</code>" has number&nbsp;3.
8097
8098
8099 <p>
8100 As a special case, the empty capture <code>()</code> captures
8101 the current string position (a number).
8102 For instance, if we apply the pattern <code>"()aa()"</code> on the
8103 string <code>"flaaap"</code>, there will be two captures: 3&nbsp;and&nbsp;5.
8104
8105
8106
8107
8108
8109
8110
8111 <h3>6.4.2 &ndash; <a name="6.4.2">Format Strings for Pack and Unpack</a></h3>
8112
8113 <p>
8114 The first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>,
8115 <a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a>
8116 is a format string,
8117 which describes the layout of the structure being created or read.
8118
8119
8120 <p>
8121 A format string is a sequence of conversion options.
8122 The conversion options are as follows:
8123
8124 <ul>
8125 <li><b><code>&lt;</code>: </b>sets little endian</li>
8126 <li><b><code>&gt;</code>: </b>sets big endian</li>
8127 <li><b><code>=</code>: </b>sets native endian</li>
8128 <li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code>
8129 (default is native alignment)</li>
8130 <li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li>
8131 <li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li>
8132 <li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li>
8133 <li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li>
8134 <li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li>
8135 <li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li>
8136 <li><b><code>j</code>: </b>a <code>lua_Integer</code></li>
8137 <li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li>
8138 <li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li>
8139 <li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes
8140 (default is native size)</li>
8141 <li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes
8142 (default is native size)</li>
8143 <li><b><code>f</code>: </b>a <code>float</code> (native size)</li>
8144 <li><b><code>d</code>: </b>a <code>double</code> (native size)</li>
8145 <li><b><code>n</code>: </b>a <code>lua_Number</code></li>
8146 <li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li>
8147 <li><b><code>z</code>: </b>a zero-terminated string</li>
8148 <li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length
8149 coded as an unsigned integer with <code>n</code> bytes
8150 (default is a <code>size_t</code>)</li>
8151 <li><b><code>x</code>: </b>one byte of padding</li>
8152 <li><b><code>X<em>op</em></code>: </b>an empty item that aligns
8153 according to option <code>op</code>
8154 (which is otherwise ignored)</li>
8155 <li><b>'<code> </code>': </b>(empty space) ignored</li>
8156 </ul><p>
8157 (A "<code>[<em>n</em>]</code>" means an optional integral numeral.)
8158 Except for padding, spaces, and configurations
8159 (options "<code>xX &lt;=&gt;!</code>"),
8160 each option corresponds to an argument (in <a href="#pdf-string.pack"><code>string.pack</code></a>)
8161 or a result (in <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
8162
8163
8164 <p>
8165 For options "<code>!<em>n</em></code>", "<code>s<em>n</em></code>", "<code>i<em>n</em></code>", and "<code>I<em>n</em></code>",
8166 <code>n</code> can be any integer between 1 and 16.
8167 All integral options check overflows;
8168 <a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size;
8169 <a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer.
8170
8171
8172 <p>
8173 Any format string starts as if prefixed by "<code>!1=</code>",
8174 that is,
8175 with maximum alignment of 1 (no alignment)
8176 and native endianness.
8177
8178
8179 <p>
8180 Alignment works as follows:
8181 For each option,
8182 the format gets extra padding until the data starts
8183 at an offset that is a multiple of the minimum between the
8184 option size and the maximum alignment;
8185 this minimum must be a power of 2.
8186 Options "<code>c</code>" and "<code>z</code>" are not aligned;
8187 option "<code>s</code>" follows the alignment of its starting integer.
8188
8189
8190 <p>
8191 All padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a>
8192 (and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
8193
8194
8195
8196
8197
8198
8199
8200 <h2>6.5 &ndash; <a name="6.5">UTF-8 Support</a></h2>
8201
8202 <p>
8203 This library provides basic support for UTF-8 encoding.
8204 It provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>.
8205 This library does not provide any support for Unicode other
8206 than the handling of the encoding.
8207 Any operation that needs the meaning of a character,
8208 such as character classification, is outside its scope.
8209
8210
8211 <p>
8212 Unless stated otherwise,
8213 all functions that expect a byte position as a parameter
8214 assume that the given position is either the start of a byte sequence
8215 or one plus the length of the subject string.
8216 As in the string library,
8217 negative indices count from the end of the string.
8218
8219
8220 <p>
8221 <hr><h3><a name="pdf-utf8.char"><code>utf8.char (&middot;&middot;&middot;)</code></a></h3>
8222 Receives zero or more integers,
8223 converts each one to its corresponding UTF-8 byte sequence
8224 and returns a string with the concatenation of all these sequences.
8225
8226
8227
8228
8229 <p>
8230 <hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3>
8231 The pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xF4][\x80-\xBF]*</code>"
8232 (see <a href="#6.4.1">&sect;6.4.1</a>),
8233 which matches exactly one UTF-8 byte sequence,
8234 assuming that the subject is a valid UTF-8 string.
8235
8236
8237
8238
8239 <p>
8240 <hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s)</code></a></h3>
8241
8242
8243 <p>
8244 Returns values so that the construction
8245
8246 <pre>
8247 for p, c in utf8.codes(s) do <em>body</em> end
8248 </pre><p>
8249 will iterate over all characters in string <code>s</code>,
8250 with <code>p</code> being the position (in bytes) and <code>c</code> the code point
8251 of each character.
8252 It raises an error if it meets any invalid byte sequence.
8253
8254
8255
8256
8257 <p>
8258 <hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j]])</code></a></h3>
8259 Returns the codepoints (as integers) from all characters in <code>s</code>
8260 that start between byte position <code>i</code> and <code>j</code> (both included).
8261 The default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>.
8262 It raises an error if it meets any invalid byte sequence.
8263
8264
8265
8266
8267 <p>
8268 <hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3>
8269 Returns the number of UTF-8 characters in string <code>s</code>
8270 that start between positions <code>i</code> and <code>j</code> (both inclusive).
8271 The default for <code>i</code> is 1 and for <code>j</code> is -1.
8272 If it finds any invalid byte sequence,
8273 returns a false value plus the position of the first invalid byte.
8274
8275
8276
8277
8278 <p>
8279 <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
8280 Returns the position (in bytes) where the encoding of the
8281 <code>n</code>-th character of <code>s</code>
8282 (counting from position <code>i</code>) starts.
8283 A negative <code>n</code> gets characters before position <code>i</code>.
8284 The default for <code>i</code> is 1 when <code>n</code> is non-negative
8285 and <code>#s + 1</code> otherwise,
8286 so that <code>utf8.offset(s, -n)</code> gets the offset of the
8287 <code>n</code>-th character from the end of the string.
8288 If the specified character is neither in the subject
8289 nor right after its end,
8290 the function returns <b>nil</b>.
8291
8292
8293 <p>
8294 As a special case,
8295 when <code>n</code> is 0 the function returns the start of the encoding
8296 of the character that contains the <code>i</code>-th byte of <code>s</code>.
8297
8298
8299 <p>
8300 This function assumes that <code>s</code> is a valid UTF-8 string.
8301
8302
8303
8304
8305
8306
8307
8308 <h2>6.6 &ndash; <a name="6.6">Table Manipulation</a></h2>
8309
8310 <p>
8311 This library provides generic functions for table manipulation.
8312 It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
8313
8314
8315 <p>
8316 Remember that, whenever an operation needs the length of a table,
8317 the table must be a proper sequence
8318 or have a <code>__len</code> metamethod (see <a href="#3.4.7">&sect;3.4.7</a>).
8319 All functions ignore non-numeric keys
8320 in the tables given as arguments.
8321
8322
8323 <p>
8324 <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
8325
8326
8327 <p>
8328 Given a list where all elements are strings or numbers,
8329 returns the string <code>list[i]..sep..list[i+1] &middot;&middot;&middot; sep..list[j]</code>.
8330 The default value for <code>sep</code> is the empty string,
8331 the default for <code>i</code> is 1,
8332 and the default for <code>j</code> is <code>#list</code>.
8333 If <code>i</code> is greater than <code>j</code>, returns the empty string.
8334
8335
8336
8337
8338 <p>
8339 <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
8340
8341
8342 <p>
8343 Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>,
8344 shifting up the elements
8345 <code>list[pos], list[pos+1], &middot;&middot;&middot;, list[#list]</code>.
8346 The default value for <code>pos</code> is <code>#list+1</code>,
8347 so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
8348 of list <code>t</code>.
8349
8350
8351
8352
8353 <p>
8354 <hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3>
8355
8356
8357 <p>
8358 Moves elements from table <code>a1</code> to table <code>a2</code>.
8359 This function performs the equivalent to the following
8360 multiple assignment:
8361 <code>a2[t],&middot;&middot;&middot; = a1[f],&middot;&middot;&middot;,a1[e]</code>.
8362 The default for <code>a2</code> is <code>a1</code>.
8363 The destination range can overlap with the source range.
8364 Index <code>f</code> must be positive.
8365
8366
8367
8368
8369 <p>
8370 <hr><h3><a name="pdf-table.pack"><code>table.pack (&middot;&middot;&middot;)</code></a></h3>
8371
8372
8373 <p>
8374 Returns a new table with all parameters stored into keys 1, 2, etc.
8375 and with a field "<code>n</code>" with the total number of parameters.
8376 Note that the resulting table may not be a sequence.
8377
8378
8379
8380
8381 <p>
8382 <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3>
8383
8384
8385 <p>
8386 Removes from <code>list</code> the element at position <code>pos</code>,
8387 returning the value of the removed element.
8388 When <code>pos</code> is an integer between 1 and <code>#list</code>,
8389 it shifts down the elements
8390 <code>list[pos+1], list[pos+2], &middot;&middot;&middot;, list[#list]</code>
8391 and erases element <code>list[#list]</code>;
8392 The index <code>pos</code> can also be 0 when <code>#list</code> is 0,
8393 or <code>#list + 1</code>;
8394 in those cases, the function erases the element <code>list[pos]</code>.
8395
8396
8397 <p>
8398 The default value for <code>pos</code> is <code>#list</code>,
8399 so that a call <code>table.remove(l)</code> removes the last element
8400 of list <code>l</code>.
8401
8402
8403
8404
8405 <p>
8406 <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3>
8407
8408
8409 <p>
8410 Sorts list elements in a given order, <em>in-place</em>,
8411 from <code>list[1]</code> to <code>list[#list]</code>.
8412 If <code>comp</code> is given,
8413 then it must be a function that receives two list elements
8414 and returns true when the first element must come
8415 before the second in the final order
8416 (so that <code>not comp(list[i+1],list[i])</code> will be true after the sort).
8417 If <code>comp</code> is not given,
8418 then the standard Lua operator <code>&lt;</code> is used instead.
8419
8420
8421 <p>
8422 The sort algorithm is not stable;
8423 that is, elements considered equal by the given order
8424 may have their relative positions changed by the sort.
8425
8426
8427
8428
8429 <p>
8430 <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3>
8431
8432
8433 <p>
8434 Returns the elements from the given list.
8435 This function is equivalent to
8436
8437 <pre>
8438 return list[i], list[i+1], &middot;&middot;&middot;, list[j]
8439 </pre><p>
8440 By default, <code>i</code> is&nbsp;1 and <code>j</code> is <code>#list</code>.
8441
8442
8443
8444
8445
8446
8447
8448 <h2>6.7 &ndash; <a name="6.7">Mathematical Functions</a></h2>
8449
8450 <p>
8451 This library provides basic mathematical functions.
8452 It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
8453 Functions with the annotation "<code>integer/float</code>" give
8454 integer results for integer arguments
8455 and float results for float (or mixed) arguments.
8456 Rounding functions
8457 (<a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a>)
8458 return an integer when the result fits in the range of an integer,
8459 or a float otherwise.
8460
8461
8462 <p>
8463 <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3>
8464
8465
8466 <p>
8467 Returns the absolute value of <code>x</code>. (integer/float)
8468
8469
8470
8471
8472 <p>
8473 <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3>
8474
8475
8476 <p>
8477 Returns the arc cosine of <code>x</code> (in radians).
8478
8479
8480
8481
8482 <p>
8483 <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3>
8484
8485
8486 <p>
8487 Returns the arc sine of <code>x</code> (in radians).
8488
8489
8490
8491
8492 <p>
8493 <hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3>
8494
8495
8496 <p>
8497
8498 Returns the arc tangent of <code>y/x</code> (in radians),
8499 but uses the signs of both parameters to find the
8500 quadrant of the result.
8501 (It also handles correctly the case of <code>x</code> being zero.)
8502
8503
8504 <p>
8505 The default value for <code>x</code> is 1,
8506 so that the call <code>math.atan(y)</code>
8507 returns the arc tangent of <code>y</code>.
8508
8509
8510
8511
8512 <p>
8513 <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
8514
8515
8516 <p>
8517 Returns the smallest integral value larger than or equal to <code>x</code>.
8518
8519
8520
8521
8522 <p>
8523 <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
8524
8525
8526 <p>
8527 Returns the cosine of <code>x</code> (assumed to be in radians).
8528
8529
8530
8531
8532 <p>
8533 <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3>
8534
8535
8536 <p>
8537 Converts the angle <code>x</code> from radians to degrees.
8538
8539
8540
8541
8542 <p>
8543 <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
8544
8545
8546 <p>
8547 Returns the value <em>e<sup>x</sup></em>
8548 (where <code>e</code> is the base of natural logarithms).
8549
8550
8551
8552
8553 <p>
8554 <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3>
8555
8556
8557 <p>
8558 Returns the largest integral value smaller than or equal to <code>x</code>.
8559
8560
8561
8562
8563 <p>
8564 <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3>
8565
8566
8567 <p>
8568 Returns the remainder of the division of <code>x</code> by <code>y</code>
8569 that rounds the quotient towards zero. (integer/float)
8570
8571
8572
8573
8574 <p>
8575 <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3>
8576
8577
8578 <p>
8579 The float value <code>HUGE_VAL</code>,
8580 a value larger than any other numerical value.
8581
8582
8583
8584
8585 <p>
8586 <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
8587
8588
8589 <p>
8590 Returns the logarithm of <code>x</code> in the given base.
8591 The default for <code>base</code> is <em>e</em>
8592 (so that the function returns the natural logarithm of <code>x</code>).
8593
8594
8595
8596
8597 <p>
8598 <hr><h3><a name="pdf-math.max"><code>math.max (x, &middot;&middot;&middot;)</code></a></h3>
8599
8600
8601 <p>
8602 Returns the argument with the maximum value,
8603 according to the Lua operator <code>&lt;</code>. (integer/float)
8604
8605
8606
8607
8608 <p>
8609 <hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3>
8610 An integer with the maximum value for an integer.
8611
8612
8613
8614
8615 <p>
8616 <hr><h3><a name="pdf-math.min"><code>math.min (x, &middot;&middot;&middot;)</code></a></h3>
8617
8618
8619 <p>
8620 Returns the argument with the minimum value,
8621 according to the Lua operator <code>&lt;</code>. (integer/float)
8622
8623
8624
8625
8626 <p>
8627 <hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
8628 An integer with the minimum value for an integer.
8629
8630
8631
8632
8633 <p>
8634 <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
8635
8636
8637 <p>
8638 Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
8639 Its second result is always a float.
8640
8641
8642
8643
8644 <p>
8645 <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
8646
8647
8648 <p>
8649 The value of <em>&pi;</em>.
8650
8651
8652
8653
8654 <p>
8655 <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3>
8656
8657
8658 <p>
8659 Converts the angle <code>x</code> from degrees to radians.
8660
8661
8662
8663
8664 <p>
8665 <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
8666
8667
8668 <p>
8669 When called without arguments,
8670 returns a pseudo-random float with uniform distribution
8671 in the range <em>[0,1)</em>.
8672 When called with two integers <code>m</code> and <code>n</code>,
8673 <code>math.random</code> returns a pseudo-random integer
8674 with uniform distribution in the range <em>[m, n]</em>.
8675 (The value <em>m-n</em> cannot be negative and must fit in a Lua integer.)
8676 The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>.
8677
8678
8679 <p>
8680 This function is an interface to the underling
8681 pseudo-random generator function provided by C.
8682 No guarantees can be given for its statistical properties.
8683
8684
8685
8686
8687 <p>
8688 <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3>
8689
8690
8691 <p>
8692 Sets <code>x</code> as the "seed"
8693 for the pseudo-random generator:
8694 equal seeds produce equal sequences of numbers.
8695
8696
8697
8698
8699 <p>
8700 <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3>
8701
8702
8703 <p>
8704 Returns the sine of <code>x</code> (assumed to be in radians).
8705
8706
8707
8708
8709 <p>
8710 <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
8711
8712
8713 <p>
8714 Returns the square root of <code>x</code>.
8715 (You can also use the expression <code>x^0.5</code> to compute this value.)
8716
8717
8718
8719
8720 <p>
8721 <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3>
8722
8723
8724 <p>
8725 Returns the tangent of <code>x</code> (assumed to be in radians).
8726
8727
8728
8729
8730 <p>
8731 <hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3>
8732
8733
8734 <p>
8735 If the value <code>x</code> is convertible to an integer,
8736 returns that integer.
8737 Otherwise, returns <b>nil</b>.
8738
8739
8740
8741
8742 <p>
8743 <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
8744
8745
8746 <p>
8747 Returns "<code>integer</code>" if <code>x</code> is an integer,
8748 "<code>float</code>" if it is a float,
8749 or <b>nil</b> if <code>x</code> is not a number.
8750
8751
8752
8753
8754 <p>
8755 <hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3>
8756
8757
8758 <p>
8759 Returns a boolean,
8760 true if integer <code>m</code> is below integer <code>n</code> when
8761 they are compared as unsigned integers.
8762
8763
8764
8765
8766
8767
8768
8769 <h2>6.8 &ndash; <a name="6.8">Input and Output Facilities</a></h2>
8770
8771 <p>
8772 The I/O library provides two different styles for file manipulation.
8773 The first one uses implicit file handles;
8774 that is, there are operations to set a default input file and a
8775 default output file,
8776 and all input/output operations are over these default files.
8777 The second style uses explicit file handles.
8778
8779
8780 <p>
8781 When using implicit file handles,
8782 all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
8783 When using explicit file handles,
8784 the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle
8785 and then all operations are supplied as methods of the file handle.
8786
8787
8788 <p>
8789 The table <code>io</code> also provides
8790 three predefined file handles with their usual meanings from C:
8791 <a name="pdf-io.stdin"><code>io.stdin</code></a>, <a name="pdf-io.stdout"><code>io.stdout</code></a>, and <a name="pdf-io.stderr"><code>io.stderr</code></a>.
8792 The I/O library never closes these files.
8793
8794
8795 <p>
8796 Unless otherwise stated,
8797 all I/O functions return <b>nil</b> on failure
8798 (plus an error message as a second result and
8799 a system-dependent error code as a third result)
8800 and some value different from <b>nil</b> on success.
8801 On non-POSIX systems,
8802 the computation of the error message and error code
8803 in case of errors
8804 may be not thread safe,
8805 because they rely on the global C variable <code>errno</code>.
8806
8807
8808 <p>
8809 <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
8810
8811
8812 <p>
8813 Equivalent to <code>file:close()</code>.
8814 Without a <code>file</code>, closes the default output file.
8815
8816
8817
8818
8819 <p>
8820 <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
8821
8822
8823 <p>
8824 Equivalent to <code>io.output():flush()</code>.
8825
8826
8827
8828
8829 <p>
8830 <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
8831
8832
8833 <p>
8834 When called with a file name, it opens the named file (in text mode),
8835 and sets its handle as the default input file.
8836 When called with a file handle,
8837 it simply sets this file handle as the default input file.
8838 When called without parameters,
8839 it returns the current default input file.
8840
8841
8842 <p>
8843 In case of errors this function raises the error,
8844 instead of returning an error code.
8845
8846
8847
8848
8849 <p>
8850 <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename &middot;&middot;&middot;])</code></a></h3>
8851
8852
8853 <p>
8854 Opens the given file name in read mode
8855 and returns an iterator function that
8856 works like <code>file:lines(&middot;&middot;&middot;)</code> over the opened file.
8857 When the iterator function detects the end of file,
8858 it returns no values (to finish the loop) and automatically closes the file.
8859
8860
8861 <p>
8862 The call <code>io.lines()</code> (with no file name) is equivalent
8863 to <code>io.input():lines("*l")</code>;
8864 that is, it iterates over the lines of the default input file.
8865 In this case it does not close the file when the loop ends.
8866
8867
8868 <p>
8869 In case of errors this function raises the error,
8870 instead of returning an error code.
8871
8872
8873
8874
8875 <p>
8876 <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
8877
8878
8879 <p>
8880 This function opens a file,
8881 in the mode specified in the string <code>mode</code>.
8882 It returns a new file handle,
8883 or, in case of errors, <b>nil</b> plus an error message.
8884
8885
8886 <p>
8887 The <code>mode</code> string can be any of the following:
8888
8889 <ul>
8890 <li><b>"<code>r</code>": </b> read mode (the default);</li>
8891 <li><b>"<code>w</code>": </b> write mode;</li>
8892 <li><b>"<code>a</code>": </b> append mode;</li>
8893 <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li>
8894 <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li>
8895 <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved,
8896 writing is only allowed at the end of file.</li>
8897 </ul><p>
8898 The <code>mode</code> string can also have a '<code>b</code>' at the end,
8899 which is needed in some systems to open the file in binary mode.
8900
8901
8902
8903
8904 <p>
8905 <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
8906
8907
8908 <p>
8909 Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file.
8910
8911
8912
8913
8914 <p>
8915 <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
8916
8917
8918 <p>
8919 This function is system dependent and is not available
8920 on all platforms.
8921
8922
8923 <p>
8924 Starts program <code>prog</code> in a separated process and returns
8925 a file handle that you can use to read data from this program
8926 (if <code>mode</code> is <code>"r"</code>, the default)
8927 or to write data to this program
8928 (if <code>mode</code> is <code>"w"</code>).
8929
8930
8931
8932
8933 <p>
8934 <hr><h3><a name="pdf-io.read"><code>io.read (&middot;&middot;&middot;)</code></a></h3>
8935
8936
8937 <p>
8938 Equivalent to <code>io.input():read(&middot;&middot;&middot;)</code>.
8939
8940
8941
8942
8943 <p>
8944 <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
8945
8946
8947 <p>
8948 Returns a handle for a temporary file.
8949 This file is opened in update mode
8950 and it is automatically removed when the program ends.
8951
8952
8953
8954
8955 <p>
8956 <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
8957
8958
8959 <p>
8960 Checks whether <code>obj</code> is a valid file handle.
8961 Returns the string <code>"file"</code> if <code>obj</code> is an open file handle,
8962 <code>"closed file"</code> if <code>obj</code> is a closed file handle,
8963 or <b>nil</b> if <code>obj</code> is not a file handle.
8964
8965
8966
8967
8968 <p>
8969 <hr><h3><a name="pdf-io.write"><code>io.write (&middot;&middot;&middot;)</code></a></h3>
8970
8971
8972 <p>
8973 Equivalent to <code>io.output():write(&middot;&middot;&middot;)</code>.
8974
8975
8976
8977
8978 <p>
8979 <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
8980
8981
8982 <p>
8983 Closes <code>file</code>.
8984 Note that files are automatically closed when
8985 their handles are garbage collected,
8986 but that takes an unpredictable amount of time to happen.
8987
8988
8989 <p>
8990 When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
8991 <a href="#pdf-file:close"><code>file:close</code></a> returns the same values
8992 returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
8993
8994
8995
8996
8997 <p>
8998 <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
8999
9000
9001 <p>
9002 Saves any written data to <code>file</code>.
9003
9004
9005
9006
9007 <p>
9008 <hr><h3><a name="pdf-file:lines"><code>file:lines (&middot;&middot;&middot;)</code></a></h3>
9009
9010
9011 <p>
9012 Returns an iterator function that,
9013 each time it is called,
9014 reads the file according to the given formats.
9015 When no format is given,
9016 uses "<code>l</code>" as a default.
9017 As an example, the construction
9018
9019 <pre>
9020 for c in file:lines(1) do <em>body</em> end
9021 </pre><p>
9022 will iterate over all characters of the file,
9023 starting at the current position.
9024 Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
9025 when the loop ends.
9026
9027
9028 <p>
9029 In case of errors this function raises the error,
9030 instead of returning an error code.
9031
9032
9033
9034
9035 <p>
9036 <hr><h3><a name="pdf-file:read"><code>file:read (&middot;&middot;&middot;)</code></a></h3>
9037
9038
9039 <p>
9040 Reads the file <code>file</code>,
9041 according to the given formats, which specify what to read.
9042 For each format,
9043 the function returns a string or a number with the characters read,
9044 or <b>nil</b> if it cannot read data with the specified format.
9045 (In this latter case,
9046 the function does not read subsequent formats.)
9047 When called without formats,
9048 it uses a default format that reads the next line
9049 (see below).
9050
9051
9052 <p>
9053 The available formats are
9054
9055 <ul>
9056
9057 <li><b>"<code>n</code>": </b>
9058 reads a numeral and returns it as a float or an integer,
9059 following the lexical conventions of Lua.
9060 (The numeral may have leading spaces and a sign.)
9061 This format always reads the longest input sequence that
9062 is a valid prefix for a number;
9063 if that prefix does not form a valid number
9064 (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
9065 it is discarded and the function returns <b>nil</b>.
9066 </li>
9067
9068 <li><b>"<code>i</code>": </b>
9069 reads an integral number and returns it as an integer.
9070 </li>
9071
9072 <li><b>"<code>a</code>": </b>
9073 reads the whole file, starting at the current position.
9074 On end of file, it returns the empty string.
9075 </li>
9076
9077 <li><b>"<code>l</code>": </b>
9078 reads the next line skipping the end of line,
9079 returning <b>nil</b> on end of file.
9080 This is the default format.
9081 </li>
9082
9083 <li><b>"<code>L</code>": </b>
9084 reads the next line keeping the end-of-line character (if present),
9085 returning <b>nil</b> on end of file.
9086 </li>
9087
9088 <li><b><em>number</em>: </b>
9089 reads a string with up to this number of bytes,
9090 returning <b>nil</b> on end of file.
9091 If <code>number</code> is zero,
9092 it reads nothing and returns an empty string,
9093 or <b>nil</b> on end of file.
9094 </li>
9095
9096 </ul><p>
9097 The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
9098
9099
9100
9101
9102 <p>
9103 <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3>
9104
9105
9106 <p>
9107 Sets and gets the file position,
9108 measured from the beginning of the file,
9109 to the position given by <code>offset</code> plus a base
9110 specified by the string <code>whence</code>, as follows:
9111
9112 <ul>
9113 <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li>
9114 <li><b>"<code>cur</code>": </b> base is current position;</li>
9115 <li><b>"<code>end</code>": </b> base is end of file;</li>
9116 </ul><p>
9117 In case of success, <code>seek</code> returns the final file position,
9118 measured in bytes from the beginning of the file.
9119 If <code>seek</code> fails, it returns <b>nil</b>,
9120 plus a string describing the error.
9121
9122
9123 <p>
9124 The default value for <code>whence</code> is <code>"cur"</code>,
9125 and for <code>offset</code> is 0.
9126 Therefore, the call <code>file:seek()</code> returns the current
9127 file position, without changing it;
9128 the call <code>file:seek("set")</code> sets the position to the
9129 beginning of the file (and returns 0);
9130 and the call <code>file:seek("end")</code> sets the position to the
9131 end of the file, and returns its size.
9132
9133
9134
9135
9136 <p>
9137 <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
9138
9139
9140 <p>
9141 Sets the buffering mode for an output file.
9142 There are three available modes:
9143
9144 <ul>
9145
9146 <li><b>"<code>no</code>": </b>
9147 no buffering; the result of any output operation appears immediately.
9148 </li>
9149
9150 <li><b>"<code>full</code>": </b>
9151 full buffering; output operation is performed only
9152 when the buffer is full or when
9153 you explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>).
9154 </li>
9155
9156 <li><b>"<code>line</code>": </b>
9157 line buffering; output is buffered until a newline is output
9158 or there is any input from some special files
9159 (such as a terminal device).
9160 </li>
9161
9162 </ul><p>
9163 For the last two cases, <code>size</code>
9164 specifies the size of the buffer, in bytes.
9165 The default is an appropriate size.
9166
9167
9168
9169
9170 <p>
9171 <hr><h3><a name="pdf-file:write"><code>file:write (&middot;&middot;&middot;)</code></a></h3>
9172
9173
9174 <p>
9175 Writes the value of each of its arguments to <code>file</code>.
9176 The arguments must be strings or numbers.
9177
9178
9179 <p>
9180 In case of success, this function returns <code>file</code>.
9181 Otherwise it returns <b>nil</b> plus a string describing the error.
9182
9183
9184
9185
9186
9187
9188
9189 <h2>6.9 &ndash; <a name="6.9">Operating System Facilities</a></h2>
9190
9191 <p>
9192 This library is implemented through table <a name="pdf-os"><code>os</code></a>.
9193
9194
9195 <p>
9196 <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
9197
9198
9199 <p>
9200 Returns an approximation of the amount in seconds of CPU time
9201 used by the program.
9202
9203
9204
9205
9206 <p>
9207 <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
9208
9209
9210 <p>
9211 Returns a string or a table containing date and time,
9212 formatted according to the given string <code>format</code>.
9213
9214
9215 <p>
9216 If the <code>time</code> argument is present,
9217 this is the time to be formatted
9218 (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
9219 Otherwise, <code>date</code> formats the current time.
9220
9221
9222 <p>
9223 If <code>format</code> starts with '<code>!</code>',
9224 then the date is formatted in Coordinated Universal Time.
9225 After this optional character,
9226 if <code>format</code> is the string "<code>*t</code>",
9227 then <code>date</code> returns a table with the following fields:
9228 <code>year</code> (four digits), <code>month</code> (1&ndash;12), <code>day</code> (1&ndash;31),
9229 <code>hour</code> (0&ndash;23), <code>min</code> (0&ndash;59), <code>sec</code> (0&ndash;61),
9230 <code>wday</code> (weekday, Sunday is&nbsp;1),
9231 <code>yday</code> (day of the year),
9232 and <code>isdst</code> (daylight saving flag, a boolean).
9233 This last field may be absent
9234 if the information is not available.
9235
9236
9237 <p>
9238 If <code>format</code> is not "<code>*t</code>",
9239 then <code>date</code> returns the date as a string,
9240 formatted according to the same rules as the ISO&nbsp;C function <code>strftime</code>.
9241
9242
9243 <p>
9244 When called without arguments,
9245 <code>date</code> returns a reasonable date and time representation that depends on
9246 the host system and on the current locale
9247 (that is, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>).
9248
9249
9250 <p>
9251 On non-POSIX systems,
9252 this function may be not thread safe
9253 because of its reliance on C&nbsp;function <code>gmtime</code> and C&nbsp;function <code>localtime</code>.
9254
9255
9256
9257
9258 <p>
9259 <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
9260
9261
9262 <p>
9263 Returns the difference, in seconds,
9264 from time <code>t1</code> to time <code>t2</code>
9265 (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>).
9266 In POSIX, Windows, and some other systems,
9267 this value is exactly <code>t2</code><em>-</em><code>t1</code>.
9268
9269
9270
9271
9272 <p>
9273 <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
9274
9275
9276 <p>
9277 This function is equivalent to the ISO&nbsp;C function <code>system</code>.
9278 It passes <code>command</code> to be executed by an operating system shell.
9279 Its first result is <b>true</b>
9280 if the command terminated successfully,
9281 or <b>nil</b> otherwise.
9282 After this first result
9283 the function returns a string plus a number,
9284 as follows:
9285
9286 <ul>
9287
9288 <li><b>"<code>exit</code>": </b>
9289 the command terminated normally;
9290 the following number is the exit status of the command.
9291 </li>
9292
9293 <li><b>"<code>signal</code>": </b>
9294 the command was terminated by a signal;
9295 the following number is the signal that terminated the command.
9296 </li>
9297
9298 </ul>
9299
9300 <p>
9301 When called without a <code>command</code>,
9302 <code>os.execute</code> returns a boolean that is true if a shell is available.
9303
9304
9305
9306
9307 <p>
9308 <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
9309
9310
9311 <p>
9312 Calls the ISO&nbsp;C function <code>exit</code> to terminate the host program.
9313 If <code>code</code> is <b>true</b>,
9314 the returned status is <code>EXIT_SUCCESS</code>;
9315 if <code>code</code> is <b>false</b>,
9316 the returned status is <code>EXIT_FAILURE</code>;
9317 if <code>code</code> is a number,
9318 the returned status is this number.
9319 The default value for <code>code</code> is <b>true</b>.
9320
9321
9322 <p>
9323 If the optional second argument <code>close</code> is true,
9324 closes the Lua state before exiting.
9325
9326
9327
9328
9329 <p>
9330 <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
9331
9332
9333 <p>
9334 Returns the value of the process environment variable <code>varname</code>,
9335 or <b>nil</b> if the variable is not defined.
9336
9337
9338
9339
9340 <p>
9341 <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
9342
9343
9344 <p>
9345 Deletes the file (or empty directory, on POSIX systems)
9346 with the given name.
9347 If this function fails, it returns <b>nil</b>,
9348 plus a string describing the error and the error code.
9349
9350
9351
9352
9353 <p>
9354 <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
9355
9356
9357 <p>
9358 Renames file or directory named <code>oldname</code> to <code>newname</code>.
9359 If this function fails, it returns <b>nil</b>,
9360 plus a string describing the error and the error code.
9361
9362
9363
9364
9365 <p>
9366 <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
9367
9368
9369 <p>
9370 Sets the current locale of the program.
9371 <code>locale</code> is a system-dependent string specifying a locale;
9372 <code>category</code> is an optional string describing which category to change:
9373 <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
9374 <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
9375 the default category is <code>"all"</code>.
9376 The function returns the name of the new locale,
9377 or <b>nil</b> if the request cannot be honored.
9378
9379
9380 <p>
9381 If <code>locale</code> is the empty string,
9382 the current locale is set to an implementation-defined native locale.
9383 If <code>locale</code> is the string "<code>C</code>",
9384 the current locale is set to the standard C locale.
9385
9386
9387 <p>
9388 When called with <b>nil</b> as the first argument,
9389 this function only returns the name of the current locale
9390 for the given category.
9391
9392
9393 <p>
9394 This function may be not thread safe
9395 because of its reliance on C&nbsp;function <code>setlocale</code>.
9396
9397
9398
9399
9400 <p>
9401 <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
9402
9403
9404 <p>
9405 Returns the current time when called without arguments,
9406 or a time representing the date and time specified by the given table.
9407 This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
9408 and may have fields
9409 <code>hour</code> (default is 12),
9410 <code>min</code> (default is 0),
9411 <code>sec</code> (default is 0),
9412 and <code>isdst</code> (default is <b>nil</b>).
9413 For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
9414
9415
9416 <p>
9417 The returned value is a number, whose meaning depends on your system.
9418 In POSIX, Windows, and some other systems,
9419 this number counts the number
9420 of seconds since some given start time (the "epoch").
9421 In other systems, the meaning is not specified,
9422 and the number returned by <code>time</code> can be used only as an argument to
9423 <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>.
9424
9425
9426
9427
9428 <p>
9429 <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
9430
9431
9432 <p>
9433 Returns a string with a file name that can
9434 be used for a temporary file.
9435 The file must be explicitly opened before its use
9436 and explicitly removed when no longer needed.
9437
9438
9439 <p>
9440 On POSIX systems,
9441 this function also creates a file with that name,
9442 to avoid security risks.
9443 (Someone else might create the file with wrong permissions
9444 in the time between getting the name and creating the file.)
9445 You still have to open the file to use it
9446 and to remove it (even if you do not use it).
9447
9448
9449 <p>
9450 When possible,
9451 you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
9452 which automatically removes the file when the program ends.
9453
9454
9455
9456
9457
9458
9459
9460 <h2>6.10 &ndash; <a name="6.10">The Debug Library</a></h2>
9461
9462 <p>
9463 This library provides
9464 the functionality of the debug interface (<a href="#4.9">&sect;4.9</a>) to Lua programs.
9465 You should exert care when using this library.
9466 Several of its functions
9467 violate basic assumptions about Lua code
9468 (e.g., that variables local to a function
9469 cannot be accessed from outside;
9470 that userdata metatables cannot be changed by Lua code;
9471 that Lua programs do not crash)
9472 and therefore can compromise otherwise secure code.
9473 Moreover, some functions in this library may be slow.
9474
9475
9476 <p>
9477 All functions in this library are provided
9478 inside the <a name="pdf-debug"><code>debug</code></a> table.
9479 All functions that operate over a thread
9480 have an optional first argument which is the
9481 thread to operate over.
9482 The default is always the current thread.
9483
9484
9485 <p>
9486 <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
9487
9488
9489 <p>
9490 Enters an interactive mode with the user,
9491 running each string that the user enters.
9492 Using simple commands and other debug facilities,
9493 the user can inspect global and local variables,
9494 change their values, evaluate expressions, and so on.
9495 A line containing only the word <code>cont</code> finishes this function,
9496 so that the caller continues its execution.
9497
9498
9499 <p>
9500 Note that commands for <code>debug.debug</code> are not lexically nested
9501 within any function and so have no direct access to local variables.
9502
9503
9504
9505
9506 <p>
9507 <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
9508
9509
9510 <p>
9511 Returns the current hook settings of the thread, as three values:
9512 the current hook function, the current hook mask,
9513 and the current hook count
9514 (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function).
9515
9516
9517
9518
9519 <p>
9520 <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
9521
9522
9523 <p>
9524 Returns a table with information about a function.
9525 You can give the function directly
9526 or you can give a number as the value of <code>f</code>,
9527 which means the function running at level <code>f</code> of the call stack
9528 of the given thread:
9529 level&nbsp;0 is the current function (<code>getinfo</code> itself);
9530 level&nbsp;1 is the function that called <code>getinfo</code>
9531 (except for tail calls, which do not count on the stack);
9532 and so on.
9533 If <code>f</code> is a number larger than the number of active functions,
9534 then <code>getinfo</code> returns <b>nil</b>.
9535
9536
9537 <p>
9538 The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>,
9539 with the string <code>what</code> describing which fields to fill in.
9540 The default for <code>what</code> is to get all information available,
9541 except the table of valid lines.
9542 If present,
9543 the option '<code>f</code>'
9544 adds a field named <code>func</code> with the function itself.
9545 If present,
9546 the option '<code>L</code>'
9547 adds a field named <code>activelines</code> with the table of
9548 valid lines.
9549
9550
9551 <p>
9552 For instance, the expression <code>debug.getinfo(1,"n").name</code> returns
9553 a table with a name for the current function,
9554 if a reasonable name can be found,
9555 and the expression <code>debug.getinfo(print)</code>
9556 returns a table with all available information
9557 about the <a href="#pdf-print"><code>print</code></a> function.
9558
9559
9560
9561
9562 <p>
9563 <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3>
9564
9565
9566 <p>
9567 This function returns the name and the value of the local variable
9568 with index <code>local</code> of the function at level <code>f</code> of the stack.
9569 This function accesses not only explicit local variables,
9570 but also parameters, temporaries, etc.
9571
9572
9573 <p>
9574 The first parameter or local variable has index&nbsp;1, and so on,
9575 following the order that they are declared in the code,
9576 counting only the variables that are active
9577 in the current scope of the function.
9578 Negative indices refer to vararg parameters;
9579 -1 is the first vararg parameter.
9580 The function returns <b>nil</b> if there is no variable with the given index,
9581 and raises an error when called with a level out of range.
9582 (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
9583
9584
9585 <p>
9586 Variable names starting with '<code>(</code>' (open parenthesis)
9587 represent variables with no known names
9588 (internal variables such as loop control variables,
9589 and variables from chunks saved without debug information).
9590
9591
9592 <p>
9593 The parameter <code>f</code> may also be a function.
9594 In that case, <code>getlocal</code> returns only the name of function parameters.
9595
9596
9597
9598
9599 <p>
9600 <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3>
9601
9602
9603 <p>
9604 Returns the metatable of the given <code>value</code>
9605 or <b>nil</b> if it does not have a metatable.
9606
9607
9608
9609
9610 <p>
9611 <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
9612
9613
9614 <p>
9615 Returns the registry table (see <a href="#4.5">&sect;4.5</a>).
9616
9617
9618
9619
9620 <p>
9621 <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3>
9622
9623
9624 <p>
9625 This function returns the name and the value of the upvalue
9626 with index <code>up</code> of the function <code>f</code>.
9627 The function returns <b>nil</b> if there is no upvalue with the given index.
9628
9629
9630 <p>
9631 Variable names starting with '<code>(</code>' (open parenthesis)
9632 represent variables with no known names
9633 (variables from chunks saved without debug information).
9634
9635
9636
9637
9638 <p>
9639 <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3>
9640
9641
9642 <p>
9643 Returns the Lua value associated to <code>u</code>.
9644 If <code>u</code> is not a userdata,
9645 returns <b>nil</b>.
9646
9647
9648
9649
9650 <p>
9651 <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
9652
9653
9654 <p>
9655 Sets the given function as a hook.
9656 The string <code>mask</code> and the number <code>count</code> describe
9657 when the hook will be called.
9658 The string mask may have any combination of the following characters,
9659 with the given meaning:
9660
9661 <ul>
9662 <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li>
9663 <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li>
9664 <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li>
9665 </ul><p>
9666 Moreover,
9667 with a <code>count</code> different from zero,
9668 the hook is called also after every <code>count</code> instructions.
9669
9670
9671 <p>
9672 When called without arguments,
9673 <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
9674
9675
9676 <p>
9677 When the hook is called, its first parameter is a string
9678 describing the event that has triggered its call:
9679 <code>"call"</code> (or <code>"tail call"</code>),
9680 <code>"return"</code>,
9681 <code>"line"</code>, and <code>"count"</code>.
9682 For line events,
9683 the hook also gets the new line number as its second parameter.
9684 Inside a hook,
9685 you can call <code>getinfo</code> with level&nbsp;2 to get more information about
9686 the running function
9687 (level&nbsp;0 is the <code>getinfo</code> function,
9688 and level&nbsp;1 is the hook function).
9689
9690
9691
9692
9693 <p>
9694 <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>
9695
9696
9697 <p>
9698 This function assigns the value <code>value</code> to the local variable
9699 with index <code>local</code> of the function at level <code>level</code> of the stack.
9700 The function returns <b>nil</b> if there is no local
9701 variable with the given index,
9702 and raises an error when called with a <code>level</code> out of range.
9703 (You can call <code>getinfo</code> to check whether the level is valid.)
9704 Otherwise, it returns the name of the local variable.
9705
9706
9707 <p>
9708 See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
9709 variable indices and names.
9710
9711
9712
9713
9714 <p>
9715 <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
9716
9717
9718 <p>
9719 Sets the metatable for the given <code>value</code> to the given <code>table</code>
9720 (which can be <b>nil</b>).
9721 Returns <code>value</code>.
9722
9723
9724
9725
9726 <p>
9727 <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
9728
9729
9730 <p>
9731 This function assigns the value <code>value</code> to the upvalue
9732 with index <code>up</code> of the function <code>f</code>.
9733 The function returns <b>nil</b> if there is no upvalue
9734 with the given index.
9735 Otherwise, it returns the name of the upvalue.
9736
9737
9738
9739
9740 <p>
9741 <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3>
9742
9743
9744 <p>
9745 Sets the given <code>value</code> as
9746 the Lua value associated to the given <code>udata</code>.
9747 <code>udata</code> must be a full userdata.
9748
9749
9750 <p>
9751 Returns <code>udata</code>.
9752
9753
9754
9755
9756 <p>
9757 <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
9758
9759
9760 <p>
9761 If <code>message</code> is present but is neither a string nor <b>nil</b>,
9762 this function returns <code>message</code> without further processing.
9763 Otherwise,
9764 it returns a string with a traceback of the call stack.
9765 The optional <code>message</code> string is appended
9766 at the beginning of the traceback.
9767 An optional <code>level</code> number tells at which level
9768 to start the traceback
9769 (default is 1, the function calling <code>traceback</code>).
9770
9771
9772
9773
9774 <p>
9775 <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3>
9776
9777
9778 <p>
9779 Returns a unique identifier (as a light userdata)
9780 for the upvalue numbered <code>n</code>
9781 from the given function.
9782
9783
9784 <p>
9785 These unique identifiers allow a program to check whether different
9786 closures share upvalues.
9787 Lua closures that share an upvalue
9788 (that is, that access a same external local variable)
9789 will return identical ids for those upvalue indices.
9790
9791
9792
9793
9794 <p>
9795 <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
9796
9797
9798 <p>
9799 Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
9800 refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
9801
9802
9803
9804
9805
9806
9807
9808 <h1>7 &ndash; <a name="7">Lua Standalone</a></h1>
9809
9810 <p>
9811 Although Lua has been designed as an extension language,
9812 to be embedded in a host C&nbsp;program,
9813 it is also frequently used as a standalone language.
9814 An interpreter for Lua as a standalone language,
9815 called simply <code>lua</code>,
9816 is provided with the standard distribution.
9817 The standalone interpreter includes
9818 all standard libraries, including the debug library.
9819 Its usage is:
9820
9821 <pre>
9822 lua [options] [script [args]]
9823 </pre><p>
9824 The options are:
9825
9826 <ul>
9827 <li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li>
9828 <li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em>;</li>
9829 <li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li>
9830 <li><b><code>-v</code>: </b> prints version information;</li>
9831 <li><b><code>-E</code>: </b> ignores environment variables;</li>
9832 <li><b><code>--</code>: </b> stops handling options;</li>
9833 <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li>
9834 </ul><p>
9835 After handling its options, <code>lua</code> runs the given <em>script</em>.
9836 When called without arguments,
9837 <code>lua</code> behaves as <code>lua -v -i</code>
9838 when the standard input (<code>stdin</code>) is a terminal,
9839 and as <code>lua -</code> otherwise.
9840
9841
9842 <p>
9843 When called without option <code>-E</code>,
9844 the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
9845 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
9846 before running any argument.
9847 If the variable content has the format <code>@<em>filename</em></code>,
9848 then <code>lua</code> executes the file.
9849 Otherwise, <code>lua</code> executes the string itself.
9850
9851
9852 <p>
9853 When called with option <code>-E</code>,
9854 besides ignoring <code>LUA_INIT</code>,
9855 Lua also ignores
9856 the values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>,
9857 setting the values of
9858 <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a>
9859 with the default paths defined in <code>luaconf.h</code>.
9860
9861
9862 <p>
9863 All options are handled in order, except <code>-i</code> and <code>-E</code>.
9864 For instance, an invocation like
9865
9866 <pre>
9867 $ lua -e'a=1' -e 'print(a)' script.lua
9868 </pre><p>
9869 will first set <code>a</code> to 1, then print the value of <code>a</code>,
9870 and finally run the file <code>script.lua</code> with no arguments.
9871 (Here <code>$</code> is the shell prompt. Your prompt may be different.)
9872
9873
9874 <p>
9875 Before running any code,
9876 <code>lua</code> collects all command-line arguments
9877 in a global table called <code>arg</code>.
9878 The script name goes to index 0,
9879 the first argument after the script name goes to index 1,
9880 and so on.
9881 Any arguments before the script name
9882 (that is, the interpreter name plus its options)
9883 go to negative indices.
9884 For instance, in the call
9885
9886 <pre>
9887 $ lua -la b.lua t1 t2
9888 </pre><p>
9889 the table is like this:
9890
9891 <pre>
9892 arg = { [-2] = "lua", [-1] = "-la",
9893 [0] = "b.lua",
9894 [1] = "t1", [2] = "t2" }
9895 </pre><p>
9896 If there is no script in the call,
9897 the interpreter name goes to index 0,
9898 followed by the other arguments.
9899 For instance, the call
9900
9901 <pre>
9902 $ lua -e "print(arg[1])"
9903 </pre><p>
9904 will print "<code>-e</code>".
9905 If there is a script,
9906 the script is called with parameters
9907 <code>arg[1]</code>, &middot;&middot;&middot;, <code>arg[#arg]</code>.
9908 (Like all chunks in Lua,
9909 the script is compiled as a vararg function.)
9910
9911
9912 <p>
9913 In interactive mode,
9914 Lua repeatedly prompts and waits for a line.
9915 After reading a line,
9916 Lua first try to interpret the line as an expression.
9917 If it succeeds, it prints its value.
9918 Otherwise, it interprets the line as a statement.
9919 If you write an incomplete statement,
9920 the interpreter waits for its completion
9921 by issuing a different prompt.
9922
9923
9924 <p>
9925 In case of unprotected errors in the script,
9926 the interpreter reports the error to the standard error stream.
9927 If the error object is not a string but
9928 has a metamethod <code>__tostring</code>,
9929 the interpreter calls this metamethod to produce the final message.
9930 Otherwise, the interpreter converts the error object to a string
9931 and adds a stack traceback to it.
9932
9933
9934 <p>
9935 When finishing normally,
9936 the interpreter closes its main Lua state
9937 (see <a href="#lua_close"><code>lua_close</code></a>).
9938 The script can avoid this step by
9939 calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
9940
9941
9942 <p>
9943 To allow the use of Lua as a
9944 script interpreter in Unix systems,
9945 the standalone interpreter skips
9946 the first line of a chunk if it starts with <code>#</code>.
9947 Therefore, Lua scripts can be made into executable programs
9948 by using <code>chmod +x</code> and the&nbsp;<code>#!</code> form,
9949 as in
9950
9951 <pre>
9952 #!/usr/local/bin/lua
9953 </pre><p>
9954 (Of course,
9955 the location of the Lua interpreter may be different in your machine.
9956 If <code>lua</code> is in your <code>PATH</code>,
9957 then
9958
9959 <pre>
9960 #!/usr/bin/env lua
9961 </pre><p>
9962 is a more portable solution.)
9963
9964
9965
9966 <h1>8 &ndash; <a name="8">Incompatibilities with the Previous Version</a></h1>
9967
9968 <p>
9969 Here we list the incompatibilities that you may find when moving a program
9970 from Lua&nbsp;5.2 to Lua&nbsp;5.3.
9971 You can avoid some incompatibilities by compiling Lua with
9972 appropriate options (see file <code>luaconf.h</code>).
9973 However,
9974 all these compatibility options will be removed in the future.
9975
9976
9977 <p>
9978 Lua versions can always change the C API in ways that
9979 do not imply source-code changes in a program,
9980 such as the numeric values for constants
9981 or the implementation of functions as macros.
9982 Therefore,
9983 you should not assume that binaries are compatible between
9984 different Lua versions.
9985 Always recompile clients of the Lua API when
9986 using a new version.
9987
9988
9989 <p>
9990 Similarly, Lua versions can always change the internal representation
9991 of precompiled chunks;
9992 precompiled chunks are not compatible between different Lua versions.
9993
9994
9995 <p>
9996 The standard paths in the official distribution may
9997 change between versions.
9998
9999
10000
10001 <h2>8.1 &ndash; <a name="8.1">Changes in the Language</a></h2>
10002 <ul>
10003
10004 <li>
10005 The main difference between Lua&nbsp;5.2 and Lua&nbsp;5.3 is the
10006 introduction of an integer subtype for numbers.
10007 Although this change should not affect "normal" computations,
10008 some computations
10009 (mainly those that involve some kind of overflow)
10010 can give different results.
10011
10012
10013 <p>
10014 You can fix these differences by forcing a number to be a float
10015 (in Lua&nbsp;5.2 all numbers were float),
10016 in particular writing constants with an ending <code>.0</code>
10017 or using <code>x = x + 0.0</code> to convert a variable.
10018 (This recommendation is only for a quick fix
10019 for an occasional incompatibility;
10020 it is not a general guideline for good programming.
10021 For good programming,
10022 use floats where you need floats
10023 and integers where you need integers.)
10024 </li>
10025
10026 <li>
10027 The conversion of a float to a string now adds a <code>.0</code> suffix
10028 to the result if it looks like an integer.
10029 (For instance, the float 2.0 will be printed as <code>2.0</code>,
10030 not as <code>2</code>.)
10031 You should always use an explicit format
10032 when you need a specific format for numbers.
10033
10034
10035 <p>
10036 (Formally this is not an incompatibility,
10037 because Lua does not specify how numbers are formatted as strings,
10038 but some programs assumed a specific format.)
10039 </li>
10040
10041 <li>
10042 The generational mode for the garbage collector was removed.
10043 (It was an experimental feature in Lua&nbsp;5.2.)
10044 </li>
10045
10046 </ul>
10047
10048
10049
10050
10051 <h2>8.2 &ndash; <a name="8.2">Changes in the Libraries</a></h2>
10052 <ul>
10053
10054 <li>
10055 The <code>bit32</code> library has been deprecated.
10056 It is easy to require a compatible external library or,
10057 better yet, to replace its functions with appropriate bitwise operations.
10058 (Keep in mind that <code>bit32</code> operates on 32-bit integers,
10059 while the bitwise operators in standard Lua operate on 64-bit integers.)
10060 </li>
10061
10062 <li>
10063 The Table library now respects metamethods
10064 for setting and getting elements.
10065 </li>
10066
10067 <li>
10068 The <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and
10069 its <code>__ipairs</code> metamethod has been deprecated.
10070 </li>
10071
10072 <li>
10073 Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
10074 For compatibility, Lua will continue to ignore this character.
10075 </li>
10076
10077 <li>
10078 The following functions were deprecated in the mathematical library:
10079 <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>,
10080 <code>frexp</code>, and <code>ldexp</code>.
10081 You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
10082 you can replace <code>math.atan2</code> with <code>math.atan</code>,
10083 which now accepts one or two parameters;
10084 you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
10085 For the other operations,
10086 you can either use an external library or
10087 implement them in Lua.
10088 </li>
10089
10090 <li>
10091 The searcher for C loaders used by <a href="#pdf-require"><code>require</code></a>
10092 changed the way it handles versioned names.
10093 Now, the version should come after the module name
10094 (as is usual in most other tools).
10095 For compatibility, that searcher still tries the old format
10096 if it cannot find an open function according to the new style.
10097 (Lua&nbsp;5.2 already worked that way,
10098 but it did not document the change.)
10099 </li>
10100
10101 </ul>
10102
10103
10104
10105
10106 <h2>8.3 &ndash; <a name="8.3">Changes in the API</a></h2>
10107
10108
10109 <ul>
10110
10111 <li>
10112 Continuation functions now receive as parameters what they needed
10113 to get through <code>lua_getctx</code>,
10114 so <code>lua_getctx</code> has been removed.
10115 Adapt your code accordingly.
10116 </li>
10117
10118 <li>
10119 Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
10120 Use 0 as the value of this parameter to get the old behavior.
10121 </li>
10122
10123 <li>
10124 Functions to inject/project unsigned integers
10125 (<code>lua_pushunsigned</code>, <code>lua_tounsigned</code>, <code>lua_tounsignedx</code>,
10126 <code>luaL_checkunsigned</code>, <code>luaL_optunsigned</code>)
10127 were deprecated.
10128 Use their signed equivalents with a type cast.
10129 </li>
10130
10131 <li>
10132 Macros to project non-default integer types
10133 (<code>luaL_checkint</code>, <code>luaL_optint</code>, <code>luaL_checklong</code>, <code>luaL_optlong</code>)
10134 were deprecated.
10135 Use their equivalent over <a href="#lua_Integer"><code>lua_Integer</code></a> with a type cast
10136 (or, when possible, use <a href="#lua_Integer"><code>lua_Integer</code></a> in your code).
10137 </li>
10138
10139 </ul>
10140
10141
10142
10143
10144 <h1>9 &ndash; <a name="9">The Complete Syntax of Lua</a></h1>
10145
10146 <p>
10147 Here is the complete syntax of Lua in extended BNF.
10148 As usual in extended BNF,
10149 {A} means 0 or more As,
10150 and [A] means an optional A.
10151 (For operator precedences, see <a href="#3.4.8">&sect;3.4.8</a>;
10152 for a description of the terminals
10153 Name, Numeral,
10154 and LiteralString, see <a href="#3.1">&sect;3.1</a>.)
10155
10156
10157
10158
10159 <pre>
10160
10161 chunk ::= block
10162
10163 block ::= {stat} [retstat]
10164
10165 stat ::= &lsquo;<b>;</b>&rsquo; |
10166 varlist &lsquo;<b>=</b>&rsquo; explist |
10167 functioncall |
10168 label |
10169 <b>break</b> |
10170 <b>goto</b> Name |
10171 <b>do</b> block <b>end</b> |
10172 <b>while</b> exp <b>do</b> block <b>end</b> |
10173 <b>repeat</b> block <b>until</b> exp |
10174 <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
10175 <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> |
10176 <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> |
10177 <b>function</b> funcname funcbody |
10178 <b>local</b> <b>function</b> Name funcbody |
10179 <b>local</b> namelist [&lsquo;<b>=</b>&rsquo; explist]
10180
10181 retstat ::= <b>return</b> [explist] [&lsquo;<b>;</b>&rsquo;]
10182
10183 label ::= &lsquo;<b>::</b>&rsquo; Name &lsquo;<b>::</b>&rsquo;
10184
10185 funcname ::= Name {&lsquo;<b>.</b>&rsquo; Name} [&lsquo;<b>:</b>&rsquo; Name]
10186
10187 varlist ::= var {&lsquo;<b>,</b>&rsquo; var}
10188
10189 var ::= Name | prefixexp &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; | prefixexp &lsquo;<b>.</b>&rsquo; Name
10190
10191 namelist ::= Name {&lsquo;<b>,</b>&rsquo; Name}
10192
10193 explist ::= exp {&lsquo;<b>,</b>&rsquo; exp}
10194
10195 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | &lsquo;<b>...</b>&rsquo; | functiondef |
10196 prefixexp | tableconstructor | exp binop exp | unop exp
10197
10198 prefixexp ::= var | functioncall | &lsquo;<b>(</b>&rsquo; exp &lsquo;<b>)</b>&rsquo;
10199
10200 functioncall ::= prefixexp args | prefixexp &lsquo;<b>:</b>&rsquo; Name args
10201
10202 args ::= &lsquo;<b>(</b>&rsquo; [explist] &lsquo;<b>)</b>&rsquo; | tableconstructor | LiteralString
10203
10204 functiondef ::= <b>function</b> funcbody
10205
10206 funcbody ::= &lsquo;<b>(</b>&rsquo; [parlist] &lsquo;<b>)</b>&rsquo; block <b>end</b>
10207
10208 parlist ::= namelist [&lsquo;<b>,</b>&rsquo; &lsquo;<b>...</b>&rsquo;] | &lsquo;<b>...</b>&rsquo;
10209
10210 tableconstructor ::= &lsquo;<b>{</b>&rsquo; [fieldlist] &lsquo;<b>}</b>&rsquo;
10211
10212 fieldlist ::= field {fieldsep field} [fieldsep]
10213
10214 field ::= &lsquo;<b>[</b>&rsquo; exp &lsquo;<b>]</b>&rsquo; &lsquo;<b>=</b>&rsquo; exp | Name &lsquo;<b>=</b>&rsquo; exp | exp
10215
10216 fieldsep ::= &lsquo;<b>,</b>&rsquo; | &lsquo;<b>;</b>&rsquo;
10217
10218 binop ::= &lsquo;<b>+</b>&rsquo; | &lsquo;<b>-</b>&rsquo; | &lsquo;<b>*</b>&rsquo; | &lsquo;<b>/</b>&rsquo; | &lsquo;<b>//</b>&rsquo; | &lsquo;<b>^</b>&rsquo; | &lsquo;<b>%</b>&rsquo; |
10219 &lsquo;<b>&amp;</b>&rsquo; | &lsquo;<b>~</b>&rsquo; | &lsquo;<b>|</b>&rsquo; | &lsquo;<b>&gt;&gt;</b>&rsquo; | &lsquo;<b>&lt;&lt;</b>&rsquo; | &lsquo;<b>..</b>&rsquo; |
10220 &lsquo;<b>&lt;</b>&rsquo; | &lsquo;<b>&lt;=</b>&rsquo; | &lsquo;<b>&gt;</b>&rsquo; | &lsquo;<b>&gt;=</b>&rsquo; | &lsquo;<b>==</b>&rsquo; | &lsquo;<b>~=</b>&rsquo; |
10221 <b>and</b> | <b>or</b>
10222
10223 unop ::= &lsquo;<b>-</b>&rsquo; | <b>not</b> | &lsquo;<b>#</b>&rsquo; | &lsquo;<b>~</b>&rsquo;
10224
10225 </pre>
10226
10227 <p>
10228
10229
10230
10231
10232
10233
10234
10235
10236 <HR>
10237 <SMALL CLASS="footer">
10238 Last update:
10239 Fri Jan 16 00:58:20 BRST 2015
10240 </SMALL>
10241 <!--
10242 Last change: minor edit
10243 -->
10244
10245 </div>
10246
10247 <% end;
10248 }
10249 end