| Acknowledgments |
|
ix | |
| Introduction |
|
xxiii | |
|
|
|
1 | (22) |
|
Choosing How to Install Lua |
|
|
1 | (2) |
|
|
|
2 | (1) |
|
|
|
3 | (1) |
|
Finding Your System's Shell |
|
|
3 | (3) |
|
|
|
3 | (1) |
|
Shells on Unix and Unix-Like systems |
|
|
3 | (1) |
|
|
|
4 | (1) |
|
|
|
4 | (1) |
|
Environment Variables on Unix-Like Systems |
|
|
4 | (1) |
|
Environment Variables on Windows |
|
|
5 | (1) |
|
Dealing with Tarballs and Zip Files |
|
|
6 | (1) |
|
|
|
7 | (11) |
|
|
|
7 | (1) |
|
Compiling Lua on Linux and Other Unix-Like Systems |
|
|
8 | (4) |
|
|
|
12 | (1) |
|
Building Lua with Microsoft Visual C++ |
|
|
13 | (1) |
|
Building Lua with the Tiny C Compiler |
|
|
14 | (2) |
|
|
|
16 | (2) |
|
|
|
18 | (3) |
|
Selecting a Prebuilt Binary Package |
|
|
18 | (1) |
|
Installing a Prebuilt Binary Package on a Unix-Type System |
|
|
19 | (1) |
|
Installing a Prebuilt Binary Package on Windows |
|
|
20 | (1) |
|
|
|
21 | (1) |
|
|
|
21 | (1) |
|
|
|
22 | (1) |
|
|
|
22 | (1) |
|
|
|
23 | (46) |
|
Numbers and Arithmetic Operations: Basic Interpreter Usage |
|
|
23 | (3) |
|
Addition, Subtraction, Multiplication, Division, and Exponentiation |
|
|
24 | (1) |
|
Interacting with the Interpreter |
|
|
24 | (1) |
|
Other Notations for Numbers |
|
|
25 | (1) |
|
|
|
26 | (1) |
|
|
|
26 | (1) |
|
|
|
26 | (1) |
|
|
|
27 | (1) |
|
Division by Zero and Overflow |
|
|
27 | (1) |
|
|
|
28 | (1) |
|
|
|
28 | (4) |
|
|
|
29 | (2) |
|
|
|
31 | (1) |
|
Variables on the Right Side of Assignments |
|
|
32 | (1) |
|
|
|
32 | (5) |
|
|
|
32 | (1) |
|
Quoting Strings with Double Quotes |
|
|
32 | (1) |
|
Quoting Strings with Single Quotes |
|
|
33 | (1) |
|
Quoting Strings with Square Brackets |
|
|
33 | (2) |
|
|
|
35 | (2) |
|
Relational Operators and Boolean Values |
|
|
37 | (3) |
|
|
|
37 | (1) |
|
|
|
38 | (2) |
|
|
|
40 | (1) |
|
|
|
41 | (4) |
|
|
|
42 | (1) |
|
|
|
43 | (1) |
|
|
|
44 | (1) |
|
The Concatenation, Length, and Modulo Operators |
|
|
45 | (3) |
|
The String Concatenation Operator |
|
|
45 | (1) |
|
|
|
46 | (1) |
|
|
|
47 | (1) |
|
Automatic Conversion of Operands |
|
|
48 | (1) |
|
Precedence and Associativity |
|
|
49 | (2) |
|
|
|
51 | (1) |
|
|
|
52 | (1) |
|
Expressions and Statements |
|
|
53 | (1) |
|
|
|
54 | (12) |
|
|
|
55 | (3) |
|
|
|
58 | (2) |
|
|
|
60 | (2) |
|
|
|
62 | (1) |
|
The break and do Statements |
|
|
63 | (3) |
|
|
|
66 | (1) |
|
|
|
66 | (3) |
|
Extending Lua with Functions |
|
|
69 | (48) |
|
|
|
72 | (9) |
|
Using a Function that Returns a Value |
|
|
72 | (1) |
|
Defining a Function that Returns a Value |
|
|
73 | (1) |
|
Using return to Alter Control Flow |
|
|
74 | (2) |
|
|
|
76 | (1) |
|
Returning Multiple Values |
|
|
77 | (1) |
|
|
|
78 | (1) |
|
Using Multiple-Valued Functions in Value Lists |
|
|
78 | (1) |
|
Using Valueless Functions in Value Lists |
|
|
79 | (2) |
|
|
|
81 | (3) |
|
|
|
84 | (7) |
|
Actual and Formal Arguments |
|
|
84 | (1) |
|
|
|
85 | (6) |
|
Understanding Side Effects |
|
|
91 | (4) |
|
|
|
91 | (2) |
|
|
|
93 | (2) |
|
Functions Calling Functions |
|
|
95 | (7) |
|
|
|
95 | (2) |
|
|
|
97 | (1) |
|
|
|
98 | (1) |
|
|
|
99 | (3) |
|
|
|
102 | (4) |
|
Replacing Built-in Functions |
|
|
102 | (1) |
|
Comparing and Printing Functions |
|
|
103 | (1) |
|
Function Definitions as Assignments |
|
|
103 | (2) |
|
|
|
105 | (1) |
|
Whitespace, Semicolons, and Function Calls |
|
|
106 | (2) |
|
|
|
108 | (5) |
|
Defining Functions that Create Functions |
|
|
108 | (2) |
|
Defining Functions with Private State |
|
|
110 | (1) |
|
Figuring Out Tricky Scope Situations |
|
|
111 | (2) |
|
|
|
113 | (1) |
|
|
|
114 | (3) |
|
|
|
117 | (54) |
|
|
|
117 | (2) |
|
A Shorter Way to Write Some Keys |
|
|
119 | (1) |
|
Altering a Table's Contents |
|
|
120 | (1) |
|
|
|
121 | (2) |
|
|
|
123 | (1) |
|
|
|
124 | (4) |
|
|
|
128 | (8) |
|
|
|
128 | (1) |
|
|
|
128 | (3) |
|
|
|
131 | (1) |
|
|
|
132 | (1) |
|
|
|
132 | (1) |
|
Object-Oriented Programming with Tables |
|
|
133 | (3) |
|
Functions with Variable Numbers of Arguments |
|
|
136 | (7) |
|
Defining Vararg Functions |
|
|
136 | (4) |
|
Script as Vararg Functions |
|
|
140 | (3) |
|
|
|
143 | (1) |
|
|
|
144 | (8) |
|
|
|
144 | (1) |
|
Avoiding Bugs by Understanding Mutability |
|
|
145 | (1) |
|
Variables and Mutable Values |
|
|
145 | (2) |
|
|
|
147 | (1) |
|
|
|
148 | (4) |
|
Building Other Data Structures from Tables |
|
|
152 | (6) |
|
|
|
158 | (5) |
|
Global Variable Environments |
|
|
163 | (5) |
|
|
|
168 | (1) |
|
|
|
169 | (2) |
|
|
|
171 | (42) |
|
Basic String Conversion Functions |
|
|
171 | (2) |
|
|
|
173 | (1) |
|
Converting Between Characters and Character Codes |
|
|
173 | (1) |
|
Formatting Strings and Numbers with string.format |
|
|
174 | (6) |
|
|
|
180 | (5) |
|
Writing to and Reading from a File |
|
|
181 | (4) |
|
|
|
185 | (25) |
|
Searching for a Specific String |
|
|
186 | (1) |
|
Matching Any of Several Characters |
|
|
186 | (7) |
|
Matches of Varying Lengths |
|
|
193 | (5) |
|
|
|
198 | (4) |
|
Matching Balanced Delimiters |
|
|
202 | (1) |
|
More on string.find, string.match, and string.gsub |
|
|
202 | (2) |
|
Iterating Through All Matches |
|
|
204 | (3) |
|
|
|
207 | (2) |
|
|
|
209 | (1) |
|
|
|
210 | (1) |
|
|
|
210 | (3) |
|
Handling and Avoiding Errors |
|
|
213 | (20) |
|
|
|
213 | (5) |
|
|
|
213 | (4) |
|
|
|
217 | (1) |
|
|
|
218 | (12) |
|
|
|
218 | (1) |
|
|
|
219 | (1) |
|
|
|
220 | (1) |
|
|
|
220 | (1) |
|
The assert and error Functions |
|
|
220 | (1) |
|
Defining Your Own Error Condition |
|
|
221 | (1) |
|
Anticipating Error Conditions |
|
|
222 | (1) |
|
Working with Return Values |
|
|
222 | (2) |
|
|
|
224 | (3) |
|
Error-Containment Functions |
|
|
227 | (1) |
|
|
|
227 | (2) |
|
|
|
229 | (1) |
|
|
|
230 | (1) |
|
|
|
230 | (1) |
|
|
|
230 | (1) |
|
|
|
231 | (2) |
|
|
|
233 | (16) |
|
Interfaces and Implementations |
|
|
233 | (1) |
|
|
|
234 | (1) |
|
|
|
235 | (1) |
|
Creating a Module Directory |
|
|
235 | (1) |
|
Setting Lua's Environment Variable |
|
|
236 | (1) |
|
Preserving a Module's Interface |
|
|
236 | (4) |
|
|
|
240 | (1) |
|
|
|
241 | (1) |
|
|
|
242 | (3) |
|
Creating and Reusing Namespaces |
|
|
242 | (2) |
|
Avoiding Global Variables |
|
|
244 | (1) |
|
|
|
244 | (1) |
|
Reporting All Global Assignments |
|
|
244 | (1) |
|
|
|
245 | (2) |
|
|
|
247 | (1) |
|
|
|
247 | (1) |
|
|
|
247 | (2) |
|
Extending Lua's Behavior with Metamethods |
|
|
249 | (22) |
|
Using Concatenation and Arithmetical Operators on Tables |
|
|
249 | (8) |
|
|
|
257 | (1) |
|
Indexing and Call Metamethods |
|
|
258 | (7) |
|
Non-Tables with Metamethods |
|
|
265 | (2) |
|
Non-Syntactical Metamethods |
|
|
267 | (1) |
|
|
|
268 | (1) |
|
|
|
268 | (1) |
|
|
|
269 | (2) |
|
Handling Events Naturally with Coroutines |
|
|
271 | (28) |
|
Coroutines and Program Control |
|
|
271 | (10) |
|
Coroutines Are Not Functions |
|
|
272 | (1) |
|
How Coroutines Are Like Programs |
|
|
272 | (1) |
|
Coroutines Transfer Control |
|
|
273 | (1) |
|
|
|
273 | (1) |
|
Coroutines Are Cooperative |
|
|
273 | (2) |
|
|
|
275 | (3) |
|
|
|
278 | (1) |
|
|
|
279 | (1) |
|
Work Shoulder-to-Shoulder |
|
|
279 | (1) |
|
|
|
280 | (1) |
|
Expect the Best, Prepare for the Worst |
|
|
280 | (1) |
|
Play on Your Side of the Fence |
|
|
280 | (1) |
|
|
|
281 | (1) |
|
Managing Concurrent Tasks |
|
|
281 | (1) |
|
|
|
282 | (5) |
|
Exercising a Coroutine's Memory |
|
|
282 | (4) |
|
Iterating with Coroutines |
|
|
286 | (1) |
|
|
|
287 | (10) |
|
|
|
288 | (8) |
|
Yielding to Another Coroutine |
|
|
296 | (1) |
|
|
|
297 | (1) |
|
|
|
297 | (2) |
|
|
|
299 | (26) |
|
|
|
299 | (4) |
|
|
|
303 | (4) |
|
The Implementation of Tables and Strings |
|
|
307 | (1) |
|
|
|
308 | (13) |
|
Inspecting and Manipulating Running Code |
|
|
308 | (7) |
|
|
|
315 | (6) |
|
Other Functions in the Debug Library |
|
|
321 | (1) |
|
|
|
321 | (1) |
|
|
|
322 | (3) |
|
Exploring Lua's Libraries |
|
|
325 | (50) |
|
|
|
325 | (11) |
|
|
|
326 | (1) |
|
|
|
326 | (2) |
|
|
|
328 | (2) |
|
Error-Containment Functions |
|
|
330 | (1) |
|
|
|
331 | (1) |
|
The Garbage-Collection Function |
|
|
332 | (1) |
|
Type and Conversion Functions |
|
|
333 | (1) |
|
|
|
333 | (1) |
|
Error-Condition Functions |
|
|
333 | (1) |
|
Table Traversal Functions |
|
|
334 | (1) |
|
|
|
335 | (1) |
|
|
|
336 | (2) |
|
|
|
338 | (2) |
|
|
|
340 | (4) |
|
Pattern-Based String Functions |
|
|
340 | (2) |
|
String-Conversion Functions |
|
|
342 | (2) |
|
|
|
344 | (1) |
|
|
|
345 | (19) |
|
|
|
345 | (3) |
|
Inverse Trigonometric Functions |
|
|
348 | (3) |
|
|
|
351 | (3) |
|
|
|
354 | (2) |
|
|
|
356 | (2) |
|
|
|
358 | (2) |
|
Floating Point Representation |
|
|
360 | (1) |
|
Angle Conversion Functions |
|
|
361 | (1) |
|
Pseudo-Random Number Functions |
|
|
362 | (1) |
|
|
|
362 | (1) |
|
Minimum and Maximum Functions |
|
|
363 | (1) |
|
|
|
363 | (1) |
|
|
|
364 | (4) |
|
|
|
368 | (2) |
|
|
|
368 | (1) |
|
|
|
368 | (1) |
|
|
|
369 | (1) |
|
Other Operating System Functions |
|
|
370 | (1) |
|
|
|
370 | (3) |
|
|
|
373 | (2) |
|
Using Community Libraries |
|
|
375 | (38) |
|
|
|
375 | (4) |
|
Dynamically Linked Libraries |
|
|
376 | (1) |
|
Resolving External References |
|
|
376 | (1) |
|
|
|
376 | (1) |
|
Libraries Built from Source Code |
|
|
377 | (1) |
|
Building Libraries on Unix-Like Systems |
|
|
378 | (1) |
|
Building Libraries on Windows |
|
|
378 | (1) |
|
|
|
379 | (1) |
|
How Lua Interacts with Libraries |
|
|
379 | (4) |
|
The Variable Registration Process |
|
|
379 | (1) |
|
Calling a C Function from Lua |
|
|
380 | (3) |
|
The pack Binary Structuring Library |
|
|
383 | (6) |
|
Building the pack Library on Unix-type Systems |
|
|
383 | (1) |
|
Building and Installing the pack Library on Windows |
|
|
384 | (1) |
|
|
|
384 | (1) |
|
Installing the pack Library |
|
|
385 | (1) |
|
|
|
385 | (4) |
|
The cURL File Transfer Library |
|
|
389 | (6) |
|
|
|
389 | (1) |
|
Building libcurl on Unix-Like Systems |
|
|
390 | (1) |
|
Building libcurl on Windows |
|
|
391 | (1) |
|
|
|
392 | (1) |
|
Building luacurl on Unix-Like Systems |
|
|
392 | (1) |
|
Building luacurl on Windows |
|
|
393 | (1) |
|
|
|
393 | (2) |
|
|
|
395 | (10) |
|
|
|
395 | (1) |
|
Building gd on Unix-Like Systems |
|
|
396 | (1) |
|
|
|
396 | (1) |
|
|
|
397 | (1) |
|
Building lua-gd on Unix-Like Systems |
|
|
397 | (1) |
|
Building lua-gd on Windows |
|
|
398 | (1) |
|
|
|
399 | (6) |
|
The SQLite Database Library |
|
|
405 | (6) |
|
|
|
405 | (1) |
|
Building SQLite3 on Unix-Like Systems |
|
|
405 | (1) |
|
Building SQLite3 on Windows |
|
|
406 | (1) |
|
|
|
407 | (1) |
|
Building Iua-sqlite3 on Unix-Like Systems |
|
|
407 | (1) |
|
Building Iua-sqlite3 on Windows |
|
|
408 | (1) |
|
|
|
409 | (2) |
|
|
|
411 | (1) |
|
|
|
412 | (1) |
|
Interfacing Lua with Other Languages |
|
|
413 | (36) |
|
|
|
413 | (2) |
|
|
|
414 | (1) |
|
|
|
414 | (1) |
|
Embedding or Extending: Which Is Best? |
|
|
414 | (1) |
|
Communicating Between Lua and C |
|
|
415 | (6) |
|
|
|
421 | (2) |
|
|
|
421 | (1) |
|
|
|
421 | (1) |
|
|
|
422 | (1) |
|
|
|
423 | (13) |
|
|
|
436 | (2) |
|
Retrieving Indexed Values |
|
|
436 | (1) |
|
|
|
437 | (1) |
|
|
|
438 | (3) |
|
|
|
438 | (1) |
|
|
|
439 | (1) |
|
|
|
439 | (1) |
|
|
|
440 | (1) |
|
|
|
441 | (1) |
|
Layering Your Extension Library |
|
|
441 | (6) |
|
|
|
447 | (1) |
|
|
|
448 | (1) |
|
Managing Information with Databases |
|
|
449 | (18) |
|
Some Basic Relational Database Concepts |
|
|
449 | (9) |
|
|
|
458 | (8) |
|
|
|
466 | (1) |
|
|
|
466 | (1) |
|
|
|
467 | (36) |
|
|
|
467 | (1) |
|
|
|
468 | (1) |
|
|
|
468 | (1) |
|
|
|
469 | (1) |
|
Creating Content at Run Time with Lua |
|
|
469 | (1) |
|
|
|
469 | (2) |
|
CGI Scripts on Unix-Type Systems |
|
|
470 | (1) |
|
|
|
470 | (1) |
|
|
|
471 | (3) |
|
|
|
471 | (1) |
|
|
|
472 | (2) |
|
Testing Your Web Server with Static Content |
|
|
474 | (1) |
|
Serving Dynamic Web Content |
|
|
474 | (15) |
|
Problems with CGI Scripts |
|
|
475 | (1) |
|
Asynchronous Calls to the Server |
|
|
476 | (2) |
|
Producing a Calendar Dynamically |
|
|
478 | (3) |
|
Producing Charts Dynamically |
|
|
481 | (8) |
|
Interactive CGI Applications |
|
|
489 | (9) |
|
|
|
489 | (9) |
|
|
|
498 | (1) |
|
|
|
498 | (1) |
|
|
|
498 | (3) |
|
|
|
499 | (1) |
|
|
|
500 | (1) |
|
|
|
501 | (1) |
|
|
|
501 | (2) |
|
Connecting to a Larger World |
|
|
503 | (40) |
|
|
|
503 | (3) |
|
|
|
504 | (1) |
|
Compiling on Linux and Other Unix-Like Systems |
|
|
504 | (1) |
|
|
|
504 | (1) |
|
Installing Windows Binaries |
|
|
505 | (1) |
|
|
|
506 | (6) |
|
|
|
506 | (1) |
|
|
|
507 | (1) |
|
|
|
507 | (1) |
|
Identifying Internet Resources |
|
|
508 | (1) |
|
|
|
509 | (1) |
|
Sockets: Streams and Datagrams |
|
|
510 | (1) |
|
|
|
511 | (1) |
|
Using LuaSocket for Network Communication |
|
|
512 | (6) |
|
Handling Multiple Persistent Connections |
|
|
518 | (6) |
|
Using Lua Coroutines with the select Function |
|
|
518 | (4) |
|
Multiple Connections on the Server Side |
|
|
522 | (1) |
|
Setting Timeout Values for the Server Socket |
|
|
523 | (1) |
|
The Application Protocols |
|
|
524 | (12) |
|
Filtering the Flow of Data |
|
|
524 | (3) |
|
|
|
527 | (2) |
|
Sending and Receiving E-mail Messages |
|
|
529 | (7) |
|
Networking with Lua and Streams |
|
|
536 | (5) |
|
On the Server Side: inetd and Friends |
|
|
536 | (2) |
|
On the Client Side: ssh and Friends |
|
|
538 | (3) |
|
|
|
541 | (1) |
|
|
|
542 | (1) |
|
Programming Games with Lua |
|
|
543 | (22) |
|
Understanding Why and When to Use Lua |
|
|
543 | (1) |
|
Simple 2-D Action Game Using SDL |
|
|
544 | (18) |
|
Installing SDL and LuaCheia |
|
|
544 | (2) |
|
|
|
546 | (16) |
|
|
|
562 | (1) |
|
|
|
562 | (3) |
|
|
|
565 | (30) |
|
Getting Started with Plua |
|
|
565 | (2) |
|
|
|
566 | (1) |
|
Examining the Distribution Contents |
|
|
566 | (1) |
|
Exploring Plua's Features |
|
|
567 | (9) |
|
Running the Plua Application |
|
|
567 | (2) |
|
|
|
569 | (1) |
|
Reading the Online Documentation |
|
|
570 | (1) |
|
|
|
571 | (1) |
|
|
|
572 | (1) |
|
|
|
573 | (3) |
|
|
|
576 | (5) |
|
The Command-Line Compiler |
|
|
576 | (1) |
|
|
|
577 | (1) |
|
|
|
577 | (1) |
|
|
|
578 | (1) |
|
|
|
578 | (1) |
|
Running Plua in the Emulator |
|
|
578 | (2) |
|
Compiling a Program in the Emulator |
|
|
580 | (1) |
|
|
|
580 | (1) |
|
|
|
581 | (1) |
|
|
|
581 | (1) |
|
|
|
581 | (1) |
|
|
|
581 | (11) |
|
|
|
582 | (1) |
|
Programming the User Interface |
|
|
583 | (7) |
|
|
|
590 | (2) |
|
|
|
592 | (1) |
|
|
|
593 | (2) |
|
Fitting into the Lua Community |
|
|
595 | (10) |
|
|
|
596 | (1) |
|
|
|
596 | (1) |
|
|
|
597 | (1) |
|
|
|
597 | (4) |
|
Viewing and Searching the Archives |
|
|
597 | (1) |
|
|
|
598 | (1) |
|
Using a Web Browser to Access the List |
|
|
599 | (1) |
|
Using a Newsreader to Access the List |
|
|
599 | (1) |
|
Subscribing to the List Server |
|
|
599 | (1) |
|
|
|
600 | (1) |
|
|
|
601 | (1) |
|
|
|
601 | (1) |
|
|
|
601 | (1) |
|
|
|
602 | (1) |
|
|
|
603 | (1) |
|
|
|
603 | (2) |
| Appendix A: Answers |
|
605 | (24) |
| Index |
|
629 | |