manual.html revision 1.5 1 1.1 mbalmer <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 1.4 mbalmer <HTML>
3 1.4 mbalmer <HEAD>
4 1.4 mbalmer <TITLE>Lua 5.3 Reference Manual</TITLE>
5 1.4 mbalmer <LINK REL="stylesheet" TYPE="text/css" HREF="lua.css">
6 1.4 mbalmer <LINK REL="stylesheet" TYPE="text/css" HREF="manual.css">
7 1.1 mbalmer <META HTTP-EQUIV="content-type" CONTENT="text/html; charset=iso-8859-1">
8 1.4 mbalmer </HEAD>
9 1.1 mbalmer
10 1.4 mbalmer <BODY>
11 1.1 mbalmer
12 1.4 mbalmer <H1>
13 1.4 mbalmer <A HREF="http://www.lua.org/"><IMG SRC="logo.gif" ALT="Lua"></A>
14 1.2 lneto Lua 5.3 Reference Manual
15 1.4 mbalmer </H1>
16 1.1 mbalmer
17 1.4 mbalmer <P>
18 1.1 mbalmer by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes
19 1.4 mbalmer
20 1.4 mbalmer <P>
21 1.4 mbalmer <SMALL>
22 1.3 lneto Copyright © 2015 Lua.org, PUC-Rio.
23 1.1 mbalmer Freely available under the terms of the
24 1.2 lneto <a href="http://www.lua.org/license.html">Lua license</a>.
25 1.4 mbalmer </SMALL>
26 1.1 mbalmer
27 1.4 mbalmer <DIV CLASS="menubar">
28 1.4 mbalmer <A HREF="contents.html#contents">contents</A>
29 1.4 mbalmer ·
30 1.4 mbalmer <A HREF="contents.html#index">index</A>
31 1.1 mbalmer ·
32 1.4 mbalmer <A HREF="http://www.lua.org/manual/">other versions</A>
33 1.4 mbalmer </DIV>
34 1.1 mbalmer
35 1.1 mbalmer <!-- ====================================================================== -->
36 1.1 mbalmer <p>
37 1.1 mbalmer
38 1.5 lneto <!-- Id: manual.of,v 1.153 2015/11/25 16:57:42 roberto Exp -->
39 1.1 mbalmer
40 1.1 mbalmer
41 1.1 mbalmer
42 1.1 mbalmer
43 1.2 lneto <h1>1 – <a name="1">Introduction</a></h1>
44 1.1 mbalmer
45 1.1 mbalmer <p>
46 1.1 mbalmer Lua is an extension programming language designed to support
47 1.1 mbalmer general procedural programming with data description
48 1.1 mbalmer facilities.
49 1.3 lneto Lua also offers good support for object-oriented programming,
50 1.1 mbalmer functional programming, and data-driven programming.
51 1.2 lneto Lua is intended to be used as a powerful, lightweight,
52 1.2 lneto embeddable scripting language for any program that needs one.
53 1.2 lneto Lua is implemented as a library, written in <em>clean C</em>,
54 1.2 lneto the common subset of Standard C and C++.
55 1.1 mbalmer
56 1.1 mbalmer
57 1.1 mbalmer <p>
58 1.3 lneto As an extension language, Lua has no notion of a "main" program:
59 1.1 mbalmer it only works <em>embedded</em> in a host client,
60 1.1 mbalmer called the <em>embedding program</em> or simply the <em>host</em>.
61 1.2 lneto The host program can invoke functions to execute a piece of Lua code,
62 1.1 mbalmer can write and read Lua variables,
63 1.1 mbalmer and can register C functions to be called by Lua code.
64 1.1 mbalmer Through the use of C functions, Lua can be augmented to cope with
65 1.1 mbalmer a wide range of different domains,
66 1.1 mbalmer thus creating customized programming languages sharing a syntactical framework.
67 1.1 mbalmer The Lua distribution includes a sample host program called <code>lua</code>,
68 1.2 lneto which uses the Lua library to offer a complete, standalone Lua interpreter,
69 1.2 lneto for interactive or batch use.
70 1.1 mbalmer
71 1.1 mbalmer
72 1.1 mbalmer <p>
73 1.1 mbalmer Lua is free software,
74 1.1 mbalmer and is provided as usual with no guarantees,
75 1.1 mbalmer as stated in its license.
76 1.1 mbalmer The implementation described in this manual is available
77 1.1 mbalmer at Lua's official web site, <code>www.lua.org</code>.
78 1.1 mbalmer
79 1.1 mbalmer
80 1.1 mbalmer <p>
81 1.1 mbalmer Like any other reference manual,
82 1.1 mbalmer this document is dry in places.
83 1.1 mbalmer For a discussion of the decisions behind the design of Lua,
84 1.1 mbalmer see the technical papers available at Lua's web site.
85 1.1 mbalmer For a detailed introduction to programming in Lua,
86 1.2 lneto see Roberto's book, <em>Programming in Lua</em>.
87 1.1 mbalmer
88 1.1 mbalmer
89 1.1 mbalmer
90 1.2 lneto <h1>2 – <a name="2">Basic Concepts</a></h1>
91 1.1 mbalmer
92 1.1 mbalmer <p>
93 1.2 lneto This section describes the basic concepts of the language.
94 1.1 mbalmer
95 1.1 mbalmer
96 1.1 mbalmer
97 1.2 lneto <h2>2.1 – <a name="2.1">Values and Types</a></h2>
98 1.1 mbalmer
99 1.1 mbalmer <p>
100 1.1 mbalmer Lua is a <em>dynamically typed language</em>.
101 1.1 mbalmer This means that
102 1.1 mbalmer variables do not have types; only values do.
103 1.1 mbalmer There are no type definitions in the language.
104 1.1 mbalmer All values carry their own type.
105 1.1 mbalmer
106 1.1 mbalmer
107 1.1 mbalmer <p>
108 1.1 mbalmer All values in Lua are <em>first-class values</em>.
109 1.1 mbalmer This means that all values can be stored in variables,
110 1.1 mbalmer passed as arguments to other functions, and returned as results.
111 1.1 mbalmer
112 1.1 mbalmer
113 1.1 mbalmer <p>
114 1.1 mbalmer There are eight basic types in Lua:
115 1.1 mbalmer <em>nil</em>, <em>boolean</em>, <em>number</em>,
116 1.1 mbalmer <em>string</em>, <em>function</em>, <em>userdata</em>,
117 1.1 mbalmer <em>thread</em>, and <em>table</em>.
118 1.4 mbalmer The type <em>nil</em> has one single value, <b>nil</b>,
119 1.1 mbalmer whose main property is to be different from any other value;
120 1.1 mbalmer it usually represents the absence of a useful value.
121 1.4 mbalmer The type <em>boolean</em> has two values, <b>false</b> and <b>true</b>.
122 1.1 mbalmer Both <b>nil</b> and <b>false</b> make a condition false;
123 1.1 mbalmer any other value makes it true.
124 1.4 mbalmer The type <em>number</em> represents both
125 1.3 lneto integer numbers and real (floating-point) numbers.
126 1.4 mbalmer The type <em>string</em> represents immutable sequences of bytes.
127 1.1 mbalmer
128 1.1 mbalmer Lua is 8-bit clean:
129 1.2 lneto strings can contain any 8-bit value,
130 1.2 lneto including embedded zeros ('<code>\0</code>').
131 1.3 lneto Lua is also encoding-agnostic;
132 1.3 lneto it makes no assumptions about the contents of a string.
133 1.2 lneto
134 1.2 lneto
135 1.2 lneto <p>
136 1.2 lneto The type <em>number</em> uses two internal representations,
137 1.4 mbalmer or two subtypes,
138 1.2 lneto one called <em>integer</em> and the other called <em>float</em>.
139 1.2 lneto Lua has explicit rules about when each representation is used,
140 1.2 lneto but it also converts between them automatically as needed (see <a href="#3.4.3">§3.4.3</a>).
141 1.2 lneto Therefore,
142 1.3 lneto the programmer may choose to mostly ignore the difference
143 1.2 lneto between integers and floats
144 1.3 lneto or to assume complete control over the representation of each number.
145 1.2 lneto Standard Lua uses 64-bit integers and double-precision (64-bit) floats,
146 1.2 lneto but you can also compile Lua so that it
147 1.2 lneto uses 32-bit integers and/or single-precision (32-bit) floats.
148 1.4 mbalmer The option with 32 bits for both integers and floats
149 1.3 lneto is particularly attractive
150 1.3 lneto for small machines and embedded systems.
151 1.3 lneto (See macro <code>LUA_32BITS</code> in file <code>luaconf.h</code>.)
152 1.1 mbalmer
153 1.1 mbalmer
154 1.1 mbalmer <p>
155 1.1 mbalmer Lua can call (and manipulate) functions written in Lua and
156 1.3 lneto functions written in C (see <a href="#3.4.10">§3.4.10</a>).
157 1.3 lneto Both are represented by the type <em>function</em>.
158 1.1 mbalmer
159 1.1 mbalmer
160 1.1 mbalmer <p>
161 1.1 mbalmer The type <em>userdata</em> is provided to allow arbitrary C data to
162 1.1 mbalmer be stored in Lua variables.
163 1.3 lneto A userdata value represents a block of raw memory.
164 1.2 lneto There are two kinds of userdata:
165 1.3 lneto <em>full userdata</em>,
166 1.3 lneto which is an object with a block of memory managed by Lua,
167 1.3 lneto and <em>light userdata</em>,
168 1.3 lneto which is simply a C pointer value.
169 1.2 lneto Userdata has no predefined operations in Lua,
170 1.1 mbalmer except assignment and identity test.
171 1.2 lneto By using <em>metatables</em>,
172 1.2 lneto the programmer can define operations for full userdata values
173 1.2 lneto (see <a href="#2.4">§2.4</a>).
174 1.1 mbalmer Userdata values cannot be created or modified in Lua,
175 1.1 mbalmer only through the C API.
176 1.1 mbalmer This guarantees the integrity of data owned by the host program.
177 1.1 mbalmer
178 1.1 mbalmer
179 1.1 mbalmer <p>
180 1.1 mbalmer The type <em>thread</em> represents independent threads of execution
181 1.2 lneto and it is used to implement coroutines (see <a href="#2.6">§2.6</a>).
182 1.3 lneto Lua threads are not related to operating-system threads.
183 1.1 mbalmer Lua supports coroutines on all systems,
184 1.3 lneto even those that do not support threads natively.
185 1.1 mbalmer
186 1.1 mbalmer
187 1.1 mbalmer <p>
188 1.1 mbalmer The type <em>table</em> implements associative arrays,
189 1.1 mbalmer that is, arrays that can be indexed not only with numbers,
190 1.3 lneto but with any Lua value except <b>nil</b> and NaN.
191 1.4 mbalmer (<em>Not a Number</em> is a special value used to represent
192 1.4 mbalmer undefined or unrepresentable numerical results, such as <code>0/0</code>.)
193 1.1 mbalmer Tables can be <em>heterogeneous</em>;
194 1.1 mbalmer that is, they can contain values of all types (except <b>nil</b>).
195 1.2 lneto Any key with value <b>nil</b> is not considered part of the table.
196 1.2 lneto Conversely, any key that is not part of a table has
197 1.2 lneto an associated value <b>nil</b>.
198 1.2 lneto
199 1.2 lneto
200 1.2 lneto <p>
201 1.3 lneto Tables are the sole data-structuring mechanism in Lua;
202 1.2 lneto they can be used to represent ordinary arrays, sequences,
203 1.1 mbalmer symbol tables, sets, records, graphs, trees, etc.
204 1.1 mbalmer To represent records, Lua uses the field name as an index.
205 1.1 mbalmer The language supports this representation by
206 1.1 mbalmer providing <code>a.name</code> as syntactic sugar for <code>a["name"]</code>.
207 1.1 mbalmer There are several convenient ways to create tables in Lua
208 1.2 lneto (see <a href="#3.4.9">§3.4.9</a>).
209 1.2 lneto
210 1.2 lneto
211 1.2 lneto <p>
212 1.2 lneto We use the term <em>sequence</em> to denote a table where
213 1.3 lneto the set of all positive numeric keys is equal to {1..<em>n</em>}
214 1.3 lneto for some non-negative integer <em>n</em>,
215 1.2 lneto which is called the length of the sequence (see <a href="#3.4.7">§3.4.7</a>).
216 1.1 mbalmer
217 1.1 mbalmer
218 1.1 mbalmer <p>
219 1.1 mbalmer Like indices,
220 1.2 lneto the values of table fields can be of any type.
221 1.1 mbalmer In particular,
222 1.1 mbalmer because functions are first-class values,
223 1.1 mbalmer table fields can contain functions.
224 1.2 lneto Thus tables can also carry <em>methods</em> (see <a href="#3.4.11">§3.4.11</a>).
225 1.2 lneto
226 1.2 lneto
227 1.2 lneto <p>
228 1.2 lneto The indexing of tables follows
229 1.2 lneto the definition of raw equality in the language.
230 1.2 lneto The expressions <code>a[i]</code> and <code>a[j]</code>
231 1.2 lneto denote the same table element
232 1.2 lneto if and only if <code>i</code> and <code>j</code> are raw equal
233 1.2 lneto (that is, equal without metamethods).
234 1.2 lneto In particular, floats with integral values
235 1.2 lneto are equal to their respective integers
236 1.2 lneto (e.g., <code>1.0 == 1</code>).
237 1.2 lneto To avoid ambiguities,
238 1.2 lneto any float with integral value used as a key
239 1.2 lneto is converted to its respective integer.
240 1.2 lneto For instance, if you write <code>a[2.0] = true</code>,
241 1.2 lneto the actual key inserted into the table will be the
242 1.2 lneto integer <code>2</code>.
243 1.3 lneto (On the other hand,
244 1.3 lneto 2 and "<code>2</code>" are different Lua values and therefore
245 1.3 lneto denote different table entries.)
246 1.1 mbalmer
247 1.1 mbalmer
248 1.1 mbalmer <p>
249 1.1 mbalmer Tables, functions, threads, and (full) userdata values are <em>objects</em>:
250 1.1 mbalmer variables do not actually <em>contain</em> these values,
251 1.1 mbalmer only <em>references</em> to them.
252 1.1 mbalmer Assignment, parameter passing, and function returns
253 1.1 mbalmer always manipulate references to such values;
254 1.1 mbalmer these operations do not imply any kind of copy.
255 1.1 mbalmer
256 1.1 mbalmer
257 1.1 mbalmer <p>
258 1.1 mbalmer The library function <a href="#pdf-type"><code>type</code></a> returns a string describing the type
259 1.2 lneto of a given value (see <a href="#6.1">§6.1</a>).
260 1.2 lneto
261 1.1 mbalmer
262 1.1 mbalmer
263 1.1 mbalmer
264 1.2 lneto
265 1.2 lneto <h2>2.2 – <a name="2.2">Environments and the Global Environment</a></h2>
266 1.1 mbalmer
267 1.1 mbalmer <p>
268 1.2 lneto As will be discussed in <a href="#3.2">§3.2</a> and <a href="#3.3.3">§3.3.3</a>,
269 1.3 lneto any reference to a free name
270 1.3 lneto (that is, a name not bound to any declaration) <code>var</code>
271 1.3 lneto is syntactically translated to <code>_ENV.var</code>.
272 1.2 lneto Moreover, every chunk is compiled in the scope of
273 1.3 lneto an external local variable named <code>_ENV</code> (see <a href="#3.3.2">§3.3.2</a>),
274 1.3 lneto so <code>_ENV</code> itself is never a free name in a chunk.
275 1.1 mbalmer
276 1.1 mbalmer
277 1.2 lneto <p>
278 1.2 lneto Despite the existence of this external <code>_ENV</code> variable and
279 1.3 lneto the translation of free names,
280 1.2 lneto <code>_ENV</code> is a completely regular name.
281 1.2 lneto In particular,
282 1.2 lneto you can define new variables and parameters with that name.
283 1.3 lneto Each reference to a free name uses the <code>_ENV</code> that is
284 1.2 lneto visible at that point in the program,
285 1.2 lneto following the usual visibility rules of Lua (see <a href="#3.5">§3.5</a>).
286 1.1 mbalmer
287 1.1 mbalmer
288 1.2 lneto <p>
289 1.2 lneto Any table used as the value of <code>_ENV</code> is called an <em>environment</em>.
290 1.1 mbalmer
291 1.1 mbalmer
292 1.2 lneto <p>
293 1.2 lneto Lua keeps a distinguished environment called the <em>global environment</em>.
294 1.2 lneto This value is kept at a special index in the C registry (see <a href="#4.5">§4.5</a>).
295 1.3 lneto In Lua, the global variable <a href="#pdf-_G"><code>_G</code></a> is initialized with this same value.
296 1.3 lneto (<a href="#pdf-_G"><code>_G</code></a> is never used internally.)
297 1.1 mbalmer
298 1.1 mbalmer
299 1.1 mbalmer <p>
300 1.3 lneto When Lua loads a chunk,
301 1.3 lneto the default value for its <code>_ENV</code> upvalue
302 1.3 lneto is the global environment (see <a href="#pdf-load"><code>load</code></a>).
303 1.2 lneto Therefore, by default,
304 1.3 lneto free names in Lua code refer to entries in the global environment
305 1.3 lneto (and, therefore, they are also called <em>global variables</em>).
306 1.2 lneto Moreover, all standard libraries are loaded in the global environment
307 1.3 lneto and some functions there operate on that environment.
308 1.2 lneto You can use <a href="#pdf-load"><code>load</code></a> (or <a href="#pdf-loadfile"><code>loadfile</code></a>)
309 1.2 lneto to load a chunk with a different environment.
310 1.2 lneto (In C, you have to load the chunk and then change the value
311 1.2 lneto of its first upvalue.)
312 1.1 mbalmer
313 1.1 mbalmer
314 1.1 mbalmer
315 1.1 mbalmer
316 1.1 mbalmer
317 1.2 lneto <h2>2.3 – <a name="2.3">Error Handling</a></h2>
318 1.1 mbalmer
319 1.1 mbalmer <p>
320 1.2 lneto Because Lua is an embedded extension language,
321 1.2 lneto all Lua actions start from C code in the host program
322 1.3 lneto calling a function from the Lua library.
323 1.3 lneto (When you use Lua standalone,
324 1.3 lneto the <code>lua</code> application is the host program.)
325 1.2 lneto Whenever an error occurs during
326 1.2 lneto the compilation or execution of a Lua chunk,
327 1.2 lneto control returns to the host,
328 1.2 lneto which can take appropriate measures
329 1.2 lneto (such as printing an error message).
330 1.1 mbalmer
331 1.1 mbalmer
332 1.1 mbalmer <p>
333 1.2 lneto Lua code can explicitly generate an error by calling the
334 1.2 lneto <a href="#pdf-error"><code>error</code></a> function.
335 1.2 lneto If you need to catch errors in Lua,
336 1.2 lneto you can use <a href="#pdf-pcall"><code>pcall</code></a> or <a href="#pdf-xpcall"><code>xpcall</code></a>
337 1.2 lneto to call a given function in <em>protected mode</em>.
338 1.1 mbalmer
339 1.1 mbalmer
340 1.1 mbalmer <p>
341 1.2 lneto Whenever there is an error,
342 1.2 lneto an <em>error object</em> (also called an <em>error message</em>)
343 1.2 lneto is propagated with information about the error.
344 1.3 lneto Lua itself only generates errors whose error object is a string,
345 1.2 lneto but programs may generate errors with
346 1.3 lneto any value as the error object.
347 1.3 lneto It is up to the Lua program or its host to handle such error objects.
348 1.1 mbalmer
349 1.1 mbalmer
350 1.1 mbalmer <p>
351 1.2 lneto When you use <a href="#pdf-xpcall"><code>xpcall</code></a> or <a href="#lua_pcall"><code>lua_pcall</code></a>,
352 1.2 lneto you may give a <em>message handler</em>
353 1.2 lneto to be called in case of errors.
354 1.2 lneto This function is called with the original error message
355 1.2 lneto and returns a new error message.
356 1.2 lneto It is called before the error unwinds the stack,
357 1.2 lneto so that it can gather more information about the error,
358 1.2 lneto for instance by inspecting the stack and creating a stack traceback.
359 1.2 lneto This message handler is still protected by the protected call;
360 1.2 lneto so, an error inside the message handler
361 1.2 lneto will call the message handler again.
362 1.3 lneto If this loop goes on for too long,
363 1.3 lneto Lua breaks it and returns an appropriate message.
364 1.1 mbalmer
365 1.1 mbalmer
366 1.1 mbalmer
367 1.1 mbalmer
368 1.1 mbalmer
369 1.2 lneto <h2>2.4 – <a name="2.4">Metatables and Metamethods</a></h2>
370 1.1 mbalmer
371 1.2 lneto <p>
372 1.2 lneto Every value in Lua can have a <em>metatable</em>.
373 1.2 lneto This <em>metatable</em> is an ordinary Lua table
374 1.2 lneto that defines the behavior of the original value
375 1.2 lneto under certain special operations.
376 1.2 lneto You can change several aspects of the behavior
377 1.2 lneto of operations over a value by setting specific fields in its metatable.
378 1.2 lneto For instance, when a non-numeric value is the operand of an addition,
379 1.2 lneto Lua checks for a function in the field "<code>__add</code>" of the value's metatable.
380 1.2 lneto If it finds one,
381 1.2 lneto Lua calls this function to perform the addition.
382 1.1 mbalmer
383 1.1 mbalmer
384 1.1 mbalmer <p>
385 1.2 lneto The keys in a metatable are derived from the <em>event</em> names;
386 1.2 lneto the corresponding values are called <em>metamethods</em>.
387 1.2 lneto In the previous example, the event is <code>"add"</code>
388 1.2 lneto and the metamethod is the function that performs the addition.
389 1.1 mbalmer
390 1.1 mbalmer
391 1.2 lneto <p>
392 1.2 lneto You can query the metatable of any value
393 1.2 lneto using the <a href="#pdf-getmetatable"><code>getmetatable</code></a> function.
394 1.1 mbalmer
395 1.1 mbalmer
396 1.1 mbalmer <p>
397 1.2 lneto You can replace the metatable of tables
398 1.2 lneto using the <a href="#pdf-setmetatable"><code>setmetatable</code></a> function.
399 1.4 mbalmer You cannot change the metatable of other types from Lua code
400 1.3 lneto (except by using the debug library (<a href="#6.10">§6.10</a>));
401 1.5 lneto you should use the C API for that.
402 1.1 mbalmer
403 1.1 mbalmer
404 1.1 mbalmer <p>
405 1.2 lneto Tables and full userdata have individual metatables
406 1.2 lneto (although multiple tables and userdata can share their metatables).
407 1.2 lneto Values of all other types share one single metatable per type;
408 1.2 lneto that is, there is one single metatable for all numbers,
409 1.2 lneto one for all strings, etc.
410 1.2 lneto By default, a value has no metatable,
411 1.2 lneto but the string library sets a metatable for the string type (see <a href="#6.4">§6.4</a>).
412 1.1 mbalmer
413 1.1 mbalmer
414 1.1 mbalmer <p>
415 1.2 lneto A metatable controls how an object behaves in
416 1.3 lneto arithmetic operations, bitwise operations,
417 1.2 lneto order comparisons, concatenation, length operation, calls, and indexing.
418 1.2 lneto A metatable also can define a function to be called
419 1.3 lneto when a userdata or a table is garbage collected (<a href="#2.5">§2.5</a>).
420 1.1 mbalmer
421 1.1 mbalmer
422 1.1 mbalmer <p>
423 1.2 lneto A detailed list of events controlled by metatables is given next.
424 1.2 lneto Each operation is identified by its corresponding event name.
425 1.2 lneto The key for each event is a string with its name prefixed by
426 1.2 lneto two underscores, '<code>__</code>';
427 1.2 lneto for instance, the key for operation "add" is the
428 1.2 lneto string "<code>__add</code>".
429 1.2 lneto Note that queries for metamethods are always raw;
430 1.2 lneto the access to a metamethod does not invoke other metamethods.
431 1.1 mbalmer
432 1.1 mbalmer
433 1.2 lneto <p>
434 1.2 lneto For the unary operators (negation, length, and bitwise not),
435 1.2 lneto the metamethod is computed and called with a dummy second operand,
436 1.2 lneto equal to the first one.
437 1.2 lneto This extra operand is only to simplify Lua's internals
438 1.2 lneto (by making these operators behave like a binary operation)
439 1.2 lneto and may be removed in future versions.
440 1.2 lneto (For most uses this extra operand is irrelevant.)
441 1.1 mbalmer
442 1.1 mbalmer
443 1.1 mbalmer
444 1.2 lneto <ul>
445 1.1 mbalmer
446 1.2 lneto <li><b>"add": </b>
447 1.2 lneto the <code>+</code> operation.
448 1.1 mbalmer
449 1.2 lneto If any operand for an addition is not a number
450 1.2 lneto (nor a string coercible to a number),
451 1.2 lneto Lua will try to call a metamethod.
452 1.2 lneto First, Lua will check the first operand (even if it is valid).
453 1.2 lneto If that operand does not define a metamethod for the "<code>__add</code>" event,
454 1.2 lneto then Lua will check the second operand.
455 1.3 lneto If Lua can find a metamethod,
456 1.2 lneto it calls the metamethod with the two operands as arguments,
457 1.2 lneto and the result of the call
458 1.2 lneto (adjusted to one value)
459 1.2 lneto is the result of the operation.
460 1.3 lneto Otherwise,
461 1.3 lneto it raises an error.
462 1.2 lneto </li>
463 1.1 mbalmer
464 1.2 lneto <li><b>"sub": </b>
465 1.2 lneto the <code>-</code> operation.
466 1.1 mbalmer
467 1.2 lneto Behavior similar to the "add" operation.
468 1.2 lneto </li>
469 1.1 mbalmer
470 1.2 lneto <li><b>"mul": </b>
471 1.2 lneto the <code>*</code> operation.
472 1.1 mbalmer
473 1.2 lneto Behavior similar to the "add" operation.
474 1.2 lneto </li>
475 1.1 mbalmer
476 1.2 lneto <li><b>"div": </b>
477 1.2 lneto the <code>/</code> operation.
478 1.1 mbalmer
479 1.2 lneto Behavior similar to the "add" operation.
480 1.2 lneto </li>
481 1.1 mbalmer
482 1.2 lneto <li><b>"mod": </b>
483 1.2 lneto the <code>%</code> operation.
484 1.1 mbalmer
485 1.2 lneto Behavior similar to the "add" operation.
486 1.2 lneto </li>
487 1.1 mbalmer
488 1.2 lneto <li><b>"pow": </b>
489 1.2 lneto the <code>^</code> (exponentiation) operation.
490 1.1 mbalmer
491 1.2 lneto Behavior similar to the "add" operation.
492 1.2 lneto </li>
493 1.1 mbalmer
494 1.2 lneto <li><b>"unm": </b>
495 1.2 lneto the <code>-</code> (unary minus) operation.
496 1.1 mbalmer
497 1.2 lneto Behavior similar to the "add" operation.
498 1.2 lneto </li>
499 1.1 mbalmer
500 1.2 lneto <li><b>"idiv": </b>
501 1.3 lneto the <code>//</code> (floor division) operation.
502 1.1 mbalmer
503 1.3 lneto Behavior similar to the "add" operation.
504 1.2 lneto </li>
505 1.1 mbalmer
506 1.2 lneto <li><b>"band": </b>
507 1.2 lneto the <code>&</code> (bitwise and) operation.
508 1.1 mbalmer
509 1.3 lneto Behavior similar to the "add" operation,
510 1.3 lneto except that Lua will try a metamethod
511 1.4 mbalmer if any operand is neither an integer
512 1.3 lneto nor a value coercible to an integer (see <a href="#3.4.3">§3.4.3</a>).
513 1.2 lneto </li>
514 1.1 mbalmer
515 1.2 lneto <li><b>"bor": </b>
516 1.2 lneto the <code>|</code> (bitwise or) operation.
517 1.1 mbalmer
518 1.2 lneto Behavior similar to the "band" operation.
519 1.2 lneto </li>
520 1.1 mbalmer
521 1.2 lneto <li><b>"bxor": </b>
522 1.2 lneto the <code>~</code> (bitwise exclusive or) operation.
523 1.1 mbalmer
524 1.2 lneto Behavior similar to the "band" operation.
525 1.2 lneto </li>
526 1.1 mbalmer
527 1.2 lneto <li><b>"bnot": </b>
528 1.2 lneto the <code>~</code> (bitwise unary not) operation.
529 1.1 mbalmer
530 1.2 lneto Behavior similar to the "band" operation.
531 1.2 lneto </li>
532 1.1 mbalmer
533 1.2 lneto <li><b>"shl": </b>
534 1.2 lneto the <code><<</code> (bitwise left shift) operation.
535 1.1 mbalmer
536 1.2 lneto Behavior similar to the "band" operation.
537 1.2 lneto </li>
538 1.1 mbalmer
539 1.2 lneto <li><b>"shr": </b>
540 1.2 lneto the <code>>></code> (bitwise right shift) operation.
541 1.1 mbalmer
542 1.2 lneto Behavior similar to the "band" operation.
543 1.2 lneto </li>
544 1.1 mbalmer
545 1.2 lneto <li><b>"concat": </b>
546 1.2 lneto the <code>..</code> (concatenation) operation.
547 1.1 mbalmer
548 1.2 lneto Behavior similar to the "add" operation,
549 1.2 lneto except that Lua will try a metamethod
550 1.4 mbalmer if any operand is neither a string nor a number
551 1.2 lneto (which is always coercible to a string).
552 1.2 lneto </li>
553 1.2 lneto
554 1.2 lneto <li><b>"len": </b>
555 1.2 lneto the <code>#</code> (length) operation.
556 1.2 lneto
557 1.2 lneto If the object is not a string,
558 1.2 lneto Lua will try its metamethod.
559 1.2 lneto If there is a metamethod,
560 1.2 lneto Lua calls it with the object as argument,
561 1.2 lneto and the result of the call
562 1.2 lneto (always adjusted to one value)
563 1.2 lneto is the result of the operation.
564 1.2 lneto If there is no metamethod but the object is a table,
565 1.2 lneto then Lua uses the table length operation (see <a href="#3.4.7">§3.4.7</a>).
566 1.2 lneto Otherwise, Lua raises an error.
567 1.2 lneto </li>
568 1.1 mbalmer
569 1.2 lneto <li><b>"eq": </b>
570 1.2 lneto the <code>==</code> (equal) operation.
571 1.1 mbalmer
572 1.2 lneto Behavior similar to the "add" operation,
573 1.2 lneto except that Lua will try a metamethod only when the values
574 1.2 lneto being compared are either both tables or both full userdata
575 1.2 lneto and they are not primitively equal.
576 1.2 lneto The result of the call is always converted to a boolean.
577 1.2 lneto </li>
578 1.1 mbalmer
579 1.2 lneto <li><b>"lt": </b>
580 1.2 lneto the <code><</code> (less than) operation.
581 1.1 mbalmer
582 1.2 lneto Behavior similar to the "add" operation,
583 1.2 lneto except that Lua will try a metamethod only when the values
584 1.2 lneto being compared are neither both numbers nor both strings.
585 1.2 lneto The result of the call is always converted to a boolean.
586 1.2 lneto </li>
587 1.2 lneto
588 1.2 lneto <li><b>"le": </b>
589 1.2 lneto the <code><=</code> (less equal) operation.
590 1.2 lneto
591 1.2 lneto Unlike other operations,
592 1.5 lneto the less-equal operation can use two different events.
593 1.2 lneto First, Lua looks for the "<code>__le</code>" metamethod in both operands,
594 1.2 lneto like in the "lt" operation.
595 1.2 lneto If it cannot find such a metamethod,
596 1.2 lneto then it will try the "<code>__lt</code>" event,
597 1.2 lneto assuming that <code>a <= b</code> is equivalent to <code>not (b < a)</code>.
598 1.2 lneto As with the other comparison operators,
599 1.2 lneto the result is always a boolean.
600 1.4 mbalmer (This use of the "<code>__lt</code>" event can be removed in future versions;
601 1.4 mbalmer it is also slower than a real "<code>__le</code>" metamethod.)
602 1.2 lneto </li>
603 1.1 mbalmer
604 1.2 lneto <li><b>"index": </b>
605 1.2 lneto The indexing access <code>table[key]</code>.
606 1.1 mbalmer
607 1.2 lneto This event happens when <code>table</code> is not a table or
608 1.2 lneto when <code>key</code> is not present in <code>table</code>.
609 1.2 lneto The metamethod is looked up in <code>table</code>.
610 1.1 mbalmer
611 1.1 mbalmer
612 1.1 mbalmer <p>
613 1.2 lneto Despite the name,
614 1.2 lneto the metamethod for this event can be either a function or a table.
615 1.2 lneto If it is a function,
616 1.2 lneto it is called with <code>table</code> and <code>key</code> as arguments.
617 1.2 lneto If it is a table,
618 1.2 lneto the final result is the result of indexing this table with <code>key</code>.
619 1.2 lneto (This indexing is regular, not raw,
620 1.2 lneto and therefore can trigger another metamethod.)
621 1.2 lneto </li>
622 1.2 lneto
623 1.2 lneto <li><b>"newindex": </b>
624 1.2 lneto The indexing assignment <code>table[key] = value</code>.
625 1.1 mbalmer
626 1.2 lneto Like the index event,
627 1.2 lneto this event happens when <code>table</code> is not a table or
628 1.2 lneto when <code>key</code> is not present in <code>table</code>.
629 1.2 lneto The metamethod is looked up in <code>table</code>.
630 1.1 mbalmer
631 1.1 mbalmer
632 1.1 mbalmer <p>
633 1.3 lneto Like with indexing,
634 1.2 lneto the metamethod for this event can be either a function or a table.
635 1.2 lneto If it is a function,
636 1.2 lneto it is called with <code>table</code>, <code>key</code>, and <code>value</code> as arguments.
637 1.2 lneto If it is a table,
638 1.2 lneto Lua does an indexing assignment to this table with the same key and value.
639 1.2 lneto (This assignment is regular, not raw,
640 1.2 lneto and therefore can trigger another metamethod.)
641 1.1 mbalmer
642 1.1 mbalmer
643 1.2 lneto <p>
644 1.3 lneto Whenever there is a "newindex" metamethod,
645 1.2 lneto Lua does not perform the primitive assignment.
646 1.2 lneto (If necessary,
647 1.2 lneto the metamethod itself can call <a href="#pdf-rawset"><code>rawset</code></a>
648 1.2 lneto to do the assignment.)
649 1.2 lneto </li>
650 1.1 mbalmer
651 1.2 lneto <li><b>"call": </b>
652 1.2 lneto The call operation <code>func(args)</code>.
653 1.1 mbalmer
654 1.2 lneto This event happens when Lua tries to call a non-function value
655 1.2 lneto (that is, <code>func</code> is not a function).
656 1.2 lneto The metamethod is looked up in <code>func</code>.
657 1.2 lneto If present,
658 1.2 lneto the metamethod is called with <code>func</code> as its first argument,
659 1.2 lneto followed by the arguments of the original call (<code>args</code>).
660 1.2 lneto </li>
661 1.1 mbalmer
662 1.2 lneto </ul>
663 1.1 mbalmer
664 1.4 mbalmer <p>
665 1.4 mbalmer It is a good practice to add all needed metamethods to a table
666 1.4 mbalmer before setting it as a metatable of some object.
667 1.4 mbalmer In particular, the "<code>__gc</code>" metamethod works only when this order
668 1.4 mbalmer is followed (see <a href="#2.5.1">§2.5.1</a>).
669 1.4 mbalmer
670 1.4 mbalmer
671 1.1 mbalmer
672 1.1 mbalmer
673 1.1 mbalmer
674 1.2 lneto <h2>2.5 – <a name="2.5">Garbage Collection</a></h2>
675 1.1 mbalmer
676 1.1 mbalmer <p>
677 1.2 lneto Lua performs automatic memory management.
678 1.2 lneto This means that
679 1.3 lneto you do not have to worry about allocating memory for new objects
680 1.3 lneto or freeing it when the objects are no longer needed.
681 1.2 lneto Lua manages memory automatically by running
682 1.2 lneto a <em>garbage collector</em> to collect all <em>dead objects</em>
683 1.2 lneto (that is, objects that are no longer accessible from Lua).
684 1.2 lneto All memory used by Lua is subject to automatic management:
685 1.2 lneto strings, tables, userdata, functions, threads, internal structures, etc.
686 1.1 mbalmer
687 1.2 lneto
688 1.2 lneto <p>
689 1.2 lneto Lua implements an incremental mark-and-sweep collector.
690 1.2 lneto It uses two numbers to control its garbage-collection cycles:
691 1.2 lneto the <em>garbage-collector pause</em> and
692 1.2 lneto the <em>garbage-collector step multiplier</em>.
693 1.2 lneto Both use percentage points as units
694 1.2 lneto (e.g., a value of 100 means an internal value of 1).
695 1.1 mbalmer
696 1.1 mbalmer
697 1.1 mbalmer <p>
698 1.2 lneto The garbage-collector pause
699 1.2 lneto controls how long the collector waits before starting a new cycle.
700 1.2 lneto Larger values make the collector less aggressive.
701 1.2 lneto Values smaller than 100 mean the collector will not wait to
702 1.2 lneto start a new cycle.
703 1.2 lneto A value of 200 means that the collector waits for the total memory in use
704 1.2 lneto to double before starting a new cycle.
705 1.2 lneto
706 1.1 mbalmer
707 1.2 lneto <p>
708 1.2 lneto The garbage-collector step multiplier
709 1.2 lneto controls the relative speed of the collector relative to
710 1.2 lneto memory allocation.
711 1.2 lneto Larger values make the collector more aggressive but also increase
712 1.2 lneto the size of each incremental step.
713 1.2 lneto You should not use values smaller than 100,
714 1.3 lneto because they make the collector too slow and
715 1.2 lneto can result in the collector never finishing a cycle.
716 1.2 lneto The default is 200,
717 1.2 lneto which means that the collector runs at "twice"
718 1.2 lneto the speed of memory allocation.
719 1.1 mbalmer
720 1.1 mbalmer
721 1.2 lneto <p>
722 1.2 lneto If you set the step multiplier to a very large number
723 1.2 lneto (larger than 10% of the maximum number of
724 1.2 lneto bytes that the program may use),
725 1.2 lneto the collector behaves like a stop-the-world collector.
726 1.2 lneto If you then set the pause to 200,
727 1.2 lneto the collector behaves as in old Lua versions,
728 1.2 lneto doing a complete collection every time Lua doubles its
729 1.2 lneto memory usage.
730 1.1 mbalmer
731 1.1 mbalmer
732 1.2 lneto <p>
733 1.2 lneto You can change these numbers by calling <a href="#lua_gc"><code>lua_gc</code></a> in C
734 1.2 lneto or <a href="#pdf-collectgarbage"><code>collectgarbage</code></a> in Lua.
735 1.2 lneto You can also use these functions to control
736 1.2 lneto the collector directly (e.g., stop and restart it).
737 1.1 mbalmer
738 1.1 mbalmer
739 1.1 mbalmer
740 1.2 lneto <h3>2.5.1 – <a name="2.5.1">Garbage-Collection Metamethods</a></h3>
741 1.1 mbalmer
742 1.2 lneto <p>
743 1.2 lneto You can set garbage-collector metamethods for tables
744 1.2 lneto and, using the C API,
745 1.2 lneto for full userdata (see <a href="#2.4">§2.4</a>).
746 1.2 lneto These metamethods are also called <em>finalizers</em>.
747 1.2 lneto Finalizers allow you to coordinate Lua's garbage collection
748 1.2 lneto with external resource management
749 1.2 lneto (such as closing files, network or database connections,
750 1.2 lneto or freeing your own memory).
751 1.1 mbalmer
752 1.1 mbalmer
753 1.1 mbalmer <p>
754 1.2 lneto For an object (table or userdata) to be finalized when collected,
755 1.2 lneto you must <em>mark</em> it for finalization.
756 1.1 mbalmer
757 1.2 lneto You mark an object for finalization when you set its metatable
758 1.2 lneto and the metatable has a field indexed by the string "<code>__gc</code>".
759 1.2 lneto Note that if you set a metatable without a <code>__gc</code> field
760 1.2 lneto and later create that field in the metatable,
761 1.2 lneto the object will not be marked for finalization.
762 1.1 mbalmer
763 1.1 mbalmer
764 1.2 lneto <p>
765 1.2 lneto When a marked object becomes garbage,
766 1.2 lneto it is not collected immediately by the garbage collector.
767 1.2 lneto Instead, Lua puts it in a list.
768 1.2 lneto After the collection,
769 1.3 lneto Lua goes through that list.
770 1.3 lneto For each object in the list,
771 1.3 lneto it checks the object's <code>__gc</code> metamethod:
772 1.3 lneto If it is a function,
773 1.3 lneto Lua calls it with the object as its single argument;
774 1.3 lneto if the metamethod is not a function,
775 1.3 lneto Lua simply ignores it.
776 1.1 mbalmer
777 1.1 mbalmer
778 1.2 lneto <p>
779 1.2 lneto At the end of each garbage-collection cycle,
780 1.2 lneto the finalizers for objects are called in
781 1.3 lneto the reverse order that the objects were marked for finalization,
782 1.2 lneto among those collected in that cycle;
783 1.2 lneto that is, the first finalizer to be called is the one associated
784 1.2 lneto with the object marked last in the program.
785 1.2 lneto The execution of each finalizer may occur at any point during
786 1.2 lneto the execution of the regular code.
787 1.1 mbalmer
788 1.1 mbalmer
789 1.2 lneto <p>
790 1.2 lneto Because the object being collected must still be used by the finalizer,
791 1.3 lneto that object (and other objects accessible only through it)
792 1.2 lneto must be <em>resurrected</em> by Lua.
793 1.2 lneto Usually, this resurrection is transient,
794 1.2 lneto and the object memory is freed in the next garbage-collection cycle.
795 1.2 lneto However, if the finalizer stores the object in some global place
796 1.2 lneto (e.g., a global variable),
797 1.3 lneto then the resurrection is permanent.
798 1.2 lneto Moreover, if the finalizer marks a finalizing object for finalization again,
799 1.2 lneto its finalizer will be called again in the next cycle where the
800 1.2 lneto object is unreachable.
801 1.2 lneto In any case,
802 1.4 mbalmer the object memory is freed only in a GC cycle where
803 1.2 lneto the object is unreachable and not marked for finalization.
804 1.1 mbalmer
805 1.1 mbalmer
806 1.2 lneto <p>
807 1.2 lneto When you close a state (see <a href="#lua_close"><code>lua_close</code></a>),
808 1.2 lneto Lua calls the finalizers of all objects marked for finalization,
809 1.2 lneto following the reverse order that they were marked.
810 1.2 lneto If any finalizer marks objects for collection during that phase,
811 1.2 lneto these marks have no effect.
812 1.1 mbalmer
813 1.1 mbalmer
814 1.1 mbalmer
815 1.1 mbalmer
816 1.1 mbalmer
817 1.2 lneto <h3>2.5.2 – <a name="2.5.2">Weak Tables</a></h3>
818 1.2 lneto
819 1.2 lneto <p>
820 1.2 lneto A <em>weak table</em> is a table whose elements are
821 1.2 lneto <em>weak references</em>.
822 1.2 lneto A weak reference is ignored by the garbage collector.
823 1.2 lneto In other words,
824 1.2 lneto if the only references to an object are weak references,
825 1.2 lneto then the garbage collector will collect that object.
826 1.2 lneto
827 1.2 lneto
828 1.2 lneto <p>
829 1.2 lneto A weak table can have weak keys, weak values, or both.
830 1.4 mbalmer A table with weak values allows the collection of its values,
831 1.4 mbalmer but prevents the collection of its keys.
832 1.2 lneto A table with both weak keys and weak values allows the collection of
833 1.2 lneto both keys and values.
834 1.2 lneto In any case, if either the key or the value is collected,
835 1.2 lneto the whole pair is removed from the table.
836 1.2 lneto The weakness of a table is controlled by the
837 1.2 lneto <code>__mode</code> field of its metatable.
838 1.2 lneto If the <code>__mode</code> field is a string containing the character '<code>k</code>',
839 1.2 lneto the keys in the table are weak.
840 1.2 lneto If <code>__mode</code> contains '<code>v</code>',
841 1.2 lneto the values in the table are weak.
842 1.1 mbalmer
843 1.1 mbalmer
844 1.2 lneto <p>
845 1.2 lneto A table with weak keys and strong values
846 1.2 lneto is also called an <em>ephemeron table</em>.
847 1.2 lneto In an ephemeron table,
848 1.2 lneto a value is considered reachable only if its key is reachable.
849 1.2 lneto In particular,
850 1.2 lneto if the only reference to a key comes through its value,
851 1.2 lneto the pair is removed.
852 1.1 mbalmer
853 1.1 mbalmer
854 1.2 lneto <p>
855 1.2 lneto Any change in the weakness of a table may take effect only
856 1.2 lneto at the next collect cycle.
857 1.2 lneto In particular, if you change the weakness to a stronger mode,
858 1.2 lneto Lua may still collect some items from that table
859 1.2 lneto before the change takes effect.
860 1.1 mbalmer
861 1.1 mbalmer
862 1.2 lneto <p>
863 1.2 lneto Only objects that have an explicit construction
864 1.2 lneto are removed from weak tables.
865 1.2 lneto Values, such as numbers and light C functions,
866 1.2 lneto are not subject to garbage collection,
867 1.2 lneto and therefore are not removed from weak tables
868 1.3 lneto (unless their associated values are collected).
869 1.2 lneto Although strings are subject to garbage collection,
870 1.2 lneto they do not have an explicit construction,
871 1.2 lneto and therefore are not removed from weak tables.
872 1.1 mbalmer
873 1.1 mbalmer
874 1.1 mbalmer <p>
875 1.2 lneto Resurrected objects
876 1.2 lneto (that is, objects being finalized
877 1.2 lneto and objects accessible only through objects being finalized)
878 1.2 lneto have a special behavior in weak tables.
879 1.2 lneto They are removed from weak values before running their finalizers,
880 1.2 lneto but are removed from weak keys only in the next collection
881 1.2 lneto after running their finalizers, when such objects are actually freed.
882 1.2 lneto This behavior allows the finalizer to access properties
883 1.2 lneto associated with the object through weak tables.
884 1.1 mbalmer
885 1.1 mbalmer
886 1.1 mbalmer <p>
887 1.2 lneto If a weak table is among the resurrected objects in a collection cycle,
888 1.2 lneto it may not be properly cleared until the next cycle.
889 1.1 mbalmer
890 1.1 mbalmer
891 1.1 mbalmer
892 1.1 mbalmer
893 1.1 mbalmer
894 1.1 mbalmer
895 1.1 mbalmer
896 1.2 lneto <h2>2.6 – <a name="2.6">Coroutines</a></h2>
897 1.1 mbalmer
898 1.1 mbalmer <p>
899 1.2 lneto Lua supports coroutines,
900 1.2 lneto also called <em>collaborative multithreading</em>.
901 1.2 lneto A coroutine in Lua represents an independent thread of execution.
902 1.2 lneto Unlike threads in multithread systems, however,
903 1.2 lneto a coroutine only suspends its execution by explicitly calling
904 1.2 lneto a yield function.
905 1.1 mbalmer
906 1.1 mbalmer
907 1.1 mbalmer <p>
908 1.2 lneto You create a coroutine by calling <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>.
909 1.2 lneto Its sole argument is a function
910 1.2 lneto that is the main function of the coroutine.
911 1.2 lneto The <code>create</code> function only creates a new coroutine and
912 1.2 lneto returns a handle to it (an object of type <em>thread</em>);
913 1.2 lneto it does not start the coroutine.
914 1.1 mbalmer
915 1.1 mbalmer
916 1.1 mbalmer <p>
917 1.2 lneto You execute a coroutine by calling <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
918 1.2 lneto When you first call <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
919 1.2 lneto passing as its first argument
920 1.2 lneto a thread returned by <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
921 1.4 mbalmer the coroutine starts its execution by
922 1.4 mbalmer calling its main function.
923 1.3 lneto Extra arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> are passed
924 1.4 mbalmer as arguments to that function.
925 1.2 lneto After the coroutine starts running,
926 1.2 lneto it runs until it terminates or <em>yields</em>.
927 1.1 mbalmer
928 1.1 mbalmer
929 1.1 mbalmer <p>
930 1.2 lneto A coroutine can terminate its execution in two ways:
931 1.2 lneto normally, when its main function returns
932 1.2 lneto (explicitly or implicitly, after the last instruction);
933 1.2 lneto and abnormally, if there is an unprotected error.
934 1.3 lneto In case of normal termination,
935 1.3 lneto <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>true</b>,
936 1.2 lneto plus any values returned by the coroutine main function.
937 1.2 lneto In case of errors, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns <b>false</b>
938 1.2 lneto plus an error message.
939 1.1 mbalmer
940 1.1 mbalmer
941 1.1 mbalmer <p>
942 1.2 lneto A coroutine yields by calling <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
943 1.2 lneto When a coroutine yields,
944 1.2 lneto the corresponding <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> returns immediately,
945 1.2 lneto even if the yield happens inside nested function calls
946 1.2 lneto (that is, not in the main function,
947 1.2 lneto but in a function directly or indirectly called by the main function).
948 1.2 lneto In the case of a yield, <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a> also returns <b>true</b>,
949 1.2 lneto plus any values passed to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a>.
950 1.2 lneto The next time you resume the same coroutine,
951 1.2 lneto it continues its execution from the point where it yielded,
952 1.2 lneto with the call to <a href="#pdf-coroutine.yield"><code>coroutine.yield</code></a> returning any extra
953 1.2 lneto arguments passed to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
954 1.1 mbalmer
955 1.1 mbalmer
956 1.1 mbalmer <p>
957 1.2 lneto Like <a href="#pdf-coroutine.create"><code>coroutine.create</code></a>,
958 1.2 lneto the <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> function also creates a coroutine,
959 1.2 lneto but instead of returning the coroutine itself,
960 1.2 lneto it returns a function that, when called, resumes the coroutine.
961 1.2 lneto Any arguments passed to this function
962 1.2 lneto go as extra arguments to <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>.
963 1.2 lneto <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> returns all the values returned by <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
964 1.2 lneto except the first one (the boolean error code).
965 1.2 lneto Unlike <a href="#pdf-coroutine.resume"><code>coroutine.resume</code></a>,
966 1.2 lneto <a href="#pdf-coroutine.wrap"><code>coroutine.wrap</code></a> does not catch errors;
967 1.2 lneto any error is propagated to the caller.
968 1.1 mbalmer
969 1.1 mbalmer
970 1.2 lneto <p>
971 1.2 lneto As an example of how coroutines work,
972 1.2 lneto consider the following code:
973 1.1 mbalmer
974 1.1 mbalmer <pre>
975 1.2 lneto function foo (a)
976 1.2 lneto print("foo", a)
977 1.2 lneto return coroutine.yield(2*a)
978 1.2 lneto end
979 1.2 lneto
980 1.2 lneto co = coroutine.create(function (a,b)
981 1.2 lneto print("co-body", a, b)
982 1.2 lneto local r = foo(a+1)
983 1.2 lneto print("co-body", r)
984 1.2 lneto local r, s = coroutine.yield(a+b, a-b)
985 1.2 lneto print("co-body", r, s)
986 1.2 lneto return b, "end"
987 1.2 lneto end)
988 1.2 lneto
989 1.2 lneto print("main", coroutine.resume(co, 1, 10))
990 1.2 lneto print("main", coroutine.resume(co, "r"))
991 1.2 lneto print("main", coroutine.resume(co, "x", "y"))
992 1.2 lneto print("main", coroutine.resume(co, "x", "y"))
993 1.1 mbalmer </pre><p>
994 1.2 lneto When you run it, it produces the following output:
995 1.1 mbalmer
996 1.2 lneto <pre>
997 1.2 lneto co-body 1 10
998 1.2 lneto foo 2
999 1.2 lneto main true 4
1000 1.2 lneto co-body r
1001 1.2 lneto main true 11 -9
1002 1.2 lneto co-body x y
1003 1.2 lneto main true 10 end
1004 1.2 lneto main false cannot resume dead coroutine
1005 1.2 lneto </pre>
1006 1.1 mbalmer
1007 1.2 lneto <p>
1008 1.2 lneto You can also create and manipulate coroutines through the C API:
1009 1.2 lneto see functions <a href="#lua_newthread"><code>lua_newthread</code></a>, <a href="#lua_resume"><code>lua_resume</code></a>,
1010 1.2 lneto and <a href="#lua_yield"><code>lua_yield</code></a>.
1011 1.1 mbalmer
1012 1.1 mbalmer
1013 1.1 mbalmer
1014 1.1 mbalmer
1015 1.1 mbalmer
1016 1.2 lneto <h1>3 – <a name="3">The Language</a></h1>
1017 1.1 mbalmer
1018 1.1 mbalmer <p>
1019 1.2 lneto This section describes the lexis, the syntax, and the semantics of Lua.
1020 1.2 lneto In other words,
1021 1.2 lneto this section describes
1022 1.2 lneto which tokens are valid,
1023 1.2 lneto how they can be combined,
1024 1.2 lneto and what their combinations mean.
1025 1.1 mbalmer
1026 1.1 mbalmer
1027 1.1 mbalmer <p>
1028 1.2 lneto Language constructs will be explained using the usual extended BNF notation,
1029 1.2 lneto in which
1030 1.2 lneto {<em>a</em>} means 0 or more <em>a</em>'s, and
1031 1.2 lneto [<em>a</em>] means an optional <em>a</em>.
1032 1.2 lneto Non-terminals are shown like non-terminal,
1033 1.2 lneto keywords are shown like <b>kword</b>,
1034 1.2 lneto and other terminal symbols are shown like ‘<b>=</b>’.
1035 1.2 lneto The complete syntax of Lua can be found in <a href="#9">§9</a>
1036 1.2 lneto at the end of this manual.
1037 1.1 mbalmer
1038 1.1 mbalmer
1039 1.1 mbalmer
1040 1.2 lneto <h2>3.1 – <a name="3.1">Lexical Conventions</a></h2>
1041 1.1 mbalmer
1042 1.1 mbalmer <p>
1043 1.2 lneto Lua is a free-form language.
1044 1.2 lneto It ignores spaces (including new lines) and comments
1045 1.2 lneto between lexical elements (tokens),
1046 1.2 lneto except as delimiters between names and keywords.
1047 1.1 mbalmer
1048 1.1 mbalmer
1049 1.1 mbalmer <p>
1050 1.2 lneto <em>Names</em>
1051 1.2 lneto (also called <em>identifiers</em>)
1052 1.2 lneto in Lua can be any string of letters,
1053 1.2 lneto digits, and underscores,
1054 1.5 lneto not beginning with a digit and
1055 1.5 lneto not being a reserved word.
1056 1.2 lneto Identifiers are used to name variables, table fields, and labels.
1057 1.1 mbalmer
1058 1.1 mbalmer
1059 1.2 lneto <p>
1060 1.2 lneto The following <em>keywords</em> are reserved
1061 1.2 lneto and cannot be used as names:
1062 1.1 mbalmer
1063 1.1 mbalmer
1064 1.2 lneto <pre>
1065 1.2 lneto and break do else elseif end
1066 1.2 lneto false for function goto if in
1067 1.2 lneto local nil not or repeat return
1068 1.2 lneto then true until while
1069 1.2 lneto </pre>
1070 1.1 mbalmer
1071 1.2 lneto <p>
1072 1.2 lneto Lua is a case-sensitive language:
1073 1.2 lneto <code>and</code> is a reserved word, but <code>And</code> and <code>AND</code>
1074 1.2 lneto are two different, valid names.
1075 1.2 lneto As a convention,
1076 1.2 lneto programs should avoid creating
1077 1.2 lneto names that start with an underscore followed by
1078 1.2 lneto one or more uppercase letters (such as <a href="#pdf-_VERSION"><code>_VERSION</code></a>).
1079 1.1 mbalmer
1080 1.1 mbalmer
1081 1.1 mbalmer <p>
1082 1.2 lneto The following strings denote other tokens:
1083 1.1 mbalmer
1084 1.1 mbalmer <pre>
1085 1.2 lneto + - * / % ^ #
1086 1.2 lneto & ~ | << >> //
1087 1.2 lneto == ~= <= >= < > =
1088 1.2 lneto ( ) { } [ ] ::
1089 1.2 lneto ; : , . .. ...
1090 1.2 lneto </pre>
1091 1.1 mbalmer
1092 1.1 mbalmer <p>
1093 1.2 lneto <em>Literal strings</em>
1094 1.2 lneto can be delimited by matching single or double quotes,
1095 1.2 lneto and can contain the following C-like escape sequences:
1096 1.2 lneto '<code>\a</code>' (bell),
1097 1.2 lneto '<code>\b</code>' (backspace),
1098 1.2 lneto '<code>\f</code>' (form feed),
1099 1.2 lneto '<code>\n</code>' (newline),
1100 1.2 lneto '<code>\r</code>' (carriage return),
1101 1.2 lneto '<code>\t</code>' (horizontal tab),
1102 1.2 lneto '<code>\v</code>' (vertical tab),
1103 1.2 lneto '<code>\\</code>' (backslash),
1104 1.2 lneto '<code>\"</code>' (quotation mark [double quote]),
1105 1.2 lneto and '<code>\'</code>' (apostrophe [single quote]).
1106 1.2 lneto A backslash followed by a real newline
1107 1.2 lneto results in a newline in the string.
1108 1.2 lneto The escape sequence '<code>\z</code>' skips the following span
1109 1.2 lneto of white-space characters,
1110 1.2 lneto including line breaks;
1111 1.2 lneto it is particularly useful to break and indent a long literal string
1112 1.2 lneto into multiple lines without adding the newlines and spaces
1113 1.2 lneto into the string contents.
1114 1.1 mbalmer
1115 1.1 mbalmer
1116 1.1 mbalmer <p>
1117 1.3 lneto Strings in Lua can contain any 8-bit value, including embedded zeros,
1118 1.3 lneto which can be specified as '<code>\0</code>'.
1119 1.3 lneto More generally,
1120 1.4 mbalmer we can specify any byte in a literal string by its numeric value.
1121 1.3 lneto This can be done
1122 1.3 lneto with the escape sequence <code>\x<em>XX</em></code>,
1123 1.2 lneto where <em>XX</em> is a sequence of exactly two hexadecimal digits,
1124 1.2 lneto or with the escape sequence <code>\<em>ddd</em></code>,
1125 1.2 lneto where <em>ddd</em> is a sequence of up to three decimal digits.
1126 1.3 lneto (Note that if a decimal escape sequence is to be followed by a digit,
1127 1.2 lneto it must be expressed using exactly three digits.)
1128 1.1 mbalmer
1129 1.1 mbalmer
1130 1.2 lneto <p>
1131 1.2 lneto The UTF-8 encoding of a Unicode character
1132 1.2 lneto can be inserted in a literal string with
1133 1.2 lneto the escape sequence <code>\u{<em>XXX</em>}</code>
1134 1.2 lneto (note the mandatory enclosing brackets),
1135 1.2 lneto where <em>XXX</em> is a sequence of one or more hexadecimal digits
1136 1.2 lneto representing the character code point.
1137 1.1 mbalmer
1138 1.1 mbalmer
1139 1.2 lneto <p>
1140 1.2 lneto Literal strings can also be defined using a long format
1141 1.2 lneto enclosed by <em>long brackets</em>.
1142 1.2 lneto We define an <em>opening long bracket of level <em>n</em></em> as an opening
1143 1.2 lneto square bracket followed by <em>n</em> equal signs followed by another
1144 1.2 lneto opening square bracket.
1145 1.2 lneto So, an opening long bracket of level 0 is written as <code>[[</code>,
1146 1.2 lneto an opening long bracket of level 1 is written as <code>[=[</code>,
1147 1.2 lneto and so on.
1148 1.2 lneto A <em>closing long bracket</em> is defined similarly;
1149 1.2 lneto for instance,
1150 1.2 lneto a closing long bracket of level 4 is written as <code>]====]</code>.
1151 1.2 lneto A <em>long literal</em> starts with an opening long bracket of any level and
1152 1.2 lneto ends at the first closing long bracket of the same level.
1153 1.3 lneto It can contain any text except a closing bracket of the same level.
1154 1.2 lneto Literals in this bracketed form can run for several lines,
1155 1.2 lneto do not interpret any escape sequences,
1156 1.2 lneto and ignore long brackets of any other level.
1157 1.2 lneto Any kind of end-of-line sequence
1158 1.2 lneto (carriage return, newline, carriage return followed by newline,
1159 1.2 lneto or newline followed by carriage return)
1160 1.2 lneto is converted to a simple newline.
1161 1.1 mbalmer
1162 1.1 mbalmer
1163 1.2 lneto <p>
1164 1.2 lneto Any byte in a literal string not
1165 1.2 lneto explicitly affected by the previous rules represents itself.
1166 1.2 lneto However, Lua opens files for parsing in text mode,
1167 1.2 lneto and the system file functions may have problems with
1168 1.2 lneto some control characters.
1169 1.2 lneto So, it is safer to represent
1170 1.2 lneto non-text data as a quoted literal with
1171 1.2 lneto explicit escape sequences for non-text characters.
1172 1.1 mbalmer
1173 1.1 mbalmer
1174 1.2 lneto <p>
1175 1.2 lneto For convenience,
1176 1.2 lneto when the opening long bracket is immediately followed by a newline,
1177 1.2 lneto the newline is not included in the string.
1178 1.2 lneto As an example, in a system using ASCII
1179 1.2 lneto (in which '<code>a</code>' is coded as 97,
1180 1.2 lneto newline is coded as 10, and '<code>1</code>' is coded as 49),
1181 1.2 lneto the five literal strings below denote the same string:
1182 1.1 mbalmer
1183 1.1 mbalmer <pre>
1184 1.2 lneto a = 'alo\n123"'
1185 1.2 lneto a = "alo\n123\""
1186 1.2 lneto a = '\97lo\10\04923"'
1187 1.2 lneto a = [[alo
1188 1.2 lneto 123"]]
1189 1.2 lneto a = [==[
1190 1.2 lneto alo
1191 1.2 lneto 123"]==]
1192 1.1 mbalmer </pre>
1193 1.1 mbalmer
1194 1.1 mbalmer <p>
1195 1.4 mbalmer A <em>numeric constant</em> (or <em>numeral</em>)
1196 1.3 lneto can be written with an optional fractional part
1197 1.2 lneto and an optional decimal exponent,
1198 1.2 lneto marked by a letter '<code>e</code>' or '<code>E</code>'.
1199 1.2 lneto Lua also accepts hexadecimal constants,
1200 1.2 lneto which start with <code>0x</code> or <code>0X</code>.
1201 1.2 lneto Hexadecimal constants also accept an optional fractional part
1202 1.2 lneto plus an optional binary exponent,
1203 1.2 lneto marked by a letter '<code>p</code>' or '<code>P</code>'.
1204 1.2 lneto A numeric constant with a fractional dot or an exponent
1205 1.2 lneto denotes a float;
1206 1.2 lneto otherwise it denotes an integer.
1207 1.2 lneto Examples of valid integer constants are
1208 1.1 mbalmer
1209 1.1 mbalmer <pre>
1210 1.2 lneto 3 345 0xff 0xBEBADA
1211 1.1 mbalmer </pre><p>
1212 1.2 lneto Examples of valid float constants are
1213 1.1 mbalmer
1214 1.1 mbalmer <pre>
1215 1.2 lneto 3.0 3.1416 314.16e-2 0.31416E1 34e1
1216 1.2 lneto 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1
1217 1.1 mbalmer </pre>
1218 1.1 mbalmer
1219 1.1 mbalmer <p>
1220 1.2 lneto A <em>comment</em> starts with a double hyphen (<code>--</code>)
1221 1.2 lneto anywhere outside a string.
1222 1.2 lneto If the text immediately after <code>--</code> is not an opening long bracket,
1223 1.2 lneto the comment is a <em>short comment</em>,
1224 1.2 lneto which runs until the end of the line.
1225 1.2 lneto Otherwise, it is a <em>long comment</em>,
1226 1.2 lneto which runs until the corresponding closing long bracket.
1227 1.2 lneto Long comments are frequently used to disable code temporarily.
1228 1.2 lneto
1229 1.1 mbalmer
1230 1.1 mbalmer
1231 1.1 mbalmer
1232 1.1 mbalmer
1233 1.2 lneto <h2>3.2 – <a name="3.2">Variables</a></h2>
1234 1.1 mbalmer
1235 1.2 lneto <p>
1236 1.2 lneto Variables are places that store values.
1237 1.2 lneto There are three kinds of variables in Lua:
1238 1.2 lneto global variables, local variables, and table fields.
1239 1.1 mbalmer
1240 1.1 mbalmer
1241 1.2 lneto <p>
1242 1.2 lneto A single name can denote a global variable or a local variable
1243 1.2 lneto (or a function's formal parameter,
1244 1.2 lneto which is a particular kind of local variable):
1245 1.1 mbalmer
1246 1.1 mbalmer <pre>
1247 1.2 lneto var ::= Name
1248 1.1 mbalmer </pre><p>
1249 1.2 lneto Name denotes identifiers, as defined in <a href="#3.1">§3.1</a>.
1250 1.1 mbalmer
1251 1.1 mbalmer
1252 1.1 mbalmer <p>
1253 1.2 lneto Any variable name is assumed to be global unless explicitly declared
1254 1.2 lneto as a local (see <a href="#3.3.7">§3.3.7</a>).
1255 1.2 lneto Local variables are <em>lexically scoped</em>:
1256 1.2 lneto local variables can be freely accessed by functions
1257 1.2 lneto defined inside their scope (see <a href="#3.5">§3.5</a>).
1258 1.2 lneto
1259 1.1 mbalmer
1260 1.2 lneto <p>
1261 1.2 lneto Before the first assignment to a variable, its value is <b>nil</b>.
1262 1.1 mbalmer
1263 1.1 mbalmer
1264 1.1 mbalmer <p>
1265 1.2 lneto Square brackets are used to index a table:
1266 1.1 mbalmer
1267 1.1 mbalmer <pre>
1268 1.2 lneto var ::= prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’
1269 1.1 mbalmer </pre><p>
1270 1.2 lneto The meaning of accesses to table fields can be changed via metatables.
1271 1.2 lneto An access to an indexed variable <code>t[i]</code> is equivalent to
1272 1.2 lneto a call <code>gettable_event(t,i)</code>.
1273 1.2 lneto (See <a href="#2.4">§2.4</a> for a complete description of the
1274 1.2 lneto <code>gettable_event</code> function.
1275 1.2 lneto This function is not defined or callable in Lua.
1276 1.2 lneto We use it here only for explanatory purposes.)
1277 1.1 mbalmer
1278 1.1 mbalmer
1279 1.1 mbalmer <p>
1280 1.2 lneto The syntax <code>var.Name</code> is just syntactic sugar for
1281 1.2 lneto <code>var["Name"]</code>:
1282 1.1 mbalmer
1283 1.1 mbalmer <pre>
1284 1.2 lneto var ::= prefixexp ‘<b>.</b>’ Name
1285 1.2 lneto </pre>
1286 1.1 mbalmer
1287 1.1 mbalmer <p>
1288 1.2 lneto An access to a global variable <code>x</code>
1289 1.2 lneto is equivalent to <code>_ENV.x</code>.
1290 1.2 lneto Due to the way that chunks are compiled,
1291 1.2 lneto <code>_ENV</code> is never a global name (see <a href="#2.2">§2.2</a>).
1292 1.1 mbalmer
1293 1.1 mbalmer
1294 1.1 mbalmer
1295 1.1 mbalmer
1296 1.1 mbalmer
1297 1.2 lneto <h2>3.3 – <a name="3.3">Statements</a></h2>
1298 1.1 mbalmer
1299 1.1 mbalmer <p>
1300 1.2 lneto Lua supports an almost conventional set of statements,
1301 1.2 lneto similar to those in Pascal or C.
1302 1.2 lneto This set includes
1303 1.2 lneto assignments, control structures, function calls,
1304 1.2 lneto and variable declarations.
1305 1.2 lneto
1306 1.2 lneto
1307 1.1 mbalmer
1308 1.2 lneto <h3>3.3.1 – <a name="3.3.1">Blocks</a></h3>
1309 1.1 mbalmer
1310 1.1 mbalmer <p>
1311 1.2 lneto A block is a list of statements,
1312 1.2 lneto which are executed sequentially:
1313 1.1 mbalmer
1314 1.1 mbalmer <pre>
1315 1.2 lneto block ::= {stat}
1316 1.1 mbalmer </pre><p>
1317 1.2 lneto Lua has <em>empty statements</em>
1318 1.2 lneto that allow you to separate statements with semicolons,
1319 1.2 lneto start a block with a semicolon
1320 1.2 lneto or write two semicolons in sequence:
1321 1.1 mbalmer
1322 1.1 mbalmer <pre>
1323 1.2 lneto stat ::= ‘<b>;</b>’
1324 1.2 lneto </pre>
1325 1.2 lneto
1326 1.2 lneto <p>
1327 1.2 lneto Function calls and assignments
1328 1.2 lneto can start with an open parenthesis.
1329 1.2 lneto This possibility leads to an ambiguity in Lua's grammar.
1330 1.2 lneto Consider the following fragment:
1331 1.1 mbalmer
1332 1.1 mbalmer <pre>
1333 1.2 lneto a = b + c
1334 1.2 lneto (print or io.write)('done')
1335 1.1 mbalmer </pre><p>
1336 1.2 lneto The grammar could see it in two ways:
1337 1.1 mbalmer
1338 1.1 mbalmer <pre>
1339 1.2 lneto a = b + c(print or io.write)('done')
1340 1.2 lneto
1341 1.2 lneto a = b + c; (print or io.write)('done')
1342 1.1 mbalmer </pre><p>
1343 1.2 lneto The current parser always sees such constructions
1344 1.2 lneto in the first way,
1345 1.2 lneto interpreting the open parenthesis
1346 1.2 lneto as the start of the arguments to a call.
1347 1.2 lneto To avoid this ambiguity,
1348 1.2 lneto it is a good practice to always precede with a semicolon
1349 1.2 lneto statements that start with a parenthesis:
1350 1.1 mbalmer
1351 1.1 mbalmer <pre>
1352 1.2 lneto ;(print or io.write)('done')
1353 1.2 lneto </pre>
1354 1.2 lneto
1355 1.2 lneto <p>
1356 1.2 lneto A block can be explicitly delimited to produce a single statement:
1357 1.1 mbalmer
1358 1.1 mbalmer <pre>
1359 1.2 lneto stat ::= <b>do</b> block <b>end</b>
1360 1.1 mbalmer </pre><p>
1361 1.2 lneto Explicit blocks are useful
1362 1.2 lneto to control the scope of variable declarations.
1363 1.2 lneto Explicit blocks are also sometimes used to
1364 1.2 lneto add a <b>return</b> statement in the middle
1365 1.2 lneto of another block (see <a href="#3.3.4">§3.3.4</a>).
1366 1.2 lneto
1367 1.2 lneto
1368 1.2 lneto
1369 1.2 lneto
1370 1.2 lneto
1371 1.2 lneto <h3>3.3.2 – <a name="3.3.2">Chunks</a></h3>
1372 1.2 lneto
1373 1.2 lneto <p>
1374 1.2 lneto The unit of compilation of Lua is called a <em>chunk</em>.
1375 1.2 lneto Syntactically,
1376 1.2 lneto a chunk is simply a block:
1377 1.1 mbalmer
1378 1.1 mbalmer <pre>
1379 1.2 lneto chunk ::= block
1380 1.2 lneto </pre>
1381 1.1 mbalmer
1382 1.2 lneto <p>
1383 1.2 lneto Lua handles a chunk as the body of an anonymous function
1384 1.2 lneto with a variable number of arguments
1385 1.2 lneto (see <a href="#3.4.11">§3.4.11</a>).
1386 1.2 lneto As such, chunks can define local variables,
1387 1.2 lneto receive arguments, and return values.
1388 1.2 lneto Moreover, such anonymous function is compiled as in the
1389 1.2 lneto scope of an external local variable called <code>_ENV</code> (see <a href="#2.2">§2.2</a>).
1390 1.2 lneto The resulting function always has <code>_ENV</code> as its only upvalue,
1391 1.2 lneto even if it does not use that variable.
1392 1.1 mbalmer
1393 1.1 mbalmer
1394 1.1 mbalmer <p>
1395 1.2 lneto A chunk can be stored in a file or in a string inside the host program.
1396 1.2 lneto To execute a chunk,
1397 1.3 lneto Lua first <em>loads</em> it,
1398 1.3 lneto precompiling the chunk's code into instructions for a virtual machine,
1399 1.3 lneto and then Lua executes the compiled code
1400 1.2 lneto with an interpreter for the virtual machine.
1401 1.1 mbalmer
1402 1.1 mbalmer
1403 1.1 mbalmer <p>
1404 1.2 lneto Chunks can also be precompiled into binary form;
1405 1.2 lneto see program <code>luac</code> and function <a href="#pdf-string.dump"><code>string.dump</code></a> for details.
1406 1.2 lneto Programs in source and compiled forms are interchangeable;
1407 1.2 lneto Lua automatically detects the file type and acts accordingly (see <a href="#pdf-load"><code>load</code></a>).
1408 1.2 lneto
1409 1.2 lneto
1410 1.2 lneto
1411 1.1 mbalmer
1412 1.1 mbalmer
1413 1.2 lneto <h3>3.3.3 – <a name="3.3.3">Assignment</a></h3>
1414 1.1 mbalmer
1415 1.1 mbalmer <p>
1416 1.2 lneto Lua allows multiple assignments.
1417 1.2 lneto Therefore, the syntax for assignment
1418 1.2 lneto defines a list of variables on the left side
1419 1.2 lneto and a list of expressions on the right side.
1420 1.2 lneto The elements in both lists are separated by commas:
1421 1.1 mbalmer
1422 1.1 mbalmer <pre>
1423 1.2 lneto stat ::= varlist ‘<b>=</b>’ explist
1424 1.2 lneto varlist ::= var {‘<b>,</b>’ var}
1425 1.2 lneto explist ::= exp {‘<b>,</b>’ exp}
1426 1.1 mbalmer </pre><p>
1427 1.2 lneto Expressions are discussed in <a href="#3.4">§3.4</a>.
1428 1.1 mbalmer
1429 1.1 mbalmer
1430 1.1 mbalmer <p>
1431 1.2 lneto Before the assignment,
1432 1.2 lneto the list of values is <em>adjusted</em> to the length of
1433 1.2 lneto the list of variables.
1434 1.2 lneto If there are more values than needed,
1435 1.2 lneto the excess values are thrown away.
1436 1.2 lneto If there are fewer values than needed,
1437 1.2 lneto the list is extended with as many <b>nil</b>'s as needed.
1438 1.2 lneto If the list of expressions ends with a function call,
1439 1.2 lneto then all values returned by that call enter the list of values,
1440 1.2 lneto before the adjustment
1441 1.2 lneto (except when the call is enclosed in parentheses; see <a href="#3.4">§3.4</a>).
1442 1.1 mbalmer
1443 1.1 mbalmer
1444 1.1 mbalmer <p>
1445 1.2 lneto The assignment statement first evaluates all its expressions
1446 1.3 lneto and only then the assignments are performed.
1447 1.2 lneto Thus the code
1448 1.1 mbalmer
1449 1.1 mbalmer <pre>
1450 1.2 lneto i = 3
1451 1.2 lneto i, a[i] = i+1, 20
1452 1.1 mbalmer </pre><p>
1453 1.2 lneto sets <code>a[3]</code> to 20, without affecting <code>a[4]</code>
1454 1.2 lneto because the <code>i</code> in <code>a[i]</code> is evaluated (to 3)
1455 1.2 lneto before it is assigned 4.
1456 1.2 lneto Similarly, the line
1457 1.1 mbalmer
1458 1.1 mbalmer <pre>
1459 1.2 lneto x, y = y, x
1460 1.2 lneto </pre><p>
1461 1.2 lneto exchanges the values of <code>x</code> and <code>y</code>,
1462 1.2 lneto and
1463 1.1 mbalmer
1464 1.2 lneto <pre>
1465 1.2 lneto x, y, z = y, z, x
1466 1.2 lneto </pre><p>
1467 1.2 lneto cyclically permutes the values of <code>x</code>, <code>y</code>, and <code>z</code>.
1468 1.1 mbalmer
1469 1.1 mbalmer
1470 1.2 lneto <p>
1471 1.2 lneto The meaning of assignments to global variables
1472 1.2 lneto and table fields can be changed via metatables.
1473 1.2 lneto An assignment to an indexed variable <code>t[i] = val</code> is equivalent to
1474 1.2 lneto <code>settable_event(t,i,val)</code>.
1475 1.2 lneto (See <a href="#2.4">§2.4</a> for a complete description of the
1476 1.2 lneto <code>settable_event</code> function.
1477 1.2 lneto This function is not defined or callable in Lua.
1478 1.2 lneto We use it here only for explanatory purposes.)
1479 1.1 mbalmer
1480 1.1 mbalmer
1481 1.2 lneto <p>
1482 1.3 lneto An assignment to a global name <code>x = val</code>
1483 1.2 lneto is equivalent to the assignment
1484 1.2 lneto <code>_ENV.x = val</code> (see <a href="#2.2">§2.2</a>).
1485 1.1 mbalmer
1486 1.1 mbalmer
1487 1.1 mbalmer
1488 1.1 mbalmer
1489 1.1 mbalmer
1490 1.2 lneto <h3>3.3.4 – <a name="3.3.4">Control Structures</a></h3><p>
1491 1.2 lneto The control structures
1492 1.2 lneto <b>if</b>, <b>while</b>, and <b>repeat</b> have the usual meaning and
1493 1.2 lneto familiar syntax:
1494 1.1 mbalmer
1495 1.1 mbalmer
1496 1.1 mbalmer
1497 1.1 mbalmer
1498 1.1 mbalmer <pre>
1499 1.2 lneto stat ::= <b>while</b> exp <b>do</b> block <b>end</b>
1500 1.2 lneto stat ::= <b>repeat</b> block <b>until</b> exp
1501 1.2 lneto stat ::= <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b>
1502 1.1 mbalmer </pre><p>
1503 1.2 lneto Lua also has a <b>for</b> statement, in two flavors (see <a href="#3.3.5">§3.3.5</a>).
1504 1.1 mbalmer
1505 1.1 mbalmer
1506 1.2 lneto <p>
1507 1.2 lneto The condition expression of a
1508 1.2 lneto control structure can return any value.
1509 1.2 lneto Both <b>false</b> and <b>nil</b> are considered false.
1510 1.2 lneto All values different from <b>nil</b> and <b>false</b> are considered true
1511 1.2 lneto (in particular, the number 0 and the empty string are also true).
1512 1.1 mbalmer
1513 1.1 mbalmer
1514 1.1 mbalmer <p>
1515 1.2 lneto In the <b>repeat</b>–<b>until</b> loop,
1516 1.2 lneto the inner block does not end at the <b>until</b> keyword,
1517 1.2 lneto but only after the condition.
1518 1.2 lneto So, the condition can refer to local variables
1519 1.2 lneto declared inside the loop block.
1520 1.1 mbalmer
1521 1.1 mbalmer
1522 1.1 mbalmer <p>
1523 1.2 lneto The <b>goto</b> statement transfers the program control to a label.
1524 1.2 lneto For syntactical reasons,
1525 1.2 lneto labels in Lua are considered statements too:
1526 1.1 mbalmer
1527 1.1 mbalmer
1528 1.1 mbalmer
1529 1.2 lneto <pre>
1530 1.2 lneto stat ::= <b>goto</b> Name
1531 1.2 lneto stat ::= label
1532 1.2 lneto label ::= ‘<b>::</b>’ Name ‘<b>::</b>’
1533 1.2 lneto </pre>
1534 1.1 mbalmer
1535 1.1 mbalmer <p>
1536 1.2 lneto A label is visible in the entire block where it is defined,
1537 1.2 lneto except
1538 1.2 lneto inside nested blocks where a label with the same name is defined and
1539 1.2 lneto inside nested functions.
1540 1.2 lneto A goto may jump to any visible label as long as it does not
1541 1.2 lneto enter into the scope of a local variable.
1542 1.1 mbalmer
1543 1.1 mbalmer
1544 1.1 mbalmer <p>
1545 1.2 lneto Labels and empty statements are called <em>void statements</em>,
1546 1.2 lneto as they perform no actions.
1547 1.1 mbalmer
1548 1.1 mbalmer
1549 1.1 mbalmer <p>
1550 1.2 lneto The <b>break</b> statement terminates the execution of a
1551 1.2 lneto <b>while</b>, <b>repeat</b>, or <b>for</b> loop,
1552 1.2 lneto skipping to the next statement after the loop:
1553 1.1 mbalmer
1554 1.1 mbalmer
1555 1.2 lneto <pre>
1556 1.2 lneto stat ::= <b>break</b>
1557 1.2 lneto </pre><p>
1558 1.2 lneto A <b>break</b> ends the innermost enclosing loop.
1559 1.1 mbalmer
1560 1.1 mbalmer
1561 1.1 mbalmer <p>
1562 1.2 lneto The <b>return</b> statement is used to return values
1563 1.3 lneto from a function or a chunk
1564 1.3 lneto (which is an anonymous function).
1565 1.1 mbalmer
1566 1.2 lneto Functions can return more than one value,
1567 1.2 lneto so the syntax for the <b>return</b> statement is
1568 1.1 mbalmer
1569 1.2 lneto <pre>
1570 1.2 lneto stat ::= <b>return</b> [explist] [‘<b>;</b>’]
1571 1.2 lneto </pre>
1572 1.1 mbalmer
1573 1.1 mbalmer <p>
1574 1.2 lneto The <b>return</b> statement can only be written
1575 1.2 lneto as the last statement of a block.
1576 1.2 lneto If it is really necessary to <b>return</b> in the middle of a block,
1577 1.2 lneto then an explicit inner block can be used,
1578 1.2 lneto as in the idiom <code>do return end</code>,
1579 1.2 lneto because now <b>return</b> is the last statement in its (inner) block.
1580 1.1 mbalmer
1581 1.1 mbalmer
1582 1.1 mbalmer
1583 1.1 mbalmer
1584 1.1 mbalmer
1585 1.2 lneto <h3>3.3.5 – <a name="3.3.5">For Statement</a></h3>
1586 1.1 mbalmer
1587 1.2 lneto <p>
1588 1.1 mbalmer
1589 1.2 lneto The <b>for</b> statement has two forms:
1590 1.4 mbalmer one numerical and one generic.
1591 1.1 mbalmer
1592 1.1 mbalmer
1593 1.2 lneto <p>
1594 1.4 mbalmer The numerical <b>for</b> loop repeats a block of code while a
1595 1.2 lneto control variable runs through an arithmetic progression.
1596 1.2 lneto It has the following syntax:
1597 1.1 mbalmer
1598 1.2 lneto <pre>
1599 1.2 lneto stat ::= <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b>
1600 1.2 lneto </pre><p>
1601 1.2 lneto The <em>block</em> is repeated for <em>name</em> starting at the value of
1602 1.2 lneto the first <em>exp</em>, until it passes the second <em>exp</em> by steps of the
1603 1.2 lneto third <em>exp</em>.
1604 1.2 lneto More precisely, a <b>for</b> statement like
1605 1.1 mbalmer
1606 1.1 mbalmer <pre>
1607 1.2 lneto for v = <em>e1</em>, <em>e2</em>, <em>e3</em> do <em>block</em> end
1608 1.1 mbalmer </pre><p>
1609 1.2 lneto is equivalent to the code:
1610 1.1 mbalmer
1611 1.1 mbalmer <pre>
1612 1.2 lneto do
1613 1.2 lneto local <em>var</em>, <em>limit</em>, <em>step</em> = tonumber(<em>e1</em>), tonumber(<em>e2</em>), tonumber(<em>e3</em>)
1614 1.2 lneto if not (<em>var</em> and <em>limit</em> and <em>step</em>) then error() end
1615 1.2 lneto <em>var</em> = <em>var</em> - <em>step</em>
1616 1.2 lneto while true do
1617 1.2 lneto <em>var</em> = <em>var</em> + <em>step</em>
1618 1.2 lneto if (<em>step</em> >= 0 and <em>var</em> > <em>limit</em>) or (<em>step</em> < 0 and <em>var</em> < <em>limit</em>) then
1619 1.2 lneto break
1620 1.1 mbalmer end
1621 1.2 lneto local v = <em>var</em>
1622 1.2 lneto <em>block</em>
1623 1.1 mbalmer end
1624 1.1 mbalmer end
1625 1.2 lneto </pre>
1626 1.2 lneto
1627 1.2 lneto <p>
1628 1.2 lneto Note the following:
1629 1.1 mbalmer
1630 1.2 lneto <ul>
1631 1.1 mbalmer
1632 1.2 lneto <li>
1633 1.2 lneto All three control expressions are evaluated only once,
1634 1.2 lneto before the loop starts.
1635 1.2 lneto They must all result in numbers.
1636 1.1 mbalmer </li>
1637 1.1 mbalmer
1638 1.2 lneto <li>
1639 1.2 lneto <code><em>var</em></code>, <code><em>limit</em></code>, and <code><em>step</em></code> are invisible variables.
1640 1.2 lneto The names shown here are for explanatory purposes only.
1641 1.1 mbalmer </li>
1642 1.1 mbalmer
1643 1.2 lneto <li>
1644 1.2 lneto If the third expression (the step) is absent,
1645 1.2 lneto then a step of 1 is used.
1646 1.2 lneto </li>
1647 1.1 mbalmer
1648 1.2 lneto <li>
1649 1.2 lneto You can use <b>break</b> and <b>goto</b> to exit a <b>for</b> loop.
1650 1.1 mbalmer </li>
1651 1.1 mbalmer
1652 1.2 lneto <li>
1653 1.2 lneto The loop variable <code>v</code> is local to the loop body.
1654 1.2 lneto If you need its value after the loop,
1655 1.2 lneto assign it to another variable before exiting the loop.
1656 1.1 mbalmer </li>
1657 1.1 mbalmer
1658 1.2 lneto </ul>
1659 1.1 mbalmer
1660 1.2 lneto <p>
1661 1.2 lneto The generic <b>for</b> statement works over functions,
1662 1.2 lneto called <em>iterators</em>.
1663 1.2 lneto On each iteration, the iterator function is called to produce a new value,
1664 1.2 lneto stopping when this new value is <b>nil</b>.
1665 1.2 lneto The generic <b>for</b> loop has the following syntax:
1666 1.1 mbalmer
1667 1.2 lneto <pre>
1668 1.2 lneto stat ::= <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b>
1669 1.2 lneto namelist ::= Name {‘<b>,</b>’ Name}
1670 1.2 lneto </pre><p>
1671 1.2 lneto A <b>for</b> statement like
1672 1.1 mbalmer
1673 1.2 lneto <pre>
1674 1.2 lneto for <em>var_1</em>, ···, <em>var_n</em> in <em>explist</em> do <em>block</em> end
1675 1.2 lneto </pre><p>
1676 1.2 lneto is equivalent to the code:
1677 1.1 mbalmer
1678 1.1 mbalmer <pre>
1679 1.2 lneto do
1680 1.2 lneto local <em>f</em>, <em>s</em>, <em>var</em> = <em>explist</em>
1681 1.2 lneto while true do
1682 1.2 lneto local <em>var_1</em>, ···, <em>var_n</em> = <em>f</em>(<em>s</em>, <em>var</em>)
1683 1.2 lneto if <em>var_1</em> == nil then break end
1684 1.2 lneto <em>var</em> = <em>var_1</em>
1685 1.2 lneto <em>block</em>
1686 1.1 mbalmer end
1687 1.1 mbalmer end
1688 1.1 mbalmer </pre><p>
1689 1.2 lneto Note the following:
1690 1.2 lneto
1691 1.2 lneto <ul>
1692 1.2 lneto
1693 1.2 lneto <li>
1694 1.2 lneto <code><em>explist</em></code> is evaluated only once.
1695 1.2 lneto Its results are an <em>iterator</em> function,
1696 1.2 lneto a <em>state</em>,
1697 1.2 lneto and an initial value for the first <em>iterator variable</em>.
1698 1.1 mbalmer </li>
1699 1.1 mbalmer
1700 1.2 lneto <li>
1701 1.2 lneto <code><em>f</em></code>, <code><em>s</em></code>, and <code><em>var</em></code> are invisible variables.
1702 1.2 lneto The names are here for explanatory purposes only.
1703 1.2 lneto </li>
1704 1.1 mbalmer
1705 1.2 lneto <li>
1706 1.2 lneto You can use <b>break</b> to exit a <b>for</b> loop.
1707 1.2 lneto </li>
1708 1.1 mbalmer
1709 1.2 lneto <li>
1710 1.2 lneto The loop variables <code><em>var_i</em></code> are local to the loop;
1711 1.2 lneto you cannot use their values after the <b>for</b> ends.
1712 1.2 lneto If you need these values,
1713 1.2 lneto then assign them to other variables before breaking or exiting the loop.
1714 1.1 mbalmer </li>
1715 1.1 mbalmer
1716 1.2 lneto </ul>
1717 1.2 lneto
1718 1.2 lneto
1719 1.1 mbalmer
1720 1.1 mbalmer
1721 1.2 lneto <h3>3.3.6 – <a name="3.3.6">Function Calls as Statements</a></h3><p>
1722 1.2 lneto To allow possible side-effects,
1723 1.2 lneto function calls can be executed as statements:
1724 1.2 lneto
1725 1.1 mbalmer <pre>
1726 1.2 lneto stat ::= functioncall
1727 1.1 mbalmer </pre><p>
1728 1.2 lneto In this case, all returned values are thrown away.
1729 1.2 lneto Function calls are explained in <a href="#3.4.10">§3.4.10</a>.
1730 1.2 lneto
1731 1.2 lneto
1732 1.2 lneto
1733 1.1 mbalmer
1734 1.1 mbalmer
1735 1.2 lneto <h3>3.3.7 – <a name="3.3.7">Local Declarations</a></h3><p>
1736 1.2 lneto Local variables can be declared anywhere inside a block.
1737 1.2 lneto The declaration can include an initial assignment:
1738 1.1 mbalmer
1739 1.1 mbalmer <pre>
1740 1.2 lneto stat ::= <b>local</b> namelist [‘<b>=</b>’ explist]
1741 1.1 mbalmer </pre><p>
1742 1.2 lneto If present, an initial assignment has the same semantics
1743 1.2 lneto of a multiple assignment (see <a href="#3.3.3">§3.3.3</a>).
1744 1.2 lneto Otherwise, all variables are initialized with <b>nil</b>.
1745 1.2 lneto
1746 1.2 lneto
1747 1.2 lneto <p>
1748 1.2 lneto A chunk is also a block (see <a href="#3.3.2">§3.3.2</a>),
1749 1.2 lneto and so local variables can be declared in a chunk outside any explicit block.
1750 1.1 mbalmer
1751 1.1 mbalmer
1752 1.2 lneto <p>
1753 1.2 lneto The visibility rules for local variables are explained in <a href="#3.5">§3.5</a>.
1754 1.2 lneto
1755 1.1 mbalmer
1756 1.1 mbalmer
1757 1.1 mbalmer
1758 1.1 mbalmer
1759 1.1 mbalmer
1760 1.1 mbalmer
1761 1.2 lneto <h2>3.4 – <a name="3.4">Expressions</a></h2>
1762 1.1 mbalmer
1763 1.2 lneto <p>
1764 1.2 lneto The basic expressions in Lua are the following:
1765 1.1 mbalmer
1766 1.1 mbalmer <pre>
1767 1.2 lneto exp ::= prefixexp
1768 1.2 lneto exp ::= <b>nil</b> | <b>false</b> | <b>true</b>
1769 1.3 lneto exp ::= Numeral
1770 1.3 lneto exp ::= LiteralString
1771 1.2 lneto exp ::= functiondef
1772 1.2 lneto exp ::= tableconstructor
1773 1.2 lneto exp ::= ‘<b>...</b>’
1774 1.2 lneto exp ::= exp binop exp
1775 1.2 lneto exp ::= unop exp
1776 1.2 lneto prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’
1777 1.2 lneto </pre>
1778 1.2 lneto
1779 1.2 lneto <p>
1780 1.3 lneto Numerals and literal strings are explained in <a href="#3.1">§3.1</a>;
1781 1.2 lneto variables are explained in <a href="#3.2">§3.2</a>;
1782 1.2 lneto function definitions are explained in <a href="#3.4.11">§3.4.11</a>;
1783 1.2 lneto function calls are explained in <a href="#3.4.10">§3.4.10</a>;
1784 1.2 lneto table constructors are explained in <a href="#3.4.9">§3.4.9</a>.
1785 1.2 lneto Vararg expressions,
1786 1.2 lneto denoted by three dots ('<code>...</code>'), can only be used when
1787 1.2 lneto directly inside a vararg function;
1788 1.2 lneto they are explained in <a href="#3.4.11">§3.4.11</a>.
1789 1.2 lneto
1790 1.1 mbalmer
1791 1.2 lneto <p>
1792 1.2 lneto Binary operators comprise arithmetic operators (see <a href="#3.4.1">§3.4.1</a>),
1793 1.2 lneto bitwise operators (see <a href="#3.4.2">§3.4.2</a>),
1794 1.2 lneto relational operators (see <a href="#3.4.4">§3.4.4</a>), logical operators (see <a href="#3.4.5">§3.4.5</a>),
1795 1.2 lneto and the concatenation operator (see <a href="#3.4.6">§3.4.6</a>).
1796 1.2 lneto Unary operators comprise the unary minus (see <a href="#3.4.1">§3.4.1</a>),
1797 1.2 lneto the unary bitwise not (see <a href="#3.4.2">§3.4.2</a>),
1798 1.3 lneto the unary logical <b>not</b> (see <a href="#3.4.5">§3.4.5</a>),
1799 1.2 lneto and the unary <em>length operator</em> (see <a href="#3.4.7">§3.4.7</a>).
1800 1.1 mbalmer
1801 1.1 mbalmer
1802 1.2 lneto <p>
1803 1.2 lneto Both function calls and vararg expressions can result in multiple values.
1804 1.2 lneto If a function call is used as a statement (see <a href="#3.3.6">§3.3.6</a>),
1805 1.2 lneto then its return list is adjusted to zero elements,
1806 1.2 lneto thus discarding all returned values.
1807 1.2 lneto If an expression is used as the last (or the only) element
1808 1.2 lneto of a list of expressions,
1809 1.2 lneto then no adjustment is made
1810 1.2 lneto (unless the expression is enclosed in parentheses).
1811 1.2 lneto In all other contexts,
1812 1.2 lneto Lua adjusts the result list to one element,
1813 1.2 lneto either discarding all values except the first one
1814 1.2 lneto or adding a single <b>nil</b> if there are no values.
1815 1.1 mbalmer
1816 1.1 mbalmer
1817 1.2 lneto <p>
1818 1.2 lneto Here are some examples:
1819 1.1 mbalmer
1820 1.1 mbalmer <pre>
1821 1.2 lneto f() -- adjusted to 0 results
1822 1.2 lneto g(f(), x) -- f() is adjusted to 1 result
1823 1.2 lneto g(x, f()) -- g gets x plus all results from f()
1824 1.2 lneto a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil)
1825 1.2 lneto a,b = ... -- a gets the first vararg parameter, b gets
1826 1.2 lneto -- the second (both a and b can get nil if there
1827 1.2 lneto -- is no corresponding vararg parameter)
1828 1.2 lneto
1829 1.2 lneto a,b,c = x, f() -- f() is adjusted to 2 results
1830 1.2 lneto a,b,c = f() -- f() is adjusted to 3 results
1831 1.2 lneto return f() -- returns all results from f()
1832 1.2 lneto return ... -- returns all received vararg parameters
1833 1.2 lneto return x,y,f() -- returns x, y, and all results from f()
1834 1.2 lneto {f()} -- creates a list with all results from f()
1835 1.2 lneto {...} -- creates a list with all vararg parameters
1836 1.2 lneto {f(), nil} -- f() is adjusted to 1 result
1837 1.2 lneto </pre>
1838 1.2 lneto
1839 1.2 lneto <p>
1840 1.2 lneto Any expression enclosed in parentheses always results in only one value.
1841 1.2 lneto Thus,
1842 1.2 lneto <code>(f(x,y,z))</code> is always a single value,
1843 1.2 lneto even if <code>f</code> returns several values.
1844 1.2 lneto (The value of <code>(f(x,y,z))</code> is the first value returned by <code>f</code>
1845 1.2 lneto or <b>nil</b> if <code>f</code> does not return any values.)
1846 1.2 lneto
1847 1.1 mbalmer
1848 1.1 mbalmer
1849 1.2 lneto <h3>3.4.1 – <a name="3.4.1">Arithmetic Operators</a></h3><p>
1850 1.2 lneto Lua supports the following arithmetic operators:
1851 1.1 mbalmer
1852 1.3 lneto <ul>
1853 1.3 lneto <li><b><code>+</code>: </b>addition</li>
1854 1.3 lneto <li><b><code>-</code>: </b>subtraction</li>
1855 1.3 lneto <li><b><code>*</code>: </b>multiplication</li>
1856 1.3 lneto <li><b><code>/</code>: </b>float division</li>
1857 1.3 lneto <li><b><code>//</code>: </b>floor division</li>
1858 1.3 lneto <li><b><code>%</code>: </b>modulo</li>
1859 1.3 lneto <li><b><code>^</code>: </b>exponentiation</li>
1860 1.3 lneto <li><b><code>-</code>: </b>unary minus</li>
1861 1.3 lneto </ul>
1862 1.1 mbalmer
1863 1.2 lneto <p>
1864 1.3 lneto With the exception of exponentiation and float division,
1865 1.2 lneto the arithmetic operators work as follows:
1866 1.2 lneto If both operands are integers,
1867 1.2 lneto the operation is performed over integers and the result is an integer.
1868 1.2 lneto Otherwise, if both operands are numbers
1869 1.2 lneto or strings that can be converted to
1870 1.2 lneto numbers (see <a href="#3.4.3">§3.4.3</a>),
1871 1.2 lneto then they are converted to floats,
1872 1.2 lneto the operation is performed following the usual rules
1873 1.2 lneto for floating-point arithmetic
1874 1.2 lneto (usually the IEEE 754 standard),
1875 1.2 lneto and the result is a float.
1876 1.1 mbalmer
1877 1.1 mbalmer
1878 1.1 mbalmer <p>
1879 1.3 lneto Exponentiation and float division (<code>/</code>)
1880 1.2 lneto always convert their operands to floats
1881 1.2 lneto and the result is always a float.
1882 1.3 lneto Exponentiation uses the ISO C function <code>pow</code>,
1883 1.2 lneto so that it works for non-integer exponents too.
1884 1.1 mbalmer
1885 1.1 mbalmer
1886 1.1 mbalmer <p>
1887 1.3 lneto Floor division (<code>//</code>) is a division
1888 1.4 mbalmer that rounds the quotient towards minus infinity,
1889 1.3 lneto that is, the floor of the division of its operands.
1890 1.1 mbalmer
1891 1.1 mbalmer
1892 1.1 mbalmer <p>
1893 1.2 lneto Modulo is defined as the remainder of a division
1894 1.4 mbalmer that rounds the quotient towards minus infinity (floor division).
1895 1.1 mbalmer
1896 1.1 mbalmer
1897 1.1 mbalmer <p>
1898 1.2 lneto In case of overflows in integer arithmetic,
1899 1.2 lneto all operations <em>wrap around</em>,
1900 1.2 lneto according to the usual rules of two-complement arithmetic.
1901 1.3 lneto (In other words,
1902 1.3 lneto they return the unique representable integer
1903 1.3 lneto that is equal modulo <em>2<sup>64</sup></em> to the mathematical result.)
1904 1.2 lneto
1905 1.2 lneto
1906 1.2 lneto
1907 1.2 lneto <h3>3.4.2 – <a name="3.4.2">Bitwise Operators</a></h3><p>
1908 1.2 lneto Lua supports the following bitwise operators:
1909 1.1 mbalmer
1910 1.3 lneto <ul>
1911 1.3 lneto <li><b><code>&</code>: </b>bitwise and</li>
1912 1.3 lneto <li><b><code>|</code>: </b>bitwise or</li>
1913 1.3 lneto <li><b><code>~</code>: </b>bitwise exclusive or</li>
1914 1.3 lneto <li><b><code>>></code>: </b>right shift</li>
1915 1.3 lneto <li><b><code><<</code>: </b>left shift</li>
1916 1.3 lneto <li><b><code>~</code>: </b>unary bitwise not</li>
1917 1.3 lneto </ul>
1918 1.1 mbalmer
1919 1.1 mbalmer <p>
1920 1.2 lneto All bitwise operations convert its operands to integers
1921 1.2 lneto (see <a href="#3.4.3">§3.4.3</a>),
1922 1.2 lneto operate on all bits of those integers,
1923 1.2 lneto and result in an integer.
1924 1.1 mbalmer
1925 1.1 mbalmer
1926 1.1 mbalmer <p>
1927 1.2 lneto Both right and left shifts fill the vacant bits with zeros.
1928 1.2 lneto Negative displacements shift to the other direction;
1929 1.2 lneto displacements with absolute values equal to or higher than
1930 1.2 lneto the number of bits in an integer
1931 1.3 lneto result in zero (as all bits are shifted out).
1932 1.2 lneto
1933 1.2 lneto
1934 1.2 lneto
1935 1.2 lneto
1936 1.2 lneto
1937 1.2 lneto <h3>3.4.3 – <a name="3.4.3">Coercions and Conversions</a></h3><p>
1938 1.2 lneto Lua provides some automatic conversions between some
1939 1.2 lneto types and representations at run time.
1940 1.3 lneto Bitwise operators always convert float operands to integers.
1941 1.3 lneto Exponentiation and float division
1942 1.3 lneto always convert integer operands to floats.
1943 1.3 lneto All other arithmetic operations applied to mixed numbers
1944 1.2 lneto (integers and floats) convert the integer operand to a float;
1945 1.2 lneto this is called the <em>usual rule</em>.
1946 1.2 lneto The C API also converts both integers to floats and
1947 1.2 lneto floats to integers, as needed.
1948 1.2 lneto Moreover, string concatenation accepts numbers as arguments,
1949 1.2 lneto besides strings.
1950 1.1 mbalmer
1951 1.1 mbalmer
1952 1.1 mbalmer <p>
1953 1.2 lneto Lua also converts strings to numbers,
1954 1.2 lneto whenever a number is expected.
1955 1.1 mbalmer
1956 1.1 mbalmer
1957 1.2 lneto <p>
1958 1.2 lneto In a conversion from integer to float,
1959 1.2 lneto if the integer value has an exact representation as a float,
1960 1.2 lneto that is the result.
1961 1.2 lneto Otherwise,
1962 1.3 lneto the conversion gets the nearest higher or
1963 1.3 lneto the nearest lower representable value.
1964 1.2 lneto This kind of conversion never fails.
1965 1.1 mbalmer
1966 1.1 mbalmer
1967 1.2 lneto <p>
1968 1.2 lneto The conversion from float to integer
1969 1.3 lneto checks whether the float has an exact representation as an integer
1970 1.3 lneto (that is, the float has an integral value and
1971 1.3 lneto it is in the range of integer representation).
1972 1.3 lneto If it does, that representation is the result.
1973 1.2 lneto Otherwise, the conversion fails.
1974 1.1 mbalmer
1975 1.1 mbalmer
1976 1.1 mbalmer <p>
1977 1.2 lneto The conversion from strings to numbers goes as follows:
1978 1.2 lneto First, the string is converted to an integer or a float,
1979 1.2 lneto following its syntax and the rules of the Lua lexer.
1980 1.2 lneto (The string may have also leading and trailing spaces and a sign.)
1981 1.4 mbalmer Then, the resulting number (float or integer)
1982 1.4 mbalmer is converted to the type (float or integer) required by the context
1983 1.4 mbalmer (e.g., the operation that forced the conversion).
1984 1.1 mbalmer
1985 1.1 mbalmer
1986 1.1 mbalmer <p>
1987 1.3 lneto The conversion from numbers to strings uses a
1988 1.3 lneto non-specified human-readable format.
1989 1.2 lneto For complete control over how numbers are converted to strings,
1990 1.2 lneto use the <code>format</code> function from the string library
1991 1.2 lneto (see <a href="#pdf-string.format"><code>string.format</code></a>).
1992 1.2 lneto
1993 1.2 lneto
1994 1.2 lneto
1995 1.2 lneto
1996 1.2 lneto
1997 1.2 lneto <h3>3.4.4 – <a name="3.4.4">Relational Operators</a></h3><p>
1998 1.2 lneto Lua supports the following relational operators:
1999 1.3 lneto
2000 1.3 lneto <ul>
2001 1.3 lneto <li><b><code>==</code>: </b>equality</li>
2002 1.3 lneto <li><b><code>~=</code>: </b>inequality</li>
2003 1.3 lneto <li><b><code><</code>: </b>less than</li>
2004 1.3 lneto <li><b><code>></code>: </b>greater than</li>
2005 1.3 lneto <li><b><code><=</code>: </b>less or equal</li>
2006 1.3 lneto <li><b><code>>=</code>: </b>greater or equal</li>
2007 1.3 lneto </ul><p>
2008 1.2 lneto These operators always result in <b>false</b> or <b>true</b>.
2009 1.1 mbalmer
2010 1.1 mbalmer
2011 1.1 mbalmer <p>
2012 1.2 lneto Equality (<code>==</code>) first compares the type of its operands.
2013 1.2 lneto If the types are different, then the result is <b>false</b>.
2014 1.2 lneto Otherwise, the values of the operands are compared.
2015 1.2 lneto Strings are compared in the obvious way.
2016 1.4 mbalmer Numbers are equal if they denote the same mathematical value.
2017 1.1 mbalmer
2018 1.1 mbalmer
2019 1.1 mbalmer <p>
2020 1.2 lneto Tables, userdata, and threads
2021 1.2 lneto are compared by reference:
2022 1.2 lneto two objects are considered equal only if they are the same object.
2023 1.2 lneto Every time you create a new object
2024 1.2 lneto (a table, userdata, or thread),
2025 1.2 lneto this new object is different from any previously existing object.
2026 1.2 lneto Closures with the same reference are always equal.
2027 1.2 lneto Closures with any detectable difference
2028 1.2 lneto (different behavior, different definition) are always different.
2029 1.1 mbalmer
2030 1.1 mbalmer
2031 1.1 mbalmer <p>
2032 1.2 lneto You can change the way that Lua compares tables and userdata
2033 1.2 lneto by using the "eq" metamethod (see <a href="#2.4">§2.4</a>).
2034 1.1 mbalmer
2035 1.1 mbalmer
2036 1.2 lneto <p>
2037 1.3 lneto Equality comparisons do not convert strings to numbers
2038 1.2 lneto or vice versa.
2039 1.2 lneto Thus, <code>"0"==0</code> evaluates to <b>false</b>,
2040 1.2 lneto and <code>t[0]</code> and <code>t["0"]</code> denote different
2041 1.2 lneto entries in a table.
2042 1.1 mbalmer
2043 1.1 mbalmer
2044 1.1 mbalmer <p>
2045 1.2 lneto The operator <code>~=</code> is exactly the negation of equality (<code>==</code>).
2046 1.1 mbalmer
2047 1.1 mbalmer
2048 1.2 lneto <p>
2049 1.2 lneto The order operators work as follows.
2050 1.2 lneto If both arguments are numbers,
2051 1.4 mbalmer then they are compared according to their mathematical values
2052 1.4 mbalmer (regardless of their subtypes).
2053 1.2 lneto Otherwise, if both arguments are strings,
2054 1.2 lneto then their values are compared according to the current locale.
2055 1.2 lneto Otherwise, Lua tries to call the "lt" or the "le"
2056 1.2 lneto metamethod (see <a href="#2.4">§2.4</a>).
2057 1.2 lneto A comparison <code>a > b</code> is translated to <code>b < a</code>
2058 1.2 lneto and <code>a >= b</code> is translated to <code>b <= a</code>.
2059 1.2 lneto
2060 1.2 lneto
2061 1.4 mbalmer <p>
2062 1.4 mbalmer Following the IEEE 754 standard,
2063 1.4 mbalmer NaN is considered neither smaller than,
2064 1.4 mbalmer nor equal to, nor greater than any value (including itself).
2065 1.4 mbalmer
2066 1.4 mbalmer
2067 1.2 lneto
2068 1.2 lneto
2069 1.2 lneto
2070 1.2 lneto <h3>3.4.5 – <a name="3.4.5">Logical Operators</a></h3><p>
2071 1.2 lneto The logical operators in Lua are
2072 1.2 lneto <b>and</b>, <b>or</b>, and <b>not</b>.
2073 1.2 lneto Like the control structures (see <a href="#3.3.4">§3.3.4</a>),
2074 1.2 lneto all logical operators consider both <b>false</b> and <b>nil</b> as false
2075 1.2 lneto and anything else as true.
2076 1.2 lneto
2077 1.2 lneto
2078 1.2 lneto <p>
2079 1.2 lneto The negation operator <b>not</b> always returns <b>false</b> or <b>true</b>.
2080 1.2 lneto The conjunction operator <b>and</b> returns its first argument
2081 1.2 lneto if this value is <b>false</b> or <b>nil</b>;
2082 1.2 lneto otherwise, <b>and</b> returns its second argument.
2083 1.2 lneto The disjunction operator <b>or</b> returns its first argument
2084 1.2 lneto if this value is different from <b>nil</b> and <b>false</b>;
2085 1.2 lneto otherwise, <b>or</b> returns its second argument.
2086 1.2 lneto Both <b>and</b> and <b>or</b> use short-circuit evaluation;
2087 1.2 lneto that is,
2088 1.2 lneto the second operand is evaluated only if necessary.
2089 1.2 lneto Here are some examples:
2090 1.2 lneto
2091 1.2 lneto <pre>
2092 1.2 lneto 10 or 20 --> 10
2093 1.2 lneto 10 or error() --> 10
2094 1.2 lneto nil or "a" --> "a"
2095 1.2 lneto nil and 10 --> nil
2096 1.2 lneto false and error() --> false
2097 1.2 lneto false and nil --> false
2098 1.2 lneto false or nil --> nil
2099 1.2 lneto 10 and 20 --> 20
2100 1.2 lneto </pre><p>
2101 1.2 lneto (In this manual,
2102 1.2 lneto <code>--></code> indicates the result of the preceding expression.)
2103 1.2 lneto
2104 1.2 lneto
2105 1.2 lneto
2106 1.2 lneto
2107 1.2 lneto
2108 1.2 lneto <h3>3.4.6 – <a name="3.4.6">Concatenation</a></h3><p>
2109 1.2 lneto The string concatenation operator in Lua is
2110 1.2 lneto denoted by two dots ('<code>..</code>').
2111 1.2 lneto If both operands are strings or numbers, then they are converted to
2112 1.2 lneto strings according to the rules described in <a href="#3.4.3">§3.4.3</a>.
2113 1.2 lneto Otherwise, the <code>__concat</code> metamethod is called (see <a href="#2.4">§2.4</a>).
2114 1.2 lneto
2115 1.2 lneto
2116 1.2 lneto
2117 1.2 lneto
2118 1.2 lneto
2119 1.2 lneto <h3>3.4.7 – <a name="3.4.7">The Length Operator</a></h3>
2120 1.2 lneto
2121 1.2 lneto <p>
2122 1.2 lneto The length operator is denoted by the unary prefix operator <code>#</code>.
2123 1.2 lneto The length of a string is its number of bytes
2124 1.2 lneto (that is, the usual meaning of string length when each
2125 1.2 lneto character is one byte).
2126 1.2 lneto
2127 1.2 lneto
2128 1.2 lneto <p>
2129 1.2 lneto A program can modify the behavior of the length operator for
2130 1.2 lneto any value but strings through the <code>__len</code> metamethod (see <a href="#2.4">§2.4</a>).
2131 1.2 lneto
2132 1.2 lneto
2133 1.2 lneto <p>
2134 1.2 lneto Unless a <code>__len</code> metamethod is given,
2135 1.2 lneto the length of a table <code>t</code> is only defined if the
2136 1.2 lneto table is a <em>sequence</em>,
2137 1.2 lneto that is,
2138 1.2 lneto the set of its positive numeric keys is equal to <em>{1..n}</em>
2139 1.2 lneto for some non-negative integer <em>n</em>.
2140 1.2 lneto In that case, <em>n</em> is its length.
2141 1.2 lneto Note that a table like
2142 1.2 lneto
2143 1.2 lneto <pre>
2144 1.2 lneto {10, 20, nil, 40}
2145 1.2 lneto </pre><p>
2146 1.2 lneto is not a sequence, because it has the key <code>4</code>
2147 1.2 lneto but does not have the key <code>3</code>.
2148 1.2 lneto (So, there is no <em>n</em> such that the set <em>{1..n}</em> is equal
2149 1.2 lneto to the set of positive numeric keys of that table.)
2150 1.2 lneto Note, however, that non-numeric keys do not interfere
2151 1.2 lneto with whether a table is a sequence.
2152 1.2 lneto
2153 1.2 lneto
2154 1.2 lneto
2155 1.2 lneto
2156 1.2 lneto
2157 1.2 lneto <h3>3.4.8 – <a name="3.4.8">Precedence</a></h3><p>
2158 1.2 lneto Operator precedence in Lua follows the table below,
2159 1.2 lneto from lower to higher priority:
2160 1.2 lneto
2161 1.2 lneto <pre>
2162 1.2 lneto or
2163 1.2 lneto and
2164 1.2 lneto < > <= >= ~= ==
2165 1.2 lneto |
2166 1.2 lneto ~
2167 1.2 lneto &
2168 1.2 lneto << >>
2169 1.2 lneto ..
2170 1.2 lneto + -
2171 1.2 lneto * / // %
2172 1.2 lneto unary operators (not # - ~)
2173 1.2 lneto ^
2174 1.2 lneto </pre><p>
2175 1.2 lneto As usual,
2176 1.2 lneto you can use parentheses to change the precedences of an expression.
2177 1.2 lneto The concatenation ('<code>..</code>') and exponentiation ('<code>^</code>')
2178 1.2 lneto operators are right associative.
2179 1.2 lneto All other binary operators are left associative.
2180 1.2 lneto
2181 1.2 lneto
2182 1.2 lneto
2183 1.2 lneto
2184 1.2 lneto
2185 1.2 lneto <h3>3.4.9 – <a name="3.4.9">Table Constructors</a></h3><p>
2186 1.2 lneto Table constructors are expressions that create tables.
2187 1.2 lneto Every time a constructor is evaluated, a new table is created.
2188 1.2 lneto A constructor can be used to create an empty table
2189 1.2 lneto or to create a table and initialize some of its fields.
2190 1.2 lneto The general syntax for constructors is
2191 1.2 lneto
2192 1.2 lneto <pre>
2193 1.2 lneto tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’
2194 1.2 lneto fieldlist ::= field {fieldsep field} [fieldsep]
2195 1.2 lneto field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp
2196 1.2 lneto fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’
2197 1.2 lneto </pre>
2198 1.2 lneto
2199 1.2 lneto <p>
2200 1.2 lneto Each field of the form <code>[exp1] = exp2</code> adds to the new table an entry
2201 1.2 lneto with key <code>exp1</code> and value <code>exp2</code>.
2202 1.2 lneto A field of the form <code>name = exp</code> is equivalent to
2203 1.2 lneto <code>["name"] = exp</code>.
2204 1.2 lneto Finally, fields of the form <code>exp</code> are equivalent to
2205 1.3 lneto <code>[i] = exp</code>, where <code>i</code> are consecutive integers
2206 1.2 lneto starting with 1.
2207 1.2 lneto Fields in the other formats do not affect this counting.
2208 1.2 lneto For example,
2209 1.2 lneto
2210 1.2 lneto <pre>
2211 1.2 lneto a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
2212 1.2 lneto </pre><p>
2213 1.2 lneto is equivalent to
2214 1.2 lneto
2215 1.2 lneto <pre>
2216 1.2 lneto do
2217 1.2 lneto local t = {}
2218 1.2 lneto t[f(1)] = g
2219 1.2 lneto t[1] = "x" -- 1st exp
2220 1.2 lneto t[2] = "y" -- 2nd exp
2221 1.2 lneto t.x = 1 -- t["x"] = 1
2222 1.2 lneto t[3] = f(x) -- 3rd exp
2223 1.2 lneto t[30] = 23
2224 1.2 lneto t[4] = 45 -- 4th exp
2225 1.2 lneto a = t
2226 1.2 lneto end
2227 1.2 lneto </pre>
2228 1.2 lneto
2229 1.2 lneto <p>
2230 1.3 lneto The order of the assignments in a constructor is undefined.
2231 1.3 lneto (This order would be relevant only when there are repeated keys.)
2232 1.3 lneto
2233 1.3 lneto
2234 1.3 lneto <p>
2235 1.2 lneto If the last field in the list has the form <code>exp</code>
2236 1.2 lneto and the expression is a function call or a vararg expression,
2237 1.2 lneto then all values returned by this expression enter the list consecutively
2238 1.2 lneto (see <a href="#3.4.10">§3.4.10</a>).
2239 1.2 lneto
2240 1.2 lneto
2241 1.2 lneto <p>
2242 1.2 lneto The field list can have an optional trailing separator,
2243 1.2 lneto as a convenience for machine-generated code.
2244 1.2 lneto
2245 1.2 lneto
2246 1.2 lneto
2247 1.2 lneto
2248 1.2 lneto
2249 1.2 lneto <h3>3.4.10 – <a name="3.4.10">Function Calls</a></h3><p>
2250 1.2 lneto A function call in Lua has the following syntax:
2251 1.2 lneto
2252 1.2 lneto <pre>
2253 1.2 lneto functioncall ::= prefixexp args
2254 1.2 lneto </pre><p>
2255 1.2 lneto In a function call,
2256 1.2 lneto first prefixexp and args are evaluated.
2257 1.2 lneto If the value of prefixexp has type <em>function</em>,
2258 1.2 lneto then this function is called
2259 1.2 lneto with the given arguments.
2260 1.2 lneto Otherwise, the prefixexp "call" metamethod is called,
2261 1.2 lneto having as first parameter the value of prefixexp,
2262 1.2 lneto followed by the original call arguments
2263 1.2 lneto (see <a href="#2.4">§2.4</a>).
2264 1.2 lneto
2265 1.2 lneto
2266 1.2 lneto <p>
2267 1.2 lneto The form
2268 1.2 lneto
2269 1.2 lneto <pre>
2270 1.2 lneto functioncall ::= prefixexp ‘<b>:</b>’ Name args
2271 1.2 lneto </pre><p>
2272 1.2 lneto can be used to call "methods".
2273 1.2 lneto A call <code>v:name(<em>args</em>)</code>
2274 1.2 lneto is syntactic sugar for <code>v.name(v,<em>args</em>)</code>,
2275 1.2 lneto except that <code>v</code> is evaluated only once.
2276 1.2 lneto
2277 1.2 lneto
2278 1.2 lneto <p>
2279 1.2 lneto Arguments have the following syntax:
2280 1.2 lneto
2281 1.2 lneto <pre>
2282 1.2 lneto args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’
2283 1.2 lneto args ::= tableconstructor
2284 1.3 lneto args ::= LiteralString
2285 1.2 lneto </pre><p>
2286 1.2 lneto All argument expressions are evaluated before the call.
2287 1.2 lneto A call of the form <code>f{<em>fields</em>}</code> is
2288 1.2 lneto syntactic sugar for <code>f({<em>fields</em>})</code>;
2289 1.2 lneto that is, the argument list is a single new table.
2290 1.2 lneto A call of the form <code>f'<em>string</em>'</code>
2291 1.2 lneto (or <code>f"<em>string</em>"</code> or <code>f[[<em>string</em>]]</code>)
2292 1.2 lneto is syntactic sugar for <code>f('<em>string</em>')</code>;
2293 1.2 lneto that is, the argument list is a single literal string.
2294 1.2 lneto
2295 1.2 lneto
2296 1.2 lneto <p>
2297 1.2 lneto A call of the form <code>return <em>functioncall</em></code> is called
2298 1.2 lneto a <em>tail call</em>.
2299 1.2 lneto Lua implements <em>proper tail calls</em>
2300 1.2 lneto (or <em>proper tail recursion</em>):
2301 1.2 lneto in a tail call,
2302 1.2 lneto the called function reuses the stack entry of the calling function.
2303 1.2 lneto Therefore, there is no limit on the number of nested tail calls that
2304 1.2 lneto a program can execute.
2305 1.2 lneto However, a tail call erases any debug information about the
2306 1.2 lneto calling function.
2307 1.2 lneto Note that a tail call only happens with a particular syntax,
2308 1.2 lneto where the <b>return</b> has one single function call as argument;
2309 1.2 lneto this syntax makes the calling function return exactly
2310 1.2 lneto the returns of the called function.
2311 1.2 lneto So, none of the following examples are tail calls:
2312 1.2 lneto
2313 1.2 lneto <pre>
2314 1.2 lneto return (f(x)) -- results adjusted to 1
2315 1.2 lneto return 2 * f(x)
2316 1.2 lneto return x, f(x) -- additional results
2317 1.2 lneto f(x); return -- results discarded
2318 1.2 lneto return x or f(x) -- results adjusted to 1
2319 1.2 lneto </pre>
2320 1.2 lneto
2321 1.2 lneto
2322 1.2 lneto
2323 1.2 lneto
2324 1.2 lneto <h3>3.4.11 – <a name="3.4.11">Function Definitions</a></h3>
2325 1.2 lneto
2326 1.2 lneto <p>
2327 1.2 lneto The syntax for function definition is
2328 1.2 lneto
2329 1.2 lneto <pre>
2330 1.2 lneto functiondef ::= <b>function</b> funcbody
2331 1.2 lneto funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b>
2332 1.2 lneto </pre>
2333 1.2 lneto
2334 1.2 lneto <p>
2335 1.2 lneto The following syntactic sugar simplifies function definitions:
2336 1.2 lneto
2337 1.2 lneto <pre>
2338 1.2 lneto stat ::= <b>function</b> funcname funcbody
2339 1.2 lneto stat ::= <b>local</b> <b>function</b> Name funcbody
2340 1.2 lneto funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name]
2341 1.2 lneto </pre><p>
2342 1.2 lneto The statement
2343 1.2 lneto
2344 1.2 lneto <pre>
2345 1.2 lneto function f () <em>body</em> end
2346 1.2 lneto </pre><p>
2347 1.2 lneto translates to
2348 1.2 lneto
2349 1.2 lneto <pre>
2350 1.2 lneto f = function () <em>body</em> end
2351 1.2 lneto </pre><p>
2352 1.2 lneto The statement
2353 1.2 lneto
2354 1.2 lneto <pre>
2355 1.2 lneto function t.a.b.c.f () <em>body</em> end
2356 1.2 lneto </pre><p>
2357 1.2 lneto translates to
2358 1.2 lneto
2359 1.2 lneto <pre>
2360 1.2 lneto t.a.b.c.f = function () <em>body</em> end
2361 1.2 lneto </pre><p>
2362 1.2 lneto The statement
2363 1.2 lneto
2364 1.2 lneto <pre>
2365 1.2 lneto local function f () <em>body</em> end
2366 1.2 lneto </pre><p>
2367 1.2 lneto translates to
2368 1.2 lneto
2369 1.2 lneto <pre>
2370 1.2 lneto local f; f = function () <em>body</em> end
2371 1.2 lneto </pre><p>
2372 1.2 lneto not to
2373 1.2 lneto
2374 1.2 lneto <pre>
2375 1.2 lneto local f = function () <em>body</em> end
2376 1.2 lneto </pre><p>
2377 1.2 lneto (This only makes a difference when the body of the function
2378 1.2 lneto contains references to <code>f</code>.)
2379 1.2 lneto
2380 1.2 lneto
2381 1.2 lneto <p>
2382 1.2 lneto A function definition is an executable expression,
2383 1.2 lneto whose value has type <em>function</em>.
2384 1.2 lneto When Lua precompiles a chunk,
2385 1.2 lneto all its function bodies are precompiled too.
2386 1.2 lneto Then, whenever Lua executes the function definition,
2387 1.2 lneto the function is <em>instantiated</em> (or <em>closed</em>).
2388 1.2 lneto This function instance (or <em>closure</em>)
2389 1.2 lneto is the final value of the expression.
2390 1.2 lneto
2391 1.2 lneto
2392 1.2 lneto <p>
2393 1.2 lneto Parameters act as local variables that are
2394 1.2 lneto initialized with the argument values:
2395 1.2 lneto
2396 1.2 lneto <pre>
2397 1.2 lneto parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’
2398 1.2 lneto </pre><p>
2399 1.2 lneto When a function is called,
2400 1.2 lneto the list of arguments is adjusted to
2401 1.2 lneto the length of the list of parameters,
2402 1.2 lneto unless the function is a <em>vararg function</em>,
2403 1.2 lneto which is indicated by three dots ('<code>...</code>')
2404 1.2 lneto at the end of its parameter list.
2405 1.2 lneto A vararg function does not adjust its argument list;
2406 1.2 lneto instead, it collects all extra arguments and supplies them
2407 1.2 lneto to the function through a <em>vararg expression</em>,
2408 1.2 lneto which is also written as three dots.
2409 1.2 lneto The value of this expression is a list of all actual extra arguments,
2410 1.2 lneto similar to a function with multiple results.
2411 1.2 lneto If a vararg expression is used inside another expression
2412 1.2 lneto or in the middle of a list of expressions,
2413 1.2 lneto then its return list is adjusted to one element.
2414 1.2 lneto If the expression is used as the last element of a list of expressions,
2415 1.2 lneto then no adjustment is made
2416 1.2 lneto (unless that last expression is enclosed in parentheses).
2417 1.2 lneto
2418 1.2 lneto
2419 1.2 lneto <p>
2420 1.2 lneto As an example, consider the following definitions:
2421 1.2 lneto
2422 1.2 lneto <pre>
2423 1.2 lneto function f(a, b) end
2424 1.2 lneto function g(a, b, ...) end
2425 1.2 lneto function r() return 1,2,3 end
2426 1.2 lneto </pre><p>
2427 1.2 lneto Then, we have the following mapping from arguments to parameters and
2428 1.2 lneto to the vararg expression:
2429 1.1 mbalmer
2430 1.1 mbalmer <pre>
2431 1.2 lneto CALL PARAMETERS
2432 1.2 lneto
2433 1.2 lneto f(3) a=3, b=nil
2434 1.2 lneto f(3, 4) a=3, b=4
2435 1.2 lneto f(3, 4, 5) a=3, b=4
2436 1.2 lneto f(r(), 10) a=1, b=10
2437 1.2 lneto f(r()) a=1, b=2
2438 1.2 lneto
2439 1.2 lneto g(3) a=3, b=nil, ... --> (nothing)
2440 1.2 lneto g(3, 4) a=3, b=4, ... --> (nothing)
2441 1.2 lneto g(3, 4, 5, 8) a=3, b=4, ... --> 5 8
2442 1.2 lneto g(5, r()) a=5, b=1, ... --> 2 3
2443 1.1 mbalmer </pre>
2444 1.1 mbalmer
2445 1.1 mbalmer <p>
2446 1.2 lneto Results are returned using the <b>return</b> statement (see <a href="#3.3.4">§3.3.4</a>).
2447 1.2 lneto If control reaches the end of a function
2448 1.2 lneto without encountering a <b>return</b> statement,
2449 1.2 lneto then the function returns with no results.
2450 1.1 mbalmer
2451 1.1 mbalmer
2452 1.1 mbalmer <p>
2453 1.1 mbalmer
2454 1.2 lneto There is a system-dependent limit on the number of values
2455 1.2 lneto that a function may return.
2456 1.2 lneto This limit is guaranteed to be larger than 1000.
2457 1.1 mbalmer
2458 1.1 mbalmer
2459 1.1 mbalmer <p>
2460 1.2 lneto The <em>colon</em> syntax
2461 1.2 lneto is used for defining <em>methods</em>,
2462 1.2 lneto that is, functions that have an implicit extra parameter <code>self</code>.
2463 1.2 lneto Thus, the statement
2464 1.1 mbalmer
2465 1.2 lneto <pre>
2466 1.2 lneto function t.a.b.c:f (<em>params</em>) <em>body</em> end
2467 1.2 lneto </pre><p>
2468 1.2 lneto is syntactic sugar for
2469 1.1 mbalmer
2470 1.2 lneto <pre>
2471 1.2 lneto t.a.b.c.f = function (self, <em>params</em>) <em>body</em> end
2472 1.2 lneto </pre>
2473 1.1 mbalmer
2474 1.1 mbalmer
2475 1.1 mbalmer
2476 1.1 mbalmer
2477 1.1 mbalmer
2478 1.1 mbalmer
2479 1.2 lneto <h2>3.5 – <a name="3.5">Visibility Rules</a></h2>
2480 1.1 mbalmer
2481 1.1 mbalmer <p>
2482 1.1 mbalmer
2483 1.2 lneto Lua is a lexically scoped language.
2484 1.2 lneto The scope of a local variable begins at the first statement after
2485 1.2 lneto its declaration and lasts until the last non-void statement
2486 1.2 lneto of the innermost block that includes the declaration.
2487 1.2 lneto Consider the following example:
2488 1.1 mbalmer
2489 1.2 lneto <pre>
2490 1.2 lneto x = 10 -- global variable
2491 1.2 lneto do -- new block
2492 1.2 lneto local x = x -- new 'x', with value 10
2493 1.2 lneto print(x) --> 10
2494 1.2 lneto x = x+1
2495 1.2 lneto do -- another block
2496 1.2 lneto local x = x+1 -- another 'x'
2497 1.2 lneto print(x) --> 12
2498 1.2 lneto end
2499 1.2 lneto print(x) --> 11
2500 1.2 lneto end
2501 1.2 lneto print(x) --> 10 (the global one)
2502 1.2 lneto </pre>
2503 1.1 mbalmer
2504 1.1 mbalmer <p>
2505 1.2 lneto Notice that, in a declaration like <code>local x = x</code>,
2506 1.2 lneto the new <code>x</code> being declared is not in scope yet,
2507 1.2 lneto and so the second <code>x</code> refers to the outside variable.
2508 1.1 mbalmer
2509 1.1 mbalmer
2510 1.1 mbalmer <p>
2511 1.2 lneto Because of the lexical scoping rules,
2512 1.2 lneto local variables can be freely accessed by functions
2513 1.2 lneto defined inside their scope.
2514 1.2 lneto A local variable used by an inner function is called
2515 1.2 lneto an <em>upvalue</em>, or <em>external local variable</em>,
2516 1.2 lneto inside the inner function.
2517 1.1 mbalmer
2518 1.1 mbalmer
2519 1.1 mbalmer <p>
2520 1.2 lneto Notice that each execution of a <b>local</b> statement
2521 1.2 lneto defines new local variables.
2522 1.2 lneto Consider the following example:
2523 1.1 mbalmer
2524 1.1 mbalmer <pre>
2525 1.2 lneto a = {}
2526 1.2 lneto local x = 20
2527 1.2 lneto for i=1,10 do
2528 1.2 lneto local y = 0
2529 1.2 lneto a[i] = function () y=y+1; return x+y end
2530 1.1 mbalmer end
2531 1.1 mbalmer </pre><p>
2532 1.2 lneto The loop creates ten closures
2533 1.2 lneto (that is, ten instances of the anonymous function).
2534 1.2 lneto Each of these closures uses a different <code>y</code> variable,
2535 1.2 lneto while all of them share the same <code>x</code>.
2536 1.1 mbalmer
2537 1.1 mbalmer
2538 1.1 mbalmer
2539 1.1 mbalmer
2540 1.1 mbalmer
2541 1.2 lneto <h1>4 – <a name="4">The Application Program Interface</a></h1>
2542 1.1 mbalmer
2543 1.1 mbalmer <p>
2544 1.1 mbalmer
2545 1.1 mbalmer This section describes the C API for Lua, that is,
2546 1.1 mbalmer the set of C functions available to the host program to communicate
2547 1.1 mbalmer with Lua.
2548 1.1 mbalmer All API functions and related types and constants
2549 1.1 mbalmer are declared in the header file <a name="pdf-lua.h"><code>lua.h</code></a>.
2550 1.1 mbalmer
2551 1.1 mbalmer
2552 1.1 mbalmer <p>
2553 1.1 mbalmer Even when we use the term "function",
2554 1.1 mbalmer any facility in the API may be provided as a macro instead.
2555 1.2 lneto Except where stated otherwise,
2556 1.2 lneto all such macros use each of their arguments exactly once
2557 1.1 mbalmer (except for the first argument, which is always a Lua state),
2558 1.1 mbalmer and so do not generate any hidden side-effects.
2559 1.1 mbalmer
2560 1.1 mbalmer
2561 1.1 mbalmer <p>
2562 1.1 mbalmer As in most C libraries,
2563 1.1 mbalmer the Lua API functions do not check their arguments for validity or consistency.
2564 1.1 mbalmer However, you can change this behavior by compiling Lua
2565 1.2 lneto with the macro <a name="pdf-LUA_USE_APICHECK"><code>LUA_USE_APICHECK</code></a> defined.
2566 1.1 mbalmer
2567 1.1 mbalmer
2568 1.1 mbalmer
2569 1.2 lneto <h2>4.1 – <a name="4.1">The Stack</a></h2>
2570 1.1 mbalmer
2571 1.1 mbalmer <p>
2572 1.1 mbalmer Lua uses a <em>virtual stack</em> to pass values to and from C.
2573 1.1 mbalmer Each element in this stack represents a Lua value
2574 1.1 mbalmer (<b>nil</b>, number, string, etc.).
2575 1.1 mbalmer
2576 1.1 mbalmer
2577 1.1 mbalmer <p>
2578 1.1 mbalmer Whenever Lua calls C, the called function gets a new stack,
2579 1.1 mbalmer which is independent of previous stacks and of stacks of
2580 1.1 mbalmer C functions that are still active.
2581 1.1 mbalmer This stack initially contains any arguments to the C function
2582 1.1 mbalmer and it is where the C function pushes its results
2583 1.1 mbalmer to be returned to the caller (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
2584 1.1 mbalmer
2585 1.1 mbalmer
2586 1.1 mbalmer <p>
2587 1.1 mbalmer For convenience,
2588 1.1 mbalmer most query operations in the API do not follow a strict stack discipline.
2589 1.1 mbalmer Instead, they can refer to any element in the stack
2590 1.1 mbalmer by using an <em>index</em>:
2591 1.2 lneto A positive index represents an absolute stack position
2592 1.1 mbalmer (starting at 1);
2593 1.2 lneto a negative index represents an offset relative to the top of the stack.
2594 1.1 mbalmer More specifically, if the stack has <em>n</em> elements,
2595 1.1 mbalmer then index 1 represents the first element
2596 1.1 mbalmer (that is, the element that was pushed onto the stack first)
2597 1.1 mbalmer and
2598 1.1 mbalmer index <em>n</em> represents the last element;
2599 1.1 mbalmer index -1 also represents the last element
2600 1.1 mbalmer (that is, the element at the top)
2601 1.1 mbalmer and index <em>-n</em> represents the first element.
2602 1.1 mbalmer
2603 1.1 mbalmer
2604 1.1 mbalmer
2605 1.1 mbalmer
2606 1.1 mbalmer
2607 1.2 lneto <h2>4.2 – <a name="4.2">Stack Size</a></h2>
2608 1.1 mbalmer
2609 1.1 mbalmer <p>
2610 1.2 lneto When you interact with the Lua API,
2611 1.1 mbalmer you are responsible for ensuring consistency.
2612 1.1 mbalmer In particular,
2613 1.1 mbalmer <em>you are responsible for controlling stack overflow</em>.
2614 1.1 mbalmer You can use the function <a href="#lua_checkstack"><code>lua_checkstack</code></a>
2615 1.3 lneto to ensure that the stack has enough space for pushing new elements.
2616 1.1 mbalmer
2617 1.1 mbalmer
2618 1.1 mbalmer <p>
2619 1.1 mbalmer Whenever Lua calls C,
2620 1.3 lneto it ensures that the stack has space for
2621 1.3 lneto at least <a name="pdf-LUA_MINSTACK"><code>LUA_MINSTACK</code></a> extra slots.
2622 1.1 mbalmer <code>LUA_MINSTACK</code> is defined as 20,
2623 1.1 mbalmer so that usually you do not have to worry about stack space
2624 1.1 mbalmer unless your code has loops pushing elements onto the stack.
2625 1.1 mbalmer
2626 1.1 mbalmer
2627 1.1 mbalmer <p>
2628 1.2 lneto When you call a Lua function
2629 1.2 lneto without a fixed number of results (see <a href="#lua_call"><code>lua_call</code></a>),
2630 1.3 lneto Lua ensures that the stack has enough space for all results,
2631 1.2 lneto but it does not ensure any extra space.
2632 1.2 lneto So, before pushing anything in the stack after such a call
2633 1.2 lneto you should use <a href="#lua_checkstack"><code>lua_checkstack</code></a>.
2634 1.2 lneto
2635 1.2 lneto
2636 1.2 lneto
2637 1.2 lneto
2638 1.1 mbalmer
2639 1.2 lneto <h2>4.3 – <a name="4.3">Valid and Acceptable Indices</a></h2>
2640 1.1 mbalmer
2641 1.2 lneto <p>
2642 1.2 lneto Any function in the API that receives stack indices
2643 1.2 lneto works only with <em>valid indices</em> or <em>acceptable indices</em>.
2644 1.1 mbalmer
2645 1.1 mbalmer
2646 1.2 lneto <p>
2647 1.2 lneto A <em>valid index</em> is an index that refers to a
2648 1.4 mbalmer position that stores a modifiable Lua value.
2649 1.4 mbalmer It comprises stack indices between 1 and the stack top
2650 1.4 mbalmer (<code>1 ≤ abs(index) ≤ top</code>)
2651 1.4 mbalmer
2652 1.4 mbalmer plus <em>pseudo-indices</em>,
2653 1.4 mbalmer which represent some positions that are accessible to C code
2654 1.4 mbalmer but that are not in the stack.
2655 1.4 mbalmer Pseudo-indices are used to access the registry (see <a href="#4.5">§4.5</a>)
2656 1.2 lneto and the upvalues of a C function (see <a href="#4.4">§4.4</a>).
2657 1.2 lneto
2658 1.2 lneto
2659 1.2 lneto <p>
2660 1.4 mbalmer Functions that do not need a specific mutable position,
2661 1.4 mbalmer but only a value (e.g., query functions),
2662 1.2 lneto can be called with acceptable indices.
2663 1.2 lneto An <em>acceptable index</em> can be any valid index,
2664 1.2 lneto but it also can be any positive index after the stack top
2665 1.2 lneto within the space allocated for the stack,
2666 1.2 lneto that is, indices up to the stack size.
2667 1.2 lneto (Note that 0 is never an acceptable index.)
2668 1.2 lneto Except when noted otherwise,
2669 1.2 lneto functions in the API work with acceptable indices.
2670 1.1 mbalmer
2671 1.1 mbalmer
2672 1.1 mbalmer <p>
2673 1.2 lneto Acceptable indices serve to avoid extra tests
2674 1.2 lneto against the stack top when querying the stack.
2675 1.2 lneto For instance, a C function can query its third argument
2676 1.2 lneto without the need to first check whether there is a third argument,
2677 1.2 lneto that is, without the need to check whether 3 is a valid index.
2678 1.1 mbalmer
2679 1.1 mbalmer
2680 1.1 mbalmer <p>
2681 1.2 lneto For functions that can be called with acceptable indices,
2682 1.2 lneto any non-valid index is treated as if it
2683 1.2 lneto contains a value of a virtual type <a name="pdf-LUA_TNONE"><code>LUA_TNONE</code></a>,
2684 1.2 lneto which behaves like a nil value.
2685 1.1 mbalmer
2686 1.1 mbalmer
2687 1.1 mbalmer
2688 1.1 mbalmer
2689 1.1 mbalmer
2690 1.2 lneto <h2>4.4 – <a name="4.4">C Closures</a></h2>
2691 1.1 mbalmer
2692 1.1 mbalmer <p>
2693 1.1 mbalmer When a C function is created,
2694 1.1 mbalmer it is possible to associate some values with it,
2695 1.2 lneto thus creating a <em>C closure</em>
2696 1.2 lneto (see <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>);
2697 1.1 mbalmer these values are called <em>upvalues</em> and are
2698 1.2 lneto accessible to the function whenever it is called.
2699 1.1 mbalmer
2700 1.1 mbalmer
2701 1.1 mbalmer <p>
2702 1.1 mbalmer Whenever a C function is called,
2703 1.1 mbalmer its upvalues are located at specific pseudo-indices.
2704 1.1 mbalmer These pseudo-indices are produced by the macro
2705 1.2 lneto <a href="#lua_upvalueindex"><code>lua_upvalueindex</code></a>.
2706 1.4 mbalmer The first upvalue associated with a function is at index
2707 1.1 mbalmer <code>lua_upvalueindex(1)</code>, and so on.
2708 1.1 mbalmer Any access to <code>lua_upvalueindex(<em>n</em>)</code>,
2709 1.1 mbalmer where <em>n</em> is greater than the number of upvalues of the
2710 1.5 lneto current function
2711 1.5 lneto (but not greater than 256,
2712 1.5 lneto which is one plus the maximum number of upvalues in a closure),
2713 1.2 lneto produces an acceptable but invalid index.
2714 1.1 mbalmer
2715 1.1 mbalmer
2716 1.1 mbalmer
2717 1.1 mbalmer
2718 1.1 mbalmer
2719 1.2 lneto <h2>4.5 – <a name="4.5">Registry</a></h2>
2720 1.1 mbalmer
2721 1.1 mbalmer <p>
2722 1.1 mbalmer Lua provides a <em>registry</em>,
2723 1.2 lneto a predefined table that can be used by any C code to
2724 1.2 lneto store whatever Lua values it needs to store.
2725 1.2 lneto The registry table is always located at pseudo-index
2726 1.4 mbalmer <a name="pdf-LUA_REGISTRYINDEX"><code>LUA_REGISTRYINDEX</code></a>.
2727 1.1 mbalmer Any C library can store data into this table,
2728 1.2 lneto but it must take care to choose keys
2729 1.2 lneto that are different from those used
2730 1.1 mbalmer by other libraries, to avoid collisions.
2731 1.2 lneto Typically, you should use as key a string containing your library name,
2732 1.2 lneto or a light userdata with the address of a C object in your code,
2733 1.2 lneto or any Lua object created by your code.
2734 1.3 lneto As with variable names,
2735 1.2 lneto string keys starting with an underscore followed by
2736 1.2 lneto uppercase letters are reserved for Lua.
2737 1.1 mbalmer
2738 1.1 mbalmer
2739 1.1 mbalmer <p>
2740 1.3 lneto The integer keys in the registry are used
2741 1.3 lneto by the reference mechanism (see <a href="#luaL_ref"><code>luaL_ref</code></a>)
2742 1.2 lneto and by some predefined values.
2743 1.2 lneto Therefore, integer keys must not be used for other purposes.
2744 1.2 lneto
2745 1.1 mbalmer
2746 1.2 lneto <p>
2747 1.2 lneto When you create a new Lua state,
2748 1.2 lneto its registry comes with some predefined values.
2749 1.2 lneto These predefined values are indexed with integer keys
2750 1.2 lneto defined as constants in <code>lua.h</code>.
2751 1.2 lneto The following constants are defined:
2752 1.2 lneto
2753 1.2 lneto <ul>
2754 1.2 lneto <li><b><a name="pdf-LUA_RIDX_MAINTHREAD"><code>LUA_RIDX_MAINTHREAD</code></a>: </b> At this index the registry has
2755 1.2 lneto the main thread of the state.
2756 1.2 lneto (The main thread is the one created together with the state.)
2757 1.2 lneto </li>
2758 1.1 mbalmer
2759 1.2 lneto <li><b><a name="pdf-LUA_RIDX_GLOBALS"><code>LUA_RIDX_GLOBALS</code></a>: </b> At this index the registry has
2760 1.2 lneto the global environment.
2761 1.2 lneto </li>
2762 1.2 lneto </ul>
2763 1.1 mbalmer
2764 1.1 mbalmer
2765 1.1 mbalmer
2766 1.2 lneto
2767 1.2 lneto <h2>4.6 – <a name="4.6">Error Handling in C</a></h2>
2768 1.1 mbalmer
2769 1.1 mbalmer <p>
2770 1.1 mbalmer Internally, Lua uses the C <code>longjmp</code> facility to handle errors.
2771 1.3 lneto (Lua will use exceptions if you compile it as C++;
2772 1.3 lneto search for <code>LUAI_THROW</code> in the source code for details.)
2773 1.1 mbalmer When Lua faces any error
2774 1.2 lneto (such as a memory allocation error, type errors, syntax errors,
2775 1.1 mbalmer and runtime errors)
2776 1.1 mbalmer it <em>raises</em> an error;
2777 1.1 mbalmer that is, it does a long jump.
2778 1.1 mbalmer A <em>protected environment</em> uses <code>setjmp</code>
2779 1.2 lneto to set a recovery point;
2780 1.2 lneto any error jumps to the most recent active recovery point.
2781 1.2 lneto
2782 1.2 lneto
2783 1.2 lneto <p>
2784 1.2 lneto If an error happens outside any protected environment,
2785 1.2 lneto Lua calls a <em>panic function</em> (see <a href="#lua_atpanic"><code>lua_atpanic</code></a>)
2786 1.2 lneto and then calls <code>abort</code>,
2787 1.2 lneto thus exiting the host application.
2788 1.2 lneto Your panic function can avoid this exit by
2789 1.2 lneto never returning
2790 1.2 lneto (e.g., doing a long jump to your own recovery point outside Lua).
2791 1.2 lneto
2792 1.2 lneto
2793 1.2 lneto <p>
2794 1.2 lneto The panic function runs as if it were a message handler (see <a href="#2.3">§2.3</a>);
2795 1.2 lneto in particular, the error message is at the top of the stack.
2796 1.3 lneto However, there is no guarantee about stack space.
2797 1.2 lneto To push anything on the stack,
2798 1.2 lneto the panic function must first check the available space (see <a href="#4.2">§4.2</a>).
2799 1.1 mbalmer
2800 1.1 mbalmer
2801 1.1 mbalmer <p>
2802 1.2 lneto Most functions in the API can raise an error,
2803 1.1 mbalmer for instance due to a memory allocation error.
2804 1.2 lneto The documentation for each function indicates whether
2805 1.2 lneto it can raise errors.
2806 1.2 lneto
2807 1.2 lneto
2808 1.2 lneto <p>
2809 1.2 lneto Inside a C function you can raise an error by calling <a href="#lua_error"><code>lua_error</code></a>.
2810 1.2 lneto
2811 1.2 lneto
2812 1.2 lneto
2813 1.2 lneto
2814 1.2 lneto
2815 1.2 lneto <h2>4.7 – <a name="4.7">Handling Yields in C</a></h2>
2816 1.2 lneto
2817 1.2 lneto <p>
2818 1.2 lneto Internally, Lua uses the C <code>longjmp</code> facility to yield a coroutine.
2819 1.2 lneto Therefore, if a C function <code>foo</code> calls an API function
2820 1.2 lneto and this API function yields
2821 1.2 lneto (directly or indirectly by calling another function that yields),
2822 1.2 lneto Lua cannot return to <code>foo</code> any more,
2823 1.2 lneto because the <code>longjmp</code> removes its frame from the C stack.
2824 1.2 lneto
2825 1.2 lneto
2826 1.2 lneto <p>
2827 1.2 lneto To avoid this kind of problem,
2828 1.2 lneto Lua raises an error whenever it tries to yield across an API call,
2829 1.2 lneto except for three functions:
2830 1.2 lneto <a href="#lua_yieldk"><code>lua_yieldk</code></a>, <a href="#lua_callk"><code>lua_callk</code></a>, and <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2831 1.2 lneto All those functions receive a <em>continuation function</em>
2832 1.3 lneto (as a parameter named <code>k</code>) to continue execution after a yield.
2833 1.2 lneto
2834 1.2 lneto
2835 1.2 lneto <p>
2836 1.2 lneto We need to set some terminology to explain continuations.
2837 1.2 lneto We have a C function called from Lua which we will call
2838 1.2 lneto the <em>original function</em>.
2839 1.2 lneto This original function then calls one of those three functions in the C API,
2840 1.2 lneto which we will call the <em>callee function</em>,
2841 1.2 lneto that then yields the current thread.
2842 1.2 lneto (This can happen when the callee function is <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
2843 1.2 lneto or when the callee function is either <a href="#lua_callk"><code>lua_callk</code></a> or <a href="#lua_pcallk"><code>lua_pcallk</code></a>
2844 1.2 lneto and the function called by them yields.)
2845 1.2 lneto
2846 1.2 lneto
2847 1.2 lneto <p>
2848 1.2 lneto Suppose the running thread yields while executing the callee function.
2849 1.2 lneto After the thread resumes,
2850 1.2 lneto it eventually will finish running the callee function.
2851 1.2 lneto However,
2852 1.2 lneto the callee function cannot return to the original function,
2853 1.2 lneto because its frame in the C stack was destroyed by the yield.
2854 1.2 lneto Instead, Lua calls a <em>continuation function</em>,
2855 1.2 lneto which was given as an argument to the callee function.
2856 1.2 lneto As the name implies,
2857 1.2 lneto the continuation function should continue the task
2858 1.2 lneto of the original function.
2859 1.2 lneto
2860 1.2 lneto
2861 1.2 lneto <p>
2862 1.2 lneto As an illustration, consider the following function:
2863 1.2 lneto
2864 1.2 lneto <pre>
2865 1.2 lneto int original_function (lua_State *L) {
2866 1.2 lneto ... /* code 1 */
2867 1.2 lneto status = lua_pcall(L, n, m, h); /* calls Lua */
2868 1.2 lneto ... /* code 2 */
2869 1.2 lneto }
2870 1.2 lneto </pre><p>
2871 1.2 lneto Now we want to allow
2872 1.3 lneto the Lua code being run by <a href="#lua_pcall"><code>lua_pcall</code></a> to yield.
2873 1.2 lneto First, we can rewrite our function like here:
2874 1.2 lneto
2875 1.2 lneto <pre>
2876 1.3 lneto int k (lua_State *L, int status, lua_KContext ctx) {
2877 1.2 lneto ... /* code 2 */
2878 1.2 lneto }
2879 1.2 lneto
2880 1.2 lneto int original_function (lua_State *L) {
2881 1.2 lneto ... /* code 1 */
2882 1.2 lneto return k(L, lua_pcall(L, n, m, h), ctx);
2883 1.2 lneto }
2884 1.2 lneto </pre><p>
2885 1.2 lneto In the above code,
2886 1.2 lneto the new function <code>k</code> is a
2887 1.2 lneto <em>continuation function</em> (with type <a href="#lua_KFunction"><code>lua_KFunction</code></a>),
2888 1.2 lneto which should do all the work that the original function
2889 1.2 lneto was doing after calling <a href="#lua_pcall"><code>lua_pcall</code></a>.
2890 1.2 lneto Now, we must inform Lua that it must call <code>k</code> if the Lua code
2891 1.3 lneto being executed by <a href="#lua_pcall"><code>lua_pcall</code></a> gets interrupted in some way
2892 1.2 lneto (errors or yielding),
2893 1.2 lneto so we rewrite the code as here,
2894 1.2 lneto replacing <a href="#lua_pcall"><code>lua_pcall</code></a> by <a href="#lua_pcallk"><code>lua_pcallk</code></a>:
2895 1.2 lneto
2896 1.2 lneto <pre>
2897 1.2 lneto int original_function (lua_State *L) {
2898 1.2 lneto ... /* code 1 */
2899 1.2 lneto return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
2900 1.2 lneto }
2901 1.3 lneto </pre><p>
2902 1.3 lneto Note the external, explicit call to the continuation:
2903 1.3 lneto Lua will call the continuation only if needed, that is,
2904 1.3 lneto in case of errors or resuming after a yield.
2905 1.3 lneto If the called function returns normally without ever yielding,
2906 1.3 lneto <a href="#lua_pcallk"><code>lua_pcallk</code></a> (and <a href="#lua_callk"><code>lua_callk</code></a>) will also return normally.
2907 1.3 lneto (Of course, instead of calling the continuation in that case,
2908 1.3 lneto you can do the equivalent work directly inside the original function.)
2909 1.3 lneto
2910 1.2 lneto
2911 1.2 lneto <p>
2912 1.2 lneto Besides the Lua state,
2913 1.2 lneto the continuation function has two other parameters:
2914 1.2 lneto the final status of the call plus the context value (<code>ctx</code>) that
2915 1.2 lneto was passed originally to <a href="#lua_pcallk"><code>lua_pcallk</code></a>.
2916 1.2 lneto (Lua does not use this context value;
2917 1.2 lneto it only passes this value from the original function to the
2918 1.2 lneto continuation function.)
2919 1.2 lneto For <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2920 1.2 lneto the status is the same value that would be returned by <a href="#lua_pcallk"><code>lua_pcallk</code></a>,
2921 1.3 lneto except that it is <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when being executed after a yield
2922 1.2 lneto (instead of <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>).
2923 1.2 lneto For <a href="#lua_yieldk"><code>lua_yieldk</code></a> and <a href="#lua_callk"><code>lua_callk</code></a>,
2924 1.2 lneto the status is always <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> when Lua calls the continuation.
2925 1.2 lneto (For these two functions,
2926 1.2 lneto Lua will not call the continuation in case of errors,
2927 1.2 lneto because they do not handle errors.)
2928 1.3 lneto Similarly, when using <a href="#lua_callk"><code>lua_callk</code></a>,
2929 1.3 lneto you should call the continuation function
2930 1.3 lneto with <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> as the status.
2931 1.3 lneto (For <a href="#lua_yieldk"><code>lua_yieldk</code></a>, there is not much point in calling
2932 1.3 lneto directly the continuation function,
2933 1.3 lneto because <a href="#lua_yieldk"><code>lua_yieldk</code></a> usually does not return.)
2934 1.1 mbalmer
2935 1.1 mbalmer
2936 1.1 mbalmer <p>
2937 1.2 lneto Lua treats the continuation function as if it were the original function.
2938 1.2 lneto The continuation function receives the same Lua stack
2939 1.2 lneto from the original function,
2940 1.2 lneto in the same state it would be if the callee function had returned.
2941 1.2 lneto (For instance,
2942 1.2 lneto after a <a href="#lua_callk"><code>lua_callk</code></a> the function and its arguments are
2943 1.2 lneto removed from the stack and replaced by the results from the call.)
2944 1.2 lneto It also has the same upvalues.
2945 1.2 lneto Whatever it returns is handled by Lua as if it were the return
2946 1.2 lneto of the original function.
2947 1.1 mbalmer
2948 1.1 mbalmer
2949 1.1 mbalmer
2950 1.1 mbalmer
2951 1.1 mbalmer
2952 1.2 lneto <h2>4.8 – <a name="4.8">Functions and Types</a></h2>
2953 1.1 mbalmer
2954 1.1 mbalmer <p>
2955 1.1 mbalmer Here we list all functions and types from the C API in
2956 1.1 mbalmer alphabetical order.
2957 1.1 mbalmer Each function has an indicator like this:
2958 1.1 mbalmer <span class="apii">[-o, +p, <em>x</em>]</span>
2959 1.1 mbalmer
2960 1.1 mbalmer
2961 1.1 mbalmer <p>
2962 1.1 mbalmer The first field, <code>o</code>,
2963 1.1 mbalmer is how many elements the function pops from the stack.
2964 1.1 mbalmer The second field, <code>p</code>,
2965 1.1 mbalmer is how many elements the function pushes onto the stack.
2966 1.1 mbalmer (Any function always pushes its results after popping its arguments.)
2967 1.1 mbalmer A field in the form <code>x|y</code> means the function can push (or pop)
2968 1.1 mbalmer <code>x</code> or <code>y</code> elements,
2969 1.1 mbalmer depending on the situation;
2970 1.1 mbalmer an interrogation mark '<code>?</code>' means that
2971 1.1 mbalmer we cannot know how many elements the function pops/pushes
2972 1.1 mbalmer by looking only at its arguments
2973 1.1 mbalmer (e.g., they may depend on what is on the stack).
2974 1.1 mbalmer The third field, <code>x</code>,
2975 1.2 lneto tells whether the function may raise errors:
2976 1.2 lneto '<code>-</code>' means the function never raises any error;
2977 1.5 lneto '<code>m</code>' means the function may raise memory errors;
2978 1.2 lneto '<code>e</code>' means the function may raise errors;
2979 1.2 lneto '<code>v</code>' means the function may raise an error on purpose.
2980 1.2 lneto
2981 1.2 lneto
2982 1.2 lneto
2983 1.2 lneto <hr><h3><a name="lua_absindex"><code>lua_absindex</code></a></h3><p>
2984 1.2 lneto <span class="apii">[-0, +0, –]</span>
2985 1.2 lneto <pre>int lua_absindex (lua_State *L, int idx);</pre>
2986 1.2 lneto
2987 1.2 lneto <p>
2988 1.4 mbalmer Converts the acceptable index <code>idx</code>
2989 1.4 mbalmer into an equivalent absolute index
2990 1.2 lneto (that is, one that does not depend on the stack top).
2991 1.2 lneto
2992 1.2 lneto
2993 1.1 mbalmer
2994 1.1 mbalmer
2995 1.1 mbalmer
2996 1.1 mbalmer <hr><h3><a name="lua_Alloc"><code>lua_Alloc</code></a></h3>
2997 1.1 mbalmer <pre>typedef void * (*lua_Alloc) (void *ud,
2998 1.1 mbalmer void *ptr,
2999 1.1 mbalmer size_t osize,
3000 1.1 mbalmer size_t nsize);</pre>
3001 1.1 mbalmer
3002 1.1 mbalmer <p>
3003 1.1 mbalmer The type of the memory-allocation function used by Lua states.
3004 1.1 mbalmer The allocator function must provide a
3005 1.1 mbalmer functionality similar to <code>realloc</code>,
3006 1.1 mbalmer but not exactly the same.
3007 1.1 mbalmer Its arguments are
3008 1.1 mbalmer <code>ud</code>, an opaque pointer passed to <a href="#lua_newstate"><code>lua_newstate</code></a>;
3009 1.1 mbalmer <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
3010 1.2 lneto <code>osize</code>, the original size of the block or some code about what
3011 1.2 lneto is being allocated;
3012 1.3 lneto and <code>nsize</code>, the new size of the block.
3013 1.2 lneto
3014 1.2 lneto
3015 1.2 lneto <p>
3016 1.2 lneto When <code>ptr</code> is not <code>NULL</code>,
3017 1.2 lneto <code>osize</code> is the size of the block pointed by <code>ptr</code>,
3018 1.2 lneto that is, the size given when it was allocated or reallocated.
3019 1.2 lneto
3020 1.2 lneto
3021 1.2 lneto <p>
3022 1.2 lneto When <code>ptr</code> is <code>NULL</code>,
3023 1.2 lneto <code>osize</code> encodes the kind of object that Lua is allocating.
3024 1.2 lneto <code>osize</code> is any of
3025 1.2 lneto <a href="#pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>, <a href="#pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>, <a href="#pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
3026 1.2 lneto <a href="#pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>, or <a href="#pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a> when (and only when)
3027 1.2 lneto Lua is creating a new object of that type.
3028 1.2 lneto When <code>osize</code> is some other value,
3029 1.2 lneto Lua is allocating memory for something else.
3030 1.2 lneto
3031 1.2 lneto
3032 1.2 lneto <p>
3033 1.2 lneto Lua assumes the following behavior from the allocator function:
3034 1.2 lneto
3035 1.2 lneto
3036 1.2 lneto <p>
3037 1.2 lneto When <code>nsize</code> is zero,
3038 1.2 lneto the allocator must behave like <code>free</code>
3039 1.2 lneto and return <code>NULL</code>.
3040 1.2 lneto
3041 1.2 lneto
3042 1.2 lneto <p>
3043 1.2 lneto When <code>nsize</code> is not zero,
3044 1.2 lneto the allocator must behave like <code>realloc</code>.
3045 1.2 lneto The allocator returns <code>NULL</code>
3046 1.2 lneto if and only if it cannot fulfill the request.
3047 1.1 mbalmer Lua assumes that the allocator never fails when
3048 1.1 mbalmer <code>osize >= nsize</code>.
3049 1.1 mbalmer
3050 1.1 mbalmer
3051 1.1 mbalmer <p>
3052 1.1 mbalmer Here is a simple implementation for the allocator function.
3053 1.1 mbalmer It is used in the auxiliary library by <a href="#luaL_newstate"><code>luaL_newstate</code></a>.
3054 1.1 mbalmer
3055 1.1 mbalmer <pre>
3056 1.1 mbalmer static void *l_alloc (void *ud, void *ptr, size_t osize,
3057 1.1 mbalmer size_t nsize) {
3058 1.1 mbalmer (void)ud; (void)osize; /* not used */
3059 1.1 mbalmer if (nsize == 0) {
3060 1.1 mbalmer free(ptr);
3061 1.1 mbalmer return NULL;
3062 1.1 mbalmer }
3063 1.1 mbalmer else
3064 1.1 mbalmer return realloc(ptr, nsize);
3065 1.1 mbalmer }
3066 1.1 mbalmer </pre><p>
3067 1.2 lneto Note that Standard C ensures
3068 1.1 mbalmer that <code>free(NULL)</code> has no effect and that
3069 1.3 lneto <code>realloc(NULL,size)</code> is equivalent to <code>malloc(size)</code>.
3070 1.2 lneto This code assumes that <code>realloc</code> does not fail when shrinking a block.
3071 1.2 lneto (Although Standard C does not ensure this behavior,
3072 1.2 lneto it seems to be a safe assumption.)
3073 1.1 mbalmer
3074 1.1 mbalmer
3075 1.1 mbalmer
3076 1.1 mbalmer
3077 1.1 mbalmer
3078 1.2 lneto <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
3079 1.2 lneto <span class="apii">[-(2|1), +1, <em>e</em>]</span>
3080 1.2 lneto <pre>void lua_arith (lua_State *L, int op);</pre>
3081 1.1 mbalmer
3082 1.1 mbalmer <p>
3083 1.2 lneto Performs an arithmetic or bitwise operation over the two values
3084 1.2 lneto (or one, in the case of negations)
3085 1.2 lneto at the top of the stack,
3086 1.2 lneto with the value at the top being the second operand,
3087 1.2 lneto pops these values, and pushes the result of the operation.
3088 1.2 lneto The function follows the semantics of the corresponding Lua operator
3089 1.2 lneto (that is, it may call metamethods).
3090 1.1 mbalmer
3091 1.1 mbalmer
3092 1.1 mbalmer <p>
3093 1.2 lneto The value of <code>op</code> must be one of the following constants:
3094 1.2 lneto
3095 1.2 lneto <ul>
3096 1.2 lneto
3097 1.2 lneto <li><b><a name="pdf-LUA_OPADD"><code>LUA_OPADD</code></a>: </b> performs addition (<code>+</code>)</li>
3098 1.2 lneto <li><b><a name="pdf-LUA_OPSUB"><code>LUA_OPSUB</code></a>: </b> performs subtraction (<code>-</code>)</li>
3099 1.2 lneto <li><b><a name="pdf-LUA_OPMUL"><code>LUA_OPMUL</code></a>: </b> performs multiplication (<code>*</code>)</li>
3100 1.2 lneto <li><b><a name="pdf-LUA_OPDIV"><code>LUA_OPDIV</code></a>: </b> performs float division (<code>/</code>)</li>
3101 1.3 lneto <li><b><a name="pdf-LUA_OPIDIV"><code>LUA_OPIDIV</code></a>: </b> performs floor division (<code>//</code>)</li>
3102 1.2 lneto <li><b><a name="pdf-LUA_OPMOD"><code>LUA_OPMOD</code></a>: </b> performs modulo (<code>%</code>)</li>
3103 1.2 lneto <li><b><a name="pdf-LUA_OPPOW"><code>LUA_OPPOW</code></a>: </b> performs exponentiation (<code>^</code>)</li>
3104 1.2 lneto <li><b><a name="pdf-LUA_OPUNM"><code>LUA_OPUNM</code></a>: </b> performs mathematical negation (unary <code>-</code>)</li>
3105 1.2 lneto <li><b><a name="pdf-LUA_OPBNOT"><code>LUA_OPBNOT</code></a>: </b> performs bitwise negation (<code>~</code>)</li>
3106 1.2 lneto <li><b><a name="pdf-LUA_OPBAND"><code>LUA_OPBAND</code></a>: </b> performs bitwise and (<code>&</code>)</li>
3107 1.2 lneto <li><b><a name="pdf-LUA_OPBOR"><code>LUA_OPBOR</code></a>: </b> performs bitwise or (<code>|</code>)</li>
3108 1.2 lneto <li><b><a name="pdf-LUA_OPBXOR"><code>LUA_OPBXOR</code></a>: </b> performs bitwise exclusive or (<code>~</code>)</li>
3109 1.2 lneto <li><b><a name="pdf-LUA_OPSHL"><code>LUA_OPSHL</code></a>: </b> performs left shift (<code><<</code>)</li>
3110 1.2 lneto <li><b><a name="pdf-LUA_OPSHR"><code>LUA_OPSHR</code></a>: </b> performs right shift (<code>>></code>)</li>
3111 1.2 lneto
3112 1.2 lneto </ul>
3113 1.2 lneto
3114 1.2 lneto
3115 1.2 lneto
3116 1.1 mbalmer
3117 1.2 lneto <hr><h3><a name="lua_atpanic"><code>lua_atpanic</code></a></h3><p>
3118 1.2 lneto <span class="apii">[-0, +0, –]</span>
3119 1.2 lneto <pre>lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);</pre>
3120 1.1 mbalmer
3121 1.1 mbalmer <p>
3122 1.2 lneto Sets a new panic function and returns the old one (see <a href="#4.6">§4.6</a>).
3123 1.1 mbalmer
3124 1.1 mbalmer
3125 1.1 mbalmer
3126 1.1 mbalmer
3127 1.1 mbalmer
3128 1.1 mbalmer <hr><h3><a name="lua_call"><code>lua_call</code></a></h3><p>
3129 1.2 lneto <span class="apii">[-(nargs+1), +nresults, <em>e</em>]</span>
3130 1.1 mbalmer <pre>void lua_call (lua_State *L, int nargs, int nresults);</pre>
3131 1.1 mbalmer
3132 1.1 mbalmer <p>
3133 1.1 mbalmer Calls a function.
3134 1.1 mbalmer
3135 1.1 mbalmer
3136 1.1 mbalmer <p>
3137 1.1 mbalmer To call a function you must use the following protocol:
3138 1.1 mbalmer first, the function to be called is pushed onto the stack;
3139 1.1 mbalmer then, the arguments to the function are pushed
3140 1.1 mbalmer in direct order;
3141 1.1 mbalmer that is, the first argument is pushed first.
3142 1.1 mbalmer Finally you call <a href="#lua_call"><code>lua_call</code></a>;
3143 1.1 mbalmer <code>nargs</code> is the number of arguments that you pushed onto the stack.
3144 1.1 mbalmer All arguments and the function value are popped from the stack
3145 1.1 mbalmer when the function is called.
3146 1.1 mbalmer The function results are pushed onto the stack when the function returns.
3147 1.1 mbalmer The number of results is adjusted to <code>nresults</code>,
3148 1.1 mbalmer unless <code>nresults</code> is <a name="pdf-LUA_MULTRET"><code>LUA_MULTRET</code></a>.
3149 1.2 lneto In this case, all results from the function are pushed.
3150 1.5 lneto Lua takes care that the returned values fit into the stack space,
3151 1.5 lneto but it does not ensure any extra space in the stack.
3152 1.1 mbalmer The function results are pushed onto the stack in direct order
3153 1.1 mbalmer (the first result is pushed first),
3154 1.1 mbalmer so that after the call the last result is on the top of the stack.
3155 1.1 mbalmer
3156 1.1 mbalmer
3157 1.1 mbalmer <p>
3158 1.1 mbalmer Any error inside the called function is propagated upwards
3159 1.1 mbalmer (with a <code>longjmp</code>).
3160 1.1 mbalmer
3161 1.1 mbalmer
3162 1.1 mbalmer <p>
3163 1.1 mbalmer The following example shows how the host program can do the
3164 1.1 mbalmer equivalent to this Lua code:
3165 1.1 mbalmer
3166 1.1 mbalmer <pre>
3167 1.1 mbalmer a = f("how", t.x, 14)
3168 1.1 mbalmer </pre><p>
3169 1.1 mbalmer Here it is in C:
3170 1.1 mbalmer
3171 1.1 mbalmer <pre>
3172 1.2 lneto lua_getglobal(L, "f"); /* function to be called */
3173 1.3 lneto lua_pushliteral(L, "how"); /* 1st argument */
3174 1.2 lneto lua_getglobal(L, "t"); /* table to be indexed */
3175 1.1 mbalmer lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
3176 1.1 mbalmer lua_remove(L, -2); /* remove 't' from the stack */
3177 1.1 mbalmer lua_pushinteger(L, 14); /* 3rd argument */
3178 1.1 mbalmer lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
3179 1.2 lneto lua_setglobal(L, "a"); /* set global 'a' */
3180 1.1 mbalmer </pre><p>
3181 1.3 lneto Note that the code above is <em>balanced</em>:
3182 1.1 mbalmer at its end, the stack is back to its original configuration.
3183 1.1 mbalmer This is considered good programming practice.
3184 1.1 mbalmer
3185 1.1 mbalmer
3186 1.1 mbalmer
3187 1.1 mbalmer
3188 1.1 mbalmer
3189 1.2 lneto <hr><h3><a name="lua_callk"><code>lua_callk</code></a></h3><p>
3190 1.2 lneto <span class="apii">[-(nargs + 1), +nresults, <em>e</em>]</span>
3191 1.3 lneto <pre>void lua_callk (lua_State *L,
3192 1.3 lneto int nargs,
3193 1.3 lneto int nresults,
3194 1.3 lneto lua_KContext ctx,
3195 1.2 lneto lua_KFunction k);</pre>
3196 1.2 lneto
3197 1.2 lneto <p>
3198 1.2 lneto This function behaves exactly like <a href="#lua_call"><code>lua_call</code></a>,
3199 1.2 lneto but allows the called function to yield (see <a href="#4.7">§4.7</a>).
3200 1.2 lneto
3201 1.2 lneto
3202 1.2 lneto
3203 1.2 lneto
3204 1.2 lneto
3205 1.1 mbalmer <hr><h3><a name="lua_CFunction"><code>lua_CFunction</code></a></h3>
3206 1.1 mbalmer <pre>typedef int (*lua_CFunction) (lua_State *L);</pre>
3207 1.1 mbalmer
3208 1.1 mbalmer <p>
3209 1.1 mbalmer Type for C functions.
3210 1.1 mbalmer
3211 1.1 mbalmer
3212 1.1 mbalmer <p>
3213 1.1 mbalmer In order to communicate properly with Lua,
3214 1.1 mbalmer a C function must use the following protocol,
3215 1.1 mbalmer which defines the way parameters and results are passed:
3216 1.1 mbalmer a C function receives its arguments from Lua in its stack
3217 1.1 mbalmer in direct order (the first argument is pushed first).
3218 1.1 mbalmer So, when the function starts,
3219 1.1 mbalmer <code>lua_gettop(L)</code> returns the number of arguments received by the function.
3220 1.1 mbalmer The first argument (if any) is at index 1
3221 1.1 mbalmer and its last argument is at index <code>lua_gettop(L)</code>.
3222 1.1 mbalmer To return values to Lua, a C function just pushes them onto the stack,
3223 1.1 mbalmer in direct order (the first result is pushed first),
3224 1.1 mbalmer and returns the number of results.
3225 1.1 mbalmer Any other value in the stack below the results will be properly
3226 1.1 mbalmer discarded by Lua.
3227 1.1 mbalmer Like a Lua function, a C function called by Lua can also return
3228 1.1 mbalmer many results.
3229 1.1 mbalmer
3230 1.1 mbalmer
3231 1.1 mbalmer <p>
3232 1.1 mbalmer As an example, the following function receives a variable number
3233 1.4 mbalmer of numeric arguments and returns their average and their sum:
3234 1.1 mbalmer
3235 1.1 mbalmer <pre>
3236 1.1 mbalmer static int foo (lua_State *L) {
3237 1.1 mbalmer int n = lua_gettop(L); /* number of arguments */
3238 1.3 lneto lua_Number sum = 0.0;
3239 1.1 mbalmer int i;
3240 1.1 mbalmer for (i = 1; i <= n; i++) {
3241 1.1 mbalmer if (!lua_isnumber(L, i)) {
3242 1.3 lneto lua_pushliteral(L, "incorrect argument");
3243 1.1 mbalmer lua_error(L);
3244 1.1 mbalmer }
3245 1.1 mbalmer sum += lua_tonumber(L, i);
3246 1.1 mbalmer }
3247 1.1 mbalmer lua_pushnumber(L, sum/n); /* first result */
3248 1.1 mbalmer lua_pushnumber(L, sum); /* second result */
3249 1.1 mbalmer return 2; /* number of results */
3250 1.1 mbalmer }
3251 1.1 mbalmer </pre>
3252 1.1 mbalmer
3253 1.1 mbalmer
3254 1.1 mbalmer
3255 1.1 mbalmer
3256 1.1 mbalmer <hr><h3><a name="lua_checkstack"><code>lua_checkstack</code></a></h3><p>
3257 1.2 lneto <span class="apii">[-0, +0, –]</span>
3258 1.3 lneto <pre>int lua_checkstack (lua_State *L, int n);</pre>
3259 1.1 mbalmer
3260 1.1 mbalmer <p>
3261 1.5 lneto Ensures that the stack has space for at least <code>n</code> extra slots
3262 1.5 lneto (that is, that you can safely push up to <code>n</code> values into it).
3263 1.2 lneto It returns false if it cannot fulfill the request,
3264 1.3 lneto either because it would cause the stack
3265 1.3 lneto to be larger than a fixed maximum size
3266 1.3 lneto (typically at least several thousand elements) or
3267 1.3 lneto because it cannot allocate memory for the extra space.
3268 1.1 mbalmer This function never shrinks the stack;
3269 1.5 lneto if the stack already has space for the extra slots,
3270 1.1 mbalmer it is left unchanged.
3271 1.1 mbalmer
3272 1.1 mbalmer
3273 1.1 mbalmer
3274 1.1 mbalmer
3275 1.1 mbalmer
3276 1.1 mbalmer <hr><h3><a name="lua_close"><code>lua_close</code></a></h3><p>
3277 1.2 lneto <span class="apii">[-0, +0, –]</span>
3278 1.1 mbalmer <pre>void lua_close (lua_State *L);</pre>
3279 1.1 mbalmer
3280 1.1 mbalmer <p>
3281 1.1 mbalmer Destroys all objects in the given Lua state
3282 1.1 mbalmer (calling the corresponding garbage-collection metamethods, if any)
3283 1.1 mbalmer and frees all dynamic memory used by this state.
3284 1.1 mbalmer On several platforms, you may not need to call this function,
3285 1.1 mbalmer because all resources are naturally released when the host program ends.
3286 1.2 lneto On the other hand, long-running programs that create multiple states,
3287 1.2 lneto such as daemons or web servers,
3288 1.3 lneto will probably need to close states as soon as they are not needed.
3289 1.2 lneto
3290 1.1 mbalmer
3291 1.1 mbalmer
3292 1.1 mbalmer
3293 1.1 mbalmer
3294 1.2 lneto <hr><h3><a name="lua_compare"><code>lua_compare</code></a></h3><p>
3295 1.2 lneto <span class="apii">[-0, +0, <em>e</em>]</span>
3296 1.2 lneto <pre>int lua_compare (lua_State *L, int index1, int index2, int op);</pre>
3297 1.2 lneto
3298 1.2 lneto <p>
3299 1.2 lneto Compares two Lua values.
3300 1.2 lneto Returns 1 if the value at index <code>index1</code> satisfies <code>op</code>
3301 1.2 lneto when compared with the value at index <code>index2</code>,
3302 1.2 lneto following the semantics of the corresponding Lua operator
3303 1.2 lneto (that is, it may call metamethods).
3304 1.2 lneto Otherwise returns 0.
3305 1.3 lneto Also returns 0 if any of the indices is not valid.
3306 1.2 lneto
3307 1.2 lneto
3308 1.2 lneto <p>
3309 1.2 lneto The value of <code>op</code> must be one of the following constants:
3310 1.2 lneto
3311 1.2 lneto <ul>
3312 1.2 lneto
3313 1.2 lneto <li><b><a name="pdf-LUA_OPEQ"><code>LUA_OPEQ</code></a>: </b> compares for equality (<code>==</code>)</li>
3314 1.2 lneto <li><b><a name="pdf-LUA_OPLT"><code>LUA_OPLT</code></a>: </b> compares for less than (<code><</code>)</li>
3315 1.2 lneto <li><b><a name="pdf-LUA_OPLE"><code>LUA_OPLE</code></a>: </b> compares for less or equal (<code><=</code>)</li>
3316 1.2 lneto
3317 1.2 lneto </ul>
3318 1.2 lneto
3319 1.2 lneto
3320 1.2 lneto
3321 1.1 mbalmer
3322 1.1 mbalmer <hr><h3><a name="lua_concat"><code>lua_concat</code></a></h3><p>
3323 1.1 mbalmer <span class="apii">[-n, +1, <em>e</em>]</span>
3324 1.1 mbalmer <pre>void lua_concat (lua_State *L, int n);</pre>
3325 1.1 mbalmer
3326 1.1 mbalmer <p>
3327 1.1 mbalmer Concatenates the <code>n</code> values at the top of the stack,
3328 1.1 mbalmer pops them, and leaves the result at the top.
3329 1.1 mbalmer If <code>n</code> is 1, the result is the single value on the stack
3330 1.1 mbalmer (that is, the function does nothing);
3331 1.1 mbalmer if <code>n</code> is 0, the result is the empty string.
3332 1.1 mbalmer Concatenation is performed following the usual semantics of Lua
3333 1.2 lneto (see <a href="#3.4.6">§3.4.6</a>).
3334 1.1 mbalmer
3335 1.1 mbalmer
3336 1.1 mbalmer
3337 1.1 mbalmer
3338 1.1 mbalmer
3339 1.2 lneto <hr><h3><a name="lua_copy"><code>lua_copy</code></a></h3><p>
3340 1.2 lneto <span class="apii">[-0, +0, –]</span>
3341 1.2 lneto <pre>void lua_copy (lua_State *L, int fromidx, int toidx);</pre>
3342 1.1 mbalmer
3343 1.1 mbalmer <p>
3344 1.3 lneto Copies the element at index <code>fromidx</code>
3345 1.3 lneto into the valid index <code>toidx</code>,
3346 1.3 lneto replacing the value at that position.
3347 1.3 lneto Values at other positions are not affected.
3348 1.1 mbalmer
3349 1.1 mbalmer
3350 1.1 mbalmer
3351 1.1 mbalmer
3352 1.1 mbalmer
3353 1.1 mbalmer <hr><h3><a name="lua_createtable"><code>lua_createtable</code></a></h3><p>
3354 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
3355 1.1 mbalmer <pre>void lua_createtable (lua_State *L, int narr, int nrec);</pre>
3356 1.1 mbalmer
3357 1.1 mbalmer <p>
3358 1.1 mbalmer Creates a new empty table and pushes it onto the stack.
3359 1.2 lneto Parameter <code>narr</code> is a hint for how many elements the table
3360 1.2 lneto will have as a sequence;
3361 1.2 lneto parameter <code>nrec</code> is a hint for how many other elements
3362 1.1 mbalmer the table will have.
3363 1.2 lneto Lua may use these hints to preallocate memory for the new table.
3364 1.5 lneto This preallocation is useful for performance when you know in advance
3365 1.2 lneto how many elements the table will have.
3366 1.1 mbalmer Otherwise you can use the function <a href="#lua_newtable"><code>lua_newtable</code></a>.
3367 1.1 mbalmer
3368 1.1 mbalmer
3369 1.1 mbalmer
3370 1.1 mbalmer
3371 1.1 mbalmer
3372 1.1 mbalmer <hr><h3><a name="lua_dump"><code>lua_dump</code></a></h3><p>
3373 1.5 lneto <span class="apii">[-0, +0, –]</span>
3374 1.2 lneto <pre>int lua_dump (lua_State *L,
3375 1.2 lneto lua_Writer writer,
3376 1.2 lneto void *data,
3377 1.2 lneto int strip);</pre>
3378 1.1 mbalmer
3379 1.1 mbalmer <p>
3380 1.1 mbalmer Dumps a function as a binary chunk.
3381 1.1 mbalmer Receives a Lua function on the top of the stack
3382 1.1 mbalmer and produces a binary chunk that,
3383 1.1 mbalmer if loaded again,
3384 1.1 mbalmer results in a function equivalent to the one dumped.
3385 1.1 mbalmer As it produces parts of the chunk,
3386 1.1 mbalmer <a href="#lua_dump"><code>lua_dump</code></a> calls function <code>writer</code> (see <a href="#lua_Writer"><code>lua_Writer</code></a>)
3387 1.1 mbalmer with the given <code>data</code>
3388 1.1 mbalmer to write them.
3389 1.1 mbalmer
3390 1.1 mbalmer
3391 1.1 mbalmer <p>
3392 1.2 lneto If <code>strip</code> is true,
3393 1.4 mbalmer the binary representation may not include all debug information
3394 1.4 mbalmer about the function,
3395 1.4 mbalmer to save space.
3396 1.2 lneto
3397 1.2 lneto
3398 1.2 lneto <p>
3399 1.1 mbalmer The value returned is the error code returned by the last
3400 1.1 mbalmer call to the writer;
3401 1.1 mbalmer 0 means no errors.
3402 1.1 mbalmer
3403 1.1 mbalmer
3404 1.1 mbalmer <p>
3405 1.1 mbalmer This function does not pop the Lua function from the stack.
3406 1.1 mbalmer
3407 1.1 mbalmer
3408 1.1 mbalmer
3409 1.1 mbalmer
3410 1.1 mbalmer
3411 1.1 mbalmer <hr><h3><a name="lua_error"><code>lua_error</code></a></h3><p>
3412 1.1 mbalmer <span class="apii">[-1, +0, <em>v</em>]</span>
3413 1.1 mbalmer <pre>int lua_error (lua_State *L);</pre>
3414 1.1 mbalmer
3415 1.1 mbalmer <p>
3416 1.3 lneto Generates a Lua error,
3417 1.3 lneto using the value at the top of the stack as the error object.
3418 1.1 mbalmer This function does a long jump,
3419 1.2 lneto and therefore never returns
3420 1.1 mbalmer (see <a href="#luaL_error"><code>luaL_error</code></a>).
3421 1.1 mbalmer
3422 1.1 mbalmer
3423 1.1 mbalmer
3424 1.1 mbalmer
3425 1.1 mbalmer
3426 1.1 mbalmer <hr><h3><a name="lua_gc"><code>lua_gc</code></a></h3><p>
3427 1.1 mbalmer <span class="apii">[-0, +0, <em>e</em>]</span>
3428 1.1 mbalmer <pre>int lua_gc (lua_State *L, int what, int data);</pre>
3429 1.1 mbalmer
3430 1.1 mbalmer <p>
3431 1.1 mbalmer Controls the garbage collector.
3432 1.1 mbalmer
3433 1.1 mbalmer
3434 1.1 mbalmer <p>
3435 1.1 mbalmer This function performs several tasks,
3436 1.1 mbalmer according to the value of the parameter <code>what</code>:
3437 1.1 mbalmer
3438 1.1 mbalmer <ul>
3439 1.1 mbalmer
3440 1.2 lneto <li><b><code>LUA_GCSTOP</code>: </b>
3441 1.1 mbalmer stops the garbage collector.
3442 1.1 mbalmer </li>
3443 1.1 mbalmer
3444 1.2 lneto <li><b><code>LUA_GCRESTART</code>: </b>
3445 1.1 mbalmer restarts the garbage collector.
3446 1.1 mbalmer </li>
3447 1.1 mbalmer
3448 1.2 lneto <li><b><code>LUA_GCCOLLECT</code>: </b>
3449 1.1 mbalmer performs a full garbage-collection cycle.
3450 1.1 mbalmer </li>
3451 1.1 mbalmer
3452 1.2 lneto <li><b><code>LUA_GCCOUNT</code>: </b>
3453 1.1 mbalmer returns the current amount of memory (in Kbytes) in use by Lua.
3454 1.1 mbalmer </li>
3455 1.1 mbalmer
3456 1.2 lneto <li><b><code>LUA_GCCOUNTB</code>: </b>
3457 1.1 mbalmer returns the remainder of dividing the current amount of bytes of
3458 1.1 mbalmer memory in use by Lua by 1024.
3459 1.1 mbalmer </li>
3460 1.1 mbalmer
3461 1.2 lneto <li><b><code>LUA_GCSTEP</code>: </b>
3462 1.1 mbalmer performs an incremental step of garbage collection.
3463 1.1 mbalmer </li>
3464 1.1 mbalmer
3465 1.2 lneto <li><b><code>LUA_GCSETPAUSE</code>: </b>
3466 1.1 mbalmer sets <code>data</code> as the new value
3467 1.2 lneto for the <em>pause</em> of the collector (see <a href="#2.5">§2.5</a>)
3468 1.2 lneto and returns the previous value of the pause.
3469 1.1 mbalmer </li>
3470 1.1 mbalmer
3471 1.2 lneto <li><b><code>LUA_GCSETSTEPMUL</code>: </b>
3472 1.1 mbalmer sets <code>data</code> as the new value for the <em>step multiplier</em> of
3473 1.2 lneto the collector (see <a href="#2.5">§2.5</a>)
3474 1.2 lneto and returns the previous value of the step multiplier.
3475 1.2 lneto </li>
3476 1.2 lneto
3477 1.2 lneto <li><b><code>LUA_GCISRUNNING</code>: </b>
3478 1.2 lneto returns a boolean that tells whether the collector is running
3479 1.2 lneto (i.e., not stopped).
3480 1.1 mbalmer </li>
3481 1.1 mbalmer
3482 1.1 mbalmer </ul>
3483 1.1 mbalmer
3484 1.2 lneto <p>
3485 1.2 lneto For more details about these options,
3486 1.2 lneto see <a href="#pdf-collectgarbage"><code>collectgarbage</code></a>.
3487 1.2 lneto
3488 1.2 lneto
3489 1.1 mbalmer
3490 1.1 mbalmer
3491 1.1 mbalmer
3492 1.1 mbalmer <hr><h3><a name="lua_getallocf"><code>lua_getallocf</code></a></h3><p>
3493 1.2 lneto <span class="apii">[-0, +0, –]</span>
3494 1.1 mbalmer <pre>lua_Alloc lua_getallocf (lua_State *L, void **ud);</pre>
3495 1.1 mbalmer
3496 1.1 mbalmer <p>
3497 1.1 mbalmer Returns the memory-allocation function of a given state.
3498 1.1 mbalmer If <code>ud</code> is not <code>NULL</code>, Lua stores in <code>*ud</code> the
3499 1.3 lneto opaque pointer given when the memory-allocator function was set.
3500 1.1 mbalmer
3501 1.1 mbalmer
3502 1.1 mbalmer
3503 1.1 mbalmer
3504 1.1 mbalmer
3505 1.1 mbalmer <hr><h3><a name="lua_getfield"><code>lua_getfield</code></a></h3><p>
3506 1.1 mbalmer <span class="apii">[-0, +1, <em>e</em>]</span>
3507 1.2 lneto <pre>int lua_getfield (lua_State *L, int index, const char *k);</pre>
3508 1.1 mbalmer
3509 1.1 mbalmer <p>
3510 1.1 mbalmer Pushes onto the stack the value <code>t[k]</code>,
3511 1.2 lneto where <code>t</code> is the value at the given index.
3512 1.1 mbalmer As in Lua, this function may trigger a metamethod
3513 1.2 lneto for the "index" event (see <a href="#2.4">§2.4</a>).
3514 1.2 lneto
3515 1.2 lneto
3516 1.2 lneto <p>
3517 1.2 lneto Returns the type of the pushed value.
3518 1.1 mbalmer
3519 1.1 mbalmer
3520 1.1 mbalmer
3521 1.1 mbalmer
3522 1.1 mbalmer
3523 1.3 lneto <hr><h3><a name="lua_getextraspace"><code>lua_getextraspace</code></a></h3><p>
3524 1.3 lneto <span class="apii">[-0, +0, –]</span>
3525 1.3 lneto <pre>void *lua_getextraspace (lua_State *L);</pre>
3526 1.3 lneto
3527 1.3 lneto <p>
3528 1.3 lneto Returns a pointer to a raw memory area associated with the
3529 1.3 lneto given Lua state.
3530 1.3 lneto The application can use this area for any purpose;
3531 1.3 lneto Lua does not use it for anything.
3532 1.3 lneto
3533 1.3 lneto
3534 1.3 lneto <p>
3535 1.3 lneto Each new thread has this area initialized with a copy
3536 1.3 lneto of the area of the main thread.
3537 1.3 lneto
3538 1.3 lneto
3539 1.3 lneto <p>
3540 1.3 lneto By default, this area has the size of a pointer to void,
3541 1.3 lneto but you can recompile Lua with a different size for this area.
3542 1.3 lneto (See <code>LUA_EXTRASPACE</code> in <code>luaconf.h</code>.)
3543 1.3 lneto
3544 1.3 lneto
3545 1.3 lneto
3546 1.3 lneto
3547 1.3 lneto
3548 1.1 mbalmer <hr><h3><a name="lua_getglobal"><code>lua_getglobal</code></a></h3><p>
3549 1.1 mbalmer <span class="apii">[-0, +1, <em>e</em>]</span>
3550 1.2 lneto <pre>int lua_getglobal (lua_State *L, const char *name);</pre>
3551 1.1 mbalmer
3552 1.1 mbalmer <p>
3553 1.1 mbalmer Pushes onto the stack the value of the global <code>name</code>.
3554 1.2 lneto Returns the type of that value.
3555 1.1 mbalmer
3556 1.1 mbalmer
3557 1.1 mbalmer
3558 1.1 mbalmer
3559 1.1 mbalmer
3560 1.3 lneto <hr><h3><a name="lua_geti"><code>lua_geti</code></a></h3><p>
3561 1.3 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
3562 1.3 lneto <pre>int lua_geti (lua_State *L, int index, lua_Integer i);</pre>
3563 1.3 lneto
3564 1.3 lneto <p>
3565 1.3 lneto Pushes onto the stack the value <code>t[i]</code>,
3566 1.3 lneto where <code>t</code> is the value at the given index.
3567 1.3 lneto As in Lua, this function may trigger a metamethod
3568 1.3 lneto for the "index" event (see <a href="#2.4">§2.4</a>).
3569 1.3 lneto
3570 1.3 lneto
3571 1.3 lneto <p>
3572 1.3 lneto Returns the type of the pushed value.
3573 1.3 lneto
3574 1.3 lneto
3575 1.3 lneto
3576 1.3 lneto
3577 1.3 lneto
3578 1.1 mbalmer <hr><h3><a name="lua_getmetatable"><code>lua_getmetatable</code></a></h3><p>
3579 1.2 lneto <span class="apii">[-0, +(0|1), –]</span>
3580 1.1 mbalmer <pre>int lua_getmetatable (lua_State *L, int index);</pre>
3581 1.1 mbalmer
3582 1.1 mbalmer <p>
3583 1.3 lneto If the value at the given index has a metatable,
3584 1.3 lneto the function pushes that metatable onto the stack and returns 1.
3585 1.3 lneto Otherwise,
3586 1.1 mbalmer the function returns 0 and pushes nothing on the stack.
3587 1.1 mbalmer
3588 1.1 mbalmer
3589 1.1 mbalmer
3590 1.1 mbalmer
3591 1.1 mbalmer
3592 1.1 mbalmer <hr><h3><a name="lua_gettable"><code>lua_gettable</code></a></h3><p>
3593 1.1 mbalmer <span class="apii">[-1, +1, <em>e</em>]</span>
3594 1.2 lneto <pre>int lua_gettable (lua_State *L, int index);</pre>
3595 1.1 mbalmer
3596 1.1 mbalmer <p>
3597 1.1 mbalmer Pushes onto the stack the value <code>t[k]</code>,
3598 1.2 lneto where <code>t</code> is the value at the given index
3599 1.1 mbalmer and <code>k</code> is the value at the top of the stack.
3600 1.1 mbalmer
3601 1.1 mbalmer
3602 1.1 mbalmer <p>
3603 1.3 lneto This function pops the key from the stack,
3604 1.3 lneto pushing the resulting value in its place.
3605 1.1 mbalmer As in Lua, this function may trigger a metamethod
3606 1.2 lneto for the "index" event (see <a href="#2.4">§2.4</a>).
3607 1.2 lneto
3608 1.2 lneto
3609 1.2 lneto <p>
3610 1.2 lneto Returns the type of the pushed value.
3611 1.1 mbalmer
3612 1.1 mbalmer
3613 1.1 mbalmer
3614 1.1 mbalmer
3615 1.1 mbalmer
3616 1.1 mbalmer <hr><h3><a name="lua_gettop"><code>lua_gettop</code></a></h3><p>
3617 1.2 lneto <span class="apii">[-0, +0, –]</span>
3618 1.1 mbalmer <pre>int lua_gettop (lua_State *L);</pre>
3619 1.1 mbalmer
3620 1.1 mbalmer <p>
3621 1.1 mbalmer Returns the index of the top element in the stack.
3622 1.1 mbalmer Because indices start at 1,
3623 1.3 lneto this result is equal to the number of elements in the stack;
3624 1.3 lneto in particular, 0 means an empty stack.
3625 1.1 mbalmer
3626 1.1 mbalmer
3627 1.1 mbalmer
3628 1.1 mbalmer
3629 1.1 mbalmer
3630 1.2 lneto <hr><h3><a name="lua_getuservalue"><code>lua_getuservalue</code></a></h3><p>
3631 1.2 lneto <span class="apii">[-0, +1, –]</span>
3632 1.2 lneto <pre>int lua_getuservalue (lua_State *L, int index);</pre>
3633 1.2 lneto
3634 1.2 lneto <p>
3635 1.2 lneto Pushes onto the stack the Lua value associated with the userdata
3636 1.2 lneto at the given index.
3637 1.2 lneto
3638 1.2 lneto
3639 1.2 lneto <p>
3640 1.2 lneto Returns the type of the pushed value.
3641 1.2 lneto
3642 1.2 lneto
3643 1.2 lneto
3644 1.2 lneto
3645 1.2 lneto
3646 1.1 mbalmer <hr><h3><a name="lua_insert"><code>lua_insert</code></a></h3><p>
3647 1.2 lneto <span class="apii">[-1, +1, –]</span>
3648 1.1 mbalmer <pre>void lua_insert (lua_State *L, int index);</pre>
3649 1.1 mbalmer
3650 1.1 mbalmer <p>
3651 1.1 mbalmer Moves the top element into the given valid index,
3652 1.1 mbalmer shifting up the elements above this index to open space.
3653 1.2 lneto This function cannot be called with a pseudo-index,
3654 1.1 mbalmer because a pseudo-index is not an actual stack position.
3655 1.1 mbalmer
3656 1.1 mbalmer
3657 1.1 mbalmer
3658 1.1 mbalmer
3659 1.1 mbalmer
3660 1.1 mbalmer <hr><h3><a name="lua_Integer"><code>lua_Integer</code></a></h3>
3661 1.2 lneto <pre>typedef ... lua_Integer;</pre>
3662 1.1 mbalmer
3663 1.1 mbalmer <p>
3664 1.2 lneto The type of integers in Lua.
3665 1.1 mbalmer
3666 1.1 mbalmer
3667 1.1 mbalmer <p>
3668 1.3 lneto By default this type is <code>long long</code>,
3669 1.2 lneto (usually a 64-bit two-complement integer),
3670 1.3 lneto but that can be changed to <code>long</code> or <code>int</code>
3671 1.2 lneto (usually a 32-bit two-complement integer).
3672 1.4 mbalmer (See <code>LUA_INT_TYPE</code> in <code>luaconf.h</code>.)
3673 1.2 lneto
3674 1.2 lneto
3675 1.2 lneto <p>
3676 1.2 lneto Lua also defines the constants
3677 1.2 lneto <a name="pdf-LUA_MININTEGER"><code>LUA_MININTEGER</code></a> and <a name="pdf-LUA_MAXINTEGER"><code>LUA_MAXINTEGER</code></a>,
3678 1.2 lneto with the minimum and the maximum values that fit in this type.
3679 1.1 mbalmer
3680 1.1 mbalmer
3681 1.1 mbalmer
3682 1.1 mbalmer
3683 1.1 mbalmer
3684 1.1 mbalmer <hr><h3><a name="lua_isboolean"><code>lua_isboolean</code></a></h3><p>
3685 1.2 lneto <span class="apii">[-0, +0, –]</span>
3686 1.1 mbalmer <pre>int lua_isboolean (lua_State *L, int index);</pre>
3687 1.1 mbalmer
3688 1.1 mbalmer <p>
3689 1.2 lneto Returns 1 if the value at the given index is a boolean,
3690 1.1 mbalmer and 0 otherwise.
3691 1.1 mbalmer
3692 1.1 mbalmer
3693 1.1 mbalmer
3694 1.1 mbalmer
3695 1.1 mbalmer
3696 1.1 mbalmer <hr><h3><a name="lua_iscfunction"><code>lua_iscfunction</code></a></h3><p>
3697 1.2 lneto <span class="apii">[-0, +0, –]</span>
3698 1.1 mbalmer <pre>int lua_iscfunction (lua_State *L, int index);</pre>
3699 1.1 mbalmer
3700 1.1 mbalmer <p>
3701 1.2 lneto Returns 1 if the value at the given index is a C function,
3702 1.1 mbalmer and 0 otherwise.
3703 1.1 mbalmer
3704 1.1 mbalmer
3705 1.1 mbalmer
3706 1.1 mbalmer
3707 1.1 mbalmer
3708 1.1 mbalmer <hr><h3><a name="lua_isfunction"><code>lua_isfunction</code></a></h3><p>
3709 1.2 lneto <span class="apii">[-0, +0, –]</span>
3710 1.1 mbalmer <pre>int lua_isfunction (lua_State *L, int index);</pre>
3711 1.1 mbalmer
3712 1.1 mbalmer <p>
3713 1.2 lneto Returns 1 if the value at the given index is a function
3714 1.1 mbalmer (either C or Lua), and 0 otherwise.
3715 1.1 mbalmer
3716 1.1 mbalmer
3717 1.1 mbalmer
3718 1.1 mbalmer
3719 1.1 mbalmer
3720 1.2 lneto <hr><h3><a name="lua_isinteger"><code>lua_isinteger</code></a></h3><p>
3721 1.2 lneto <span class="apii">[-0, +0, –]</span>
3722 1.2 lneto <pre>int lua_isinteger (lua_State *L, int index);</pre>
3723 1.2 lneto
3724 1.2 lneto <p>
3725 1.2 lneto Returns 1 if the value at the given index is an integer
3726 1.2 lneto (that is, the value is a number and is represented as an integer),
3727 1.2 lneto and 0 otherwise.
3728 1.2 lneto
3729 1.2 lneto
3730 1.2 lneto
3731 1.2 lneto
3732 1.2 lneto
3733 1.1 mbalmer <hr><h3><a name="lua_islightuserdata"><code>lua_islightuserdata</code></a></h3><p>
3734 1.2 lneto <span class="apii">[-0, +0, –]</span>
3735 1.1 mbalmer <pre>int lua_islightuserdata (lua_State *L, int index);</pre>
3736 1.1 mbalmer
3737 1.1 mbalmer <p>
3738 1.2 lneto Returns 1 if the value at the given index is a light userdata,
3739 1.1 mbalmer and 0 otherwise.
3740 1.1 mbalmer
3741 1.1 mbalmer
3742 1.1 mbalmer
3743 1.1 mbalmer
3744 1.1 mbalmer
3745 1.1 mbalmer <hr><h3><a name="lua_isnil"><code>lua_isnil</code></a></h3><p>
3746 1.2 lneto <span class="apii">[-0, +0, –]</span>
3747 1.1 mbalmer <pre>int lua_isnil (lua_State *L, int index);</pre>
3748 1.1 mbalmer
3749 1.1 mbalmer <p>
3750 1.2 lneto Returns 1 if the value at the given index is <b>nil</b>,
3751 1.1 mbalmer and 0 otherwise.
3752 1.1 mbalmer
3753 1.1 mbalmer
3754 1.1 mbalmer
3755 1.1 mbalmer
3756 1.1 mbalmer
3757 1.1 mbalmer <hr><h3><a name="lua_isnone"><code>lua_isnone</code></a></h3><p>
3758 1.2 lneto <span class="apii">[-0, +0, –]</span>
3759 1.1 mbalmer <pre>int lua_isnone (lua_State *L, int index);</pre>
3760 1.1 mbalmer
3761 1.1 mbalmer <p>
3762 1.2 lneto Returns 1 if the given index is not valid,
3763 1.1 mbalmer and 0 otherwise.
3764 1.1 mbalmer
3765 1.1 mbalmer
3766 1.1 mbalmer
3767 1.1 mbalmer
3768 1.1 mbalmer
3769 1.1 mbalmer <hr><h3><a name="lua_isnoneornil"><code>lua_isnoneornil</code></a></h3><p>
3770 1.2 lneto <span class="apii">[-0, +0, –]</span>
3771 1.1 mbalmer <pre>int lua_isnoneornil (lua_State *L, int index);</pre>
3772 1.1 mbalmer
3773 1.1 mbalmer <p>
3774 1.2 lneto Returns 1 if the given index is not valid
3775 1.1 mbalmer or if the value at this index is <b>nil</b>,
3776 1.1 mbalmer and 0 otherwise.
3777 1.1 mbalmer
3778 1.1 mbalmer
3779 1.1 mbalmer
3780 1.1 mbalmer
3781 1.1 mbalmer
3782 1.1 mbalmer <hr><h3><a name="lua_isnumber"><code>lua_isnumber</code></a></h3><p>
3783 1.2 lneto <span class="apii">[-0, +0, –]</span>
3784 1.1 mbalmer <pre>int lua_isnumber (lua_State *L, int index);</pre>
3785 1.1 mbalmer
3786 1.1 mbalmer <p>
3787 1.2 lneto Returns 1 if the value at the given index is a number
3788 1.1 mbalmer or a string convertible to a number,
3789 1.1 mbalmer and 0 otherwise.
3790 1.1 mbalmer
3791 1.1 mbalmer
3792 1.1 mbalmer
3793 1.1 mbalmer
3794 1.1 mbalmer
3795 1.1 mbalmer <hr><h3><a name="lua_isstring"><code>lua_isstring</code></a></h3><p>
3796 1.2 lneto <span class="apii">[-0, +0, –]</span>
3797 1.1 mbalmer <pre>int lua_isstring (lua_State *L, int index);</pre>
3798 1.1 mbalmer
3799 1.1 mbalmer <p>
3800 1.2 lneto Returns 1 if the value at the given index is a string
3801 1.1 mbalmer or a number (which is always convertible to a string),
3802 1.1 mbalmer and 0 otherwise.
3803 1.1 mbalmer
3804 1.1 mbalmer
3805 1.1 mbalmer
3806 1.1 mbalmer
3807 1.1 mbalmer
3808 1.1 mbalmer <hr><h3><a name="lua_istable"><code>lua_istable</code></a></h3><p>
3809 1.2 lneto <span class="apii">[-0, +0, –]</span>
3810 1.1 mbalmer <pre>int lua_istable (lua_State *L, int index);</pre>
3811 1.1 mbalmer
3812 1.1 mbalmer <p>
3813 1.2 lneto Returns 1 if the value at the given index is a table,
3814 1.1 mbalmer and 0 otherwise.
3815 1.1 mbalmer
3816 1.1 mbalmer
3817 1.1 mbalmer
3818 1.1 mbalmer
3819 1.1 mbalmer
3820 1.1 mbalmer <hr><h3><a name="lua_isthread"><code>lua_isthread</code></a></h3><p>
3821 1.2 lneto <span class="apii">[-0, +0, –]</span>
3822 1.1 mbalmer <pre>int lua_isthread (lua_State *L, int index);</pre>
3823 1.1 mbalmer
3824 1.1 mbalmer <p>
3825 1.2 lneto Returns 1 if the value at the given index is a thread,
3826 1.1 mbalmer and 0 otherwise.
3827 1.1 mbalmer
3828 1.1 mbalmer
3829 1.1 mbalmer
3830 1.1 mbalmer
3831 1.1 mbalmer
3832 1.1 mbalmer <hr><h3><a name="lua_isuserdata"><code>lua_isuserdata</code></a></h3><p>
3833 1.2 lneto <span class="apii">[-0, +0, –]</span>
3834 1.1 mbalmer <pre>int lua_isuserdata (lua_State *L, int index);</pre>
3835 1.1 mbalmer
3836 1.1 mbalmer <p>
3837 1.2 lneto Returns 1 if the value at the given index is a userdata
3838 1.1 mbalmer (either full or light), and 0 otherwise.
3839 1.1 mbalmer
3840 1.1 mbalmer
3841 1.1 mbalmer
3842 1.1 mbalmer
3843 1.1 mbalmer
3844 1.2 lneto <hr><h3><a name="lua_isyieldable"><code>lua_isyieldable</code></a></h3><p>
3845 1.2 lneto <span class="apii">[-0, +0, –]</span>
3846 1.2 lneto <pre>int lua_isyieldable (lua_State *L);</pre>
3847 1.2 lneto
3848 1.2 lneto <p>
3849 1.2 lneto Returns 1 if the given coroutine can yield,
3850 1.2 lneto and 0 otherwise.
3851 1.2 lneto
3852 1.2 lneto
3853 1.2 lneto
3854 1.2 lneto
3855 1.2 lneto
3856 1.3 lneto <hr><h3><a name="lua_KContext"><code>lua_KContext</code></a></h3>
3857 1.3 lneto <pre>typedef ... lua_KContext;</pre>
3858 1.3 lneto
3859 1.3 lneto <p>
3860 1.3 lneto The type for continuation-function contexts.
3861 1.4 mbalmer It must be a numeric type.
3862 1.3 lneto This type is defined as <code>intptr_t</code>
3863 1.3 lneto when <code>intptr_t</code> is available,
3864 1.3 lneto so that it can store pointers too.
3865 1.3 lneto Otherwise, it is defined as <code>ptrdiff_t</code>.
3866 1.3 lneto
3867 1.3 lneto
3868 1.3 lneto
3869 1.3 lneto
3870 1.3 lneto
3871 1.2 lneto <hr><h3><a name="lua_KFunction"><code>lua_KFunction</code></a></h3>
3872 1.3 lneto <pre>typedef int (*lua_KFunction) (lua_State *L, int status, lua_KContext ctx);</pre>
3873 1.2 lneto
3874 1.2 lneto <p>
3875 1.2 lneto Type for continuation functions (see <a href="#4.7">§4.7</a>).
3876 1.2 lneto
3877 1.2 lneto
3878 1.2 lneto
3879 1.2 lneto
3880 1.2 lneto
3881 1.2 lneto <hr><h3><a name="lua_len"><code>lua_len</code></a></h3><p>
3882 1.2 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
3883 1.2 lneto <pre>void lua_len (lua_State *L, int index);</pre>
3884 1.1 mbalmer
3885 1.1 mbalmer <p>
3886 1.3 lneto Returns the length of the value at the given index.
3887 1.3 lneto It is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>) and
3888 1.3 lneto may trigger a metamethod for the "length" event (see <a href="#2.4">§2.4</a>).
3889 1.2 lneto The result is pushed on the stack.
3890 1.1 mbalmer
3891 1.1 mbalmer
3892 1.1 mbalmer
3893 1.1 mbalmer
3894 1.1 mbalmer
3895 1.1 mbalmer <hr><h3><a name="lua_load"><code>lua_load</code></a></h3><p>
3896 1.2 lneto <span class="apii">[-0, +1, –]</span>
3897 1.1 mbalmer <pre>int lua_load (lua_State *L,
3898 1.1 mbalmer lua_Reader reader,
3899 1.1 mbalmer void *data,
3900 1.3 lneto const char *chunkname,
3901 1.2 lneto const char *mode);</pre>
3902 1.1 mbalmer
3903 1.1 mbalmer <p>
3904 1.3 lneto Loads a Lua chunk without running it.
3905 1.1 mbalmer If there are no errors,
3906 1.2 lneto <code>lua_load</code> pushes the compiled chunk as a Lua
3907 1.1 mbalmer function on top of the stack.
3908 1.1 mbalmer Otherwise, it pushes an error message.
3909 1.2 lneto
3910 1.2 lneto
3911 1.2 lneto <p>
3912 1.2 lneto The return values of <code>lua_load</code> are:
3913 1.1 mbalmer
3914 1.1 mbalmer <ul>
3915 1.1 mbalmer
3916 1.2 lneto <li><b><a href="#pdf-LUA_OK"><code>LUA_OK</code></a>: </b> no errors;</li>
3917 1.2 lneto
3918 1.2 lneto <li><b><a name="pdf-LUA_ERRSYNTAX"><code>LUA_ERRSYNTAX</code></a>: </b>
3919 1.2 lneto syntax error during precompilation;</li>
3920 1.1 mbalmer
3921 1.2 lneto <li><b><a href="#pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
3922 1.2 lneto memory allocation error;</li>
3923 1.1 mbalmer
3924 1.2 lneto <li><b><a href="#pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
3925 1.2 lneto error while running a <code>__gc</code> metamethod.
3926 1.2 lneto (This error has no relation with the chunk being loaded.
3927 1.2 lneto It is generated by the garbage collector.)
3928 1.2 lneto </li>
3929 1.1 mbalmer
3930 1.1 mbalmer </ul>
3931 1.1 mbalmer
3932 1.1 mbalmer <p>
3933 1.2 lneto The <code>lua_load</code> function uses a user-supplied <code>reader</code> function
3934 1.2 lneto to read the chunk (see <a href="#lua_Reader"><code>lua_Reader</code></a>).
3935 1.2 lneto The <code>data</code> argument is an opaque value passed to the reader function.
3936 1.2 lneto
3937 1.2 lneto
3938 1.2 lneto <p>
3939 1.3 lneto The <code>chunkname</code> argument gives a name to the chunk,
3940 1.2 lneto which is used for error messages and in debug information (see <a href="#4.9">§4.9</a>).
3941 1.1 mbalmer
3942 1.1 mbalmer
3943 1.1 mbalmer <p>
3944 1.2 lneto <code>lua_load</code> automatically detects whether the chunk is text or binary
3945 1.1 mbalmer and loads it accordingly (see program <code>luac</code>).
3946 1.2 lneto The string <code>mode</code> works as in function <a href="#pdf-load"><code>load</code></a>,
3947 1.2 lneto with the addition that
3948 1.2 lneto a <code>NULL</code> value is equivalent to the string "<code>bt</code>".
3949 1.1 mbalmer
3950 1.1 mbalmer
3951 1.1 mbalmer <p>
3952 1.2 lneto <code>lua_load</code> uses the stack internally,
3953 1.2 lneto so the reader function must always leave the stack
3954 1.2 lneto unmodified when returning.
3955 1.1 mbalmer
3956 1.1 mbalmer
3957 1.1 mbalmer <p>
3958 1.3 lneto If the resulting function has upvalues,
3959 1.3 lneto its first upvalue is set to the value of the global environment
3960 1.2 lneto stored at index <code>LUA_RIDX_GLOBALS</code> in the registry (see <a href="#4.5">§4.5</a>).
3961 1.2 lneto When loading main chunks,
3962 1.2 lneto this upvalue will be the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).
3963 1.3 lneto Other upvalues are initialized with <b>nil</b>.
3964 1.1 mbalmer
3965 1.1 mbalmer
3966 1.1 mbalmer
3967 1.1 mbalmer
3968 1.1 mbalmer
3969 1.1 mbalmer <hr><h3><a name="lua_newstate"><code>lua_newstate</code></a></h3><p>
3970 1.2 lneto <span class="apii">[-0, +0, –]</span>
3971 1.1 mbalmer <pre>lua_State *lua_newstate (lua_Alloc f, void *ud);</pre>
3972 1.1 mbalmer
3973 1.1 mbalmer <p>
3974 1.2 lneto Creates a new thread running in a new, independent state.
3975 1.2 lneto Returns <code>NULL</code> if it cannot create the thread or the state
3976 1.1 mbalmer (due to lack of memory).
3977 1.1 mbalmer The argument <code>f</code> is the allocator function;
3978 1.1 mbalmer Lua does all memory allocation for this state through this function.
3979 1.1 mbalmer The second argument, <code>ud</code>, is an opaque pointer that Lua
3980 1.2 lneto passes to the allocator in every call.
3981 1.1 mbalmer
3982 1.1 mbalmer
3983 1.1 mbalmer
3984 1.1 mbalmer
3985 1.1 mbalmer
3986 1.1 mbalmer <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
3987 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
3988 1.1 mbalmer <pre>void lua_newtable (lua_State *L);</pre>
3989 1.1 mbalmer
3990 1.1 mbalmer <p>
3991 1.1 mbalmer Creates a new empty table and pushes it onto the stack.
3992 1.1 mbalmer It is equivalent to <code>lua_createtable(L, 0, 0)</code>.
3993 1.1 mbalmer
3994 1.1 mbalmer
3995 1.1 mbalmer
3996 1.1 mbalmer
3997 1.1 mbalmer
3998 1.1 mbalmer <hr><h3><a name="lua_newthread"><code>lua_newthread</code></a></h3><p>
3999 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4000 1.1 mbalmer <pre>lua_State *lua_newthread (lua_State *L);</pre>
4001 1.1 mbalmer
4002 1.1 mbalmer <p>
4003 1.1 mbalmer Creates a new thread, pushes it on the stack,
4004 1.1 mbalmer and returns a pointer to a <a href="#lua_State"><code>lua_State</code></a> that represents this new thread.
4005 1.2 lneto The new thread returned by this function shares with the original thread
4006 1.2 lneto its global environment,
4007 1.1 mbalmer but has an independent execution stack.
4008 1.1 mbalmer
4009 1.1 mbalmer
4010 1.1 mbalmer <p>
4011 1.1 mbalmer There is no explicit function to close or to destroy a thread.
4012 1.1 mbalmer Threads are subject to garbage collection,
4013 1.1 mbalmer like any Lua object.
4014 1.1 mbalmer
4015 1.1 mbalmer
4016 1.1 mbalmer
4017 1.1 mbalmer
4018 1.1 mbalmer
4019 1.1 mbalmer <hr><h3><a name="lua_newuserdata"><code>lua_newuserdata</code></a></h3><p>
4020 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4021 1.1 mbalmer <pre>void *lua_newuserdata (lua_State *L, size_t size);</pre>
4022 1.1 mbalmer
4023 1.1 mbalmer <p>
4024 1.1 mbalmer This function allocates a new block of memory with the given size,
4025 1.1 mbalmer pushes onto the stack a new full userdata with the block address,
4026 1.1 mbalmer and returns this address.
4027 1.2 lneto The host program can freely use this memory.
4028 1.1 mbalmer
4029 1.1 mbalmer
4030 1.1 mbalmer
4031 1.1 mbalmer
4032 1.1 mbalmer
4033 1.1 mbalmer <hr><h3><a name="lua_next"><code>lua_next</code></a></h3><p>
4034 1.1 mbalmer <span class="apii">[-1, +(2|0), <em>e</em>]</span>
4035 1.1 mbalmer <pre>int lua_next (lua_State *L, int index);</pre>
4036 1.1 mbalmer
4037 1.1 mbalmer <p>
4038 1.1 mbalmer Pops a key from the stack,
4039 1.2 lneto and pushes a key–value pair from the table at the given index
4040 1.1 mbalmer (the "next" pair after the given key).
4041 1.1 mbalmer If there are no more elements in the table,
4042 1.1 mbalmer then <a href="#lua_next"><code>lua_next</code></a> returns 0 (and pushes nothing).
4043 1.1 mbalmer
4044 1.1 mbalmer
4045 1.1 mbalmer <p>
4046 1.1 mbalmer A typical traversal looks like this:
4047 1.1 mbalmer
4048 1.1 mbalmer <pre>
4049 1.1 mbalmer /* table is in the stack at index 't' */
4050 1.1 mbalmer lua_pushnil(L); /* first key */
4051 1.1 mbalmer while (lua_next(L, t) != 0) {
4052 1.1 mbalmer /* uses 'key' (at index -2) and 'value' (at index -1) */
4053 1.1 mbalmer printf("%s - %s\n",
4054 1.1 mbalmer lua_typename(L, lua_type(L, -2)),
4055 1.1 mbalmer lua_typename(L, lua_type(L, -1)));
4056 1.1 mbalmer /* removes 'value'; keeps 'key' for next iteration */
4057 1.1 mbalmer lua_pop(L, 1);
4058 1.1 mbalmer }
4059 1.1 mbalmer </pre>
4060 1.1 mbalmer
4061 1.1 mbalmer <p>
4062 1.1 mbalmer While traversing a table,
4063 1.1 mbalmer do not call <a href="#lua_tolstring"><code>lua_tolstring</code></a> directly on a key,
4064 1.1 mbalmer unless you know that the key is actually a string.
4065 1.2 lneto Recall that <a href="#lua_tolstring"><code>lua_tolstring</code></a> may change
4066 1.1 mbalmer the value at the given index;
4067 1.1 mbalmer this confuses the next call to <a href="#lua_next"><code>lua_next</code></a>.
4068 1.1 mbalmer
4069 1.1 mbalmer
4070 1.2 lneto <p>
4071 1.2 lneto See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
4072 1.2 lneto the table during its traversal.
4073 1.2 lneto
4074 1.2 lneto
4075 1.1 mbalmer
4076 1.1 mbalmer
4077 1.1 mbalmer
4078 1.1 mbalmer <hr><h3><a name="lua_Number"><code>lua_Number</code></a></h3>
4079 1.4 mbalmer <pre>typedef ... lua_Number;</pre>
4080 1.1 mbalmer
4081 1.1 mbalmer <p>
4082 1.2 lneto The type of floats in Lua.
4083 1.1 mbalmer
4084 1.1 mbalmer
4085 1.1 mbalmer <p>
4086 1.2 lneto By default this type is double,
4087 1.4 mbalmer but that can be changed to a single float or a long double.
4088 1.4 mbalmer (See <code>LUA_FLOAT_TYPE</code> in <code>luaconf.h</code>.)
4089 1.2 lneto
4090 1.2 lneto
4091 1.1 mbalmer
4092 1.1 mbalmer
4093 1.1 mbalmer
4094 1.3 lneto <hr><h3><a name="lua_numbertointeger"><code>lua_numbertointeger</code></a></h3>
4095 1.3 lneto <pre>int lua_numbertointeger (lua_Number n, lua_Integer *p);</pre>
4096 1.1 mbalmer
4097 1.2 lneto <p>
4098 1.2 lneto Converts a Lua float to a Lua integer.
4099 1.2 lneto This macro assumes that <code>n</code> has an integral value.
4100 1.2 lneto If that value is within the range of Lua integers,
4101 1.2 lneto it is converted to an integer and assigned to <code>*p</code>.
4102 1.2 lneto The macro results in a boolean indicating whether the
4103 1.2 lneto conversion was successful.
4104 1.2 lneto (Note that this range test can be tricky to do
4105 1.2 lneto correctly without this macro,
4106 1.2 lneto due to roundings.)
4107 1.1 mbalmer
4108 1.1 mbalmer
4109 1.1 mbalmer <p>
4110 1.2 lneto This macro may evaluate its arguments more than once.
4111 1.1 mbalmer
4112 1.1 mbalmer
4113 1.1 mbalmer
4114 1.1 mbalmer
4115 1.1 mbalmer
4116 1.1 mbalmer <hr><h3><a name="lua_pcall"><code>lua_pcall</code></a></h3><p>
4117 1.2 lneto <span class="apii">[-(nargs + 1), +(nresults|1), –]</span>
4118 1.2 lneto <pre>int lua_pcall (lua_State *L, int nargs, int nresults, int msgh);</pre>
4119 1.1 mbalmer
4120 1.1 mbalmer <p>
4121 1.1 mbalmer Calls a function in protected mode.
4122 1.1 mbalmer
4123 1.1 mbalmer
4124 1.1 mbalmer <p>
4125 1.1 mbalmer Both <code>nargs</code> and <code>nresults</code> have the same meaning as
4126 1.1 mbalmer in <a href="#lua_call"><code>lua_call</code></a>.
4127 1.1 mbalmer If there are no errors during the call,
4128 1.1 mbalmer <a href="#lua_pcall"><code>lua_pcall</code></a> behaves exactly like <a href="#lua_call"><code>lua_call</code></a>.
4129 1.1 mbalmer However, if there is any error,
4130 1.1 mbalmer <a href="#lua_pcall"><code>lua_pcall</code></a> catches it,
4131 1.1 mbalmer pushes a single value on the stack (the error message),
4132 1.1 mbalmer and returns an error code.
4133 1.1 mbalmer Like <a href="#lua_call"><code>lua_call</code></a>,
4134 1.1 mbalmer <a href="#lua_pcall"><code>lua_pcall</code></a> always removes the function
4135 1.1 mbalmer and its arguments from the stack.
4136 1.1 mbalmer
4137 1.1 mbalmer
4138 1.1 mbalmer <p>
4139 1.2 lneto If <code>msgh</code> is 0,
4140 1.1 mbalmer then the error message returned on the stack
4141 1.1 mbalmer is exactly the original error message.
4142 1.2 lneto Otherwise, <code>msgh</code> is the stack index of a
4143 1.2 lneto <em>message handler</em>.
4144 1.4 mbalmer (This index cannot be a pseudo-index.)
4145 1.1 mbalmer In case of runtime errors,
4146 1.1 mbalmer this function will be called with the error message
4147 1.2 lneto and its return value will be the message
4148 1.2 lneto returned on the stack by <a href="#lua_pcall"><code>lua_pcall</code></a>.
4149 1.1 mbalmer
4150 1.1 mbalmer
4151 1.1 mbalmer <p>
4152 1.2 lneto Typically, the message handler is used to add more debug
4153 1.1 mbalmer information to the error message, such as a stack traceback.
4154 1.1 mbalmer Such information cannot be gathered after the return of <a href="#lua_pcall"><code>lua_pcall</code></a>,
4155 1.1 mbalmer since by then the stack has unwound.
4156 1.1 mbalmer
4157 1.1 mbalmer
4158 1.1 mbalmer <p>
4159 1.2 lneto The <a href="#lua_pcall"><code>lua_pcall</code></a> function returns one of the following constants
4160 1.1 mbalmer (defined in <code>lua.h</code>):
4161 1.1 mbalmer
4162 1.1 mbalmer <ul>
4163 1.1 mbalmer
4164 1.2 lneto <li><b><a name="pdf-LUA_OK"><code>LUA_OK</code></a> (0): </b>
4165 1.2 lneto success.</li>
4166 1.2 lneto
4167 1.2 lneto <li><b><a name="pdf-LUA_ERRRUN"><code>LUA_ERRRUN</code></a>: </b>
4168 1.1 mbalmer a runtime error.
4169 1.1 mbalmer </li>
4170 1.1 mbalmer
4171 1.2 lneto <li><b><a name="pdf-LUA_ERRMEM"><code>LUA_ERRMEM</code></a>: </b>
4172 1.1 mbalmer memory allocation error.
4173 1.2 lneto For such errors, Lua does not call the message handler.
4174 1.2 lneto </li>
4175 1.2 lneto
4176 1.2 lneto <li><b><a name="pdf-LUA_ERRERR"><code>LUA_ERRERR</code></a>: </b>
4177 1.2 lneto error while running the message handler.
4178 1.1 mbalmer </li>
4179 1.1 mbalmer
4180 1.2 lneto <li><b><a name="pdf-LUA_ERRGCMM"><code>LUA_ERRGCMM</code></a>: </b>
4181 1.2 lneto error while running a <code>__gc</code> metamethod.
4182 1.2 lneto (This error typically has no relation with the function being called.)
4183 1.1 mbalmer </li>
4184 1.1 mbalmer
4185 1.1 mbalmer </ul>
4186 1.1 mbalmer
4187 1.1 mbalmer
4188 1.1 mbalmer
4189 1.1 mbalmer
4190 1.2 lneto <hr><h3><a name="lua_pcallk"><code>lua_pcallk</code></a></h3><p>
4191 1.2 lneto <span class="apii">[-(nargs + 1), +(nresults|1), –]</span>
4192 1.2 lneto <pre>int lua_pcallk (lua_State *L,
4193 1.2 lneto int nargs,
4194 1.2 lneto int nresults,
4195 1.3 lneto int msgh,
4196 1.3 lneto lua_KContext ctx,
4197 1.2 lneto lua_KFunction k);</pre>
4198 1.2 lneto
4199 1.2 lneto <p>
4200 1.2 lneto This function behaves exactly like <a href="#lua_pcall"><code>lua_pcall</code></a>,
4201 1.2 lneto but allows the called function to yield (see <a href="#4.7">§4.7</a>).
4202 1.2 lneto
4203 1.2 lneto
4204 1.2 lneto
4205 1.2 lneto
4206 1.2 lneto
4207 1.1 mbalmer <hr><h3><a name="lua_pop"><code>lua_pop</code></a></h3><p>
4208 1.2 lneto <span class="apii">[-n, +0, –]</span>
4209 1.1 mbalmer <pre>void lua_pop (lua_State *L, int n);</pre>
4210 1.1 mbalmer
4211 1.1 mbalmer <p>
4212 1.1 mbalmer Pops <code>n</code> elements from the stack.
4213 1.1 mbalmer
4214 1.1 mbalmer
4215 1.1 mbalmer
4216 1.1 mbalmer
4217 1.1 mbalmer
4218 1.1 mbalmer <hr><h3><a name="lua_pushboolean"><code>lua_pushboolean</code></a></h3><p>
4219 1.2 lneto <span class="apii">[-0, +1, –]</span>
4220 1.1 mbalmer <pre>void lua_pushboolean (lua_State *L, int b);</pre>
4221 1.1 mbalmer
4222 1.1 mbalmer <p>
4223 1.1 mbalmer Pushes a boolean value with value <code>b</code> onto the stack.
4224 1.1 mbalmer
4225 1.1 mbalmer
4226 1.1 mbalmer
4227 1.1 mbalmer
4228 1.1 mbalmer
4229 1.1 mbalmer <hr><h3><a name="lua_pushcclosure"><code>lua_pushcclosure</code></a></h3><p>
4230 1.5 lneto <span class="apii">[-n, +1, <em>m</em>]</span>
4231 1.1 mbalmer <pre>void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);</pre>
4232 1.1 mbalmer
4233 1.1 mbalmer <p>
4234 1.1 mbalmer Pushes a new C closure onto the stack.
4235 1.1 mbalmer
4236 1.1 mbalmer
4237 1.1 mbalmer <p>
4238 1.1 mbalmer When a C function is created,
4239 1.1 mbalmer it is possible to associate some values with it,
4240 1.2 lneto thus creating a C closure (see <a href="#4.4">§4.4</a>);
4241 1.1 mbalmer these values are then accessible to the function whenever it is called.
4242 1.1 mbalmer To associate values with a C function,
4243 1.2 lneto first these values must be pushed onto the stack
4244 1.1 mbalmer (when there are multiple values, the first value is pushed first).
4245 1.1 mbalmer Then <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a>
4246 1.1 mbalmer is called to create and push the C function onto the stack,
4247 1.2 lneto with the argument <code>n</code> telling how many values will be
4248 1.1 mbalmer associated with the function.
4249 1.1 mbalmer <a href="#lua_pushcclosure"><code>lua_pushcclosure</code></a> also pops these values from the stack.
4250 1.1 mbalmer
4251 1.1 mbalmer
4252 1.1 mbalmer <p>
4253 1.1 mbalmer The maximum value for <code>n</code> is 255.
4254 1.1 mbalmer
4255 1.1 mbalmer
4256 1.2 lneto <p>
4257 1.2 lneto When <code>n</code> is zero,
4258 1.2 lneto this function creates a <em>light C function</em>,
4259 1.2 lneto which is just a pointer to the C function.
4260 1.2 lneto In that case, it never raises a memory error.
4261 1.2 lneto
4262 1.2 lneto
4263 1.1 mbalmer
4264 1.1 mbalmer
4265 1.1 mbalmer
4266 1.1 mbalmer <hr><h3><a name="lua_pushcfunction"><code>lua_pushcfunction</code></a></h3><p>
4267 1.2 lneto <span class="apii">[-0, +1, –]</span>
4268 1.1 mbalmer <pre>void lua_pushcfunction (lua_State *L, lua_CFunction f);</pre>
4269 1.1 mbalmer
4270 1.1 mbalmer <p>
4271 1.1 mbalmer Pushes a C function onto the stack.
4272 1.1 mbalmer This function receives a pointer to a C function
4273 1.1 mbalmer and pushes onto the stack a Lua value of type <code>function</code> that,
4274 1.1 mbalmer when called, invokes the corresponding C function.
4275 1.1 mbalmer
4276 1.1 mbalmer
4277 1.1 mbalmer <p>
4278 1.4 mbalmer Any function to be callable by Lua must
4279 1.1 mbalmer follow the correct protocol to receive its parameters
4280 1.1 mbalmer and return its results (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
4281 1.1 mbalmer
4282 1.1 mbalmer
4283 1.1 mbalmer
4284 1.1 mbalmer
4285 1.1 mbalmer
4286 1.1 mbalmer <hr><h3><a name="lua_pushfstring"><code>lua_pushfstring</code></a></h3><p>
4287 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4288 1.1 mbalmer <pre>const char *lua_pushfstring (lua_State *L, const char *fmt, ...);</pre>
4289 1.1 mbalmer
4290 1.1 mbalmer <p>
4291 1.1 mbalmer Pushes onto the stack a formatted string
4292 1.1 mbalmer and returns a pointer to this string.
4293 1.3 lneto It is similar to the ISO C function <code>sprintf</code>,
4294 1.1 mbalmer but has some important differences:
4295 1.1 mbalmer
4296 1.1 mbalmer <ul>
4297 1.1 mbalmer
4298 1.1 mbalmer <li>
4299 1.1 mbalmer You do not have to allocate space for the result:
4300 1.1 mbalmer the result is a Lua string and Lua takes care of memory allocation
4301 1.1 mbalmer (and deallocation, through garbage collection).
4302 1.1 mbalmer </li>
4303 1.1 mbalmer
4304 1.1 mbalmer <li>
4305 1.1 mbalmer The conversion specifiers are quite restricted.
4306 1.1 mbalmer There are no flags, widths, or precisions.
4307 1.1 mbalmer The conversion specifiers can only be
4308 1.2 lneto '<code>%%</code>' (inserts the character '<code>%</code>'),
4309 1.1 mbalmer '<code>%s</code>' (inserts a zero-terminated string, with no size restrictions),
4310 1.1 mbalmer '<code>%f</code>' (inserts a <a href="#lua_Number"><code>lua_Number</code></a>),
4311 1.4 mbalmer '<code>%I</code>' (inserts a <a href="#lua_Integer"><code>lua_Integer</code></a>),
4312 1.1 mbalmer '<code>%p</code>' (inserts a pointer as a hexadecimal numeral),
4313 1.2 lneto '<code>%d</code>' (inserts an <code>int</code>),
4314 1.2 lneto '<code>%c</code>' (inserts an <code>int</code> as a one-byte character), and
4315 1.3 lneto '<code>%U</code>' (inserts a <code>long int</code> as a UTF-8 byte sequence).
4316 1.1 mbalmer </li>
4317 1.1 mbalmer
4318 1.1 mbalmer </ul>
4319 1.1 mbalmer
4320 1.1 mbalmer
4321 1.1 mbalmer
4322 1.1 mbalmer
4323 1.2 lneto <hr><h3><a name="lua_pushglobaltable"><code>lua_pushglobaltable</code></a></h3><p>
4324 1.2 lneto <span class="apii">[-0, +1, –]</span>
4325 1.2 lneto <pre>void lua_pushglobaltable (lua_State *L);</pre>
4326 1.2 lneto
4327 1.2 lneto <p>
4328 1.2 lneto Pushes the global environment onto the stack.
4329 1.2 lneto
4330 1.2 lneto
4331 1.2 lneto
4332 1.2 lneto
4333 1.2 lneto
4334 1.1 mbalmer <hr><h3><a name="lua_pushinteger"><code>lua_pushinteger</code></a></h3><p>
4335 1.2 lneto <span class="apii">[-0, +1, –]</span>
4336 1.1 mbalmer <pre>void lua_pushinteger (lua_State *L, lua_Integer n);</pre>
4337 1.1 mbalmer
4338 1.1 mbalmer <p>
4339 1.2 lneto Pushes an integer with value <code>n</code> onto the stack.
4340 1.1 mbalmer
4341 1.1 mbalmer
4342 1.1 mbalmer
4343 1.1 mbalmer
4344 1.1 mbalmer
4345 1.1 mbalmer <hr><h3><a name="lua_pushlightuserdata"><code>lua_pushlightuserdata</code></a></h3><p>
4346 1.2 lneto <span class="apii">[-0, +1, –]</span>
4347 1.1 mbalmer <pre>void lua_pushlightuserdata (lua_State *L, void *p);</pre>
4348 1.1 mbalmer
4349 1.1 mbalmer <p>
4350 1.1 mbalmer Pushes a light userdata onto the stack.
4351 1.1 mbalmer
4352 1.1 mbalmer
4353 1.1 mbalmer <p>
4354 1.1 mbalmer Userdata represent C values in Lua.
4355 1.2 lneto A <em>light userdata</em> represents a pointer, a <code>void*</code>.
4356 1.1 mbalmer It is a value (like a number):
4357 1.1 mbalmer you do not create it, it has no individual metatable,
4358 1.1 mbalmer and it is not collected (as it was never created).
4359 1.1 mbalmer A light userdata is equal to "any"
4360 1.1 mbalmer light userdata with the same C address.
4361 1.1 mbalmer
4362 1.1 mbalmer
4363 1.1 mbalmer
4364 1.1 mbalmer
4365 1.1 mbalmer
4366 1.1 mbalmer <hr><h3><a name="lua_pushliteral"><code>lua_pushliteral</code></a></h3><p>
4367 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4368 1.2 lneto <pre>const char *lua_pushliteral (lua_State *L, const char *s);</pre>
4369 1.1 mbalmer
4370 1.1 mbalmer <p>
4371 1.4 mbalmer This macro is equivalent to <a href="#lua_pushstring"><code>lua_pushstring</code></a>,
4372 1.4 mbalmer but should be used only when <code>s</code> is a literal string.
4373 1.1 mbalmer
4374 1.1 mbalmer
4375 1.1 mbalmer
4376 1.1 mbalmer
4377 1.1 mbalmer
4378 1.1 mbalmer <hr><h3><a name="lua_pushlstring"><code>lua_pushlstring</code></a></h3><p>
4379 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4380 1.2 lneto <pre>const char *lua_pushlstring (lua_State *L, const char *s, size_t len);</pre>
4381 1.1 mbalmer
4382 1.1 mbalmer <p>
4383 1.1 mbalmer Pushes the string pointed to by <code>s</code> with size <code>len</code>
4384 1.1 mbalmer onto the stack.
4385 1.1 mbalmer Lua makes (or reuses) an internal copy of the given string,
4386 1.1 mbalmer so the memory at <code>s</code> can be freed or reused immediately after
4387 1.1 mbalmer the function returns.
4388 1.2 lneto The string can contain any binary data,
4389 1.2 lneto including embedded zeros.
4390 1.2 lneto
4391 1.2 lneto
4392 1.2 lneto <p>
4393 1.2 lneto Returns a pointer to the internal copy of the string.
4394 1.1 mbalmer
4395 1.1 mbalmer
4396 1.1 mbalmer
4397 1.1 mbalmer
4398 1.1 mbalmer
4399 1.1 mbalmer <hr><h3><a name="lua_pushnil"><code>lua_pushnil</code></a></h3><p>
4400 1.2 lneto <span class="apii">[-0, +1, –]</span>
4401 1.1 mbalmer <pre>void lua_pushnil (lua_State *L);</pre>
4402 1.1 mbalmer
4403 1.1 mbalmer <p>
4404 1.1 mbalmer Pushes a nil value onto the stack.
4405 1.1 mbalmer
4406 1.1 mbalmer
4407 1.1 mbalmer
4408 1.1 mbalmer
4409 1.1 mbalmer
4410 1.1 mbalmer <hr><h3><a name="lua_pushnumber"><code>lua_pushnumber</code></a></h3><p>
4411 1.2 lneto <span class="apii">[-0, +1, –]</span>
4412 1.1 mbalmer <pre>void lua_pushnumber (lua_State *L, lua_Number n);</pre>
4413 1.1 mbalmer
4414 1.1 mbalmer <p>
4415 1.2 lneto Pushes a float with value <code>n</code> onto the stack.
4416 1.1 mbalmer
4417 1.1 mbalmer
4418 1.1 mbalmer
4419 1.1 mbalmer
4420 1.1 mbalmer
4421 1.1 mbalmer <hr><h3><a name="lua_pushstring"><code>lua_pushstring</code></a></h3><p>
4422 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4423 1.2 lneto <pre>const char *lua_pushstring (lua_State *L, const char *s);</pre>
4424 1.1 mbalmer
4425 1.1 mbalmer <p>
4426 1.1 mbalmer Pushes the zero-terminated string pointed to by <code>s</code>
4427 1.1 mbalmer onto the stack.
4428 1.1 mbalmer Lua makes (or reuses) an internal copy of the given string,
4429 1.1 mbalmer so the memory at <code>s</code> can be freed or reused immediately after
4430 1.1 mbalmer the function returns.
4431 1.2 lneto
4432 1.2 lneto
4433 1.2 lneto <p>
4434 1.2 lneto Returns a pointer to the internal copy of the string.
4435 1.2 lneto
4436 1.2 lneto
4437 1.2 lneto <p>
4438 1.2 lneto If <code>s</code> is <code>NULL</code>, pushes <b>nil</b> and returns <code>NULL</code>.
4439 1.1 mbalmer
4440 1.1 mbalmer
4441 1.1 mbalmer
4442 1.1 mbalmer
4443 1.1 mbalmer
4444 1.1 mbalmer <hr><h3><a name="lua_pushthread"><code>lua_pushthread</code></a></h3><p>
4445 1.2 lneto <span class="apii">[-0, +1, –]</span>
4446 1.1 mbalmer <pre>int lua_pushthread (lua_State *L);</pre>
4447 1.1 mbalmer
4448 1.1 mbalmer <p>
4449 1.1 mbalmer Pushes the thread represented by <code>L</code> onto the stack.
4450 1.1 mbalmer Returns 1 if this thread is the main thread of its state.
4451 1.1 mbalmer
4452 1.1 mbalmer
4453 1.1 mbalmer
4454 1.1 mbalmer
4455 1.1 mbalmer
4456 1.1 mbalmer <hr><h3><a name="lua_pushvalue"><code>lua_pushvalue</code></a></h3><p>
4457 1.2 lneto <span class="apii">[-0, +1, –]</span>
4458 1.1 mbalmer <pre>void lua_pushvalue (lua_State *L, int index);</pre>
4459 1.1 mbalmer
4460 1.1 mbalmer <p>
4461 1.2 lneto Pushes a copy of the element at the given index
4462 1.1 mbalmer onto the stack.
4463 1.1 mbalmer
4464 1.1 mbalmer
4465 1.1 mbalmer
4466 1.1 mbalmer
4467 1.1 mbalmer
4468 1.1 mbalmer <hr><h3><a name="lua_pushvfstring"><code>lua_pushvfstring</code></a></h3><p>
4469 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
4470 1.1 mbalmer <pre>const char *lua_pushvfstring (lua_State *L,
4471 1.1 mbalmer const char *fmt,
4472 1.1 mbalmer va_list argp);</pre>
4473 1.1 mbalmer
4474 1.1 mbalmer <p>
4475 1.1 mbalmer Equivalent to <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>, except that it receives a <code>va_list</code>
4476 1.1 mbalmer instead of a variable number of arguments.
4477 1.1 mbalmer
4478 1.1 mbalmer
4479 1.1 mbalmer
4480 1.1 mbalmer
4481 1.1 mbalmer
4482 1.1 mbalmer <hr><h3><a name="lua_rawequal"><code>lua_rawequal</code></a></h3><p>
4483 1.2 lneto <span class="apii">[-0, +0, –]</span>
4484 1.1 mbalmer <pre>int lua_rawequal (lua_State *L, int index1, int index2);</pre>
4485 1.1 mbalmer
4486 1.1 mbalmer <p>
4487 1.2 lneto Returns 1 if the two values in indices <code>index1</code> and
4488 1.1 mbalmer <code>index2</code> are primitively equal
4489 1.1 mbalmer (that is, without calling metamethods).
4490 1.1 mbalmer Otherwise returns 0.
4491 1.3 lneto Also returns 0 if any of the indices are not valid.
4492 1.1 mbalmer
4493 1.1 mbalmer
4494 1.1 mbalmer
4495 1.1 mbalmer
4496 1.1 mbalmer
4497 1.1 mbalmer <hr><h3><a name="lua_rawget"><code>lua_rawget</code></a></h3><p>
4498 1.2 lneto <span class="apii">[-1, +1, –]</span>
4499 1.2 lneto <pre>int lua_rawget (lua_State *L, int index);</pre>
4500 1.1 mbalmer
4501 1.1 mbalmer <p>
4502 1.1 mbalmer Similar to <a href="#lua_gettable"><code>lua_gettable</code></a>, but does a raw access
4503 1.1 mbalmer (i.e., without metamethods).
4504 1.1 mbalmer
4505 1.1 mbalmer
4506 1.1 mbalmer
4507 1.1 mbalmer
4508 1.1 mbalmer
4509 1.2 lneto <hr><h3><a name="lua_rawgeti"><code>lua_rawgeti</code></a></h3><p>
4510 1.2 lneto <span class="apii">[-0, +1, –]</span>
4511 1.2 lneto <pre>int lua_rawgeti (lua_State *L, int index, lua_Integer n);</pre>
4512 1.2 lneto
4513 1.2 lneto <p>
4514 1.2 lneto Pushes onto the stack the value <code>t[n]</code>,
4515 1.2 lneto where <code>t</code> is the table at the given index.
4516 1.2 lneto The access is raw;
4517 1.2 lneto that is, it does not invoke metamethods.
4518 1.2 lneto
4519 1.2 lneto
4520 1.2 lneto <p>
4521 1.2 lneto Returns the type of the pushed value.
4522 1.2 lneto
4523 1.2 lneto
4524 1.2 lneto
4525 1.2 lneto
4526 1.2 lneto
4527 1.2 lneto <hr><h3><a name="lua_rawgetp"><code>lua_rawgetp</code></a></h3><p>
4528 1.2 lneto <span class="apii">[-0, +1, –]</span>
4529 1.2 lneto <pre>int lua_rawgetp (lua_State *L, int index, const void *p);</pre>
4530 1.2 lneto
4531 1.2 lneto <p>
4532 1.2 lneto Pushes onto the stack the value <code>t[k]</code>,
4533 1.2 lneto where <code>t</code> is the table at the given index and
4534 1.2 lneto <code>k</code> is the pointer <code>p</code> represented as a light userdata.
4535 1.2 lneto The access is raw;
4536 1.2 lneto that is, it does not invoke metamethods.
4537 1.2 lneto
4538 1.2 lneto
4539 1.2 lneto <p>
4540 1.2 lneto Returns the type of the pushed value.
4541 1.2 lneto
4542 1.2 lneto
4543 1.2 lneto
4544 1.2 lneto
4545 1.2 lneto
4546 1.2 lneto <hr><h3><a name="lua_rawlen"><code>lua_rawlen</code></a></h3><p>
4547 1.2 lneto <span class="apii">[-0, +0, –]</span>
4548 1.2 lneto <pre>size_t lua_rawlen (lua_State *L, int index);</pre>
4549 1.2 lneto
4550 1.2 lneto <p>
4551 1.2 lneto Returns the raw "length" of the value at the given index:
4552 1.2 lneto for strings, this is the string length;
4553 1.2 lneto for tables, this is the result of the length operator ('<code>#</code>')
4554 1.2 lneto with no metamethods;
4555 1.2 lneto for userdata, this is the size of the block of memory allocated
4556 1.2 lneto for the userdata;
4557 1.2 lneto for other values, it is 0.
4558 1.1 mbalmer
4559 1.1 mbalmer
4560 1.1 mbalmer
4561 1.1 mbalmer
4562 1.1 mbalmer
4563 1.1 mbalmer <hr><h3><a name="lua_rawset"><code>lua_rawset</code></a></h3><p>
4564 1.5 lneto <span class="apii">[-2, +0, <em>m</em>]</span>
4565 1.1 mbalmer <pre>void lua_rawset (lua_State *L, int index);</pre>
4566 1.1 mbalmer
4567 1.1 mbalmer <p>
4568 1.1 mbalmer Similar to <a href="#lua_settable"><code>lua_settable</code></a>, but does a raw assignment
4569 1.1 mbalmer (i.e., without metamethods).
4570 1.1 mbalmer
4571 1.1 mbalmer
4572 1.1 mbalmer
4573 1.1 mbalmer
4574 1.1 mbalmer
4575 1.1 mbalmer <hr><h3><a name="lua_rawseti"><code>lua_rawseti</code></a></h3><p>
4576 1.5 lneto <span class="apii">[-1, +0, <em>m</em>]</span>
4577 1.3 lneto <pre>void lua_rawseti (lua_State *L, int index, lua_Integer i);</pre>
4578 1.1 mbalmer
4579 1.1 mbalmer <p>
4580 1.3 lneto Does the equivalent of <code>t[i] = v</code>,
4581 1.2 lneto where <code>t</code> is the table at the given index
4582 1.2 lneto and <code>v</code> is the value at the top of the stack.
4583 1.2 lneto
4584 1.2 lneto
4585 1.2 lneto <p>
4586 1.2 lneto This function pops the value from the stack.
4587 1.2 lneto The assignment is raw;
4588 1.2 lneto that is, it does not invoke metamethods.
4589 1.2 lneto
4590 1.2 lneto
4591 1.2 lneto
4592 1.2 lneto
4593 1.2 lneto
4594 1.2 lneto <hr><h3><a name="lua_rawsetp"><code>lua_rawsetp</code></a></h3><p>
4595 1.5 lneto <span class="apii">[-1, +0, <em>m</em>]</span>
4596 1.2 lneto <pre>void lua_rawsetp (lua_State *L, int index, const void *p);</pre>
4597 1.2 lneto
4598 1.2 lneto <p>
4599 1.4 mbalmer Does the equivalent of <code>t[p] = v</code>,
4600 1.2 lneto where <code>t</code> is the table at the given index,
4601 1.4 mbalmer <code>p</code> is encoded as a light userdata,
4602 1.1 mbalmer and <code>v</code> is the value at the top of the stack.
4603 1.1 mbalmer
4604 1.1 mbalmer
4605 1.1 mbalmer <p>
4606 1.1 mbalmer This function pops the value from the stack.
4607 1.1 mbalmer The assignment is raw;
4608 1.1 mbalmer that is, it does not invoke metamethods.
4609 1.1 mbalmer
4610 1.1 mbalmer
4611 1.1 mbalmer
4612 1.1 mbalmer
4613 1.1 mbalmer
4614 1.1 mbalmer <hr><h3><a name="lua_Reader"><code>lua_Reader</code></a></h3>
4615 1.1 mbalmer <pre>typedef const char * (*lua_Reader) (lua_State *L,
4616 1.1 mbalmer void *data,
4617 1.1 mbalmer size_t *size);</pre>
4618 1.1 mbalmer
4619 1.1 mbalmer <p>
4620 1.1 mbalmer The reader function used by <a href="#lua_load"><code>lua_load</code></a>.
4621 1.1 mbalmer Every time it needs another piece of the chunk,
4622 1.1 mbalmer <a href="#lua_load"><code>lua_load</code></a> calls the reader,
4623 1.1 mbalmer passing along its <code>data</code> parameter.
4624 1.1 mbalmer The reader must return a pointer to a block of memory
4625 1.1 mbalmer with a new piece of the chunk
4626 1.1 mbalmer and set <code>size</code> to the block size.
4627 1.1 mbalmer The block must exist until the reader function is called again.
4628 1.1 mbalmer To signal the end of the chunk,
4629 1.1 mbalmer the reader must return <code>NULL</code> or set <code>size</code> to zero.
4630 1.1 mbalmer The reader function may return pieces of any size greater than zero.
4631 1.1 mbalmer
4632 1.1 mbalmer
4633 1.1 mbalmer
4634 1.1 mbalmer
4635 1.1 mbalmer
4636 1.1 mbalmer <hr><h3><a name="lua_register"><code>lua_register</code></a></h3><p>
4637 1.1 mbalmer <span class="apii">[-0, +0, <em>e</em>]</span>
4638 1.2 lneto <pre>void lua_register (lua_State *L, const char *name, lua_CFunction f);</pre>
4639 1.1 mbalmer
4640 1.1 mbalmer <p>
4641 1.1 mbalmer Sets the C function <code>f</code> as the new value of global <code>name</code>.
4642 1.1 mbalmer It is defined as a macro:
4643 1.1 mbalmer
4644 1.1 mbalmer <pre>
4645 1.1 mbalmer #define lua_register(L,n,f) \
4646 1.1 mbalmer (lua_pushcfunction(L, f), lua_setglobal(L, n))
4647 1.1 mbalmer </pre>
4648 1.1 mbalmer
4649 1.1 mbalmer
4650 1.1 mbalmer
4651 1.1 mbalmer
4652 1.1 mbalmer <hr><h3><a name="lua_remove"><code>lua_remove</code></a></h3><p>
4653 1.2 lneto <span class="apii">[-1, +0, –]</span>
4654 1.1 mbalmer <pre>void lua_remove (lua_State *L, int index);</pre>
4655 1.1 mbalmer
4656 1.1 mbalmer <p>
4657 1.1 mbalmer Removes the element at the given valid index,
4658 1.1 mbalmer shifting down the elements above this index to fill the gap.
4659 1.2 lneto This function cannot be called with a pseudo-index,
4660 1.1 mbalmer because a pseudo-index is not an actual stack position.
4661 1.1 mbalmer
4662 1.1 mbalmer
4663 1.1 mbalmer
4664 1.1 mbalmer
4665 1.1 mbalmer
4666 1.1 mbalmer <hr><h3><a name="lua_replace"><code>lua_replace</code></a></h3><p>
4667 1.2 lneto <span class="apii">[-1, +0, –]</span>
4668 1.1 mbalmer <pre>void lua_replace (lua_State *L, int index);</pre>
4669 1.1 mbalmer
4670 1.1 mbalmer <p>
4671 1.2 lneto Moves the top element into the given valid index
4672 1.1 mbalmer without shifting any element
4673 1.4 mbalmer (therefore replacing the value at that given index),
4674 1.2 lneto and then pops the top element.
4675 1.1 mbalmer
4676 1.1 mbalmer
4677 1.1 mbalmer
4678 1.1 mbalmer
4679 1.1 mbalmer
4680 1.1 mbalmer <hr><h3><a name="lua_resume"><code>lua_resume</code></a></h3><p>
4681 1.2 lneto <span class="apii">[-?, +?, –]</span>
4682 1.2 lneto <pre>int lua_resume (lua_State *L, lua_State *from, int nargs);</pre>
4683 1.1 mbalmer
4684 1.1 mbalmer <p>
4685 1.4 mbalmer Starts and resumes a coroutine in the given thread <code>L</code>.
4686 1.1 mbalmer
4687 1.1 mbalmer
4688 1.1 mbalmer <p>
4689 1.2 lneto To start a coroutine,
4690 1.2 lneto you push onto the thread stack the main function plus any arguments;
4691 1.1 mbalmer then you call <a href="#lua_resume"><code>lua_resume</code></a>,
4692 1.2 lneto with <code>nargs</code> being the number of arguments.
4693 1.1 mbalmer This call returns when the coroutine suspends or finishes its execution.
4694 1.1 mbalmer When it returns, the stack contains all values passed to <a href="#lua_yield"><code>lua_yield</code></a>,
4695 1.1 mbalmer or all values returned by the body function.
4696 1.1 mbalmer <a href="#lua_resume"><code>lua_resume</code></a> returns
4697 1.1 mbalmer <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the coroutine yields,
4698 1.2 lneto <a href="#pdf-LUA_OK"><code>LUA_OK</code></a> if the coroutine finishes its execution
4699 1.1 mbalmer without errors,
4700 1.1 mbalmer or an error code in case of errors (see <a href="#lua_pcall"><code>lua_pcall</code></a>).
4701 1.2 lneto
4702 1.2 lneto
4703 1.2 lneto <p>
4704 1.1 mbalmer In case of errors,
4705 1.1 mbalmer the stack is not unwound,
4706 1.1 mbalmer so you can use the debug API over it.
4707 1.1 mbalmer The error message is on the top of the stack.
4708 1.2 lneto
4709 1.2 lneto
4710 1.2 lneto <p>
4711 1.2 lneto To resume a coroutine,
4712 1.2 lneto you remove any results from the last <a href="#lua_yield"><code>lua_yield</code></a>,
4713 1.2 lneto put on its stack only the values to
4714 1.1 mbalmer be passed as results from <code>yield</code>,
4715 1.1 mbalmer and then call <a href="#lua_resume"><code>lua_resume</code></a>.
4716 1.1 mbalmer
4717 1.1 mbalmer
4718 1.2 lneto <p>
4719 1.2 lneto The parameter <code>from</code> represents the coroutine that is resuming <code>L</code>.
4720 1.2 lneto If there is no such coroutine,
4721 1.2 lneto this parameter can be <code>NULL</code>.
4722 1.2 lneto
4723 1.2 lneto
4724 1.1 mbalmer
4725 1.1 mbalmer
4726 1.1 mbalmer
4727 1.2 lneto <hr><h3><a name="lua_rotate"><code>lua_rotate</code></a></h3><p>
4728 1.2 lneto <span class="apii">[-0, +0, –]</span>
4729 1.2 lneto <pre>void lua_rotate (lua_State *L, int idx, int n);</pre>
4730 1.1 mbalmer
4731 1.1 mbalmer <p>
4732 1.4 mbalmer Rotates the stack elements between the valid index <code>idx</code>
4733 1.4 mbalmer and the top of the stack.
4734 1.4 mbalmer The elements are rotated <code>n</code> positions in the direction of the top,
4735 1.4 mbalmer for a positive <code>n</code>,
4736 1.2 lneto or <code>-n</code> positions in the direction of the bottom,
4737 1.2 lneto for a negative <code>n</code>.
4738 1.2 lneto The absolute value of <code>n</code> must not be greater than the size
4739 1.2 lneto of the slice being rotated.
4740 1.4 mbalmer This function cannot be called with a pseudo-index,
4741 1.4 mbalmer because a pseudo-index is not an actual stack position.
4742 1.1 mbalmer
4743 1.1 mbalmer
4744 1.1 mbalmer
4745 1.1 mbalmer
4746 1.1 mbalmer
4747 1.2 lneto <hr><h3><a name="lua_setallocf"><code>lua_setallocf</code></a></h3><p>
4748 1.2 lneto <span class="apii">[-0, +0, –]</span>
4749 1.2 lneto <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
4750 1.1 mbalmer
4751 1.1 mbalmer <p>
4752 1.2 lneto Changes the allocator function of a given state to <code>f</code>
4753 1.2 lneto with user data <code>ud</code>.
4754 1.1 mbalmer
4755 1.1 mbalmer
4756 1.1 mbalmer
4757 1.1 mbalmer
4758 1.1 mbalmer
4759 1.1 mbalmer <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
4760 1.1 mbalmer <span class="apii">[-1, +0, <em>e</em>]</span>
4761 1.1 mbalmer <pre>void lua_setfield (lua_State *L, int index, const char *k);</pre>
4762 1.1 mbalmer
4763 1.1 mbalmer <p>
4764 1.1 mbalmer Does the equivalent to <code>t[k] = v</code>,
4765 1.2 lneto where <code>t</code> is the value at the given index
4766 1.1 mbalmer and <code>v</code> is the value at the top of the stack.
4767 1.1 mbalmer
4768 1.1 mbalmer
4769 1.1 mbalmer <p>
4770 1.1 mbalmer This function pops the value from the stack.
4771 1.1 mbalmer As in Lua, this function may trigger a metamethod
4772 1.2 lneto for the "newindex" event (see <a href="#2.4">§2.4</a>).
4773 1.1 mbalmer
4774 1.1 mbalmer
4775 1.1 mbalmer
4776 1.1 mbalmer
4777 1.1 mbalmer
4778 1.1 mbalmer <hr><h3><a name="lua_setglobal"><code>lua_setglobal</code></a></h3><p>
4779 1.1 mbalmer <span class="apii">[-1, +0, <em>e</em>]</span>
4780 1.1 mbalmer <pre>void lua_setglobal (lua_State *L, const char *name);</pre>
4781 1.1 mbalmer
4782 1.1 mbalmer <p>
4783 1.1 mbalmer Pops a value from the stack and
4784 1.1 mbalmer sets it as the new value of global <code>name</code>.
4785 1.1 mbalmer
4786 1.1 mbalmer
4787 1.1 mbalmer
4788 1.1 mbalmer
4789 1.1 mbalmer
4790 1.3 lneto <hr><h3><a name="lua_seti"><code>lua_seti</code></a></h3><p>
4791 1.3 lneto <span class="apii">[-1, +0, <em>e</em>]</span>
4792 1.3 lneto <pre>void lua_seti (lua_State *L, int index, lua_Integer n);</pre>
4793 1.3 lneto
4794 1.3 lneto <p>
4795 1.3 lneto Does the equivalent to <code>t[n] = v</code>,
4796 1.3 lneto where <code>t</code> is the value at the given index
4797 1.3 lneto and <code>v</code> is the value at the top of the stack.
4798 1.3 lneto
4799 1.3 lneto
4800 1.3 lneto <p>
4801 1.3 lneto This function pops the value from the stack.
4802 1.3 lneto As in Lua, this function may trigger a metamethod
4803 1.3 lneto for the "newindex" event (see <a href="#2.4">§2.4</a>).
4804 1.3 lneto
4805 1.3 lneto
4806 1.3 lneto
4807 1.3 lneto
4808 1.3 lneto
4809 1.1 mbalmer <hr><h3><a name="lua_setmetatable"><code>lua_setmetatable</code></a></h3><p>
4810 1.2 lneto <span class="apii">[-1, +0, –]</span>
4811 1.2 lneto <pre>void lua_setmetatable (lua_State *L, int index);</pre>
4812 1.1 mbalmer
4813 1.1 mbalmer <p>
4814 1.1 mbalmer Pops a table from the stack and
4815 1.2 lneto sets it as the new metatable for the value at the given index.
4816 1.1 mbalmer
4817 1.1 mbalmer
4818 1.1 mbalmer
4819 1.1 mbalmer
4820 1.1 mbalmer
4821 1.1 mbalmer <hr><h3><a name="lua_settable"><code>lua_settable</code></a></h3><p>
4822 1.1 mbalmer <span class="apii">[-2, +0, <em>e</em>]</span>
4823 1.1 mbalmer <pre>void lua_settable (lua_State *L, int index);</pre>
4824 1.1 mbalmer
4825 1.1 mbalmer <p>
4826 1.1 mbalmer Does the equivalent to <code>t[k] = v</code>,
4827 1.2 lneto where <code>t</code> is the value at the given index,
4828 1.1 mbalmer <code>v</code> is the value at the top of the stack,
4829 1.1 mbalmer and <code>k</code> is the value just below the top.
4830 1.1 mbalmer
4831 1.1 mbalmer
4832 1.1 mbalmer <p>
4833 1.1 mbalmer This function pops both the key and the value from the stack.
4834 1.1 mbalmer As in Lua, this function may trigger a metamethod
4835 1.2 lneto for the "newindex" event (see <a href="#2.4">§2.4</a>).
4836 1.1 mbalmer
4837 1.1 mbalmer
4838 1.1 mbalmer
4839 1.1 mbalmer
4840 1.1 mbalmer
4841 1.1 mbalmer <hr><h3><a name="lua_settop"><code>lua_settop</code></a></h3><p>
4842 1.2 lneto <span class="apii">[-?, +?, –]</span>
4843 1.1 mbalmer <pre>void lua_settop (lua_State *L, int index);</pre>
4844 1.1 mbalmer
4845 1.1 mbalmer <p>
4846 1.2 lneto Accepts any index, or 0,
4847 1.1 mbalmer and sets the stack top to this index.
4848 1.1 mbalmer If the new top is larger than the old one,
4849 1.1 mbalmer then the new elements are filled with <b>nil</b>.
4850 1.1 mbalmer If <code>index</code> is 0, then all stack elements are removed.
4851 1.1 mbalmer
4852 1.1 mbalmer
4853 1.1 mbalmer
4854 1.1 mbalmer
4855 1.1 mbalmer
4856 1.2 lneto <hr><h3><a name="lua_setuservalue"><code>lua_setuservalue</code></a></h3><p>
4857 1.2 lneto <span class="apii">[-1, +0, –]</span>
4858 1.2 lneto <pre>void lua_setuservalue (lua_State *L, int index);</pre>
4859 1.2 lneto
4860 1.2 lneto <p>
4861 1.2 lneto Pops a value from the stack and sets it as
4862 1.2 lneto the new value associated to the userdata at the given index.
4863 1.2 lneto
4864 1.2 lneto
4865 1.2 lneto
4866 1.2 lneto
4867 1.2 lneto
4868 1.1 mbalmer <hr><h3><a name="lua_State"><code>lua_State</code></a></h3>
4869 1.1 mbalmer <pre>typedef struct lua_State lua_State;</pre>
4870 1.1 mbalmer
4871 1.1 mbalmer <p>
4872 1.2 lneto An opaque structure that points to a thread and indirectly
4873 1.2 lneto (through the thread) to the whole state of a Lua interpreter.
4874 1.1 mbalmer The Lua library is fully reentrant:
4875 1.1 mbalmer it has no global variables.
4876 1.2 lneto All information about a state is accessible through this structure.
4877 1.1 mbalmer
4878 1.1 mbalmer
4879 1.1 mbalmer <p>
4880 1.2 lneto A pointer to this structure must be passed as the first argument to
4881 1.1 mbalmer every function in the library, except to <a href="#lua_newstate"><code>lua_newstate</code></a>,
4882 1.1 mbalmer which creates a Lua state from scratch.
4883 1.1 mbalmer
4884 1.1 mbalmer
4885 1.1 mbalmer
4886 1.1 mbalmer
4887 1.1 mbalmer
4888 1.1 mbalmer <hr><h3><a name="lua_status"><code>lua_status</code></a></h3><p>
4889 1.2 lneto <span class="apii">[-0, +0, –]</span>
4890 1.1 mbalmer <pre>int lua_status (lua_State *L);</pre>
4891 1.1 mbalmer
4892 1.1 mbalmer <p>
4893 1.1 mbalmer Returns the status of the thread <code>L</code>.
4894 1.1 mbalmer
4895 1.1 mbalmer
4896 1.1 mbalmer <p>
4897 1.2 lneto The status can be 0 (<a href="#pdf-LUA_OK"><code>LUA_OK</code></a>) for a normal thread,
4898 1.2 lneto an error code if the thread finished the execution
4899 1.2 lneto of a <a href="#lua_resume"><code>lua_resume</code></a> with an error,
4900 1.1 mbalmer or <a name="pdf-LUA_YIELD"><code>LUA_YIELD</code></a> if the thread is suspended.
4901 1.1 mbalmer
4902 1.1 mbalmer
4903 1.2 lneto <p>
4904 1.2 lneto You can only call functions in threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>.
4905 1.2 lneto You can resume threads with status <a href="#pdf-LUA_OK"><code>LUA_OK</code></a>
4906 1.2 lneto (to start a new coroutine) or <a href="#pdf-LUA_YIELD"><code>LUA_YIELD</code></a>
4907 1.2 lneto (to resume a coroutine).
4908 1.2 lneto
4909 1.2 lneto
4910 1.2 lneto
4911 1.2 lneto
4912 1.2 lneto
4913 1.3 lneto <hr><h3><a name="lua_stringtonumber"><code>lua_stringtonumber</code></a></h3><p>
4914 1.2 lneto <span class="apii">[-0, +1, –]</span>
4915 1.3 lneto <pre>size_t lua_stringtonumber (lua_State *L, const char *s);</pre>
4916 1.2 lneto
4917 1.2 lneto <p>
4918 1.2 lneto Converts the zero-terminated string <code>s</code> to a number,
4919 1.2 lneto pushes that number into the stack,
4920 1.3 lneto and returns the total size of the string,
4921 1.3 lneto that is, its length plus one.
4922 1.2 lneto The conversion can result in an integer or a float,
4923 1.2 lneto according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>).
4924 1.2 lneto The string may have leading and trailing spaces and a sign.
4925 1.2 lneto If the string is not a valid numeral,
4926 1.2 lneto returns 0 and pushes nothing.
4927 1.3 lneto (Note that the result can be used as a boolean,
4928 1.3 lneto true if the conversion succeeds.)
4929 1.2 lneto
4930 1.2 lneto
4931 1.1 mbalmer
4932 1.1 mbalmer
4933 1.1 mbalmer
4934 1.1 mbalmer <hr><h3><a name="lua_toboolean"><code>lua_toboolean</code></a></h3><p>
4935 1.2 lneto <span class="apii">[-0, +0, –]</span>
4936 1.1 mbalmer <pre>int lua_toboolean (lua_State *L, int index);</pre>
4937 1.1 mbalmer
4938 1.1 mbalmer <p>
4939 1.2 lneto Converts the Lua value at the given index to a C boolean
4940 1.1 mbalmer value (0 or 1).
4941 1.1 mbalmer Like all tests in Lua,
4942 1.2 lneto <a href="#lua_toboolean"><code>lua_toboolean</code></a> returns true for any Lua value
4943 1.1 mbalmer different from <b>false</b> and <b>nil</b>;
4944 1.2 lneto otherwise it returns false.
4945 1.1 mbalmer (If you want to accept only actual boolean values,
4946 1.1 mbalmer use <a href="#lua_isboolean"><code>lua_isboolean</code></a> to test the value's type.)
4947 1.1 mbalmer
4948 1.1 mbalmer
4949 1.1 mbalmer
4950 1.1 mbalmer
4951 1.1 mbalmer
4952 1.1 mbalmer <hr><h3><a name="lua_tocfunction"><code>lua_tocfunction</code></a></h3><p>
4953 1.2 lneto <span class="apii">[-0, +0, –]</span>
4954 1.1 mbalmer <pre>lua_CFunction lua_tocfunction (lua_State *L, int index);</pre>
4955 1.1 mbalmer
4956 1.1 mbalmer <p>
4957 1.2 lneto Converts a value at the given index to a C function.
4958 1.1 mbalmer That value must be a C function;
4959 1.1 mbalmer otherwise, returns <code>NULL</code>.
4960 1.1 mbalmer
4961 1.1 mbalmer
4962 1.1 mbalmer
4963 1.1 mbalmer
4964 1.1 mbalmer
4965 1.1 mbalmer <hr><h3><a name="lua_tointeger"><code>lua_tointeger</code></a></h3><p>
4966 1.2 lneto <span class="apii">[-0, +0, –]</span>
4967 1.1 mbalmer <pre>lua_Integer lua_tointeger (lua_State *L, int index);</pre>
4968 1.1 mbalmer
4969 1.1 mbalmer <p>
4970 1.2 lneto Equivalent to <a href="#lua_tointegerx"><code>lua_tointegerx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
4971 1.2 lneto
4972 1.2 lneto
4973 1.2 lneto
4974 1.2 lneto
4975 1.2 lneto
4976 1.2 lneto <hr><h3><a name="lua_tointegerx"><code>lua_tointegerx</code></a></h3><p>
4977 1.2 lneto <span class="apii">[-0, +0, –]</span>
4978 1.2 lneto <pre>lua_Integer lua_tointegerx (lua_State *L, int index, int *isnum);</pre>
4979 1.2 lneto
4980 1.2 lneto <p>
4981 1.2 lneto Converts the Lua value at the given index
4982 1.1 mbalmer to the signed integral type <a href="#lua_Integer"><code>lua_Integer</code></a>.
4983 1.2 lneto The Lua value must be an integer,
4984 1.2 lneto or a number or string convertible to an integer (see <a href="#3.4.3">§3.4.3</a>);
4985 1.2 lneto otherwise, <code>lua_tointegerx</code> returns 0.
4986 1.1 mbalmer
4987 1.1 mbalmer
4988 1.1 mbalmer <p>
4989 1.2 lneto If <code>isnum</code> is not <code>NULL</code>,
4990 1.2 lneto its referent is assigned a boolean value that
4991 1.2 lneto indicates whether the operation succeeded.
4992 1.1 mbalmer
4993 1.1 mbalmer
4994 1.1 mbalmer
4995 1.1 mbalmer
4996 1.1 mbalmer
4997 1.1 mbalmer <hr><h3><a name="lua_tolstring"><code>lua_tolstring</code></a></h3><p>
4998 1.5 lneto <span class="apii">[-0, +0, <em>m</em>]</span>
4999 1.1 mbalmer <pre>const char *lua_tolstring (lua_State *L, int index, size_t *len);</pre>
5000 1.1 mbalmer
5001 1.1 mbalmer <p>
5002 1.2 lneto Converts the Lua value at the given index to a C string.
5003 1.1 mbalmer If <code>len</code> is not <code>NULL</code>,
5004 1.5 lneto it sets <code>*len</code> with the string length.
5005 1.1 mbalmer The Lua value must be a string or a number;
5006 1.1 mbalmer otherwise, the function returns <code>NULL</code>.
5007 1.1 mbalmer If the value is a number,
5008 1.2 lneto then <code>lua_tolstring</code> also
5009 1.1 mbalmer <em>changes the actual value in the stack to a string</em>.
5010 1.1 mbalmer (This change confuses <a href="#lua_next"><code>lua_next</code></a>
5011 1.2 lneto when <code>lua_tolstring</code> is applied to keys during a table traversal.)
5012 1.1 mbalmer
5013 1.1 mbalmer
5014 1.1 mbalmer <p>
5015 1.5 lneto <code>lua_tolstring</code> returns a pointer
5016 1.1 mbalmer to a string inside the Lua state.
5017 1.1 mbalmer This string always has a zero ('<code>\0</code>')
5018 1.1 mbalmer after its last character (as in C),
5019 1.1 mbalmer but can contain other zeros in its body.
5020 1.3 lneto
5021 1.3 lneto
5022 1.3 lneto <p>
5023 1.1 mbalmer Because Lua has garbage collection,
5024 1.2 lneto there is no guarantee that the pointer returned by <code>lua_tolstring</code>
5025 1.3 lneto will be valid after the corresponding Lua value is removed from the stack.
5026 1.1 mbalmer
5027 1.1 mbalmer
5028 1.1 mbalmer
5029 1.1 mbalmer
5030 1.1 mbalmer
5031 1.1 mbalmer <hr><h3><a name="lua_tonumber"><code>lua_tonumber</code></a></h3><p>
5032 1.2 lneto <span class="apii">[-0, +0, –]</span>
5033 1.1 mbalmer <pre>lua_Number lua_tonumber (lua_State *L, int index);</pre>
5034 1.1 mbalmer
5035 1.1 mbalmer <p>
5036 1.2 lneto Equivalent to <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> with <code>isnum</code> equal to <code>NULL</code>.
5037 1.2 lneto
5038 1.2 lneto
5039 1.2 lneto
5040 1.2 lneto
5041 1.2 lneto
5042 1.2 lneto <hr><h3><a name="lua_tonumberx"><code>lua_tonumberx</code></a></h3><p>
5043 1.2 lneto <span class="apii">[-0, +0, –]</span>
5044 1.2 lneto <pre>lua_Number lua_tonumberx (lua_State *L, int index, int *isnum);</pre>
5045 1.2 lneto
5046 1.2 lneto <p>
5047 1.2 lneto Converts the Lua value at the given index
5048 1.1 mbalmer to the C type <a href="#lua_Number"><code>lua_Number</code></a> (see <a href="#lua_Number"><code>lua_Number</code></a>).
5049 1.1 mbalmer The Lua value must be a number or a string convertible to a number
5050 1.2 lneto (see <a href="#3.4.3">§3.4.3</a>);
5051 1.2 lneto otherwise, <a href="#lua_tonumberx"><code>lua_tonumberx</code></a> returns 0.
5052 1.2 lneto
5053 1.2 lneto
5054 1.2 lneto <p>
5055 1.2 lneto If <code>isnum</code> is not <code>NULL</code>,
5056 1.2 lneto its referent is assigned a boolean value that
5057 1.2 lneto indicates whether the operation succeeded.
5058 1.1 mbalmer
5059 1.1 mbalmer
5060 1.1 mbalmer
5061 1.1 mbalmer
5062 1.1 mbalmer
5063 1.1 mbalmer <hr><h3><a name="lua_topointer"><code>lua_topointer</code></a></h3><p>
5064 1.2 lneto <span class="apii">[-0, +0, –]</span>
5065 1.1 mbalmer <pre>const void *lua_topointer (lua_State *L, int index);</pre>
5066 1.1 mbalmer
5067 1.1 mbalmer <p>
5068 1.2 lneto Converts the value at the given index to a generic
5069 1.1 mbalmer C pointer (<code>void*</code>).
5070 1.1 mbalmer The value can be a userdata, a table, a thread, or a function;
5071 1.2 lneto otherwise, <code>lua_topointer</code> returns <code>NULL</code>.
5072 1.1 mbalmer Different objects will give different pointers.
5073 1.1 mbalmer There is no way to convert the pointer back to its original value.
5074 1.1 mbalmer
5075 1.1 mbalmer
5076 1.1 mbalmer <p>
5077 1.4 mbalmer Typically this function is used only for hashing and debug information.
5078 1.1 mbalmer
5079 1.1 mbalmer
5080 1.1 mbalmer
5081 1.1 mbalmer
5082 1.1 mbalmer
5083 1.1 mbalmer <hr><h3><a name="lua_tostring"><code>lua_tostring</code></a></h3><p>
5084 1.5 lneto <span class="apii">[-0, +0, <em>m</em>]</span>
5085 1.1 mbalmer <pre>const char *lua_tostring (lua_State *L, int index);</pre>
5086 1.1 mbalmer
5087 1.1 mbalmer <p>
5088 1.1 mbalmer Equivalent to <a href="#lua_tolstring"><code>lua_tolstring</code></a> with <code>len</code> equal to <code>NULL</code>.
5089 1.1 mbalmer
5090 1.1 mbalmer
5091 1.1 mbalmer
5092 1.1 mbalmer
5093 1.1 mbalmer
5094 1.1 mbalmer <hr><h3><a name="lua_tothread"><code>lua_tothread</code></a></h3><p>
5095 1.2 lneto <span class="apii">[-0, +0, –]</span>
5096 1.1 mbalmer <pre>lua_State *lua_tothread (lua_State *L, int index);</pre>
5097 1.1 mbalmer
5098 1.1 mbalmer <p>
5099 1.2 lneto Converts the value at the given index to a Lua thread
5100 1.1 mbalmer (represented as <code>lua_State*</code>).
5101 1.1 mbalmer This value must be a thread;
5102 1.1 mbalmer otherwise, the function returns <code>NULL</code>.
5103 1.1 mbalmer
5104 1.1 mbalmer
5105 1.1 mbalmer
5106 1.1 mbalmer
5107 1.1 mbalmer
5108 1.1 mbalmer <hr><h3><a name="lua_touserdata"><code>lua_touserdata</code></a></h3><p>
5109 1.2 lneto <span class="apii">[-0, +0, –]</span>
5110 1.1 mbalmer <pre>void *lua_touserdata (lua_State *L, int index);</pre>
5111 1.1 mbalmer
5112 1.1 mbalmer <p>
5113 1.2 lneto If the value at the given index is a full userdata,
5114 1.1 mbalmer returns its block address.
5115 1.1 mbalmer If the value is a light userdata,
5116 1.1 mbalmer returns its pointer.
5117 1.1 mbalmer Otherwise, returns <code>NULL</code>.
5118 1.1 mbalmer
5119 1.1 mbalmer
5120 1.1 mbalmer
5121 1.1 mbalmer
5122 1.1 mbalmer
5123 1.1 mbalmer <hr><h3><a name="lua_type"><code>lua_type</code></a></h3><p>
5124 1.2 lneto <span class="apii">[-0, +0, –]</span>
5125 1.1 mbalmer <pre>int lua_type (lua_State *L, int index);</pre>
5126 1.1 mbalmer
5127 1.1 mbalmer <p>
5128 1.2 lneto Returns the type of the value in the given valid index,
5129 1.2 lneto or <code>LUA_TNONE</code> for a non-valid (but acceptable) index.
5130 1.1 mbalmer The types returned by <a href="#lua_type"><code>lua_type</code></a> are coded by the following constants
5131 1.1 mbalmer defined in <code>lua.h</code>:
5132 1.4 mbalmer <a name="pdf-LUA_TNIL"><code>LUA_TNIL</code></a> (0),
5133 1.2 lneto <a name="pdf-LUA_TNUMBER"><code>LUA_TNUMBER</code></a>,
5134 1.2 lneto <a name="pdf-LUA_TBOOLEAN"><code>LUA_TBOOLEAN</code></a>,
5135 1.2 lneto <a name="pdf-LUA_TSTRING"><code>LUA_TSTRING</code></a>,
5136 1.2 lneto <a name="pdf-LUA_TTABLE"><code>LUA_TTABLE</code></a>,
5137 1.2 lneto <a name="pdf-LUA_TFUNCTION"><code>LUA_TFUNCTION</code></a>,
5138 1.2 lneto <a name="pdf-LUA_TUSERDATA"><code>LUA_TUSERDATA</code></a>,
5139 1.2 lneto <a name="pdf-LUA_TTHREAD"><code>LUA_TTHREAD</code></a>,
5140 1.1 mbalmer and
5141 1.2 lneto <a name="pdf-LUA_TLIGHTUSERDATA"><code>LUA_TLIGHTUSERDATA</code></a>.
5142 1.1 mbalmer
5143 1.1 mbalmer
5144 1.1 mbalmer
5145 1.1 mbalmer
5146 1.1 mbalmer
5147 1.1 mbalmer <hr><h3><a name="lua_typename"><code>lua_typename</code></a></h3><p>
5148 1.2 lneto <span class="apii">[-0, +0, –]</span>
5149 1.2 lneto <pre>const char *lua_typename (lua_State *L, int tp);</pre>
5150 1.1 mbalmer
5151 1.1 mbalmer <p>
5152 1.1 mbalmer Returns the name of the type encoded by the value <code>tp</code>,
5153 1.1 mbalmer which must be one the values returned by <a href="#lua_type"><code>lua_type</code></a>.
5154 1.1 mbalmer
5155 1.1 mbalmer
5156 1.1 mbalmer
5157 1.1 mbalmer
5158 1.1 mbalmer
5159 1.2 lneto <hr><h3><a name="lua_Unsigned"><code>lua_Unsigned</code></a></h3>
5160 1.2 lneto <pre>typedef ... lua_Unsigned;</pre>
5161 1.2 lneto
5162 1.2 lneto <p>
5163 1.2 lneto The unsigned version of <a href="#lua_Integer"><code>lua_Integer</code></a>.
5164 1.2 lneto
5165 1.2 lneto
5166 1.2 lneto
5167 1.2 lneto
5168 1.2 lneto
5169 1.2 lneto <hr><h3><a name="lua_upvalueindex"><code>lua_upvalueindex</code></a></h3><p>
5170 1.2 lneto <span class="apii">[-0, +0, –]</span>
5171 1.2 lneto <pre>int lua_upvalueindex (int i);</pre>
5172 1.2 lneto
5173 1.2 lneto <p>
5174 1.2 lneto Returns the pseudo-index that represents the <code>i</code>-th upvalue of
5175 1.2 lneto the running function (see <a href="#4.4">§4.4</a>).
5176 1.2 lneto
5177 1.2 lneto
5178 1.2 lneto
5179 1.2 lneto
5180 1.2 lneto
5181 1.2 lneto <hr><h3><a name="lua_version"><code>lua_version</code></a></h3><p>
5182 1.2 lneto <span class="apii">[-0, +0, <em>v</em>]</span>
5183 1.2 lneto <pre>const lua_Number *lua_version (lua_State *L);</pre>
5184 1.2 lneto
5185 1.2 lneto <p>
5186 1.2 lneto Returns the address of the version number stored in the Lua core.
5187 1.2 lneto When called with a valid <a href="#lua_State"><code>lua_State</code></a>,
5188 1.2 lneto returns the address of the version used to create that state.
5189 1.2 lneto When called with <code>NULL</code>,
5190 1.2 lneto returns the address of the version running the call.
5191 1.2 lneto
5192 1.2 lneto
5193 1.2 lneto
5194 1.2 lneto
5195 1.2 lneto
5196 1.1 mbalmer <hr><h3><a name="lua_Writer"><code>lua_Writer</code></a></h3>
5197 1.1 mbalmer <pre>typedef int (*lua_Writer) (lua_State *L,
5198 1.1 mbalmer const void* p,
5199 1.1 mbalmer size_t sz,
5200 1.1 mbalmer void* ud);</pre>
5201 1.1 mbalmer
5202 1.1 mbalmer <p>
5203 1.1 mbalmer The type of the writer function used by <a href="#lua_dump"><code>lua_dump</code></a>.
5204 1.1 mbalmer Every time it produces another piece of chunk,
5205 1.1 mbalmer <a href="#lua_dump"><code>lua_dump</code></a> calls the writer,
5206 1.1 mbalmer passing along the buffer to be written (<code>p</code>),
5207 1.1 mbalmer its size (<code>sz</code>),
5208 1.1 mbalmer and the <code>data</code> parameter supplied to <a href="#lua_dump"><code>lua_dump</code></a>.
5209 1.1 mbalmer
5210 1.1 mbalmer
5211 1.1 mbalmer <p>
5212 1.1 mbalmer The writer returns an error code:
5213 1.1 mbalmer 0 means no errors;
5214 1.1 mbalmer any other value means an error and stops <a href="#lua_dump"><code>lua_dump</code></a> from
5215 1.1 mbalmer calling the writer again.
5216 1.1 mbalmer
5217 1.1 mbalmer
5218 1.1 mbalmer
5219 1.1 mbalmer
5220 1.1 mbalmer
5221 1.1 mbalmer <hr><h3><a name="lua_xmove"><code>lua_xmove</code></a></h3><p>
5222 1.2 lneto <span class="apii">[-?, +?, –]</span>
5223 1.1 mbalmer <pre>void lua_xmove (lua_State *from, lua_State *to, int n);</pre>
5224 1.1 mbalmer
5225 1.1 mbalmer <p>
5226 1.2 lneto Exchange values between different threads of the same state.
5227 1.1 mbalmer
5228 1.1 mbalmer
5229 1.1 mbalmer <p>
5230 1.1 mbalmer This function pops <code>n</code> values from the stack <code>from</code>,
5231 1.1 mbalmer and pushes them onto the stack <code>to</code>.
5232 1.1 mbalmer
5233 1.1 mbalmer
5234 1.1 mbalmer
5235 1.1 mbalmer
5236 1.1 mbalmer
5237 1.1 mbalmer <hr><h3><a name="lua_yield"><code>lua_yield</code></a></h3><p>
5238 1.3 lneto <span class="apii">[-?, +?, <em>e</em>]</span>
5239 1.2 lneto <pre>int lua_yield (lua_State *L, int nresults);</pre>
5240 1.2 lneto
5241 1.2 lneto <p>
5242 1.2 lneto This function is equivalent to <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5243 1.2 lneto but it has no continuation (see <a href="#4.7">§4.7</a>).
5244 1.2 lneto Therefore, when the thread resumes,
5245 1.3 lneto it continues the function that called
5246 1.2 lneto the function calling <code>lua_yield</code>.
5247 1.2 lneto
5248 1.2 lneto
5249 1.2 lneto
5250 1.2 lneto
5251 1.2 lneto
5252 1.2 lneto <hr><h3><a name="lua_yieldk"><code>lua_yieldk</code></a></h3><p>
5253 1.3 lneto <span class="apii">[-?, +?, <em>e</em>]</span>
5254 1.3 lneto <pre>int lua_yieldk (lua_State *L,
5255 1.3 lneto int nresults,
5256 1.3 lneto lua_KContext ctx,
5257 1.3 lneto lua_KFunction k);</pre>
5258 1.1 mbalmer
5259 1.1 mbalmer <p>
5260 1.3 lneto Yields a coroutine (thread).
5261 1.1 mbalmer
5262 1.1 mbalmer
5263 1.1 mbalmer <p>
5264 1.3 lneto When a C function calls <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5265 1.1 mbalmer the running coroutine suspends its execution,
5266 1.1 mbalmer and the call to <a href="#lua_resume"><code>lua_resume</code></a> that started this coroutine returns.
5267 1.1 mbalmer The parameter <code>nresults</code> is the number of values from the stack
5268 1.2 lneto that will be passed as results to <a href="#lua_resume"><code>lua_resume</code></a>.
5269 1.2 lneto
5270 1.2 lneto
5271 1.2 lneto <p>
5272 1.2 lneto When the coroutine is resumed again,
5273 1.2 lneto Lua calls the given continuation function <code>k</code> to continue
5274 1.2 lneto the execution of the C function that yielded (see <a href="#4.7">§4.7</a>).
5275 1.2 lneto This continuation function receives the same stack
5276 1.2 lneto from the previous function,
5277 1.2 lneto with the <code>n</code> results removed and
5278 1.2 lneto replaced by the arguments passed to <a href="#lua_resume"><code>lua_resume</code></a>.
5279 1.2 lneto Moreover,
5280 1.2 lneto the continuation function receives the value <code>ctx</code>
5281 1.2 lneto that was passed to <a href="#lua_yieldk"><code>lua_yieldk</code></a>.
5282 1.1 mbalmer
5283 1.1 mbalmer
5284 1.3 lneto <p>
5285 1.3 lneto Usually, this function does not return;
5286 1.3 lneto when the coroutine eventually resumes,
5287 1.3 lneto it continues executing the continuation function.
5288 1.3 lneto However, there is one special case,
5289 1.3 lneto which is when this function is called
5290 1.3 lneto from inside a line hook (see <a href="#4.9">§4.9</a>).
5291 1.3 lneto In that case, <code>lua_yieldk</code> should be called with no continuation
5292 1.3 lneto (probably in the form of <a href="#lua_yield"><code>lua_yield</code></a>),
5293 1.3 lneto and the hook should return immediately after the call.
5294 1.3 lneto Lua will yield and,
5295 1.3 lneto when the coroutine resumes again,
5296 1.3 lneto it will continue the normal execution
5297 1.3 lneto of the (Lua) function that triggered the hook.
5298 1.3 lneto
5299 1.3 lneto
5300 1.3 lneto <p>
5301 1.3 lneto This function can raise an error if it is called from a thread
5302 1.3 lneto with a pending C call with no continuation function,
5303 1.3 lneto or it is called from a thread that is not running inside a resume
5304 1.3 lneto (e.g., the main thread).
5305 1.3 lneto
5306 1.3 lneto
5307 1.1 mbalmer
5308 1.1 mbalmer
5309 1.1 mbalmer
5310 1.1 mbalmer
5311 1.1 mbalmer
5312 1.2 lneto <h2>4.9 – <a name="4.9">The Debug Interface</a></h2>
5313 1.1 mbalmer
5314 1.1 mbalmer <p>
5315 1.1 mbalmer Lua has no built-in debugging facilities.
5316 1.1 mbalmer Instead, it offers a special interface
5317 1.1 mbalmer by means of functions and <em>hooks</em>.
5318 1.1 mbalmer This interface allows the construction of different
5319 1.1 mbalmer kinds of debuggers, profilers, and other tools
5320 1.1 mbalmer that need "inside information" from the interpreter.
5321 1.1 mbalmer
5322 1.1 mbalmer
5323 1.1 mbalmer
5324 1.1 mbalmer <hr><h3><a name="lua_Debug"><code>lua_Debug</code></a></h3>
5325 1.1 mbalmer <pre>typedef struct lua_Debug {
5326 1.1 mbalmer int event;
5327 1.1 mbalmer const char *name; /* (n) */
5328 1.1 mbalmer const char *namewhat; /* (n) */
5329 1.1 mbalmer const char *what; /* (S) */
5330 1.1 mbalmer const char *source; /* (S) */
5331 1.1 mbalmer int currentline; /* (l) */
5332 1.1 mbalmer int linedefined; /* (S) */
5333 1.1 mbalmer int lastlinedefined; /* (S) */
5334 1.2 lneto unsigned char nups; /* (u) number of upvalues */
5335 1.2 lneto unsigned char nparams; /* (u) number of parameters */
5336 1.2 lneto char isvararg; /* (u) */
5337 1.2 lneto char istailcall; /* (t) */
5338 1.1 mbalmer char short_src[LUA_IDSIZE]; /* (S) */
5339 1.1 mbalmer /* private part */
5340 1.1 mbalmer <em>other fields</em>
5341 1.1 mbalmer } lua_Debug;</pre>
5342 1.1 mbalmer
5343 1.1 mbalmer <p>
5344 1.1 mbalmer A structure used to carry different pieces of
5345 1.2 lneto information about a function or an activation record.
5346 1.1 mbalmer <a href="#lua_getstack"><code>lua_getstack</code></a> fills only the private part
5347 1.1 mbalmer of this structure, for later use.
5348 1.1 mbalmer To fill the other fields of <a href="#lua_Debug"><code>lua_Debug</code></a> with useful information,
5349 1.1 mbalmer call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5350 1.1 mbalmer
5351 1.1 mbalmer
5352 1.1 mbalmer <p>
5353 1.1 mbalmer The fields of <a href="#lua_Debug"><code>lua_Debug</code></a> have the following meaning:
5354 1.1 mbalmer
5355 1.1 mbalmer <ul>
5356 1.1 mbalmer
5357 1.2 lneto <li><b><code>source</code>: </b>
5358 1.3 lneto the name of the chunk that created the function.
5359 1.2 lneto If <code>source</code> starts with a '<code>@</code>',
5360 1.2 lneto it means that the function was defined in a file where
5361 1.2 lneto the file name follows the '<code>@</code>'.
5362 1.2 lneto If <code>source</code> starts with a '<code>=</code>',
5363 1.2 lneto the remainder of its contents describe the source in a user-dependent manner.
5364 1.2 lneto Otherwise,
5365 1.2 lneto the function was defined in a string where
5366 1.2 lneto <code>source</code> is that string.
5367 1.1 mbalmer </li>
5368 1.1 mbalmer
5369 1.2 lneto <li><b><code>short_src</code>: </b>
5370 1.1 mbalmer a "printable" version of <code>source</code>, to be used in error messages.
5371 1.1 mbalmer </li>
5372 1.1 mbalmer
5373 1.2 lneto <li><b><code>linedefined</code>: </b>
5374 1.1 mbalmer the line number where the definition of the function starts.
5375 1.1 mbalmer </li>
5376 1.1 mbalmer
5377 1.2 lneto <li><b><code>lastlinedefined</code>: </b>
5378 1.1 mbalmer the line number where the definition of the function ends.
5379 1.1 mbalmer </li>
5380 1.1 mbalmer
5381 1.2 lneto <li><b><code>what</code>: </b>
5382 1.1 mbalmer the string <code>"Lua"</code> if the function is a Lua function,
5383 1.1 mbalmer <code>"C"</code> if it is a C function,
5384 1.2 lneto <code>"main"</code> if it is the main part of a chunk.
5385 1.1 mbalmer </li>
5386 1.1 mbalmer
5387 1.2 lneto <li><b><code>currentline</code>: </b>
5388 1.1 mbalmer the current line where the given function is executing.
5389 1.1 mbalmer When no line information is available,
5390 1.1 mbalmer <code>currentline</code> is set to -1.
5391 1.1 mbalmer </li>
5392 1.1 mbalmer
5393 1.2 lneto <li><b><code>name</code>: </b>
5394 1.1 mbalmer a reasonable name for the given function.
5395 1.1 mbalmer Because functions in Lua are first-class values,
5396 1.1 mbalmer they do not have a fixed name:
5397 1.1 mbalmer some functions can be the value of multiple global variables,
5398 1.1 mbalmer while others can be stored only in a table field.
5399 1.1 mbalmer The <code>lua_getinfo</code> function checks how the function was
5400 1.1 mbalmer called to find a suitable name.
5401 1.1 mbalmer If it cannot find a name,
5402 1.1 mbalmer then <code>name</code> is set to <code>NULL</code>.
5403 1.1 mbalmer </li>
5404 1.1 mbalmer
5405 1.2 lneto <li><b><code>namewhat</code>: </b>
5406 1.1 mbalmer explains the <code>name</code> field.
5407 1.1 mbalmer The value of <code>namewhat</code> can be
5408 1.1 mbalmer <code>"global"</code>, <code>"local"</code>, <code>"method"</code>,
5409 1.1 mbalmer <code>"field"</code>, <code>"upvalue"</code>, or <code>""</code> (the empty string),
5410 1.1 mbalmer according to how the function was called.
5411 1.1 mbalmer (Lua uses the empty string when no other option seems to apply.)
5412 1.1 mbalmer </li>
5413 1.1 mbalmer
5414 1.2 lneto <li><b><code>istailcall</code>: </b>
5415 1.2 lneto true if this function invocation was called by a tail call.
5416 1.2 lneto In this case, the caller of this level is not in the stack.
5417 1.2 lneto </li>
5418 1.2 lneto
5419 1.2 lneto <li><b><code>nups</code>: </b>
5420 1.1 mbalmer the number of upvalues of the function.
5421 1.1 mbalmer </li>
5422 1.1 mbalmer
5423 1.2 lneto <li><b><code>nparams</code>: </b>
5424 1.2 lneto the number of fixed parameters of the function
5425 1.2 lneto (always 0 for C functions).
5426 1.2 lneto </li>
5427 1.2 lneto
5428 1.2 lneto <li><b><code>isvararg</code>: </b>
5429 1.2 lneto true if the function is a vararg function
5430 1.2 lneto (always true for C functions).
5431 1.2 lneto </li>
5432 1.2 lneto
5433 1.1 mbalmer </ul>
5434 1.1 mbalmer
5435 1.1 mbalmer
5436 1.1 mbalmer
5437 1.1 mbalmer
5438 1.1 mbalmer <hr><h3><a name="lua_gethook"><code>lua_gethook</code></a></h3><p>
5439 1.2 lneto <span class="apii">[-0, +0, –]</span>
5440 1.1 mbalmer <pre>lua_Hook lua_gethook (lua_State *L);</pre>
5441 1.1 mbalmer
5442 1.1 mbalmer <p>
5443 1.1 mbalmer Returns the current hook function.
5444 1.1 mbalmer
5445 1.1 mbalmer
5446 1.1 mbalmer
5447 1.1 mbalmer
5448 1.1 mbalmer
5449 1.1 mbalmer <hr><h3><a name="lua_gethookcount"><code>lua_gethookcount</code></a></h3><p>
5450 1.2 lneto <span class="apii">[-0, +0, –]</span>
5451 1.1 mbalmer <pre>int lua_gethookcount (lua_State *L);</pre>
5452 1.1 mbalmer
5453 1.1 mbalmer <p>
5454 1.1 mbalmer Returns the current hook count.
5455 1.1 mbalmer
5456 1.1 mbalmer
5457 1.1 mbalmer
5458 1.1 mbalmer
5459 1.1 mbalmer
5460 1.1 mbalmer <hr><h3><a name="lua_gethookmask"><code>lua_gethookmask</code></a></h3><p>
5461 1.2 lneto <span class="apii">[-0, +0, –]</span>
5462 1.1 mbalmer <pre>int lua_gethookmask (lua_State *L);</pre>
5463 1.1 mbalmer
5464 1.1 mbalmer <p>
5465 1.1 mbalmer Returns the current hook mask.
5466 1.1 mbalmer
5467 1.1 mbalmer
5468 1.1 mbalmer
5469 1.1 mbalmer
5470 1.1 mbalmer
5471 1.1 mbalmer <hr><h3><a name="lua_getinfo"><code>lua_getinfo</code></a></h3><p>
5472 1.2 lneto <span class="apii">[-(0|1), +(0|1|2), <em>e</em>]</span>
5473 1.1 mbalmer <pre>int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);</pre>
5474 1.1 mbalmer
5475 1.1 mbalmer <p>
5476 1.2 lneto Gets information about a specific function or function invocation.
5477 1.1 mbalmer
5478 1.1 mbalmer
5479 1.1 mbalmer <p>
5480 1.1 mbalmer To get information about a function invocation,
5481 1.1 mbalmer the parameter <code>ar</code> must be a valid activation record that was
5482 1.1 mbalmer filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5483 1.1 mbalmer given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5484 1.1 mbalmer
5485 1.1 mbalmer
5486 1.1 mbalmer <p>
5487 1.1 mbalmer To get information about a function you push it onto the stack
5488 1.1 mbalmer and start the <code>what</code> string with the character '<code>></code>'.
5489 1.1 mbalmer (In that case,
5490 1.2 lneto <code>lua_getinfo</code> pops the function from the top of the stack.)
5491 1.1 mbalmer For instance, to know in which line a function <code>f</code> was defined,
5492 1.1 mbalmer you can write the following code:
5493 1.1 mbalmer
5494 1.1 mbalmer <pre>
5495 1.1 mbalmer lua_Debug ar;
5496 1.2 lneto lua_getglobal(L, "f"); /* get global 'f' */
5497 1.1 mbalmer lua_getinfo(L, ">S", &ar);
5498 1.1 mbalmer printf("%d\n", ar.linedefined);
5499 1.1 mbalmer </pre>
5500 1.1 mbalmer
5501 1.1 mbalmer <p>
5502 1.1 mbalmer Each character in the string <code>what</code>
5503 1.1 mbalmer selects some fields of the structure <code>ar</code> to be filled or
5504 1.1 mbalmer a value to be pushed on the stack:
5505 1.1 mbalmer
5506 1.1 mbalmer <ul>
5507 1.1 mbalmer
5508 1.2 lneto <li><b>'<code>n</code>': </b> fills in the field <code>name</code> and <code>namewhat</code>;
5509 1.1 mbalmer </li>
5510 1.1 mbalmer
5511 1.2 lneto <li><b>'<code>S</code>': </b>
5512 1.1 mbalmer fills in the fields <code>source</code>, <code>short_src</code>,
5513 1.1 mbalmer <code>linedefined</code>, <code>lastlinedefined</code>, and <code>what</code>;
5514 1.1 mbalmer </li>
5515 1.1 mbalmer
5516 1.2 lneto <li><b>'<code>l</code>': </b> fills in the field <code>currentline</code>;
5517 1.2 lneto </li>
5518 1.2 lneto
5519 1.2 lneto <li><b>'<code>t</code>': </b> fills in the field <code>istailcall</code>;
5520 1.1 mbalmer </li>
5521 1.1 mbalmer
5522 1.2 lneto <li><b>'<code>u</code>': </b> fills in the fields
5523 1.2 lneto <code>nups</code>, <code>nparams</code>, and <code>isvararg</code>;
5524 1.1 mbalmer </li>
5525 1.1 mbalmer
5526 1.2 lneto <li><b>'<code>f</code>': </b>
5527 1.1 mbalmer pushes onto the stack the function that is
5528 1.1 mbalmer running at the given level;
5529 1.1 mbalmer </li>
5530 1.1 mbalmer
5531 1.2 lneto <li><b>'<code>L</code>': </b>
5532 1.1 mbalmer pushes onto the stack a table whose indices are the
5533 1.1 mbalmer numbers of the lines that are valid on the function.
5534 1.1 mbalmer (A <em>valid line</em> is a line with some associated code,
5535 1.1 mbalmer that is, a line where you can put a break point.
5536 1.1 mbalmer Non-valid lines include empty lines and comments.)
5537 1.2 lneto
5538 1.2 lneto
5539 1.2 lneto <p>
5540 1.2 lneto If this option is given together with option '<code>f</code>',
5541 1.2 lneto its table is pushed after the function.
5542 1.1 mbalmer </li>
5543 1.1 mbalmer
5544 1.1 mbalmer </ul>
5545 1.1 mbalmer
5546 1.1 mbalmer <p>
5547 1.1 mbalmer This function returns 0 on error
5548 1.1 mbalmer (for instance, an invalid option in <code>what</code>).
5549 1.1 mbalmer
5550 1.1 mbalmer
5551 1.1 mbalmer
5552 1.1 mbalmer
5553 1.1 mbalmer
5554 1.1 mbalmer <hr><h3><a name="lua_getlocal"><code>lua_getlocal</code></a></h3><p>
5555 1.2 lneto <span class="apii">[-0, +(0|1), –]</span>
5556 1.3 lneto <pre>const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
5557 1.1 mbalmer
5558 1.1 mbalmer <p>
5559 1.2 lneto Gets information about a local variable of
5560 1.2 lneto a given activation record or a given function.
5561 1.2 lneto
5562 1.2 lneto
5563 1.2 lneto <p>
5564 1.2 lneto In the first case,
5565 1.2 lneto the parameter <code>ar</code> must be a valid activation record that was
5566 1.1 mbalmer filled by a previous call to <a href="#lua_getstack"><code>lua_getstack</code></a> or
5567 1.1 mbalmer given as argument to a hook (see <a href="#lua_Hook"><code>lua_Hook</code></a>).
5568 1.2 lneto The index <code>n</code> selects which local variable to inspect;
5569 1.2 lneto see <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for details about variable indices
5570 1.2 lneto and names.
5571 1.2 lneto
5572 1.2 lneto
5573 1.2 lneto <p>
5574 1.1 mbalmer <a href="#lua_getlocal"><code>lua_getlocal</code></a> pushes the variable's value onto the stack
5575 1.1 mbalmer and returns its name.
5576 1.1 mbalmer
5577 1.1 mbalmer
5578 1.1 mbalmer <p>
5579 1.2 lneto In the second case, <code>ar</code> must be <code>NULL</code> and the function
5580 1.2 lneto to be inspected must be at the top of the stack.
5581 1.2 lneto In this case, only parameters of Lua functions are visible
5582 1.2 lneto (as there is no information about what variables are active)
5583 1.2 lneto and no values are pushed onto the stack.
5584 1.1 mbalmer
5585 1.1 mbalmer
5586 1.1 mbalmer <p>
5587 1.1 mbalmer Returns <code>NULL</code> (and pushes nothing)
5588 1.1 mbalmer when the index is greater than
5589 1.1 mbalmer the number of active local variables.
5590 1.1 mbalmer
5591 1.1 mbalmer
5592 1.1 mbalmer
5593 1.1 mbalmer
5594 1.1 mbalmer
5595 1.1 mbalmer <hr><h3><a name="lua_getstack"><code>lua_getstack</code></a></h3><p>
5596 1.2 lneto <span class="apii">[-0, +0, –]</span>
5597 1.1 mbalmer <pre>int lua_getstack (lua_State *L, int level, lua_Debug *ar);</pre>
5598 1.1 mbalmer
5599 1.1 mbalmer <p>
5600 1.2 lneto Gets information about the interpreter runtime stack.
5601 1.1 mbalmer
5602 1.1 mbalmer
5603 1.1 mbalmer <p>
5604 1.1 mbalmer This function fills parts of a <a href="#lua_Debug"><code>lua_Debug</code></a> structure with
5605 1.1 mbalmer an identification of the <em>activation record</em>
5606 1.1 mbalmer of the function executing at a given level.
5607 1.1 mbalmer Level 0 is the current running function,
5608 1.2 lneto whereas level <em>n+1</em> is the function that has called level <em>n</em>
5609 1.2 lneto (except for tail calls, which do not count on the stack).
5610 1.1 mbalmer When there are no errors, <a href="#lua_getstack"><code>lua_getstack</code></a> returns 1;
5611 1.1 mbalmer when called with a level greater than the stack depth,
5612 1.1 mbalmer it returns 0.
5613 1.1 mbalmer
5614 1.1 mbalmer
5615 1.1 mbalmer
5616 1.1 mbalmer
5617 1.1 mbalmer
5618 1.1 mbalmer <hr><h3><a name="lua_getupvalue"><code>lua_getupvalue</code></a></h3><p>
5619 1.2 lneto <span class="apii">[-0, +(0|1), –]</span>
5620 1.1 mbalmer <pre>const char *lua_getupvalue (lua_State *L, int funcindex, int n);</pre>
5621 1.1 mbalmer
5622 1.1 mbalmer <p>
5623 1.4 mbalmer Gets information about the <code>n</code>-th upvalue
5624 1.4 mbalmer of the closure at index <code>funcindex</code>.
5625 1.4 mbalmer It pushes the upvalue's value onto the stack
5626 1.4 mbalmer and returns its name.
5627 1.4 mbalmer Returns <code>NULL</code> (and pushes nothing)
5628 1.4 mbalmer when the index <code>n</code> is greater than the number of upvalues.
5629 1.4 mbalmer
5630 1.4 mbalmer
5631 1.4 mbalmer <p>
5632 1.4 mbalmer For C functions, this function uses the empty string <code>""</code>
5633 1.4 mbalmer as a name for all upvalues.
5634 1.1 mbalmer (For Lua functions,
5635 1.1 mbalmer upvalues are the external local variables that the function uses,
5636 1.1 mbalmer and that are consequently included in its closure.)
5637 1.1 mbalmer
5638 1.1 mbalmer
5639 1.1 mbalmer <p>
5640 1.4 mbalmer Upvalues have no particular order,
5641 1.4 mbalmer as they are active through the whole function.
5642 1.4 mbalmer They are numbered in an arbitrary order.
5643 1.1 mbalmer
5644 1.1 mbalmer
5645 1.1 mbalmer
5646 1.1 mbalmer
5647 1.1 mbalmer
5648 1.1 mbalmer <hr><h3><a name="lua_Hook"><code>lua_Hook</code></a></h3>
5649 1.1 mbalmer <pre>typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);</pre>
5650 1.1 mbalmer
5651 1.1 mbalmer <p>
5652 1.1 mbalmer Type for debugging hook functions.
5653 1.1 mbalmer
5654 1.1 mbalmer
5655 1.1 mbalmer <p>
5656 1.1 mbalmer Whenever a hook is called, its <code>ar</code> argument has its field
5657 1.1 mbalmer <code>event</code> set to the specific event that triggered the hook.
5658 1.1 mbalmer Lua identifies these events with the following constants:
5659 1.1 mbalmer <a name="pdf-LUA_HOOKCALL"><code>LUA_HOOKCALL</code></a>, <a name="pdf-LUA_HOOKRET"><code>LUA_HOOKRET</code></a>,
5660 1.2 lneto <a name="pdf-LUA_HOOKTAILCALL"><code>LUA_HOOKTAILCALL</code></a>, <a name="pdf-LUA_HOOKLINE"><code>LUA_HOOKLINE</code></a>,
5661 1.1 mbalmer and <a name="pdf-LUA_HOOKCOUNT"><code>LUA_HOOKCOUNT</code></a>.
5662 1.1 mbalmer Moreover, for line events, the field <code>currentline</code> is also set.
5663 1.1 mbalmer To get the value of any other field in <code>ar</code>,
5664 1.1 mbalmer the hook must call <a href="#lua_getinfo"><code>lua_getinfo</code></a>.
5665 1.2 lneto
5666 1.2 lneto
5667 1.2 lneto <p>
5668 1.2 lneto For call events, <code>event</code> can be <code>LUA_HOOKCALL</code>,
5669 1.2 lneto the normal value, or <code>LUA_HOOKTAILCALL</code>, for a tail call;
5670 1.2 lneto in this case, there will be no corresponding return event.
5671 1.1 mbalmer
5672 1.1 mbalmer
5673 1.1 mbalmer <p>
5674 1.1 mbalmer While Lua is running a hook, it disables other calls to hooks.
5675 1.1 mbalmer Therefore, if a hook calls back Lua to execute a function or a chunk,
5676 1.1 mbalmer this execution occurs without any calls to hooks.
5677 1.1 mbalmer
5678 1.1 mbalmer
5679 1.2 lneto <p>
5680 1.2 lneto Hook functions cannot have continuations,
5681 1.2 lneto that is, they cannot call <a href="#lua_yieldk"><code>lua_yieldk</code></a>,
5682 1.2 lneto <a href="#lua_pcallk"><code>lua_pcallk</code></a>, or <a href="#lua_callk"><code>lua_callk</code></a> with a non-null <code>k</code>.
5683 1.2 lneto
5684 1.2 lneto
5685 1.2 lneto <p>
5686 1.2 lneto Hook functions can yield under the following conditions:
5687 1.4 mbalmer Only count and line events can yield;
5688 1.4 mbalmer to yield, a hook function must finish its execution
5689 1.4 mbalmer calling <a href="#lua_yield"><code>lua_yield</code></a> with <code>nresults</code> equal to zero
5690 1.4 mbalmer (that is, with no values).
5691 1.2 lneto
5692 1.2 lneto
5693 1.1 mbalmer
5694 1.1 mbalmer
5695 1.1 mbalmer
5696 1.1 mbalmer <hr><h3><a name="lua_sethook"><code>lua_sethook</code></a></h3><p>
5697 1.2 lneto <span class="apii">[-0, +0, –]</span>
5698 1.2 lneto <pre>void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);</pre>
5699 1.1 mbalmer
5700 1.1 mbalmer <p>
5701 1.1 mbalmer Sets the debugging hook function.
5702 1.1 mbalmer
5703 1.1 mbalmer
5704 1.1 mbalmer <p>
5705 1.1 mbalmer Argument <code>f</code> is the hook function.
5706 1.1 mbalmer <code>mask</code> specifies on which events the hook will be called:
5707 1.1 mbalmer it is formed by a bitwise or of the constants
5708 1.1 mbalmer <a name="pdf-LUA_MASKCALL"><code>LUA_MASKCALL</code></a>,
5709 1.1 mbalmer <a name="pdf-LUA_MASKRET"><code>LUA_MASKRET</code></a>,
5710 1.1 mbalmer <a name="pdf-LUA_MASKLINE"><code>LUA_MASKLINE</code></a>,
5711 1.1 mbalmer and <a name="pdf-LUA_MASKCOUNT"><code>LUA_MASKCOUNT</code></a>.
5712 1.1 mbalmer The <code>count</code> argument is only meaningful when the mask
5713 1.1 mbalmer includes <code>LUA_MASKCOUNT</code>.
5714 1.1 mbalmer For each event, the hook is called as explained below:
5715 1.1 mbalmer
5716 1.1 mbalmer <ul>
5717 1.1 mbalmer
5718 1.2 lneto <li><b>The call hook: </b> is called when the interpreter calls a function.
5719 1.1 mbalmer The hook is called just after Lua enters the new function,
5720 1.1 mbalmer before the function gets its arguments.
5721 1.1 mbalmer </li>
5722 1.1 mbalmer
5723 1.2 lneto <li><b>The return hook: </b> is called when the interpreter returns from a function.
5724 1.1 mbalmer The hook is called just before Lua leaves the function.
5725 1.2 lneto There is no standard way to access the values
5726 1.2 lneto to be returned by the function.
5727 1.1 mbalmer </li>
5728 1.1 mbalmer
5729 1.2 lneto <li><b>The line hook: </b> is called when the interpreter is about to
5730 1.1 mbalmer start the execution of a new line of code,
5731 1.1 mbalmer or when it jumps back in the code (even to the same line).
5732 1.1 mbalmer (This event only happens while Lua is executing a Lua function.)
5733 1.1 mbalmer </li>
5734 1.1 mbalmer
5735 1.2 lneto <li><b>The count hook: </b> is called after the interpreter executes every
5736 1.1 mbalmer <code>count</code> instructions.
5737 1.1 mbalmer (This event only happens while Lua is executing a Lua function.)
5738 1.1 mbalmer </li>
5739 1.1 mbalmer
5740 1.1 mbalmer </ul>
5741 1.1 mbalmer
5742 1.1 mbalmer <p>
5743 1.1 mbalmer A hook is disabled by setting <code>mask</code> to zero.
5744 1.1 mbalmer
5745 1.1 mbalmer
5746 1.1 mbalmer
5747 1.1 mbalmer
5748 1.1 mbalmer
5749 1.1 mbalmer <hr><h3><a name="lua_setlocal"><code>lua_setlocal</code></a></h3><p>
5750 1.2 lneto <span class="apii">[-(0|1), +0, –]</span>
5751 1.3 lneto <pre>const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);</pre>
5752 1.1 mbalmer
5753 1.1 mbalmer <p>
5754 1.1 mbalmer Sets the value of a local variable of a given activation record.
5755 1.4 mbalmer It assigns the value at the top of the stack
5756 1.1 mbalmer to the variable and returns its name.
5757 1.1 mbalmer It also pops the value from the stack.
5758 1.1 mbalmer
5759 1.1 mbalmer
5760 1.1 mbalmer <p>
5761 1.1 mbalmer Returns <code>NULL</code> (and pops nothing)
5762 1.1 mbalmer when the index is greater than
5763 1.1 mbalmer the number of active local variables.
5764 1.1 mbalmer
5765 1.1 mbalmer
5766 1.4 mbalmer <p>
5767 1.4 mbalmer Parameters <code>ar</code> and <code>n</code> are as in function <a href="#lua_getlocal"><code>lua_getlocal</code></a>.
5768 1.4 mbalmer
5769 1.4 mbalmer
5770 1.1 mbalmer
5771 1.1 mbalmer
5772 1.1 mbalmer
5773 1.1 mbalmer <hr><h3><a name="lua_setupvalue"><code>lua_setupvalue</code></a></h3><p>
5774 1.2 lneto <span class="apii">[-(0|1), +0, –]</span>
5775 1.1 mbalmer <pre>const char *lua_setupvalue (lua_State *L, int funcindex, int n);</pre>
5776 1.1 mbalmer
5777 1.1 mbalmer <p>
5778 1.1 mbalmer Sets the value of a closure's upvalue.
5779 1.1 mbalmer It assigns the value at the top of the stack
5780 1.1 mbalmer to the upvalue and returns its name.
5781 1.1 mbalmer It also pops the value from the stack.
5782 1.1 mbalmer
5783 1.1 mbalmer
5784 1.1 mbalmer <p>
5785 1.1 mbalmer Returns <code>NULL</code> (and pops nothing)
5786 1.4 mbalmer when the index <code>n</code> is greater than the number of upvalues.
5787 1.4 mbalmer
5788 1.4 mbalmer
5789 1.4 mbalmer <p>
5790 1.4 mbalmer Parameters <code>funcindex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>.
5791 1.1 mbalmer
5792 1.1 mbalmer
5793 1.1 mbalmer
5794 1.1 mbalmer
5795 1.1 mbalmer
5796 1.2 lneto <hr><h3><a name="lua_upvalueid"><code>lua_upvalueid</code></a></h3><p>
5797 1.2 lneto <span class="apii">[-0, +0, –]</span>
5798 1.2 lneto <pre>void *lua_upvalueid (lua_State *L, int funcindex, int n);</pre>
5799 1.2 lneto
5800 1.2 lneto <p>
5801 1.3 lneto Returns a unique identifier for the upvalue numbered <code>n</code>
5802 1.2 lneto from the closure at index <code>funcindex</code>.
5803 1.2 lneto
5804 1.2 lneto
5805 1.2 lneto <p>
5806 1.2 lneto These unique identifiers allow a program to check whether different
5807 1.2 lneto closures share upvalues.
5808 1.2 lneto Lua closures that share an upvalue
5809 1.2 lneto (that is, that access a same external local variable)
5810 1.2 lneto will return identical ids for those upvalue indices.
5811 1.2 lneto
5812 1.2 lneto
5813 1.4 mbalmer <p>
5814 1.4 mbalmer Parameters <code>funcindex</code> and <code>n</code> are as in function <a href="#lua_getupvalue"><code>lua_getupvalue</code></a>,
5815 1.4 mbalmer but <code>n</code> cannot be greater than the number of upvalues.
5816 1.4 mbalmer
5817 1.4 mbalmer
5818 1.2 lneto
5819 1.2 lneto
5820 1.2 lneto
5821 1.2 lneto <hr><h3><a name="lua_upvaluejoin"><code>lua_upvaluejoin</code></a></h3><p>
5822 1.2 lneto <span class="apii">[-0, +0, –]</span>
5823 1.2 lneto <pre>void lua_upvaluejoin (lua_State *L, int funcindex1, int n1,
5824 1.2 lneto int funcindex2, int n2);</pre>
5825 1.2 lneto
5826 1.2 lneto <p>
5827 1.2 lneto Make the <code>n1</code>-th upvalue of the Lua closure at index <code>funcindex1</code>
5828 1.2 lneto refer to the <code>n2</code>-th upvalue of the Lua closure at index <code>funcindex2</code>.
5829 1.2 lneto
5830 1.2 lneto
5831 1.2 lneto
5832 1.2 lneto
5833 1.2 lneto
5834 1.1 mbalmer
5835 1.1 mbalmer
5836 1.2 lneto <h1>5 – <a name="5">The Auxiliary Library</a></h1>
5837 1.1 mbalmer
5838 1.1 mbalmer <p>
5839 1.1 mbalmer
5840 1.1 mbalmer The <em>auxiliary library</em> provides several convenient functions
5841 1.1 mbalmer to interface C with Lua.
5842 1.2 lneto While the basic API provides the primitive functions for all
5843 1.1 mbalmer interactions between C and Lua,
5844 1.1 mbalmer the auxiliary library provides higher-level functions for some
5845 1.1 mbalmer common tasks.
5846 1.1 mbalmer
5847 1.1 mbalmer
5848 1.1 mbalmer <p>
5849 1.2 lneto All functions and types from the auxiliary library
5850 1.1 mbalmer are defined in header file <code>lauxlib.h</code> and
5851 1.1 mbalmer have a prefix <code>luaL_</code>.
5852 1.1 mbalmer
5853 1.1 mbalmer
5854 1.1 mbalmer <p>
5855 1.1 mbalmer All functions in the auxiliary library are built on
5856 1.1 mbalmer top of the basic API,
5857 1.2 lneto and so they provide nothing that cannot be done with that API.
5858 1.2 lneto Nevertheless, the use of the auxiliary library ensures
5859 1.2 lneto more consistency to your code.
5860 1.2 lneto
5861 1.2 lneto
5862 1.2 lneto <p>
5863 1.2 lneto Several functions in the auxiliary library use internally some
5864 1.2 lneto extra stack slots.
5865 1.2 lneto When a function in the auxiliary library uses less than five slots,
5866 1.2 lneto it does not check the stack size;
5867 1.2 lneto it simply assumes that there are enough slots.
5868 1.1 mbalmer
5869 1.1 mbalmer
5870 1.1 mbalmer <p>
5871 1.1 mbalmer Several functions in the auxiliary library are used to
5872 1.1 mbalmer check C function arguments.
5873 1.1 mbalmer Because the error message is formatted for arguments
5874 1.1 mbalmer (e.g., "<code>bad argument #1</code>"),
5875 1.1 mbalmer you should not use these functions for other stack values.
5876 1.1 mbalmer
5877 1.1 mbalmer
5878 1.2 lneto <p>
5879 1.2 lneto Functions called <code>luaL_check*</code>
5880 1.2 lneto always raise an error if the check is not satisfied.
5881 1.2 lneto
5882 1.2 lneto
5883 1.1 mbalmer
5884 1.2 lneto <h2>5.1 – <a name="5.1">Functions and Types</a></h2>
5885 1.1 mbalmer
5886 1.1 mbalmer <p>
5887 1.1 mbalmer Here we list all functions and types from the auxiliary library
5888 1.1 mbalmer in alphabetical order.
5889 1.1 mbalmer
5890 1.1 mbalmer
5891 1.1 mbalmer
5892 1.1 mbalmer <hr><h3><a name="luaL_addchar"><code>luaL_addchar</code></a></h3><p>
5893 1.5 lneto <span class="apii">[-?, +?, <em>m</em>]</span>
5894 1.1 mbalmer <pre>void luaL_addchar (luaL_Buffer *B, char c);</pre>
5895 1.1 mbalmer
5896 1.1 mbalmer <p>
5897 1.2 lneto Adds the byte <code>c</code> to the buffer <code>B</code>
5898 1.1 mbalmer (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5899 1.1 mbalmer
5900 1.1 mbalmer
5901 1.1 mbalmer
5902 1.1 mbalmer
5903 1.1 mbalmer
5904 1.1 mbalmer <hr><h3><a name="luaL_addlstring"><code>luaL_addlstring</code></a></h3><p>
5905 1.5 lneto <span class="apii">[-?, +?, <em>m</em>]</span>
5906 1.1 mbalmer <pre>void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);</pre>
5907 1.1 mbalmer
5908 1.1 mbalmer <p>
5909 1.1 mbalmer Adds the string pointed to by <code>s</code> with length <code>l</code> to
5910 1.1 mbalmer the buffer <code>B</code>
5911 1.1 mbalmer (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5912 1.2 lneto The string can contain embedded zeros.
5913 1.1 mbalmer
5914 1.1 mbalmer
5915 1.1 mbalmer
5916 1.1 mbalmer
5917 1.1 mbalmer
5918 1.1 mbalmer <hr><h3><a name="luaL_addsize"><code>luaL_addsize</code></a></h3><p>
5919 1.5 lneto <span class="apii">[-?, +?, –]</span>
5920 1.1 mbalmer <pre>void luaL_addsize (luaL_Buffer *B, size_t n);</pre>
5921 1.1 mbalmer
5922 1.1 mbalmer <p>
5923 1.1 mbalmer Adds to the buffer <code>B</code> (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>)
5924 1.1 mbalmer a string of length <code>n</code> previously copied to the
5925 1.1 mbalmer buffer area (see <a href="#luaL_prepbuffer"><code>luaL_prepbuffer</code></a>).
5926 1.1 mbalmer
5927 1.1 mbalmer
5928 1.1 mbalmer
5929 1.1 mbalmer
5930 1.1 mbalmer
5931 1.1 mbalmer <hr><h3><a name="luaL_addstring"><code>luaL_addstring</code></a></h3><p>
5932 1.5 lneto <span class="apii">[-?, +?, <em>m</em>]</span>
5933 1.1 mbalmer <pre>void luaL_addstring (luaL_Buffer *B, const char *s);</pre>
5934 1.1 mbalmer
5935 1.1 mbalmer <p>
5936 1.1 mbalmer Adds the zero-terminated string pointed to by <code>s</code>
5937 1.1 mbalmer to the buffer <code>B</code>
5938 1.1 mbalmer (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5939 1.1 mbalmer
5940 1.1 mbalmer
5941 1.1 mbalmer
5942 1.1 mbalmer
5943 1.1 mbalmer
5944 1.1 mbalmer <hr><h3><a name="luaL_addvalue"><code>luaL_addvalue</code></a></h3><p>
5945 1.5 lneto <span class="apii">[-1, +?, <em>m</em>]</span>
5946 1.1 mbalmer <pre>void luaL_addvalue (luaL_Buffer *B);</pre>
5947 1.1 mbalmer
5948 1.1 mbalmer <p>
5949 1.1 mbalmer Adds the value at the top of the stack
5950 1.1 mbalmer to the buffer <code>B</code>
5951 1.1 mbalmer (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
5952 1.1 mbalmer Pops the value.
5953 1.1 mbalmer
5954 1.1 mbalmer
5955 1.1 mbalmer <p>
5956 1.1 mbalmer This is the only function on string buffers that can (and must)
5957 1.1 mbalmer be called with an extra element on the stack,
5958 1.1 mbalmer which is the value to be added to the buffer.
5959 1.1 mbalmer
5960 1.1 mbalmer
5961 1.1 mbalmer
5962 1.1 mbalmer
5963 1.1 mbalmer
5964 1.1 mbalmer <hr><h3><a name="luaL_argcheck"><code>luaL_argcheck</code></a></h3><p>
5965 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
5966 1.1 mbalmer <pre>void luaL_argcheck (lua_State *L,
5967 1.1 mbalmer int cond,
5968 1.2 lneto int arg,
5969 1.1 mbalmer const char *extramsg);</pre>
5970 1.1 mbalmer
5971 1.1 mbalmer <p>
5972 1.1 mbalmer Checks whether <code>cond</code> is true.
5973 1.2 lneto If it is not, raises an error with a standard message (see <a href="#luaL_argerror"><code>luaL_argerror</code></a>).
5974 1.1 mbalmer
5975 1.1 mbalmer
5976 1.1 mbalmer
5977 1.1 mbalmer
5978 1.1 mbalmer
5979 1.1 mbalmer <hr><h3><a name="luaL_argerror"><code>luaL_argerror</code></a></h3><p>
5980 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
5981 1.2 lneto <pre>int luaL_argerror (lua_State *L, int arg, const char *extramsg);</pre>
5982 1.1 mbalmer
5983 1.1 mbalmer <p>
5984 1.2 lneto Raises an error reporting a problem with argument <code>arg</code>
5985 1.2 lneto of the C function that called it,
5986 1.2 lneto using a standard message
5987 1.2 lneto that includes <code>extramsg</code> as a comment:
5988 1.1 mbalmer
5989 1.1 mbalmer <pre>
5990 1.2 lneto bad argument #<em>arg</em> to '<em>funcname</em>' (<em>extramsg</em>)
5991 1.2 lneto </pre><p>
5992 1.2 lneto This function never returns.
5993 1.1 mbalmer
5994 1.1 mbalmer
5995 1.1 mbalmer
5996 1.1 mbalmer
5997 1.1 mbalmer
5998 1.1 mbalmer <hr><h3><a name="luaL_Buffer"><code>luaL_Buffer</code></a></h3>
5999 1.1 mbalmer <pre>typedef struct luaL_Buffer luaL_Buffer;</pre>
6000 1.1 mbalmer
6001 1.1 mbalmer <p>
6002 1.1 mbalmer Type for a <em>string buffer</em>.
6003 1.1 mbalmer
6004 1.1 mbalmer
6005 1.1 mbalmer <p>
6006 1.1 mbalmer A string buffer allows C code to build Lua strings piecemeal.
6007 1.1 mbalmer Its pattern of use is as follows:
6008 1.1 mbalmer
6009 1.1 mbalmer <ul>
6010 1.1 mbalmer
6011 1.2 lneto <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
6012 1.1 mbalmer
6013 1.2 lneto <li>Then initialize it with a call <code>luaL_buffinit(L, &b)</code>.</li>
6014 1.1 mbalmer
6015 1.1 mbalmer <li>
6016 1.2 lneto Then add string pieces to the buffer calling any of
6017 1.1 mbalmer the <code>luaL_add*</code> functions.
6018 1.1 mbalmer </li>
6019 1.1 mbalmer
6020 1.1 mbalmer <li>
6021 1.2 lneto Finish by calling <code>luaL_pushresult(&b)</code>.
6022 1.1 mbalmer This call leaves the final string on the top of the stack.
6023 1.1 mbalmer </li>
6024 1.1 mbalmer
6025 1.1 mbalmer </ul>
6026 1.1 mbalmer
6027 1.1 mbalmer <p>
6028 1.2 lneto If you know beforehand the total size of the resulting string,
6029 1.2 lneto you can use the buffer like this:
6030 1.2 lneto
6031 1.2 lneto <ul>
6032 1.2 lneto
6033 1.2 lneto <li>First declare a variable <code>b</code> of type <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>.</li>
6034 1.2 lneto
6035 1.2 lneto <li>Then initialize it and preallocate a space of
6036 1.2 lneto size <code>sz</code> with a call <code>luaL_buffinitsize(L, &b, sz)</code>.</li>
6037 1.2 lneto
6038 1.2 lneto <li>Then copy the string into that space.</li>
6039 1.2 lneto
6040 1.2 lneto <li>
6041 1.2 lneto Finish by calling <code>luaL_pushresultsize(&b, sz)</code>,
6042 1.2 lneto where <code>sz</code> is the total size of the resulting string
6043 1.2 lneto copied into that space.
6044 1.2 lneto </li>
6045 1.2 lneto
6046 1.2 lneto </ul>
6047 1.2 lneto
6048 1.2 lneto <p>
6049 1.1 mbalmer During its normal operation,
6050 1.1 mbalmer a string buffer uses a variable number of stack slots.
6051 1.1 mbalmer So, while using a buffer, you cannot assume that you know where
6052 1.1 mbalmer the top of the stack is.
6053 1.1 mbalmer You can use the stack between successive calls to buffer operations
6054 1.1 mbalmer as long as that use is balanced;
6055 1.1 mbalmer that is,
6056 1.1 mbalmer when you call a buffer operation,
6057 1.1 mbalmer the stack is at the same level
6058 1.1 mbalmer it was immediately after the previous buffer operation.
6059 1.1 mbalmer (The only exception to this rule is <a href="#luaL_addvalue"><code>luaL_addvalue</code></a>.)
6060 1.1 mbalmer After calling <a href="#luaL_pushresult"><code>luaL_pushresult</code></a> the stack is back to its
6061 1.1 mbalmer level when the buffer was initialized,
6062 1.1 mbalmer plus the final string on its top.
6063 1.1 mbalmer
6064 1.1 mbalmer
6065 1.1 mbalmer
6066 1.1 mbalmer
6067 1.1 mbalmer
6068 1.1 mbalmer <hr><h3><a name="luaL_buffinit"><code>luaL_buffinit</code></a></h3><p>
6069 1.2 lneto <span class="apii">[-0, +0, –]</span>
6070 1.1 mbalmer <pre>void luaL_buffinit (lua_State *L, luaL_Buffer *B);</pre>
6071 1.1 mbalmer
6072 1.1 mbalmer <p>
6073 1.1 mbalmer Initializes a buffer <code>B</code>.
6074 1.1 mbalmer This function does not allocate any space;
6075 1.1 mbalmer the buffer must be declared as a variable
6076 1.1 mbalmer (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6077 1.1 mbalmer
6078 1.1 mbalmer
6079 1.1 mbalmer
6080 1.1 mbalmer
6081 1.1 mbalmer
6082 1.2 lneto <hr><h3><a name="luaL_buffinitsize"><code>luaL_buffinitsize</code></a></h3><p>
6083 1.5 lneto <span class="apii">[-?, +?, <em>m</em>]</span>
6084 1.2 lneto <pre>char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);</pre>
6085 1.2 lneto
6086 1.2 lneto <p>
6087 1.2 lneto Equivalent to the sequence
6088 1.2 lneto <a href="#luaL_buffinit"><code>luaL_buffinit</code></a>, <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>.
6089 1.2 lneto
6090 1.2 lneto
6091 1.2 lneto
6092 1.2 lneto
6093 1.2 lneto
6094 1.1 mbalmer <hr><h3><a name="luaL_callmeta"><code>luaL_callmeta</code></a></h3><p>
6095 1.1 mbalmer <span class="apii">[-0, +(0|1), <em>e</em>]</span>
6096 1.1 mbalmer <pre>int luaL_callmeta (lua_State *L, int obj, const char *e);</pre>
6097 1.1 mbalmer
6098 1.1 mbalmer <p>
6099 1.1 mbalmer Calls a metamethod.
6100 1.1 mbalmer
6101 1.1 mbalmer
6102 1.1 mbalmer <p>
6103 1.1 mbalmer If the object at index <code>obj</code> has a metatable and this
6104 1.1 mbalmer metatable has a field <code>e</code>,
6105 1.2 lneto this function calls this field passing the object as its only argument.
6106 1.2 lneto In this case this function returns true and pushes onto the
6107 1.1 mbalmer stack the value returned by the call.
6108 1.1 mbalmer If there is no metatable or no metamethod,
6109 1.2 lneto this function returns false (without pushing any value on the stack).
6110 1.1 mbalmer
6111 1.1 mbalmer
6112 1.1 mbalmer
6113 1.1 mbalmer
6114 1.1 mbalmer
6115 1.1 mbalmer <hr><h3><a name="luaL_checkany"><code>luaL_checkany</code></a></h3><p>
6116 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6117 1.2 lneto <pre>void luaL_checkany (lua_State *L, int arg);</pre>
6118 1.1 mbalmer
6119 1.1 mbalmer <p>
6120 1.1 mbalmer Checks whether the function has an argument
6121 1.2 lneto of any type (including <b>nil</b>) at position <code>arg</code>.
6122 1.1 mbalmer
6123 1.1 mbalmer
6124 1.1 mbalmer
6125 1.1 mbalmer
6126 1.1 mbalmer
6127 1.1 mbalmer <hr><h3><a name="luaL_checkinteger"><code>luaL_checkinteger</code></a></h3><p>
6128 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6129 1.2 lneto <pre>lua_Integer luaL_checkinteger (lua_State *L, int arg);</pre>
6130 1.1 mbalmer
6131 1.1 mbalmer <p>
6132 1.2 lneto Checks whether the function argument <code>arg</code> is an integer
6133 1.2 lneto (or can be converted to an integer)
6134 1.2 lneto and returns this integer cast to a <a href="#lua_Integer"><code>lua_Integer</code></a>.
6135 1.1 mbalmer
6136 1.1 mbalmer
6137 1.1 mbalmer
6138 1.1 mbalmer
6139 1.1 mbalmer
6140 1.1 mbalmer <hr><h3><a name="luaL_checklstring"><code>luaL_checklstring</code></a></h3><p>
6141 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6142 1.2 lneto <pre>const char *luaL_checklstring (lua_State *L, int arg, size_t *l);</pre>
6143 1.1 mbalmer
6144 1.1 mbalmer <p>
6145 1.2 lneto Checks whether the function argument <code>arg</code> is a string
6146 1.1 mbalmer and returns this string;
6147 1.1 mbalmer if <code>l</code> is not <code>NULL</code> fills <code>*l</code>
6148 1.1 mbalmer with the string's length.
6149 1.1 mbalmer
6150 1.1 mbalmer
6151 1.1 mbalmer <p>
6152 1.1 mbalmer This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
6153 1.1 mbalmer so all conversions and caveats of that function apply here.
6154 1.1 mbalmer
6155 1.1 mbalmer
6156 1.1 mbalmer
6157 1.1 mbalmer
6158 1.1 mbalmer
6159 1.1 mbalmer <hr><h3><a name="luaL_checknumber"><code>luaL_checknumber</code></a></h3><p>
6160 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6161 1.2 lneto <pre>lua_Number luaL_checknumber (lua_State *L, int arg);</pre>
6162 1.1 mbalmer
6163 1.1 mbalmer <p>
6164 1.2 lneto Checks whether the function argument <code>arg</code> is a number
6165 1.1 mbalmer and returns this number.
6166 1.1 mbalmer
6167 1.1 mbalmer
6168 1.1 mbalmer
6169 1.1 mbalmer
6170 1.1 mbalmer
6171 1.1 mbalmer <hr><h3><a name="luaL_checkoption"><code>luaL_checkoption</code></a></h3><p>
6172 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6173 1.1 mbalmer <pre>int luaL_checkoption (lua_State *L,
6174 1.2 lneto int arg,
6175 1.1 mbalmer const char *def,
6176 1.1 mbalmer const char *const lst[]);</pre>
6177 1.1 mbalmer
6178 1.1 mbalmer <p>
6179 1.2 lneto Checks whether the function argument <code>arg</code> is a string and
6180 1.1 mbalmer searches for this string in the array <code>lst</code>
6181 1.1 mbalmer (which must be NULL-terminated).
6182 1.1 mbalmer Returns the index in the array where the string was found.
6183 1.1 mbalmer Raises an error if the argument is not a string or
6184 1.1 mbalmer if the string cannot be found.
6185 1.1 mbalmer
6186 1.1 mbalmer
6187 1.1 mbalmer <p>
6188 1.1 mbalmer If <code>def</code> is not <code>NULL</code>,
6189 1.1 mbalmer the function uses <code>def</code> as a default value when
6190 1.2 lneto there is no argument <code>arg</code> or when this argument is <b>nil</b>.
6191 1.1 mbalmer
6192 1.1 mbalmer
6193 1.1 mbalmer <p>
6194 1.1 mbalmer This is a useful function for mapping strings to C enums.
6195 1.1 mbalmer (The usual convention in Lua libraries is
6196 1.1 mbalmer to use strings instead of numbers to select options.)
6197 1.1 mbalmer
6198 1.1 mbalmer
6199 1.1 mbalmer
6200 1.1 mbalmer
6201 1.1 mbalmer
6202 1.1 mbalmer <hr><h3><a name="luaL_checkstack"><code>luaL_checkstack</code></a></h3><p>
6203 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6204 1.1 mbalmer <pre>void luaL_checkstack (lua_State *L, int sz, const char *msg);</pre>
6205 1.1 mbalmer
6206 1.1 mbalmer <p>
6207 1.1 mbalmer Grows the stack size to <code>top + sz</code> elements,
6208 1.1 mbalmer raising an error if the stack cannot grow to that size.
6209 1.2 lneto <code>msg</code> is an additional text to go into the error message
6210 1.2 lneto (or <code>NULL</code> for no additional text).
6211 1.1 mbalmer
6212 1.1 mbalmer
6213 1.1 mbalmer
6214 1.1 mbalmer
6215 1.1 mbalmer
6216 1.1 mbalmer <hr><h3><a name="luaL_checkstring"><code>luaL_checkstring</code></a></h3><p>
6217 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6218 1.2 lneto <pre>const char *luaL_checkstring (lua_State *L, int arg);</pre>
6219 1.1 mbalmer
6220 1.1 mbalmer <p>
6221 1.2 lneto Checks whether the function argument <code>arg</code> is a string
6222 1.1 mbalmer and returns this string.
6223 1.1 mbalmer
6224 1.1 mbalmer
6225 1.1 mbalmer <p>
6226 1.1 mbalmer This function uses <a href="#lua_tolstring"><code>lua_tolstring</code></a> to get its result,
6227 1.1 mbalmer so all conversions and caveats of that function apply here.
6228 1.1 mbalmer
6229 1.1 mbalmer
6230 1.1 mbalmer
6231 1.1 mbalmer
6232 1.1 mbalmer
6233 1.1 mbalmer <hr><h3><a name="luaL_checktype"><code>luaL_checktype</code></a></h3><p>
6234 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6235 1.2 lneto <pre>void luaL_checktype (lua_State *L, int arg, int t);</pre>
6236 1.1 mbalmer
6237 1.1 mbalmer <p>
6238 1.2 lneto Checks whether the function argument <code>arg</code> has type <code>t</code>.
6239 1.1 mbalmer See <a href="#lua_type"><code>lua_type</code></a> for the encoding of types for <code>t</code>.
6240 1.1 mbalmer
6241 1.1 mbalmer
6242 1.1 mbalmer
6243 1.1 mbalmer
6244 1.1 mbalmer
6245 1.1 mbalmer <hr><h3><a name="luaL_checkudata"><code>luaL_checkudata</code></a></h3><p>
6246 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6247 1.2 lneto <pre>void *luaL_checkudata (lua_State *L, int arg, const char *tname);</pre>
6248 1.2 lneto
6249 1.2 lneto <p>
6250 1.2 lneto Checks whether the function argument <code>arg</code> is a userdata
6251 1.2 lneto of the type <code>tname</code> (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>) and
6252 1.2 lneto returns the userdata address (see <a href="#lua_touserdata"><code>lua_touserdata</code></a>).
6253 1.2 lneto
6254 1.2 lneto
6255 1.2 lneto
6256 1.2 lneto
6257 1.2 lneto
6258 1.2 lneto <hr><h3><a name="luaL_checkversion"><code>luaL_checkversion</code></a></h3><p>
6259 1.2 lneto <span class="apii">[-0, +0, –]</span>
6260 1.2 lneto <pre>void luaL_checkversion (lua_State *L);</pre>
6261 1.1 mbalmer
6262 1.1 mbalmer <p>
6263 1.2 lneto Checks whether the core running the call,
6264 1.2 lneto the core that created the Lua state,
6265 1.2 lneto and the code making the call are all using the same version of Lua.
6266 1.2 lneto Also checks whether the core running the call
6267 1.2 lneto and the core that created the Lua state
6268 1.2 lneto are using the same address space.
6269 1.1 mbalmer
6270 1.1 mbalmer
6271 1.1 mbalmer
6272 1.1 mbalmer
6273 1.1 mbalmer
6274 1.1 mbalmer <hr><h3><a name="luaL_dofile"><code>luaL_dofile</code></a></h3><p>
6275 1.2 lneto <span class="apii">[-0, +?, <em>e</em>]</span>
6276 1.1 mbalmer <pre>int luaL_dofile (lua_State *L, const char *filename);</pre>
6277 1.1 mbalmer
6278 1.1 mbalmer <p>
6279 1.1 mbalmer Loads and runs the given file.
6280 1.1 mbalmer It is defined as the following macro:
6281 1.1 mbalmer
6282 1.1 mbalmer <pre>
6283 1.1 mbalmer (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
6284 1.1 mbalmer </pre><p>
6285 1.2 lneto It returns false if there are no errors
6286 1.2 lneto or true in case of errors.
6287 1.1 mbalmer
6288 1.1 mbalmer
6289 1.1 mbalmer
6290 1.1 mbalmer
6291 1.1 mbalmer
6292 1.1 mbalmer <hr><h3><a name="luaL_dostring"><code>luaL_dostring</code></a></h3><p>
6293 1.2 lneto <span class="apii">[-0, +?, –]</span>
6294 1.1 mbalmer <pre>int luaL_dostring (lua_State *L, const char *str);</pre>
6295 1.1 mbalmer
6296 1.1 mbalmer <p>
6297 1.1 mbalmer Loads and runs the given string.
6298 1.1 mbalmer It is defined as the following macro:
6299 1.1 mbalmer
6300 1.1 mbalmer <pre>
6301 1.1 mbalmer (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
6302 1.1 mbalmer </pre><p>
6303 1.2 lneto It returns false if there are no errors
6304 1.2 lneto or true in case of errors.
6305 1.1 mbalmer
6306 1.1 mbalmer
6307 1.1 mbalmer
6308 1.1 mbalmer
6309 1.1 mbalmer
6310 1.1 mbalmer <hr><h3><a name="luaL_error"><code>luaL_error</code></a></h3><p>
6311 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6312 1.1 mbalmer <pre>int luaL_error (lua_State *L, const char *fmt, ...);</pre>
6313 1.1 mbalmer
6314 1.1 mbalmer <p>
6315 1.1 mbalmer Raises an error.
6316 1.2 lneto The error message format is given by <code>fmt</code>
6317 1.2 lneto plus any extra arguments,
6318 1.2 lneto following the same rules of <a href="#lua_pushfstring"><code>lua_pushfstring</code></a>.
6319 1.2 lneto It also adds at the beginning of the message the file name and
6320 1.2 lneto the line number where the error occurred,
6321 1.2 lneto if this information is available.
6322 1.2 lneto
6323 1.2 lneto
6324 1.2 lneto <p>
6325 1.2 lneto This function never returns,
6326 1.2 lneto but it is an idiom to use it in C functions
6327 1.2 lneto as <code>return luaL_error(<em>args</em>)</code>.
6328 1.2 lneto
6329 1.2 lneto
6330 1.2 lneto
6331 1.2 lneto
6332 1.2 lneto
6333 1.2 lneto <hr><h3><a name="luaL_execresult"><code>luaL_execresult</code></a></h3><p>
6334 1.5 lneto <span class="apii">[-0, +3, <em>m</em>]</span>
6335 1.2 lneto <pre>int luaL_execresult (lua_State *L, int stat);</pre>
6336 1.2 lneto
6337 1.2 lneto <p>
6338 1.2 lneto This function produces the return values for
6339 1.2 lneto process-related functions in the standard library
6340 1.2 lneto (<a href="#pdf-os.execute"><code>os.execute</code></a> and <a href="#pdf-io.close"><code>io.close</code></a>).
6341 1.2 lneto
6342 1.2 lneto
6343 1.2 lneto
6344 1.2 lneto
6345 1.1 mbalmer
6346 1.2 lneto <hr><h3><a name="luaL_fileresult"><code>luaL_fileresult</code></a></h3><p>
6347 1.5 lneto <span class="apii">[-0, +(1|3), <em>m</em>]</span>
6348 1.2 lneto <pre>int luaL_fileresult (lua_State *L, int stat, const char *fname);</pre>
6349 1.1 mbalmer
6350 1.1 mbalmer <p>
6351 1.2 lneto This function produces the return values for
6352 1.2 lneto file-related functions in the standard library
6353 1.2 lneto (<a href="#pdf-io.open"><code>io.open</code></a>, <a href="#pdf-os.rename"><code>os.rename</code></a>, <a href="#pdf-file:seek"><code>file:seek</code></a>, etc.).
6354 1.1 mbalmer
6355 1.1 mbalmer
6356 1.1 mbalmer
6357 1.1 mbalmer
6358 1.1 mbalmer
6359 1.1 mbalmer <hr><h3><a name="luaL_getmetafield"><code>luaL_getmetafield</code></a></h3><p>
6360 1.5 lneto <span class="apii">[-0, +(0|1), <em>m</em>]</span>
6361 1.1 mbalmer <pre>int luaL_getmetafield (lua_State *L, int obj, const char *e);</pre>
6362 1.1 mbalmer
6363 1.1 mbalmer <p>
6364 1.1 mbalmer Pushes onto the stack the field <code>e</code> from the metatable
6365 1.3 lneto of the object at index <code>obj</code> and returns the type of pushed value.
6366 1.1 mbalmer If the object does not have a metatable,
6367 1.1 mbalmer or if the metatable does not have this field,
6368 1.3 lneto pushes nothing and returns <code>LUA_TNIL</code>.
6369 1.1 mbalmer
6370 1.1 mbalmer
6371 1.1 mbalmer
6372 1.1 mbalmer
6373 1.1 mbalmer
6374 1.1 mbalmer <hr><h3><a name="luaL_getmetatable"><code>luaL_getmetatable</code></a></h3><p>
6375 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6376 1.3 lneto <pre>int luaL_getmetatable (lua_State *L, const char *tname);</pre>
6377 1.1 mbalmer
6378 1.1 mbalmer <p>
6379 1.1 mbalmer Pushes onto the stack the metatable associated with name <code>tname</code>
6380 1.4 mbalmer in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>)
6381 1.4 mbalmer (<b>nil</b> if there is no metatable associated with that name).
6382 1.4 mbalmer Returns the type of the pushed value.
6383 1.1 mbalmer
6384 1.1 mbalmer
6385 1.1 mbalmer
6386 1.1 mbalmer
6387 1.1 mbalmer
6388 1.2 lneto <hr><h3><a name="luaL_getsubtable"><code>luaL_getsubtable</code></a></h3><p>
6389 1.2 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
6390 1.2 lneto <pre>int luaL_getsubtable (lua_State *L, int idx, const char *fname);</pre>
6391 1.2 lneto
6392 1.2 lneto <p>
6393 1.2 lneto Ensures that the value <code>t[fname]</code>,
6394 1.2 lneto where <code>t</code> is the value at index <code>idx</code>,
6395 1.2 lneto is a table,
6396 1.2 lneto and pushes that table onto the stack.
6397 1.2 lneto Returns true if it finds a previous table there
6398 1.2 lneto and false if it creates a new table.
6399 1.2 lneto
6400 1.2 lneto
6401 1.2 lneto
6402 1.2 lneto
6403 1.2 lneto
6404 1.1 mbalmer <hr><h3><a name="luaL_gsub"><code>luaL_gsub</code></a></h3><p>
6405 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6406 1.1 mbalmer <pre>const char *luaL_gsub (lua_State *L,
6407 1.1 mbalmer const char *s,
6408 1.1 mbalmer const char *p,
6409 1.1 mbalmer const char *r);</pre>
6410 1.1 mbalmer
6411 1.1 mbalmer <p>
6412 1.1 mbalmer Creates a copy of string <code>s</code> by replacing
6413 1.1 mbalmer any occurrence of the string <code>p</code>
6414 1.1 mbalmer with the string <code>r</code>.
6415 1.1 mbalmer Pushes the resulting string on the stack and returns it.
6416 1.1 mbalmer
6417 1.1 mbalmer
6418 1.1 mbalmer
6419 1.1 mbalmer
6420 1.1 mbalmer
6421 1.2 lneto <hr><h3><a name="luaL_len"><code>luaL_len</code></a></h3><p>
6422 1.2 lneto <span class="apii">[-0, +0, <em>e</em>]</span>
6423 1.2 lneto <pre>lua_Integer luaL_len (lua_State *L, int index);</pre>
6424 1.2 lneto
6425 1.2 lneto <p>
6426 1.2 lneto Returns the "length" of the value at the given index
6427 1.2 lneto as a number;
6428 1.2 lneto it is equivalent to the '<code>#</code>' operator in Lua (see <a href="#3.4.7">§3.4.7</a>).
6429 1.2 lneto Raises an error if the result of the operation is not an integer.
6430 1.2 lneto (This case only can happen through metamethods.)
6431 1.2 lneto
6432 1.2 lneto
6433 1.2 lneto
6434 1.2 lneto
6435 1.2 lneto
6436 1.1 mbalmer <hr><h3><a name="luaL_loadbuffer"><code>luaL_loadbuffer</code></a></h3><p>
6437 1.2 lneto <span class="apii">[-0, +1, –]</span>
6438 1.1 mbalmer <pre>int luaL_loadbuffer (lua_State *L,
6439 1.1 mbalmer const char *buff,
6440 1.1 mbalmer size_t sz,
6441 1.1 mbalmer const char *name);</pre>
6442 1.1 mbalmer
6443 1.1 mbalmer <p>
6444 1.2 lneto Equivalent to <a href="#luaL_loadbufferx"><code>luaL_loadbufferx</code></a> with <code>mode</code> equal to <code>NULL</code>.
6445 1.2 lneto
6446 1.2 lneto
6447 1.2 lneto
6448 1.2 lneto
6449 1.2 lneto
6450 1.2 lneto <hr><h3><a name="luaL_loadbufferx"><code>luaL_loadbufferx</code></a></h3><p>
6451 1.2 lneto <span class="apii">[-0, +1, –]</span>
6452 1.2 lneto <pre>int luaL_loadbufferx (lua_State *L,
6453 1.2 lneto const char *buff,
6454 1.2 lneto size_t sz,
6455 1.2 lneto const char *name,
6456 1.2 lneto const char *mode);</pre>
6457 1.2 lneto
6458 1.2 lneto <p>
6459 1.1 mbalmer Loads a buffer as a Lua chunk.
6460 1.1 mbalmer This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the
6461 1.1 mbalmer buffer pointed to by <code>buff</code> with size <code>sz</code>.
6462 1.1 mbalmer
6463 1.1 mbalmer
6464 1.1 mbalmer <p>
6465 1.1 mbalmer This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
6466 1.1 mbalmer <code>name</code> is the chunk name,
6467 1.1 mbalmer used for debug information and error messages.
6468 1.2 lneto The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
6469 1.1 mbalmer
6470 1.1 mbalmer
6471 1.1 mbalmer
6472 1.1 mbalmer
6473 1.1 mbalmer
6474 1.1 mbalmer <hr><h3><a name="luaL_loadfile"><code>luaL_loadfile</code></a></h3><p>
6475 1.2 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
6476 1.1 mbalmer <pre>int luaL_loadfile (lua_State *L, const char *filename);</pre>
6477 1.1 mbalmer
6478 1.1 mbalmer <p>
6479 1.2 lneto Equivalent to <a href="#luaL_loadfilex"><code>luaL_loadfilex</code></a> with <code>mode</code> equal to <code>NULL</code>.
6480 1.2 lneto
6481 1.2 lneto
6482 1.2 lneto
6483 1.2 lneto
6484 1.2 lneto
6485 1.2 lneto <hr><h3><a name="luaL_loadfilex"><code>luaL_loadfilex</code></a></h3><p>
6486 1.2 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
6487 1.2 lneto <pre>int luaL_loadfilex (lua_State *L, const char *filename,
6488 1.2 lneto const char *mode);</pre>
6489 1.2 lneto
6490 1.2 lneto <p>
6491 1.1 mbalmer Loads a file as a Lua chunk.
6492 1.1 mbalmer This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in the file
6493 1.1 mbalmer named <code>filename</code>.
6494 1.1 mbalmer If <code>filename</code> is <code>NULL</code>,
6495 1.1 mbalmer then it loads from the standard input.
6496 1.1 mbalmer The first line in the file is ignored if it starts with a <code>#</code>.
6497 1.1 mbalmer
6498 1.1 mbalmer
6499 1.1 mbalmer <p>
6500 1.2 lneto The string <code>mode</code> works as in function <a href="#lua_load"><code>lua_load</code></a>.
6501 1.2 lneto
6502 1.2 lneto
6503 1.2 lneto <p>
6504 1.1 mbalmer This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>,
6505 1.1 mbalmer but it has an extra error code <a name="pdf-LUA_ERRFILE"><code>LUA_ERRFILE</code></a>
6506 1.2 lneto if it cannot open/read the file or the file has a wrong mode.
6507 1.1 mbalmer
6508 1.1 mbalmer
6509 1.1 mbalmer <p>
6510 1.1 mbalmer As <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
6511 1.1 mbalmer it does not run it.
6512 1.1 mbalmer
6513 1.1 mbalmer
6514 1.1 mbalmer
6515 1.1 mbalmer
6516 1.1 mbalmer
6517 1.1 mbalmer <hr><h3><a name="luaL_loadstring"><code>luaL_loadstring</code></a></h3><p>
6518 1.2 lneto <span class="apii">[-0, +1, –]</span>
6519 1.1 mbalmer <pre>int luaL_loadstring (lua_State *L, const char *s);</pre>
6520 1.1 mbalmer
6521 1.1 mbalmer <p>
6522 1.1 mbalmer Loads a string as a Lua chunk.
6523 1.1 mbalmer This function uses <a href="#lua_load"><code>lua_load</code></a> to load the chunk in
6524 1.1 mbalmer the zero-terminated string <code>s</code>.
6525 1.1 mbalmer
6526 1.1 mbalmer
6527 1.1 mbalmer <p>
6528 1.1 mbalmer This function returns the same results as <a href="#lua_load"><code>lua_load</code></a>.
6529 1.1 mbalmer
6530 1.1 mbalmer
6531 1.1 mbalmer <p>
6532 1.1 mbalmer Also as <a href="#lua_load"><code>lua_load</code></a>, this function only loads the chunk;
6533 1.1 mbalmer it does not run it.
6534 1.1 mbalmer
6535 1.1 mbalmer
6536 1.1 mbalmer
6537 1.1 mbalmer
6538 1.1 mbalmer
6539 1.2 lneto <hr><h3><a name="luaL_newlib"><code>luaL_newlib</code></a></h3><p>
6540 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6541 1.3 lneto <pre>void luaL_newlib (lua_State *L, const luaL_Reg l[]);</pre>
6542 1.2 lneto
6543 1.2 lneto <p>
6544 1.2 lneto Creates a new table and registers there
6545 1.2 lneto the functions in list <code>l</code>.
6546 1.3 lneto
6547 1.3 lneto
6548 1.3 lneto <p>
6549 1.2 lneto It is implemented as the following macro:
6550 1.2 lneto
6551 1.2 lneto <pre>
6552 1.2 lneto (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
6553 1.3 lneto </pre><p>
6554 1.3 lneto The array <code>l</code> must be the actual array,
6555 1.3 lneto not a pointer to it.
6556 1.3 lneto
6557 1.2 lneto
6558 1.2 lneto
6559 1.2 lneto
6560 1.2 lneto
6561 1.2 lneto <hr><h3><a name="luaL_newlibtable"><code>luaL_newlibtable</code></a></h3><p>
6562 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6563 1.2 lneto <pre>void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);</pre>
6564 1.2 lneto
6565 1.2 lneto <p>
6566 1.2 lneto Creates a new table with a size optimized
6567 1.2 lneto to store all entries in the array <code>l</code>
6568 1.2 lneto (but does not actually store them).
6569 1.2 lneto It is intended to be used in conjunction with <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>
6570 1.2 lneto (see <a href="#luaL_newlib"><code>luaL_newlib</code></a>).
6571 1.2 lneto
6572 1.2 lneto
6573 1.2 lneto <p>
6574 1.2 lneto It is implemented as a macro.
6575 1.2 lneto The array <code>l</code> must be the actual array,
6576 1.2 lneto not a pointer to it.
6577 1.2 lneto
6578 1.2 lneto
6579 1.2 lneto
6580 1.2 lneto
6581 1.2 lneto
6582 1.1 mbalmer <hr><h3><a name="luaL_newmetatable"><code>luaL_newmetatable</code></a></h3><p>
6583 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6584 1.1 mbalmer <pre>int luaL_newmetatable (lua_State *L, const char *tname);</pre>
6585 1.1 mbalmer
6586 1.1 mbalmer <p>
6587 1.1 mbalmer If the registry already has the key <code>tname</code>,
6588 1.1 mbalmer returns 0.
6589 1.1 mbalmer Otherwise,
6590 1.1 mbalmer creates a new table to be used as a metatable for userdata,
6591 1.2 lneto adds to this new table the pair <code>__name = tname</code>,
6592 1.2 lneto adds to the registry the pair <code>[tname] = new table</code>,
6593 1.1 mbalmer and returns 1.
6594 1.3 lneto (The entry <code>__name</code> is used by some error-reporting functions.)
6595 1.1 mbalmer
6596 1.1 mbalmer
6597 1.1 mbalmer <p>
6598 1.1 mbalmer In both cases pushes onto the stack the final value associated
6599 1.1 mbalmer with <code>tname</code> in the registry.
6600 1.1 mbalmer
6601 1.1 mbalmer
6602 1.1 mbalmer
6603 1.1 mbalmer
6604 1.1 mbalmer
6605 1.1 mbalmer <hr><h3><a name="luaL_newstate"><code>luaL_newstate</code></a></h3><p>
6606 1.2 lneto <span class="apii">[-0, +0, –]</span>
6607 1.1 mbalmer <pre>lua_State *luaL_newstate (void);</pre>
6608 1.1 mbalmer
6609 1.1 mbalmer <p>
6610 1.1 mbalmer Creates a new Lua state.
6611 1.1 mbalmer It calls <a href="#lua_newstate"><code>lua_newstate</code></a> with an
6612 1.1 mbalmer allocator based on the standard C <code>realloc</code> function
6613 1.2 lneto and then sets a panic function (see <a href="#4.6">§4.6</a>) that prints
6614 1.1 mbalmer an error message to the standard error output in case of fatal
6615 1.1 mbalmer errors.
6616 1.1 mbalmer
6617 1.1 mbalmer
6618 1.1 mbalmer <p>
6619 1.1 mbalmer Returns the new state,
6620 1.1 mbalmer or <code>NULL</code> if there is a memory allocation error.
6621 1.1 mbalmer
6622 1.1 mbalmer
6623 1.1 mbalmer
6624 1.1 mbalmer
6625 1.1 mbalmer
6626 1.1 mbalmer <hr><h3><a name="luaL_openlibs"><code>luaL_openlibs</code></a></h3><p>
6627 1.2 lneto <span class="apii">[-0, +0, <em>e</em>]</span>
6628 1.1 mbalmer <pre>void luaL_openlibs (lua_State *L);</pre>
6629 1.1 mbalmer
6630 1.1 mbalmer <p>
6631 1.1 mbalmer Opens all standard Lua libraries into the given state.
6632 1.1 mbalmer
6633 1.1 mbalmer
6634 1.1 mbalmer
6635 1.1 mbalmer
6636 1.1 mbalmer
6637 1.1 mbalmer <hr><h3><a name="luaL_optinteger"><code>luaL_optinteger</code></a></h3><p>
6638 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6639 1.1 mbalmer <pre>lua_Integer luaL_optinteger (lua_State *L,
6640 1.2 lneto int arg,
6641 1.1 mbalmer lua_Integer d);</pre>
6642 1.1 mbalmer
6643 1.1 mbalmer <p>
6644 1.2 lneto If the function argument <code>arg</code> is an integer
6645 1.2 lneto (or convertible to an integer),
6646 1.2 lneto returns this integer.
6647 1.1 mbalmer If this argument is absent or is <b>nil</b>,
6648 1.1 mbalmer returns <code>d</code>.
6649 1.1 mbalmer Otherwise, raises an error.
6650 1.1 mbalmer
6651 1.1 mbalmer
6652 1.1 mbalmer
6653 1.1 mbalmer
6654 1.1 mbalmer
6655 1.1 mbalmer <hr><h3><a name="luaL_optlstring"><code>luaL_optlstring</code></a></h3><p>
6656 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6657 1.1 mbalmer <pre>const char *luaL_optlstring (lua_State *L,
6658 1.2 lneto int arg,
6659 1.1 mbalmer const char *d,
6660 1.1 mbalmer size_t *l);</pre>
6661 1.1 mbalmer
6662 1.1 mbalmer <p>
6663 1.2 lneto If the function argument <code>arg</code> is a string,
6664 1.1 mbalmer returns this string.
6665 1.1 mbalmer If this argument is absent or is <b>nil</b>,
6666 1.1 mbalmer returns <code>d</code>.
6667 1.1 mbalmer Otherwise, raises an error.
6668 1.1 mbalmer
6669 1.1 mbalmer
6670 1.1 mbalmer <p>
6671 1.1 mbalmer If <code>l</code> is not <code>NULL</code>,
6672 1.2 lneto fills the position <code>*l</code> with the result's length.
6673 1.5 lneto If the result is <code>NULL</code>
6674 1.5 lneto (only possible when returning <code>d</code> and <code>d == NULL</code>),
6675 1.5 lneto its length is considered zero.
6676 1.1 mbalmer
6677 1.1 mbalmer
6678 1.1 mbalmer
6679 1.1 mbalmer
6680 1.1 mbalmer
6681 1.1 mbalmer <hr><h3><a name="luaL_optnumber"><code>luaL_optnumber</code></a></h3><p>
6682 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6683 1.2 lneto <pre>lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);</pre>
6684 1.1 mbalmer
6685 1.1 mbalmer <p>
6686 1.2 lneto If the function argument <code>arg</code> is a number,
6687 1.1 mbalmer returns this number.
6688 1.1 mbalmer If this argument is absent or is <b>nil</b>,
6689 1.1 mbalmer returns <code>d</code>.
6690 1.1 mbalmer Otherwise, raises an error.
6691 1.1 mbalmer
6692 1.1 mbalmer
6693 1.1 mbalmer
6694 1.1 mbalmer
6695 1.1 mbalmer
6696 1.1 mbalmer <hr><h3><a name="luaL_optstring"><code>luaL_optstring</code></a></h3><p>
6697 1.1 mbalmer <span class="apii">[-0, +0, <em>v</em>]</span>
6698 1.1 mbalmer <pre>const char *luaL_optstring (lua_State *L,
6699 1.2 lneto int arg,
6700 1.1 mbalmer const char *d);</pre>
6701 1.1 mbalmer
6702 1.1 mbalmer <p>
6703 1.2 lneto If the function argument <code>arg</code> is a string,
6704 1.1 mbalmer returns this string.
6705 1.1 mbalmer If this argument is absent or is <b>nil</b>,
6706 1.1 mbalmer returns <code>d</code>.
6707 1.1 mbalmer Otherwise, raises an error.
6708 1.1 mbalmer
6709 1.1 mbalmer
6710 1.1 mbalmer
6711 1.1 mbalmer
6712 1.1 mbalmer
6713 1.1 mbalmer <hr><h3><a name="luaL_prepbuffer"><code>luaL_prepbuffer</code></a></h3><p>
6714 1.5 lneto <span class="apii">[-?, +?, <em>m</em>]</span>
6715 1.1 mbalmer <pre>char *luaL_prepbuffer (luaL_Buffer *B);</pre>
6716 1.1 mbalmer
6717 1.1 mbalmer <p>
6718 1.2 lneto Equivalent to <a href="#luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a>
6719 1.2 lneto with the predefined size <a name="pdf-LUAL_BUFFERSIZE"><code>LUAL_BUFFERSIZE</code></a>.
6720 1.2 lneto
6721 1.2 lneto
6722 1.2 lneto
6723 1.2 lneto
6724 1.2 lneto
6725 1.2 lneto <hr><h3><a name="luaL_prepbuffsize"><code>luaL_prepbuffsize</code></a></h3><p>
6726 1.5 lneto <span class="apii">[-?, +?, <em>m</em>]</span>
6727 1.2 lneto <pre>char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);</pre>
6728 1.2 lneto
6729 1.2 lneto <p>
6730 1.2 lneto Returns an address to a space of size <code>sz</code>
6731 1.1 mbalmer where you can copy a string to be added to buffer <code>B</code>
6732 1.1 mbalmer (see <a href="#luaL_Buffer"><code>luaL_Buffer</code></a>).
6733 1.1 mbalmer After copying the string into this space you must call
6734 1.2 lneto <a href="#luaL_addsize"><code>luaL_addsize</code></a> with the size of the string to actually add
6735 1.1 mbalmer it to the buffer.
6736 1.1 mbalmer
6737 1.1 mbalmer
6738 1.1 mbalmer
6739 1.1 mbalmer
6740 1.1 mbalmer
6741 1.1 mbalmer <hr><h3><a name="luaL_pushresult"><code>luaL_pushresult</code></a></h3><p>
6742 1.5 lneto <span class="apii">[-?, +1, <em>m</em>]</span>
6743 1.1 mbalmer <pre>void luaL_pushresult (luaL_Buffer *B);</pre>
6744 1.1 mbalmer
6745 1.1 mbalmer <p>
6746 1.1 mbalmer Finishes the use of buffer <code>B</code> leaving the final string on
6747 1.1 mbalmer the top of the stack.
6748 1.1 mbalmer
6749 1.1 mbalmer
6750 1.1 mbalmer
6751 1.1 mbalmer
6752 1.1 mbalmer
6753 1.2 lneto <hr><h3><a name="luaL_pushresultsize"><code>luaL_pushresultsize</code></a></h3><p>
6754 1.5 lneto <span class="apii">[-?, +1, <em>m</em>]</span>
6755 1.2 lneto <pre>void luaL_pushresultsize (luaL_Buffer *B, size_t sz);</pre>
6756 1.2 lneto
6757 1.2 lneto <p>
6758 1.2 lneto Equivalent to the sequence <a href="#luaL_addsize"><code>luaL_addsize</code></a>, <a href="#luaL_pushresult"><code>luaL_pushresult</code></a>.
6759 1.2 lneto
6760 1.2 lneto
6761 1.2 lneto
6762 1.2 lneto
6763 1.2 lneto
6764 1.1 mbalmer <hr><h3><a name="luaL_ref"><code>luaL_ref</code></a></h3><p>
6765 1.5 lneto <span class="apii">[-1, +0, <em>m</em>]</span>
6766 1.1 mbalmer <pre>int luaL_ref (lua_State *L, int t);</pre>
6767 1.1 mbalmer
6768 1.1 mbalmer <p>
6769 1.1 mbalmer Creates and returns a <em>reference</em>,
6770 1.1 mbalmer in the table at index <code>t</code>,
6771 1.1 mbalmer for the object at the top of the stack (and pops the object).
6772 1.1 mbalmer
6773 1.1 mbalmer
6774 1.1 mbalmer <p>
6775 1.1 mbalmer A reference is a unique integer key.
6776 1.1 mbalmer As long as you do not manually add integer keys into table <code>t</code>,
6777 1.1 mbalmer <a href="#luaL_ref"><code>luaL_ref</code></a> ensures the uniqueness of the key it returns.
6778 1.1 mbalmer You can retrieve an object referred by reference <code>r</code>
6779 1.1 mbalmer by calling <code>lua_rawgeti(L, t, r)</code>.
6780 1.1 mbalmer Function <a href="#luaL_unref"><code>luaL_unref</code></a> frees a reference and its associated object.
6781 1.1 mbalmer
6782 1.1 mbalmer
6783 1.1 mbalmer <p>
6784 1.1 mbalmer If the object at the top of the stack is <b>nil</b>,
6785 1.1 mbalmer <a href="#luaL_ref"><code>luaL_ref</code></a> returns the constant <a name="pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>.
6786 1.1 mbalmer The constant <a name="pdf-LUA_NOREF"><code>LUA_NOREF</code></a> is guaranteed to be different
6787 1.1 mbalmer from any reference returned by <a href="#luaL_ref"><code>luaL_ref</code></a>.
6788 1.1 mbalmer
6789 1.1 mbalmer
6790 1.1 mbalmer
6791 1.1 mbalmer
6792 1.1 mbalmer
6793 1.1 mbalmer <hr><h3><a name="luaL_Reg"><code>luaL_Reg</code></a></h3>
6794 1.1 mbalmer <pre>typedef struct luaL_Reg {
6795 1.1 mbalmer const char *name;
6796 1.1 mbalmer lua_CFunction func;
6797 1.1 mbalmer } luaL_Reg;</pre>
6798 1.1 mbalmer
6799 1.1 mbalmer <p>
6800 1.1 mbalmer Type for arrays of functions to be registered by
6801 1.2 lneto <a href="#luaL_setfuncs"><code>luaL_setfuncs</code></a>.
6802 1.1 mbalmer <code>name</code> is the function name and <code>func</code> is a pointer to
6803 1.1 mbalmer the function.
6804 1.3 lneto Any array of <a href="#luaL_Reg"><code>luaL_Reg</code></a> must end with a sentinel entry
6805 1.1 mbalmer in which both <code>name</code> and <code>func</code> are <code>NULL</code>.
6806 1.1 mbalmer
6807 1.1 mbalmer
6808 1.1 mbalmer
6809 1.1 mbalmer
6810 1.1 mbalmer
6811 1.2 lneto <hr><h3><a name="luaL_requiref"><code>luaL_requiref</code></a></h3><p>
6812 1.2 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
6813 1.2 lneto <pre>void luaL_requiref (lua_State *L, const char *modname,
6814 1.2 lneto lua_CFunction openf, int glb);</pre>
6815 1.2 lneto
6816 1.2 lneto <p>
6817 1.3 lneto If <code>modname</code> is not already present in <a href="#pdf-package.loaded"><code>package.loaded</code></a>,
6818 1.3 lneto calls function <code>openf</code> with string <code>modname</code> as an argument
6819 1.2 lneto and sets the call result in <code>package.loaded[modname]</code>,
6820 1.2 lneto as if that function has been called through <a href="#pdf-require"><code>require</code></a>.
6821 1.2 lneto
6822 1.2 lneto
6823 1.2 lneto <p>
6824 1.2 lneto If <code>glb</code> is true,
6825 1.3 lneto also stores the module into global <code>modname</code>.
6826 1.2 lneto
6827 1.2 lneto
6828 1.2 lneto <p>
6829 1.3 lneto Leaves a copy of the module on the stack.
6830 1.2 lneto
6831 1.2 lneto
6832 1.2 lneto
6833 1.2 lneto
6834 1.2 lneto
6835 1.2 lneto <hr><h3><a name="luaL_setfuncs"><code>luaL_setfuncs</code></a></h3><p>
6836 1.5 lneto <span class="apii">[-nup, +0, <em>m</em>]</span>
6837 1.2 lneto <pre>void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);</pre>
6838 1.2 lneto
6839 1.2 lneto <p>
6840 1.2 lneto Registers all functions in the array <code>l</code>
6841 1.2 lneto (see <a href="#luaL_Reg"><code>luaL_Reg</code></a>) into the table on the top of the stack
6842 1.2 lneto (below optional upvalues, see next).
6843 1.2 lneto
6844 1.1 mbalmer
6845 1.1 mbalmer <p>
6846 1.2 lneto When <code>nup</code> is not zero,
6847 1.2 lneto all functions are created sharing <code>nup</code> upvalues,
6848 1.2 lneto which must be previously pushed on the stack
6849 1.2 lneto on top of the library table.
6850 1.2 lneto These values are popped from the stack after the registration.
6851 1.1 mbalmer
6852 1.1 mbalmer
6853 1.2 lneto
6854 1.2 lneto
6855 1.2 lneto
6856 1.2 lneto <hr><h3><a name="luaL_setmetatable"><code>luaL_setmetatable</code></a></h3><p>
6857 1.2 lneto <span class="apii">[-0, +0, –]</span>
6858 1.2 lneto <pre>void luaL_setmetatable (lua_State *L, const char *tname);</pre>
6859 1.2 lneto
6860 1.2 lneto <p>
6861 1.2 lneto Sets the metatable of the object at the top of the stack
6862 1.2 lneto as the metatable associated with name <code>tname</code>
6863 1.2 lneto in the registry (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6864 1.2 lneto
6865 1.2 lneto
6866 1.2 lneto
6867 1.2 lneto
6868 1.2 lneto
6869 1.2 lneto <hr><h3><a name="luaL_Stream"><code>luaL_Stream</code></a></h3>
6870 1.2 lneto <pre>typedef struct luaL_Stream {
6871 1.2 lneto FILE *f;
6872 1.2 lneto lua_CFunction closef;
6873 1.2 lneto } luaL_Stream;</pre>
6874 1.2 lneto
6875 1.1 mbalmer <p>
6876 1.2 lneto The standard representation for file handles,
6877 1.2 lneto which is used by the standard I/O library.
6878 1.1 mbalmer
6879 1.1 mbalmer
6880 1.1 mbalmer <p>
6881 1.2 lneto A file handle is implemented as a full userdata,
6882 1.3 lneto with a metatable called <code>LUA_FILEHANDLE</code>
6883 1.3 lneto (where <code>LUA_FILEHANDLE</code> is a macro with the actual metatable's name).
6884 1.2 lneto The metatable is created by the I/O library
6885 1.2 lneto (see <a href="#luaL_newmetatable"><code>luaL_newmetatable</code></a>).
6886 1.2 lneto
6887 1.2 lneto
6888 1.2 lneto <p>
6889 1.2 lneto This userdata must start with the structure <code>luaL_Stream</code>;
6890 1.2 lneto it can contain other data after this initial structure.
6891 1.2 lneto Field <code>f</code> points to the corresponding C stream
6892 1.2 lneto (or it can be <code>NULL</code> to indicate an incompletely created handle).
6893 1.2 lneto Field <code>closef</code> points to a Lua function
6894 1.2 lneto that will be called to close the stream
6895 1.2 lneto when the handle is closed or collected;
6896 1.2 lneto this function receives the file handle as its sole argument and
6897 1.2 lneto must return either <b>true</b> (in case of success)
6898 1.2 lneto or <b>nil</b> plus an error message (in case of error).
6899 1.2 lneto Once Lua calls this field,
6900 1.5 lneto it changes the field value to <code>NULL</code>
6901 1.3 lneto to signal that the handle is closed.
6902 1.2 lneto
6903 1.1 mbalmer
6904 1.1 mbalmer
6905 1.2 lneto
6906 1.2 lneto
6907 1.2 lneto <hr><h3><a name="luaL_testudata"><code>luaL_testudata</code></a></h3><p>
6908 1.5 lneto <span class="apii">[-0, +0, <em>m</em>]</span>
6909 1.2 lneto <pre>void *luaL_testudata (lua_State *L, int arg, const char *tname);</pre>
6910 1.2 lneto
6911 1.1 mbalmer <p>
6912 1.2 lneto This function works like <a href="#luaL_checkudata"><code>luaL_checkudata</code></a>,
6913 1.2 lneto except that, when the test fails,
6914 1.2 lneto it returns <code>NULL</code> instead of raising an error.
6915 1.1 mbalmer
6916 1.1 mbalmer
6917 1.1 mbalmer
6918 1.1 mbalmer
6919 1.1 mbalmer
6920 1.2 lneto <hr><h3><a name="luaL_tolstring"><code>luaL_tolstring</code></a></h3><p>
6921 1.2 lneto <span class="apii">[-0, +1, <em>e</em>]</span>
6922 1.2 lneto <pre>const char *luaL_tolstring (lua_State *L, int idx, size_t *len);</pre>
6923 1.2 lneto
6924 1.2 lneto <p>
6925 1.2 lneto Converts any Lua value at the given index to a C string
6926 1.2 lneto in a reasonable format.
6927 1.2 lneto The resulting string is pushed onto the stack and also
6928 1.2 lneto returned by the function.
6929 1.2 lneto If <code>len</code> is not <code>NULL</code>,
6930 1.2 lneto the function also sets <code>*len</code> with the string length.
6931 1.2 lneto
6932 1.1 mbalmer
6933 1.1 mbalmer <p>
6934 1.2 lneto If the value has a metatable with a <code>"__tostring"</code> field,
6935 1.2 lneto then <code>luaL_tolstring</code> calls the corresponding metamethod
6936 1.2 lneto with the value as argument,
6937 1.2 lneto and uses the result of the call as its result.
6938 1.1 mbalmer
6939 1.1 mbalmer
6940 1.1 mbalmer
6941 1.1 mbalmer
6942 1.1 mbalmer
6943 1.2 lneto <hr><h3><a name="luaL_traceback"><code>luaL_traceback</code></a></h3><p>
6944 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6945 1.2 lneto <pre>void luaL_traceback (lua_State *L, lua_State *L1, const char *msg,
6946 1.2 lneto int level);</pre>
6947 1.1 mbalmer
6948 1.1 mbalmer <p>
6949 1.2 lneto Creates and pushes a traceback of the stack <code>L1</code>.
6950 1.2 lneto If <code>msg</code> is not <code>NULL</code> it is appended
6951 1.2 lneto at the beginning of the traceback.
6952 1.2 lneto The <code>level</code> parameter tells at which level
6953 1.2 lneto to start the traceback.
6954 1.1 mbalmer
6955 1.2 lneto
6956 1.2 lneto
6957 1.2 lneto
6958 1.2 lneto
6959 1.2 lneto <hr><h3><a name="luaL_typename"><code>luaL_typename</code></a></h3><p>
6960 1.2 lneto <span class="apii">[-0, +0, –]</span>
6961 1.2 lneto <pre>const char *luaL_typename (lua_State *L, int index);</pre>
6962 1.2 lneto
6963 1.2 lneto <p>
6964 1.2 lneto Returns the name of the type of the value at the given index.
6965 1.1 mbalmer
6966 1.1 mbalmer
6967 1.1 mbalmer
6968 1.1 mbalmer
6969 1.1 mbalmer
6970 1.1 mbalmer <hr><h3><a name="luaL_unref"><code>luaL_unref</code></a></h3><p>
6971 1.2 lneto <span class="apii">[-0, +0, –]</span>
6972 1.1 mbalmer <pre>void luaL_unref (lua_State *L, int t, int ref);</pre>
6973 1.1 mbalmer
6974 1.1 mbalmer <p>
6975 1.1 mbalmer Releases reference <code>ref</code> from the table at index <code>t</code>
6976 1.1 mbalmer (see <a href="#luaL_ref"><code>luaL_ref</code></a>).
6977 1.1 mbalmer The entry is removed from the table,
6978 1.1 mbalmer so that the referred object can be collected.
6979 1.1 mbalmer The reference <code>ref</code> is also freed to be used again.
6980 1.1 mbalmer
6981 1.1 mbalmer
6982 1.1 mbalmer <p>
6983 1.1 mbalmer If <code>ref</code> is <a href="#pdf-LUA_NOREF"><code>LUA_NOREF</code></a> or <a href="#pdf-LUA_REFNIL"><code>LUA_REFNIL</code></a>,
6984 1.1 mbalmer <a href="#luaL_unref"><code>luaL_unref</code></a> does nothing.
6985 1.1 mbalmer
6986 1.1 mbalmer
6987 1.1 mbalmer
6988 1.1 mbalmer
6989 1.1 mbalmer
6990 1.1 mbalmer <hr><h3><a name="luaL_where"><code>luaL_where</code></a></h3><p>
6991 1.5 lneto <span class="apii">[-0, +1, <em>m</em>]</span>
6992 1.1 mbalmer <pre>void luaL_where (lua_State *L, int lvl);</pre>
6993 1.1 mbalmer
6994 1.1 mbalmer <p>
6995 1.1 mbalmer Pushes onto the stack a string identifying the current position
6996 1.1 mbalmer of the control at level <code>lvl</code> in the call stack.
6997 1.1 mbalmer Typically this string has the following format:
6998 1.1 mbalmer
6999 1.1 mbalmer <pre>
7000 1.1 mbalmer <em>chunkname</em>:<em>currentline</em>:
7001 1.1 mbalmer </pre><p>
7002 1.1 mbalmer Level 0 is the running function,
7003 1.1 mbalmer level 1 is the function that called the running function,
7004 1.1 mbalmer etc.
7005 1.1 mbalmer
7006 1.1 mbalmer
7007 1.1 mbalmer <p>
7008 1.1 mbalmer This function is used to build a prefix for error messages.
7009 1.1 mbalmer
7010 1.1 mbalmer
7011 1.1 mbalmer
7012 1.1 mbalmer
7013 1.1 mbalmer
7014 1.1 mbalmer
7015 1.1 mbalmer
7016 1.2 lneto <h1>6 – <a name="6">Standard Libraries</a></h1>
7017 1.1 mbalmer
7018 1.1 mbalmer <p>
7019 1.1 mbalmer The standard Lua libraries provide useful functions
7020 1.1 mbalmer that are implemented directly through the C API.
7021 1.1 mbalmer Some of these functions provide essential services to the language
7022 1.1 mbalmer (e.g., <a href="#pdf-type"><code>type</code></a> and <a href="#pdf-getmetatable"><code>getmetatable</code></a>);
7023 1.1 mbalmer others provide access to "outside" services (e.g., I/O);
7024 1.1 mbalmer and others could be implemented in Lua itself,
7025 1.1 mbalmer but are quite useful or have critical performance requirements that
7026 1.1 mbalmer deserve an implementation in C (e.g., <a href="#pdf-table.sort"><code>table.sort</code></a>).
7027 1.1 mbalmer
7028 1.1 mbalmer
7029 1.1 mbalmer <p>
7030 1.1 mbalmer All libraries are implemented through the official C API
7031 1.1 mbalmer and are provided as separate C modules.
7032 1.1 mbalmer Currently, Lua has the following standard libraries:
7033 1.1 mbalmer
7034 1.1 mbalmer <ul>
7035 1.1 mbalmer
7036 1.2 lneto <li>basic library (<a href="#6.1">§6.1</a>);</li>
7037 1.1 mbalmer
7038 1.2 lneto <li>coroutine library (<a href="#6.2">§6.2</a>);</li>
7039 1.1 mbalmer
7040 1.2 lneto <li>package library (<a href="#6.3">§6.3</a>);</li>
7041 1.1 mbalmer
7042 1.2 lneto <li>string manipulation (<a href="#6.4">§6.4</a>);</li>
7043 1.1 mbalmer
7044 1.2 lneto <li>basic UTF-8 support (<a href="#6.5">§6.5</a>);</li>
7045 1.1 mbalmer
7046 1.2 lneto <li>table manipulation (<a href="#6.6">§6.6</a>);</li>
7047 1.1 mbalmer
7048 1.2 lneto <li>mathematical functions (<a href="#6.7">§6.7</a>) (sin, log, etc.);</li>
7049 1.1 mbalmer
7050 1.2 lneto <li>input and output (<a href="#6.8">§6.8</a>);</li>
7051 1.2 lneto
7052 1.2 lneto <li>operating system facilities (<a href="#6.9">§6.9</a>);</li>
7053 1.2 lneto
7054 1.2 lneto <li>debug facilities (<a href="#6.10">§6.10</a>).</li>
7055 1.1 mbalmer
7056 1.1 mbalmer </ul><p>
7057 1.2 lneto Except for the basic and the package libraries,
7058 1.1 mbalmer each library provides all its functions as fields of a global table
7059 1.1 mbalmer or as methods of its objects.
7060 1.1 mbalmer
7061 1.1 mbalmer
7062 1.1 mbalmer <p>
7063 1.1 mbalmer To have access to these libraries,
7064 1.1 mbalmer the C host program should call the <a href="#luaL_openlibs"><code>luaL_openlibs</code></a> function,
7065 1.1 mbalmer which opens all standard libraries.
7066 1.1 mbalmer Alternatively,
7067 1.2 lneto the host program can open them individually by using
7068 1.2 lneto <a href="#luaL_requiref"><code>luaL_requiref</code></a> to call
7069 1.1 mbalmer <a name="pdf-luaopen_base"><code>luaopen_base</code></a> (for the basic library),
7070 1.1 mbalmer <a name="pdf-luaopen_package"><code>luaopen_package</code></a> (for the package library),
7071 1.2 lneto <a name="pdf-luaopen_coroutine"><code>luaopen_coroutine</code></a> (for the coroutine library),
7072 1.1 mbalmer <a name="pdf-luaopen_string"><code>luaopen_string</code></a> (for the string library),
7073 1.3 lneto <a name="pdf-luaopen_utf8"><code>luaopen_utf8</code></a> (for the UTF8 library),
7074 1.1 mbalmer <a name="pdf-luaopen_table"><code>luaopen_table</code></a> (for the table library),
7075 1.1 mbalmer <a name="pdf-luaopen_math"><code>luaopen_math</code></a> (for the mathematical library),
7076 1.1 mbalmer <a name="pdf-luaopen_io"><code>luaopen_io</code></a> (for the I/O library),
7077 1.3 lneto <a name="pdf-luaopen_os"><code>luaopen_os</code></a> (for the operating system library),
7078 1.1 mbalmer and <a name="pdf-luaopen_debug"><code>luaopen_debug</code></a> (for the debug library).
7079 1.2 lneto These functions are declared in <a name="pdf-lualib.h"><code>lualib.h</code></a>.
7080 1.1 mbalmer
7081 1.1 mbalmer
7082 1.1 mbalmer
7083 1.2 lneto <h2>6.1 – <a name="6.1">Basic Functions</a></h2>
7084 1.1 mbalmer
7085 1.1 mbalmer <p>
7086 1.2 lneto The basic library provides core functions to Lua.
7087 1.1 mbalmer If you do not include this library in your application,
7088 1.2 lneto you should check carefully whether you need to provide
7089 1.1 mbalmer implementations for some of its facilities.
7090 1.1 mbalmer
7091 1.1 mbalmer
7092 1.1 mbalmer <p>
7093 1.1 mbalmer <hr><h3><a name="pdf-assert"><code>assert (v [, message])</code></a></h3>
7094 1.2 lneto
7095 1.2 lneto
7096 1.2 lneto <p>
7097 1.2 lneto Calls <a href="#pdf-error"><code>error</code></a> if
7098 1.1 mbalmer the value of its argument <code>v</code> is false (i.e., <b>nil</b> or <b>false</b>);
7099 1.1 mbalmer otherwise, returns all its arguments.
7100 1.2 lneto In case of error,
7101 1.2 lneto <code>message</code> is the error object;
7102 1.2 lneto when absent, it defaults to "<code>assertion failed!</code>"
7103 1.1 mbalmer
7104 1.1 mbalmer
7105 1.1 mbalmer
7106 1.1 mbalmer
7107 1.1 mbalmer <p>
7108 1.2 lneto <hr><h3><a name="pdf-collectgarbage"><code>collectgarbage ([opt [, arg]])</code></a></h3>
7109 1.1 mbalmer
7110 1.1 mbalmer
7111 1.1 mbalmer <p>
7112 1.1 mbalmer This function is a generic interface to the garbage collector.
7113 1.1 mbalmer It performs different functions according to its first argument, <code>opt</code>:
7114 1.1 mbalmer
7115 1.1 mbalmer <ul>
7116 1.1 mbalmer
7117 1.2 lneto <li><b>"<code>collect</code>": </b>
7118 1.2 lneto performs a full garbage-collection cycle.
7119 1.2 lneto This is the default option.
7120 1.1 mbalmer </li>
7121 1.1 mbalmer
7122 1.2 lneto <li><b>"<code>stop</code>": </b>
7123 1.2 lneto stops automatic execution of the garbage collector.
7124 1.2 lneto The collector will run only when explicitly invoked,
7125 1.2 lneto until a call to restart it.
7126 1.1 mbalmer </li>
7127 1.1 mbalmer
7128 1.2 lneto <li><b>"<code>restart</code>": </b>
7129 1.2 lneto restarts automatic execution of the garbage collector.
7130 1.1 mbalmer </li>
7131 1.1 mbalmer
7132 1.2 lneto <li><b>"<code>count</code>": </b>
7133 1.2 lneto returns the total memory in use by Lua in Kbytes.
7134 1.2 lneto The value has a fractional part,
7135 1.2 lneto so that it multiplied by 1024
7136 1.2 lneto gives the exact number of bytes in use by Lua
7137 1.2 lneto (except for overflows).
7138 1.1 mbalmer </li>
7139 1.1 mbalmer
7140 1.2 lneto <li><b>"<code>step</code>": </b>
7141 1.1 mbalmer performs a garbage-collection step.
7142 1.2 lneto The step "size" is controlled by <code>arg</code>.
7143 1.2 lneto With a zero value,
7144 1.2 lneto the collector will perform one basic (indivisible) step.
7145 1.2 lneto For non-zero values,
7146 1.2 lneto the collector will perform as if that amount of memory
7147 1.2 lneto (in KBytes) had been allocated by Lua.
7148 1.1 mbalmer Returns <b>true</b> if the step finished a collection cycle.
7149 1.1 mbalmer </li>
7150 1.1 mbalmer
7151 1.2 lneto <li><b>"<code>setpause</code>": </b>
7152 1.1 mbalmer sets <code>arg</code> as the new value for the <em>pause</em> of
7153 1.2 lneto the collector (see <a href="#2.5">§2.5</a>).
7154 1.1 mbalmer Returns the previous value for <em>pause</em>.
7155 1.1 mbalmer </li>
7156 1.1 mbalmer
7157 1.2 lneto <li><b>"<code>setstepmul</code>": </b>
7158 1.1 mbalmer sets <code>arg</code> as the new value for the <em>step multiplier</em> of
7159 1.2 lneto the collector (see <a href="#2.5">§2.5</a>).
7160 1.1 mbalmer Returns the previous value for <em>step</em>.
7161 1.1 mbalmer </li>
7162 1.1 mbalmer
7163 1.2 lneto <li><b>"<code>isrunning</code>": </b>
7164 1.2 lneto returns a boolean that tells whether the collector is running
7165 1.2 lneto (i.e., not stopped).
7166 1.2 lneto </li>
7167 1.2 lneto
7168 1.1 mbalmer </ul>
7169 1.1 mbalmer
7170 1.1 mbalmer
7171 1.1 mbalmer
7172 1.1 mbalmer <p>
7173 1.2 lneto <hr><h3><a name="pdf-dofile"><code>dofile ([filename])</code></a></h3>
7174 1.1 mbalmer Opens the named file and executes its contents as a Lua chunk.
7175 1.1 mbalmer When called without arguments,
7176 1.1 mbalmer <code>dofile</code> executes the contents of the standard input (<code>stdin</code>).
7177 1.1 mbalmer Returns all values returned by the chunk.
7178 1.1 mbalmer In case of errors, <code>dofile</code> propagates the error
7179 1.1 mbalmer to its caller (that is, <code>dofile</code> does not run in protected mode).
7180 1.1 mbalmer
7181 1.1 mbalmer
7182 1.1 mbalmer
7183 1.1 mbalmer
7184 1.1 mbalmer <p>
7185 1.1 mbalmer <hr><h3><a name="pdf-error"><code>error (message [, level])</code></a></h3>
7186 1.1 mbalmer Terminates the last protected function called
7187 1.2 lneto and returns <code>message</code> as the error object.
7188 1.1 mbalmer Function <code>error</code> never returns.
7189 1.1 mbalmer
7190 1.1 mbalmer
7191 1.1 mbalmer <p>
7192 1.1 mbalmer Usually, <code>error</code> adds some information about the error position
7193 1.2 lneto at the beginning of the message, if the message is a string.
7194 1.1 mbalmer The <code>level</code> argument specifies how to get the error position.
7195 1.1 mbalmer With level 1 (the default), the error position is where the
7196 1.1 mbalmer <code>error</code> function was called.
7197 1.1 mbalmer Level 2 points the error to where the function
7198 1.1 mbalmer that called <code>error</code> was called; and so on.
7199 1.1 mbalmer Passing a level 0 avoids the addition of error position information
7200 1.1 mbalmer to the message.
7201 1.1 mbalmer
7202 1.1 mbalmer
7203 1.1 mbalmer
7204 1.1 mbalmer
7205 1.1 mbalmer <p>
7206 1.1 mbalmer <hr><h3><a name="pdf-_G"><code>_G</code></a></h3>
7207 1.1 mbalmer A global variable (not a function) that
7208 1.2 lneto holds the global environment (see <a href="#2.2">§2.2</a>).
7209 1.1 mbalmer Lua itself does not use this variable;
7210 1.1 mbalmer changing its value does not affect any environment,
7211 1.2 lneto nor vice versa.
7212 1.1 mbalmer
7213 1.1 mbalmer
7214 1.1 mbalmer
7215 1.1 mbalmer
7216 1.1 mbalmer <p>
7217 1.1 mbalmer <hr><h3><a name="pdf-getmetatable"><code>getmetatable (object)</code></a></h3>
7218 1.1 mbalmer
7219 1.1 mbalmer
7220 1.1 mbalmer <p>
7221 1.1 mbalmer If <code>object</code> does not have a metatable, returns <b>nil</b>.
7222 1.1 mbalmer Otherwise,
7223 1.1 mbalmer if the object's metatable has a <code>"__metatable"</code> field,
7224 1.1 mbalmer returns the associated value.
7225 1.1 mbalmer Otherwise, returns the metatable of the given object.
7226 1.1 mbalmer
7227 1.1 mbalmer
7228 1.1 mbalmer
7229 1.1 mbalmer
7230 1.1 mbalmer <p>
7231 1.1 mbalmer <hr><h3><a name="pdf-ipairs"><code>ipairs (t)</code></a></h3>
7232 1.1 mbalmer
7233 1.1 mbalmer
7234 1.1 mbalmer <p>
7235 1.3 lneto Returns three values (an iterator function, the table <code>t</code>, and 0)
7236 1.1 mbalmer so that the construction
7237 1.1 mbalmer
7238 1.1 mbalmer <pre>
7239 1.1 mbalmer for i,v in ipairs(t) do <em>body</em> end
7240 1.1 mbalmer </pre><p>
7241 1.3 lneto will iterate over the key–value pairs
7242 1.3 lneto (<code>1,t[1]</code>), (<code>2,t[2]</code>), ...,
7243 1.3 lneto up to the first nil value.
7244 1.1 mbalmer
7245 1.1 mbalmer
7246 1.1 mbalmer
7247 1.1 mbalmer
7248 1.1 mbalmer <p>
7249 1.3 lneto <hr><h3><a name="pdf-load"><code>load (chunk [, chunkname [, mode [, env]]])</code></a></h3>
7250 1.1 mbalmer
7251 1.1 mbalmer
7252 1.1 mbalmer <p>
7253 1.2 lneto Loads a chunk.
7254 1.2 lneto
7255 1.2 lneto
7256 1.2 lneto <p>
7257 1.3 lneto If <code>chunk</code> is a string, the chunk is this string.
7258 1.3 lneto If <code>chunk</code> is a function,
7259 1.2 lneto <code>load</code> calls it repeatedly to get the chunk pieces.
7260 1.3 lneto Each call to <code>chunk</code> must return a string that concatenates
7261 1.1 mbalmer with previous results.
7262 1.1 mbalmer A return of an empty string, <b>nil</b>, or no value signals the end of the chunk.
7263 1.1 mbalmer
7264 1.1 mbalmer
7265 1.1 mbalmer <p>
7266 1.2 lneto If there are no syntactic errors,
7267 1.1 mbalmer returns the compiled chunk as a function;
7268 1.1 mbalmer otherwise, returns <b>nil</b> plus the error message.
7269 1.1 mbalmer
7270 1.1 mbalmer
7271 1.1 mbalmer <p>
7272 1.2 lneto If the resulting function has upvalues,
7273 1.2 lneto the first upvalue is set to the value of <code>env</code>,
7274 1.2 lneto if that parameter is given,
7275 1.2 lneto or to the value of the global environment.
7276 1.3 lneto Other upvalues are initialized with <b>nil</b>.
7277 1.2 lneto (When you load a main chunk,
7278 1.2 lneto the resulting function will always have exactly one upvalue,
7279 1.2 lneto the <code>_ENV</code> variable (see <a href="#2.2">§2.2</a>).
7280 1.3 lneto However,
7281 1.3 lneto when you load a binary chunk created from a function (see <a href="#pdf-string.dump"><code>string.dump</code></a>),
7282 1.3 lneto the resulting function can have an arbitrary number of upvalues.)
7283 1.3 lneto All upvalues are fresh, that is,
7284 1.3 lneto they are not shared with any other function.
7285 1.1 mbalmer
7286 1.1 mbalmer
7287 1.2 lneto <p>
7288 1.3 lneto <code>chunkname</code> is used as the name of the chunk for error messages
7289 1.2 lneto and debug information (see <a href="#4.9">§4.9</a>).
7290 1.2 lneto When absent,
7291 1.3 lneto it defaults to <code>chunk</code>, if <code>chunk</code> is a string,
7292 1.2 lneto or to "<code>=(load)</code>" otherwise.
7293 1.1 mbalmer
7294 1.1 mbalmer
7295 1.1 mbalmer <p>
7296 1.2 lneto The string <code>mode</code> controls whether the chunk can be text or binary
7297 1.2 lneto (that is, a precompiled chunk).
7298 1.2 lneto It may be the string "<code>b</code>" (only binary chunks),
7299 1.2 lneto "<code>t</code>" (only text chunks),
7300 1.2 lneto or "<code>bt</code>" (both binary and text).
7301 1.2 lneto The default is "<code>bt</code>".
7302 1.1 mbalmer
7303 1.1 mbalmer
7304 1.1 mbalmer <p>
7305 1.2 lneto Lua does not check the consistency of binary chunks.
7306 1.2 lneto Maliciously crafted binary chunks can crash
7307 1.2 lneto the interpreter.
7308 1.1 mbalmer
7309 1.1 mbalmer
7310 1.1 mbalmer
7311 1.1 mbalmer
7312 1.1 mbalmer <p>
7313 1.2 lneto <hr><h3><a name="pdf-loadfile"><code>loadfile ([filename [, mode [, env]]])</code></a></h3>
7314 1.1 mbalmer
7315 1.1 mbalmer
7316 1.1 mbalmer <p>
7317 1.1 mbalmer Similar to <a href="#pdf-load"><code>load</code></a>,
7318 1.2 lneto but gets the chunk from file <code>filename</code>
7319 1.2 lneto or from the standard input,
7320 1.2 lneto if no file name is given.
7321 1.1 mbalmer
7322 1.1 mbalmer
7323 1.1 mbalmer
7324 1.1 mbalmer
7325 1.1 mbalmer <p>
7326 1.1 mbalmer <hr><h3><a name="pdf-next"><code>next (table [, index])</code></a></h3>
7327 1.1 mbalmer
7328 1.1 mbalmer
7329 1.1 mbalmer <p>
7330 1.1 mbalmer Allows a program to traverse all fields of a table.
7331 1.1 mbalmer Its first argument is a table and its second argument
7332 1.1 mbalmer is an index in this table.
7333 1.1 mbalmer <code>next</code> returns the next index of the table
7334 1.1 mbalmer and its associated value.
7335 1.1 mbalmer When called with <b>nil</b> as its second argument,
7336 1.1 mbalmer <code>next</code> returns an initial index
7337 1.1 mbalmer and its associated value.
7338 1.1 mbalmer When called with the last index,
7339 1.1 mbalmer or with <b>nil</b> in an empty table,
7340 1.1 mbalmer <code>next</code> returns <b>nil</b>.
7341 1.1 mbalmer If the second argument is absent, then it is interpreted as <b>nil</b>.
7342 1.1 mbalmer In particular,
7343 1.1 mbalmer you can use <code>next(t)</code> to check whether a table is empty.
7344 1.1 mbalmer
7345 1.1 mbalmer
7346 1.1 mbalmer <p>
7347 1.1 mbalmer The order in which the indices are enumerated is not specified,
7348 1.1 mbalmer <em>even for numeric indices</em>.
7349 1.4 mbalmer (To traverse a table in numerical order,
7350 1.2 lneto use a numerical <b>for</b>.)
7351 1.1 mbalmer
7352 1.1 mbalmer
7353 1.1 mbalmer <p>
7354 1.2 lneto The behavior of <code>next</code> is undefined if,
7355 1.1 mbalmer during the traversal,
7356 1.1 mbalmer you assign any value to a non-existent field in the table.
7357 1.1 mbalmer You may however modify existing fields.
7358 1.1 mbalmer In particular, you may clear existing fields.
7359 1.1 mbalmer
7360 1.1 mbalmer
7361 1.1 mbalmer
7362 1.1 mbalmer
7363 1.1 mbalmer <p>
7364 1.1 mbalmer <hr><h3><a name="pdf-pairs"><code>pairs (t)</code></a></h3>
7365 1.1 mbalmer
7366 1.1 mbalmer
7367 1.1 mbalmer <p>
7368 1.2 lneto If <code>t</code> has a metamethod <code>__pairs</code>,
7369 1.2 lneto calls it with <code>t</code> as argument and returns the first three
7370 1.2 lneto results from the call.
7371 1.2 lneto
7372 1.2 lneto
7373 1.2 lneto <p>
7374 1.2 lneto Otherwise,
7375 1.2 lneto returns three values: the <a href="#pdf-next"><code>next</code></a> function, the table <code>t</code>, and <b>nil</b>,
7376 1.1 mbalmer so that the construction
7377 1.1 mbalmer
7378 1.1 mbalmer <pre>
7379 1.1 mbalmer for k,v in pairs(t) do <em>body</em> end
7380 1.1 mbalmer </pre><p>
7381 1.1 mbalmer will iterate over all key–value pairs of table <code>t</code>.
7382 1.1 mbalmer
7383 1.1 mbalmer
7384 1.1 mbalmer <p>
7385 1.1 mbalmer See function <a href="#pdf-next"><code>next</code></a> for the caveats of modifying
7386 1.1 mbalmer the table during its traversal.
7387 1.1 mbalmer
7388 1.1 mbalmer
7389 1.1 mbalmer
7390 1.1 mbalmer
7391 1.1 mbalmer <p>
7392 1.2 lneto <hr><h3><a name="pdf-pcall"><code>pcall (f [, arg1, ···])</code></a></h3>
7393 1.1 mbalmer
7394 1.1 mbalmer
7395 1.1 mbalmer <p>
7396 1.1 mbalmer Calls function <code>f</code> with
7397 1.1 mbalmer the given arguments in <em>protected mode</em>.
7398 1.1 mbalmer This means that any error inside <code>f</code> is not propagated;
7399 1.1 mbalmer instead, <code>pcall</code> catches the error
7400 1.1 mbalmer and returns a status code.
7401 1.1 mbalmer Its first result is the status code (a boolean),
7402 1.1 mbalmer which is true if the call succeeds without errors.
7403 1.1 mbalmer In such case, <code>pcall</code> also returns all results from the call,
7404 1.1 mbalmer after this first result.
7405 1.1 mbalmer In case of any error, <code>pcall</code> returns <b>false</b> plus the error message.
7406 1.1 mbalmer
7407 1.1 mbalmer
7408 1.1 mbalmer
7409 1.1 mbalmer
7410 1.1 mbalmer <p>
7411 1.1 mbalmer <hr><h3><a name="pdf-print"><code>print (···)</code></a></h3>
7412 1.2 lneto Receives any number of arguments
7413 1.1 mbalmer and prints their values to <code>stdout</code>,
7414 1.2 lneto using the <a href="#pdf-tostring"><code>tostring</code></a> function to convert each argument to a string.
7415 1.1 mbalmer <code>print</code> is not intended for formatted output,
7416 1.1 mbalmer but only as a quick way to show a value,
7417 1.2 lneto for instance for debugging.
7418 1.2 lneto For complete control over the output,
7419 1.2 lneto use <a href="#pdf-string.format"><code>string.format</code></a> and <a href="#pdf-io.write"><code>io.write</code></a>.
7420 1.1 mbalmer
7421 1.1 mbalmer
7422 1.1 mbalmer
7423 1.1 mbalmer
7424 1.1 mbalmer <p>
7425 1.1 mbalmer <hr><h3><a name="pdf-rawequal"><code>rawequal (v1, v2)</code></a></h3>
7426 1.1 mbalmer Checks whether <code>v1</code> is equal to <code>v2</code>,
7427 1.1 mbalmer without invoking any metamethod.
7428 1.1 mbalmer Returns a boolean.
7429 1.1 mbalmer
7430 1.1 mbalmer
7431 1.1 mbalmer
7432 1.1 mbalmer
7433 1.1 mbalmer <p>
7434 1.1 mbalmer <hr><h3><a name="pdf-rawget"><code>rawget (table, index)</code></a></h3>
7435 1.1 mbalmer Gets the real value of <code>table[index]</code>,
7436 1.1 mbalmer without invoking any metamethod.
7437 1.1 mbalmer <code>table</code> must be a table;
7438 1.1 mbalmer <code>index</code> may be any value.
7439 1.1 mbalmer
7440 1.1 mbalmer
7441 1.1 mbalmer
7442 1.1 mbalmer
7443 1.1 mbalmer <p>
7444 1.2 lneto <hr><h3><a name="pdf-rawlen"><code>rawlen (v)</code></a></h3>
7445 1.2 lneto Returns the length of the object <code>v</code>,
7446 1.2 lneto which must be a table or a string,
7447 1.2 lneto without invoking any metamethod.
7448 1.2 lneto Returns an integer.
7449 1.2 lneto
7450 1.2 lneto
7451 1.2 lneto
7452 1.2 lneto
7453 1.2 lneto <p>
7454 1.1 mbalmer <hr><h3><a name="pdf-rawset"><code>rawset (table, index, value)</code></a></h3>
7455 1.1 mbalmer Sets the real value of <code>table[index]</code> to <code>value</code>,
7456 1.1 mbalmer without invoking any metamethod.
7457 1.1 mbalmer <code>table</code> must be a table,
7458 1.2 lneto <code>index</code> any value different from <b>nil</b> and NaN,
7459 1.1 mbalmer and <code>value</code> any Lua value.
7460 1.1 mbalmer
7461 1.1 mbalmer
7462 1.1 mbalmer <p>
7463 1.1 mbalmer This function returns <code>table</code>.
7464 1.1 mbalmer
7465 1.1 mbalmer
7466 1.1 mbalmer
7467 1.1 mbalmer
7468 1.1 mbalmer <p>
7469 1.1 mbalmer <hr><h3><a name="pdf-select"><code>select (index, ···)</code></a></h3>
7470 1.1 mbalmer
7471 1.1 mbalmer
7472 1.1 mbalmer <p>
7473 1.1 mbalmer If <code>index</code> is a number,
7474 1.2 lneto returns all arguments after argument number <code>index</code>;
7475 1.2 lneto a negative number indexes from the end (-1 is the last argument).
7476 1.1 mbalmer Otherwise, <code>index</code> must be the string <code>"#"</code>,
7477 1.1 mbalmer and <code>select</code> returns the total number of extra arguments it received.
7478 1.1 mbalmer
7479 1.1 mbalmer
7480 1.1 mbalmer
7481 1.1 mbalmer
7482 1.1 mbalmer <p>
7483 1.1 mbalmer <hr><h3><a name="pdf-setmetatable"><code>setmetatable (table, metatable)</code></a></h3>
7484 1.1 mbalmer
7485 1.1 mbalmer
7486 1.1 mbalmer <p>
7487 1.1 mbalmer Sets the metatable for the given table.
7488 1.5 lneto (To change the metatable of other types from Lua code,
7489 1.5 lneto you must use the debug library (<a href="#6.10">§6.10</a>).)
7490 1.1 mbalmer If <code>metatable</code> is <b>nil</b>,
7491 1.1 mbalmer removes the metatable of the given table.
7492 1.1 mbalmer If the original metatable has a <code>"__metatable"</code> field,
7493 1.1 mbalmer raises an error.
7494 1.1 mbalmer
7495 1.1 mbalmer
7496 1.1 mbalmer <p>
7497 1.1 mbalmer This function returns <code>table</code>.
7498 1.1 mbalmer
7499 1.1 mbalmer
7500 1.1 mbalmer
7501 1.1 mbalmer
7502 1.1 mbalmer <p>
7503 1.1 mbalmer <hr><h3><a name="pdf-tonumber"><code>tonumber (e [, base])</code></a></h3>
7504 1.2 lneto
7505 1.2 lneto
7506 1.2 lneto <p>
7507 1.2 lneto When called with no <code>base</code>,
7508 1.2 lneto <code>tonumber</code> tries to convert its argument to a number.
7509 1.2 lneto If the argument is already a number or
7510 1.2 lneto a string convertible to a number,
7511 1.2 lneto then <code>tonumber</code> returns this number;
7512 1.1 mbalmer otherwise, it returns <b>nil</b>.
7513 1.1 mbalmer
7514 1.1 mbalmer
7515 1.1 mbalmer <p>
7516 1.2 lneto The conversion of strings can result in integers or floats,
7517 1.2 lneto according to the lexical conventions of Lua (see <a href="#3.1">§3.1</a>).
7518 1.2 lneto (The string may have leading and trailing spaces and a sign.)
7519 1.2 lneto
7520 1.2 lneto
7521 1.2 lneto <p>
7522 1.2 lneto When called with <code>base</code>,
7523 1.2 lneto then <code>e</code> must be a string to be interpreted as
7524 1.2 lneto an integer numeral in that base.
7525 1.1 mbalmer The base may be any integer between 2 and 36, inclusive.
7526 1.1 mbalmer In bases above 10, the letter '<code>A</code>' (in either upper or lower case)
7527 1.1 mbalmer represents 10, '<code>B</code>' represents 11, and so forth,
7528 1.1 mbalmer with '<code>Z</code>' representing 35.
7529 1.2 lneto If the string <code>e</code> is not a valid numeral in the given base,
7530 1.2 lneto the function returns <b>nil</b>.
7531 1.1 mbalmer
7532 1.1 mbalmer
7533 1.1 mbalmer
7534 1.1 mbalmer
7535 1.1 mbalmer <p>
7536 1.2 lneto <hr><h3><a name="pdf-tostring"><code>tostring (v)</code></a></h3>
7537 1.2 lneto Receives a value of any type and
7538 1.2 lneto converts it to a string in a human-readable format.
7539 1.2 lneto (For complete control of how numbers are converted,
7540 1.2 lneto use <a href="#pdf-string.format"><code>string.format</code></a>.)
7541 1.1 mbalmer
7542 1.1 mbalmer
7543 1.1 mbalmer <p>
7544 1.2 lneto If the metatable of <code>v</code> has a <code>"__tostring"</code> field,
7545 1.1 mbalmer then <code>tostring</code> calls the corresponding value
7546 1.2 lneto with <code>v</code> as argument,
7547 1.1 mbalmer and uses the result of the call as its result.
7548 1.1 mbalmer
7549 1.1 mbalmer
7550 1.1 mbalmer
7551 1.1 mbalmer
7552 1.1 mbalmer <p>
7553 1.1 mbalmer <hr><h3><a name="pdf-type"><code>type (v)</code></a></h3>
7554 1.1 mbalmer Returns the type of its only argument, coded as a string.
7555 1.1 mbalmer The possible results of this function are
7556 1.1 mbalmer "<code>nil</code>" (a string, not the value <b>nil</b>),
7557 1.1 mbalmer "<code>number</code>",
7558 1.1 mbalmer "<code>string</code>",
7559 1.1 mbalmer "<code>boolean</code>",
7560 1.1 mbalmer "<code>table</code>",
7561 1.1 mbalmer "<code>function</code>",
7562 1.1 mbalmer "<code>thread</code>",
7563 1.1 mbalmer and "<code>userdata</code>".
7564 1.1 mbalmer
7565 1.1 mbalmer
7566 1.1 mbalmer
7567 1.1 mbalmer
7568 1.1 mbalmer <p>
7569 1.1 mbalmer <hr><h3><a name="pdf-_VERSION"><code>_VERSION</code></a></h3>
7570 1.5 lneto
7571 1.5 lneto
7572 1.5 lneto <p>
7573 1.1 mbalmer A global variable (not a function) that
7574 1.5 lneto holds a string containing the running Lua version.
7575 1.2 lneto The current value of this variable is "<code>Lua 5.3</code>".
7576 1.1 mbalmer
7577 1.1 mbalmer
7578 1.1 mbalmer
7579 1.1 mbalmer
7580 1.1 mbalmer <p>
7581 1.2 lneto <hr><h3><a name="pdf-xpcall"><code>xpcall (f, msgh [, arg1, ···])</code></a></h3>
7582 1.1 mbalmer
7583 1.1 mbalmer
7584 1.1 mbalmer <p>
7585 1.1 mbalmer This function is similar to <a href="#pdf-pcall"><code>pcall</code></a>,
7586 1.2 lneto except that it sets a new message handler <code>msgh</code>.
7587 1.1 mbalmer
7588 1.1 mbalmer
7589 1.1 mbalmer
7590 1.1 mbalmer
7591 1.1 mbalmer
7592 1.1 mbalmer
7593 1.1 mbalmer
7594 1.2 lneto <h2>6.2 – <a name="6.2">Coroutine Manipulation</a></h2>
7595 1.1 mbalmer
7596 1.1 mbalmer <p>
7597 1.4 mbalmer This library comprises the operations to manipulate coroutines,
7598 1.4 mbalmer which come inside the table <a name="pdf-coroutine"><code>coroutine</code></a>.
7599 1.2 lneto See <a href="#2.6">§2.6</a> for a general description of coroutines.
7600 1.1 mbalmer
7601 1.1 mbalmer
7602 1.1 mbalmer <p>
7603 1.1 mbalmer <hr><h3><a name="pdf-coroutine.create"><code>coroutine.create (f)</code></a></h3>
7604 1.1 mbalmer
7605 1.1 mbalmer
7606 1.1 mbalmer <p>
7607 1.1 mbalmer Creates a new coroutine, with body <code>f</code>.
7608 1.4 mbalmer <code>f</code> must be a function.
7609 1.2 lneto Returns this new coroutine,
7610 1.2 lneto an object with type <code>"thread"</code>.
7611 1.2 lneto
7612 1.2 lneto
7613 1.2 lneto
7614 1.2 lneto
7615 1.2 lneto <p>
7616 1.2 lneto <hr><h3><a name="pdf-coroutine.isyieldable"><code>coroutine.isyieldable ()</code></a></h3>
7617 1.2 lneto
7618 1.2 lneto
7619 1.2 lneto <p>
7620 1.2 lneto Returns true when the running coroutine can yield.
7621 1.2 lneto
7622 1.2 lneto
7623 1.2 lneto <p>
7624 1.2 lneto A running coroutine is yieldable if it is not the main thread and
7625 1.2 lneto it is not inside a non-yieldable C function.
7626 1.1 mbalmer
7627 1.1 mbalmer
7628 1.1 mbalmer
7629 1.1 mbalmer
7630 1.1 mbalmer <p>
7631 1.1 mbalmer <hr><h3><a name="pdf-coroutine.resume"><code>coroutine.resume (co [, val1, ···])</code></a></h3>
7632 1.1 mbalmer
7633 1.1 mbalmer
7634 1.1 mbalmer <p>
7635 1.1 mbalmer Starts or continues the execution of coroutine <code>co</code>.
7636 1.1 mbalmer The first time you resume a coroutine,
7637 1.1 mbalmer it starts running its body.
7638 1.2 lneto The values <code>val1</code>, ... are passed
7639 1.1 mbalmer as the arguments to the body function.
7640 1.1 mbalmer If the coroutine has yielded,
7641 1.1 mbalmer <code>resume</code> restarts it;
7642 1.2 lneto the values <code>val1</code>, ... are passed
7643 1.1 mbalmer as the results from the yield.
7644 1.1 mbalmer
7645 1.1 mbalmer
7646 1.1 mbalmer <p>
7647 1.1 mbalmer If the coroutine runs without any errors,
7648 1.1 mbalmer <code>resume</code> returns <b>true</b> plus any values passed to <code>yield</code>
7649 1.2 lneto (when the coroutine yields) or any values returned by the body function
7650 1.2 lneto (when the coroutine terminates).
7651 1.1 mbalmer If there is any error,
7652 1.1 mbalmer <code>resume</code> returns <b>false</b> plus the error message.
7653 1.1 mbalmer
7654 1.1 mbalmer
7655 1.1 mbalmer
7656 1.1 mbalmer
7657 1.1 mbalmer <p>
7658 1.1 mbalmer <hr><h3><a name="pdf-coroutine.running"><code>coroutine.running ()</code></a></h3>
7659 1.1 mbalmer
7660 1.1 mbalmer
7661 1.1 mbalmer <p>
7662 1.2 lneto Returns the running coroutine plus a boolean,
7663 1.2 lneto true when the running coroutine is the main one.
7664 1.1 mbalmer
7665 1.1 mbalmer
7666 1.1 mbalmer
7667 1.1 mbalmer
7668 1.1 mbalmer <p>
7669 1.1 mbalmer <hr><h3><a name="pdf-coroutine.status"><code>coroutine.status (co)</code></a></h3>
7670 1.1 mbalmer
7671 1.1 mbalmer
7672 1.1 mbalmer <p>
7673 1.1 mbalmer Returns the status of coroutine <code>co</code>, as a string:
7674 1.1 mbalmer <code>"running"</code>,
7675 1.1 mbalmer if the coroutine is running (that is, it called <code>status</code>);
7676 1.1 mbalmer <code>"suspended"</code>, if the coroutine is suspended in a call to <code>yield</code>,
7677 1.1 mbalmer or if it has not started running yet;
7678 1.1 mbalmer <code>"normal"</code> if the coroutine is active but not running
7679 1.1 mbalmer (that is, it has resumed another coroutine);
7680 1.1 mbalmer and <code>"dead"</code> if the coroutine has finished its body function,
7681 1.1 mbalmer or if it has stopped with an error.
7682 1.1 mbalmer
7683 1.1 mbalmer
7684 1.1 mbalmer
7685 1.1 mbalmer
7686 1.1 mbalmer <p>
7687 1.1 mbalmer <hr><h3><a name="pdf-coroutine.wrap"><code>coroutine.wrap (f)</code></a></h3>
7688 1.1 mbalmer
7689 1.1 mbalmer
7690 1.1 mbalmer <p>
7691 1.1 mbalmer Creates a new coroutine, with body <code>f</code>.
7692 1.4 mbalmer <code>f</code> must be a function.
7693 1.1 mbalmer Returns a function that resumes the coroutine each time it is called.
7694 1.1 mbalmer Any arguments passed to the function behave as the
7695 1.1 mbalmer extra arguments to <code>resume</code>.
7696 1.1 mbalmer Returns the same values returned by <code>resume</code>,
7697 1.1 mbalmer except the first boolean.
7698 1.1 mbalmer In case of error, propagates the error.
7699 1.1 mbalmer
7700 1.1 mbalmer
7701 1.1 mbalmer
7702 1.1 mbalmer
7703 1.1 mbalmer <p>
7704 1.1 mbalmer <hr><h3><a name="pdf-coroutine.yield"><code>coroutine.yield (···)</code></a></h3>
7705 1.1 mbalmer
7706 1.1 mbalmer
7707 1.1 mbalmer <p>
7708 1.1 mbalmer Suspends the execution of the calling coroutine.
7709 1.1 mbalmer Any arguments to <code>yield</code> are passed as extra results to <code>resume</code>.
7710 1.1 mbalmer
7711 1.1 mbalmer
7712 1.1 mbalmer
7713 1.1 mbalmer
7714 1.1 mbalmer
7715 1.1 mbalmer
7716 1.1 mbalmer
7717 1.2 lneto <h2>6.3 – <a name="6.3">Modules</a></h2>
7718 1.1 mbalmer
7719 1.1 mbalmer <p>
7720 1.1 mbalmer The package library provides basic
7721 1.2 lneto facilities for loading modules in Lua.
7722 1.2 lneto It exports one function directly in the global environment:
7723 1.2 lneto <a href="#pdf-require"><code>require</code></a>.
7724 1.1 mbalmer Everything else is exported in a table <a name="pdf-package"><code>package</code></a>.
7725 1.1 mbalmer
7726 1.1 mbalmer
7727 1.1 mbalmer <p>
7728 1.1 mbalmer <hr><h3><a name="pdf-require"><code>require (modname)</code></a></h3>
7729 1.1 mbalmer
7730 1.1 mbalmer
7731 1.1 mbalmer <p>
7732 1.1 mbalmer Loads the given module.
7733 1.1 mbalmer The function starts by looking into the <a href="#pdf-package.loaded"><code>package.loaded</code></a> table
7734 1.1 mbalmer to determine whether <code>modname</code> is already loaded.
7735 1.1 mbalmer If it is, then <code>require</code> returns the value stored
7736 1.1 mbalmer at <code>package.loaded[modname]</code>.
7737 1.1 mbalmer Otherwise, it tries to find a <em>loader</em> for the module.
7738 1.1 mbalmer
7739 1.1 mbalmer
7740 1.1 mbalmer <p>
7741 1.1 mbalmer To find a loader,
7742 1.2 lneto <code>require</code> is guided by the <a href="#pdf-package.searchers"><code>package.searchers</code></a> sequence.
7743 1.2 lneto By changing this sequence,
7744 1.1 mbalmer we can change how <code>require</code> looks for a module.
7745 1.1 mbalmer The following explanation is based on the default configuration
7746 1.2 lneto for <a href="#pdf-package.searchers"><code>package.searchers</code></a>.
7747 1.1 mbalmer
7748 1.1 mbalmer
7749 1.1 mbalmer <p>
7750 1.1 mbalmer First <code>require</code> queries <code>package.preload[modname]</code>.
7751 1.1 mbalmer If it has a value,
7752 1.2 lneto this value (which must be a function) is the loader.
7753 1.1 mbalmer Otherwise <code>require</code> searches for a Lua loader using the
7754 1.1 mbalmer path stored in <a href="#pdf-package.path"><code>package.path</code></a>.
7755 1.1 mbalmer If that also fails, it searches for a C loader using the
7756 1.1 mbalmer path stored in <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
7757 1.1 mbalmer If that also fails,
7758 1.2 lneto it tries an <em>all-in-one</em> loader (see <a href="#pdf-package.searchers"><code>package.searchers</code></a>).
7759 1.1 mbalmer
7760 1.1 mbalmer
7761 1.1 mbalmer <p>
7762 1.1 mbalmer Once a loader is found,
7763 1.2 lneto <code>require</code> calls the loader with two arguments:
7764 1.2 lneto <code>modname</code> and an extra value dependent on how it got the loader.
7765 1.2 lneto (If the loader came from a file,
7766 1.2 lneto this extra value is the file name.)
7767 1.2 lneto If the loader returns any non-nil value,
7768 1.1 mbalmer <code>require</code> assigns the returned value to <code>package.loaded[modname]</code>.
7769 1.2 lneto If the loader does not return a non-nil value and
7770 1.1 mbalmer has not assigned any value to <code>package.loaded[modname]</code>,
7771 1.1 mbalmer then <code>require</code> assigns <b>true</b> to this entry.
7772 1.1 mbalmer In any case, <code>require</code> returns the
7773 1.1 mbalmer final value of <code>package.loaded[modname]</code>.
7774 1.1 mbalmer
7775 1.1 mbalmer
7776 1.1 mbalmer <p>
7777 1.1 mbalmer If there is any error loading or running the module,
7778 1.1 mbalmer or if it cannot find any loader for the module,
7779 1.2 lneto then <code>require</code> raises an error.
7780 1.2 lneto
7781 1.2 lneto
7782 1.2 lneto
7783 1.2 lneto
7784 1.2 lneto <p>
7785 1.2 lneto <hr><h3><a name="pdf-package.config"><code>package.config</code></a></h3>
7786 1.2 lneto
7787 1.2 lneto
7788 1.2 lneto <p>
7789 1.2 lneto A string describing some compile-time configurations for packages.
7790 1.2 lneto This string is a sequence of lines:
7791 1.2 lneto
7792 1.2 lneto <ul>
7793 1.2 lneto
7794 1.2 lneto <li>The first line is the directory separator string.
7795 1.2 lneto Default is '<code>\</code>' for Windows and '<code>/</code>' for all other systems.</li>
7796 1.1 mbalmer
7797 1.2 lneto <li>The second line is the character that separates templates in a path.
7798 1.2 lneto Default is '<code>;</code>'.</li>
7799 1.2 lneto
7800 1.2 lneto <li>The third line is the string that marks the
7801 1.2 lneto substitution points in a template.
7802 1.2 lneto Default is '<code>?</code>'.</li>
7803 1.2 lneto
7804 1.2 lneto <li>The fourth line is a string that, in a path in Windows,
7805 1.2 lneto is replaced by the executable's directory.
7806 1.2 lneto Default is '<code>!</code>'.</li>
7807 1.2 lneto
7808 1.3 lneto <li>The fifth line is a mark to ignore all text after it
7809 1.2 lneto when building the <code>luaopen_</code> function name.
7810 1.2 lneto Default is '<code>-</code>'.</li>
7811 1.2 lneto
7812 1.2 lneto </ul>
7813 1.1 mbalmer
7814 1.1 mbalmer
7815 1.1 mbalmer
7816 1.1 mbalmer <p>
7817 1.1 mbalmer <hr><h3><a name="pdf-package.cpath"><code>package.cpath</code></a></h3>
7818 1.1 mbalmer
7819 1.1 mbalmer
7820 1.1 mbalmer <p>
7821 1.1 mbalmer The path used by <a href="#pdf-require"><code>require</code></a> to search for a C loader.
7822 1.1 mbalmer
7823 1.1 mbalmer
7824 1.1 mbalmer <p>
7825 1.1 mbalmer Lua initializes the C path <a href="#pdf-package.cpath"><code>package.cpath</code></a> in the same way
7826 1.1 mbalmer it initializes the Lua path <a href="#pdf-package.path"><code>package.path</code></a>,
7827 1.2 lneto using the environment variable <a name="pdf-LUA_CPATH_5_3"><code>LUA_CPATH_5_3</code></a>
7828 1.2 lneto or the environment variable <a name="pdf-LUA_CPATH"><code>LUA_CPATH</code></a>
7829 1.1 mbalmer or a default path defined in <code>luaconf.h</code>.
7830 1.1 mbalmer
7831 1.1 mbalmer
7832 1.1 mbalmer
7833 1.1 mbalmer
7834 1.1 mbalmer <p>
7835 1.1 mbalmer <hr><h3><a name="pdf-package.loaded"><code>package.loaded</code></a></h3>
7836 1.1 mbalmer
7837 1.1 mbalmer
7838 1.1 mbalmer <p>
7839 1.1 mbalmer A table used by <a href="#pdf-require"><code>require</code></a> to control which
7840 1.1 mbalmer modules are already loaded.
7841 1.1 mbalmer When you require a module <code>modname</code> and
7842 1.1 mbalmer <code>package.loaded[modname]</code> is not false,
7843 1.1 mbalmer <a href="#pdf-require"><code>require</code></a> simply returns the value stored there.
7844 1.1 mbalmer
7845 1.1 mbalmer
7846 1.2 lneto <p>
7847 1.2 lneto This variable is only a reference to the real table;
7848 1.2 lneto assignments to this variable do not change the
7849 1.2 lneto table used by <a href="#pdf-require"><code>require</code></a>.
7850 1.2 lneto
7851 1.2 lneto
7852 1.2 lneto
7853 1.2 lneto
7854 1.2 lneto <p>
7855 1.2 lneto <hr><h3><a name="pdf-package.loadlib"><code>package.loadlib (libname, funcname)</code></a></h3>
7856 1.2 lneto
7857 1.2 lneto
7858 1.2 lneto <p>
7859 1.2 lneto Dynamically links the host program with the C library <code>libname</code>.
7860 1.2 lneto
7861 1.2 lneto
7862 1.2 lneto <p>
7863 1.2 lneto If <code>funcname</code> is "<code>*</code>",
7864 1.2 lneto then it only links with the library,
7865 1.2 lneto making the symbols exported by the library
7866 1.2 lneto available to other dynamically linked libraries.
7867 1.2 lneto Otherwise,
7868 1.2 lneto it looks for a function <code>funcname</code> inside the library
7869 1.2 lneto and returns this function as a C function.
7870 1.2 lneto So, <code>funcname</code> must follow the <a href="#lua_CFunction"><code>lua_CFunction</code></a> prototype
7871 1.2 lneto (see <a href="#lua_CFunction"><code>lua_CFunction</code></a>).
7872 1.2 lneto
7873 1.2 lneto
7874 1.2 lneto <p>
7875 1.2 lneto This is a low-level function.
7876 1.2 lneto It completely bypasses the package and module system.
7877 1.2 lneto Unlike <a href="#pdf-require"><code>require</code></a>,
7878 1.2 lneto it does not perform any path searching and
7879 1.2 lneto does not automatically adds extensions.
7880 1.2 lneto <code>libname</code> must be the complete file name of the C library,
7881 1.2 lneto including if necessary a path and an extension.
7882 1.2 lneto <code>funcname</code> must be the exact name exported by the C library
7883 1.2 lneto (which may depend on the C compiler and linker used).
7884 1.2 lneto
7885 1.2 lneto
7886 1.2 lneto <p>
7887 1.2 lneto This function is not supported by Standard C.
7888 1.2 lneto As such, it is only available on some platforms
7889 1.2 lneto (Windows, Linux, Mac OS X, Solaris, BSD,
7890 1.2 lneto plus other Unix systems that support the <code>dlfcn</code> standard).
7891 1.2 lneto
7892 1.2 lneto
7893 1.2 lneto
7894 1.2 lneto
7895 1.2 lneto <p>
7896 1.2 lneto <hr><h3><a name="pdf-package.path"><code>package.path</code></a></h3>
7897 1.2 lneto
7898 1.2 lneto
7899 1.2 lneto <p>
7900 1.2 lneto The path used by <a href="#pdf-require"><code>require</code></a> to search for a Lua loader.
7901 1.2 lneto
7902 1.2 lneto
7903 1.2 lneto <p>
7904 1.2 lneto At start-up, Lua initializes this variable with
7905 1.2 lneto the value of the environment variable <a name="pdf-LUA_PATH_5_3"><code>LUA_PATH_5_3</code></a> or
7906 1.2 lneto the environment variable <a name="pdf-LUA_PATH"><code>LUA_PATH</code></a> or
7907 1.2 lneto with a default path defined in <code>luaconf.h</code>,
7908 1.2 lneto if those environment variables are not defined.
7909 1.2 lneto Any "<code>;;</code>" in the value of the environment variable
7910 1.2 lneto is replaced by the default path.
7911 1.2 lneto
7912 1.2 lneto
7913 1.2 lneto
7914 1.2 lneto
7915 1.2 lneto <p>
7916 1.2 lneto <hr><h3><a name="pdf-package.preload"><code>package.preload</code></a></h3>
7917 1.2 lneto
7918 1.2 lneto
7919 1.2 lneto <p>
7920 1.2 lneto A table to store loaders for specific modules
7921 1.2 lneto (see <a href="#pdf-require"><code>require</code></a>).
7922 1.2 lneto
7923 1.2 lneto
7924 1.2 lneto <p>
7925 1.2 lneto This variable is only a reference to the real table;
7926 1.2 lneto assignments to this variable do not change the
7927 1.2 lneto table used by <a href="#pdf-require"><code>require</code></a>.
7928 1.2 lneto
7929 1.2 lneto
7930 1.1 mbalmer
7931 1.1 mbalmer
7932 1.1 mbalmer <p>
7933 1.2 lneto <hr><h3><a name="pdf-package.searchers"><code>package.searchers</code></a></h3>
7934 1.1 mbalmer
7935 1.1 mbalmer
7936 1.1 mbalmer <p>
7937 1.1 mbalmer A table used by <a href="#pdf-require"><code>require</code></a> to control how to load modules.
7938 1.1 mbalmer
7939 1.1 mbalmer
7940 1.1 mbalmer <p>
7941 1.1 mbalmer Each entry in this table is a <em>searcher function</em>.
7942 1.1 mbalmer When looking for a module,
7943 1.1 mbalmer <a href="#pdf-require"><code>require</code></a> calls each of these searchers in ascending order,
7944 1.1 mbalmer with the module name (the argument given to <a href="#pdf-require"><code>require</code></a>) as its
7945 1.1 mbalmer sole parameter.
7946 1.1 mbalmer The function can return another function (the module <em>loader</em>)
7947 1.2 lneto plus an extra value that will be passed to that loader,
7948 1.1 mbalmer or a string explaining why it did not find that module
7949 1.1 mbalmer (or <b>nil</b> if it has nothing to say).
7950 1.2 lneto
7951 1.2 lneto
7952 1.2 lneto <p>
7953 1.2 lneto Lua initializes this table with four searcher functions.
7954 1.1 mbalmer
7955 1.1 mbalmer
7956 1.1 mbalmer <p>
7957 1.1 mbalmer The first searcher simply looks for a loader in the
7958 1.1 mbalmer <a href="#pdf-package.preload"><code>package.preload</code></a> table.
7959 1.1 mbalmer
7960 1.1 mbalmer
7961 1.1 mbalmer <p>
7962 1.1 mbalmer The second searcher looks for a loader as a Lua library,
7963 1.1 mbalmer using the path stored at <a href="#pdf-package.path"><code>package.path</code></a>.
7964 1.2 lneto The search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
7965 1.1 mbalmer
7966 1.1 mbalmer
7967 1.1 mbalmer <p>
7968 1.1 mbalmer The third searcher looks for a loader as a C library,
7969 1.1 mbalmer using the path given by the variable <a href="#pdf-package.cpath"><code>package.cpath</code></a>.
7970 1.2 lneto Again,
7971 1.2 lneto the search is done as described in function <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
7972 1.1 mbalmer For instance,
7973 1.1 mbalmer if the C path is the string
7974 1.1 mbalmer
7975 1.1 mbalmer <pre>
7976 1.1 mbalmer "./?.so;./?.dll;/usr/local/?/init.so"
7977 1.1 mbalmer </pre><p>
7978 1.1 mbalmer the searcher for module <code>foo</code>
7979 1.1 mbalmer will try to open the files <code>./foo.so</code>, <code>./foo.dll</code>,
7980 1.1 mbalmer and <code>/usr/local/foo/init.so</code>, in that order.
7981 1.1 mbalmer Once it finds a C library,
7982 1.1 mbalmer this searcher first uses a dynamic link facility to link the
7983 1.1 mbalmer application with the library.
7984 1.1 mbalmer Then it tries to find a C function inside the library to
7985 1.1 mbalmer be used as the loader.
7986 1.1 mbalmer The name of this C function is the string "<code>luaopen_</code>"
7987 1.1 mbalmer concatenated with a copy of the module name where each dot
7988 1.1 mbalmer is replaced by an underscore.
7989 1.1 mbalmer Moreover, if the module name has a hyphen,
7990 1.3 lneto its suffix after (and including) the first hyphen is removed.
7991 1.3 lneto For instance, if the module name is <code>a.b.c-v2.1</code>,
7992 1.3 lneto the function name will be <code>luaopen_a_b_c</code>.
7993 1.1 mbalmer
7994 1.1 mbalmer
7995 1.1 mbalmer <p>
7996 1.1 mbalmer The fourth searcher tries an <em>all-in-one loader</em>.
7997 1.1 mbalmer It searches the C path for a library for
7998 1.1 mbalmer the root name of the given module.
7999 1.1 mbalmer For instance, when requiring <code>a.b.c</code>,
8000 1.1 mbalmer it will search for a C library for <code>a</code>.
8001 1.1 mbalmer If found, it looks into it for an open function for
8002 1.1 mbalmer the submodule;
8003 1.1 mbalmer in our example, that would be <code>luaopen_a_b_c</code>.
8004 1.1 mbalmer With this facility, a package can pack several C submodules
8005 1.1 mbalmer into one single library,
8006 1.1 mbalmer with each submodule keeping its original open function.
8007 1.1 mbalmer
8008 1.1 mbalmer
8009 1.1 mbalmer <p>
8010 1.2 lneto All searchers except the first one (preload) return as the extra value
8011 1.2 lneto the file name where the module was found,
8012 1.2 lneto as returned by <a href="#pdf-package.searchpath"><code>package.searchpath</code></a>.
8013 1.2 lneto The first searcher returns no extra value.
8014 1.1 mbalmer
8015 1.1 mbalmer
8016 1.1 mbalmer
8017 1.1 mbalmer
8018 1.1 mbalmer <p>
8019 1.2 lneto <hr><h3><a name="pdf-package.searchpath"><code>package.searchpath (name, path [, sep [, rep]])</code></a></h3>
8020 1.1 mbalmer
8021 1.1 mbalmer
8022 1.1 mbalmer <p>
8023 1.2 lneto Searches for the given <code>name</code> in the given <code>path</code>.
8024 1.1 mbalmer
8025 1.1 mbalmer
8026 1.1 mbalmer <p>
8027 1.2 lneto A path is a string containing a sequence of
8028 1.2 lneto <em>templates</em> separated by semicolons.
8029 1.2 lneto For each template,
8030 1.2 lneto the function replaces each interrogation mark (if any)
8031 1.2 lneto in the template with a copy of <code>name</code>
8032 1.2 lneto wherein all occurrences of <code>sep</code>
8033 1.2 lneto (a dot, by default)
8034 1.2 lneto were replaced by <code>rep</code>
8035 1.2 lneto (the system's directory separator, by default),
8036 1.2 lneto and then tries to open the resulting file name.
8037 1.1 mbalmer
8038 1.1 mbalmer
8039 1.1 mbalmer <p>
8040 1.2 lneto For instance, if the path is the string
8041 1.1 mbalmer
8042 1.2 lneto <pre>
8043 1.2 lneto "./?.lua;./?.lc;/usr/local/?/init.lua"
8044 1.2 lneto </pre><p>
8045 1.2 lneto the search for the name <code>foo.a</code>
8046 1.2 lneto will try to open the files
8047 1.2 lneto <code>./foo/a.lua</code>, <code>./foo/a.lc</code>, and
8048 1.2 lneto <code>/usr/local/foo/a/init.lua</code>, in that order.
8049 1.1 mbalmer
8050 1.1 mbalmer
8051 1.1 mbalmer <p>
8052 1.2 lneto Returns the resulting name of the first file that it can
8053 1.2 lneto open in read mode (after closing the file),
8054 1.2 lneto or <b>nil</b> plus an error message if none succeeds.
8055 1.2 lneto (This error message lists all file names it tried to open.)
8056 1.1 mbalmer
8057 1.1 mbalmer
8058 1.1 mbalmer
8059 1.1 mbalmer
8060 1.1 mbalmer
8061 1.1 mbalmer
8062 1.1 mbalmer
8063 1.2 lneto <h2>6.4 – <a name="6.4">String Manipulation</a></h2>
8064 1.1 mbalmer
8065 1.1 mbalmer <p>
8066 1.1 mbalmer This library provides generic functions for string manipulation,
8067 1.1 mbalmer such as finding and extracting substrings, and pattern matching.
8068 1.1 mbalmer When indexing a string in Lua, the first character is at position 1
8069 1.1 mbalmer (not at 0, as in C).
8070 1.1 mbalmer Indices are allowed to be negative and are interpreted as indexing backwards,
8071 1.1 mbalmer from the end of the string.
8072 1.1 mbalmer Thus, the last character is at position -1, and so on.
8073 1.1 mbalmer
8074 1.1 mbalmer
8075 1.1 mbalmer <p>
8076 1.1 mbalmer The string library provides all its functions inside the table
8077 1.1 mbalmer <a name="pdf-string"><code>string</code></a>.
8078 1.1 mbalmer It also sets a metatable for strings
8079 1.1 mbalmer where the <code>__index</code> field points to the <code>string</code> table.
8080 1.1 mbalmer Therefore, you can use the string functions in object-oriented style.
8081 1.2 lneto For instance, <code>string.byte(s,i)</code>
8082 1.1 mbalmer can be written as <code>s:byte(i)</code>.
8083 1.1 mbalmer
8084 1.1 mbalmer
8085 1.1 mbalmer <p>
8086 1.1 mbalmer The string library assumes one-byte character encodings.
8087 1.1 mbalmer
8088 1.1 mbalmer
8089 1.1 mbalmer <p>
8090 1.1 mbalmer <hr><h3><a name="pdf-string.byte"><code>string.byte (s [, i [, j]])</code></a></h3>
8091 1.4 mbalmer Returns the internal numeric codes of the characters <code>s[i]</code>,
8092 1.2 lneto <code>s[i+1]</code>, ..., <code>s[j]</code>.
8093 1.1 mbalmer The default value for <code>i</code> is 1;
8094 1.1 mbalmer the default value for <code>j</code> is <code>i</code>.
8095 1.2 lneto These indices are corrected
8096 1.2 lneto following the same rules of function <a href="#pdf-string.sub"><code>string.sub</code></a>.
8097 1.1 mbalmer
8098 1.1 mbalmer
8099 1.1 mbalmer <p>
8100 1.4 mbalmer Numeric codes are not necessarily portable across platforms.
8101 1.1 mbalmer
8102 1.1 mbalmer
8103 1.1 mbalmer
8104 1.1 mbalmer
8105 1.1 mbalmer <p>
8106 1.1 mbalmer <hr><h3><a name="pdf-string.char"><code>string.char (···)</code></a></h3>
8107 1.1 mbalmer Receives zero or more integers.
8108 1.1 mbalmer Returns a string with length equal to the number of arguments,
8109 1.4 mbalmer in which each character has the internal numeric code equal
8110 1.1 mbalmer to its corresponding argument.
8111 1.1 mbalmer
8112 1.1 mbalmer
8113 1.1 mbalmer <p>
8114 1.4 mbalmer Numeric codes are not necessarily portable across platforms.
8115 1.2 lneto
8116 1.2 lneto
8117 1.2 lneto
8118 1.2 lneto
8119 1.2 lneto <p>
8120 1.2 lneto <hr><h3><a name="pdf-string.dump"><code>string.dump (function [, strip])</code></a></h3>
8121 1.1 mbalmer
8122 1.1 mbalmer
8123 1.2 lneto <p>
8124 1.2 lneto Returns a string containing a binary representation
8125 1.2 lneto (a <em>binary chunk</em>)
8126 1.2 lneto of the given function,
8127 1.2 lneto so that a later <a href="#pdf-load"><code>load</code></a> on this string returns
8128 1.2 lneto a copy of the function (but with new upvalues).
8129 1.2 lneto If <code>strip</code> is a true value,
8130 1.4 mbalmer the binary representation may not include all debug information
8131 1.4 mbalmer about the function,
8132 1.4 mbalmer to save space.
8133 1.2 lneto
8134 1.2 lneto
8135 1.2 lneto <p>
8136 1.3 lneto Functions with upvalues have only their number of upvalues saved.
8137 1.3 lneto When (re)loaded,
8138 1.3 lneto those upvalues receive fresh instances containing <b>nil</b>.
8139 1.3 lneto (You can use the debug library to serialize
8140 1.3 lneto and reload the upvalues of a function
8141 1.3 lneto in a way adequate to your needs.)
8142 1.1 mbalmer
8143 1.1 mbalmer
8144 1.1 mbalmer
8145 1.1 mbalmer
8146 1.1 mbalmer <p>
8147 1.1 mbalmer <hr><h3><a name="pdf-string.find"><code>string.find (s, pattern [, init [, plain]])</code></a></h3>
8148 1.2 lneto
8149 1.2 lneto
8150 1.2 lneto <p>
8151 1.1 mbalmer Looks for the first match of
8152 1.3 lneto <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>.
8153 1.1 mbalmer If it finds a match, then <code>find</code> returns the indices of <code>s</code>
8154 1.1 mbalmer where this occurrence starts and ends;
8155 1.1 mbalmer otherwise, it returns <b>nil</b>.
8156 1.4 mbalmer A third, optional numeric argument <code>init</code> specifies
8157 1.1 mbalmer where to start the search;
8158 1.1 mbalmer its default value is 1 and can be negative.
8159 1.1 mbalmer A value of <b>true</b> as a fourth, optional argument <code>plain</code>
8160 1.1 mbalmer turns off the pattern matching facilities,
8161 1.1 mbalmer so the function does a plain "find substring" operation,
8162 1.2 lneto with no characters in <code>pattern</code> being considered magic.
8163 1.1 mbalmer Note that if <code>plain</code> is given, then <code>init</code> must be given as well.
8164 1.1 mbalmer
8165 1.1 mbalmer
8166 1.1 mbalmer <p>
8167 1.1 mbalmer If the pattern has captures,
8168 1.1 mbalmer then in a successful match
8169 1.1 mbalmer the captured values are also returned,
8170 1.1 mbalmer after the two indices.
8171 1.1 mbalmer
8172 1.1 mbalmer
8173 1.1 mbalmer
8174 1.1 mbalmer
8175 1.1 mbalmer <p>
8176 1.1 mbalmer <hr><h3><a name="pdf-string.format"><code>string.format (formatstring, ···)</code></a></h3>
8177 1.2 lneto
8178 1.2 lneto
8179 1.2 lneto <p>
8180 1.1 mbalmer Returns a formatted version of its variable number of arguments
8181 1.1 mbalmer following the description given in its first argument (which must be a string).
8182 1.3 lneto The format string follows the same rules as the ISO C function <code>sprintf</code>.
8183 1.1 mbalmer The only differences are that the options/modifiers
8184 1.2 lneto <code>*</code>, <code>h</code>, <code>L</code>, <code>l</code>, <code>n</code>,
8185 1.2 lneto and <code>p</code> are not supported
8186 1.1 mbalmer and that there is an extra option, <code>q</code>.
8187 1.2 lneto The <code>q</code> option formats a string between double quotes,
8188 1.2 lneto using escape sequences when necessary to ensure that
8189 1.2 lneto it can safely be read back by the Lua interpreter.
8190 1.1 mbalmer For instance, the call
8191 1.1 mbalmer
8192 1.1 mbalmer <pre>
8193 1.1 mbalmer string.format('%q', 'a string with "quotes" and \n new line')
8194 1.1 mbalmer </pre><p>
8195 1.2 lneto may produce the string:
8196 1.1 mbalmer
8197 1.1 mbalmer <pre>
8198 1.1 mbalmer "a string with \"quotes\" and \
8199 1.1 mbalmer new line"
8200 1.1 mbalmer </pre>
8201 1.1 mbalmer
8202 1.1 mbalmer <p>
8203 1.2 lneto Options
8204 1.4 mbalmer <code>A</code>, <code>a</code>, <code>E</code>, <code>e</code>, <code>f</code>,
8205 1.2 lneto <code>G</code>, and <code>g</code> all expect a number as argument.
8206 1.2 lneto Options <code>c</code>, <code>d</code>,
8207 1.2 lneto <code>i</code>, <code>o</code>, <code>u</code>, <code>X</code>, and <code>x</code>
8208 1.2 lneto expect an integer.
8209 1.4 mbalmer Option <code>q</code> expects a string.
8210 1.5 lneto Option <code>s</code> expects a string;
8211 1.4 mbalmer if its argument is not a string,
8212 1.2 lneto it is converted to one following the same rules of <a href="#pdf-tostring"><code>tostring</code></a>.
8213 1.5 lneto If the option has any modifier (flags, width, length),
8214 1.5 lneto the string argument should not contain embedded zeros.
8215 1.1 mbalmer
8216 1.1 mbalmer
8217 1.4 mbalmer <p>
8218 1.4 mbalmer When Lua is compiled with a non-C99 compiler,
8219 1.4 mbalmer options <code>A</code> and <code>a</code> (hexadecimal floats)
8220 1.4 mbalmer do not support any modifier (flags, width, length).
8221 1.4 mbalmer
8222 1.4 mbalmer
8223 1.1 mbalmer
8224 1.1 mbalmer
8225 1.1 mbalmer <p>
8226 1.1 mbalmer <hr><h3><a name="pdf-string.gmatch"><code>string.gmatch (s, pattern)</code></a></h3>
8227 1.1 mbalmer Returns an iterator function that,
8228 1.1 mbalmer each time it is called,
8229 1.3 lneto returns the next captures from <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>)
8230 1.3 lneto over the string <code>s</code>.
8231 1.1 mbalmer If <code>pattern</code> specifies no captures,
8232 1.1 mbalmer then the whole match is produced in each call.
8233 1.1 mbalmer
8234 1.1 mbalmer
8235 1.1 mbalmer <p>
8236 1.1 mbalmer As an example, the following loop
8237 1.2 lneto will iterate over all the words from string <code>s</code>,
8238 1.2 lneto printing one per line:
8239 1.1 mbalmer
8240 1.1 mbalmer <pre>
8241 1.1 mbalmer s = "hello world from Lua"
8242 1.1 mbalmer for w in string.gmatch(s, "%a+") do
8243 1.1 mbalmer print(w)
8244 1.1 mbalmer end
8245 1.1 mbalmer </pre><p>
8246 1.1 mbalmer The next example collects all pairs <code>key=value</code> from the
8247 1.1 mbalmer given string into a table:
8248 1.1 mbalmer
8249 1.1 mbalmer <pre>
8250 1.1 mbalmer t = {}
8251 1.1 mbalmer s = "from=world, to=Lua"
8252 1.1 mbalmer for k, v in string.gmatch(s, "(%w+)=(%w+)") do
8253 1.1 mbalmer t[k] = v
8254 1.1 mbalmer end
8255 1.1 mbalmer </pre>
8256 1.1 mbalmer
8257 1.1 mbalmer <p>
8258 1.2 lneto For this function, a caret '<code>^</code>' at the start of a pattern does not
8259 1.1 mbalmer work as an anchor, as this would prevent the iteration.
8260 1.1 mbalmer
8261 1.1 mbalmer
8262 1.1 mbalmer
8263 1.1 mbalmer
8264 1.1 mbalmer <p>
8265 1.1 mbalmer <hr><h3><a name="pdf-string.gsub"><code>string.gsub (s, pattern, repl [, n])</code></a></h3>
8266 1.1 mbalmer Returns a copy of <code>s</code>
8267 1.1 mbalmer in which all (or the first <code>n</code>, if given)
8268 1.3 lneto occurrences of the <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) have been
8269 1.1 mbalmer replaced by a replacement string specified by <code>repl</code>,
8270 1.1 mbalmer which can be a string, a table, or a function.
8271 1.1 mbalmer <code>gsub</code> also returns, as its second value,
8272 1.1 mbalmer the total number of matches that occurred.
8273 1.2 lneto The name <code>gsub</code> comes from <em>Global SUBstitution</em>.
8274 1.1 mbalmer
8275 1.1 mbalmer
8276 1.1 mbalmer <p>
8277 1.1 mbalmer If <code>repl</code> is a string, then its value is used for replacement.
8278 1.1 mbalmer The character <code>%</code> works as an escape character:
8279 1.2 lneto any sequence in <code>repl</code> of the form <code>%<em>d</em></code>,
8280 1.2 lneto with <em>d</em> between 1 and 9,
8281 1.2 lneto stands for the value of the <em>d</em>-th captured substring.
8282 1.1 mbalmer The sequence <code>%0</code> stands for the whole match.
8283 1.1 mbalmer The sequence <code>%%</code> stands for a single <code>%</code>.
8284 1.1 mbalmer
8285 1.1 mbalmer
8286 1.1 mbalmer <p>
8287 1.1 mbalmer If <code>repl</code> is a table, then the table is queried for every match,
8288 1.2 lneto using the first capture as the key.
8289 1.1 mbalmer
8290 1.1 mbalmer
8291 1.1 mbalmer <p>
8292 1.1 mbalmer If <code>repl</code> is a function, then this function is called every time a
8293 1.1 mbalmer match occurs, with all captured substrings passed as arguments,
8294 1.2 lneto in order.
8295 1.2 lneto
8296 1.2 lneto
8297 1.2 lneto <p>
8298 1.2 lneto In any case,
8299 1.1 mbalmer if the pattern specifies no captures,
8300 1.2 lneto then it behaves as if the whole pattern was inside a capture.
8301 1.1 mbalmer
8302 1.1 mbalmer
8303 1.1 mbalmer <p>
8304 1.1 mbalmer If the value returned by the table query or by the function call
8305 1.1 mbalmer is a string or a number,
8306 1.1 mbalmer then it is used as the replacement string;
8307 1.1 mbalmer otherwise, if it is <b>false</b> or <b>nil</b>,
8308 1.1 mbalmer then there is no replacement
8309 1.1 mbalmer (that is, the original match is kept in the string).
8310 1.1 mbalmer
8311 1.1 mbalmer
8312 1.1 mbalmer <p>
8313 1.1 mbalmer Here are some examples:
8314 1.1 mbalmer
8315 1.1 mbalmer <pre>
8316 1.1 mbalmer x = string.gsub("hello world", "(%w+)", "%1 %1")
8317 1.1 mbalmer --> x="hello hello world world"
8318 1.1 mbalmer
8319 1.1 mbalmer x = string.gsub("hello world", "%w+", "%0 %0", 1)
8320 1.1 mbalmer --> x="hello hello world"
8321 1.1 mbalmer
8322 1.1 mbalmer x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
8323 1.1 mbalmer --> x="world hello Lua from"
8324 1.1 mbalmer
8325 1.1 mbalmer x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
8326 1.1 mbalmer --> x="home = /home/roberto, user = roberto"
8327 1.1 mbalmer
8328 1.1 mbalmer x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
8329 1.2 lneto return load(s)()
8330 1.1 mbalmer end)
8331 1.1 mbalmer --> x="4+5 = 9"
8332 1.1 mbalmer
8333 1.2 lneto local t = {name="lua", version="5.3"}
8334 1.1 mbalmer x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
8335 1.2 lneto --> x="lua-5.3.tar.gz"
8336 1.1 mbalmer </pre>
8337 1.1 mbalmer
8338 1.1 mbalmer
8339 1.1 mbalmer
8340 1.1 mbalmer <p>
8341 1.1 mbalmer <hr><h3><a name="pdf-string.len"><code>string.len (s)</code></a></h3>
8342 1.1 mbalmer Receives a string and returns its length.
8343 1.1 mbalmer The empty string <code>""</code> has length 0.
8344 1.1 mbalmer Embedded zeros are counted,
8345 1.1 mbalmer so <code>"a\000bc\000"</code> has length 5.
8346 1.1 mbalmer
8347 1.1 mbalmer
8348 1.1 mbalmer
8349 1.1 mbalmer
8350 1.1 mbalmer <p>
8351 1.1 mbalmer <hr><h3><a name="pdf-string.lower"><code>string.lower (s)</code></a></h3>
8352 1.1 mbalmer Receives a string and returns a copy of this string with all
8353 1.1 mbalmer uppercase letters changed to lowercase.
8354 1.1 mbalmer All other characters are left unchanged.
8355 1.1 mbalmer The definition of what an uppercase letter is depends on the current locale.
8356 1.1 mbalmer
8357 1.1 mbalmer
8358 1.1 mbalmer
8359 1.1 mbalmer
8360 1.1 mbalmer <p>
8361 1.1 mbalmer <hr><h3><a name="pdf-string.match"><code>string.match (s, pattern [, init])</code></a></h3>
8362 1.1 mbalmer Looks for the first <em>match</em> of
8363 1.3 lneto <code>pattern</code> (see <a href="#6.4.1">§6.4.1</a>) in the string <code>s</code>.
8364 1.1 mbalmer If it finds one, then <code>match</code> returns
8365 1.1 mbalmer the captures from the pattern;
8366 1.1 mbalmer otherwise it returns <b>nil</b>.
8367 1.1 mbalmer If <code>pattern</code> specifies no captures,
8368 1.1 mbalmer then the whole match is returned.
8369 1.4 mbalmer A third, optional numeric argument <code>init</code> specifies
8370 1.1 mbalmer where to start the search;
8371 1.1 mbalmer its default value is 1 and can be negative.
8372 1.1 mbalmer
8373 1.1 mbalmer
8374 1.1 mbalmer
8375 1.1 mbalmer
8376 1.1 mbalmer <p>
8377 1.3 lneto <hr><h3><a name="pdf-string.pack"><code>string.pack (fmt, v1, v2, ···)</code></a></h3>
8378 1.3 lneto
8379 1.3 lneto
8380 1.3 lneto <p>
8381 1.3 lneto Returns a binary string containing the values <code>v1</code>, <code>v2</code>, etc.
8382 1.3 lneto packed (that is, serialized in binary form)
8383 1.3 lneto according to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>).
8384 1.3 lneto
8385 1.3 lneto
8386 1.3 lneto
8387 1.3 lneto
8388 1.3 lneto <p>
8389 1.3 lneto <hr><h3><a name="pdf-string.packsize"><code>string.packsize (fmt)</code></a></h3>
8390 1.3 lneto
8391 1.3 lneto
8392 1.3 lneto <p>
8393 1.3 lneto Returns the size of a string resulting from <a href="#pdf-string.pack"><code>string.pack</code></a>
8394 1.3 lneto with the given format.
8395 1.3 lneto The format string cannot have the variable-length options
8396 1.3 lneto '<code>s</code>' or '<code>z</code>' (see <a href="#6.4.2">§6.4.2</a>).
8397 1.3 lneto
8398 1.3 lneto
8399 1.3 lneto
8400 1.3 lneto
8401 1.3 lneto <p>
8402 1.2 lneto <hr><h3><a name="pdf-string.rep"><code>string.rep (s, n [, sep])</code></a></h3>
8403 1.1 mbalmer Returns a string that is the concatenation of <code>n</code> copies of
8404 1.2 lneto the string <code>s</code> separated by the string <code>sep</code>.
8405 1.2 lneto The default value for <code>sep</code> is the empty string
8406 1.2 lneto (that is, no separator).
8407 1.3 lneto Returns the empty string if <code>n</code> is not positive.
8408 1.1 mbalmer
8409 1.1 mbalmer
8410 1.5 lneto <p>
8411 1.5 lneto (Note that it is very easy to exhaust the memory of your machine
8412 1.5 lneto with a single call to this function.)
8413 1.5 lneto
8414 1.5 lneto
8415 1.1 mbalmer
8416 1.1 mbalmer
8417 1.1 mbalmer <p>
8418 1.1 mbalmer <hr><h3><a name="pdf-string.reverse"><code>string.reverse (s)</code></a></h3>
8419 1.1 mbalmer Returns a string that is the string <code>s</code> reversed.
8420 1.1 mbalmer
8421 1.1 mbalmer
8422 1.1 mbalmer
8423 1.1 mbalmer
8424 1.1 mbalmer <p>
8425 1.1 mbalmer <hr><h3><a name="pdf-string.sub"><code>string.sub (s, i [, j])</code></a></h3>
8426 1.1 mbalmer Returns the substring of <code>s</code> that
8427 1.1 mbalmer starts at <code>i</code> and continues until <code>j</code>;
8428 1.1 mbalmer <code>i</code> and <code>j</code> can be negative.
8429 1.1 mbalmer If <code>j</code> is absent, then it is assumed to be equal to -1
8430 1.1 mbalmer (which is the same as the string length).
8431 1.1 mbalmer In particular,
8432 1.1 mbalmer the call <code>string.sub(s,1,j)</code> returns a prefix of <code>s</code>
8433 1.1 mbalmer with length <code>j</code>,
8434 1.1 mbalmer and <code>string.sub(s, -i)</code> returns a suffix of <code>s</code>
8435 1.1 mbalmer with length <code>i</code>.
8436 1.1 mbalmer
8437 1.1 mbalmer
8438 1.2 lneto <p>
8439 1.2 lneto If, after the translation of negative indices,
8440 1.2 lneto <code>i</code> is less than 1,
8441 1.2 lneto it is corrected to 1.
8442 1.2 lneto If <code>j</code> is greater than the string length,
8443 1.2 lneto it is corrected to that length.
8444 1.2 lneto If, after these corrections,
8445 1.2 lneto <code>i</code> is greater than <code>j</code>,
8446 1.2 lneto the function returns the empty string.
8447 1.2 lneto
8448 1.2 lneto
8449 1.2 lneto
8450 1.2 lneto
8451 1.2 lneto <p>
8452 1.3 lneto <hr><h3><a name="pdf-string.unpack"><code>string.unpack (fmt, s [, pos])</code></a></h3>
8453 1.2 lneto
8454 1.2 lneto
8455 1.2 lneto <p>
8456 1.3 lneto Returns the values packed in string <code>s</code> (see <a href="#pdf-string.pack"><code>string.pack</code></a>)
8457 1.3 lneto according to the format string <code>fmt</code> (see <a href="#6.4.2">§6.4.2</a>).
8458 1.3 lneto An optional <code>pos</code> marks where
8459 1.3 lneto to start reading in <code>s</code> (default is 1).
8460 1.3 lneto After the read values,
8461 1.3 lneto this function also returns the index of the first unread byte in <code>s</code>.
8462 1.2 lneto
8463 1.2 lneto
8464 1.1 mbalmer
8465 1.1 mbalmer
8466 1.1 mbalmer <p>
8467 1.1 mbalmer <hr><h3><a name="pdf-string.upper"><code>string.upper (s)</code></a></h3>
8468 1.1 mbalmer Receives a string and returns a copy of this string with all
8469 1.1 mbalmer lowercase letters changed to uppercase.
8470 1.1 mbalmer All other characters are left unchanged.
8471 1.1 mbalmer The definition of what a lowercase letter is depends on the current locale.
8472 1.1 mbalmer
8473 1.1 mbalmer
8474 1.1 mbalmer
8475 1.3 lneto
8476 1.3 lneto
8477 1.2 lneto <h3>6.4.1 – <a name="6.4.1">Patterns</a></h3>
8478 1.1 mbalmer
8479 1.3 lneto <p>
8480 1.3 lneto Patterns in Lua are described by regular strings,
8481 1.3 lneto which are interpreted as patterns by the pattern-matching functions
8482 1.3 lneto <a href="#pdf-string.find"><code>string.find</code></a>,
8483 1.3 lneto <a href="#pdf-string.gmatch"><code>string.gmatch</code></a>,
8484 1.3 lneto <a href="#pdf-string.gsub"><code>string.gsub</code></a>,
8485 1.3 lneto and <a href="#pdf-string.match"><code>string.match</code></a>.
8486 1.3 lneto This section describes the syntax and the meaning
8487 1.3 lneto (that is, what they match) of these strings.
8488 1.3 lneto
8489 1.3 lneto
8490 1.1 mbalmer
8491 1.1 mbalmer <h4>Character Class:</h4><p>
8492 1.1 mbalmer A <em>character class</em> is used to represent a set of characters.
8493 1.1 mbalmer The following combinations are allowed in describing a character class:
8494 1.1 mbalmer
8495 1.1 mbalmer <ul>
8496 1.1 mbalmer
8497 1.2 lneto <li><b><em>x</em>: </b>
8498 1.1 mbalmer (where <em>x</em> is not one of the <em>magic characters</em>
8499 1.1 mbalmer <code>^$()%.[]*+-?</code>)
8500 1.1 mbalmer represents the character <em>x</em> itself.
8501 1.1 mbalmer </li>
8502 1.1 mbalmer
8503 1.2 lneto <li><b><code>.</code>: </b> (a dot) represents all characters.</li>
8504 1.1 mbalmer
8505 1.2 lneto <li><b><code>%a</code>: </b> represents all letters.</li>
8506 1.1 mbalmer
8507 1.2 lneto <li><b><code>%c</code>: </b> represents all control characters.</li>
8508 1.1 mbalmer
8509 1.2 lneto <li><b><code>%d</code>: </b> represents all digits.</li>
8510 1.1 mbalmer
8511 1.2 lneto <li><b><code>%g</code>: </b> represents all printable characters except space.</li>
8512 1.1 mbalmer
8513 1.2 lneto <li><b><code>%l</code>: </b> represents all lowercase letters.</li>
8514 1.1 mbalmer
8515 1.2 lneto <li><b><code>%p</code>: </b> represents all punctuation characters.</li>
8516 1.1 mbalmer
8517 1.2 lneto <li><b><code>%s</code>: </b> represents all space characters.</li>
8518 1.1 mbalmer
8519 1.2 lneto <li><b><code>%u</code>: </b> represents all uppercase letters.</li>
8520 1.1 mbalmer
8521 1.2 lneto <li><b><code>%w</code>: </b> represents all alphanumeric characters.</li>
8522 1.1 mbalmer
8523 1.2 lneto <li><b><code>%x</code>: </b> represents all hexadecimal digits.</li>
8524 1.1 mbalmer
8525 1.2 lneto <li><b><code>%<em>x</em></code>: </b> (where <em>x</em> is any non-alphanumeric character)
8526 1.1 mbalmer represents the character <em>x</em>.
8527 1.1 mbalmer This is the standard way to escape the magic characters.
8528 1.3 lneto Any non-alphanumeric character
8529 1.4 mbalmer (including all punctuation characters, even the non-magical)
8530 1.1 mbalmer can be preceded by a '<code>%</code>'
8531 1.1 mbalmer when used to represent itself in a pattern.
8532 1.1 mbalmer </li>
8533 1.1 mbalmer
8534 1.2 lneto <li><b><code>[<em>set</em>]</code>: </b>
8535 1.1 mbalmer represents the class which is the union of all
8536 1.1 mbalmer characters in <em>set</em>.
8537 1.1 mbalmer A range of characters can be specified by
8538 1.2 lneto separating the end characters of the range,
8539 1.2 lneto in ascending order, with a '<code>-</code>'.
8540 1.1 mbalmer All classes <code>%</code><em>x</em> described above can also be used as
8541 1.1 mbalmer components in <em>set</em>.
8542 1.1 mbalmer All other characters in <em>set</em> represent themselves.
8543 1.1 mbalmer For example, <code>[%w_]</code> (or <code>[_%w]</code>)
8544 1.1 mbalmer represents all alphanumeric characters plus the underscore,
8545 1.1 mbalmer <code>[0-7]</code> represents the octal digits,
8546 1.1 mbalmer and <code>[0-7%l%-]</code> represents the octal digits plus
8547 1.1 mbalmer the lowercase letters plus the '<code>-</code>' character.
8548 1.1 mbalmer
8549 1.1 mbalmer
8550 1.1 mbalmer <p>
8551 1.1 mbalmer The interaction between ranges and classes is not defined.
8552 1.1 mbalmer Therefore, patterns like <code>[%a-z]</code> or <code>[a-%%]</code>
8553 1.1 mbalmer have no meaning.
8554 1.1 mbalmer </li>
8555 1.1 mbalmer
8556 1.2 lneto <li><b><code>[^<em>set</em>]</code>: </b>
8557 1.1 mbalmer represents the complement of <em>set</em>,
8558 1.1 mbalmer where <em>set</em> is interpreted as above.
8559 1.1 mbalmer </li>
8560 1.1 mbalmer
8561 1.1 mbalmer </ul><p>
8562 1.1 mbalmer For all classes represented by single letters (<code>%a</code>, <code>%c</code>, etc.),
8563 1.1 mbalmer the corresponding uppercase letter represents the complement of the class.
8564 1.1 mbalmer For instance, <code>%S</code> represents all non-space characters.
8565 1.1 mbalmer
8566 1.1 mbalmer
8567 1.1 mbalmer <p>
8568 1.1 mbalmer The definitions of letter, space, and other character groups
8569 1.1 mbalmer depend on the current locale.
8570 1.1 mbalmer In particular, the class <code>[a-z]</code> may not be equivalent to <code>%l</code>.
8571 1.1 mbalmer
8572 1.1 mbalmer
8573 1.1 mbalmer
8574 1.1 mbalmer
8575 1.1 mbalmer
8576 1.1 mbalmer <h4>Pattern Item:</h4><p>
8577 1.1 mbalmer A <em>pattern item</em> can be
8578 1.1 mbalmer
8579 1.1 mbalmer <ul>
8580 1.1 mbalmer
8581 1.1 mbalmer <li>
8582 1.1 mbalmer a single character class,
8583 1.1 mbalmer which matches any single character in the class;
8584 1.1 mbalmer </li>
8585 1.1 mbalmer
8586 1.1 mbalmer <li>
8587 1.1 mbalmer a single character class followed by '<code>*</code>',
8588 1.3 lneto which matches zero or more repetitions of characters in the class.
8589 1.1 mbalmer These repetition items will always match the longest possible sequence;
8590 1.1 mbalmer </li>
8591 1.1 mbalmer
8592 1.1 mbalmer <li>
8593 1.1 mbalmer a single character class followed by '<code>+</code>',
8594 1.3 lneto which matches one or more repetitions of characters in the class.
8595 1.1 mbalmer These repetition items will always match the longest possible sequence;
8596 1.1 mbalmer </li>
8597 1.1 mbalmer
8598 1.1 mbalmer <li>
8599 1.1 mbalmer a single character class followed by '<code>-</code>',
8600 1.3 lneto which also matches zero or more repetitions of characters in the class.
8601 1.1 mbalmer Unlike '<code>*</code>',
8602 1.2 lneto these repetition items will always match the shortest possible sequence;
8603 1.1 mbalmer </li>
8604 1.1 mbalmer
8605 1.1 mbalmer <li>
8606 1.1 mbalmer a single character class followed by '<code>?</code>',
8607 1.3 lneto which matches zero or one occurrence of a character in the class.
8608 1.3 lneto It always matches one occurrence if possible;
8609 1.1 mbalmer </li>
8610 1.1 mbalmer
8611 1.1 mbalmer <li>
8612 1.1 mbalmer <code>%<em>n</em></code>, for <em>n</em> between 1 and 9;
8613 1.1 mbalmer such item matches a substring equal to the <em>n</em>-th captured string
8614 1.1 mbalmer (see below);
8615 1.1 mbalmer </li>
8616 1.1 mbalmer
8617 1.1 mbalmer <li>
8618 1.1 mbalmer <code>%b<em>xy</em></code>, where <em>x</em> and <em>y</em> are two distinct characters;
8619 1.1 mbalmer such item matches strings that start with <em>x</em>, end with <em>y</em>,
8620 1.1 mbalmer and where the <em>x</em> and <em>y</em> are <em>balanced</em>.
8621 1.1 mbalmer This means that, if one reads the string from left to right,
8622 1.1 mbalmer counting <em>+1</em> for an <em>x</em> and <em>-1</em> for a <em>y</em>,
8623 1.1 mbalmer the ending <em>y</em> is the first <em>y</em> where the count reaches 0.
8624 1.1 mbalmer For instance, the item <code>%b()</code> matches expressions with
8625 1.1 mbalmer balanced parentheses.
8626 1.1 mbalmer </li>
8627 1.1 mbalmer
8628 1.2 lneto <li>
8629 1.2 lneto <code>%f[<em>set</em>]</code>, a <em>frontier pattern</em>;
8630 1.2 lneto such item matches an empty string at any position such that
8631 1.2 lneto the next character belongs to <em>set</em>
8632 1.2 lneto and the previous character does not belong to <em>set</em>.
8633 1.2 lneto The set <em>set</em> is interpreted as previously described.
8634 1.2 lneto The beginning and the end of the subject are handled as if
8635 1.2 lneto they were the character '<code>\0</code>'.
8636 1.2 lneto </li>
8637 1.2 lneto
8638 1.1 mbalmer </ul>
8639 1.1 mbalmer
8640 1.1 mbalmer
8641 1.1 mbalmer
8642 1.1 mbalmer
8643 1.1 mbalmer <h4>Pattern:</h4><p>
8644 1.1 mbalmer A <em>pattern</em> is a sequence of pattern items.
8645 1.2 lneto A caret '<code>^</code>' at the beginning of a pattern anchors the match at the
8646 1.1 mbalmer beginning of the subject string.
8647 1.1 mbalmer A '<code>$</code>' at the end of a pattern anchors the match at the
8648 1.1 mbalmer end of the subject string.
8649 1.1 mbalmer At other positions,
8650 1.1 mbalmer '<code>^</code>' and '<code>$</code>' have no special meaning and represent themselves.
8651 1.1 mbalmer
8652 1.1 mbalmer
8653 1.1 mbalmer
8654 1.1 mbalmer
8655 1.1 mbalmer
8656 1.1 mbalmer <h4>Captures:</h4><p>
8657 1.1 mbalmer A pattern can contain sub-patterns enclosed in parentheses;
8658 1.1 mbalmer they describe <em>captures</em>.
8659 1.1 mbalmer When a match succeeds, the substrings of the subject string
8660 1.1 mbalmer that match captures are stored (<em>captured</em>) for future use.
8661 1.1 mbalmer Captures are numbered according to their left parentheses.
8662 1.1 mbalmer For instance, in the pattern <code>"(a*(.)%w(%s*))"</code>,
8663 1.1 mbalmer the part of the string matching <code>"a*(.)%w(%s*)"</code> is
8664 1.1 mbalmer stored as the first capture (and therefore has number 1);
8665 1.1 mbalmer the character matching "<code>.</code>" is captured with number 2,
8666 1.1 mbalmer and the part matching "<code>%s*</code>" has number 3.
8667 1.1 mbalmer
8668 1.1 mbalmer
8669 1.1 mbalmer <p>
8670 1.1 mbalmer As a special case, the empty capture <code>()</code> captures
8671 1.1 mbalmer the current string position (a number).
8672 1.1 mbalmer For instance, if we apply the pattern <code>"()aa()"</code> on the
8673 1.1 mbalmer string <code>"flaaap"</code>, there will be two captures: 3 and 5.
8674 1.1 mbalmer
8675 1.1 mbalmer
8676 1.2 lneto
8677 1.2 lneto
8678 1.2 lneto
8679 1.2 lneto
8680 1.2 lneto
8681 1.3 lneto <h3>6.4.2 – <a name="6.4.2">Format Strings for Pack and Unpack</a></h3>
8682 1.3 lneto
8683 1.3 lneto <p>
8684 1.3 lneto The first argument to <a href="#pdf-string.pack"><code>string.pack</code></a>,
8685 1.3 lneto <a href="#pdf-string.packsize"><code>string.packsize</code></a>, and <a href="#pdf-string.unpack"><code>string.unpack</code></a>
8686 1.3 lneto is a format string,
8687 1.3 lneto which describes the layout of the structure being created or read.
8688 1.3 lneto
8689 1.3 lneto
8690 1.3 lneto <p>
8691 1.3 lneto A format string is a sequence of conversion options.
8692 1.3 lneto The conversion options are as follows:
8693 1.3 lneto
8694 1.3 lneto <ul>
8695 1.3 lneto <li><b><code><</code>: </b>sets little endian</li>
8696 1.3 lneto <li><b><code>></code>: </b>sets big endian</li>
8697 1.3 lneto <li><b><code>=</code>: </b>sets native endian</li>
8698 1.3 lneto <li><b><code>![<em>n</em>]</code>: </b>sets maximum alignment to <code>n</code>
8699 1.3 lneto (default is native alignment)</li>
8700 1.3 lneto <li><b><code>b</code>: </b>a signed byte (<code>char</code>)</li>
8701 1.3 lneto <li><b><code>B</code>: </b>an unsigned byte (<code>char</code>)</li>
8702 1.3 lneto <li><b><code>h</code>: </b>a signed <code>short</code> (native size)</li>
8703 1.3 lneto <li><b><code>H</code>: </b>an unsigned <code>short</code> (native size)</li>
8704 1.3 lneto <li><b><code>l</code>: </b>a signed <code>long</code> (native size)</li>
8705 1.3 lneto <li><b><code>L</code>: </b>an unsigned <code>long</code> (native size)</li>
8706 1.3 lneto <li><b><code>j</code>: </b>a <code>lua_Integer</code></li>
8707 1.3 lneto <li><b><code>J</code>: </b>a <code>lua_Unsigned</code></li>
8708 1.3 lneto <li><b><code>T</code>: </b>a <code>size_t</code> (native size)</li>
8709 1.3 lneto <li><b><code>i[<em>n</em>]</code>: </b>a signed <code>int</code> with <code>n</code> bytes
8710 1.3 lneto (default is native size)</li>
8711 1.3 lneto <li><b><code>I[<em>n</em>]</code>: </b>an unsigned <code>int</code> with <code>n</code> bytes
8712 1.3 lneto (default is native size)</li>
8713 1.3 lneto <li><b><code>f</code>: </b>a <code>float</code> (native size)</li>
8714 1.3 lneto <li><b><code>d</code>: </b>a <code>double</code> (native size)</li>
8715 1.3 lneto <li><b><code>n</code>: </b>a <code>lua_Number</code></li>
8716 1.3 lneto <li><b><code>c<em>n</em></code>: </b>a fixed-sized string with <code>n</code> bytes</li>
8717 1.3 lneto <li><b><code>z</code>: </b>a zero-terminated string</li>
8718 1.3 lneto <li><b><code>s[<em>n</em>]</code>: </b>a string preceded by its length
8719 1.3 lneto coded as an unsigned integer with <code>n</code> bytes
8720 1.3 lneto (default is a <code>size_t</code>)</li>
8721 1.3 lneto <li><b><code>x</code>: </b>one byte of padding</li>
8722 1.3 lneto <li><b><code>X<em>op</em></code>: </b>an empty item that aligns
8723 1.3 lneto according to option <code>op</code>
8724 1.3 lneto (which is otherwise ignored)</li>
8725 1.3 lneto <li><b>'<code> </code>': </b>(empty space) ignored</li>
8726 1.3 lneto </ul><p>
8727 1.3 lneto (A "<code>[<em>n</em>]</code>" means an optional integral numeral.)
8728 1.3 lneto Except for padding, spaces, and configurations
8729 1.3 lneto (options "<code>xX <=>!</code>"),
8730 1.3 lneto each option corresponds to an argument (in <a href="#pdf-string.pack"><code>string.pack</code></a>)
8731 1.3 lneto or a result (in <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
8732 1.3 lneto
8733 1.3 lneto
8734 1.3 lneto <p>
8735 1.3 lneto For options "<code>!<em>n</em></code>", "<code>s<em>n</em></code>", "<code>i<em>n</em></code>", and "<code>I<em>n</em></code>",
8736 1.3 lneto <code>n</code> can be any integer between 1 and 16.
8737 1.3 lneto All integral options check overflows;
8738 1.3 lneto <a href="#pdf-string.pack"><code>string.pack</code></a> checks whether the given value fits in the given size;
8739 1.3 lneto <a href="#pdf-string.unpack"><code>string.unpack</code></a> checks whether the read value fits in a Lua integer.
8740 1.3 lneto
8741 1.3 lneto
8742 1.3 lneto <p>
8743 1.3 lneto Any format string starts as if prefixed by "<code>!1=</code>",
8744 1.3 lneto that is,
8745 1.3 lneto with maximum alignment of 1 (no alignment)
8746 1.3 lneto and native endianness.
8747 1.3 lneto
8748 1.3 lneto
8749 1.3 lneto <p>
8750 1.3 lneto Alignment works as follows:
8751 1.3 lneto For each option,
8752 1.3 lneto the format gets extra padding until the data starts
8753 1.3 lneto at an offset that is a multiple of the minimum between the
8754 1.3 lneto option size and the maximum alignment;
8755 1.3 lneto this minimum must be a power of 2.
8756 1.3 lneto Options "<code>c</code>" and "<code>z</code>" are not aligned;
8757 1.3 lneto option "<code>s</code>" follows the alignment of its starting integer.
8758 1.3 lneto
8759 1.3 lneto
8760 1.3 lneto <p>
8761 1.3 lneto All padding is filled with zeros by <a href="#pdf-string.pack"><code>string.pack</code></a>
8762 1.3 lneto (and ignored by <a href="#pdf-string.unpack"><code>string.unpack</code></a>).
8763 1.3 lneto
8764 1.3 lneto
8765 1.3 lneto
8766 1.2 lneto
8767 1.2 lneto
8768 1.2 lneto
8769 1.2 lneto
8770 1.2 lneto <h2>6.5 – <a name="6.5">UTF-8 Support</a></h2>
8771 1.2 lneto
8772 1.2 lneto <p>
8773 1.2 lneto This library provides basic support for UTF-8 encoding.
8774 1.2 lneto It provides all its functions inside the table <a name="pdf-utf8"><code>utf8</code></a>.
8775 1.2 lneto This library does not provide any support for Unicode other
8776 1.2 lneto than the handling of the encoding.
8777 1.2 lneto Any operation that needs the meaning of a character,
8778 1.2 lneto such as character classification, is outside its scope.
8779 1.2 lneto
8780 1.2 lneto
8781 1.2 lneto <p>
8782 1.2 lneto Unless stated otherwise,
8783 1.2 lneto all functions that expect a byte position as a parameter
8784 1.2 lneto assume that the given position is either the start of a byte sequence
8785 1.2 lneto or one plus the length of the subject string.
8786 1.2 lneto As in the string library,
8787 1.2 lneto negative indices count from the end of the string.
8788 1.2 lneto
8789 1.2 lneto
8790 1.2 lneto <p>
8791 1.2 lneto <hr><h3><a name="pdf-utf8.char"><code>utf8.char (···)</code></a></h3>
8792 1.2 lneto Receives zero or more integers,
8793 1.2 lneto converts each one to its corresponding UTF-8 byte sequence
8794 1.2 lneto and returns a string with the concatenation of all these sequences.
8795 1.2 lneto
8796 1.2 lneto
8797 1.2 lneto
8798 1.2 lneto
8799 1.2 lneto <p>
8800 1.3 lneto <hr><h3><a name="pdf-utf8.charpattern"><code>utf8.charpattern</code></a></h3>
8801 1.2 lneto The pattern (a string, not a function) "<code>[\0-\x7F\xC2-\xF4][\x80-\xBF]*</code>"
8802 1.2 lneto (see <a href="#6.4.1">§6.4.1</a>),
8803 1.2 lneto which matches exactly one UTF-8 byte sequence,
8804 1.2 lneto assuming that the subject is a valid UTF-8 string.
8805 1.2 lneto
8806 1.2 lneto
8807 1.2 lneto
8808 1.2 lneto
8809 1.2 lneto <p>
8810 1.2 lneto <hr><h3><a name="pdf-utf8.codes"><code>utf8.codes (s)</code></a></h3>
8811 1.2 lneto
8812 1.2 lneto
8813 1.2 lneto <p>
8814 1.2 lneto Returns values so that the construction
8815 1.2 lneto
8816 1.2 lneto <pre>
8817 1.2 lneto for p, c in utf8.codes(s) do <em>body</em> end
8818 1.2 lneto </pre><p>
8819 1.2 lneto will iterate over all characters in string <code>s</code>,
8820 1.2 lneto with <code>p</code> being the position (in bytes) and <code>c</code> the code point
8821 1.2 lneto of each character.
8822 1.2 lneto It raises an error if it meets any invalid byte sequence.
8823 1.2 lneto
8824 1.2 lneto
8825 1.2 lneto
8826 1.2 lneto
8827 1.2 lneto <p>
8828 1.2 lneto <hr><h3><a name="pdf-utf8.codepoint"><code>utf8.codepoint (s [, i [, j]])</code></a></h3>
8829 1.2 lneto Returns the codepoints (as integers) from all characters in <code>s</code>
8830 1.2 lneto that start between byte position <code>i</code> and <code>j</code> (both included).
8831 1.2 lneto The default for <code>i</code> is 1 and for <code>j</code> is <code>i</code>.
8832 1.2 lneto It raises an error if it meets any invalid byte sequence.
8833 1.2 lneto
8834 1.2 lneto
8835 1.2 lneto
8836 1.2 lneto
8837 1.2 lneto <p>
8838 1.2 lneto <hr><h3><a name="pdf-utf8.len"><code>utf8.len (s [, i [, j]])</code></a></h3>
8839 1.2 lneto Returns the number of UTF-8 characters in string <code>s</code>
8840 1.3 lneto that start between positions <code>i</code> and <code>j</code> (both inclusive).
8841 1.2 lneto The default for <code>i</code> is 1 and for <code>j</code> is -1.
8842 1.2 lneto If it finds any invalid byte sequence,
8843 1.3 lneto returns a false value plus the position of the first invalid byte.
8844 1.2 lneto
8845 1.2 lneto
8846 1.2 lneto
8847 1.2 lneto
8848 1.2 lneto <p>
8849 1.2 lneto <hr><h3><a name="pdf-utf8.offset"><code>utf8.offset (s, n [, i])</code></a></h3>
8850 1.2 lneto Returns the position (in bytes) where the encoding of the
8851 1.2 lneto <code>n</code>-th character of <code>s</code>
8852 1.2 lneto (counting from position <code>i</code>) starts.
8853 1.2 lneto A negative <code>n</code> gets characters before position <code>i</code>.
8854 1.2 lneto The default for <code>i</code> is 1 when <code>n</code> is non-negative
8855 1.2 lneto and <code>#s + 1</code> otherwise,
8856 1.2 lneto so that <code>utf8.offset(s, -n)</code> gets the offset of the
8857 1.2 lneto <code>n</code>-th character from the end of the string.
8858 1.3 lneto If the specified character is neither in the subject
8859 1.3 lneto nor right after its end,
8860 1.2 lneto the function returns <b>nil</b>.
8861 1.2 lneto
8862 1.2 lneto
8863 1.2 lneto <p>
8864 1.2 lneto As a special case,
8865 1.2 lneto when <code>n</code> is 0 the function returns the start of the encoding
8866 1.2 lneto of the character that contains the <code>i</code>-th byte of <code>s</code>.
8867 1.2 lneto
8868 1.2 lneto
8869 1.1 mbalmer <p>
8870 1.2 lneto This function assumes that <code>s</code> is a valid UTF-8 string.
8871 1.1 mbalmer
8872 1.1 mbalmer
8873 1.1 mbalmer
8874 1.1 mbalmer
8875 1.1 mbalmer
8876 1.1 mbalmer
8877 1.1 mbalmer
8878 1.2 lneto <h2>6.6 – <a name="6.6">Table Manipulation</a></h2>
8879 1.2 lneto
8880 1.2 lneto <p>
8881 1.2 lneto This library provides generic functions for table manipulation.
8882 1.2 lneto It provides all its functions inside the table <a name="pdf-table"><code>table</code></a>.
8883 1.1 mbalmer
8884 1.1 mbalmer
8885 1.2 lneto <p>
8886 1.2 lneto Remember that, whenever an operation needs the length of a table,
8887 1.2 lneto the table must be a proper sequence
8888 1.2 lneto or have a <code>__len</code> metamethod (see <a href="#3.4.7">§3.4.7</a>).
8889 1.2 lneto All functions ignore non-numeric keys
8890 1.3 lneto in the tables given as arguments.
8891 1.1 mbalmer
8892 1.1 mbalmer
8893 1.1 mbalmer <p>
8894 1.2 lneto <hr><h3><a name="pdf-table.concat"><code>table.concat (list [, sep [, i [, j]]])</code></a></h3>
8895 1.1 mbalmer
8896 1.1 mbalmer
8897 1.1 mbalmer <p>
8898 1.2 lneto Given a list where all elements are strings or numbers,
8899 1.2 lneto returns the string <code>list[i]..sep..list[i+1] ··· sep..list[j]</code>.
8900 1.1 mbalmer The default value for <code>sep</code> is the empty string,
8901 1.1 mbalmer the default for <code>i</code> is 1,
8902 1.2 lneto and the default for <code>j</code> is <code>#list</code>.
8903 1.1 mbalmer If <code>i</code> is greater than <code>j</code>, returns the empty string.
8904 1.1 mbalmer
8905 1.1 mbalmer
8906 1.1 mbalmer
8907 1.1 mbalmer
8908 1.1 mbalmer <p>
8909 1.2 lneto <hr><h3><a name="pdf-table.insert"><code>table.insert (list, [pos,] value)</code></a></h3>
8910 1.1 mbalmer
8911 1.1 mbalmer
8912 1.1 mbalmer <p>
8913 1.2 lneto Inserts element <code>value</code> at position <code>pos</code> in <code>list</code>,
8914 1.2 lneto shifting up the elements
8915 1.2 lneto <code>list[pos], list[pos+1], ···, list[#list]</code>.
8916 1.2 lneto The default value for <code>pos</code> is <code>#list+1</code>,
8917 1.1 mbalmer so that a call <code>table.insert(t,x)</code> inserts <code>x</code> at the end
8918 1.2 lneto of list <code>t</code>.
8919 1.1 mbalmer
8920 1.1 mbalmer
8921 1.1 mbalmer
8922 1.1 mbalmer
8923 1.1 mbalmer <p>
8924 1.3 lneto <hr><h3><a name="pdf-table.move"><code>table.move (a1, f, e, t [,a2])</code></a></h3>
8925 1.3 lneto
8926 1.3 lneto
8927 1.3 lneto <p>
8928 1.3 lneto Moves elements from table <code>a1</code> to table <code>a2</code>.
8929 1.3 lneto This function performs the equivalent to the following
8930 1.3 lneto multiple assignment:
8931 1.3 lneto <code>a2[t],··· = a1[f],···,a1[e]</code>.
8932 1.3 lneto The default for <code>a2</code> is <code>a1</code>.
8933 1.3 lneto The destination range can overlap with the source range.
8934 1.4 mbalmer The number of elements to be moved must fit in a Lua integer.
8935 1.3 lneto
8936 1.3 lneto
8937 1.3 lneto
8938 1.3 lneto
8939 1.3 lneto <p>
8940 1.2 lneto <hr><h3><a name="pdf-table.pack"><code>table.pack (···)</code></a></h3>
8941 1.1 mbalmer
8942 1.1 mbalmer
8943 1.1 mbalmer <p>
8944 1.2 lneto Returns a new table with all parameters stored into keys 1, 2, etc.
8945 1.2 lneto and with a field "<code>n</code>" with the total number of parameters.
8946 1.2 lneto Note that the resulting table may not be a sequence.
8947 1.1 mbalmer
8948 1.1 mbalmer
8949 1.1 mbalmer
8950 1.1 mbalmer
8951 1.1 mbalmer <p>
8952 1.2 lneto <hr><h3><a name="pdf-table.remove"><code>table.remove (list [, pos])</code></a></h3>
8953 1.1 mbalmer
8954 1.1 mbalmer
8955 1.1 mbalmer <p>
8956 1.2 lneto Removes from <code>list</code> the element at position <code>pos</code>,
8957 1.2 lneto returning the value of the removed element.
8958 1.2 lneto When <code>pos</code> is an integer between 1 and <code>#list</code>,
8959 1.2 lneto it shifts down the elements
8960 1.2 lneto <code>list[pos+1], list[pos+2], ···, list[#list]</code>
8961 1.2 lneto and erases element <code>list[#list]</code>;
8962 1.2 lneto The index <code>pos</code> can also be 0 when <code>#list</code> is 0,
8963 1.2 lneto or <code>#list + 1</code>;
8964 1.2 lneto in those cases, the function erases the element <code>list[pos]</code>.
8965 1.2 lneto
8966 1.2 lneto
8967 1.2 lneto <p>
8968 1.2 lneto The default value for <code>pos</code> is <code>#list</code>,
8969 1.2 lneto so that a call <code>table.remove(l)</code> removes the last element
8970 1.2 lneto of list <code>l</code>.
8971 1.2 lneto
8972 1.1 mbalmer
8973 1.1 mbalmer
8974 1.1 mbalmer
8975 1.2 lneto <p>
8976 1.2 lneto <hr><h3><a name="pdf-table.sort"><code>table.sort (list [, comp])</code></a></h3>
8977 1.2 lneto
8978 1.1 mbalmer
8979 1.1 mbalmer <p>
8980 1.2 lneto Sorts list elements in a given order, <em>in-place</em>,
8981 1.2 lneto from <code>list[1]</code> to <code>list[#list]</code>.
8982 1.1 mbalmer If <code>comp</code> is given,
8983 1.2 lneto then it must be a function that receives two list elements
8984 1.2 lneto and returns true when the first element must come
8985 1.2 lneto before the second in the final order
8986 1.5 lneto (so that, after the sort,
8987 1.5 lneto <code>i < j</code> implies <code>not comp(list[j],list[i])</code>).
8988 1.1 mbalmer If <code>comp</code> is not given,
8989 1.1 mbalmer then the standard Lua operator <code><</code> is used instead.
8990 1.1 mbalmer
8991 1.1 mbalmer
8992 1.1 mbalmer <p>
8993 1.5 lneto Note that the <code>comp</code> function must define
8994 1.5 lneto a strict partial order over the elements in the list;
8995 1.5 lneto that is, it must be asymmetric and transitive.
8996 1.5 lneto Otherwise, no valid sort may be possible.
8997 1.5 lneto
8998 1.5 lneto
8999 1.5 lneto <p>
9000 1.1 mbalmer The sort algorithm is not stable;
9001 1.5 lneto that is, elements not comparable by the given order
9002 1.5 lneto (e.g., equal elements)
9003 1.1 mbalmer may have their relative positions changed by the sort.
9004 1.1 mbalmer
9005 1.1 mbalmer
9006 1.1 mbalmer
9007 1.1 mbalmer
9008 1.2 lneto <p>
9009 1.2 lneto <hr><h3><a name="pdf-table.unpack"><code>table.unpack (list [, i [, j]])</code></a></h3>
9010 1.2 lneto
9011 1.2 lneto
9012 1.2 lneto <p>
9013 1.2 lneto Returns the elements from the given list.
9014 1.2 lneto This function is equivalent to
9015 1.2 lneto
9016 1.2 lneto <pre>
9017 1.2 lneto return list[i], list[i+1], ···, list[j]
9018 1.2 lneto </pre><p>
9019 1.2 lneto By default, <code>i</code> is 1 and <code>j</code> is <code>#list</code>.
9020 1.2 lneto
9021 1.2 lneto
9022 1.2 lneto
9023 1.1 mbalmer
9024 1.1 mbalmer
9025 1.1 mbalmer
9026 1.2 lneto
9027 1.2 lneto <h2>6.7 – <a name="6.7">Mathematical Functions</a></h2>
9028 1.1 mbalmer
9029 1.1 mbalmer <p>
9030 1.2 lneto This library provides basic mathematical functions.
9031 1.2 lneto It provides all its functions and constants inside the table <a name="pdf-math"><code>math</code></a>.
9032 1.2 lneto Functions with the annotation "<code>integer/float</code>" give
9033 1.2 lneto integer results for integer arguments
9034 1.2 lneto and float results for float (or mixed) arguments.
9035 1.2 lneto Rounding functions
9036 1.2 lneto (<a href="#pdf-math.ceil"><code>math.ceil</code></a>, <a href="#pdf-math.floor"><code>math.floor</code></a>, and <a href="#pdf-math.modf"><code>math.modf</code></a>)
9037 1.2 lneto return an integer when the result fits in the range of an integer,
9038 1.3 lneto or a float otherwise.
9039 1.1 mbalmer
9040 1.1 mbalmer
9041 1.1 mbalmer <p>
9042 1.1 mbalmer <hr><h3><a name="pdf-math.abs"><code>math.abs (x)</code></a></h3>
9043 1.1 mbalmer
9044 1.1 mbalmer
9045 1.1 mbalmer <p>
9046 1.2 lneto Returns the absolute value of <code>x</code>. (integer/float)
9047 1.1 mbalmer
9048 1.1 mbalmer
9049 1.1 mbalmer
9050 1.1 mbalmer
9051 1.1 mbalmer <p>
9052 1.1 mbalmer <hr><h3><a name="pdf-math.acos"><code>math.acos (x)</code></a></h3>
9053 1.1 mbalmer
9054 1.1 mbalmer
9055 1.1 mbalmer <p>
9056 1.1 mbalmer Returns the arc cosine of <code>x</code> (in radians).
9057 1.1 mbalmer
9058 1.1 mbalmer
9059 1.1 mbalmer
9060 1.1 mbalmer
9061 1.1 mbalmer <p>
9062 1.1 mbalmer <hr><h3><a name="pdf-math.asin"><code>math.asin (x)</code></a></h3>
9063 1.1 mbalmer
9064 1.1 mbalmer
9065 1.1 mbalmer <p>
9066 1.1 mbalmer Returns the arc sine of <code>x</code> (in radians).
9067 1.1 mbalmer
9068 1.1 mbalmer
9069 1.1 mbalmer
9070 1.1 mbalmer
9071 1.1 mbalmer <p>
9072 1.2 lneto <hr><h3><a name="pdf-math.atan"><code>math.atan (y [, x])</code></a></h3>
9073 1.1 mbalmer
9074 1.1 mbalmer
9075 1.1 mbalmer <p>
9076 1.1 mbalmer
9077 1.1 mbalmer Returns the arc tangent of <code>y/x</code> (in radians),
9078 1.1 mbalmer but uses the signs of both parameters to find the
9079 1.1 mbalmer quadrant of the result.
9080 1.1 mbalmer (It also handles correctly the case of <code>x</code> being zero.)
9081 1.1 mbalmer
9082 1.1 mbalmer
9083 1.2 lneto <p>
9084 1.2 lneto The default value for <code>x</code> is 1,
9085 1.2 lneto so that the call <code>math.atan(y)</code>
9086 1.2 lneto returns the arc tangent of <code>y</code>.
9087 1.2 lneto
9088 1.2 lneto
9089 1.1 mbalmer
9090 1.1 mbalmer
9091 1.1 mbalmer <p>
9092 1.1 mbalmer <hr><h3><a name="pdf-math.ceil"><code>math.ceil (x)</code></a></h3>
9093 1.1 mbalmer
9094 1.1 mbalmer
9095 1.1 mbalmer <p>
9096 1.3 lneto Returns the smallest integral value larger than or equal to <code>x</code>.
9097 1.1 mbalmer
9098 1.1 mbalmer
9099 1.1 mbalmer
9100 1.1 mbalmer
9101 1.1 mbalmer <p>
9102 1.1 mbalmer <hr><h3><a name="pdf-math.cos"><code>math.cos (x)</code></a></h3>
9103 1.1 mbalmer
9104 1.1 mbalmer
9105 1.1 mbalmer <p>
9106 1.1 mbalmer Returns the cosine of <code>x</code> (assumed to be in radians).
9107 1.1 mbalmer
9108 1.1 mbalmer
9109 1.1 mbalmer
9110 1.1 mbalmer
9111 1.1 mbalmer <p>
9112 1.1 mbalmer <hr><h3><a name="pdf-math.deg"><code>math.deg (x)</code></a></h3>
9113 1.1 mbalmer
9114 1.1 mbalmer
9115 1.1 mbalmer <p>
9116 1.2 lneto Converts the angle <code>x</code> from radians to degrees.
9117 1.1 mbalmer
9118 1.1 mbalmer
9119 1.1 mbalmer
9120 1.1 mbalmer
9121 1.1 mbalmer <p>
9122 1.3 lneto <hr><h3><a name="pdf-math.exp"><code>math.exp (x)</code></a></h3>
9123 1.3 lneto
9124 1.3 lneto
9125 1.3 lneto <p>
9126 1.3 lneto Returns the value <em>e<sup>x</sup></em>
9127 1.3 lneto (where <code>e</code> is the base of natural logarithms).
9128 1.3 lneto
9129 1.3 lneto
9130 1.3 lneto
9131 1.3 lneto
9132 1.3 lneto <p>
9133 1.1 mbalmer <hr><h3><a name="pdf-math.floor"><code>math.floor (x)</code></a></h3>
9134 1.1 mbalmer
9135 1.1 mbalmer
9136 1.1 mbalmer <p>
9137 1.2 lneto Returns the largest integral value smaller than or equal to <code>x</code>.
9138 1.1 mbalmer
9139 1.1 mbalmer
9140 1.1 mbalmer
9141 1.1 mbalmer
9142 1.1 mbalmer <p>
9143 1.1 mbalmer <hr><h3><a name="pdf-math.fmod"><code>math.fmod (x, y)</code></a></h3>
9144 1.1 mbalmer
9145 1.1 mbalmer
9146 1.1 mbalmer <p>
9147 1.1 mbalmer Returns the remainder of the division of <code>x</code> by <code>y</code>
9148 1.2 lneto that rounds the quotient towards zero. (integer/float)
9149 1.1 mbalmer
9150 1.1 mbalmer
9151 1.1 mbalmer
9152 1.1 mbalmer
9153 1.1 mbalmer <p>
9154 1.2 lneto <hr><h3><a name="pdf-math.huge"><code>math.huge</code></a></h3>
9155 1.1 mbalmer
9156 1.1 mbalmer
9157 1.1 mbalmer <p>
9158 1.2 lneto The float value <code>HUGE_VAL</code>,
9159 1.4 mbalmer a value larger than any other numeric value.
9160 1.1 mbalmer
9161 1.1 mbalmer
9162 1.1 mbalmer
9163 1.1 mbalmer
9164 1.1 mbalmer <p>
9165 1.2 lneto <hr><h3><a name="pdf-math.log"><code>math.log (x [, base])</code></a></h3>
9166 1.1 mbalmer
9167 1.1 mbalmer
9168 1.1 mbalmer <p>
9169 1.2 lneto Returns the logarithm of <code>x</code> in the given base.
9170 1.2 lneto The default for <code>base</code> is <em>e</em>
9171 1.2 lneto (so that the function returns the natural logarithm of <code>x</code>).
9172 1.1 mbalmer
9173 1.1 mbalmer
9174 1.1 mbalmer
9175 1.1 mbalmer
9176 1.1 mbalmer <p>
9177 1.2 lneto <hr><h3><a name="pdf-math.max"><code>math.max (x, ···)</code></a></h3>
9178 1.1 mbalmer
9179 1.1 mbalmer
9180 1.1 mbalmer <p>
9181 1.2 lneto Returns the argument with the maximum value,
9182 1.2 lneto according to the Lua operator <code><</code>. (integer/float)
9183 1.1 mbalmer
9184 1.1 mbalmer
9185 1.1 mbalmer
9186 1.1 mbalmer
9187 1.1 mbalmer <p>
9188 1.2 lneto <hr><h3><a name="pdf-math.maxinteger"><code>math.maxinteger</code></a></h3>
9189 1.2 lneto An integer with the maximum value for an integer.
9190 1.1 mbalmer
9191 1.1 mbalmer
9192 1.1 mbalmer
9193 1.1 mbalmer
9194 1.1 mbalmer <p>
9195 1.2 lneto <hr><h3><a name="pdf-math.min"><code>math.min (x, ···)</code></a></h3>
9196 1.1 mbalmer
9197 1.1 mbalmer
9198 1.1 mbalmer <p>
9199 1.2 lneto Returns the argument with the minimum value,
9200 1.2 lneto according to the Lua operator <code><</code>. (integer/float)
9201 1.1 mbalmer
9202 1.1 mbalmer
9203 1.1 mbalmer
9204 1.1 mbalmer
9205 1.1 mbalmer <p>
9206 1.2 lneto <hr><h3><a name="pdf-math.mininteger"><code>math.mininteger</code></a></h3>
9207 1.2 lneto An integer with the minimum value for an integer.
9208 1.1 mbalmer
9209 1.1 mbalmer
9210 1.1 mbalmer
9211 1.1 mbalmer
9212 1.1 mbalmer <p>
9213 1.1 mbalmer <hr><h3><a name="pdf-math.modf"><code>math.modf (x)</code></a></h3>
9214 1.1 mbalmer
9215 1.1 mbalmer
9216 1.1 mbalmer <p>
9217 1.2 lneto Returns the integral part of <code>x</code> and the fractional part of <code>x</code>.
9218 1.2 lneto Its second result is always a float.
9219 1.1 mbalmer
9220 1.1 mbalmer
9221 1.1 mbalmer
9222 1.1 mbalmer
9223 1.1 mbalmer <p>
9224 1.1 mbalmer <hr><h3><a name="pdf-math.pi"><code>math.pi</code></a></h3>
9225 1.1 mbalmer
9226 1.1 mbalmer
9227 1.1 mbalmer <p>
9228 1.2 lneto The value of <em>π</em>.
9229 1.1 mbalmer
9230 1.1 mbalmer
9231 1.1 mbalmer
9232 1.1 mbalmer
9233 1.1 mbalmer <p>
9234 1.1 mbalmer <hr><h3><a name="pdf-math.rad"><code>math.rad (x)</code></a></h3>
9235 1.1 mbalmer
9236 1.1 mbalmer
9237 1.1 mbalmer <p>
9238 1.2 lneto Converts the angle <code>x</code> from degrees to radians.
9239 1.1 mbalmer
9240 1.1 mbalmer
9241 1.1 mbalmer
9242 1.1 mbalmer
9243 1.1 mbalmer <p>
9244 1.1 mbalmer <hr><h3><a name="pdf-math.random"><code>math.random ([m [, n]])</code></a></h3>
9245 1.1 mbalmer
9246 1.1 mbalmer
9247 1.1 mbalmer <p>
9248 1.2 lneto When called without arguments,
9249 1.2 lneto returns a pseudo-random float with uniform distribution
9250 1.2 lneto in the range <em>[0,1)</em>.
9251 1.2 lneto When called with two integers <code>m</code> and <code>n</code>,
9252 1.2 lneto <code>math.random</code> returns a pseudo-random integer
9253 1.2 lneto with uniform distribution in the range <em>[m, n]</em>.
9254 1.5 lneto (The value <em>n-m</em> cannot be negative and must fit in a Lua integer.)
9255 1.2 lneto The call <code>math.random(n)</code> is equivalent to <code>math.random(1,n)</code>.
9256 1.1 mbalmer
9257 1.1 mbalmer
9258 1.1 mbalmer <p>
9259 1.2 lneto This function is an interface to the underling
9260 1.2 lneto pseudo-random generator function provided by C.
9261 1.1 mbalmer
9262 1.1 mbalmer
9263 1.1 mbalmer
9264 1.1 mbalmer
9265 1.1 mbalmer <p>
9266 1.1 mbalmer <hr><h3><a name="pdf-math.randomseed"><code>math.randomseed (x)</code></a></h3>
9267 1.1 mbalmer
9268 1.1 mbalmer
9269 1.1 mbalmer <p>
9270 1.1 mbalmer Sets <code>x</code> as the "seed"
9271 1.1 mbalmer for the pseudo-random generator:
9272 1.1 mbalmer equal seeds produce equal sequences of numbers.
9273 1.1 mbalmer
9274 1.1 mbalmer
9275 1.1 mbalmer
9276 1.1 mbalmer
9277 1.1 mbalmer <p>
9278 1.1 mbalmer <hr><h3><a name="pdf-math.sin"><code>math.sin (x)</code></a></h3>
9279 1.1 mbalmer
9280 1.1 mbalmer
9281 1.1 mbalmer <p>
9282 1.1 mbalmer Returns the sine of <code>x</code> (assumed to be in radians).
9283 1.1 mbalmer
9284 1.1 mbalmer
9285 1.1 mbalmer
9286 1.1 mbalmer
9287 1.1 mbalmer <p>
9288 1.1 mbalmer <hr><h3><a name="pdf-math.sqrt"><code>math.sqrt (x)</code></a></h3>
9289 1.1 mbalmer
9290 1.1 mbalmer
9291 1.1 mbalmer <p>
9292 1.1 mbalmer Returns the square root of <code>x</code>.
9293 1.1 mbalmer (You can also use the expression <code>x^0.5</code> to compute this value.)
9294 1.1 mbalmer
9295 1.1 mbalmer
9296 1.1 mbalmer
9297 1.1 mbalmer
9298 1.1 mbalmer <p>
9299 1.1 mbalmer <hr><h3><a name="pdf-math.tan"><code>math.tan (x)</code></a></h3>
9300 1.1 mbalmer
9301 1.1 mbalmer
9302 1.1 mbalmer <p>
9303 1.1 mbalmer Returns the tangent of <code>x</code> (assumed to be in radians).
9304 1.1 mbalmer
9305 1.1 mbalmer
9306 1.1 mbalmer
9307 1.1 mbalmer
9308 1.1 mbalmer <p>
9309 1.3 lneto <hr><h3><a name="pdf-math.tointeger"><code>math.tointeger (x)</code></a></h3>
9310 1.3 lneto
9311 1.3 lneto
9312 1.3 lneto <p>
9313 1.3 lneto If the value <code>x</code> is convertible to an integer,
9314 1.3 lneto returns that integer.
9315 1.3 lneto Otherwise, returns <b>nil</b>.
9316 1.3 lneto
9317 1.3 lneto
9318 1.3 lneto
9319 1.3 lneto
9320 1.3 lneto <p>
9321 1.2 lneto <hr><h3><a name="pdf-math.type"><code>math.type (x)</code></a></h3>
9322 1.1 mbalmer
9323 1.1 mbalmer
9324 1.1 mbalmer <p>
9325 1.2 lneto Returns "<code>integer</code>" if <code>x</code> is an integer,
9326 1.2 lneto "<code>float</code>" if it is a float,
9327 1.2 lneto or <b>nil</b> if <code>x</code> is not a number.
9328 1.1 mbalmer
9329 1.1 mbalmer
9330 1.1 mbalmer
9331 1.1 mbalmer
9332 1.3 lneto <p>
9333 1.3 lneto <hr><h3><a name="pdf-math.ult"><code>math.ult (m, n)</code></a></h3>
9334 1.3 lneto
9335 1.3 lneto
9336 1.3 lneto <p>
9337 1.3 lneto Returns a boolean,
9338 1.3 lneto true if integer <code>m</code> is below integer <code>n</code> when
9339 1.3 lneto they are compared as unsigned integers.
9340 1.3 lneto
9341 1.3 lneto
9342 1.3 lneto
9343 1.3 lneto
9344 1.1 mbalmer
9345 1.1 mbalmer
9346 1.1 mbalmer
9347 1.2 lneto <h2>6.8 – <a name="6.8">Input and Output Facilities</a></h2>
9348 1.1 mbalmer
9349 1.1 mbalmer <p>
9350 1.1 mbalmer The I/O library provides two different styles for file manipulation.
9351 1.2 lneto The first one uses implicit file handles;
9352 1.1 mbalmer that is, there are operations to set a default input file and a
9353 1.1 mbalmer default output file,
9354 1.1 mbalmer and all input/output operations are over these default files.
9355 1.2 lneto The second style uses explicit file handles.
9356 1.1 mbalmer
9357 1.1 mbalmer
9358 1.1 mbalmer <p>
9359 1.2 lneto When using implicit file handles,
9360 1.1 mbalmer all operations are supplied by table <a name="pdf-io"><code>io</code></a>.
9361 1.2 lneto When using explicit file handles,
9362 1.2 lneto the operation <a href="#pdf-io.open"><code>io.open</code></a> returns a file handle
9363 1.2 lneto and then all operations are supplied as methods of the file handle.
9364 1.1 mbalmer
9365 1.1 mbalmer
9366 1.1 mbalmer <p>
9367 1.1 mbalmer The table <code>io</code> also provides
9368 1.2 lneto three predefined file handles with their usual meanings from C:
9369 1.1 mbalmer <a name="pdf-io.stdin"><code>io.stdin</code></a>, <a name="pdf-io.stdout"><code>io.stdout</code></a>, and <a name="pdf-io.stderr"><code>io.stderr</code></a>.
9370 1.1 mbalmer The I/O library never closes these files.
9371 1.1 mbalmer
9372 1.1 mbalmer
9373 1.1 mbalmer <p>
9374 1.1 mbalmer Unless otherwise stated,
9375 1.1 mbalmer all I/O functions return <b>nil</b> on failure
9376 1.1 mbalmer (plus an error message as a second result and
9377 1.1 mbalmer a system-dependent error code as a third result)
9378 1.1 mbalmer and some value different from <b>nil</b> on success.
9379 1.2 lneto On non-POSIX systems,
9380 1.2 lneto the computation of the error message and error code
9381 1.2 lneto in case of errors
9382 1.2 lneto may be not thread safe,
9383 1.2 lneto because they rely on the global C variable <code>errno</code>.
9384 1.1 mbalmer
9385 1.1 mbalmer
9386 1.1 mbalmer <p>
9387 1.1 mbalmer <hr><h3><a name="pdf-io.close"><code>io.close ([file])</code></a></h3>
9388 1.1 mbalmer
9389 1.1 mbalmer
9390 1.1 mbalmer <p>
9391 1.1 mbalmer Equivalent to <code>file:close()</code>.
9392 1.1 mbalmer Without a <code>file</code>, closes the default output file.
9393 1.1 mbalmer
9394 1.1 mbalmer
9395 1.1 mbalmer
9396 1.1 mbalmer
9397 1.1 mbalmer <p>
9398 1.1 mbalmer <hr><h3><a name="pdf-io.flush"><code>io.flush ()</code></a></h3>
9399 1.1 mbalmer
9400 1.1 mbalmer
9401 1.1 mbalmer <p>
9402 1.2 lneto Equivalent to <code>io.output():flush()</code>.
9403 1.1 mbalmer
9404 1.1 mbalmer
9405 1.1 mbalmer
9406 1.1 mbalmer
9407 1.1 mbalmer <p>
9408 1.1 mbalmer <hr><h3><a name="pdf-io.input"><code>io.input ([file])</code></a></h3>
9409 1.1 mbalmer
9410 1.1 mbalmer
9411 1.1 mbalmer <p>
9412 1.1 mbalmer When called with a file name, it opens the named file (in text mode),
9413 1.1 mbalmer and sets its handle as the default input file.
9414 1.1 mbalmer When called with a file handle,
9415 1.1 mbalmer it simply sets this file handle as the default input file.
9416 1.1 mbalmer When called without parameters,
9417 1.1 mbalmer it returns the current default input file.
9418 1.1 mbalmer
9419 1.1 mbalmer
9420 1.1 mbalmer <p>
9421 1.1 mbalmer In case of errors this function raises the error,
9422 1.1 mbalmer instead of returning an error code.
9423 1.1 mbalmer
9424 1.1 mbalmer
9425 1.1 mbalmer
9426 1.1 mbalmer
9427 1.1 mbalmer <p>
9428 1.5 lneto <hr><h3><a name="pdf-io.lines"><code>io.lines ([filename, ···])</code></a></h3>
9429 1.1 mbalmer
9430 1.1 mbalmer
9431 1.1 mbalmer <p>
9432 1.1 mbalmer Opens the given file name in read mode
9433 1.2 lneto and returns an iterator function that
9434 1.2 lneto works like <code>file:lines(···)</code> over the opened file.
9435 1.1 mbalmer When the iterator function detects the end of file,
9436 1.2 lneto it returns no values (to finish the loop) and automatically closes the file.
9437 1.1 mbalmer
9438 1.1 mbalmer
9439 1.1 mbalmer <p>
9440 1.1 mbalmer The call <code>io.lines()</code> (with no file name) is equivalent
9441 1.2 lneto to <code>io.input():lines("*l")</code>;
9442 1.1 mbalmer that is, it iterates over the lines of the default input file.
9443 1.1 mbalmer In this case it does not close the file when the loop ends.
9444 1.1 mbalmer
9445 1.1 mbalmer
9446 1.2 lneto <p>
9447 1.2 lneto In case of errors this function raises the error,
9448 1.2 lneto instead of returning an error code.
9449 1.2 lneto
9450 1.2 lneto
9451 1.1 mbalmer
9452 1.1 mbalmer
9453 1.1 mbalmer <p>
9454 1.1 mbalmer <hr><h3><a name="pdf-io.open"><code>io.open (filename [, mode])</code></a></h3>
9455 1.1 mbalmer
9456 1.1 mbalmer
9457 1.1 mbalmer <p>
9458 1.1 mbalmer This function opens a file,
9459 1.1 mbalmer in the mode specified in the string <code>mode</code>.
9460 1.1 mbalmer It returns a new file handle,
9461 1.1 mbalmer or, in case of errors, <b>nil</b> plus an error message.
9462 1.1 mbalmer
9463 1.1 mbalmer
9464 1.1 mbalmer <p>
9465 1.1 mbalmer The <code>mode</code> string can be any of the following:
9466 1.1 mbalmer
9467 1.1 mbalmer <ul>
9468 1.2 lneto <li><b>"<code>r</code>": </b> read mode (the default);</li>
9469 1.2 lneto <li><b>"<code>w</code>": </b> write mode;</li>
9470 1.2 lneto <li><b>"<code>a</code>": </b> append mode;</li>
9471 1.2 lneto <li><b>"<code>r+</code>": </b> update mode, all previous data is preserved;</li>
9472 1.2 lneto <li><b>"<code>w+</code>": </b> update mode, all previous data is erased;</li>
9473 1.2 lneto <li><b>"<code>a+</code>": </b> append update mode, previous data is preserved,
9474 1.1 mbalmer writing is only allowed at the end of file.</li>
9475 1.1 mbalmer </ul><p>
9476 1.1 mbalmer The <code>mode</code> string can also have a '<code>b</code>' at the end,
9477 1.1 mbalmer which is needed in some systems to open the file in binary mode.
9478 1.1 mbalmer
9479 1.1 mbalmer
9480 1.1 mbalmer
9481 1.1 mbalmer
9482 1.1 mbalmer <p>
9483 1.1 mbalmer <hr><h3><a name="pdf-io.output"><code>io.output ([file])</code></a></h3>
9484 1.1 mbalmer
9485 1.1 mbalmer
9486 1.1 mbalmer <p>
9487 1.1 mbalmer Similar to <a href="#pdf-io.input"><code>io.input</code></a>, but operates over the default output file.
9488 1.1 mbalmer
9489 1.1 mbalmer
9490 1.1 mbalmer
9491 1.1 mbalmer
9492 1.1 mbalmer <p>
9493 1.1 mbalmer <hr><h3><a name="pdf-io.popen"><code>io.popen (prog [, mode])</code></a></h3>
9494 1.1 mbalmer
9495 1.1 mbalmer
9496 1.1 mbalmer <p>
9497 1.2 lneto This function is system dependent and is not available
9498 1.2 lneto on all platforms.
9499 1.2 lneto
9500 1.2 lneto
9501 1.2 lneto <p>
9502 1.1 mbalmer Starts program <code>prog</code> in a separated process and returns
9503 1.1 mbalmer a file handle that you can use to read data from this program
9504 1.1 mbalmer (if <code>mode</code> is <code>"r"</code>, the default)
9505 1.1 mbalmer or to write data to this program
9506 1.1 mbalmer (if <code>mode</code> is <code>"w"</code>).
9507 1.1 mbalmer
9508 1.1 mbalmer
9509 1.1 mbalmer
9510 1.1 mbalmer
9511 1.1 mbalmer <p>
9512 1.1 mbalmer <hr><h3><a name="pdf-io.read"><code>io.read (···)</code></a></h3>
9513 1.1 mbalmer
9514 1.1 mbalmer
9515 1.1 mbalmer <p>
9516 1.2 lneto Equivalent to <code>io.input():read(···)</code>.
9517 1.1 mbalmer
9518 1.1 mbalmer
9519 1.1 mbalmer
9520 1.1 mbalmer
9521 1.1 mbalmer <p>
9522 1.1 mbalmer <hr><h3><a name="pdf-io.tmpfile"><code>io.tmpfile ()</code></a></h3>
9523 1.1 mbalmer
9524 1.1 mbalmer
9525 1.1 mbalmer <p>
9526 1.1 mbalmer Returns a handle for a temporary file.
9527 1.1 mbalmer This file is opened in update mode
9528 1.1 mbalmer and it is automatically removed when the program ends.
9529 1.1 mbalmer
9530 1.1 mbalmer
9531 1.1 mbalmer
9532 1.1 mbalmer
9533 1.1 mbalmer <p>
9534 1.1 mbalmer <hr><h3><a name="pdf-io.type"><code>io.type (obj)</code></a></h3>
9535 1.1 mbalmer
9536 1.1 mbalmer
9537 1.1 mbalmer <p>
9538 1.1 mbalmer Checks whether <code>obj</code> is a valid file handle.
9539 1.1 mbalmer Returns the string <code>"file"</code> if <code>obj</code> is an open file handle,
9540 1.1 mbalmer <code>"closed file"</code> if <code>obj</code> is a closed file handle,
9541 1.1 mbalmer or <b>nil</b> if <code>obj</code> is not a file handle.
9542 1.1 mbalmer
9543 1.1 mbalmer
9544 1.1 mbalmer
9545 1.1 mbalmer
9546 1.1 mbalmer <p>
9547 1.1 mbalmer <hr><h3><a name="pdf-io.write"><code>io.write (···)</code></a></h3>
9548 1.1 mbalmer
9549 1.1 mbalmer
9550 1.1 mbalmer <p>
9551 1.2 lneto Equivalent to <code>io.output():write(···)</code>.
9552 1.1 mbalmer
9553 1.1 mbalmer
9554 1.1 mbalmer
9555 1.1 mbalmer
9556 1.1 mbalmer <p>
9557 1.1 mbalmer <hr><h3><a name="pdf-file:close"><code>file:close ()</code></a></h3>
9558 1.1 mbalmer
9559 1.1 mbalmer
9560 1.1 mbalmer <p>
9561 1.1 mbalmer Closes <code>file</code>.
9562 1.1 mbalmer Note that files are automatically closed when
9563 1.1 mbalmer their handles are garbage collected,
9564 1.1 mbalmer but that takes an unpredictable amount of time to happen.
9565 1.1 mbalmer
9566 1.1 mbalmer
9567 1.2 lneto <p>
9568 1.2 lneto When closing a file handle created with <a href="#pdf-io.popen"><code>io.popen</code></a>,
9569 1.2 lneto <a href="#pdf-file:close"><code>file:close</code></a> returns the same values
9570 1.2 lneto returned by <a href="#pdf-os.execute"><code>os.execute</code></a>.
9571 1.2 lneto
9572 1.2 lneto
9573 1.1 mbalmer
9574 1.1 mbalmer
9575 1.1 mbalmer <p>
9576 1.1 mbalmer <hr><h3><a name="pdf-file:flush"><code>file:flush ()</code></a></h3>
9577 1.1 mbalmer
9578 1.1 mbalmer
9579 1.1 mbalmer <p>
9580 1.1 mbalmer Saves any written data to <code>file</code>.
9581 1.1 mbalmer
9582 1.1 mbalmer
9583 1.1 mbalmer
9584 1.1 mbalmer
9585 1.1 mbalmer <p>
9586 1.2 lneto <hr><h3><a name="pdf-file:lines"><code>file:lines (···)</code></a></h3>
9587 1.1 mbalmer
9588 1.1 mbalmer
9589 1.1 mbalmer <p>
9590 1.1 mbalmer Returns an iterator function that,
9591 1.1 mbalmer each time it is called,
9592 1.2 lneto reads the file according to the given formats.
9593 1.2 lneto When no format is given,
9594 1.2 lneto uses "<code>l</code>" as a default.
9595 1.2 lneto As an example, the construction
9596 1.1 mbalmer
9597 1.1 mbalmer <pre>
9598 1.2 lneto for c in file:lines(1) do <em>body</em> end
9599 1.2 lneto </pre><p>
9600 1.2 lneto will iterate over all characters of the file,
9601 1.2 lneto starting at the current position.
9602 1.2 lneto Unlike <a href="#pdf-io.lines"><code>io.lines</code></a>, this function does not close the file
9603 1.2 lneto when the loop ends.
9604 1.2 lneto
9605 1.2 lneto
9606 1.2 lneto <p>
9607 1.2 lneto In case of errors this function raises the error,
9608 1.2 lneto instead of returning an error code.
9609 1.1 mbalmer
9610 1.1 mbalmer
9611 1.1 mbalmer
9612 1.1 mbalmer
9613 1.1 mbalmer <p>
9614 1.1 mbalmer <hr><h3><a name="pdf-file:read"><code>file:read (···)</code></a></h3>
9615 1.1 mbalmer
9616 1.1 mbalmer
9617 1.1 mbalmer <p>
9618 1.1 mbalmer Reads the file <code>file</code>,
9619 1.1 mbalmer according to the given formats, which specify what to read.
9620 1.1 mbalmer For each format,
9621 1.2 lneto the function returns a string or a number with the characters read,
9622 1.1 mbalmer or <b>nil</b> if it cannot read data with the specified format.
9623 1.3 lneto (In this latter case,
9624 1.3 lneto the function does not read subsequent formats.)
9625 1.1 mbalmer When called without formats,
9626 1.2 lneto it uses a default format that reads the next line
9627 1.1 mbalmer (see below).
9628 1.1 mbalmer
9629 1.1 mbalmer
9630 1.1 mbalmer <p>
9631 1.1 mbalmer The available formats are
9632 1.1 mbalmer
9633 1.1 mbalmer <ul>
9634 1.1 mbalmer
9635 1.2 lneto <li><b>"<code>n</code>": </b>
9636 1.2 lneto reads a numeral and returns it as a float or an integer,
9637 1.2 lneto following the lexical conventions of Lua.
9638 1.2 lneto (The numeral may have leading spaces and a sign.)
9639 1.2 lneto This format always reads the longest input sequence that
9640 1.4 mbalmer is a valid prefix for a numeral;
9641 1.4 mbalmer if that prefix does not form a valid numeral
9642 1.2 lneto (e.g., an empty string, "<code>0x</code>", or "<code>3.4e-</code>"),
9643 1.2 lneto it is discarded and the function returns <b>nil</b>.
9644 1.2 lneto </li>
9645 1.2 lneto
9646 1.2 lneto <li><b>"<code>a</code>": </b>
9647 1.1 mbalmer reads the whole file, starting at the current position.
9648 1.1 mbalmer On end of file, it returns the empty string.
9649 1.1 mbalmer </li>
9650 1.1 mbalmer
9651 1.2 lneto <li><b>"<code>l</code>": </b>
9652 1.2 lneto reads the next line skipping the end of line,
9653 1.1 mbalmer returning <b>nil</b> on end of file.
9654 1.1 mbalmer This is the default format.
9655 1.1 mbalmer </li>
9656 1.1 mbalmer
9657 1.2 lneto <li><b>"<code>L</code>": </b>
9658 1.2 lneto reads the next line keeping the end-of-line character (if present),
9659 1.2 lneto returning <b>nil</b> on end of file.
9660 1.2 lneto </li>
9661 1.2 lneto
9662 1.2 lneto <li><b><em>number</em>: </b>
9663 1.2 lneto reads a string with up to this number of bytes,
9664 1.1 mbalmer returning <b>nil</b> on end of file.
9665 1.2 lneto If <code>number</code> is zero,
9666 1.1 mbalmer it reads nothing and returns an empty string,
9667 1.1 mbalmer or <b>nil</b> on end of file.
9668 1.1 mbalmer </li>
9669 1.1 mbalmer
9670 1.2 lneto </ul><p>
9671 1.2 lneto The formats "<code>l</code>" and "<code>L</code>" should be used only for text files.
9672 1.2 lneto
9673 1.1 mbalmer
9674 1.1 mbalmer
9675 1.1 mbalmer
9676 1.1 mbalmer <p>
9677 1.2 lneto <hr><h3><a name="pdf-file:seek"><code>file:seek ([whence [, offset]])</code></a></h3>
9678 1.1 mbalmer
9679 1.1 mbalmer
9680 1.1 mbalmer <p>
9681 1.1 mbalmer Sets and gets the file position,
9682 1.1 mbalmer measured from the beginning of the file,
9683 1.1 mbalmer to the position given by <code>offset</code> plus a base
9684 1.1 mbalmer specified by the string <code>whence</code>, as follows:
9685 1.1 mbalmer
9686 1.1 mbalmer <ul>
9687 1.2 lneto <li><b>"<code>set</code>": </b> base is position 0 (beginning of the file);</li>
9688 1.2 lneto <li><b>"<code>cur</code>": </b> base is current position;</li>
9689 1.2 lneto <li><b>"<code>end</code>": </b> base is end of file;</li>
9690 1.1 mbalmer </ul><p>
9691 1.2 lneto In case of success, <code>seek</code> returns the final file position,
9692 1.1 mbalmer measured in bytes from the beginning of the file.
9693 1.2 lneto If <code>seek</code> fails, it returns <b>nil</b>,
9694 1.1 mbalmer plus a string describing the error.
9695 1.1 mbalmer
9696 1.1 mbalmer
9697 1.1 mbalmer <p>
9698 1.1 mbalmer The default value for <code>whence</code> is <code>"cur"</code>,
9699 1.1 mbalmer and for <code>offset</code> is 0.
9700 1.1 mbalmer Therefore, the call <code>file:seek()</code> returns the current
9701 1.1 mbalmer file position, without changing it;
9702 1.1 mbalmer the call <code>file:seek("set")</code> sets the position to the
9703 1.1 mbalmer beginning of the file (and returns 0);
9704 1.1 mbalmer and the call <code>file:seek("end")</code> sets the position to the
9705 1.1 mbalmer end of the file, and returns its size.
9706 1.1 mbalmer
9707 1.1 mbalmer
9708 1.1 mbalmer
9709 1.1 mbalmer
9710 1.1 mbalmer <p>
9711 1.1 mbalmer <hr><h3><a name="pdf-file:setvbuf"><code>file:setvbuf (mode [, size])</code></a></h3>
9712 1.1 mbalmer
9713 1.1 mbalmer
9714 1.1 mbalmer <p>
9715 1.1 mbalmer Sets the buffering mode for an output file.
9716 1.1 mbalmer There are three available modes:
9717 1.1 mbalmer
9718 1.1 mbalmer <ul>
9719 1.1 mbalmer
9720 1.2 lneto <li><b>"<code>no</code>": </b>
9721 1.1 mbalmer no buffering; the result of any output operation appears immediately.
9722 1.1 mbalmer </li>
9723 1.1 mbalmer
9724 1.2 lneto <li><b>"<code>full</code>": </b>
9725 1.1 mbalmer full buffering; output operation is performed only
9726 1.2 lneto when the buffer is full or when
9727 1.2 lneto you explicitly <code>flush</code> the file (see <a href="#pdf-io.flush"><code>io.flush</code></a>).
9728 1.1 mbalmer </li>
9729 1.1 mbalmer
9730 1.2 lneto <li><b>"<code>line</code>": </b>
9731 1.1 mbalmer line buffering; output is buffered until a newline is output
9732 1.1 mbalmer or there is any input from some special files
9733 1.1 mbalmer (such as a terminal device).
9734 1.1 mbalmer </li>
9735 1.1 mbalmer
9736 1.1 mbalmer </ul><p>
9737 1.1 mbalmer For the last two cases, <code>size</code>
9738 1.1 mbalmer specifies the size of the buffer, in bytes.
9739 1.1 mbalmer The default is an appropriate size.
9740 1.1 mbalmer
9741 1.1 mbalmer
9742 1.1 mbalmer
9743 1.1 mbalmer
9744 1.1 mbalmer <p>
9745 1.1 mbalmer <hr><h3><a name="pdf-file:write"><code>file:write (···)</code></a></h3>
9746 1.1 mbalmer
9747 1.1 mbalmer
9748 1.1 mbalmer <p>
9749 1.2 lneto Writes the value of each of its arguments to <code>file</code>.
9750 1.1 mbalmer The arguments must be strings or numbers.
9751 1.2 lneto
9752 1.2 lneto
9753 1.2 lneto <p>
9754 1.2 lneto In case of success, this function returns <code>file</code>.
9755 1.2 lneto Otherwise it returns <b>nil</b> plus a string describing the error.
9756 1.1 mbalmer
9757 1.1 mbalmer
9758 1.1 mbalmer
9759 1.1 mbalmer
9760 1.1 mbalmer
9761 1.1 mbalmer
9762 1.1 mbalmer
9763 1.2 lneto <h2>6.9 – <a name="6.9">Operating System Facilities</a></h2>
9764 1.1 mbalmer
9765 1.1 mbalmer <p>
9766 1.1 mbalmer This library is implemented through table <a name="pdf-os"><code>os</code></a>.
9767 1.1 mbalmer
9768 1.1 mbalmer
9769 1.1 mbalmer <p>
9770 1.1 mbalmer <hr><h3><a name="pdf-os.clock"><code>os.clock ()</code></a></h3>
9771 1.1 mbalmer
9772 1.1 mbalmer
9773 1.1 mbalmer <p>
9774 1.1 mbalmer Returns an approximation of the amount in seconds of CPU time
9775 1.1 mbalmer used by the program.
9776 1.1 mbalmer
9777 1.1 mbalmer
9778 1.1 mbalmer
9779 1.1 mbalmer
9780 1.1 mbalmer <p>
9781 1.1 mbalmer <hr><h3><a name="pdf-os.date"><code>os.date ([format [, time]])</code></a></h3>
9782 1.1 mbalmer
9783 1.1 mbalmer
9784 1.1 mbalmer <p>
9785 1.1 mbalmer Returns a string or a table containing date and time,
9786 1.1 mbalmer formatted according to the given string <code>format</code>.
9787 1.1 mbalmer
9788 1.1 mbalmer
9789 1.1 mbalmer <p>
9790 1.1 mbalmer If the <code>time</code> argument is present,
9791 1.1 mbalmer this is the time to be formatted
9792 1.1 mbalmer (see the <a href="#pdf-os.time"><code>os.time</code></a> function for a description of this value).
9793 1.1 mbalmer Otherwise, <code>date</code> formats the current time.
9794 1.1 mbalmer
9795 1.1 mbalmer
9796 1.1 mbalmer <p>
9797 1.1 mbalmer If <code>format</code> starts with '<code>!</code>',
9798 1.1 mbalmer then the date is formatted in Coordinated Universal Time.
9799 1.1 mbalmer After this optional character,
9800 1.1 mbalmer if <code>format</code> is the string "<code>*t</code>",
9801 1.1 mbalmer then <code>date</code> returns a table with the following fields:
9802 1.5 lneto <code>year</code>, <code>month</code> (1–12), <code>day</code> (1–31),
9803 1.2 lneto <code>hour</code> (0–23), <code>min</code> (0–59), <code>sec</code> (0–61),
9804 1.1 mbalmer <code>wday</code> (weekday, Sunday is 1),
9805 1.1 mbalmer <code>yday</code> (day of the year),
9806 1.1 mbalmer and <code>isdst</code> (daylight saving flag, a boolean).
9807 1.2 lneto This last field may be absent
9808 1.2 lneto if the information is not available.
9809 1.1 mbalmer
9810 1.1 mbalmer
9811 1.1 mbalmer <p>
9812 1.1 mbalmer If <code>format</code> is not "<code>*t</code>",
9813 1.1 mbalmer then <code>date</code> returns the date as a string,
9814 1.3 lneto formatted according to the same rules as the ISO C function <code>strftime</code>.
9815 1.1 mbalmer
9816 1.1 mbalmer
9817 1.1 mbalmer <p>
9818 1.1 mbalmer When called without arguments,
9819 1.1 mbalmer <code>date</code> returns a reasonable date and time representation that depends on
9820 1.5 lneto the host system and on the current locale.
9821 1.5 lneto (More specifically, <code>os.date()</code> is equivalent to <code>os.date("%c")</code>.)
9822 1.1 mbalmer
9823 1.1 mbalmer
9824 1.2 lneto <p>
9825 1.2 lneto On non-POSIX systems,
9826 1.2 lneto this function may be not thread safe
9827 1.2 lneto because of its reliance on C function <code>gmtime</code> and C function <code>localtime</code>.
9828 1.2 lneto
9829 1.2 lneto
9830 1.1 mbalmer
9831 1.1 mbalmer
9832 1.1 mbalmer <p>
9833 1.1 mbalmer <hr><h3><a name="pdf-os.difftime"><code>os.difftime (t2, t1)</code></a></h3>
9834 1.1 mbalmer
9835 1.1 mbalmer
9836 1.1 mbalmer <p>
9837 1.3 lneto Returns the difference, in seconds,
9838 1.3 lneto from time <code>t1</code> to time <code>t2</code>
9839 1.3 lneto (where the times are values returned by <a href="#pdf-os.time"><code>os.time</code></a>).
9840 1.1 mbalmer In POSIX, Windows, and some other systems,
9841 1.1 mbalmer this value is exactly <code>t2</code><em>-</em><code>t1</code>.
9842 1.1 mbalmer
9843 1.1 mbalmer
9844 1.1 mbalmer
9845 1.1 mbalmer
9846 1.1 mbalmer <p>
9847 1.1 mbalmer <hr><h3><a name="pdf-os.execute"><code>os.execute ([command])</code></a></h3>
9848 1.1 mbalmer
9849 1.1 mbalmer
9850 1.1 mbalmer <p>
9851 1.3 lneto This function is equivalent to the ISO C function <code>system</code>.
9852 1.1 mbalmer It passes <code>command</code> to be executed by an operating system shell.
9853 1.2 lneto Its first result is <b>true</b>
9854 1.2 lneto if the command terminated successfully,
9855 1.2 lneto or <b>nil</b> otherwise.
9856 1.2 lneto After this first result
9857 1.2 lneto the function returns a string plus a number,
9858 1.2 lneto as follows:
9859 1.2 lneto
9860 1.2 lneto <ul>
9861 1.2 lneto
9862 1.2 lneto <li><b>"<code>exit</code>": </b>
9863 1.2 lneto the command terminated normally;
9864 1.2 lneto the following number is the exit status of the command.
9865 1.2 lneto </li>
9866 1.2 lneto
9867 1.2 lneto <li><b>"<code>signal</code>": </b>
9868 1.2 lneto the command was terminated by a signal;
9869 1.2 lneto the following number is the signal that terminated the command.
9870 1.2 lneto </li>
9871 1.2 lneto
9872 1.2 lneto </ul>
9873 1.2 lneto
9874 1.2 lneto <p>
9875 1.2 lneto When called without a <code>command</code>,
9876 1.2 lneto <code>os.execute</code> returns a boolean that is true if a shell is available.
9877 1.2 lneto
9878 1.1 mbalmer
9879 1.1 mbalmer
9880 1.1 mbalmer
9881 1.2 lneto <p>
9882 1.2 lneto <hr><h3><a name="pdf-os.exit"><code>os.exit ([code [, close]])</code></a></h3>
9883 1.2 lneto
9884 1.1 mbalmer
9885 1.1 mbalmer <p>
9886 1.3 lneto Calls the ISO C function <code>exit</code> to terminate the host program.
9887 1.2 lneto If <code>code</code> is <b>true</b>,
9888 1.2 lneto the returned status is <code>EXIT_SUCCESS</code>;
9889 1.2 lneto if <code>code</code> is <b>false</b>,
9890 1.2 lneto the returned status is <code>EXIT_FAILURE</code>;
9891 1.2 lneto if <code>code</code> is a number,
9892 1.2 lneto the returned status is this number.
9893 1.2 lneto The default value for <code>code</code> is <b>true</b>.
9894 1.1 mbalmer
9895 1.1 mbalmer
9896 1.1 mbalmer <p>
9897 1.2 lneto If the optional second argument <code>close</code> is true,
9898 1.2 lneto closes the Lua state before exiting.
9899 1.1 mbalmer
9900 1.1 mbalmer
9901 1.1 mbalmer
9902 1.1 mbalmer
9903 1.1 mbalmer <p>
9904 1.1 mbalmer <hr><h3><a name="pdf-os.getenv"><code>os.getenv (varname)</code></a></h3>
9905 1.1 mbalmer
9906 1.1 mbalmer
9907 1.1 mbalmer <p>
9908 1.1 mbalmer Returns the value of the process environment variable <code>varname</code>,
9909 1.1 mbalmer or <b>nil</b> if the variable is not defined.
9910 1.1 mbalmer
9911 1.1 mbalmer
9912 1.1 mbalmer
9913 1.1 mbalmer
9914 1.1 mbalmer <p>
9915 1.1 mbalmer <hr><h3><a name="pdf-os.remove"><code>os.remove (filename)</code></a></h3>
9916 1.1 mbalmer
9917 1.1 mbalmer
9918 1.1 mbalmer <p>
9919 1.2 lneto Deletes the file (or empty directory, on POSIX systems)
9920 1.2 lneto with the given name.
9921 1.1 mbalmer If this function fails, it returns <b>nil</b>,
9922 1.2 lneto plus a string describing the error and the error code.
9923 1.1 mbalmer
9924 1.1 mbalmer
9925 1.1 mbalmer
9926 1.1 mbalmer
9927 1.1 mbalmer <p>
9928 1.1 mbalmer <hr><h3><a name="pdf-os.rename"><code>os.rename (oldname, newname)</code></a></h3>
9929 1.1 mbalmer
9930 1.1 mbalmer
9931 1.1 mbalmer <p>
9932 1.1 mbalmer Renames file or directory named <code>oldname</code> to <code>newname</code>.
9933 1.1 mbalmer If this function fails, it returns <b>nil</b>,
9934 1.2 lneto plus a string describing the error and the error code.
9935 1.1 mbalmer
9936 1.1 mbalmer
9937 1.1 mbalmer
9938 1.1 mbalmer
9939 1.1 mbalmer <p>
9940 1.1 mbalmer <hr><h3><a name="pdf-os.setlocale"><code>os.setlocale (locale [, category])</code></a></h3>
9941 1.1 mbalmer
9942 1.1 mbalmer
9943 1.1 mbalmer <p>
9944 1.1 mbalmer Sets the current locale of the program.
9945 1.2 lneto <code>locale</code> is a system-dependent string specifying a locale;
9946 1.1 mbalmer <code>category</code> is an optional string describing which category to change:
9947 1.1 mbalmer <code>"all"</code>, <code>"collate"</code>, <code>"ctype"</code>,
9948 1.1 mbalmer <code>"monetary"</code>, <code>"numeric"</code>, or <code>"time"</code>;
9949 1.1 mbalmer the default category is <code>"all"</code>.
9950 1.1 mbalmer The function returns the name of the new locale,
9951 1.1 mbalmer or <b>nil</b> if the request cannot be honored.
9952 1.1 mbalmer
9953 1.1 mbalmer
9954 1.1 mbalmer <p>
9955 1.1 mbalmer If <code>locale</code> is the empty string,
9956 1.1 mbalmer the current locale is set to an implementation-defined native locale.
9957 1.1 mbalmer If <code>locale</code> is the string "<code>C</code>",
9958 1.1 mbalmer the current locale is set to the standard C locale.
9959 1.1 mbalmer
9960 1.1 mbalmer
9961 1.1 mbalmer <p>
9962 1.1 mbalmer When called with <b>nil</b> as the first argument,
9963 1.1 mbalmer this function only returns the name of the current locale
9964 1.1 mbalmer for the given category.
9965 1.1 mbalmer
9966 1.1 mbalmer
9967 1.2 lneto <p>
9968 1.2 lneto This function may be not thread safe
9969 1.2 lneto because of its reliance on C function <code>setlocale</code>.
9970 1.2 lneto
9971 1.2 lneto
9972 1.1 mbalmer
9973 1.1 mbalmer
9974 1.1 mbalmer <p>
9975 1.1 mbalmer <hr><h3><a name="pdf-os.time"><code>os.time ([table])</code></a></h3>
9976 1.1 mbalmer
9977 1.1 mbalmer
9978 1.1 mbalmer <p>
9979 1.1 mbalmer Returns the current time when called without arguments,
9980 1.4 mbalmer or a time representing the local date and time specified by the given table.
9981 1.1 mbalmer This table must have fields <code>year</code>, <code>month</code>, and <code>day</code>,
9982 1.2 lneto and may have fields
9983 1.2 lneto <code>hour</code> (default is 12),
9984 1.2 lneto <code>min</code> (default is 0),
9985 1.2 lneto <code>sec</code> (default is 0),
9986 1.2 lneto and <code>isdst</code> (default is <b>nil</b>).
9987 1.4 mbalmer Other fields are ignored.
9988 1.2 lneto For a description of these fields, see the <a href="#pdf-os.date"><code>os.date</code></a> function.
9989 1.1 mbalmer
9990 1.1 mbalmer
9991 1.1 mbalmer <p>
9992 1.4 mbalmer The values in these fields do not need to be inside their valid ranges.
9993 1.4 mbalmer For instance, if <code>sec</code> is -10,
9994 1.4 mbalmer it means -10 seconds from the time specified by the other fields;
9995 1.4 mbalmer if <code>hour</code> is 1000,
9996 1.4 mbalmer it means +1000 hours from the time specified by the other fields.
9997 1.4 mbalmer
9998 1.4 mbalmer
9999 1.4 mbalmer <p>
10000 1.1 mbalmer The returned value is a number, whose meaning depends on your system.
10001 1.2 lneto In POSIX, Windows, and some other systems,
10002 1.2 lneto this number counts the number
10003 1.1 mbalmer of seconds since some given start time (the "epoch").
10004 1.1 mbalmer In other systems, the meaning is not specified,
10005 1.1 mbalmer and the number returned by <code>time</code> can be used only as an argument to
10006 1.2 lneto <a href="#pdf-os.date"><code>os.date</code></a> and <a href="#pdf-os.difftime"><code>os.difftime</code></a>.
10007 1.1 mbalmer
10008 1.1 mbalmer
10009 1.1 mbalmer
10010 1.1 mbalmer
10011 1.1 mbalmer <p>
10012 1.1 mbalmer <hr><h3><a name="pdf-os.tmpname"><code>os.tmpname ()</code></a></h3>
10013 1.1 mbalmer
10014 1.1 mbalmer
10015 1.1 mbalmer <p>
10016 1.1 mbalmer Returns a string with a file name that can
10017 1.1 mbalmer be used for a temporary file.
10018 1.1 mbalmer The file must be explicitly opened before its use
10019 1.1 mbalmer and explicitly removed when no longer needed.
10020 1.1 mbalmer
10021 1.1 mbalmer
10022 1.1 mbalmer <p>
10023 1.2 lneto On POSIX systems,
10024 1.1 mbalmer this function also creates a file with that name,
10025 1.1 mbalmer to avoid security risks.
10026 1.1 mbalmer (Someone else might create the file with wrong permissions
10027 1.1 mbalmer in the time between getting the name and creating the file.)
10028 1.1 mbalmer You still have to open the file to use it
10029 1.1 mbalmer and to remove it (even if you do not use it).
10030 1.1 mbalmer
10031 1.1 mbalmer
10032 1.1 mbalmer <p>
10033 1.1 mbalmer When possible,
10034 1.1 mbalmer you may prefer to use <a href="#pdf-io.tmpfile"><code>io.tmpfile</code></a>,
10035 1.1 mbalmer which automatically removes the file when the program ends.
10036 1.1 mbalmer
10037 1.1 mbalmer
10038 1.1 mbalmer
10039 1.1 mbalmer
10040 1.1 mbalmer
10041 1.1 mbalmer
10042 1.1 mbalmer
10043 1.2 lneto <h2>6.10 – <a name="6.10">The Debug Library</a></h2>
10044 1.1 mbalmer
10045 1.1 mbalmer <p>
10046 1.1 mbalmer This library provides
10047 1.2 lneto the functionality of the debug interface (<a href="#4.9">§4.9</a>) to Lua programs.
10048 1.1 mbalmer You should exert care when using this library.
10049 1.2 lneto Several of its functions
10050 1.2 lneto violate basic assumptions about Lua code
10051 1.1 mbalmer (e.g., that variables local to a function
10052 1.2 lneto cannot be accessed from outside;
10053 1.2 lneto that userdata metatables cannot be changed by Lua code;
10054 1.2 lneto that Lua programs do not crash)
10055 1.1 mbalmer and therefore can compromise otherwise secure code.
10056 1.2 lneto Moreover, some functions in this library may be slow.
10057 1.1 mbalmer
10058 1.1 mbalmer
10059 1.1 mbalmer <p>
10060 1.1 mbalmer All functions in this library are provided
10061 1.1 mbalmer inside the <a name="pdf-debug"><code>debug</code></a> table.
10062 1.1 mbalmer All functions that operate over a thread
10063 1.1 mbalmer have an optional first argument which is the
10064 1.1 mbalmer thread to operate over.
10065 1.1 mbalmer The default is always the current thread.
10066 1.1 mbalmer
10067 1.1 mbalmer
10068 1.1 mbalmer <p>
10069 1.1 mbalmer <hr><h3><a name="pdf-debug.debug"><code>debug.debug ()</code></a></h3>
10070 1.1 mbalmer
10071 1.1 mbalmer
10072 1.1 mbalmer <p>
10073 1.1 mbalmer Enters an interactive mode with the user,
10074 1.1 mbalmer running each string that the user enters.
10075 1.1 mbalmer Using simple commands and other debug facilities,
10076 1.1 mbalmer the user can inspect global and local variables,
10077 1.1 mbalmer change their values, evaluate expressions, and so on.
10078 1.1 mbalmer A line containing only the word <code>cont</code> finishes this function,
10079 1.1 mbalmer so that the caller continues its execution.
10080 1.1 mbalmer
10081 1.1 mbalmer
10082 1.1 mbalmer <p>
10083 1.1 mbalmer Note that commands for <code>debug.debug</code> are not lexically nested
10084 1.2 lneto within any function and so have no direct access to local variables.
10085 1.1 mbalmer
10086 1.1 mbalmer
10087 1.1 mbalmer
10088 1.1 mbalmer
10089 1.1 mbalmer <p>
10090 1.1 mbalmer <hr><h3><a name="pdf-debug.gethook"><code>debug.gethook ([thread])</code></a></h3>
10091 1.1 mbalmer
10092 1.1 mbalmer
10093 1.1 mbalmer <p>
10094 1.1 mbalmer Returns the current hook settings of the thread, as three values:
10095 1.1 mbalmer the current hook function, the current hook mask,
10096 1.1 mbalmer and the current hook count
10097 1.1 mbalmer (as set by the <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> function).
10098 1.1 mbalmer
10099 1.1 mbalmer
10100 1.1 mbalmer
10101 1.1 mbalmer
10102 1.1 mbalmer <p>
10103 1.2 lneto <hr><h3><a name="pdf-debug.getinfo"><code>debug.getinfo ([thread,] f [, what])</code></a></h3>
10104 1.1 mbalmer
10105 1.1 mbalmer
10106 1.1 mbalmer <p>
10107 1.1 mbalmer Returns a table with information about a function.
10108 1.2 lneto You can give the function directly
10109 1.2 lneto or you can give a number as the value of <code>f</code>,
10110 1.2 lneto which means the function running at level <code>f</code> of the call stack
10111 1.1 mbalmer of the given thread:
10112 1.1 mbalmer level 0 is the current function (<code>getinfo</code> itself);
10113 1.2 lneto level 1 is the function that called <code>getinfo</code>
10114 1.2 lneto (except for tail calls, which do not count on the stack);
10115 1.1 mbalmer and so on.
10116 1.2 lneto If <code>f</code> is a number larger than the number of active functions,
10117 1.1 mbalmer then <code>getinfo</code> returns <b>nil</b>.
10118 1.1 mbalmer
10119 1.1 mbalmer
10120 1.1 mbalmer <p>
10121 1.1 mbalmer The returned table can contain all the fields returned by <a href="#lua_getinfo"><code>lua_getinfo</code></a>,
10122 1.1 mbalmer with the string <code>what</code> describing which fields to fill in.
10123 1.1 mbalmer The default for <code>what</code> is to get all information available,
10124 1.1 mbalmer except the table of valid lines.
10125 1.1 mbalmer If present,
10126 1.1 mbalmer the option '<code>f</code>'
10127 1.1 mbalmer adds a field named <code>func</code> with the function itself.
10128 1.1 mbalmer If present,
10129 1.1 mbalmer the option '<code>L</code>'
10130 1.1 mbalmer adds a field named <code>activelines</code> with the table of
10131 1.1 mbalmer valid lines.
10132 1.1 mbalmer
10133 1.1 mbalmer
10134 1.1 mbalmer <p>
10135 1.1 mbalmer For instance, the expression <code>debug.getinfo(1,"n").name</code> returns
10136 1.4 mbalmer a name for the current function,
10137 1.1 mbalmer if a reasonable name can be found,
10138 1.1 mbalmer and the expression <code>debug.getinfo(print)</code>
10139 1.1 mbalmer returns a table with all available information
10140 1.1 mbalmer about the <a href="#pdf-print"><code>print</code></a> function.
10141 1.1 mbalmer
10142 1.1 mbalmer
10143 1.1 mbalmer
10144 1.1 mbalmer
10145 1.1 mbalmer <p>
10146 1.2 lneto <hr><h3><a name="pdf-debug.getlocal"><code>debug.getlocal ([thread,] f, local)</code></a></h3>
10147 1.1 mbalmer
10148 1.1 mbalmer
10149 1.1 mbalmer <p>
10150 1.1 mbalmer This function returns the name and the value of the local variable
10151 1.2 lneto with index <code>local</code> of the function at level <code>f</code> of the stack.
10152 1.2 lneto This function accesses not only explicit local variables,
10153 1.2 lneto but also parameters, temporaries, etc.
10154 1.2 lneto
10155 1.2 lneto
10156 1.2 lneto <p>
10157 1.2 lneto The first parameter or local variable has index 1, and so on,
10158 1.3 lneto following the order that they are declared in the code,
10159 1.3 lneto counting only the variables that are active
10160 1.3 lneto in the current scope of the function.
10161 1.2 lneto Negative indices refer to vararg parameters;
10162 1.2 lneto -1 is the first vararg parameter.
10163 1.2 lneto The function returns <b>nil</b> if there is no variable with the given index,
10164 1.2 lneto and raises an error when called with a level out of range.
10165 1.1 mbalmer (You can call <a href="#pdf-debug.getinfo"><code>debug.getinfo</code></a> to check whether the level is valid.)
10166 1.1 mbalmer
10167 1.1 mbalmer
10168 1.1 mbalmer <p>
10169 1.2 lneto Variable names starting with '<code>(</code>' (open parenthesis)
10170 1.2 lneto represent variables with no known names
10171 1.3 lneto (internal variables such as loop control variables,
10172 1.2 lneto and variables from chunks saved without debug information).
10173 1.2 lneto
10174 1.2 lneto
10175 1.2 lneto <p>
10176 1.2 lneto The parameter <code>f</code> may also be a function.
10177 1.2 lneto In that case, <code>getlocal</code> returns only the name of function parameters.
10178 1.1 mbalmer
10179 1.1 mbalmer
10180 1.1 mbalmer
10181 1.1 mbalmer
10182 1.1 mbalmer <p>
10183 1.2 lneto <hr><h3><a name="pdf-debug.getmetatable"><code>debug.getmetatable (value)</code></a></h3>
10184 1.1 mbalmer
10185 1.1 mbalmer
10186 1.1 mbalmer <p>
10187 1.2 lneto Returns the metatable of the given <code>value</code>
10188 1.1 mbalmer or <b>nil</b> if it does not have a metatable.
10189 1.1 mbalmer
10190 1.1 mbalmer
10191 1.1 mbalmer
10192 1.1 mbalmer
10193 1.1 mbalmer <p>
10194 1.1 mbalmer <hr><h3><a name="pdf-debug.getregistry"><code>debug.getregistry ()</code></a></h3>
10195 1.1 mbalmer
10196 1.1 mbalmer
10197 1.1 mbalmer <p>
10198 1.2 lneto Returns the registry table (see <a href="#4.5">§4.5</a>).
10199 1.1 mbalmer
10200 1.1 mbalmer
10201 1.1 mbalmer
10202 1.1 mbalmer
10203 1.1 mbalmer <p>
10204 1.2 lneto <hr><h3><a name="pdf-debug.getupvalue"><code>debug.getupvalue (f, up)</code></a></h3>
10205 1.1 mbalmer
10206 1.1 mbalmer
10207 1.1 mbalmer <p>
10208 1.1 mbalmer This function returns the name and the value of the upvalue
10209 1.2 lneto with index <code>up</code> of the function <code>f</code>.
10210 1.1 mbalmer The function returns <b>nil</b> if there is no upvalue with the given index.
10211 1.1 mbalmer
10212 1.1 mbalmer
10213 1.2 lneto <p>
10214 1.2 lneto Variable names starting with '<code>(</code>' (open parenthesis)
10215 1.2 lneto represent variables with no known names
10216 1.2 lneto (variables from chunks saved without debug information).
10217 1.2 lneto
10218 1.2 lneto
10219 1.1 mbalmer
10220 1.1 mbalmer
10221 1.1 mbalmer <p>
10222 1.2 lneto <hr><h3><a name="pdf-debug.getuservalue"><code>debug.getuservalue (u)</code></a></h3>
10223 1.1 mbalmer
10224 1.1 mbalmer
10225 1.1 mbalmer <p>
10226 1.2 lneto Returns the Lua value associated to <code>u</code>.
10227 1.2 lneto If <code>u</code> is not a userdata,
10228 1.2 lneto returns <b>nil</b>.
10229 1.1 mbalmer
10230 1.1 mbalmer
10231 1.1 mbalmer
10232 1.1 mbalmer
10233 1.1 mbalmer <p>
10234 1.1 mbalmer <hr><h3><a name="pdf-debug.sethook"><code>debug.sethook ([thread,] hook, mask [, count])</code></a></h3>
10235 1.1 mbalmer
10236 1.1 mbalmer
10237 1.1 mbalmer <p>
10238 1.1 mbalmer Sets the given function as a hook.
10239 1.1 mbalmer The string <code>mask</code> and the number <code>count</code> describe
10240 1.1 mbalmer when the hook will be called.
10241 1.2 lneto The string mask may have any combination of the following characters,
10242 1.1 mbalmer with the given meaning:
10243 1.1 mbalmer
10244 1.1 mbalmer <ul>
10245 1.2 lneto <li><b>'<code>c</code>': </b> the hook is called every time Lua calls a function;</li>
10246 1.2 lneto <li><b>'<code>r</code>': </b> the hook is called every time Lua returns from a function;</li>
10247 1.2 lneto <li><b>'<code>l</code>': </b> the hook is called every time Lua enters a new line of code.</li>
10248 1.1 mbalmer </ul><p>
10249 1.2 lneto Moreover,
10250 1.2 lneto with a <code>count</code> different from zero,
10251 1.2 lneto the hook is called also after every <code>count</code> instructions.
10252 1.1 mbalmer
10253 1.1 mbalmer
10254 1.1 mbalmer <p>
10255 1.1 mbalmer When called without arguments,
10256 1.1 mbalmer <a href="#pdf-debug.sethook"><code>debug.sethook</code></a> turns off the hook.
10257 1.1 mbalmer
10258 1.1 mbalmer
10259 1.1 mbalmer <p>
10260 1.1 mbalmer When the hook is called, its first parameter is a string
10261 1.1 mbalmer describing the event that has triggered its call:
10262 1.2 lneto <code>"call"</code> (or <code>"tail call"</code>),
10263 1.2 lneto <code>"return"</code>,
10264 1.1 mbalmer <code>"line"</code>, and <code>"count"</code>.
10265 1.1 mbalmer For line events,
10266 1.1 mbalmer the hook also gets the new line number as its second parameter.
10267 1.1 mbalmer Inside a hook,
10268 1.1 mbalmer you can call <code>getinfo</code> with level 2 to get more information about
10269 1.1 mbalmer the running function
10270 1.1 mbalmer (level 0 is the <code>getinfo</code> function,
10271 1.2 lneto and level 1 is the hook function).
10272 1.1 mbalmer
10273 1.1 mbalmer
10274 1.1 mbalmer
10275 1.1 mbalmer
10276 1.1 mbalmer <p>
10277 1.1 mbalmer <hr><h3><a name="pdf-debug.setlocal"><code>debug.setlocal ([thread,] level, local, value)</code></a></h3>
10278 1.1 mbalmer
10279 1.1 mbalmer
10280 1.1 mbalmer <p>
10281 1.1 mbalmer This function assigns the value <code>value</code> to the local variable
10282 1.1 mbalmer with index <code>local</code> of the function at level <code>level</code> of the stack.
10283 1.1 mbalmer The function returns <b>nil</b> if there is no local
10284 1.1 mbalmer variable with the given index,
10285 1.1 mbalmer and raises an error when called with a <code>level</code> out of range.
10286 1.1 mbalmer (You can call <code>getinfo</code> to check whether the level is valid.)
10287 1.1 mbalmer Otherwise, it returns the name of the local variable.
10288 1.1 mbalmer
10289 1.1 mbalmer
10290 1.2 lneto <p>
10291 1.2 lneto See <a href="#pdf-debug.getlocal"><code>debug.getlocal</code></a> for more information about
10292 1.2 lneto variable indices and names.
10293 1.2 lneto
10294 1.2 lneto
10295 1.1 mbalmer
10296 1.1 mbalmer
10297 1.1 mbalmer <p>
10298 1.2 lneto <hr><h3><a name="pdf-debug.setmetatable"><code>debug.setmetatable (value, table)</code></a></h3>
10299 1.1 mbalmer
10300 1.1 mbalmer
10301 1.1 mbalmer <p>
10302 1.2 lneto Sets the metatable for the given <code>value</code> to the given <code>table</code>
10303 1.1 mbalmer (which can be <b>nil</b>).
10304 1.2 lneto Returns <code>value</code>.
10305 1.1 mbalmer
10306 1.1 mbalmer
10307 1.1 mbalmer
10308 1.1 mbalmer
10309 1.1 mbalmer <p>
10310 1.2 lneto <hr><h3><a name="pdf-debug.setupvalue"><code>debug.setupvalue (f, up, value)</code></a></h3>
10311 1.1 mbalmer
10312 1.1 mbalmer
10313 1.1 mbalmer <p>
10314 1.1 mbalmer This function assigns the value <code>value</code> to the upvalue
10315 1.2 lneto with index <code>up</code> of the function <code>f</code>.
10316 1.1 mbalmer The function returns <b>nil</b> if there is no upvalue
10317 1.1 mbalmer with the given index.
10318 1.1 mbalmer Otherwise, it returns the name of the upvalue.
10319 1.1 mbalmer
10320 1.1 mbalmer
10321 1.1 mbalmer
10322 1.1 mbalmer
10323 1.1 mbalmer <p>
10324 1.2 lneto <hr><h3><a name="pdf-debug.setuservalue"><code>debug.setuservalue (udata, value)</code></a></h3>
10325 1.2 lneto
10326 1.2 lneto
10327 1.2 lneto <p>
10328 1.2 lneto Sets the given <code>value</code> as
10329 1.2 lneto the Lua value associated to the given <code>udata</code>.
10330 1.2 lneto <code>udata</code> must be a full userdata.
10331 1.2 lneto
10332 1.2 lneto
10333 1.2 lneto <p>
10334 1.2 lneto Returns <code>udata</code>.
10335 1.2 lneto
10336 1.2 lneto
10337 1.2 lneto
10338 1.2 lneto
10339 1.2 lneto <p>
10340 1.2 lneto <hr><h3><a name="pdf-debug.traceback"><code>debug.traceback ([thread,] [message [, level]])</code></a></h3>
10341 1.1 mbalmer
10342 1.1 mbalmer
10343 1.1 mbalmer <p>
10344 1.2 lneto If <code>message</code> is present but is neither a string nor <b>nil</b>,
10345 1.2 lneto this function returns <code>message</code> without further processing.
10346 1.2 lneto Otherwise,
10347 1.2 lneto it returns a string with a traceback of the call stack.
10348 1.3 lneto The optional <code>message</code> string is appended
10349 1.1 mbalmer at the beginning of the traceback.
10350 1.1 mbalmer An optional <code>level</code> number tells at which level
10351 1.1 mbalmer to start the traceback
10352 1.1 mbalmer (default is 1, the function calling <code>traceback</code>).
10353 1.1 mbalmer
10354 1.1 mbalmer
10355 1.1 mbalmer
10356 1.1 mbalmer
10357 1.2 lneto <p>
10358 1.2 lneto <hr><h3><a name="pdf-debug.upvalueid"><code>debug.upvalueid (f, n)</code></a></h3>
10359 1.2 lneto
10360 1.2 lneto
10361 1.2 lneto <p>
10362 1.3 lneto Returns a unique identifier (as a light userdata)
10363 1.2 lneto for the upvalue numbered <code>n</code>
10364 1.2 lneto from the given function.
10365 1.2 lneto
10366 1.2 lneto
10367 1.2 lneto <p>
10368 1.2 lneto These unique identifiers allow a program to check whether different
10369 1.2 lneto closures share upvalues.
10370 1.2 lneto Lua closures that share an upvalue
10371 1.2 lneto (that is, that access a same external local variable)
10372 1.2 lneto will return identical ids for those upvalue indices.
10373 1.2 lneto
10374 1.2 lneto
10375 1.2 lneto
10376 1.2 lneto
10377 1.2 lneto <p>
10378 1.2 lneto <hr><h3><a name="pdf-debug.upvaluejoin"><code>debug.upvaluejoin (f1, n1, f2, n2)</code></a></h3>
10379 1.2 lneto
10380 1.2 lneto
10381 1.2 lneto <p>
10382 1.2 lneto Make the <code>n1</code>-th upvalue of the Lua closure <code>f1</code>
10383 1.2 lneto refer to the <code>n2</code>-th upvalue of the Lua closure <code>f2</code>.
10384 1.2 lneto
10385 1.2 lneto
10386 1.2 lneto
10387 1.2 lneto
10388 1.1 mbalmer
10389 1.1 mbalmer
10390 1.1 mbalmer
10391 1.2 lneto <h1>7 – <a name="7">Lua Standalone</a></h1>
10392 1.1 mbalmer
10393 1.1 mbalmer <p>
10394 1.1 mbalmer Although Lua has been designed as an extension language,
10395 1.1 mbalmer to be embedded in a host C program,
10396 1.2 lneto it is also frequently used as a standalone language.
10397 1.2 lneto An interpreter for Lua as a standalone language,
10398 1.1 mbalmer called simply <code>lua</code>,
10399 1.1 mbalmer is provided with the standard distribution.
10400 1.2 lneto The standalone interpreter includes
10401 1.1 mbalmer all standard libraries, including the debug library.
10402 1.1 mbalmer Its usage is:
10403 1.1 mbalmer
10404 1.1 mbalmer <pre>
10405 1.1 mbalmer lua [options] [script [args]]
10406 1.1 mbalmer </pre><p>
10407 1.1 mbalmer The options are:
10408 1.1 mbalmer
10409 1.1 mbalmer <ul>
10410 1.2 lneto <li><b><code>-e <em>stat</em></code>: </b> executes string <em>stat</em>;</li>
10411 1.2 lneto <li><b><code>-l <em>mod</em></code>: </b> "requires" <em>mod</em>;</li>
10412 1.2 lneto <li><b><code>-i</code>: </b> enters interactive mode after running <em>script</em>;</li>
10413 1.2 lneto <li><b><code>-v</code>: </b> prints version information;</li>
10414 1.2 lneto <li><b><code>-E</code>: </b> ignores environment variables;</li>
10415 1.2 lneto <li><b><code>--</code>: </b> stops handling options;</li>
10416 1.2 lneto <li><b><code>-</code>: </b> executes <code>stdin</code> as a file and stops handling options.</li>
10417 1.1 mbalmer </ul><p>
10418 1.3 lneto After handling its options, <code>lua</code> runs the given <em>script</em>.
10419 1.1 mbalmer When called without arguments,
10420 1.1 mbalmer <code>lua</code> behaves as <code>lua -v -i</code>
10421 1.1 mbalmer when the standard input (<code>stdin</code>) is a terminal,
10422 1.1 mbalmer and as <code>lua -</code> otherwise.
10423 1.1 mbalmer
10424 1.1 mbalmer
10425 1.1 mbalmer <p>
10426 1.2 lneto When called without option <code>-E</code>,
10427 1.2 lneto the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_3"><code>LUA_INIT_5_3</code></a>
10428 1.3 lneto (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if the versioned name is not defined)
10429 1.2 lneto before running any argument.
10430 1.2 lneto If the variable content has the format <code>@<em>filename</em></code>,
10431 1.1 mbalmer then <code>lua</code> executes the file.
10432 1.1 mbalmer Otherwise, <code>lua</code> executes the string itself.
10433 1.1 mbalmer
10434 1.1 mbalmer
10435 1.1 mbalmer <p>
10436 1.2 lneto When called with option <code>-E</code>,
10437 1.2 lneto besides ignoring <code>LUA_INIT</code>,
10438 1.2 lneto Lua also ignores
10439 1.2 lneto the values of <code>LUA_PATH</code> and <code>LUA_CPATH</code>,
10440 1.2 lneto setting the values of
10441 1.2 lneto <a href="#pdf-package.path"><code>package.path</code></a> and <a href="#pdf-package.cpath"><code>package.cpath</code></a>
10442 1.2 lneto with the default paths defined in <code>luaconf.h</code>.
10443 1.2 lneto
10444 1.2 lneto
10445 1.2 lneto <p>
10446 1.2 lneto All options are handled in order, except <code>-i</code> and <code>-E</code>.
10447 1.1 mbalmer For instance, an invocation like
10448 1.1 mbalmer
10449 1.1 mbalmer <pre>
10450 1.1 mbalmer $ lua -e'a=1' -e 'print(a)' script.lua
10451 1.1 mbalmer </pre><p>
10452 1.2 lneto will first set <code>a</code> to 1, then print the value of <code>a</code>,
10453 1.1 mbalmer and finally run the file <code>script.lua</code> with no arguments.
10454 1.1 mbalmer (Here <code>$</code> is the shell prompt. Your prompt may be different.)
10455 1.1 mbalmer
10456 1.1 mbalmer
10457 1.1 mbalmer <p>
10458 1.2 lneto Before running any code,
10459 1.2 lneto <code>lua</code> collects all command-line arguments
10460 1.1 mbalmer in a global table called <code>arg</code>.
10461 1.2 lneto The script name goes to index 0,
10462 1.1 mbalmer the first argument after the script name goes to index 1,
10463 1.1 mbalmer and so on.
10464 1.1 mbalmer Any arguments before the script name
10465 1.2 lneto (that is, the interpreter name plus its options)
10466 1.1 mbalmer go to negative indices.
10467 1.1 mbalmer For instance, in the call
10468 1.1 mbalmer
10469 1.1 mbalmer <pre>
10470 1.1 mbalmer $ lua -la b.lua t1 t2
10471 1.1 mbalmer </pre><p>
10472 1.2 lneto the table is like this:
10473 1.1 mbalmer
10474 1.1 mbalmer <pre>
10475 1.1 mbalmer arg = { [-2] = "lua", [-1] = "-la",
10476 1.1 mbalmer [0] = "b.lua",
10477 1.1 mbalmer [1] = "t1", [2] = "t2" }
10478 1.1 mbalmer </pre><p>
10479 1.2 lneto If there is no script in the call,
10480 1.2 lneto the interpreter name goes to index 0,
10481 1.2 lneto followed by the other arguments.
10482 1.2 lneto For instance, the call
10483 1.2 lneto
10484 1.2 lneto <pre>
10485 1.2 lneto $ lua -e "print(arg[1])"
10486 1.2 lneto </pre><p>
10487 1.2 lneto will print "<code>-e</code>".
10488 1.3 lneto If there is a script,
10489 1.3 lneto the script is called with parameters
10490 1.3 lneto <code>arg[1]</code>, ···, <code>arg[#arg]</code>.
10491 1.3 lneto (Like all chunks in Lua,
10492 1.3 lneto the script is compiled as a vararg function.)
10493 1.1 mbalmer
10494 1.1 mbalmer
10495 1.1 mbalmer <p>
10496 1.1 mbalmer In interactive mode,
10497 1.2 lneto Lua repeatedly prompts and waits for a line.
10498 1.2 lneto After reading a line,
10499 1.2 lneto Lua first try to interpret the line as an expression.
10500 1.2 lneto If it succeeds, it prints its value.
10501 1.2 lneto Otherwise, it interprets the line as a statement.
10502 1.2 lneto If you write an incomplete statement,
10503 1.1 mbalmer the interpreter waits for its completion
10504 1.1 mbalmer by issuing a different prompt.
10505 1.1 mbalmer
10506 1.1 mbalmer
10507 1.1 mbalmer <p>
10508 1.2 lneto In case of unprotected errors in the script,
10509 1.2 lneto the interpreter reports the error to the standard error stream.
10510 1.3 lneto If the error object is not a string but
10511 1.3 lneto has a metamethod <code>__tostring</code>,
10512 1.2 lneto the interpreter calls this metamethod to produce the final message.
10513 1.3 lneto Otherwise, the interpreter converts the error object to a string
10514 1.3 lneto and adds a stack traceback to it.
10515 1.2 lneto
10516 1.2 lneto
10517 1.2 lneto <p>
10518 1.2 lneto When finishing normally,
10519 1.2 lneto the interpreter closes its main Lua state
10520 1.2 lneto (see <a href="#lua_close"><code>lua_close</code></a>).
10521 1.2 lneto The script can avoid this step by
10522 1.2 lneto calling <a href="#pdf-os.exit"><code>os.exit</code></a> to terminate.
10523 1.1 mbalmer
10524 1.1 mbalmer
10525 1.1 mbalmer <p>
10526 1.1 mbalmer To allow the use of Lua as a
10527 1.1 mbalmer script interpreter in Unix systems,
10528 1.2 lneto the standalone interpreter skips
10529 1.1 mbalmer the first line of a chunk if it starts with <code>#</code>.
10530 1.1 mbalmer Therefore, Lua scripts can be made into executable programs
10531 1.1 mbalmer by using <code>chmod +x</code> and the <code>#!</code> form,
10532 1.1 mbalmer as in
10533 1.1 mbalmer
10534 1.1 mbalmer <pre>
10535 1.1 mbalmer #!/usr/local/bin/lua
10536 1.1 mbalmer </pre><p>
10537 1.1 mbalmer (Of course,
10538 1.1 mbalmer the location of the Lua interpreter may be different in your machine.
10539 1.1 mbalmer If <code>lua</code> is in your <code>PATH</code>,
10540 1.2 lneto then
10541 1.1 mbalmer
10542 1.1 mbalmer <pre>
10543 1.1 mbalmer #!/usr/bin/env lua
10544 1.1 mbalmer </pre><p>
10545 1.2 lneto is a more portable solution.)
10546 1.1 mbalmer
10547 1.1 mbalmer
10548 1.1 mbalmer
10549 1.2 lneto <h1>8 – <a name="8">Incompatibilities with the Previous Version</a></h1>
10550 1.1 mbalmer
10551 1.1 mbalmer <p>
10552 1.1 mbalmer Here we list the incompatibilities that you may find when moving a program
10553 1.2 lneto from Lua 5.2 to Lua 5.3.
10554 1.2 lneto You can avoid some incompatibilities by compiling Lua with
10555 1.1 mbalmer appropriate options (see file <code>luaconf.h</code>).
10556 1.1 mbalmer However,
10557 1.2 lneto all these compatibility options will be removed in the future.
10558 1.1 mbalmer
10559 1.1 mbalmer
10560 1.2 lneto <p>
10561 1.2 lneto Lua versions can always change the C API in ways that
10562 1.2 lneto do not imply source-code changes in a program,
10563 1.2 lneto such as the numeric values for constants
10564 1.2 lneto or the implementation of functions as macros.
10565 1.2 lneto Therefore,
10566 1.3 lneto you should not assume that binaries are compatible between
10567 1.2 lneto different Lua versions.
10568 1.2 lneto Always recompile clients of the Lua API when
10569 1.2 lneto using a new version.
10570 1.1 mbalmer
10571 1.1 mbalmer
10572 1.2 lneto <p>
10573 1.2 lneto Similarly, Lua versions can always change the internal representation
10574 1.2 lneto of precompiled chunks;
10575 1.3 lneto precompiled chunks are not compatible between different Lua versions.
10576 1.1 mbalmer
10577 1.1 mbalmer
10578 1.2 lneto <p>
10579 1.2 lneto The standard paths in the official distribution may
10580 1.2 lneto change between versions.
10581 1.1 mbalmer
10582 1.1 mbalmer
10583 1.1 mbalmer
10584 1.2 lneto <h2>8.1 – <a name="8.1">Changes in the Language</a></h2>
10585 1.1 mbalmer <ul>
10586 1.1 mbalmer
10587 1.1 mbalmer <li>
10588 1.2 lneto The main difference between Lua 5.2 and Lua 5.3 is the
10589 1.2 lneto introduction of an integer subtype for numbers.
10590 1.2 lneto Although this change should not affect "normal" computations,
10591 1.2 lneto some computations
10592 1.2 lneto (mainly those that involve some kind of overflow)
10593 1.2 lneto can give different results.
10594 1.2 lneto
10595 1.1 mbalmer
10596 1.2 lneto <p>
10597 1.2 lneto You can fix these differences by forcing a number to be a float
10598 1.2 lneto (in Lua 5.2 all numbers were float),
10599 1.2 lneto in particular writing constants with an ending <code>.0</code>
10600 1.2 lneto or using <code>x = x + 0.0</code> to convert a variable.
10601 1.2 lneto (This recommendation is only for a quick fix
10602 1.2 lneto for an occasional incompatibility;
10603 1.2 lneto it is not a general guideline for good programming.
10604 1.2 lneto For good programming,
10605 1.2 lneto use floats where you need floats
10606 1.2 lneto and integers where you need integers.)
10607 1.1 mbalmer </li>
10608 1.1 mbalmer
10609 1.1 mbalmer <li>
10610 1.2 lneto The conversion of a float to a string now adds a <code>.0</code> suffix
10611 1.2 lneto to the result if it looks like an integer.
10612 1.2 lneto (For instance, the float 2.0 will be printed as <code>2.0</code>,
10613 1.2 lneto not as <code>2</code>.)
10614 1.2 lneto You should always use an explicit format
10615 1.2 lneto when you need a specific format for numbers.
10616 1.1 mbalmer
10617 1.1 mbalmer
10618 1.2 lneto <p>
10619 1.2 lneto (Formally this is not an incompatibility,
10620 1.2 lneto because Lua does not specify how numbers are formatted as strings,
10621 1.2 lneto but some programs assumed a specific format.)
10622 1.1 mbalmer </li>
10623 1.1 mbalmer
10624 1.1 mbalmer <li>
10625 1.2 lneto The generational mode for the garbage collector was removed.
10626 1.2 lneto (It was an experimental feature in Lua 5.2.)
10627 1.1 mbalmer </li>
10628 1.1 mbalmer
10629 1.1 mbalmer </ul>
10630 1.1 mbalmer
10631 1.1 mbalmer
10632 1.1 mbalmer
10633 1.1 mbalmer
10634 1.2 lneto <h2>8.2 – <a name="8.2">Changes in the Libraries</a></h2>
10635 1.1 mbalmer <ul>
10636 1.1 mbalmer
10637 1.1 mbalmer <li>
10638 1.2 lneto The <code>bit32</code> library has been deprecated.
10639 1.2 lneto It is easy to require a compatible external library or,
10640 1.2 lneto better yet, to replace its functions with appropriate bitwise operations.
10641 1.2 lneto (Keep in mind that <code>bit32</code> operates on 32-bit integers,
10642 1.4 mbalmer while the bitwise operators in Lua 5.3 operate on Lua integers,
10643 1.4 mbalmer which by default have 64 bits.)
10644 1.3 lneto </li>
10645 1.3 lneto
10646 1.3 lneto <li>
10647 1.3 lneto The Table library now respects metamethods
10648 1.3 lneto for setting and getting elements.
10649 1.3 lneto </li>
10650 1.3 lneto
10651 1.3 lneto <li>
10652 1.3 lneto The <a href="#pdf-ipairs"><code>ipairs</code></a> iterator now respects metamethods and
10653 1.3 lneto its <code>__ipairs</code> metamethod has been deprecated.
10654 1.1 mbalmer </li>
10655 1.1 mbalmer
10656 1.1 mbalmer <li>
10657 1.2 lneto Option names in <a href="#pdf-io.read"><code>io.read</code></a> do not have a starting '<code>*</code>' anymore.
10658 1.4 mbalmer For compatibility, Lua will continue to accept (and ignore) this character.
10659 1.1 mbalmer </li>
10660 1.1 mbalmer
10661 1.1 mbalmer <li>
10662 1.2 lneto The following functions were deprecated in the mathematical library:
10663 1.2 lneto <code>atan2</code>, <code>cosh</code>, <code>sinh</code>, <code>tanh</code>, <code>pow</code>,
10664 1.2 lneto <code>frexp</code>, and <code>ldexp</code>.
10665 1.2 lneto You can replace <code>math.pow(x,y)</code> with <code>x^y</code>;
10666 1.2 lneto you can replace <code>math.atan2</code> with <code>math.atan</code>,
10667 1.3 lneto which now accepts one or two parameters;
10668 1.2 lneto you can replace <code>math.ldexp(x,exp)</code> with <code>x * 2.0^exp</code>.
10669 1.2 lneto For the other operations,
10670 1.3 lneto you can either use an external library or
10671 1.3 lneto implement them in Lua.
10672 1.3 lneto </li>
10673 1.3 lneto
10674 1.3 lneto <li>
10675 1.3 lneto The searcher for C loaders used by <a href="#pdf-require"><code>require</code></a>
10676 1.3 lneto changed the way it handles versioned names.
10677 1.3 lneto Now, the version should come after the module name
10678 1.3 lneto (as is usual in most other tools).
10679 1.3 lneto For compatibility, that searcher still tries the old format
10680 1.3 lneto if it cannot find an open function according to the new style.
10681 1.3 lneto (Lua 5.2 already worked that way,
10682 1.3 lneto but it did not document the change.)
10683 1.1 mbalmer </li>
10684 1.1 mbalmer
10685 1.4 mbalmer <li>
10686 1.4 mbalmer The call <code>collectgarbage("count")</code> now returns only one result.
10687 1.4 mbalmer (You can compute that second result from the fractional part
10688 1.4 mbalmer of the first result.)
10689 1.4 mbalmer </li>
10690 1.4 mbalmer
10691 1.2 lneto </ul>
10692 1.2 lneto
10693 1.2 lneto
10694 1.2 lneto
10695 1.2 lneto
10696 1.2 lneto <h2>8.3 – <a name="8.3">Changes in the API</a></h2>
10697 1.2 lneto
10698 1.2 lneto
10699 1.2 lneto <ul>
10700 1.2 lneto
10701 1.1 mbalmer <li>
10702 1.2 lneto Continuation functions now receive as parameters what they needed
10703 1.2 lneto to get through <code>lua_getctx</code>,
10704 1.2 lneto so <code>lua_getctx</code> has been removed.
10705 1.2 lneto Adapt your code accordingly.
10706 1.1 mbalmer </li>
10707 1.1 mbalmer
10708 1.1 mbalmer <li>
10709 1.2 lneto Function <a href="#lua_dump"><code>lua_dump</code></a> has an extra parameter, <code>strip</code>.
10710 1.2 lneto Use 0 as the value of this parameter to get the old behavior.
10711 1.1 mbalmer </li>
10712 1.1 mbalmer
10713 1.3 lneto <li>
10714 1.3 lneto Functions to inject/project unsigned integers
10715 1.3 lneto (<code>lua_pushunsigned</code>, <code>lua_tounsigned</code>, <code>lua_tounsignedx</code>,
10716 1.3 lneto <code>luaL_checkunsigned</code>, <code>luaL_optunsigned</code>)
10717 1.3 lneto were deprecated.
10718 1.3 lneto Use their signed equivalents with a type cast.
10719 1.3 lneto </li>
10720 1.3 lneto
10721 1.3 lneto <li>
10722 1.3 lneto Macros to project non-default integer types
10723 1.3 lneto (<code>luaL_checkint</code>, <code>luaL_optint</code>, <code>luaL_checklong</code>, <code>luaL_optlong</code>)
10724 1.3 lneto were deprecated.
10725 1.3 lneto Use their equivalent over <a href="#lua_Integer"><code>lua_Integer</code></a> with a type cast
10726 1.3 lneto (or, when possible, use <a href="#lua_Integer"><code>lua_Integer</code></a> in your code).
10727 1.3 lneto </li>
10728 1.3 lneto
10729 1.1 mbalmer </ul>
10730 1.1 mbalmer
10731 1.1 mbalmer
10732 1.1 mbalmer
10733 1.1 mbalmer
10734 1.2 lneto <h1>9 – <a name="9">The Complete Syntax of Lua</a></h1>
10735 1.1 mbalmer
10736 1.1 mbalmer <p>
10737 1.1 mbalmer Here is the complete syntax of Lua in extended BNF.
10738 1.3 lneto As usual in extended BNF,
10739 1.3 lneto {A} means 0 or more As,
10740 1.3 lneto and [A] means an optional A.
10741 1.3 lneto (For operator precedences, see <a href="#3.4.8">§3.4.8</a>;
10742 1.3 lneto for a description of the terminals
10743 1.3 lneto Name, Numeral,
10744 1.3 lneto and LiteralString, see <a href="#3.1">§3.1</a>.)
10745 1.1 mbalmer
10746 1.1 mbalmer
10747 1.1 mbalmer
10748 1.1 mbalmer
10749 1.1 mbalmer <pre>
10750 1.1 mbalmer
10751 1.2 lneto chunk ::= block
10752 1.1 mbalmer
10753 1.2 lneto block ::= {stat} [retstat]
10754 1.1 mbalmer
10755 1.2 lneto stat ::= ‘<b>;</b>’ |
10756 1.2 lneto varlist ‘<b>=</b>’ explist |
10757 1.1 mbalmer functioncall |
10758 1.2 lneto label |
10759 1.2 lneto <b>break</b> |
10760 1.2 lneto <b>goto</b> Name |
10761 1.1 mbalmer <b>do</b> block <b>end</b> |
10762 1.1 mbalmer <b>while</b> exp <b>do</b> block <b>end</b> |
10763 1.1 mbalmer <b>repeat</b> block <b>until</b> exp |
10764 1.1 mbalmer <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> |
10765 1.2 lneto <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> |
10766 1.1 mbalmer <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> |
10767 1.1 mbalmer <b>function</b> funcname funcbody |
10768 1.1 mbalmer <b>local</b> <b>function</b> Name funcbody |
10769 1.2 lneto <b>local</b> namelist [‘<b>=</b>’ explist]
10770 1.1 mbalmer
10771 1.2 lneto retstat ::= <b>return</b> [explist] [‘<b>;</b>’]
10772 1.1 mbalmer
10773 1.2 lneto label ::= ‘<b>::</b>’ Name ‘<b>::</b>’
10774 1.1 mbalmer
10775 1.2 lneto funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name]
10776 1.1 mbalmer
10777 1.2 lneto varlist ::= var {‘<b>,</b>’ var}
10778 1.1 mbalmer
10779 1.2 lneto var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name
10780 1.1 mbalmer
10781 1.2 lneto namelist ::= Name {‘<b>,</b>’ Name}
10782 1.1 mbalmer
10783 1.2 lneto explist ::= exp {‘<b>,</b>’ exp}
10784 1.2 lneto
10785 1.3 lneto exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Numeral | LiteralString | ‘<b>...</b>’ | functiondef |
10786 1.1 mbalmer prefixexp | tableconstructor | exp binop exp | unop exp
10787 1.1 mbalmer
10788 1.2 lneto prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’
10789 1.1 mbalmer
10790 1.2 lneto functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args
10791 1.1 mbalmer
10792 1.3 lneto args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | LiteralString
10793 1.1 mbalmer
10794 1.2 lneto functiondef ::= <b>function</b> funcbody
10795 1.1 mbalmer
10796 1.2 lneto funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b>
10797 1.1 mbalmer
10798 1.2 lneto parlist ::= namelist [‘<b>,</b>’ ‘<b>...</b>’] | ‘<b>...</b>’
10799 1.1 mbalmer
10800 1.2 lneto tableconstructor ::= ‘<b>{</b>’ [fieldlist] ‘<b>}</b>’
10801 1.1 mbalmer
10802 1.1 mbalmer fieldlist ::= field {fieldsep field} [fieldsep]
10803 1.1 mbalmer
10804 1.2 lneto field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp
10805 1.1 mbalmer
10806 1.2 lneto fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’
10807 1.1 mbalmer
10808 1.2 lneto binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>//</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ |
10809 1.2 lneto ‘<b>&</b>’ | ‘<b>~</b>’ | ‘<b>|</b>’ | ‘<b>>></b>’ | ‘<b><<</b>’ | ‘<b>..</b>’ |
10810 1.2 lneto ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ |
10811 1.1 mbalmer <b>and</b> | <b>or</b>
10812 1.1 mbalmer
10813 1.2 lneto unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | ‘<b>~</b>’
10814 1.1 mbalmer
10815 1.1 mbalmer </pre>
10816 1.1 mbalmer
10817 1.1 mbalmer <p>
10818 1.1 mbalmer
10819 1.1 mbalmer
10820 1.1 mbalmer
10821 1.1 mbalmer
10822 1.1 mbalmer
10823 1.1 mbalmer
10824 1.1 mbalmer
10825 1.2 lneto
10826 1.4 mbalmer <P CLASS="footer">
10827 1.1 mbalmer Last update:
10828 1.5 lneto Wed Nov 25 15:19:10 BRST 2015
10829 1.4 mbalmer </P>
10830 1.1 mbalmer <!--
10831 1.5 lneto Last change: revised for Lua 5.3.2
10832 1.1 mbalmer -->
10833 1.1 mbalmer
10834 1.1 mbalmer </body></html>
10835 1.1 mbalmer
10836