Lua is spreading more and more throughout different areas of software, from embedded systems and mobile devices to the Web and the Internet of Things. Besides, it has a major role in the game industry, where knowledge of Lua has become an indisputable asset. Lua is the language of choice for anyone who needs a scripting language that is simple, efficient, extensible, portable, and free. Programming in Lua is the official book about the language, providing a solid base to any programmer who wants to use Lua.
Authored by Roberto Ierusalimschy, the chief architect of the language, it covers all aspects of Lua 5---from the basics to its API with C. The book is the main source of programming patterns for Lua, with numerous code examples that help the reader to make the most of Lua's flexibility and powerful mechanisms. Programming in Lua is targeted at people with some programming background, but does not assume any prior knowledge about Lua or other scripting languages. This Fourth Edition updates the book to Lua 5.3 and marks a complete reorganization of the text. Building on his many years of experience teaching Lua, Roberto has restructured the book to present the material in a growing order of complexity, allowing the reader to better absorb the character of the language.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
The definitive guide to Lua 5.3, written by the language's chief architect, teaching you both the language itself and how to embed and extend it through C. Best for programmers who already know another language and want a scripting engine that is small, fast, and designed to be extended.
【Book Arc】
- **Opening (~0%–10%)**: Establishes Lua's design philosophy—simplicity, extensibility, portability—and gets you running the interpreter, then covers the core value types: numbers (the new 64-bit integers vs. floats in 5.3) and strings (byte sequences, immutability, UTF-8 handling).
- **Early (~10%–30%)**: The heart of the language: tables as the universal data structure, traversal with `pairs`/`ipairs`, multiple return values, proper tail calls, and the scoping rules (locals, `do` blocks, `goto`) that shape idiomatic Lua.
- **Middle (~30%–50%)**: Practical programming with the standard libraries—pattern matching and string manipulation, date/time, file I/O—plus classic data structures (linked lists, queues, graphs) built from tables, and serialization techniques.
- **Late (~50%–80%)**: The advanced language machinery: metatables and metamethods, environments, modules and packages, object-oriented programming via table fields, and the mechanisms that make Lua so flexible.
- **Ending (~80%–100%)**: The C API—interfacing Lua with C/C++ and other languages, extending Lua with external libraries, and the embedding patterns that make Lua a "kit for building domain-specific languages."
【Key Takeaways】
- **Lua is a kit, not just a language** (Opening): Its extensibility is so central that many treat it as a framework for building domain-specific languages; Lua implements much of its own basic functionality through external libraries.
- **Tables are the single universal data structure** (Early): Arrays, records, sets, objects, and modules are all tables; understanding anonymous tables, reference semantics, and garbage collection is the foundation for everything else.
- **Lua 5.3's hallmark is the integer/float split** (Opening): 64-bit integers wrap around predictably on overflow, and the change created few incompatibilities because doubles represent integers exactly up to 2^53.
- **Functions are first-class and multi-valued** (Early): Functions can return multiple results, be stored in table fields (the basis of OOP), and Lua performs proper tail-call elimination for state machines and recursion.
- **Strings are immutable byte sequences** (Opening): Lua is eight-bit clean and can hold any binary data; the standard string library assumes one-byte characters but handles UTF-8 reasonably, with a dedicated utf8 library since 5.3.
- **Patterns are a lightweight, powerful alternative to regex** (Middle): Lua's pattern matching supports character classes, repetition modifiers, and captures, enabling tasks like recursive XML transformation and URL encoding.
- **Data structures are built, not built-in** (Middle): Linked lists, queues, double-ended queues, and graphs are all implemented from tables; often a simpler table representation beats a "textbook" structure.
- **The C API is where Lua's real power lives** (Ending): Interfacing with C/C++, Java, C#, and Python is straightforward, and the API is the path to embedding Lua in games, embedded systems, and IoT devices.
【Reading Tips】
- **Deep-read the early chapters on tables and functions**—they are the conceptual core; skimming them will make metatables and OOP feel arbitrary later.
- **Skim the standard-library chapters** (strings, I/O, dates) on a first pass and return to them as reference; the pattern-matching chapter deserves a slower second read.
- **Treat the C API section as a separate project**: it assumes C fluency and is best read with a compiler open, experimenting with embedding a small interpreter.
- **Do the exercises**, especially in the data-structures and serialization chapters—they expose edge cases (empty strings, queue index reset, cyclic tables) that the prose only hints at.
- **Watch the version notes**: the book targets Lua 5.3, and the integer/float distinction is the main incompatibility with older code you may encounter.
【Coverage Limits】
This guide is based on stratified excerpts covering roughly the first half of the book in detail, with lighter coverage of the metatable, module, OOP, and C API chapters; specific chapter titles and later-chapter examples are not fully represented in the source material.
Page 11
ontributes to its small size. (Its Linux 64-bit executable, including all standard libraries, has 220 KB.) Efficiency: Lua has a quite efficient implementati...
an return multiple results. Several predefined functions in Lua return multiple values. We have already seen the function string.find, which locates a patter...
it starts with a one-line header with the string "P1"; then there is one line with the width and height of the drawing, in pixels. Finally, there is a sequen...
o that both indices return to zero when the queue is empty. Exercise 14.3: Modify the graph structure so that it can keep a label for each arc. The structure...
: the invariant state and the control variable. A stateless iterator generates the next element for the iteration using only these two values. A typical exam...
rom load using an auxiliary table. Before calling load, the server checks in the table whether the given string already has a translation. If it cannot find...
he calling function. (The returned table should not include environmental variables; instead, it should inherit them from the original environment.) IV The C...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Add Tag
Enter tag name (max 50 characters)
Share E-Book
Programming in Lua (Roberto Ierusalimschy)(Z-Library)
Scan QR code with your phone to access
Copy the link or scan the QR code to access this e-book on your phone
Share E-Book via Email
Please enter email address
Donation Statistics
¥.00
Total Donations
0
Donation Count
Programming in Lua (Roberto Ierusalimschy)(Z-Library)
Find Your Favorite Books
Only registered users can comment after logging in. Comments need to be reviewed by administrators before being displayed
Loading comments...
Reply to Comment
Edit Comment