Lines Matching +full:- +full:a
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
7 <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
13 <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A>
22 Copyright © 2015–2018 Lua.org, PUC-Rio.
24 <a href="http://www.lua.org/license.html">Lua license</a>.
28 <A HREF="contents.html#contents">contents</A>
30 <A HREF="contents.html#index">index</A>
32 <A HREF="http://www.lua.org/manual/">other versions</A>
35 <!-- ====================================================================== -->
38 <!-- $Id: manual.of,v 1.167.1.2 2018/06/26 15:49:07 roberto Exp $ -->
43 <h1>1 – <a name="1">Introduction</a></h1>
46 Lua is a powerful, efficient, lightweight, embeddable scripting language.
48 object-oriented programming, functional programming,
49 data-driven programming, and data description.
56 runs by interpreting bytecode with a register-based
65 Lua is implemented as a library, written in <em>clean C</em>,
67 The Lua distribution includes a host program called <code>lua</code>,
68 which uses the Lua library to offer a complete,
71 Lua is intended to be used both as a powerful, lightweight,
73 and as a powerful but lightweight and efficient stand-alone language.
77 As an extension language, Lua has no notion of a "main" program:
78 it works <em>embedded</em> in a host client,
80 (Frequently, this host is the stand-alone <code>lua</code> program.)
81 The host program can invoke functions to execute a piece of Lua code,
85 a wide range of different domains,
86 thus creating customized programming languages sharing a syntactical framework.
100 For a discussion of the decisions behind the design of Lua,
102 For a detailed introduction to programming in Lua,
107 <h1>2 – <a name="2">Basic Concepts</a></h1>
114 <h2>2.1 – <a name="2.1">Values and Types</a></h2>
117 Lua is a <em>dynamically typed language</em>.
125 All values in Lua are <em>first-class values</em>.
137 it usually represents the absence of a useful value.
139 Both <b>nil</b> and <b>false</b> make a condition false;
142 integer numbers and real (floating-point) numbers.
145 Lua is 8-bit clean:
146 strings can contain any 8-bit value,
148 Lua is also encoding-agnostic;
149 it makes no assumptions about the contents of a string.
157 but it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>).
162 Standard Lua uses 64-bit integers and double-precision (64-bit) floats,
164 uses 32-bit integers and/or single-precision (32-bit) floats.
173 functions written in C (see <a href="#3.4.10">§3.4.10</a>).
180 A userdata value represents a block of raw memory.
183 which is an object with a block of memory managed by Lua,
185 which is simply a C pointer value.
190 (see <a href="#2.4">§2.4</a>).
198 and it is used to implement coroutines (see <a href="#2.6">§2.6</a>).
199 Lua threads are not related to operating-system threads.
208 (<em>Not a Number</em> is a special value used to represent
213 Conversely, any key that is not part of a table has
218 Tables are the sole data-structuring mechanism in Lua;
223 providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
225 (see <a href="#3.4.9">§3.4.9</a>).
232 because functions are first-class values,
234 Thus tables can also carry <em>methods</em> (see <a href="#3.4.11">§3.4.11</a>).
240 The expressions <code>a[i]</code> and <code>a[j]</code>
248 any float with integral value used as a key
250 For instance, if you write <code>a[2.0] = true</code>,
268 The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type
269 of a given value (see <a href="#6.1">§6.1</a>).
275 <h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2>
278 As will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>,
279 any reference to a free name
280 (that is, a name not bound to any declaration) <code>var</code>
283 an external local variable named <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>),
284 so <code>_ENV</code> itself is never a free name in a chunk.
290 <code>_ENV</code> is a completely regular name.
293 Each reference to a free name uses the <code>_ENV</code> that is
295 following the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>).
303 Lua keeps a distinguished environment called the <em>global environment</em>.
304 This value is kept at a special index in the C registry (see <a href="#4.5">§4.5</a>).
305 In Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same val…
306 (<a href="#pdf-_G"><code>_G</code></a> is never used internally.)
310 When Lua loads a chunk,
312 is the global environment (see <a href="#pdf-load"><code>load</code></a>).
318 You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</c…
319 to load a chunk with a different environment.
327 <h2>2.3 – <a name="2.3">Error Handling</a></h2>
332 calling a function from the Lua library.
336 the compilation or execution of a Lua chunk,
344 <a href="#pdf-error"><code>error</code></a> function.
346 you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code…
347 to call a given function in <em>protected mode</em>.
354 Lua itself only generates errors whose error object is a string,
361 When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall<…
362 you may give a <em>message handler</em>
365 and returns a new error object.
368 for instance by inspecting the stack and creating a stack traceback.
375 It is not called for memory-allocation errors
382 <h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2>
385 Every value in Lua can have a <em>metatable</em>.
390 of operations over a value by setting specific fields in its metatable.
391 For instance, when a non-numeric value is the operand of an addition,
392 Lua checks for a function in the field "<code>__add</code>" of the value's metatable.
398 The key for each event in a metatable is a string
409 using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function.
410 Lua queries metamethods in metatables using a raw access (see <a href="#pdf-rawget"><code>rawget</c…
420 using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function.
422 (except by using the debug library (<a href="#6.10">§6.10</a>));
432 By default, a value has no metatable,
433 but the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>).
437 A metatable controls how an object behaves in
440 A metatable also can define a function to be called
441 when a userdata or a table is garbage collected (<a href="#2.5">§2.5</a>).
446 the metamethod is computed and called with a dummy second operand,
449 (by making these operators behave like a binary operation)
455 A detailed list of events controlled by metatables is given next.
464 If any operand for an addition is not a number
465 (nor a string coercible to a number),
466 Lua will try to call a metamethod.
468 If that operand does not define a metamethod for <code>__add</code>,
470 If Lua can find a metamethod,
480 the subtraction (<code>-</code>) operation.
505 the negation (unary <code>-</code>) operation.
517 except that Lua will try a metamethod
519 nor a value coercible to an integer (see <a href="#3.4.3">§3.4.3</a>).
550 except that Lua will try a metamethod
551 if any operand is neither a string nor a number
552 (which is always coercible to a string).
557 If the object is not a string,
559 If there is a metamethod,
564 If there is no metamethod but the object is a table,
565 then Lua uses the table length operation (see <a href="#3.4.7">§3.4.7</a>).
572 except that Lua will try a metamethod only when the values
575 The result of the call is always converted to a boolean.
581 except that Lua will try a metamethod only when the values
583 The result of the call is always converted to a boolean.
589 the less-equal operation can use two different events.
592 If it cannot find such a metamethod,
594 assuming that <code>a <= b</code> is equivalent to <code>not (b < a)</code>.
596 the result is always a boolean.
598 it is also slower than a real <code>__le</code> metamethod.)
603 This event happens when <code>table</code> is not a table or
610 the metamethod for this event can be either a function or a table.
611 If it is a function,
616 If it is a table,
625 this event happens when <code>table</code> is not a table or
632 the metamethod for this event can be either a function or a table.
633 If it is a function,
635 If it is a table,
642 Whenever there is a <code>__newindex</code> metamethod,
645 the metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a>
651 This event happens when Lua tries to call a non-function value
652 (that is, <code>func</code> is not a function).
665 It is a good practice to add all needed metamethods to a table
666 before setting it as a metatable of some object.
668 is followed (see <a href="#2.5.1">§2.5.1</a>).
676 (e.g., <a href="#pdf-tostring"><code>tostring</code></a>)
683 <h2>2.5 – <a name="2.5">Garbage Collection</a></h2>
691 a <em>garbage collector</em> to collect all <em>dead objects</em>
698 Lua implements an incremental mark-and-sweep collector.
699 It uses two numbers to control its garbage-collection cycles:
700 the <em>garbage-collector pause</em> and
701 the <em>garbage-collector step multiplier</em>.
703 (e.g., a value of 100 means an internal value of 1).
707 The garbage-collector pause
708 controls how long the collector waits before starting a new cycle.
711 start a new cycle.
712 A value of 200 means that the collector waits for the total memory in use
713 to double before starting a new cycle.
717 The garbage-collector step multiplier
724 can result in the collector never finishing a cycle.
731 If you set the step multiplier to a very large number
734 the collector behaves like a stop-the-world collector.
737 doing a complete collection every time Lua doubles its
742 You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
743 or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
749 <h3>2.5.1 – <a name="2.5.1">Garbage-Collection Metamethods</a></h3>
752 You can set garbage-collector metamethods for tables
754 for full userdata (see <a href="#2.4">§2.4</a>).
767 and the metatable has a field indexed by the string "<code>__gc</code>".
768 Note that if you set a metatable without a <code>__gc</code> field
774 When a marked object becomes garbage,
776 Instead, Lua puts it in a list.
781 If it is a function,
783 if the metamethod is not a function,
788 At the end of each garbage-collection cycle,
803 and the object memory is freed in the next garbage-collection cycle.
805 (e.g., a global variable),
807 Moreover, if the finalizer marks a finalizing object for finalization again,
811 the object memory is freed only in a GC cycle where
816 When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
826 <h3>2.5.2 – <a name="2.5.2">Weak Tables</a></h3>
829 A <em>weak table</em> is a table whose elements are
831 A weak reference is ignored by the garbage collector.
838 A weak table can have weak keys, weak values, or both.
839 A table with weak values allows the collection of its values,
841 A table with both weak keys and weak values allows the collection of
845 The weakness of a table is controlled by the
847 If the <code>__mode</code> field is a string containing the character '<code>k</code>',
854 A table with weak keys and strong values
857 a value is considered reachable only if its key is reachable.
859 if the only reference to a key comes through its value,
864 Any change in the weakness of a table may take effect only
866 In particular, if you change the weakness to a stronger mode,
887 have a special behavior in weak tables.
896 If a weak table is among the resurrected objects in a collection cycle,
905 <h2>2.6 – <a name="2.6">Coroutines</a></h2>
910 A coroutine in Lua represents an independent thread of execution.
912 a coroutine only suspends its execution by explicitly calling
913 a yield function.
917 You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>.
918 Its sole argument is a function
920 The <code>create</code> function only creates a new coroutine and
921 returns a handle to it (an object of type <em>thread</em>);
926 You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a…
927 When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
929 a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
932 Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are pas…
939 A coroutine can terminate its execution in two ways:
944 <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>,
946 In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>fal…
951 A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
952 When a coroutine yields,
953 the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immedia…
956 but in a function directly or indirectly called by the main function).
957 In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also retu…
958 plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
961 with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra
962 arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
966 Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
967 the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine,
969 it returns a function that, when called, resumes the coroutine.
971 go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
972 …a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> returns all the values returned by <a…
974 Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
975 <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors;
984 function foo (a)
985 print("foo", a)
986 return coroutine.yield(2*a)
989 co = coroutine.create(function (a,b)
990 print("co-body", a, b)
991 local r = foo(a+1)
992 print("co-body", r)
993 local r, s = coroutine.yield(a+b, a-b)
994 print("co-body", r, s)
1006 co-body 1 10
1009 co-body r
1010 main true 11 -9
1011 co-body x y
1018 see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>…
1019 and <a href="#lua_yield"><code>lua_yield</code></a>.
1025 <h1>3 – <a name="3">The Language</a></h1>
1039 {<em>a</em>} means 0 or more <em>a</em>'s, and
1040 [<em>a</em>] means an optional <em>a</em>.
1041 Non-terminals are shown like non-terminal,
1044 The complete syntax of Lua can be found in <a href="#9">§9</a>
1049 <h2>3.1 – <a name="3.1">Lexical Conventions</a></h2>
1052 Lua is a free-form language.
1063 not beginning with a digit and
1064 not being a reserved word.
1081 Lua is a case-sensitive language:
1082 <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
1084 As a convention,
1087 one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>).
1094 + - * / % ^ #
1102 A <em>short literal string</em>
1104 and can contain the following C-like escape sequences:
1105 '<code>\a</code>' (bell),
1115 A backslash followed by a line break
1116 results in a newline in the string.
1118 of white-space characters,
1120 it is particularly useful to break and indent a long literal string
1123 A short literal string cannot contain unescaped line breaks
1124 nor escapes not forming a valid escape sequence.
1128 We can specify any byte in a short literal string by its numeric value
1132 where <em>XX</em> is a sequence of exactly two hexadecimal digits,
1134 where <em>ddd</em> is a sequence of up to three decimal digits.
1135 (Note that if a decimal escape sequence is to be followed by a digit,
1140 The UTF-8 encoding of a Unicode character
1141 can be inserted in a literal string with
1144 where <em>XXX</em> is a sequence of one or more hexadecimal digits
1149 Literal strings can also be defined using a long format
1157 A <em>closing long bracket</em> is defined similarly;
1159 a closing long bracket of level 4 is written as <code>]====]</code>.
1160 A <em>long literal</em> starts with an opening long bracket of any level and
1162 It can contain any text except a closing bracket of the same level.
1166 Any kind of end-of-line sequence
1169 is converted to a simple newline.
1174 when the opening long bracket is immediately followed by a newline,
1176 As an example, in a system using ASCII
1177 (in which '<code>a</code>' is coded as 97,
1182 a = 'alo\n123"'
1183 a = "alo\n123\""
1184 a = '\97lo\10\04923"'
1185 a = [[alo
1187 a = [==[
1193 Any byte in a literal string not
1199 non-text data as a quoted literal with
1200 explicit escape sequences for the non-text characters.
1204 A <em>numeric constant</em> (or <em>numeral</em>)
1207 marked by a letter '<code>e</code>' or '<code>E</code>'.
1212 marked by a letter '<code>p</code>' or '<code>P</code>'.
1213 A numeric constant with a radix point or an exponent
1214 denotes a float;
1226 3.0 3.1416 314.16e-2 0.31416E1 34e1
1227 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
1231 A <em>comment</em> starts with a double hyphen (<code>--</code>)
1232 anywhere outside a string.
1233 If the text immediately after <code>--</code> is not an opening long bracket,
1234 the comment is a <em>short comment</em>,
1236 Otherwise, it is a <em>long comment</em>,
1244 <h2>3.2 – <a name="3.2">Variables</a></h2>
1253 A single name can denote a global variable or a local variable
1254 (or a function's formal parameter,
1255 which is a particular kind of local variable):
1260 Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>.
1265 as a local (see <a href="#3.3.7">§3.3.7</a>).
1268 defined inside their scope (see <a href="#3.5">§3.5</a>).
1272 Before the first assignment to a variable, its value is <b>nil</b>.
1276 Square brackets are used to index a table:
1282 (see <a href="#2.4">§2.4</a>).
1294 An access to a global variable <code>x</code>
1297 <code>_ENV</code> is never a global name (see <a href="#2.2">§2.2</a>).
1303 <h2>3.3 – <a name="3.3">Statements</a></h2>
1314 <h3>3.3.1 – <a name="3.3.1">Blocks</a></h3>
1317 A block is a list of statements,
1325 start a block with a semicolon
1339 a = b + c
1345 a = b + c(print or io.write)('done')
1347 a = b + c; (print or io.write)('done')
1352 as the start of the arguments to a call.
1354 it is a good practice to always precede with a semicolon
1355 statements that start with a parenthesis:
1362 A block can be explicitly delimited to produce a single statement:
1370 add a <b>return</b> statement in the middle
1371 of another block (see <a href="#3.3.4">§3.3.4</a>).
1377 <h3>3.3.2 – <a name="3.3.2">Chunks</a></h3>
1380 The unit of compilation of Lua is called a <em>chunk</em>.
1382 a chunk is simply a block:
1389 Lua handles a chunk as the body of an anonymous function
1390 with a variable number of arguments
1391 (see <a href="#3.4.11">§3.4.11</a>).
1395 scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>).
1401 A chunk can be stored in a file or in a string inside the host program.
1402 To execute a chunk,
1404 precompiling the chunk's code into instructions for a virtual machine,
1411 see program <code>luac</code> and function <a href="#pdf-string.dump"><code>string.dump</code></a> …
1413 …tically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>).
1419 <h3>3.3.3 – <a name="3.3.3">Assignment</a></h3>
1424 defines a list of variables on the left side
1425 and a list of expressions on the right side.
1433 Expressions are discussed in <a href="#3.4">§3.4</a>.
1444 If the list of expressions ends with a function call,
1447 (except when the call is enclosed in parentheses; see <a href="#3.4">§3.4</a>).
1457 i, a[i] = i+1, 20
1459 sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
1460 because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
1477 An assignment to a global name <code>x = val</code>
1479 <code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>).
1485 can be changed via metatables (see <a href="#2.4">§2.4</a>).
1491 <h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p>
1504 Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>).
1508 The condition expression of a
1524 The <b>goto</b> statement transfers the program control to a label.
1537 A label is visible in the entire block where it is defined,
1539 inside nested blocks where a label with the same name is defined and
1541 A goto may jump to any visible label as long as it does not
1542 enter into the scope of a local variable.
1551 The <b>break</b> statement terminates the execution of a
1559 A <b>break</b> ends the innermost enclosing loop.
1564 from a function or a chunk
1576 as the last statement of a block.
1577 If it is really necessary to <b>return</b> in the middle of a block,
1586 <h3>3.3.5 – <a name="3.3.5">For Statement</a></h3>
1595 The numerical <b>for</b> loop repeats a block of code while a
1605 More precisely, a <b>for</b> statement like
1616 <em>var</em> = <em>var</em> - <em>step</em>
1646 then a step of 1 is used.
1650 You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop.
1664 On each iteration, the iterator function is called to produce a new value,
1672 A <b>for</b> statement like
1697 a <em>state</em>,
1707 You can use <b>break</b> to exit a <b>for</b> loop.
1722 <h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p>
1723 To allow possible side-effects,
1730 Function calls are explained in <a href="#3.4.10">§3.4.10</a>.
1736 <h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p>
1737 Local variables can be declared anywhere inside a block.
1744 of a multiple assignment (see <a href="#3.3.3">§3.3.3</a>).
1749 A chunk is also a block (see <a href="#3.3.2">§3.3.2</a>),
1750 and so local variables can be declared in a chunk outside any explicit block.
1754 The visibility rules for local variables are explained in <a href="#3.5">§3.5</a>.
1762 <h2>3.4 – <a name="3.4">Expressions</a></h2>
1781 Numerals and literal strings are explained in <a href="#3.1">§3.1</a>;
1782 variables are explained in <a href="#3.2">§3.2</a>;
1783 function definitions are explained in <a href="#3.4.11">§3.4.11</a>;
1784 function calls are explained in <a href="#3.4.10">§3.4.10</a>;
1785 table constructors are explained in <a href="#3.4.9">§3.4.9</a>.
1788 directly inside a vararg function;
1789 they are explained in <a href="#3.4.11">§3.4.11</a>.
1793 Binary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>),
1794 bitwise operators (see <a href="#3.4.2">§3.4.2</a>),
1795 relational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5…
1796 and the concatenation operator (see <a href="#3.4.6">§3.4.6</a>).
1797 Unary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>),
1798 the unary bitwise NOT (see <a href="#3.4.2">§3.4.2</a>),
1799 the unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>),
1800 and the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>).
1805 If a function call is used as a statement (see <a href="#3.3.6">§3.3.6</a>),
1809 of a list of expressions,
1815 or adding a single <b>nil</b> if there are no values.
1822 f() -- adjusted to 0 results
1823 g(f(), x) -- f() is adjusted to 1 result
1824 g(x, f()) -- g gets x plus all results from f()
1825 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
1826 a,b = ... -- a gets the first vararg argument, b gets
1827 -- the second (both a and b can get nil if there
1828 -- is no corresponding vararg argument)
1830 a,b,c = x, f() -- f() is adjusted to 2 results
1831 a,b,c = f() -- f() is adjusted to 3 results
1832 return f() -- returns all results from f()
1833 return ... -- returns all received vararg arguments
1834 return x,y,f() -- returns x, y, and all results from f()
1835 {f()} -- creates a list with all results from f()
1836 {...} -- creates a list with all vararg arguments
1837 {f(), nil} -- f() is adjusted to 1 result
1843 <code>(f(x,y,z))</code> is always a single value,
1850 <h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p>
1855 <li><b><code>-</code>: </b>subtraction</li>
1861 <li><b><code>-</code>: </b>unary minus</li>
1871 numbers (see <a href="#3.4.3">§3.4.3</a>),
1874 for floating-point arithmetic
1876 and the result is a float.
1882 and the result is always a float.
1884 so that it works for non-integer exponents too.
1888 Floor division (<code>//</code>) is a division
1894 Modulo is defined as the remainder of a division
1901 according to the usual rules of two-complement arithmetic.
1908 <h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p>
1922 (see <a href="#3.4.3">§3.4.3</a>),
1938 <h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p>
1945 (integers and floats) convert the integer operand to a float;
1955 whenever a number is expected.
1959 In a conversion from integer to float,
1960 if the integer value has an exact representation as a float,
1979 First, the string is converted to an integer or a float,
1981 (The string may have also leading and trailing spaces and a sign.)
1989 accept both a dot and the current locale mark
1991 (The Lua lexer, however, accepts only a dot.)
1995 The conversion from numbers to strings uses a
1996 non-specified human-readable format.
1999 (see <a href="#pdf-string.format"><code>string.format</code></a>).
2005 <h3>3.4.4 – <a name="3.4.4">Relational Operators</a></h3><p>
2031 Every time you create a new object
2032 (a table, userdata, or thread),
2034 A closure is always equal to itself.
2044 by using the "eq" metamethod (see <a href="#2.4">§2.4</a>).
2052 entries in a table.
2067 metamethod (see <a href="#2.4">§2.4</a>).
2068 A comparison <code>a > b</code> is translated to <code>b < a</code>
2069 and <code>a >= b</code> is translated to <code>b <= a</code>.
2081 <h3>3.4.5 – <a name="3.4.5">Logical Operators</a></h3><p>
2084 Like the control structures (see <a href="#3.3.4">§3.3.4</a>),
2097 Both <b>and</b> and <b>or</b> use short-circuit evaluation;
2103 10 or 20 --> 10
2104 10 or error() --> 10
2105 nil or "a" --> "a"
2106 nil and 10 --> nil
2107 false and error() --> false
2108 false and nil --> false
2109 false or nil --> nil
2110 10 and 20 --> 20
2113 <code>--></code> indicates the result of the preceding expression.)
2119 <h3>3.4.6 – <a name="3.4.6">Concatenation</a></h3><p>
2123 strings according to the rules described in <a href="#3.4.3">§3.4.3</a>.
2124 Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">§2.4</a>).
2130 <h3>3.4.7 – <a name="3.4.7">The Length Operator</a></h3>
2137 The length of a string is its number of bytes
2143 The length operator applied on a table
2144 returns a border in that table.
2145 A <em>border</em> in a table <code>t</code> is any natural number
2152 a border is any (natural) index in a table
2153 where a non-nil value is followed by a nil value
2158 A table with exactly one border is called a <em>sequence</em>.
2159 For instance, the table <code>{10, 20, 30, 40, 50}</code> is a sequence,
2162 and therefore it is not a sequence.
2165 so it is not a sequence, too.
2166 The table <code>{}</code> is a sequence with border 0.
2167 Note that non-natural keys do not interfere
2168 with whether a table is a sequence.
2172 When <code>t</code> is a sequence,
2175 When <code>t</code> is not a sequence,
2180 the memory addresses of its non-numeric keys.)
2184 The computation of the length of a table
2185 has a guaranteed worst time of <em>O(log n)</em>,
2190 A program can modify the behavior of the length operator for
2191 any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">§2.4</a>).
2197 <h3>3.4.8 – <a name="3.4.8">Precedence</a></h3><p>
2210 + -
2212 unary operators (not # - ~)
2225 <h3>3.4.9 – <a name="3.4.9">Table Constructors</a></h3><p>
2227 Every time a constructor is evaluated, a new table is created.
2228 A constructor can be used to create an empty table
2229 or to create a table and initialize some of its fields.
2242 A field of the form <code>name = exp</code> is equivalent to
2251 a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
2259 t[1] = "x" -- 1st exp
2260 t[2] = "y" -- 2nd exp
2261 t.x = 1 -- t["x"] = 1
2262 t[3] = f(x) -- 3rd exp
2264 t[4] = 45 -- 4th exp
2265 a = t
2270 The order of the assignments in a constructor is undefined.
2276 and the expression is a function call or a vararg expression,
2278 (see <a href="#3.4.10">§3.4.10</a>).
2283 as a convenience for machine-generated code.
2289 <h3>3.4.10 – <a name="3.4.10">Function Calls</a></h3><p>
2290 A function call in Lua has the following syntax:
2295 In a function call,
2303 (see <a href="#2.4">§2.4</a>).
2313 A call <code>v:name(<em>args</em>)</code>
2327 A call of the form <code>f{<em>fields</em>}</code> is
2329 that is, the argument list is a single new table.
2330 A call of the form <code>f'<em>string</em>'</code>
2333 that is, the argument list is a single literal string.
2337 A call of the form <code>return <em>functioncall</em></code> is called
2338 a <em>tail call</em>.
2341 in a tail call,
2344 a program can execute.
2345 However, a tail call erases any debug information about the
2347 Note that a tail call only happens with a particular syntax,
2354 return (f(x)) -- results adjusted to 1
2356 return x, f(x) -- additional results
2357 f(x); return -- results discarded
2358 return x or f(x) -- results adjusted to 1
2364 <h3>3.4.11 – <a name="3.4.11">Function Definitions</a></h3>
2395 function t.a.b.c.f () <em>body</em> end
2400 t.a.b.c.f = function () <em>body</em> end
2417 (This only makes a difference when the body of the function
2422 A function definition is an executable expression,
2424 When Lua precompiles a chunk,
2439 When a function is called,
2442 unless the function is a <em>vararg function</em>,
2445 A vararg function does not adjust its argument list;
2447 to the function through a <em>vararg expression</em>,
2449 The value of this expression is a list of all actual extra arguments,
2450 similar to a function with multiple results.
2451 If a vararg expression is used inside another expression
2452 or in the middle of a list of expressions,
2454 If the expression is used as the last element of a list of expressions,
2463 function f(a, b) end
2464 function g(a, b, ...) end
2473 f(3) a=3, b=nil
2474 f(3, 4) a=3, b=4
2475 f(3, 4, 5) a=3, b=4
2476 f(r(), 10) a=1, b=10
2477 f(r()) a=1, b=2
2479 g(3) a=3, b=nil, ... --> (nothing)
2480 g(3, 4) a=3, b=4, ... --> (nothing)
2481 g(3, 4, 5, 8) a=3, b=4, ... --> 5 8
2482 g(5, r()) a=5, b=1, ... --> 2 3
2486 Results are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>).
2487 If control reaches the end of a function
2488 without encountering a <b>return</b> statement,
2494 There is a system-dependent limit on the number of values
2495 that a function may return.
2506 function t.a.b.c:f (<em>params</em>) <em>body</em> end
2511 t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
2519 <h2>3.5 – <a name="3.5">Visibility Rules</a></h2>
2523 Lua is a lexically scoped language.
2524 The scope of a local variable begins at the first statement after
2525 its declaration and lasts until the last non-void statement
2530 x = 10 -- global variable
2531 do -- new block
2532 local x = x -- new 'x', with value 10
2533 print(x) --> 10
2535 do -- another block
2536 local x = x+1 -- another 'x'
2537 print(x) --> 12
2539 print(x) --> 11
2541 print(x) --> 10 (the global one)
2545 Notice that, in a declaration like <code>local x = x</code>,
2554 A local variable used by an inner function is called
2560 Notice that each execution of a <b>local</b> statement
2565 a = {}
2569 a[i] = function () y=y+1; return x+y end
2574 Each of these closures uses a different <code>y</code> variable,
2581 <h1>4 – <a name="4">The Application Program Interface</a></h1>
2589 are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
2594 any facility in the API may be provided as a macro instead.
2597 (except for the first argument, which is always a Lua state),
2598 and so do not generate any hidden side-effects.
2605 with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
2611 It keeps all information it needs in a dynamic structure,
2618 The type <a href="#lua_State"><code>lua_State</code></a> (despite its name) refers to a thread.
2624 A pointer to a thread must be passed as the first argument to
2625 every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
2626 which creates a Lua state from scratch and returns a pointer
2631 <h2>4.1 – <a name="4.1">The Stack</a></h2>
2634 Lua uses a <em>virtual stack</em> to pass values to and from C.
2635 Each element in this stack represents a Lua value
2642 Whenever Lua calls C, the called function gets a new stack,
2648 to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
2653 most query operations in the API do not follow a strict stack discipline.
2656 A positive index represents an absolute stack position
2658 a negative index represents an offset relative to the top of the stack.
2664 index -1 also represents the last element
2666 and index <em>-n</em> represents the first element.
2672 <h2>4.2 – <a name="4.2">Stack Size</a></h2>
2679 You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
2686 at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
2693 When you call a Lua function
2694 without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
2697 So, before pushing anything in the stack after such a call
2698 you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2704 <h2>4.3 – <a name="4.3">Valid and Acceptable Indices</a></h2>
2712 A <em>valid index</em> is an index that refers to a
2713 position that stores a modifiable Lua value.
2717 plus <em>pseudo-indices</em>,
2720 Pseudo-indices are used to access the registry (see <a href="#4.5">§4.5</a>)
2721 and the upvalues of a C function (see <a href="#4.4">§4.4</a>).
2725 Functions that do not need a specific mutable position,
2726 but only a value (e.g., query functions),
2740 For instance, a C function can query its third argument
2741 without the need to first check whether there is a third argument,
2742 that is, without the need to check whether 3 is a valid index.
2747 any non-valid index is treated as if it
2748 contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
2749 which behaves like a nil value.
2755 <h2>4.4 – <a name="4.4">C Closures</a></h2>
2758 When a C function is created,
2760 thus creating a <em>C closure</em>
2761 (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
2767 Whenever a C function is called,
2768 its upvalues are located at specific pseudo-indices.
2769 These pseudo-indices are produced by the macro
2770 <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
2771 The first upvalue associated with a function is at index
2777 which is one plus the maximum number of upvalues in a closure),
2784 <h2>4.5 – <a name="4.5">Registry</a></h2>
2787 Lua provides a <em>registry</em>,
2788 a predefined table that can be used by any C code to
2790 The registry table is always located at pseudo-index
2791 <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>.
2796 Typically, you should use as key a string containing your library name,
2797 or a light userdata with the address of a C object in your code,
2806 by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>)
2812 When you create a new Lua state,
2819 <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index t…
2824 <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the reg…
2832 <h2>4.6 – <a name="4.6">Error Handling in C</a></h2>
2839 (such as a memory allocation error or a type error)
2841 that is, it does a long jump.
2842 A <em>protected environment</em> uses <code>setjmp</code>
2843 to set a recovery point;
2848 Inside a C function you can raise an error by calling <a href="#lua_error"><code>lua_error</co…
2853 for instance due to a memory allocation error.
2860 Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
2865 (e.g., doing a long jump to your own recovery point outside Lua).
2871 is a mechanism of last resort.
2873 As a general rule,
2874 when a C function is called by Lua with a Lua state,
2879 (e.g., a Lua argument to the function,
2880 a Lua state stored in the registry, or
2881 the result of <a href="#lua_newthread"><code>lua_newthread</code></a>),
2886 The panic function runs as if it were a message handler (see <a href="#2.3">§2.3</a>);
2890 the panic function must first check the available space (see <a href="#4.2">§4.2</a>).
2896 <h2>4.7 – <a name="4.7">Handling Yields in C</a></h2>
2899 Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2900 Therefore, if a C function <code>foo</code> calls an API function
2911 <a href="#lua_yieldk"><code>lua_yieldk</code></a>, <a href="#lua_callk"><code>lua_callk</code></a>,…
2912 All those functions receive a <em>continuation function</em>
2913 (as a parameter named <code>k</code>) to continue execution after a yield.
2918 We have a C function called from Lua which we will call
2923 (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
2924 …callee function is either <a href="#lua_callk"><code>lua_callk</code></a> or <a href="#lua_pcallk"…
2935 Instead, Lua calls a <em>continuation function</em>,
2953 the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
2967 the new function <code>k</code> is a
2968 <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
2970 was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
2972 being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
2975 replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk…
2985 in case of errors or resuming after a yield.
2987 <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code><…
2996 was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
3000 For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
3001 … status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code><…
3002 except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a y…
3003 (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
3004 For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</cod…
3005 the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continu…
3009 Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
3011 with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
3012 (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
3014 because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
3023 after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
3033 <h2>4.8 – <a name="4.8">Functions and Types</a></h2>
3039 <span class="apii">[-o, +p, <em>x</em>]</span>
3048 A field in the form <code>x|y</code> means the function can push (or pop)
3057 '<code>-</code>' means the function never raises any error;
3058 '<code>m</code>' means the function may raise out-of-memory errors
3059 and errors running a <code>__gc</code> metamethod;
3067 <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
3068 <span class="apii">[-0, +0, –]</span>
3080 <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
3087 The type of the memory-allocation function used by Lua states.
3088 The allocator function must provide a
3092 <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
3093 <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
3109 …a href="#pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, <a href="#pdf-LUA_TTABLE"><code>LUA_TTABLE…
3110 <a href="#pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, or <a href="#pdf-LUA_TTHREAD"><code>LU…
3111 Lua is creating a new object of that type.
3136 Here is a simple implementation for the allocator function.
3137 It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
3154 This code assumes that <code>realloc</code> does not fail when shrinking a block.
3156 it seems to be a safe assumption.)
3162 <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
3163 <span class="apii">[-(2|1), +1, <em>e</em>]</span>
3181 <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)<…
3182 <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code…
3183 <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</c…
3184 <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</c…
3185 <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//…
3186 <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</l…
3187 <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</c…
3188 <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (una…
3189 <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise NOT (<code>~</co…
3190 <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise AND (<code>&…
3191 <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise OR (<code>|</code>…
3192 <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive OR (<c…
3193 <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code><<…
3194 <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>>>…
3201 <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
3202 <span class="apii">[-0, +0, –]</span>
3206 Sets a new panic function and returns the old one (see <a href="#4.6">§4.6</a>).
3212 <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
3213 <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
3217 Calls a function.
3221 To call a function you must use the following protocol:
3226 Finally you call <a href="#lua_call"><code>lua_call</code></a>;
3232 unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
3243 (with a <code>longjmp</code>).
3251 a = f("how", t.x, 14)
3259 lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
3260 lua_remove(L, -2); /* remove 't' from the stack */
3263 lua_setglobal(L, "a"); /* set global 'a' */
3273 <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
3274 <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
3282 This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
3283 but allows the called function to yield (see <a href="#4.7">§4.7</a>).
3289 <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
3298 a C function must use the following protocol,
3300 a C function receives its arguments from Lua in its stack
3306 To return values to Lua, a C function just pushes them onto the stack,
3311 Like a Lua function, a C function called by Lua can also return
3316 As an example, the following function receives a variable number
3340 <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
3341 <span class="apii">[-0, +0, –]</span>
3349 to be larger than a fixed maximum size
3360 <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
3361 <span class="apii">[-0, +0, –]</span>
3366 (calling the corresponding garbage-collection metamethods, if any)
3370 On the other hand, long-running programs that create multiple states,
3378 <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
3379 <span class="apii">[-0, +0, <em>e</em>]</span>
3397 <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code…
3398 <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code><</c…
3399 <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code><…
3406 <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
3407 <span class="apii">[-n, +1, <em>e</em>]</span>
3417 (see <a href="#3.4.6">§3.4.6</a>).
3423 <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
3424 <span class="apii">[-0, +0, –]</span>
3437 <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
3438 <span class="apii">[-0, +1, <em>m</em>]</span>
3442 Creates a new empty table and pushes it onto the stack.
3443 Parameter <code>narr</code> is a hint for how many elements the table
3444 will have as a sequence;
3445 parameter <code>nrec</code> is a hint for how many other elements
3450 Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
3456 <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
3457 <span class="apii">[-0, +0, –]</span>
3464 Dumps a function as a binary chunk.
3465 Receives a Lua function on the top of the stack
3466 and produces a binary chunk that,
3468 results in a function equivalent to the one dumped.
3470 <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua…
3495 <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
3496 <span class="apii">[-1, +0, <em>v</em>]</span>
3500 Generates a Lua error,
3502 This function does a long jump,
3504 (see <a href="#luaL_error"><code>luaL_error</code></a>).
3510 <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
3511 <span class="apii">[-0, +0, <em>m</em>]</span>
3533 performs a full garbage-collection cycle.
3551 for the <em>pause</em> of the collector (see <a href="#2.5">§2.5</a>)
3557 the collector (see <a href="#2.5">§2.5</a>)
3562 returns a boolean that tells whether the collector is running
3570 see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
3576 <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
3577 <span class="apii">[-0, +0, –]</span>
3581 Returns the memory-allocation function of a given state.
3583 opaque pointer given when the memory-allocator function was set.
3589 <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
3590 <span class="apii">[-0, +1, <em>e</em>]</span>
3596 As in Lua, this function may trigger a metamethod
3597 for the "index" event (see <a href="#2.4">§2.4</a>).
3607 <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
3608 <span class="apii">[-0, +0, –]</span>
3612 Returns a pointer to a raw memory area associated with the
3619 Each new thread has this area initialized with a copy
3624 By default, this area has the size of a pointer to void,
3625 but you can recompile Lua with a different size for this area.
3632 <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
3633 <span class="apii">[-0, +1, <em>e</em>]</span>
3644 <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p>
3645 <span class="apii">[-0, +1, <em>e</em>]</span>
3651 As in Lua, this function may trigger a metamethod
3652 for the "index" event (see <a href="#2.4">§2.4</a>).
3662 <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
3663 <span class="apii">[-0, +(0|1), –]</span>
3667 If the value at the given index has a metatable,
3676 <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
3677 <span class="apii">[-1, +1, <em>e</em>]</span>
3689 As in Lua, this function may trigger a metamethod
3690 for the "index" event (see <a href="#2.4">§2.4</a>).
3700 <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
3701 <span class="apii">[-0, +0, –]</span>
3714 <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
3715 <span class="apii">[-0, +1, –]</span>
3730 <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
3731 <span class="apii">[-1, +1, –]</span>
3737 This function cannot be called with a pseudo-index,
3738 because a pseudo-index is not an actual stack position.
3744 <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
3753 (usually a 64-bit two-complement integer),
3755 (usually a 32-bit two-complement integer).
3761 <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code…
3768 <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
3769 <span class="apii">[-0, +0, –]</span>
3773 Returns 1 if the value at the given index is a boolean,
3780 <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
3781 <span class="apii">[-0, +0, –]</span>
3785 Returns 1 if the value at the given index is a C function,
3792 <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
3793 <span class="apii">[-0, +0, –]</span>
3797 Returns 1 if the value at the given index is a function
3804 <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p>
3805 <span class="apii">[-0, +0, –]</span>
3810 (that is, the value is a number and is represented as an integer),
3817 <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
3818 <span class="apii">[-0, +0, –]</span>
3822 Returns 1 if the value at the given index is a light userdata,
3829 <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
3830 <span class="apii">[-0, +0, –]</span>
3841 <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
3842 <span class="apii">[-0, +0, –]</span>
3853 <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
3854 <span class="apii">[-0, +0, –]</span>
3866 <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
3867 <span class="apii">[-0, +0, –]</span>
3871 Returns 1 if the value at the given index is a number
3872 or a string convertible to a number,
3879 <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
3880 <span class="apii">[-0, +0, –]</span>
3884 Returns 1 if the value at the given index is a string
3885 or a number (which is always convertible to a string),
3892 <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
3893 <span class="apii">[-0, +0, –]</span>
3897 Returns 1 if the value at the given index is a table,
3904 <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
3905 <span class="apii">[-0, +0, –]</span>
3909 Returns 1 if the value at the given index is a thread,
3916 <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
3917 <span class="apii">[-0, +0, –]</span>
3921 Returns 1 if the value at the given index is a userdata
3928 <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
3929 <span class="apii">[-0, +0, –]</span>
3940 <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3>
3944 The type for continuation-function contexts.
3945 It must be a numeric type.
3955 <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
3959 Type for continuation functions (see <a href="#4.7">§4.7</a>).
3965 <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
3966 <span class="apii">[-0, +1, <em>e</em>]</span>
3971 It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>) and
3972 may trigger a metamethod for the "length" event (see <a href="#2.4">§2.4</a>).
3979 <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
3980 <span class="apii">[-0, +1, –]</span>
3988 Loads a Lua chunk without running it.
3990 <code>lua_load</code> pushes the compiled chunk as a Lua
4000 <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
4002 <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
4005 <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
4006 memory allocation (out-of-memory) error;</li>
4008 <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
4009 error while running a <code>__gc</code> metamethod.
4017 The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
4018 to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
4023 The <code>chunkname</code> argument gives a name to the chunk,
4024 which is used for error messages and in debug information (see <a href="#4.9">§4.9</a>).
4030 The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
4032 a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
4044 stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">§4.5</a>).
4046 this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).
4053 <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
4054 <span class="apii">[-0, +0, –]</span>
4058 Creates a new thread running in a new, independent state.
4063 through this function (see <a href="#lua_Alloc"><code>lua_Alloc</code></a>).
4071 <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
4072 <span class="apii">[-0, +1, <em>m</em>]</span>
4076 Creates a new empty table and pushes it onto the stack.
4083 <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
4084 <span class="apii">[-0, +1, <em>m</em>]</span>
4088 Creates a new thread, pushes it on the stack,
4089 and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new…
4096 There is no explicit function to close or to destroy a thread.
4104 <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
4105 <span class="apii">[-0, +1, <em>m</em>]</span>
4109 This function allocates a new block of memory with the given size,
4110 pushes onto the stack a new full userdata with the block address,
4118 <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
4119 <span class="apii">[-1, +(2|0), <em>e</em>]</span>
4123 Pops a key from the stack,
4124 and pushes a key–value pair from the table at the given index
4127 then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
4131 A typical traversal looks like this:
4137 /* uses 'key' (at index -2) and 'value' (at index -1) */
4138 printf("%s - %s\n",
4139 lua_typename(L, lua_type(L, -2)),
4140 lua_typename(L, lua_type(L, -1)));
4147 While traversing a table,
4148 do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
4149 unless you know that the key is actually a string.
4150 Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
4152 this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
4156 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
4163 <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
4172 but that can be changed to a single float or a long double.
4179 <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3>
4183 Converts a Lua float to a Lua integer.
4187 The macro results in a boolean indicating whether the
4201 <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
4202 <span class="apii">[-(nargs + 1), +(nresults|1), –]</span>
4206 Calls a function in protected mode.
4211 in <a href="#lua_call"><code>lua_call</code></a>.
4213 <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_…
4215 <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
4216 pushes a single value on the stack (the error object),
4218 Like <a href="#lua_call"><code>lua_call</code></a>,
4219 <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
4227 Otherwise, <code>msgh</code> is the stack index of a
4229 (This index cannot be a pseudo-index.)
4233 returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
4238 information to the error object, such as a stack traceback.
4239 …h information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></…
4244 The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
4249 <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b>
4252 <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b>
4253 a runtime error.
4256 <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
4261 <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b>
4265 <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
4266 error while running a <code>__gc</code> metamethod.
4277 <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
4278 <span class="apii">[-(nargs + 1), +(nresults|1), –]</span>
4287 This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
4288 but allows the called function to yield (see <a href="#4.7">§4.7</a>).
4294 <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
4295 <span class="apii">[-n, +0, –]</span>
4305 <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
4306 <span class="apii">[-0, +1, –]</span>
4310 Pushes a boolean value with value <code>b</code> onto the stack.
4316 <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
4317 <span class="apii">[-n, +1, <em>m</em>]</span>
4321 Pushes a new C closure onto the stack.
4325 When a C function is created,
4327 thus creating a C closure (see <a href="#4.4">§4.4</a>);
4329 To associate values with a C function,
4332 Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
4336 <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
4345 this function creates a <em>light C function</em>,
4346 which is just a pointer to the C function.
4347 In that case, it never raises a memory error.
4353 <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
4354 <span class="apii">[-0, +1, –]</span>
4358 Pushes a C function onto the stack.
4359 This function receives a pointer to a C function
4360 and pushes onto the stack a Lua value of type <code>function</code> that,
4367 and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
4373 <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
4374 <span class="apii">[-0, +1, <em>e</em>]</span>
4378 Pushes onto the stack a formatted string
4379 and returns a pointer to this string.
4387 the result is a Lua string and Lua takes care of memory allocation
4396 '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
4397 '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
4398 '<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>),
4399 '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
4401 '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and
4402 '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence).
4416 <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
4417 <span class="apii">[-0, +1, –]</span>
4427 <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
4428 <span class="apii">[-0, +1, –]</span>
4438 <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
4439 <span class="apii">[-0, +1, –]</span>
4443 Pushes a light userdata onto the stack.
4448 A <em>light userdata</em> represents a pointer, a <code>void*</code>.
4449 It is a value (like a number):
4452 A light userdata is equal to "any"
4459 <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
4460 <span class="apii">[-0, +1, <em>m</em>]</span>
4464 This macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>,
4465 but should be used only when <code>s</code> is a literal string.
4471 <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
4472 <span class="apii">[-0, +1, <em>m</em>]</span>
4486 Returns a pointer to the internal copy of the string.
4492 <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
4493 <span class="apii">[-0, +1, –]</span>
4497 Pushes a nil value onto the stack.
4503 <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
4504 <span class="apii">[-0, +1, –]</span>
4508 Pushes a float with value <code>n</code> onto the stack.
4514 <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
4515 <span class="apii">[-0, +1, <em>m</em>]</span>
4519 Pushes the zero-terminated string pointed to by <code>s</code>
4527 Returns a pointer to the internal copy of the string.
4537 <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
4538 <span class="apii">[-0, +1, –]</span>
4549 <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
4550 <span class="apii">[-0, +1, –]</span>
4554 Pushes a copy of the element at the given index
4561 <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
4562 <span class="apii">[-0, +1, <em>m</em>]</span>
4568 Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives …
4569 instead of a variable number of arguments.
4575 <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
4576 <span class="apii">[-0, +0, –]</span>
4590 <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
4591 <span class="apii">[-1, +1, –]</span>
4595 Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
4602 <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
4603 <span class="apii">[-0, +1, –]</span>
4620 <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
4621 <span class="apii">[-0, +1, –]</span>
4627 <code>k</code> is the pointer <code>p</code> represented as a light userdata.
4639 <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
4640 <span class="apii">[-0, +0, –]</span>
4656 <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
4657 <span class="apii">[-2, +0, <em>m</em>]</span>
4661 Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
4668 <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
4669 <span class="apii">[-1, +0, <em>m</em>]</span>
4687 <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
4688 <span class="apii">[-1, +0, <em>m</em>]</span>
4694 <code>p</code> is encoded as a light userdata,
4707 <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
4713 The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
4715 <a href="#lua_load"><code>lua_load</code></a> calls the reader,
4717 The reader must return a pointer to a block of memory
4718 with a new piece of the chunk
4729 <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
4730 <span class="apii">[-0, +0, <em>e</em>]</span>
4735 It is defined as a macro:
4745 <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
4746 <span class="apii">[-1, +0, –]</span>
4752 This function cannot be called with a pseudo-index,
4753 because a pseudo-index is not an actual stack position.
4759 <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
4760 <span class="apii">[-1, +0, –]</span>
4773 <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
4774 <span class="apii">[-?, +?, –]</span>
4778 Starts and resumes a coroutine in the given thread <code>L</code>.
4782 To start a coroutine,
4784 then you call <a href="#lua_resume"><code>lua_resume</code></a>,
4787 … it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></…
4789 <a href="#lua_resume"><code>lua_resume</code></a> returns
4790 <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
4791 <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
4793 or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
4804 To resume a coroutine,
4805 you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>,
4808 and then call <a href="#lua_resume"><code>lua_resume</code></a>.
4820 <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
4821 <span class="apii">[-0, +0, –]</span>
4828 for a positive <code>n</code>,
4829 or <code>-n</code> positions in the direction of the bottom,
4830 for a negative <code>n</code>.
4833 This function cannot be called with a pseudo-index,
4834 because a pseudo-index is not an actual stack position.
4840 <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
4841 <span class="apii">[-0, +0, –]</span>
4845 Changes the allocator function of a given state to <code>f</code>
4852 <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
4853 <span class="apii">[-1, +0, <em>e</em>]</span>
4864 As in Lua, this function may trigger a metamethod
4865 for the "newindex" event (see <a href="#2.4">§2.4</a>).
4871 <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
4872 <span class="apii">[-1, +0, <em>e</em>]</span>
4876 Pops a value from the stack and
4883 <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p>
4884 <span class="apii">[-1, +0, <em>e</em>]</span>
4895 As in Lua, this function may trigger a metamethod
4896 for the "newindex" event (see <a href="#2.4">§2.4</a>).
4902 <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
4903 <span class="apii">[-1, +0, –]</span>
4907 Pops a table from the stack and
4914 <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
4915 <span class="apii">[-2, +0, <em>e</em>]</span>
4927 As in Lua, this function may trigger a metamethod
4928 for the "newindex" event (see <a href="#2.4">§2.4</a>).
4934 <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
4935 <span class="apii">[-?, +?, –]</span>
4949 <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
4950 <span class="apii">[-1, +0, –]</span>
4954 Pops a value from the stack and sets it as
4961 <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
4965 An opaque structure that points to a thread and indirectly
4966 (through the thread) to the whole state of a Lua interpreter.
4969 All information about a state is accessible through this structure.
4973 A pointer to this structure must be passed as the first argument to
4974 every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
4975 which creates a Lua state from scratch.
4981 <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
4982 <span class="apii">[-0, +0, –]</span>
4990 The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread,
4992 of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
4993 or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
4997 You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
4998 You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
4999 (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
5000 (to resume a coroutine).
5006 <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p>
5007 <span class="apii">[-0, +1, –]</span>
5011 Converts the zero-terminated string <code>s</code> to a number,
5015 The conversion can result in an integer or a float,
5016 according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>).
5017 The string may have leading and trailing spaces and a sign.
5018 If the string is not a valid numeral,
5020 (Note that the result can be used as a boolean,
5027 <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
5028 <span class="apii">[-0, +0, –]</span>
5032 Converts the Lua value at the given index to a C boolean
5035 <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
5039 use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
5045 <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
5046 <span class="apii">[-0, +0, –]</span>
5050 Converts a value at the given index to a C function.
5051 That value must be a C function;
5058 <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
5059 <span class="apii">[-0, +0, –]</span>
5063 Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equ…
5069 <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
5070 <span class="apii">[-0, +0, –]</span>
5075 to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
5077 or a number or string convertible to an integer (see <a href="#3.4.3">§3.4.3</a>);
5083 its referent is assigned a boolean value that
5090 <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
5091 <span class="apii">[-0, +0, <em>m</em>]</span>
5095 Converts the Lua value at the given index to a C string.
5098 The Lua value must be a string or a number;
5100 If the value is a number,
5102 <em>changes the actual value in the stack to a string</em>.
5103 (This change confuses <a href="#lua_next"><code>lua_next</code></a>
5104 when <code>lua_tolstring</code> is applied to keys during a table traversal.)
5108 <code>lua_tolstring</code> returns a pointer
5109 to a string inside the Lua state.
5110 This string always has a zero ('<code>\0</code>')
5124 <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
5125 <span class="apii">[-0, +0, –]</span>
5129 Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal…
5135 <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
5136 <span class="apii">[-0, +0, –]</span>
5141 to the C type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><co…
5142 The Lua value must be a number or a string convertible to a number
5143 (see <a href="#3.4.3">§3.4.3</a>);
5144 otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns 0.
5149 its referent is assigned a boolean value that
5156 <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
5157 <span class="apii">[-0, +0, –]</span>
5161 Converts the value at the given index to a generic
5163 The value can be a userdata, a table, a thread, or a function;
5176 <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
5177 <span class="apii">[-0, +0, <em>m</em>]</span>
5181 Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal t…
5187 <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
5188 <span class="apii">[-0, +0, –]</span>
5192 Converts the value at the given index to a Lua thread
5194 This value must be a thread;
5201 <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
5202 <span class="apii">[-0, +0, –]</span>
5206 If the value at the given index is a full userdata,
5208 If the value is a light userdata,
5216 <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
5217 <span class="apii">[-0, +0, –]</span>
5222 or <code>LUA_TNONE</code> for a non-valid (but acceptable) index.
5223 The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following cons…
5225 <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a> (0),
5226 <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
5227 <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
5228 <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
5229 <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
5230 <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
5231 <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
5232 <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
5234 <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
5240 <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
5241 <span class="apii">[-0, +0, –]</span>
5246 which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
5252 <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
5256 The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
5262 <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
5263 <span class="apii">[-0, +0, –]</span>
5267 Returns the pseudo-index that represents the <code>i</code>-th upvalue of
5268 the running function (see <a href="#4.4">§4.4</a>).
5274 <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
5275 <span class="apii">[-0, +0, –]</span>
5280 (a C static variable)
5282 When called with a valid <a href="#lua_State"><code>lua_State</code></a>,
5291 <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
5298 The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
5300 <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
5303 and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
5309 any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
5316 <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
5317 <span class="apii">[-?, +?, –]</span>
5332 <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
5333 <span class="apii">[-?, +?, <em>e</em>]</span>
5337 This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5338 but it has no continuation (see <a href="#4.7">§4.7</a>).
5347 <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
5348 <span class="apii">[-?, +?, <em>e</em>]</span>
5355 Yields a coroutine (thread).
5359 When a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5361 and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine retur…
5363 that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
5369 the execution of the C function that yielded (see <a href="#4.7">§4.7</a>).
5373 replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
5376 that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
5385 from inside a line or a count hook (see <a href="#4.9">§4.9</a>).
5387 (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>) and no results,
5396 This function can raise an error if it is called from a thread
5397 with a pending C call with no continuation function,
5398 or it is called from a thread that is not running inside a resume
5407 <h2>4.9 – <a name="4.9">The Debug Interface</a></h2>
5410 Lua has no built-in debugging facilities.
5411 Instead, it offers a special interface
5419 <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
5439 A structure used to carry different pieces of
5440 information about a function or an activation record.
5441 <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
5443 To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
5444 call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5448 The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
5454 If <code>source</code> starts with a '<code>@</code>',
5455 it means that the function was defined in a file where
5457 If <code>source</code> starts with a '<code>=</code>',
5458 the remainder of its contents describe the source in a user-dependent manner.
5460 the function was defined in a string where
5465 a "printable" version of <code>source</code>, to be used in error messages.
5477 the string <code>"Lua"</code> if the function is a Lua function,
5478 <code>"C"</code> if it is a C function,
5479 <code>"main"</code> if it is the main part of a chunk.
5485 <code>currentline</code> is set to -1.
5489 a reasonable name for the given function.
5490 Because functions in Lua are first-class values,
5491 they do not have a fixed name:
5493 while others can be stored only in a table field.
5495 called to find a suitable name.
5496 If it cannot find a name,
5510 true if this function invocation was called by a tail call.
5524 true if the function is a vararg function
5533 <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
5534 <span class="apii">[-0, +0, –]</span>
5544 <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
5545 <span class="apii">[-0, +0, –]</span>
5555 <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
5556 <span class="apii">[-0, +0, –]</span>
5566 <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
5567 <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span>
5571 Gets information about a specific function or function invocation.
5575 To get information about a function invocation,
5576 the parameter <code>ar</code> must be a valid activation record that was
5577 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5578 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5582 To get information about a function, you push it onto the stack
5586 For instance, to know in which line a function <code>f</code> was defined,
5599 a value to be pushed on the stack:
5627 pushes onto the stack a table whose indices are the
5629 (A <em>valid line</em> is a line with some associated code,
5630 that is, a line where you can put a break point.
5631 Non-valid lines include empty lines and comments.)
5649 <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
5650 <span class="apii">[-0, +(0|1), –]</span>
5654 Gets information about a local variable of
5655 a given activation record or a given function.
5660 the parameter <code>ar</code> must be a valid activation record that was
5661 filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5662 given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5664 see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
5669 <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
5690 <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
5691 <span class="apii">[-0, +0, –]</span>
5699 This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
5701 of the function executing at a given level.
5705 When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
5706 when called with a level greater than the stack depth,
5713 <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
5714 <span class="apii">[-0, +(0|1), –]</span>
5718 Gets information about the <code>n</code>-th upvalue
5728 as a name for all upvalues.
5743 <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
5751 Whenever a hook is called, its <code>ar</code> argument has its field
5754 <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKR…
5755 <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>…
5756 and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
5759 the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5764 the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
5769 While Lua is running a hook, it disables other calls to hooks.
5770 Therefore, if a hook calls back Lua to execute a function or a chunk,
5776 that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5777 <a href="#lua_pcallk"><code>lua_pcallk</code></a>, or <a href="#lua_callk"><code>lua_callk</code></…
5783 to yield, a hook function must finish its execution
5784 calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero
5791 <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
5792 <span class="apii">[-0, +0, –]</span>
5802 it is formed by a bitwise OR of the constants
5803 <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
5804 <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
5805 <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
5806 and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
5813 <li><b>The call hook: </b> is called when the interpreter calls a function.
5818 <li><b>The return hook: </b> is called when the interpreter returns from a function.
5825 start the execution of a new line of code,
5827 (This event only happens while Lua is executing a Lua function.)
5832 (This event only happens while Lua is executing a Lua function.)
5838 A hook is disabled by setting <code>mask</code> to zero.
5844 <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
5845 <span class="apii">[-(0|1), +0, –]</span>
5849 Sets the value of a local variable of a given activation record.
5862 …e>ar</code> and <code>n</code> are as in function <a href="#lua_getlocal"><code>lua_getlocal</code…
5868 <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
5869 <span class="apii">[-(0|1), +0, –]</span>
5873 Sets the value of a closure's upvalue.
5885 …ex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</co…
5891 <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
5892 <span class="apii">[-0, +0, –]</span>
5896 Returns a unique identifier for the upvalue numbered <code>n</code>
5901 These unique identifiers allow a program to check whether different
5904 (that is, that access a same external local variable)
5909 …ex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</co…
5916 <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
5917 <span class="apii">[-0, +0, –]</span>
5922 Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
5923 refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
5931 <h1>5 – <a name="5">The Auxiliary Library</a></h1>
5939 the auxiliary library provides higher-level functions for some
5946 have a prefix <code>luaL_</code>.
5960 When a function in the auxiliary library uses less than five slots,
5979 <h2>5.1 – <a name="5.1">Functions and Types</a></h2>
5987 <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
5988 <span class="apii">[-?, +?, <em>m</em>]</span>
5993 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5999 <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
6000 <span class="apii">[-?, +?, <em>m</em>]</span>
6006 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6013 <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
6014 <span class="apii">[-?, +?, –]</span>
6018 Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
6019 a string of length <code>n</code> previously copied to the
6020 buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
6026 <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
6027 <span class="apii">[-?, +?, <em>m</em>]</span>
6031 Adds the zero-terminated string pointed to by <code>s</code>
6033 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6039 <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
6040 <span class="apii">[-1, +?, <em>m</em>]</span>
6046 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6059 <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
6060 <span class="apii">[-0, +0, <em>v</em>]</span>
6068 If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_arge…
6074 <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
6075 <span class="apii">[-0, +0, <em>v</em>]</span>
6079 Raises an error reporting a problem with argument <code>arg</code>
6081 using a standard message
6082 that includes <code>extramsg</code> as a comment:
6093 <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
6097 Type for a <em>string buffer</em>.
6101 A string buffer allows C code to build Lua strings piecemeal.
6106 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code>…
6108 <li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li>
6128 <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code>…
6130 <li>Then initialize it and preallocate a space of
6131 size <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li>
6145 a string buffer uses a variable number of stack slots.
6146 So, while using a buffer, you cannot assume that you know where
6151 when you call a buffer operation,
6154 (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
6155 After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
6163 <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
6164 <span class="apii">[-0, +0, –]</span>
6168 Initializes a buffer <code>B</code>.
6170 the buffer must be declared as a variable
6171 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6177 <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
6178 <span class="apii">[-?, +?, <em>m</em>]</span>
6183 <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_pr…
6189 <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
6190 <span class="apii">[-0, +(0|1), <em>e</em>]</span>
6194 Calls a metamethod.
6198 If the object at index <code>obj</code> has a metatable and this
6199 metatable has a field <code>e</code>,
6210 <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
6211 <span class="apii">[-0, +0, <em>v</em>]</span>
6222 <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
6223 <span class="apii">[-0, +0, <em>v</em>]</span>
6229 and returns this integer cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
6235 <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
6236 <span class="apii">[-0, +0, <em>v</em>]</span>
6240 Checks whether the function argument <code>arg</code> is a string
6247 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
6254 <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
6255 <span class="apii">[-0, +0, <em>v</em>]</span>
6259 Checks whether the function argument <code>arg</code> is a number
6266 <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
6267 <span class="apii">[-0, +0, <em>v</em>]</span>
6274 Checks whether the function argument <code>arg</code> is a string and
6276 (which must be NULL-terminated).
6278 Raises an error if the argument is not a string or
6284 the function uses <code>def</code> as a default value when
6289 This is a useful function for mapping strings to C enums.
6297 <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
6298 <span class="apii">[-0, +0, <em>v</em>]</span>
6311 <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
6312 <span class="apii">[-0, +0, <em>v</em>]</span>
6316 Checks whether the function argument <code>arg</code> is a string
6321 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
6328 <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
6329 <span class="apii">[-0, +0, <em>v</em>]</span>
6334 See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
6340 <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
6341 <span class="apii">[-0, +0, <em>v</em>]</span>
6345 Checks whether the function argument <code>arg</code> is a userdata
6346 of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>…
6347 returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
6353 <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
6354 <span class="apii">[-0, +0, <em>v</em>]</span>
6369 <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
6370 <span class="apii">[-0, +?, <em>e</em>]</span>
6387 <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
6388 <span class="apii">[-0, +?, –]</span>
6405 <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
6406 <span class="apii">[-0, +0, <em>v</em>]</span>
6413 following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
6428 <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
6429 <span class="apii">[-0, +3, <em>m</em>]</span>
6434 process-related functions in the standard library
6435 (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</…
6441 <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
6442 <span class="apii">[-0, +(1|3), <em>m</em>]</span>
6447 file-related functions in the standard library
6448 …<a href="#pdf-io.open"><code>io.open</code></a>, <a href="#pdf-os.rename"><code>os.rename</code></…
6454 <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
6455 <span class="apii">[-0, +(0|1), <em>m</em>]</span>
6461 If the object does not have a metatable,
6469 <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
6470 <span class="apii">[-0, +1, <em>m</em>]</span>
6475 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>)
6483 <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
6484 <span class="apii">[-0, +1, <em>e</em>]</span>
6490 is a table,
6492 Returns true if it finds a previous table there
6493 and false if it creates a new table.
6499 <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
6500 <span class="apii">[-0, +1, <em>m</em>]</span>
6507 Creates a copy of string <code>s</code> by replacing
6516 <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
6517 <span class="apii">[-0, +0, <em>e</em>]</span>
6522 as a number;
6523 it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>).
6531 <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
6532 <span class="apii">[-0, +1, –]</span>
6539 Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> …
6545 <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
6546 <span class="apii">[-0, +1, –]</span>
6554 Loads a buffer as a Lua chunk.
6555 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
6560 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
6563 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
6569 <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
6570 <span class="apii">[-0, +1, <em>m</em>]</span>
6574 Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equa…
6580 <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
6581 <span class="apii">[-0, +1, <em>m</em>]</span>
6586 Loads a file as a Lua chunk.
6587 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
6591 The first line in the file is ignored if it starts with a <code>#</code>.
6595 The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
6599 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
6600 but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
6601 for file-related errors
6606 As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
6613 <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
6614 <span class="apii">[-0, +1, –]</span>
6618 Loads a string as a Lua chunk.
6619 This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
6620 the zero-terminated string <code>s</code>.
6624 This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
6628 Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
6635 <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
6636 <span class="apii">[-0, +1, <em>m</em>]</span>
6640 Creates a new table and registers there
6651 not a pointer to it.
6657 <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
6658 <span class="apii">[-0, +1, <em>m</em>]</span>
6662 Creates a new table with a size optimized
6665 It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></…
6666 (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
6670 It is implemented as a macro.
6672 not a pointer to it.
6678 <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
6679 <span class="apii">[-0, +1, <em>m</em>]</span>
6686 creates a new table to be used as a metatable for userdata,
6690 (The entry <code>__name</code> is used by some error-reporting functions.)
6701 <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
6702 <span class="apii">[-0, +0, –]</span>
6706 Creates a new Lua state.
6707 It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
6709 and then sets a panic function (see <a href="#4.6">§4.6</a>) that prints
6716 or <code>NULL</code> if there is a memory allocation error.
6722 <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
6723 <span class="apii">[-0, +0, <em>e</em>]</span>
6733 <hr><h3><a name="luaL_opt"><code>luaL_opt</code></a></h3><p>
6734 <span class="apii">[-0, +0, <em>e</em>]</span>
6754 <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
6755 <span class="apii">[-0, +0, <em>v</em>]</span>
6772 <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
6773 <span class="apii">[-0, +0, <em>v</em>]</span>
6780 If the function argument <code>arg</code> is a string,
6796 This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
6803 <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
6804 <span class="apii">[-0, +0, <em>v</em>]</span>
6808 If the function argument <code>arg</code> is a number,
6818 <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
6819 <span class="apii">[-0, +0, <em>v</em>]</span>
6825 If the function argument <code>arg</code> is a string,
6835 <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
6836 <span class="apii">[-?, +?, <em>m</em>]</span>
6840 Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
6841 with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
6847 <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
6848 <span class="apii">[-?, +?, <em>m</em>]</span>
6852 Returns an address to a space of size <code>sz</code>
6853 where you can copy a string to be added to buffer <code>B</code>
6854 (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6856 <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
6863 <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
6864 <span class="apii">[-?, +1, <em>m</em>]</span>
6875 <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
6876 <span class="apii">[-?, +1, <em>m</em>]</span>
6880 …nt to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresul…
6886 <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
6887 <span class="apii">[-1, +0, <em>m</em>]</span>
6891 Creates and returns a <em>reference</em>,
6897 A reference is a unique integer key.
6899 <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
6902 Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated obj…
6907 <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>L…
6908 The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
6909 from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
6915 <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
6923 <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
6924 <code>name</code> is the function name and <code>func</code> is a pointer to
6926 Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry
6933 <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
6934 <span class="apii">[-0, +1, <em>e</em>]</span>
6939 …code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</…
6942 as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
6951 Leaves a copy of the module on the stack.
6957 <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
6958 <span class="apii">[-nup, +0, <em>m</em>]</span>
6963 (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
6978 <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
6979 <span class="apii">[-0, +0, –]</span>
6985 in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6991 <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3>
7003 A file handle is implemented as a full userdata,
7004 with a metatable called <code>LUA_FILEHANDLE</code>
7005 (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name).
7007 (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
7015 Field <code>closef</code> points to a Lua function
7029 <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
7030 <span class="apii">[-0, +0, <em>m</em>]</span>
7034 This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
7042 <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
7043 <span class="apii">[-0, +1, <em>e</em>]</span>
7047 Converts any Lua value at the given index to a C string
7048 in a reasonable format.
7056 If the value has a metatable with a <code>__tostring</code> field,
7065 <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
7066 <span class="apii">[-0, +1, <em>m</em>]</span>
7071 Creates and pushes a traceback of the stack <code>L1</code>.
7081 <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
7082 <span class="apii">[-0, +0, –]</span>
7092 <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
7093 <span class="apii">[-0, +0, –]</span>
7098 (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
7105 …f <code>ref</code> is <a href="#pdf-LUA_NOREF"><code>LUA_NOREF</code></a> or <a href="#pdf-LUA_REF…
7106 <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
7112 <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
7113 <span class="apii">[-0, +1, <em>m</em>]</span>
7117 Pushes onto the stack a string identifying the current position
7130 This function is used to build a prefix for error messages.
7138 <h1>6 – <a name="6">Standard Libraries</a></h1>
7144 (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable…
7148 deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>).
7158 <li>basic library (<a href="#6.1">§6.1</a>);</li>
7160 <li>coroutine library (<a href="#6.2">§6.2</a>);</li>
7162 <li>package library (<a href="#6.3">§6.3</a>);</li>
7164 <li>string manipulation (<a href="#6.4">§6.4</a>);</li>
7166 <li>basic UTF-8 support (<a href="#6.5">§6.5</a>);</li>
7168 <li>table manipulation (<a href="#6.6">§6.6</a>);</li>
7170 <li>mathematical functions (<a href="#6.7">§6.7</a>) (sin, log, etc.);</li>
7172 <li>input and output (<a href="#6.8">§6.8</a>);</li>
7174 <li>operating system facilities (<a href="#6.9">§6.9</a>);</li>
7176 <li>debug facilities (<a href="#6.10">§6.10</a>).</li>
7180 each library provides all its functions as fields of a global table
7186 the C host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> fun…
7190 <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call
7191 <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library),
7192 <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library),
7193 <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library),
7194 <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library),
7195 <a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF8 library),
7196 <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library),
7197 <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library),
7198 <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library),
7199 <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library),
7200 and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library).
7201 These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>.
7205 <h2>6.1 – <a name="6.1">Basic Functions</a></h2>
7215 <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
7219 Calls <a href="#pdf-error"><code>error</code></a> if
7230 <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3>
7234 This function is a generic interface to the garbage collector.
7240 performs a full garbage-collection cycle.
7247 until a call to restart it.
7256 The value has a fractional part,
7263 performs a garbage-collection step.
7265 With a zero value,
7267 For non-zero values,
7270 Returns <b>true</b> if the step finished a collection cycle.
7275 the collector (see <a href="#2.5">§2.5</a>).
7281 the collector (see <a href="#2.5">§2.5</a>).
7286 returns a boolean that tells whether the collector is running
7295 <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3>
7296 Opens the named file and executes its contents as a Lua chunk.
7307 <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
7315 at the beginning of the message, if the message is a string.
7321 Passing a level 0 avoids the addition of error position information
7328 <hr><h3><a name="pdf-_G"><code>_G</code></a></h3>
7329 A global variable (not a function) that
7330 holds the global environment (see <a href="#2.2">§2.2</a>).
7339 <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3>
7343 If <code>object</code> does not have a metatable, returns <b>nil</b>.
7345 if the object's metatable has a <code>__metatable</code> field,
7353 <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3>
7371 <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3>
7375 Loads a chunk.
7379 If <code>chunk</code> is a string, the chunk is this string.
7380 If <code>chunk</code> is a function,
7382 Each call to <code>chunk</code> must return a string that concatenates
7384 A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
7389 returns the compiled chunk as a function;
7399 (When you load a main chunk,
7401 the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).
7403 when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.d…
7411 and debug information (see <a href="#4.9">§4.9</a>).
7413 it defaults to <code>chunk</code>, if <code>chunk</code> is a string,
7419 (that is, a precompiled chunk).
7435 <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3>
7439 Similar to <a href="#pdf-load"><code>load</code></a>,
7448 <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
7452 Allows a program to traverse all fields of a table.
7453 Its first argument is a table and its second argument
7465 you can use <code>next(t)</code> to check whether a table is empty.
7471 (To traverse a table in numerical order,
7472 use a numerical <b>for</b>.)
7478 you assign any value to a non-existent field in the table.
7486 <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3>
7490 If <code>t</code> has a metamethod <code>__pairs</code>,
7497 returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</co…
7507 See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
7514 <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3>
7522 and returns a status code.
7523 Its first result is the status code (a boolean),
7533 <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3>
7536 using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a …
7538 but only as a quick way to show a value,
7541 use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>i…
7547 <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3>
7550 Returns a boolean.
7556 <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3>
7559 <code>table</code> must be a table;
7566 <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3>
7568 which must be a table or a string,
7576 <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
7579 <code>table</code> must be a table,
7591 <hr><h3><a name="pdf-select"><code>select (index, ···)</code></a></h3>
7595 If <code>index</code> is a number,
7597 a negative number indexes from the end (-1 is the last argument).
7605 <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3>
7611 you must use the debug library (<a href="#6.10">§6.10</a>).)
7614 If the original metatable has a <code>__metatable</code> field,
7625 <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
7630 <code>tonumber</code> tries to convert its argument to a number.
7631 If the argument is already a number or
7632 a string convertible to a number,
7639 according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>).
7640 (The string may have leading and trailing spaces and a sign.)
7645 then <code>e</code> must be a string to be interpreted as
7648 In bases above 10, the letter '<code>A</code>' (in either upper or lower case)
7651 If the string <code>e</code> is not a valid numeral in the given base,
7658 <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3>
7659 Receives a value of any type and
7660 converts it to a string in a human-readable format.
7662 use <a href="#pdf-string.format"><code>string.format</code></a>.)
7666 If the metatable of <code>v</code> has a <code>__tostring</code> field,
7675 <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3>
7676 Returns the type of its only argument, coded as a string.
7678 "<code>nil</code>" (a string, not the value <b>nil</b>),
7691 <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3>
7695 A global variable (not a function) that
7696 holds a string containing the running Lua version.
7703 <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></…
7707 This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
7708 except that it sets a new message handler <code>msgh</code>.
7716 <h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2>
7720 which come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>.
7721 See <a href="#2.6">§2.6</a> for a general description of coroutines.
7725 <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3>
7729 Creates a new coroutine, with body <code>f</code>.
7730 <code>f</code> must be a function.
7738 <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3>
7746 A running coroutine is yieldable if it is not the main thread and
7747 it is not inside a non-yieldable C function.
7753 <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···…
7758 The first time you resume a coroutine,
7780 <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3>
7784 Returns the running coroutine plus a boolean,
7791 <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3>
7795 Returns the status of coroutine <code>co</code>, as a string:
7798 <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>,
7809 <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3>
7813 Creates a new coroutine, with body <code>f</code>.
7814 <code>f</code> must be a function.
7815 Returns a function that resumes the coroutine each time it is called.
7826 <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></…
7839 <h2>6.3 – <a name="6.3">Modules</a></h2>
7845 <a href="#pdf-require"><code>require</code></a>.
7846 Everything else is exported in a table <a name="pdf-package"><code>package</code></a>.
7850 <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
7855 The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></…
7859 Otherwise, it tries to find a <em>loader</em> for the module.
7863 To find a loader,
7864 <code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</cod…
7866 we can change how <code>require</code> looks for a module.
7868 for <a href="#pdf-package.searchers"><code>package.searchers</code></a>.
7873 If it has a value,
7874 this value (which must be a function) is the loader.
7875 Otherwise <code>require</code> searches for a Lua loader using the
7876 path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
7877 If that also fails, it searches for a C loader using the
7878 path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
7880 it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searcher…
7884 Once a loader is found,
7887 (If the loader came from a file,
7889 If the loader returns any non-nil value,
7891 If the loader does not return a non-nil value and
7907 <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3>
7911 A string describing some compile-time configurations for packages.
7912 This string is a sequence of lines:
7919 <li>The second line is the character that separates templates in a path.
7923 substitution points in a template.
7926 <li>The fourth line is a string that, in a path in Windows,
7930 <li>The fifth line is a mark to ignore all text after it
7932 Default is '<code>-</code>'.</li>
7939 <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
7943 The path used by <a href="#pdf-require"><code>require</code></a> to search for a C loader.
7947 Lua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the …
7948 it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
7949 using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>,
7950 or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>,
7951 or a default path defined in <code>luaconf.h</code>.
7957 <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
7961 A table used by <a href="#pdf-require"><code>require</code></a> to control which
7963 When you require a module <code>modname</code> and
7965 <a href="#pdf-require"><code>require</code></a> simply returns the value stored there.
7969 This variable is only a reference to the real table;
7971 table used by <a href="#pdf-require"><code>require</code></a>.
7977 <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
7990 it looks for a function <code>funcname</code> inside the library
7991 and returns this function as a C function.
7992 So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> p…
7993 (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
7997 This is a low-level function.
7999 Unlike <a href="#pdf-require"><code>require</code></a>,
8003 including if necessary a path and an extension.
8018 <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
8022 The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader.
8026 At start-up, Lua initializes this variable with
8027 the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
8028 the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
8029 with a default path defined in <code>luaconf.h</code>,
8038 <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
8042 A table to store loaders for specific modules
8043 (see <a href="#pdf-require"><code>require</code></a>).
8047 This variable is only a reference to the real table;
8049 table used by <a href="#pdf-require"><code>require</code></a>.
8055 <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3>
8059 A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules.
8063 Each entry in this table is a <em>searcher function</em>.
8064 When looking for a module,
8065 <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
8066 with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
8070 or a string explaining why it did not find that module
8079 The first searcher simply looks for a loader in the
8080 <a href="#pdf-package.preload"><code>package.preload</code></a> table.
8084 The second searcher looks for a loader as a Lua library,
8085 using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
8086 …e search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpa…
8090 The third searcher looks for a loader as a C library,
8091 using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
8093 …e search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpa…
8103 Once it finds a C library,
8104 this searcher first uses a dynamic link facility to link the
8106 Then it tries to find a C function inside the library to
8109 concatenated with a copy of the module name where each dot
8111 Moreover, if the module name has a hyphen,
8113 For instance, if the module name is <code>a.b.c-v2.1</code>,
8118 The fourth searcher tries an <em>all-in-one loader</em>.
8119 It searches the C path for a library for
8121 For instance, when requiring <code>a.b.c</code>,
8122 it will search for a C library for <code>a</code>.
8126 With this facility, a package can pack several C submodules
8134 as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
8141 <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</cod…
8149 A path is a string containing a sequence of
8153 in the template with a copy of <code>name</code>
8155 (a dot, by default)
8167 the search for the name <code>foo.a</code>
8169 <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and
8170 <code>/usr/local/foo/a/init.lua</code>, in that order.
8185 <h2>6.4 – <a name="6.4">String Manipulation</a></h2>
8190 When indexing a string in Lua, the first character is at position 1
8194 Thus, the last character is at position -1, and so on.
8199 <a name="pdf-string"><code>string</code></a>.
8200 It also sets a metatable for strings
8202 Therefore, you can use the string functions in object-oriented style.
8208 The string library assumes one-byte character encodings.
8212 <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
8218 following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>.
8228 <hr><h3><a name="pdf-string.char"><code>string.char (···)</code></a></h3>
8230 Returns a string with length equal to the number of arguments,
8242 <hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3>
8246 Returns a string containing a binary representation
8247 (a <em>binary chunk</em>)
8249 so that a later <a href="#pdf-load"><code>load</code></a> on this string returns
8250 a copy of the function (but with new upvalues).
8251 If <code>strip</code> is a true value,
8262 and reload the upvalues of a function
8263 in a way adequate to your needs.)
8269 <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
8274 <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>.
8275 If it finds a match, then <code>find</code> returns the indices of <code>s</code>
8278 A third, optional numeric argument <code>init</code> specifies
8281 A value of <b>true</b> as a fourth, optional argument <code>plain</code>
8283 so the function does a plain "find substring" operation,
8290 then in a successful match
8298 <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, ···)</c…
8302 Returns a formatted version of its variable number of arguments
8303 following the description given in its first argument (which must be a string).
8312 The <code>q</code> option formats a string between double quotes,
8318 string.format('%q', 'a string with "quotes" and \n new line')
8323 "a string with \"quotes\" and \
8329 <code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>,
8330 <code>G</code>, and <code>g</code> all expect a number as argument.
8334 When Lua is compiled with a C89 compiler,
8335 options <code>A</code> and <code>a</code> (hexadecimal floats)
8340 Option <code>s</code> expects a string;
8341 if its argument is not a string,
8342 it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a…
8350 <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
8353 returns the next captures from <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>)
8366 for w in string.gmatch(s, "%a+") do
8371 given string into a table:
8382 For this function, a caret '<code>^</code>' at the start of a pattern does not
8389 <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
8390 Returns a copy of <code>s</code>
8392 occurrences of the <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) have been
8393 replaced by a replacement string specified by <code>repl</code>,
8394 which can be a string, a table, or a function.
8401 If <code>repl</code> is a string, then its value is used for replacement.
8405 stands for the value of the <em>d</em>-th captured substring.
8407 The sequence <code>%%</code> stands for a single <code>%</code>.
8411 If <code>repl</code> is a table, then the table is queried for every match,
8416 If <code>repl</code> is a function, then this function is called every time a
8424 then it behaves as if the whole pattern was inside a capture.
8429 is a string or a number,
8441 --> x="hello hello world world"
8444 --> x="hello hello world"
8447 --> x="world hello Lua from"
8450 --> x="home = /home/roberto, user = roberto"
8452 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
8455 --> x="4+5 = 9"
8458 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
8459 --> x="lua-5.3.tar.gz"
8465 <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3>
8466 Receives a string and returns its length.
8469 so <code>"a\000bc\000"</code> has length 5.
8475 <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3>
8476 Receives a string and returns a copy of this string with all
8485 <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
8487 <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>.
8493 A third, optional numeric argument <code>init</code> specifies
8501 <hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, ···)</code><…
8505 Returns a binary string containing the values <code>v1</code>, <code>v2</code>, etc.
8507 according to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>).
8513 <hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3>
8517 Returns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a>
8519 The format string cannot have the variable-length options
8520 '<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">§6.4.2</a>).
8526 <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
8527 Returns a string that is the concatenation of <code>n</code> copies of
8536 with a single call to this function.)
8542 <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3>
8543 Returns a string that is the string <code>s</code> reversed.
8549 <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3>
8553 If <code>j</code> is absent, then it is assumed to be equal to -1
8556 the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
8558 and <code>string.sub(s, -i)</code> (for a positive <code>i</code>)
8559 returns a suffix of <code>s</code>
8577 <hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3>
8581 … the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</cod…
8582 according to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>).
8592 <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3>
8593 Receives a string and returns a copy of this string with all
8596 The definition of what a lowercase letter is depends on the current locale.
8602 <h3>6.4.1 – <a name="6.4.1">Patterns</a></h3>
8606 which are interpreted as patterns by the pattern-matching functions
8607 <a href="#pdf-string.find"><code>string.find</code></a>,
8608 <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>,
8609 <a href="#pdf-string.gsub"><code>string.gsub</code></a>,
8610 and <a href="#pdf-string.match"><code>string.match</code></a>.
8617 A <em>character class</em> is used to represent a set of characters.
8618 The following combinations are allowed in describing a character class:
8624 <code>^$()%.[]*+-?</code>)
8628 <li><b><code>.</code>: </b> (a dot) represents all characters.</li>
8630 <li><b><code>%a</code>: </b> represents all letters.</li>
8650 <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character)
8653 Any non-alphanumeric character
8654 (including all punctuation characters, even the non-magical)
8655 can be preceded by a '<code>%</code>'
8656 when used to represent itself in a pattern.
8662 A range of characters can be specified by
8664 in ascending order, with a '<code>-</code>'.
8670 <code>[0-7]</code> represents the octal digits,
8671 and <code>[0-7%l%-]</code> represents the octal digits plus
8672 the lowercase letters plus the '<code>-</code>' character.
8676 You can put a closing square bracket in a set
8678 You can put a hyphen in a set
8685 Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>
8695 For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),
8697 For instance, <code>%S</code> represents all non-space characters.
8703 In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.
8710 A <em>pattern item</em> can be
8715 a single character class,
8720 a single character class followed by '<code>*</code>',
8726 a single character class followed by '<code>+</code>',
8732 a single character class followed by '<code>-</code>',
8739 a single character class followed by '<code>?</code>',
8740 which matches zero or one occurrence of a character in the class.
8746 such item matches a substring equal to the <em>n</em>-th captured string
8755 counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,
8762 <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>;
8777 A <em>pattern</em> is a sequence of pattern items.
8778 A caret '<code>^</code>' at the beginning of a pattern anchors the match at the
8780 A '<code>$</code>' at the end of a pattern anchors the match at the
8790 A pattern can contain sub-patterns enclosed in parentheses;
8792 When a match succeeds, the substrings of the subject string
8795 For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>,
8796 the part of the string matching <code>"a*(.)%w(%s*)"</code> is
8803 As a special case, the empty capture <code>()</code> captures
8804 the current string position (a number).
8814 <h3>6.4.2 – <a name="6.4.2">Format Strings for Pack and Unpack</a></h3>
8817 The first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>,
8818 <a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><…
8819 is a format string,
8824 A format string is a sequence of conversion options.
8833 <li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li>
8835 <li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li>
8837 <li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li>
8839 <li><b><code>j</code>: </b>a <code>lua_Integer</code></li>
8840 <li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li>
8841 <li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li>
8842 <li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes
8846 <li><b><code>f</code>: </b>a <code>float</code> (native size)</li>
8847 <li><b><code>d</code>: </b>a <code>double</code> (native size)</li>
8848 <li><b><code>n</code>: </b>a <code>lua_Number</code></li>
8849 <li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li>
8850 <li><b><code>z</code>: </b>a zero-terminated string</li>
8851 <li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length
8853 (default is a <code>size_t</code>)</li>
8860 (A "<code>[<em>n</em>]</code>" means an optional integral numeral.)
8863 each option corresponds to an argument (in <a href="#pdf-string.pack"><code>string.pack</code></a>)
8864 or a result (in <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
8871 <a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the …
8872 <a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a…
8886 at an offset that is a multiple of the minimum between the
8888 this minimum must be a power of 2.
8894 All padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a>
8895 (and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
8903 <h2>6.5 – <a name="6.5">UTF-8 Support</a></h2>
8906 This library provides basic support for UTF-8 encoding.
8907 It provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>.
8910 Any operation that needs the meaning of a character,
8916 all functions that expect a byte position as a parameter
8917 assume that the given position is either the start of a byte sequence
8924 <hr><h3><a name="pdf-utf8.char"><code>utf8.char (···)</code></a></h3>
8926 converts each one to its corresponding UTF-8 byte sequence
8927 and returns a string with the concatenation of all these sequences.
8933 <hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3>
8934 The pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xF4][\x80-\xBF]*</code>"
8935 (see <a href="#6.4.1">§6.4.1</a>),
8936 which matches exactly one UTF-8 byte sequence,
8937 assuming that the subject is a valid UTF-8 string.
8943 <hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s)</code></a></h3>
8961 <hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j]])</code></a></h3>
8971 <hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3>
8972 Returns the number of UTF-8 characters in string <code>s</code>
8974 The default for <code>i</code> is 1 and for <code>j</code> is -1.
8976 returns a false value plus the position of the first invalid byte.
8982 <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
8984 <code>n</code>-th character of <code>s</code>
8986 A negative <code>n</code> gets characters before position <code>i</code>.
8987 The default for <code>i</code> is 1 when <code>n</code> is non-negative
8989 so that <code>utf8.offset(s, -n)</code> gets the offset of the
8990 <code>n</code>-th character from the end of the string.
8997 As a special case,
8999 of the character that contains the <code>i</code>-th byte of <code>s</code>.
9003 This function assumes that <code>s</code> is a valid UTF-8 string.
9011 <h2>6.6 – <a name="6.6">Table Manipulation</a></h2>
9015 It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
9019 Remember that, whenever an operation needs the length of a table,
9020 all caveats about the length operator apply (see <a href="#3.4.7">§3.4.7</a>).
9021 All functions ignore non-numeric keys
9026 <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
9030 Given a list where all elements are strings or numbers,
9041 <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
9049 so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
9056 <hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3>
9066 The number of elements to be moved must fit in a Lua integer.
9076 <hr><h3><a name="pdf-table.pack"><code>table.pack (···)</code></a></h3>
9080 Returns a new table with all arguments stored into keys 1, 2, etc.
9081 and with a field "<code>n</code>" with the total number of arguments.
9082 Note that the resulting table may not be a sequence.
9088 <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3>
9105 so that a call <code>table.remove(l)</code> removes the last element
9112 <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3>
9116 Sorts list elements in a given order, <em>in-place</em>,
9119 then it must be a function that receives two list elements
9130 a strict partial order over the elements in the list;
9144 <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3>
9162 <h2>6.7 – <a name="6.7">Mathematical Functions</a></h2>
9166 …provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
9171 …a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</co…
9173 or a float otherwise.
9177 <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3>
9187 <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3>
9197 <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3>
9207 <hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3>
9227 <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
9237 <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
9247 <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3>
9257 <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
9268 <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3>
9278 <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3>
9289 <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3>
9294 a value larger than any other numeric value.
9300 <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
9312 <hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3>
9323 <hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3>
9330 <hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3>
9341 <hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
9348 <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
9353 Its second result is always a float.
9359 <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
9369 <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3>
9379 <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
9384 returns a pseudo-random float with uniform distribution
9387 <code>math.random</code> returns a pseudo-random integer
9389 (The value <em>n-m</em> cannot be negative and must fit in a Lua integer.)
9395 pseudo-random generator function provided by C.
9401 <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3>
9406 for the pseudo-random generator:
9413 <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3>
9423 <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
9434 <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3>
9444 <hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3>
9456 <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
9461 "<code>float</code>" if it is a float,
9462 or <b>nil</b> if <code>x</code> is not a number.
9468 <hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3>
9472 Returns a boolean,
9482 <h2>6.8 – <a name="6.8">Input and Output Facilities</a></h2>
9487 that is, there are operations to set a default input file and a
9495 all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
9497 the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle
9504 …a name="pdf-io.stdin"><code>io.stdin</code></a>, <a name="pdf-io.stdout"><code>io.stdout</code></a…
9511 (plus an error message as a second result and
9512 a system-dependent error code as a third result)
9514 In non-POSIX systems,
9522 <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
9527 Without a <code>file</code>, closes the default output file.
9533 <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
9543 <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
9547 When called with a file name, it opens the named file (in text mode),
9549 When called with a file handle,
9563 <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename, ···])</code></a></h3>
9589 <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
9593 This function opens a file,
9596 it returns a new file handle.
9605 <li><b>"<code>a</code>": </b> append mode;</li>
9608 <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved,
9611 The <code>mode</code> string can also have a '<code>b</code>' at the end,
9618 <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
9622 Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output …
9628 <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
9637 Starts program <code>prog</code> in a separated process and returns
9638 a file handle that you can use to read data from this program
9647 <hr><h3><a name="pdf-io.read"><code>io.read (···)</code></a></h3>
9657 <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
9662 returns a handle for a temporary file.
9670 <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
9674 Checks whether <code>obj</code> is a valid file handle.
9676 <code>"closed file"</code> if <code>obj</code> is a closed file handle,
9677 or <b>nil</b> if <code>obj</code> is not a file handle.
9683 <hr><h3><a name="pdf-io.write"><code>io.write (···)</code></a></h3>
9693 <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
9704 When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
9705 <a href="#pdf-file:close"><code>file:close</code></a> returns the same values
9706 returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
9712 <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
9722 <hr><h3><a name="pdf-file:lines"><code>file:lines (···)</code></a></h3>
9730 uses "<code>l</code>" as a default.
9738 Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
9750 <hr><h3><a name="pdf-file:read"><code>file:read (···)</code></a></h3>
9757 the function returns a string or a number with the characters read,
9762 it uses a default format that reads the next line
9772 reads a numeral and returns it as a float or an integer,
9774 (The numeral may have leading spaces and a sign.)
9776 is a valid prefix for a numeral;
9777 if that prefix does not form a valid numeral
9778 (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
9782 <li><b>"<code>a</code>": </b>
9794 reads the next line keeping the end-of-line character (if present),
9799 reads a string with up to this number of bytes,
9813 <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3>
9819 to the position given by <code>offset</code> plus a base
9830 plus a string describing the error.
9847 <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
9863 you explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>).
9867 line buffering; output is buffered until a newline is output
9869 (such as a terminal device).
9881 <hr><h3><a name="pdf-file:write"><code>file:write (···)</code></a></h3>
9891 Otherwise it returns <b>nil</b> plus a string describing the error.
9899 <h2>6.9 – <a name="6.9">Operating System Facilities</a></h2>
9902 This library is implemented through table <a name="pdf-os"><code>os</code></a>.
9906 <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
9917 <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
9921 Returns a string or a table containing date and time,
9928 (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
9937 then <code>date</code> returns a table with the following fields:
9942 and <code>isdst</code> (daylight saving flag, a boolean).
9949 then <code>date</code> returns the date as a string,
9955 <code>date</code> returns a reasonable date and time representation that depends on
9961 In non-POSIX systems,
9969 <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
9975 (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>).
9977 this value is exactly <code>t2</code><em>-</em><code>t1</code>.
9983 <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
9993 the function returns a string plus a number,
10004 the command was terminated by a signal;
10011 When called without a <code>command</code>,
10012 <code>os.execute</code> returns a boolean that is true if a shell is available.
10018 <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
10027 if <code>code</code> is a number,
10040 <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
10051 <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
10058 plus a string describing the error and the error code.
10065 <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
10071 plus a string describing the error and the error code.
10078 <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
10083 <code>locale</code> is a system-dependent string specifying a locale;
10094 the current locale is set to an implementation-defined native locale.
10113 <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
10118 or a time representing the local date and time specified by the given table.
10126 For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
10131 For instance, if <code>sec</code> is -10,
10132 it means -10 seconds from the time specified by the other fields;
10138 The returned value is a number, whose meaning depends on your system.
10144 <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</c…
10150 <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
10154 Returns a string with a file name that can
10155 be used for a temporary file.
10162 this function also creates a file with that name,
10172 you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
10181 <h2>6.10 – <a name="6.10">The Debug Library</a></h2>
10185 the functionality of the debug interface (<a href="#4.9">§4.9</a>) to Lua programs.
10189 (e.g., that variables local to a function
10199 inside the <a name="pdf-debug"><code>debug</code></a> table.
10200 All functions that operate over a thread
10207 <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
10216 A line containing only the word <code>cont</code> finishes this function,
10228 <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
10235 (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function).
10241 <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
10245 Returns a table with information about a function.
10247 or you can give a number as the value of <code>f</code>,
10254 If <code>f</code> is a number larger than the number of active functions,
10259 …urned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code>…
10265 adds a field named <code>func</code> with the function itself.
10268 adds a field named <code>activelines</code> with the table of
10274 a name for the current function,
10275 if a reasonable name can be found,
10277 returns a table with all available information
10278 about the <a href="#pdf-print"><code>print</code></a> function.
10284 <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3>
10300 -1 is the first vararg argument.
10302 and raises an error when called with a level out of range.
10303 (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the leve…
10314 The parameter <code>f</code> may also be a function.
10321 <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3>
10326 or <b>nil</b> if it does not have a metatable.
10332 <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
10336 Returns the registry table (see <a href="#4.5">§4.5</a>).
10342 <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3>
10360 <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3>
10365 If <code>u</code> is not a full userdata,
10372 <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a>…
10376 Sets the given function as a hook.
10383 <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li>
10384 <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li>
10385 <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li>
10388 with a <code>count</code> different from zero,
10394 <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
10398 When the hook is called, its first argument is a string
10405 Inside a hook,
10415 <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a…
10423 and raises an error when called with a <code>level</code> out of range.
10429 See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
10436 <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
10448 <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
10462 <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3>
10468 <code>udata</code> must be a full userdata.
10478 <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code><…
10482 If <code>message</code> is present but is neither a string nor <b>nil</b>,
10485 it returns a string with a traceback of the call stack.
10496 <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3>
10500 Returns a unique identifier (as a light userdata)
10506 These unique identifiers allow a program to check whether different
10509 (that is, that access a same external local variable)
10516 <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
10520 Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
10521 refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
10529 <h1>7 – <a name="7">Lua Standalone</a></h1>
10533 to be embedded in a host C program,
10534 it is also frequently used as a standalone language.
10535 An interpreter for Lua as a standalone language,
10548 <li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li>
10549 <li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em> and assigns the
10551 <li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li>
10552 <li><b><code>-v</code>: </b> prints version information;</li>
10553 <li><b><code>-E</code>: </b> ignores environment variables;</li>
10554 <li><b><code>--</code>: </b> stops handling options;</li>
10555 <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li>
10559 <code>lua</code> behaves as <code>lua -v -i</code>
10560 when the standard input (<code>stdin</code>) is a terminal,
10561 and as <code>lua -</code> otherwise.
10565 When called without option <code>-E</code>,
10566 … interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</cod…
10567 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
10575 When called with option <code>-E</code>,
10580 <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>pa…
10585 All options are handled in order, except <code>-i</code> and <code>-E</code>.
10589 $ lua -e'a=1' -e 'print(a)' script.lua
10591 will first set <code>a</code> to 1, then print the value of <code>a</code>,
10598 <code>lua</code> collects all command-line arguments
10599 in a global table called <code>arg</code>.
10609 $ lua -la b.lua t1 t2
10614 arg = { [-2] = "lua", [-1] = "-la",
10624 $ lua -e "print(arg[1])"
10626 will print "<code>-e</code>".
10627 If there is a script,
10631 the script is compiled as a vararg function.)
10636 Lua repeatedly prompts and waits for a line.
10637 After reading a line,
10640 Otherwise, it interprets the line as a statement.
10643 by issuing a different prompt.
10647 If the global variable <a name="pdf-_PROMPT"><code>_PROMPT</code></a> contains a string,
10649 Similarly, if the global variable <a name="pdf-_PROMPT2"><code>_PROMPT2</code></a> contains a strin…
10657 If the error object is not a string but
10658 has a metamethod <code>__tostring</code>,
10660 Otherwise, the interpreter converts the error object to a string
10661 and adds a stack traceback to it.
10667 (see <a href="#lua_close"><code>lua_close</code></a>).
10669 calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
10673 To allow the use of Lua as a
10676 the first line of a chunk if it starts with <code>#</code>.
10692 is a more portable solution.)
10696 <h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1>
10699 Here we list the incompatibilities that you may find when moving a program
10709 do not imply source-code changes in a program,
10716 using a new version.
10731 <h2>8.1 – <a name="8.1">Changes in the Language</a></h2>
10744 You can fix these differences by forcing a number to be a float
10747 or using <code>x = x + 0.0</code> to convert a variable.
10748 (This recommendation is only for a quick fix
10750 it is not a general guideline for good programming.
10757 The conversion of a float to a string now adds a <code>.0</code> suffix
10762 when you need a specific format for numbers.
10768 but some programs assumed a specific format.)
10781 <h2>8.2 – <a name="8.2">Changes in the Libraries</a></h2>
10786 It is easy to require a compatible external library or,
10788 (Keep in mind that <code>bit32</code> operates on 32-bit integers,
10799 The <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and
10804 Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</co…
10822 The searcher for C loaders used by <a href="#pdf-require"><code>require</code></a>
10843 <h2>8.3 – <a name="8.3">Changes in the API</a></h2>
10856 Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
10865 Use their signed equivalents with a type cast.
10869 Macros to project non-default integer types
10872 Use their equivalent over <a href="#lua_Integer"><code>lua_Integer</code></a> with a type cast
10873 (or, when possible, use <a href="#lua_Integer"><code>lua_Integer</code></a> in your code).
10881 <h1>9 – <a name="9">The Complete Syntax of Lua</a></h1>
10886 {A} means 0 or more As,
10887 and [A] means an optional A.
10888 (For operator precedences, see <a href="#3.4.8">§3.4.8</a>;
10889 for a description of the terminals
10891 and LiteralString, see <a href="#3.1">§3.1</a>.)
10955 …binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/<…
10960 unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | ‘<b>~</b>’
10975 Tue Jun 26 13:16:37 -03 2018
10977 <!--
10979 -->