About the Book
When Waldemar, Luiz,and I started the development of Lua,back in 1993,we could hardly imagine that it would spread as it did.Started as an in-house language for two specific projects,currently Lua is widely used in all areas that can benefit from asimple, extensible, portable, and efficient scripting language,such as embedded systems,mobile devices,the Internet of Things,and, of course, games.
We designed Lua, from the beginning,to be integrated with software written in C/C++and other conventional languages.This integration brings many benefits.Lua is a small and simple language,partly because it does not try to dowhat C is already good for,such as sheer performanceand interface with third-party software.Lua relies on C for these tasks.What Lua does offer is what C is not good for:a good distance from the hardware,dynamic structures, no redundancies,and ease of testing and debugging.For these goals, Lua has a safe environment,automatic memory management,and good facilities for handling strings and otherkinds of data with dynamic size.
Part of the power of Lua comes from its libraries.This is not by chance.After all,one of the main strengths of Lua is its extensibility.Many features contribute to this strength.Dynamic typing allows a great degree of polymorphism.Automatic memory management simplifies interfaces,because there is no need to decide who is responsiblefor allocating and deallocating memoryor how to handle overflows.First-class functions allowa high degree of parameterization,making functions more versatile.
More than an extensible language,Lua is also a glue language.Lua supports a component-based approach to software development,where we create an application by gluing togetherexisting high-level components.These components are written in a compiled,statically-typed language, such as C or C++;Lua is the glue that we use to compose and connect these components.Usually, the components (or objects) represent more concrete,low-level concepts (such as widgets and data structures)that are not subject to many changes during program development,and that take the bulk of the CPU time of the final program.Lua gives the final shape of the application,which will probably change a lotduring the life cycle of the product.We can use Lua not only to glue components,but also to adapt and reshape them,and to create completely new components.
Of course, Lua is not the only scripting language around.There are other languages that you can use for more or lessthe same purposes.Nevertheless, Lua offers a set of features that makes ityour best choice for many tasks and gives it a unique profile:
Extensibility:Luas extensibility is so remarkable that many peopleregard Lua not as a language,but as a kit for building domain-specific languages.We designed Lua from scratch to be extended,both through Lua code and through external C code.As a proof of concept,Lua implements most of its own basic functionalitythrough external libraries.It is really easy to interface Lua withexternal languages likeC/C++, Java, C#, and Python.
Simplicity:Lua is a simple and small language.It has few (but powerful) concepts.This simplicity makes Lua easy to learnand contributes to its small size.(Its Linux 64-bit executable,including all standard libraries,has 220 KB.)
Efficiency:Lua has a quite efficient implementation.Independent benchmarks show Lua asone of the fastest languages in the realm ofscripting languages.
Portability:When we talk about portability,we are talking about running Luaon all platforms we have ever heard about:all flavors of UNIX (Linux, FreeBSD, etc.)Windows,Android,iOS,OS X,IBM mainframes,game consoles (PlayStation, Xbox, Wii, etc.),microcontrollers (Arduino, etc.),and many more.The source code for each of these platformsis virtually the same.Lua does not use conditional compilationto adapt its code to different machines;instead, it sticks to the standard ISO (ANSI) C.This way,you do not usually need to adapt it to a new environment:if you have an ISO C compiler,you just have to compile Lua,out of the box.
Audience
This book does not assume any prior knowledge of Lua orany specific programming languageexcept for its last part, which discusses the Lua API with C.However, it assumes the knowledge of some basic programming concepts,in particular variables and assignment,control structures, functions and parameters,recursion,streams and files, and basic data structures.
Lua users typically fall into three broad groups:those that use Lua already embedded in an application program,those that use Lua stand alone,and those that use Lua and C together.This book has much to offer to all these groups.
Many people use Lua embedded in an application program,such as Adobe Lightroom, Nmap, or World of Warcraft.These applications use Luas C APIto register new functions,to create new types,and to change the behavior of some language operations,configuring Lua for their specific domains.Often,the users of such applications do not even know thatLua is an independent language adapted for a particular domain.For instance,many developers of plug-ins for Lightroom do not knowabout other uses of the language;Nmap users tend to think of Lua as the languageof the Nmap Scripting Engine;many players of World of Warcraftregard Lua as a language exclusive to that game.Despite these different worlds,the core language is still the same,and the programming techniques you will learn hereapply everywhere.
Lua is useful also as a stand-alone language,not only for text processing and one-shot little programs,but for medium-to-large projects, too.For such uses, the main functionality of Lua comes from libraries.The standard libraries, for instance,offer pattern matching andother functions for string handling.As Lua has improved its support for libraries,there has been a proliferation of external packages.LuaRocks,a deployment and management system for Lua modules,passed one thousand modules in 2015,covering all sorts of domains.
Finally, there are those programmers that work onthe other side of the bench,writing applications that use Lua as a C library.Those people will program more in C than in Lua,although they need a good understanding of Luato create interfaces that are simple,easy to use, and well integrated with the language.
Book Structure
This editionadds new material and examples in many areas,including sandboxing, coroutines,date and time manipulation,in addition to the new material related to version 5.3:integers, bitwise operations, unsigned integers, etc.
More importantly,this edition marks a major restructuring of the text.Instead of organizing the materialaround the language(e.g., with separate chapters for each library),I tried to organize the material aroundcommon themes in programming.That organization allows the bookto better follow an order of increasing complexity,with simple themes coming first.That order came from experience teachingcourses about the language;in particular,I think this new organization fitsthe book better as a didactic resourcefor courses involving Lua.
As the previous editions,this one is organized in four parts,each with around nine chapters.However, the parts have a quite new character.
The first partcovers the basics of the language(and it is fittingly named The Basics).It is organized around the main types of values in Lua:numbers, strings, tables, and functions.It also covers basic I/O andgives an overview of the syntax of the language.
The second part,called Real Programming,covers more advanced topics thatyou can expect to find in other similar languages,such as closures, pattern matching,date and time manipulation,data structures,modules, and error handling.
The third part is called