Preface
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, 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 tiny and simple language,partly because it does not try to dowhat C is already good for,such as sheer performance, low-level operations,and 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 memory,or how to handle overflows.Higher-order functions and anonymous 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.However, unlike other glue technologies,Lua is a full-fledged language as well.Therefore, 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 with C/C++,and Lua has been used integrated withseveral other languages as well,such as Fortran, Java, Smalltalk, Ada, C#,and even with other scripting languages,such as Perl 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 complete distribution(source code, manual, plus binaries for some platforms)fits comfortably in a floppy disk.
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 and Windows,PlayStation,Xbox, Mac OS X and iOS, Android,Kindle Fire, NOOK,Haiku, QUALCOMM Brew, IBM mainframes,RISC OS, Symbian OS,Rabbit processors, Raspberry Pi, Arduino,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 ANSI (ISO) C.This way,you do not usually need to adapt it to a new environment:if you have an ANSI C compiler,you just have to compile Lua,out of the box.
Audience
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.
Many people use Lua embedded in an application program,such as Adobe Lightroom, Nmap, or World of Warcraft.These applications use the LuaC APIto register new functions,to create new types,and to change the behavior of some language operations,configuring Lua for their specific domains.Frequently,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;players of World of Warcraftmay regard Lua as a language exclusive to that game.
Lua is useful also as a stand-alone language,not only for text-processing and one-shot little programs,but increasingly for medium-to-large projects, too.For such uses, the main functionality of Lua comes from libraries.The standard libraries, for instance,offer basic pattern matching andother functions for string handling.As Lua improves its support for libraries,there has been a proliferation of external packages.Lua Rocks,a deployment and management system for Lua modules,currently features more than 150 packages.
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.
This book has much to offer to all these people.The first part covers the language itself,showing how we can explore all its potential.We focus on different language constructsand use numerous examples and exercisesto show how to use them forpractical tasks.Some chapters in this part cover basic concepts,such as control structures,while others cover topics more advanced,such as iterators and coroutines.
The second part is entirely devoted to tables,the sole data structure in Lua.Its chapters discuss data structures,persistence, packages, and object-oriented programming.There we will unveil the real power of the language.
The third part presents the standard libraries.This part is particularly useful for those that use Luaas a stand-alone language,although many other applicationsalso incorporate all or part of the standard libraries.This part devotes one chapter to each standard library:the mathematical library,the bitwise library,the table library,the string library,the I/O library,the operating system library,and the debug library.
Finally,the last part of the book covers the API between Lua and C,for those that use C to get the full power of Lua.The flavor of this part is necessarily quite different from therest of the book.There, we will be programming in C, not in Lua;therefore, we will be wearing a different hat.For some readers,the discussion of the C API may be of marginal interest;for others, it may be the most relevant part of this book.
About the Third Edition
This book is an updated and expanded version ofthe second edition of Programming in Lua(also known as the PiL 2 book).Although the book structure is virtually the same,this new edition has substantial new material.