lua programming
TRANSCRIPT
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 1/13
Beginning
Lua Programming
Kurt Jung and Aaron Brown
1 S O 7
© W IL E Y
2 O O 7
Wiley Publishing, Inc.
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 2/13
Conten
Acknowledgments Introduction
Chapter 1 : Ge tting Situa ted
Choosing How to Install Lua
Building Lua Yourself Selecting Prebuilt Lua
Finding Your System's Shell
Windows Shel ls She l ls on Unix and Unix-Like sys tem s
Shel l Features
The Envi ronmen t
Environment Variables on Unix-Like System s
Environment Variables on Windows
Dealing with Tarballs and Zip Files Compiling Lua
The Lua Source Tarball Compiling Lua on Linux and Other Unix-Like Systems
Compiling Lua on Windows Building Lua with Microsoft.Visual C++
Building Lua with the Tiny C Compiler
Building Lua with MinGW
Binary Packages Selecting a Prebuilt Binary Package Installing a Prebuilt Binary Package on a Unix-Type System
Installing a Prebuilt Binary Package on Windows Additional Tools
Programmer's Editor Revision Control System
Summary
Chapter 2 : First Step s •
Numbers and Arithmetic Operations: Basic Interpreter Usage Addition, Subtraction, Multiplication, Division, and Exponentiation Interacting with the Interpreter
Other Notations for Numbers
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 3/13
Contents
Interpreter Know-How
Quitting the Interpreter
Interpreter Shortcu ts
Numerical Gotchas
Division by Zero and Overflow
Floating-Point Rounding
Variables and Assignment
Assignment Basics
Multiple Assignment
Variables on the Right Side of Assignm ents
Strings
Quoting Strings
Quoting Strings with Double Quotes
Quoting Strings with Single Quotes -
Quoting Strings with Square Brackets
Backslash Escaping Relational Operators and Boolean Values
Comparing Numbers
Comparing Strings
The nil Value
Boolean Operators
The and Operator
The or Operator
The not Unary Operator
The Conc atenation, Length, and Modulo Operators
The String Concatenation Operator The Length Operator
The Modulo Operator
Au tom atic Conversion of Operands
Precedence and Associativity
Variables and Values
Comments
Expressions and State m ents
Compound Statem ents
The if Statemen t
The while Loop
The for Loop
The repeat Loop /
The break and do Statements '
Summary
Exercises
XII
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 4/13
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 5/13
Contents
Looping through Tables 124
Tables of Functions 128
The Table Library 128
table.sort 128
table.concat 131
table.remove 132
table.maxn 132
Object-Oriented Programming with Tables 133
Functions with Variable Numbers of Arguments 136
Defining Vararg Functions 136
Scripts as Vararg Functions 140
Keyword Arguments 143
Different but the Same 144
Table Equality 144
Avoiding Bugs by Understanding Mutability 145
Variables and Mutable Values 145
Tables and Functions 147
Copying Tables 148
Building Other Data Structures from Tables 152
Custom-Made Loops 158
Global Variable Environments 163
Summary 168
Exercises 169
Chapter 5: Using Strings 11 1
Basic String Conversion Functions 171
String Length 173
Converting Between Characters and Character Codes 173
Formatting Strings and Numbers with string.format 174
Input/Output 180
Writing to and Reading from a File 181
Pattern-Matching 185
Searching for a Specific String 186
Matching Any of Several Characters 186
Matches of Varying Lengths 193Captures 198
Matching Balanced Delimiters 202
More on string.find, string.match, and string.gsub ' 202
Iterating Through All Matches 204
Tricks for the Tricky 207
Magic Characters Chart 209
Summary 210
Exercises 210
xiv
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 6/13
Conte
Chapter 6: Handling and Avoiding Errors 2
Kinds of Errors
Syntax Errors
Runtime Errors
Handling Errors
Default Error BehaviorCheck ing Assumpt ions
Code Errors
Data Errors
The assert and error Functions
Defining Your Own Error Condition
Ant ic ipat ing Error Condi t ions
Working wi th Return Values
St ructur ing Code
Error-Containment Funct ions
The pcall Function
The xpcall Function
User-Written Scripts
Locating Errors
Summary
Exercises
Chapter 7: Using Modules
Interfaces and Implementations The require Function
Where to Put Modules
Creating a Module Directory
Setting Lua's Environment Variable
Preserving a Module's Interface
Module Bookkeeping
Bytecode
Namespaces
Creat ing and Reusing Namespaces
Avoiding Global Variables •Using the s trict Module
Reporting All Global Assignments
The module Function •
C Modules
Summary
Exercises
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 7/13
Contents
Chapter 8: Extending Lua's Behavior w ith M etam etho ds 2 4 9
Using Concatenation and Arithmetical Operators on Tables 24 9Relational Metamethods 257Indexing and Call Metamethods 2 5 8Non-Tables with Metamethods 265
Non-Syntactical Metamethods 267Metamethod Applicability . 2 6 8Summary 268Exercises 269
Chapter 9 : Handling Events Naturally with Coroutines 2 7 1
Coroutines and Program Control 2 7 1Coroutines Are Not Functions 27 2
How Coroutines Are Like Programs 27 2
Coroutines Transfer Control 27 3Wrapping a Coroutine 27 3
Corout ines Are Cooperat ive 2 73
Outside Looking In 27 5
Corout ines Have Statu s 2 7 8
Rules of Conduc t 2 79
Work Shoulder-to-Shoulder 27 9
Trust the Dispatcher 28 0
Expect the Best, Prepare for the Worst 28 0
Play on Your Side of the Fence 28 0
Avoid the Deep End 2 8 1Managing Concurrent Tasks 2 8 1Retaining State 2 82
Exercising a Coroutine's Memory 28 2
Iterating with Coroutines 28 6
Handling Events Simply 2 8 7
The Event Loop 288
Yielding to Another Coroutine 29 6
Summary 297Exercises 297
Chapter 1 0 : Looking Under the Hood 2 9 9
Bytecode and luac 29 9Garbage Collection 303The Implementation of Tables and Strings 307
XVI
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 8/13
Conte
The Debug Library 3
Inspecting and Manipulating Running Code 3
Hooks 3
Other Functions in the Debug Library
Summary 3
Exercises
Chapter 11: Exploring Lua's Libraries 3
Core Library 3
Environment Functions 3
Metatable Functions 3
Chunk-Loading Functions 3
Error-Containment Functions 3
Module Functions
The Garbage-Collection Function , 3Type and Conversion Functions 3
Basic Output 3
Error-Condition Functions 3
Table Traversal Functions 3
Vararg-Related Functions 3
Coroutine Library 3
Package Library 3
String Library 3
Pattern-Based String Functions 3
String-Conversion Functions 3Table Library 3
Math Library 3
Trigonometric Functions 3
Inverse Trigonometric Functions 3
Hyperbolic Functions
Exponent Functions 3
Logarithm Functions 3
Adjustment Functions 3
Floating Point Representation 3
Angle Conversion FunctionsPseudo-Random Number Functions 3
Modulus Functions 3
Minimum and Maximum Functions ' 3
Constants 3
Input/Output Library 3
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 9/13
Contents
Operating System Library 368
CPU Tim ing 368
Time and Date Functions 368
Filesystem Functions 369
Other Operating System Functions 370
Debugging Library 370
Summary 37
Chapter 12: Using Community Libraries 375
Library Overview 375
Dynamically Linked Libraries 37
Resolving External References 37
Configuration Options 37
Librar ies Bui l t f rom Source Code 37
Building Libraries onUnix-Like Systems 37
Building Libraries on Windows 37
L imi ts to Portabi l i ty 37
How Lua Interacts with Libraries 37
The Variable Registration P rocess 379
Calling a C Function from Lua 38
The pack Binary Structuring Library 38
Building the pack Library on Unix-type S ystems 38
Building and Installing the pack Library on Windows 38
Testing the pack Library 38
Installing the pack Library 38
Using the pack Library 38
The cU RL F ile Transfer Library 38
Building libcurl 38
Building libcurl on Unix-Like Systems 39
Building libcurl on Windows 39
Building luacurl 39
Building luacurl on Unix-Like Systems 39
Building luacurl on Windows 39
Using luacurl 39
The gd Graphics Library 39
Bui ld ing gd 39
Building gd on Unix-Like System s , 39
Installing gd on W indows ' 39
Building lua-gd 39
Building lua-gd on Unix-Like Systems 39
Building lua-gd on Windows 39
Using lua-gd 39
xvi i i
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 10/13
Conten
The SQLite Database Library 4Bui ld ing SQLi te3 4
Building SQLite3 on Unix-Like Systems 4
Building SQLite3 on Windows 4
Bui ld ing Iua-sql i te3 4
Building Iua-sqlite3 on Unix-Like System s 4
Building Iua-sqlite3 on Windows 4Using Iua-sql i te3 4
Summary 4Exercises 4
Chapter 1 3 : Interfacing Lua with Other Languages 4
How C Programs Use Lua 4Embedding Lua 4
Extending Lua 4
Embedding or Extending: Which Is Best? 4Comm unicating Between Lua and C 4Calling Lua from C 4
Obtaining a Lua Function 4
Calling a Lua Function 4
Protected Calls 4
Working with Userdata 4Indexing Values in C 4
Retrieving Indexed Values 4
Setting Indexed Values 4
Retaining Values in C 4The Registry 4
C Function Environments 4
Upvalues in C 4
Referencing Values 4
The Thread Environment 4
Layering Your Extension Library 4Summary 4Exercises 4
Chapter 1 4 : M anag ing Information with Databases 4
Some Basic Relational Database Concepts • 4SQL, LuaSQL, and MySQL 4Summary 4
Exercises 4
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 11/13
Contents
Chapter 15: Programming for the Web 4
A Web Server Primer 4
Dynamic Web Content 4
Embedded Web Server 4
Extended Web Server 4
Creating Content at Run Time with Lua 4Executing CGI Scripts 4
CGI Scripts on Unix-Type Systems 4
CGI Scripts onWindows 4
Installing a Web Server 4
Apache '
TinyWeb 4
Testing Your Web Server with Static Content 4
Serving Dynamic Web Content 4
Problems w ith CGI Scripts 4
Asynchronous Calls to the Server 4
Producing a Calendar Dynamically 4
Producing Charts Dynamically 4
Interactive CGI Applications 4
CGI Helper Routines 4
Developing CGI Scripts 4
Security Issues 4
The Kepler Project 4
CGI the Kepler Way 4
Lua Pages 5Summary 5
Exercises 5
Chapter 16: Connecting to a Larger World 5
Installing LuaSocket 5
Compiling LuaSocket 5
Compiling on Linux and Other Unix-Like Systems 5
Compiling on Windows 5
Installing Windows Binaries 5Network Overview 5
Routed Packets 5
Addresses '• 5
Domain Names 5
Identifying Internet Resources 5
Transport Protocols 5
Sockets: Streams and Datagrams 5
TCP Socket Sociology 5
XX
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 12/13
Conten
Using LuaSocket for Network Communication 5
Handling Multiple Persistent Connections 5
Using Lua Coroutines with the select Function 5
Multiple Connections on the Server Side 5
Setting Timeout Values for the Server Socket 5
The Application Protocols 5
Filtering the Flow of Data 5
Accessing Web Pages 5
Sending and Receiving E-mail Messages 5
Networking with Lua and Streams 5
On the Server Side: inetd and Friends 5
On the Client Side: ssh and Friends 5
Summary 5
Exercises 5
Chapter 17: Programming Games with Lua 5
Understanding Why and When to Use Lua 5
Simple 2-D Action Game Using SDL 5
Installing SDL and LuaCheia 5
Using SDL 5
Summary 5
Exercise 5
Chapter 18: Carrying Lua with You 5
Getting Started with Plua 5
Obtaining Plua 5
Examining the Distribution Contents 5
Exploring Plua's Features 5
Running the Plua Application 5
Saving Plua Programs 5
Reading the Online Documentation 5
Using Palm OS Streams 5
Compiling Applications 5
Compiling Libraries5
Plua on the Mothership 5
The Command-Line Compiler 5
The Palm OS Emula to r •
Obtaining the Emulator
Installing on W indows
Configuring POSE
Running Plua in the Emulator
8/3/2019 Lua Programming
http://slidepdf.com/reader/full/lua-programming 13/13
Contents
Compiling a Program in the Emulator 58
Exiting th e Emulator 58
The Palm OS Simu la to r 5 8
Obtaining the Simulator 5 8
Using the Simulator . 5 8
Programming with Plua 5 8Generating Graphics 58
Programming the User Interface . 58Accessing Databases 59
Summary 59Exercises 59
Chapter 1 9 : Fitting into th e Lua Com munity 5 9
The Lua Web Site 59
The Lua Reference Manual 59Framing Questions 59The Lua Mailing List 59
Viewing and Searching the Archives 59
Downloading the Archives 59
Using a Web Browser to Access the List 59
Using a Newsreader to Access the List 59
Subscribing to the List Server 59
Posting Messages 60
The Lua Chat Room 6 0
Forums 60The Lua Wiki 6 0
LuaForge 60Annual Workshops 60Summary 60
Appendix A: Answers 6 0
Index 62