


















Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!



















Department of Computer Science, PUC-Rio, Rio de Janeiro, Brazil [email protected]
IMPA–Instituto Nacional de Matem´atica Pura e Aplicada, Brazil [email protected]
Department of Computer Science, PUC-Rio, Rio de Janeiro, Brazil [email protected]
We report on the birth and evolution of Lua and discuss how it moved from a simple configuration language to a versatile, widely used language that supports extensible semantics, anonymous functions, full lexical scoping, proper tail calls, and coroutines.
Categories and Subject Descriptors K.2 [HISTORY OF COMPUTING]: Software; D.3 [PROGRAMMING LAN- GUAGES]
Lua is a scripting language born in 1993 at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil. Since then, Lua has evolved to become widely used in all kinds of industrial applications, such as robotics, literate programming, distributed business, image processing, exten- sible text editors, Ethernet switches, bioinformatics, finite- element packages, web development, and more [2]. In par- ticular, Lua is one of the leading scripting languages in game development. Lua has gone far beyond our most optimistic expecta- tions. Indeed, while almost all programming languages come from North America and Western Europe (with the notable exception of Ruby, from Japan) [4], Lua is the only language created in a developing country to have achieved global rel- evance. From the start, Lua was designed to be simple, small, portable, fast, and easily embedded into applications. These design principles are still in force, and we believe that they account for Lua’s success in industry. The main characteris- tic of Lua, and a vivid expression of its simplicity, is that it offers a single kind of data structure, the table, which is the Lua term for an associative array [9]. Although most script-
ing languages offer associative arrays, in no other language do associative arrays play such a central role. Lua tables provide simple and efficient implementations for modules, prototype-based objects, class-based objects, records, arrays, sets, bags, lists, and many other data structures [28]. In this paper, we report on the birth and evolution of Lua. We discuss how Lua moved from a simple configuration language to a powerful (but still simple) language that sup- ports extensible semantics, anonymous functions, full lexical scoping, proper tail calls, and coroutines. In §2 we give an overview of the main concepts in Lua, which we use in the other sections to discuss how Lua has evolved. In §3 we re- late the prehistory of Lua, that is, the setting that led to its creation. In §4 we relate how Lua was born, what its original design goals were, and what features its first version had. A discussion of how and why Lua has evolved is given in §5. A detailed discussion of the evolution of selected features is given in §6. The paper ends in §7 with a retrospective of the evolution of Lua and in §8 with a brief discussion of the reasons for Lua’s success, especially in games.
In this section we give a brief overview of the Lua language and introduce the concepts discussed in §5 and §6. For a complete definition of Lua, see its reference manual [32]. For a detailed introduction to Lua, see Roberto’s book [28]. For concreteness, we shall describe Lua 5.1, which is the current version at the time of this writing (April 2007), but most of this section applies unchanged to previous versions. Syntactically, Lua is reminiscent of Modula and uses familiar keywords. To give a taste of Lua’s syntax, the code below shows two implementations of the factorial function, one recursive and another iterative. Anyone with a basic knowledge of programming can probably understand these examples without explanation.
function factorial(n) function factorial(n) if n == 0 then local a = 1 return 1 for i = 1,n do else a = ai return nfactorial(n-1) end end return a end end
Semantically, Lua has many similarities with Scheme, even though these similarities are not immediately clear be- cause the two languages are syntactically very different. The influence of Scheme on Lua has gradually increased during Lua’s evolution: initially, Scheme was just a language in the background, but later it became increasingly important as a source of inspiration, especially with the introduction of anonymous functions and full lexical scoping. Like Scheme, Lua is dynamically typed: variables do not have types; only values have types. As in Scheme, a variable in Lua never contains a structured value, only a reference to one. As in Scheme, a function name has no special status in Lua: it is just a regular variable that happens to refer to a function value. Actually, the syntax for function definition ‘function foo() · · · end’ used above is just syntactic sugar for the assignment of an anonymous function to a variable: ‘foo = function () · · · end’. Like Scheme, Lua has first-class functions with lexical scoping. Actually, all values in Lua are first-class values: they can be assigned to global and local variables, stored in tables, passed as arguments to functions, and returned from functions. One important semantic difference between Lua and Scheme — and probably the main distinguishing feature of Lua — is that Lua offers tables as its sole data-structuring mechanism. Lua tables are associative arrays [9], but with some important features. Like all values in Lua, tables are first-class values: they are not bound to specific variable names, as they are in Awk and Perl. A table can have any value as key and can store any value. Tables allow sim- ple and efficient implementation of records (by using field names as keys), sets (by using set elements as keys), generic linked structures, and many other data structures. Moreover, we can use a table to implement an array by using natural numbers as indices. A careful implementation [31] ensures that such a table uses the same amount of memory that an array would (because it is represented internally as an actual array) and performs better than arrays in similar languages, as independent benchmarks show [1]. Lua offers an expressive syntax for creating tables in the form of constructors. The simplest constructor is the expression ‘{}’, which creates a new, empty table. There are also constructors to create lists (or arrays), such as
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}
and to create records, such as
{lat= -22.90, long= -43.23, city= "Rio de Janeiro"}
These two forms can be freely mixed. Tables are indexed using square brackets, as in ‘t[2]’, with ‘t.x’ as sugar for ‘t["x"]’. The combination of table constructors and functions turns Lua into a powerful general-purpose procedural data- description language. For instance, a bibliographic database in a format similar to the one used in BibTEX [34] can be written as a series of table constructors such as this:
article{"spe96", authors = {"Roberto Ierusalimschy", "Luiz Henrique de Figueiredo", "Waldemar Celes"}, title = "Lua: an Extensible Extension Language", journal = "Software: Practice & Experience", year = 1996, }
Although such a database seems to be an inert data file, it is actually a valid Lua program: when the database is loaded into Lua, each item in it invokes a function, because ‘article{· · ·}’ is syntactic sugar for ‘article({· · ·})’, that is, a function call with a table as its single argument. It is in this sense that such files are called procedural data files. We say that Lua is an extensible extension language [30]. It is an extension language because it helps to extend ap- plications through configuration, macros, and other end-user customizations. Lua is designed to be embedded into a host application so that users can control how the application be- haves by writing Lua programs that access application ser- vices and manipulate application data. It is extensible be- cause it offers userdata values to hold application data and extensible semantics mechanisms to manipulate these values in natural ways. Lua is provided as a small core that can be extended with user functions written in both Lua and C. In particular, input and output, string manipulation, mathema- tical functions, and interfaces to the operating system are all provided as external libraries. Other distinguishing features of Lua come from its im- plementation:
Portability: Lua is easy to build because it is implemented in strict ANSI C.^1 It compiles out-of-the-box on most platforms (Linux, Unix, Windows, Mac OS X, etc.), and runs with at most a few small adjustments in virtually all platforms we know of, including mobile devices (e.g., handheld computers and cell phones) and embedded mi- croprocessors (e.g., ARM and Rabbit). To ensure porta- bility, we strive for warning-free compilations under as many compilers as possible. Ease of embedding: Lua has been designed to be easily embedded into applications. An important part of Lua is a well-defined application programming interface (API) that allows full communication between Lua code and external code. In particular, it is easy to extend Lua by exporting C functions from the host application. The API allows Lua to interface not only with C and C++, but also with other languages, such as Fortran, Java, Smalltalk, Ada, C# (.Net), and even with other scripting languages (e.g., Perl and Ruby).
(^1) Actually, Lua is implemented in “clean C”, that is, the intersection of C and C++. Lua compiles unmodified as a C++ library.
programming languages. The important difference — and what made DEL suitable for the data-entry problem — is that entity names also appeared in a separate graphics metafile, which contained the associated diagram over which the en- gineer did the data entry. A single interactive graphical inter- preter called ED (an acronym for ‘entrada de dados’, which means ‘data entry’ in Portuguese) was written to interpret DEL programs. All those data-entry front-ends requested by Petrobras were implemented as DEL programs that ran under this single graphical application. DEL was a success both among the developers at Tec- graf and among the users at Petrobras. At Tecgraf, DEL simplified the development of those front-ends, as originally intended. At Petrobras, DEL allowed users to tailor data- entry applications to their needs. Soon users began to de- mand more power from DEL, such as boolean expressions for controlling whether an entity was active for input or not, and DEL became heavier. When users began to ask for con- trol flow, with conditionals and loops, it was clear that ED needed a real programming language instead of DEL.
At about the same time that DEL was created, a team lead by Roberto Ierusalimschy and Waldemar Celes started working on PGM, a configurable report generator for lithology pro- files, also for Petrobras. The reports generated by PGM con- sisted of several columns (called “tracks”) and were highly configurable: users could create and position the tracks, and could choose colors, fonts, and labels; each track could have a grid, which also had its set of options (log/linear, verti- cal and horizontal ticks, etc.); each curve had its own scale, which had to be changed automatically in case of overflow; etc. All this configuration was to be done by the end-users, typically geologists and engineers from Petrobras working in oil plants and off-shore platforms. The configurations had to be stored in files, for reuse. The team decided that the best way to configure PGM was through a specialized description language called SOL, an acronym for Simple Object Lan- guage. Because PGM had to deal with many different objects, each with many different attributes, the SOL team decided not to fix those objects and attributes into the language. In- stead, SOL allowed type declarations, as in the code below:
type @track{ x:number, y:number=23, id=0 } type @line{ t:@track=@track{x=8}, z:number* } T = @track{ y=9, x=10, id="1992-34" } L = @line{ t=@track{x=T.y, y=T.x}, z=[2,3,4] }
This code defines two types, track and line, and creates two objects, a track T and a line L. The track type contains two numeric attributes, x and y, and an untyped attribute, id; attributes y and id have default values. The line type con- tains a track t and a list of numbers z. The track t has as default value a track with x=8, y=23, and id=0. The syntax
of SOL was strongly influenced by BibTEX [34] and UIL, a language for describing user interfaces in Motif [39]. The main task of the SOL interpreter was to read a report description, check whether the given objects and attributes were correctly typed, and then present the information to the main program (PGM). To allow the communication between the main program and the SOL interpreter, the latter was implemented as a C library that was linked to the main program. The main program could access all configuration information through an API in this library. In particular, the main program could register a callback function for each type, which the SOL interpreter would call to create an object of that type.
The SOL team finished an initial implementation of SOL in March 1993, but they never delivered it. PGM would soon require support for procedural programming to allow the creation of more sophisticated layouts, and SOL would have to be extended. At the same time, as mentioned before, ED users had requested more power from DEL. ED also needed further descriptive facilities for programming its user interface. Around mid-1993, Roberto, Luiz Henrique, and Waldemar got together to discuss DEL and SOL, and con- cluded that the two languages could be replaced by a single, more powerful language, which they decided to design and implement. Thus the Lua team was born; it has not changed since. Given the requirements of ED and PGM, we decided that we needed a real programming language, with assignments, control structures, subroutines, etc. The language should also offer data-description facilities, such as those offered by SOL. Moreover, because many potential users of the language were not professional programmers, the language should avoid cryptic syntax and semantics. The implemen- tation of the new language should be highly portable, be- cause Tecgraf’s clients had a very diverse collection of com- puter platforms. Finally, since we expected that other Tec- graf products would also need to embed a scripting lan- guage, the new language should follow the example of SOL and be provided as a library with a C API. At that point, we could have adopted an existing scripting language instead of creating a new one. In 1993, the only real contender was Tcl [40], which had been explicitly designed to be embedded into applications. However, Tcl had unfa- miliar syntax, did not offer good support for data description, and ran only on Unix platforms. We did not consider LISP or Scheme because of their unfriendly syntax. Python was still in its infancy. In the free, do-it-yourself atmosphere that then reigned in Tecgraf, it was quite natural that we should try to develop our own scripting language. So, we started working on a new language that we hoped would be simpler to use than existing languages. Our original design decisions were: keep the language simple and small, and keep the im-
plementation simple and portable. Because the new language was partially inspired by SOL (sun in Portuguese), a friend at Tecgraf (Carlos Henrique Levy) suggested the name ‘Lua’ (moon in Portuguese), and Lua was born. (DEL did not in- fluence Lua as a language. The main influence of DEL on the birth of Lua was rather the realization that large parts of complex applications could be written using embeddable scripting languages.) We wanted a light full language with data-description fa- cilities. So we took SOL’s syntax for record and list con- struction (but not type declaration), and unified their imple- mentation using tables: records use strings (the field names) as indices; lists use natural numbers. An assignment such as
T = @track{ y=9, x=10, id="1992-34" }
which was valid in SOL, remained valid in Lua, but with a different meaning: it created an object (that is, a table) with the given fields, and then called the function track on this table to validate the object or perhaps to provide default values to some of its fields. The final value of the expression was that table. Except for its procedural data-description constructs, Lua introduced no new concepts: Lua was created for production use, not as an academic language designed to support re- search in programming languages. So, we simply borrowed (even unconsciously) things that we had seen or read about in other languages. We did not reread old papers to remem- ber details of existing languages. We just started from what we knew about other languages and reshaped that according to our tastes and needs. We quickly settled on a small set of control structures, with syntax mostly borrowed from Modula (while, if, and repeat until). From CLU we took multiple assignment and multiple returns from function calls. We regarded mul- tiple returns as a simpler alternative to reference parameters used in Pascal and Modula and to in-out parameters used in Ada; we also wanted to avoid explicit pointers (used in C). From C++ we took the neat idea of allowing a local vari- able to be declared only where we need it. From SNOBOL and Awk we took associative arrays, which we called tables; however, tables were to be objects in Lua, not attached to variables as in Awk. One of the few (and rather minor) innovations in Lua was the syntax for string concatenation. The natural ‘+’ operator would be ambiguous, because we wanted automatic coer- cion of strings to numbers in arithmetic operations. So, we invented the syntax ‘..’ (two dots) for string concatenation.
A polemic point was the use of semicolons. We thought that requiring semicolons could be a little confusing for en- gineers with a Fortran background, but not allowing them could confuse those with a C or Pascal background. In typi- cal committee fashion, we settled on optional semicolons. Initially, Lua had seven types: numbers (implemented solely as reals), strings, tables, nil, userdata (pointers to C objects), Lua functions, and C functions. To keep the lan- guage small, we did not initially include a boolean type: as in Lisp, nil represented false and any other value repre- sented true. Over 13 years of continuous evolution, the only changes in Lua types were the unification of Lua functions and C functions into a single function type in Lua 3.0 (1997) and the introduction of booleans and threads in Lua 5. (2003) (see §6.1). For simplicity, we chose to use dynamic typing instead of static typing. For applications that needed type checking, we provided basic reflective facilities, such as run-time type information and traversal of the global en- vironment, as built-in functions (see §6.11). By July 1993, Waldemar had finished the first implemen- tation of Lua as a course project supervised by Roberto. The implementation followed a tenet that is now central to Extreme Programming: “the simplest thing that could pos- sibly work” [7]. The lexical scanner was written with lex and the parser with yacc, the classic Unix tools for imple- menting languages. The parser translated Lua programs into instructions for a stack-based virtual machine, which were then executed by a simple interpreter. The C API made it easy to add new functions to Lua, and so this first version provided only a tiny library of five built-in functions (next, nextvar, print, tonumber, type) and three small exter- nal libraries (input and output, mathematical functions, and string manipulation). Despite this simple implementation — or possibly be- cause of it — Lua surpassed our expectations. Both PGM and ED used Lua successfully (PGM is still in use today; ED was replaced by EDG [12], which was mostly written in Lua). Lua was an immediate success in Tecgraf and soon other projects started using it. This initial use of Lua at Tec- graf was reported in a brief talk at the VII Brazilian Sympo- sium on Software Engineering, in October 1993 [29]. The remainder of this paper relates our journey in im- proving Lua.
Figure 1 shows a timeline of the releases of Lua. As can be seen, the time interval between versions has been gradually increasing since Lua 3.0. This reflects our perception that
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
1.0 1.1 2.1 2.2 2.4 2.5 3.0 3.1 3.2 4.0 5.0 5.
Figure 1. The releases of Lua.
for many instructions, to try to improve performance. How- ever, this turned out to be too complicated for little per- formance gain and we removed those variants in Lua 3.2. Since Lua 5.0, the virtual machine is register-based [31]. This change gave the code generator more opportunities for optimization and reduced the number of instructions of typi- cal Lua programs. (Instruction dispatch is a significant frac- tion of the time spent in the virtual machine [13].) As far as we know, the virtual machine of Lua 5.0 was the first register-based virtual machine to have wide use.
5.1 Lua 1
The initial implementation of Lua was a success in Tec- graf and Lua attracted users from other Tecgraf projects. New users create new demands. Several users wanted to use Lua as the support language for graphics metafiles, which abounded in Tecgraf. Compared with other programmable metafiles, Lua metafiles have the advantage of being based on a truly procedural language: it is natural to model com- plex objects by combining procedural code fragments with declarative statements. In contrast, for instance, VRML [8] must use another language (Javascript) to model procedural objects. The use of Lua for this kind of data description, especially large graphics metafiles, posed challenges that were unusual for typical scripting languages. For instance, it was not un- common for a diagram used in the data-entry program ED to have several thousand parts described by a single Lua ta- ble constructor with several thousand items. That meant that Lua had to cope with huge programs and huge expressions. Because Lua precompiled all programs to bytecode for a vir- tual machine on the fly, it also meant that the Lua compiler had to run fast, even for large programs. By replacing the lex-generated scanner used in the first version by a hand-written one, we almost doubled the speed of the Lua compiler on typical metafiles. We also modified Lua’s virtual machine to handle a long constructor by adding key-value pairs to the table in batches, not individually as in the original virtual machine. These changes solved the initial demands for better performance. Since then, we have always tried to reduce the time spent on precompilation. In July 1994, we released a new version of Lua with those optimizations. This release coincided with the publication of the first paper describing Lua, its design, and its implementa- tion [15]. We named the new version ‘Lua 1.1’. The previous version, which was never publicly released, was then named ‘Lua 1.0’. (A snapshot of Lua 1.0 taken in July 1993 was released in October 2003 to celebrate 10 years of Lua.) Lua 1.1 was publicly released as software available in source code by ftp, before the open-source movement got its current momentum. Lua 1.1 had a restrictive user license: it was freely available for academic purposes but commercial uses had to be negotiated. That part of the license did not work: although we had a few initial contacts, no commer- cial uses were ever negotiated. This and the fact that other
scripting languages (e.g, Tcl) were free made us realize that restrictions on commercial uses might even discourage aca- demic uses, since some academic projects plan to go to mar- ket eventually. So, when the time came to release the next version (Lua 2.1), we chose to release it as unrestricted free software. Naively, we wrote our own license text as a slight collage and rewording of existing licenses. We thought it was clear that the new license was quite liberal. Later, how- ever, with the spread of open-source licenses, our license text became a source of noise among some users; in particular, it was not clear whether our license was compatible with GPL. In May 2002, after a long discussion in the mailing list, we decided to release future versions of Lua (starting with Lua 5.0) under the well-known and very liberal MIT license [3]. In July 2002, the Free Software Foundation con- firmed that our previous license was compatible with GPL, but we were already committed to adopting the MIT license. Questions about our license have all but vanished since then. 5.2 Lua 2 Despite all the hype surrounding object-oriented program- ming (which in the early 1990s had reached its peak) and the consequent user pressure to add object-oriented features to Lua, we did not want to turn Lua into an object-oriented language because we did not want to fix a programming paradigm for Lua. In particular, we did not think that Lua needed objects and classes as primitive language concepts, especially because they could be implemented with tables if needed (a table can hold both object data and methods, since functions are first-class values). Despite recurring user pres- sure, we have not changed our minds to this day: Lua does not force any object or class model onto the programmer. Several object models have been proposed and implemented by users; it is a frequent topic of discussion in our mailing list. We think this is healthy. On the other hand, we wanted to allow object-oriented programming with Lua. Instead of fixing a model, we de- cided to provide flexible mechanisms that would allow the programmer to build whatever model was suitable to the ap- plication. Lua 2.1, released in February 1995, marked the in- troduction of these extensible semantics mechanisms, which have greatly increased the expressiveness of Lua. Extensible semantics has become a hallmark of Lua. One of the goals of extensible semantics was to allow ta- bles to be used as a basis for objects and classes. For that, we needed to implement inheritance for tables. Another goal was to turn userdata into natural proxies for application data, not merely handles meant to be used solely as arguments to functions. We wanted to be able to index userdata as if they were tables and to call methods on them. This would allow Lua to fulfill one of its main design goals more naturally: to extend applications by providing scriptable access to ap- plication services and data. Instead of adding mechanisms to support all these features directly in the language, we de- cided that it would be conceptually simpler to define a more
general fallback mechanism to let the programmer intervene whenever Lua did not know how to proceed. We introduced fallbacks in Lua 2.1 and defined them for the following operations: table indexing, arithmetic oper- ations, string concatenation, order comparisons, and func- tion calls.^3 When one of these operations was applied to the “wrong” kind of values, the corresponding fallback was called, allowing the programmer to determine how Lua would proceed. The table indexing fallbacks allowed userdata (and other values) to behave as tables, which was one of our motivations. We also defined a fallback to be called when a key was absent from a table, so that we could support many forms of inheritance (through dele- gation). To complete the support for object-oriented pro- gramming, we added two pieces of syntactic sugar: method definitions of the form ‘function a:foo(· · ·)’ as sugar for ‘function a.foo(self,· · ·)’ and method calls of the form ‘a:foo(· · ·)’ as sugar for ‘a.foo(a,· · ·)’. In §6.8 we discuss fallbacks in detail and how they evolved into their later incarnations: tag methods and metamethods. Since Lua 1.0, we have provided introspective functions for values: type, which queries the type of a Lua value; next, which traverses a table; and nextvar, which traverses the global environment. (As mentioned in §4, this was par- tially motivated by the need to implement SOL-like type checking.) In response to user pressure for full debug fa- cilities, Lua 2.2 (November 1995) introduced a debug API to provide information about running functions. This API gave users the means to write in C their own introspective tools, such as debuggers and profilers. The debug API was initially quite simple: it allowed access to the Lua call stack, to the currently executing line, and provided a function to find the name of a variable holding a given value. Following the M.Sc. work of Tom´as Gorham [22], the debug API was improved in Lua 2.4 (May 1996) by functions to access local variables and hooks to be called at line changes and function calls. With the widespread use of Lua at Tecgraf, many large graphics metafiles were being written in Lua as the output of graphical editors. Loading such metafiles was taking in- creasingly longer as they became larger and more complex.^4 Since its first version, Lua precompiled all programs to byte- code just before running them. The load time of a large pro- gram could be substantially reduced by saving this bytecode to a file. This would be especially relevant for procedural data files such as graphics metafiles. So, in Lua 2.4, we in- troduced an external compiler, called luac, which precom- piled a Lua program and saved the generated bytecode to a binary file. (Our first paper about Lua [15] had already an-
(^3) We also introduced fallbacks for handling fatal errors and for monitoring garbage collection, even though they were not part of extensible semantics. (^4) Surprisingly, a substantial fraction of the load time was taken in the lexer for converting real numbers from text form to floating-point representation. Real numbers abound in graphics metafiles.
ticipated the possibility of an external compiler.) The format of this file was chosen to be easily loaded and reasonably portable. With luac, programmers could avoid parsing and code generation at run time, which in the early days were costly. Besides faster loading, luac also allowed off-line syntax checking and protection from casual user changes. Many products (e.g., The Sims and Adobe Lightroom) dis- tribute Lua scripts in precompiled form. During the implementation of luac, we started to restruc- ture Lua’s core into clearly separated modules. As a conse- quence, it is now quite easy to remove the parsing modules (lexer, parser, and code generator), which currently repre- sent 35% of the core code, leaving just the module that loads precompiled Lua programs, which is merely 3% of the core code. This reduction can be significant when embedding Lua in small devices such as mobile devices, robots and sensors.^5 Since its first version, Lua has included a library for string-processing. The facilities provided by this library were minimal until Lua 2.4. However, as Lua matured, it became desirable to do heavier text processing in Lua. We thought that a natural addition to Lua would be pattern matching, in the tradition of Snobol, Icon, Awk, and Perl. However, we did not want to include a third-party pattern- matching engine in Lua because such engines tend to be very large; we also wanted to avoid copyright issues that could be raised by including third-party code in Lua. As a student project supervised by Roberto in the second semester of 1995, Milton Jonathan, Pedro Miller Rabinovitch, Pedro Willemsens, and Vinicius Almendra pro- duced a pattern-matching library for Lua. Experience with that design led us to write our own pattern-matching en- gine for Lua, which we added to Lua 2.5 (November 1996) in two functions: strfind (which originally only found plain substrings) and the new gsub function (a name taken from Awk). The gsub function globally replaced substrings matching a given pattern in a larger string. It accepted either a replacement string or a function that was called each time a match was found and was intended to return the replace- ment string for that match. (That was an innovation at the time.) Aiming at a small implementation, we did not include full regular expressions. Instead, the patterns understood by our engine were based on character classes, repetitions, and captures (but not alternation or grouping). Despite its sim- plicity, this kind of pattern matching is quite powerful and was an important addition to Lua. That year was a turning point in the history of Lua be- cause it gained international exposure. In June 1996 we pub- lished a paper about Lua in Software: Practice & Experi- ence [30] that brought external attention to Lua, at least in
(^5) Crazy Ivan, a robot that won RoboCup in 2000 and 2001 in Denmark, had a “brain” implemented in Lua. It ran directly on a Motorola Coldfire 5206e processor without any operating system (in other words, Lua was the operating system). Lua was stored on a system ROM and loaded programs at startup from the serial port.
5.4 Lua 4
Lua 4.0 was released in November 2000. As mentioned above, the main change in Lua 4.0 was a fully reentrant API, motivated by applications that needed multiple Lua states. Since making the API fully reentrant was already a major change, we took the opportunity and completely redesigned the API around a clear stack metaphor for exchanging val- ues with C (see §6.9). This was first suggested by Reuben Thomas in July 2000. Lua 4.0 also introduced a ‘for’ statement, then a top item in the wish-list of most Lua users and a frequent topic in the mailing list. We had not included a ‘for’ statement earlier because ‘while’ loops were more general. However, users complained that they kept forgetting to update the control variable at the end of ‘while’ loops, thus leading to infinite loops. Also, we could not agree on a good syntax. We considered the Modula ‘for’ too restrictive because it did not cover iterations over the elements of a table or over the lines of a file. A ‘for’ loop in the C tradition did not fit with the rest of Lua. With the introduction of closures and anonymous functions in Lua 3.1, we decided to use higher-order functions for implementing iterations. So, Lua 3.1 provided a higher-order function that iterated over a table by calling a user-supplied function over all pairs in the table. To print all pairs in a table t, one simply said ‘foreach(t,print)’. In Lua 4.0 we finally designed a ‘for’ loop, in two vari- ants: a numeric loop and a table-traversal loop (first sug- gested by Michael Spalinski in October 1997). These two variants covered most common loops; for a really generic loop, there was still the ‘while’ loop. Printing all pairs in a table t could then be done as follows:^8
for k,v in t do print(k,v) end
The addition of a ‘for’ statement was a simple one but it did change the look of Lua programs. In particular, Roberto had to rewrite many examples in his draft book on Lua programming. Roberto had been writing this book since 1998, but he could never finish it because Lua was a moving target. With the release of Lua 4.0, large parts of the book and almost all its code snippets had to be rewritten. Soon after the release of Lua 4.0, we started working on Lua 4.1. Probably the main issue we faced for Lua 4. was whether and how to support multithreading, a big is- sue at that time. With the growing popularity of Java and Pthreads, many programmers began to consider support for multithreading as an essential feature in any programming language. However, for us, supporting multithreading in Lua posed serious questions. First, to implement multithread- ing in C requires primitives that are not part of ANSI C —
(^8) With the introduction of ‘for’ iterators in Lua 5.0, this syntax was marked as obsolete and later removed in Lua 5.1.
although Pthreads was popular, there were (and still there are) many platforms without this library. Second, and more important, we did not (and still do not) believe in the stan- dard multithreading model, which is preemptive concur- rency with shared memory: we still think that no one can write correct programs in a language where ‘a=a+1’ is not deterministic. For Lua 4.1, we tried to solve those difficulties in a typi- cal Lua fashion: we implemented only a basic mechanism of multiple stacks, which we called threads. External libraries could use those Lua threads to implement multithreading, based on a support library such as Pthreads. The same mech- anism could be used to implement coroutines, in the form of non-preemptive, collaborative multithreading. Lua 4.1 alpha was released in July 2001 with support for external multi- threading and coroutines; it also introduced support for weak tables and featured a register-based virtual machine, with which we wanted to experiment. The day after Lua 4.1 alpha was released, John D. Rams- dell started a big discussion in the mailing list about lexi- cal scoping. After several dozen messages, it became clear that Lua needed full lexical scoping, instead of the upvalue mechanism adopted since Lua 3.1. By October 2001 we had come up with an efficient implementation of full lexi- cal scoping, which we released as a work version in Novem- ber 2001. (See §6.6 for a detailed discussion of lexical scop- ing.) That version also introduced a new hybrid representa- tion for tables that let them be implemented as arrays when appropriate (see §6.2 for further details). Because that ver- sion implemented new basic algorithms, we decided to re- lease it as a work version, even though we had already re- leased an alpha version for Lua 4.1. In February 2002 we released a new work version for Lua 4.1, with three relevant novelties: a generic ‘for’ loop based on iterator functions, metatables and metamethods as a replacement for tags and fallbacks^9 (see §6.8), and coroutines (see §6.7). After that release, we realized that Lua 4.1 would bring too many major changes — perhaps ‘Lua 5.0’ would be a better name for the next version.
5.5 Lua 5 The final blow to the name ‘Lua 4.1’ came a few days later, during the Lua Library Design Workshop organized by Christian Lindig and Norman Ramsey at Harvard. One of the main conclusions of the workshop was that Lua needed some kind of module system. Although we had always con- sidered that modules could be implemented using tables, not even the standard Lua libraries followed this path. We then decided to take that step for the next version.
(^9) The use of ordinary Lua tables for implementing extensible semantics had already been suggested by Stephan Herrmann in December 1996, but we forgot all about it until it was suggested again by Edgar Toernig in Octo- ber 2000, as part of a larger proposal, which he called ‘unified methods’. The term ‘metatable’ was suggested by Rici Lake in November 2001.
Packaging library functions inside tables had a big practi- cal impact, because it affected any program that used at least one library function. For instance, the old strfind function was now called string.find (field ‘find’ in string library stored in the ‘string’ table); openfile became io.open; sin became math.sin; and so on. To make the transition easier, we provided a compatibility script that defined the old functions in terms of the new ones:
strfind = string.find openfile = io.open sin = math.sin ...
Nevertheless, packaging libraries in tables was a major change. In June 2002, when we released the next work version incorporating this change, we dropped the name ‘Lua 4.1’ and named it ‘Lua 5.0 work0’. Progress to the final version was steady from then on and Lua 5.0 was re- leased in April 2003. This release froze Lua enough to allow Roberto to finish his book, which was published in Decem- ber 2003 [27]. Soon after the release of Lua 5.0 we started working on Lua 5.1. The initial motivation was the implementation of incremental garbage collection in response to requests from game developers. Lua uses a traditional mark-and- sweep garbage collector, and, until Lua 5.0, garbage col- lection was performed atomically. As a consequence, some applications might experience potentially long pauses dur- ing garbage collection.^10 At that time, our main concern was that adding the write barriers needed to implement an incre- mental garbage collector would have a negative impact on Lua performance. To compensate for that we tried to make the collector generational as well. We also wanted to keep the adaptive behavior of the old collector, which adjusted the frequency of collection cycles according to the total memory in use. Moreover, we wanted to keep the collector simple, like the rest of Lua. We worked on the incremental generational garbage col- lector for over a year. But since we did not have access to applications with strong memory requirements (like games), it was difficult for us to test the collector in real scenarios. From March to December 2004 we released several work versions trying to get concrete feedback on the performance of the collector in real applications. We finally received re- ports of bizarre memory-allocation behavior, which we later managed to reproduce but not explain. In January 2005, Mike Pall, an active member of the Lua community, came up with memory-allocation graphs that explained the prob- lem: in some scenarios, there were subtle interactions be- tween the incremental behavior, the generational behavior, and the adaptive behavior, such that the collector “adapted”
(^10) Erik Hougaard reported that the Crazy Ivan robot would initially drive off course when Lua performed garbage collection (which could take a half second, but that was enough). To stay in course, they had to stop both motors and pause the robot during garbage collection.
for less and less frequent collections. Because it was getting too complicated and unpredictable, we gave up the genera- tional aspect and implemented a simpler incremental collec- tor in Lua 5.1. During that time, programmers had been experimenting with the module system introduced in Lua 5.0. New pack- ages started to be produced, and old packages migrated to the new system. Package writers wanted to know the best way to build modules. In July 2005, during the development of Lua 5.1, an international Lua workshop organized by Mark Hamburg was held at Adobe in San Jose. (A similar work- shop organized by Wim Couwenberg and Daniel Silverstone was held in September 2006 at Oc´e in Venlo.) One of the presentations was about the novelties of Lua 5.1, and there were long discussions about modules and packages. As a re- sult, we made a few small but significant changes in the mod- ule system. Despite our “mechanisms, not policy” guideline for Lua, we defined a set of policies for writing modules and loading packages, and made small changes to support these policies better. Lua 5.1 was released in February 2006. Although the original motivation for Lua 5.1 was incremen- tal garbage collection, the improvement in the module sys- tem was probably the most visible change. On the other hand, that incremental garbage collection remained invisible shows that it succeeded in avoiding long pauses.
In this section, we discuss in detail the evolution of some of the features of Lua.
6.1 Types Types in Lua have been fairly stable. For a long time, Lua had only six basic types: nil, number, string, table, function, and userdata. (Actually, until Lua 3.0, C functions and Lua functions had different types internally, but that difference was transparent to callers.) The only real change happened in Lua 5.0, which introduced two new types: threads and booleans. The type thread was introduced to represent coroutines. Like all other Lua values, threads are first-class values. To avoid creating new syntax, all primitive operations on threads are provided by a library. For a long time we resisted introducing boolean values in Lua: nil was false and anything else was true. This state of affairs was simple and seemed sufficient for our purposes. However, nil was also used for absent fields in tables and for undefined variables. In some applications, it is important to allow table fields to be marked as false but still be seen as present; an explicit false value can be used for this. In Lua 5.0 we finally introduced boolean values true and false. Nil is still treated as false. In retrospect, it would probably have been better if nil raised an error in boolean expres- sions, as it does in other expressions. This would be more consistent with its role as proxy for undefined values. How-
were moved to fields inside tables (see §5.5). More recently, Lua 5.1 brought a complete package and module system based on tables. Tables play a prominent role in Lua’s core. On two oc- casions we have been able to replace special data structures inside the core with ordinary Lua tables: in Lua 4.0 for repre- senting the global environment (which keeps all global vari- ables) and in Lua 5.0 for implementing extensible seman- tics (see §6.8). Starting with Lua 4.0, global variables are stored in an ordinary Lua table, called the table of globals, a simplification suggested by John Belmonte in April 2000. In Lua 5.0 we replaced tags and tag methods (introduced in Lua 3.0) by metatables and metamethods. Metatables are ordinary Lua tables and metamethods are stored as fields in metatables. Lua 5.0 also introduced environment tables that can be attached to Lua functions; they are the tables where global names in Lua functions are resolved at run time. Lua 5.1 extended environment tables to C functions, userdata, and threads, thus replacing the notion of global en- vironment. These changes simplified both the implementa- tion of Lua and the API for Lua and C programmers, be- cause globals and metamethods can be manipulated within Lua without the need for special functions.
6.3 Strings
Strings play a major role in scripting languages and so the facilities to create and manipulate strings are an important part of the usability of such languages. The syntax for literal strings in Lua has had an interesting evolution. Since Lua 1.1, a literal string can be delimited by matching single or double quotes, and can contain C-like escape sequences. The use of both single and double quotes to delimit strings with the same semantics was a bit unusual at the time. (For instance, in the tradition of shell languages, Perl expands variables inside double-quoted strings, but not inside single-quoted strings.) While these dual quotes allow strings to contain one kind of quote without having to escape it, escape sequences are still needed for arbitrary text. Lua 2.2 introduced long strings, a feature not present in classical programming languages, but present in most script- ing languages.^11 Long strings can run for several lines and do not interpret escape sequences; they provide a convenient way to include arbitrary text as a string, without having to worry about its contents. However, it is not trivial to de- sign a good syntax for long strings, especially because it is common to use them to include arbitrary program text (which may contain other long strings). This raises the ques- tion of how long strings end and whether they may nest. Until Lua 5.0, long strings were wrapped inside matching ‘[[· · ·]]’ and could contain nested long strings. Unfortu- nately, the closing delimiter ‘]]’ could easily be part of a valid Lua program in an unbalanced way, as in ‘a[b[i]]’,
(^11) ‘Long string’ is a Lua term. Other languages use terms such as ‘verbatim text’ or ‘heredoc’.
or in other contexts, such as ‘<[!CDATA[· · ·]]>’ from XML. So, it was hard to reliably wrap arbitrary text as a long string. Lua 5.1 introduced a new form for long strings: text de- limited by matching ‘[===[· · ·]===]’, where the number of ‘=’ characters is arbitrary (including zero). These new long strings do not nest: a long string ends as soon as a closing de- limiter with the right number of ‘=’ is seen. Nevertheless, it is now easy to wrap arbitrary text, even text containing other long strings or unbalanced ‘]= · · · =]’ sequences: simply use an adequate number of ‘=’ characters. 6.4 Block comments Comments in Lua are signaled by ‘--’ and continue to the end of the line. This is the simplest kind of comment, and is very effective. Several other languages use single-line comments, with different marks. Languages that use ‘--’ for comments include Ada and Haskell. We never felt the need for multi-line comments, or block comments, except as a quick way to disable code. There was always the question of which syntax to use: the famil- iar ‘/* · · · */’ syntax used in C and several other languages does not mesh well with Lua’s single-line comments. There was also the question of whether block comments could nest or not, always a source of noise for users and of complexity for the lexer. Nested block comments happen when program- mers want to ‘comment out’ some block of code, to disable it. Naturally, they expect that comments inside the block of code are handled correctly, which can only happen if block comments can be nested. ANSI C supports block comments but does not allow nesting. C programmers typically disable code by using the C preprocessor idiom ‘#if 0 · · · #endif’. This scheme has the clear advantage that it interacts gracefully with existing comments in the disabled code. With this motivation and in- spiration, we addressed the need for disabling blocks of code in Lua — not the need for block comments — by introducing conditional compilation in Lua 3.0 via pragmas inspired in the C preprocessor. Although conditional compilation could be used for block comments, we do not think that it ever was. During work on Lua 4.0, we decided that the support for conditional compilation was not worth the complexity in the lexer and in its semantics for the user, especially after not having reached any consensus about a full macro facil- ity (see §7). So, in Lua 4.0 we removed support for con- ditional compilation and Lua remained without support for block comments.^12 Block comments were finally introduced in Lua 5.0, in the form ‘--[[· · ·]]’. Because they intentionally mimicked the syntax of long strings (see §6.3), it was easy to modify the lexer to support block comments. This similarity also helped users to grasp both concepts and their syntax. Block (^12) A further motivation was that by that time we had found a better way to generate and use debug information, and so the pragmas that controlled this were no longer needed. Removing conditional compilation allowed us to get rid of all pragmas.
comments can also be used to disable code: the idiom is to surround the code between two lines containing ‘--[[’ and ‘--]]’. The code inside those lines can be re-enabled by simply adding a single ‘-’ at the start of the first line: both lines then become harmless single-line comments. Like long strings, block comments could nest, but they had the same problems as long strings. In particular, valid Lua code containing unbalanced ‘]]’s, such as ‘a[b[i]]’, could not be reliably commented out in Lua 5.0. The new scheme for long strings in Lua 5.1 also applies to block com- ments, in the form of matching ‘--[===[· · ·]===]’, and so provides a simple and robust solution for this problem.
6.5 Functions
Functions in Lua have always been first-class values. A func- tion can be created at run time by compiling and executing a string containing its definition.^13 Since the introduction of anonymous functions and upvalues in Lua 3.1, programmers are able to create functions at run time without resorting to compilation from text. Functions in Lua, whether written in C or in Lua, have no declaration. At call time they accept a variable number of arguments: excess arguments are discarded and missing arguments are given the value nil. (This coincides with the semantics of multiple assignment.) C functions have always been able to handle a variable number of arguments. Lua 2. introduced vararg Lua functions, marked by a parameter list ending in ‘...’ (an experimental feature that became official only in Lua 3.0). When a vararg function was called, the arguments corresponding to the dots were collected into a table named ‘arg’. While this was simple and mostly convenient, there was no way to pass those arguments to another function, except by unpacking this table. Because programmers frequently want to just pass the arguments along to other functions, Lua 5.1 allows ‘...’ to be used in argument lists and on the right-hand side of assignments. This avoids the creation of the ‘arg’ table if it is not needed. The unit of execution of Lua is called a chunk; it is simply a sequence of statements. A chunk in Lua is like the main program in other languages: it can contain both function definitions and executable code. (Actually, a func- tion definition is executable code: an assignment.) At the same time, a chunk closely resembles an ordinary Lua func- tion. For instance, chunks have always had exactly the same kind of bytecode as ordinary Lua functions. However, before Lua 5.0, chunks needed some internal magic to start execut- ing. Chunks began to look like ordinary functions in Lua 2.2, when local variables outside functions were allowed as an undocumented feature (that became official only in Lua 3.1). Lua 2.5 allowed chunks to return values. In Lua 3.0 chunks became functions internally, except that they were executed
(^13) Some people maintain that the ability to evaluate code from text at run time and within the environment of the running program is what character- izes scripting languages.
right after being compiled; they did not exist as functions at the user level. This final step was taken in Lua 5.0, which broke the loading and execution of chunks into two steps, to provide host programmers better control for handling and reporting errors. As a consequence, in Lua 5.0 chunks be- came ordinary anonymous functions with no arguments. In Lua 5.1 chunks became anonymous vararg functions and thus can be passed values at execution time. Those values are accessed via the new ‘...’ mechanism. From a different point of view, chunks are like modules in other languages: they usually provide functions and vari- ables to the global environment. Originally, we did not in- tend Lua to be used for large-scale programming and so we did not feel the need to add an explicit notion of modules to Lua. Moreover, we felt that tables would be sufficient for building modules, if necessary. In Lua 5.0 we made that feel- ing explicit by packaging all standard libraries into tables. This encouraged other people to do the same and made it easier to share libraries. We now feel that Lua can be used for large-scale programming, especially after Lua 5.1 brought a package system and a module system, both based on tables. 6.6 Lexical scoping From an early stage in the development of Lua we started thinking about first-class functions with full lexical scoping. This is an elegant construct that fits well within Lua’s philos- ophy of providing few but powerful constructs. It also makes Lua apt for functional programming. However, we could not figure out a reasonable implementation for full lexical scop- ing. Since the beginning Lua has used a simple array stack to keep activation records (where all local variables and tem- poraries live). This implementation had proved simple and efficient, and we saw no reason to change it. When we allow nested functions with full lexical scoping, a variable used by an inner function may outlive the function that created it, and so we cannot use a stack discipline for such variables. Simple Scheme implementations allocate frames in the heap. Already in 1987, Dybvig [20] described how to use a stack to allocate frames, provided that those frames did not contain variables used by nested functions. His method requires that the compiler know beforehand whether a vari- able appears as a free variable in a nested function. This does not suit the Lua compiler because it generates code to ma- nipulate variables as soon as it parses an expression; at that moment, it cannot know whether any variable is later used free in a nested function. We wanted to keep this design for implementing Lua, because of its simplicity and efficiency, and so could not use Dybvig’s method. For the same rea- son, we could not use advanced compiler techniques, such as data-flow analysis. Currently there are several optimization strategies to avoid using the heap for frames (e.g., [21]), but they all need compilers with intermediate representations, which the Lua compiler does not use. McDermott’s proposal for stack frame allocation [36], which is explicitly addressed to inter-
satisfactory for her experiments, but unacceptable for an of- ficial Lua version because the ease of mixing Lua code with C code is one of Lua’s hallmarks. Unaware of this difficulty, in December 2001 Thatcher Ulrich announced in the mailing list:
I’ve created a patch for Lua 4.0 that makes calls from Lua to Lua non-recursive (i.e., ‘stackless’). This al- lows the implementation of a ‘sleep()’ call, which ex- its from the host program [... ], and leaves the Lua state in a condition where the script can be resumed later via a call to a new API function, lua_resume.
In other words, he proposed an asymmetric coroutine mech- anism, based on two primitives: yield (which he called sleep) and resume. His patch followed the high-level description given in the mailing list by Bret Mogilefsky on the changes made to Lua 2.5 and 3.1 to add cooperative multitasking in Grim Fandango. (Bret could not provide details, which were proprietary.) Shortly after this announcement, during the Lua Library Design Workshop held at Harvard in February 2002, there was some discussion about first-class continuations in Lua. Some people claimed that, if first-class continuations were deemed too complex, we could implement one-shot contin- uations. Others argued that it would be better to implement symmetric coroutines. But we could not find a proper imple- mentation of any of these mechanisms that could solve the difficulty related to C calls. It took us some time to realize why it was hard to im- plement symmetric coroutines in Lua, and also to under- stand how Ulrich’s proposal, based on asymmetric corou- tines, avoided our difficulties. Both one-shot continuations and symmetric coroutines involve the manipulation of full continuations. So, as long as these continuations include any C part, it is impossible to capture them (except by using fa- cilities outside ANSI C). In contrast, an asymmetric corou- tine mechanism based on yield and resume manipulates par- tial continuations: yield captures the continuation up to the corresponding resume [19]. With asymmetric coroutines, the current continuation can include C parts, as long as they are outside the partial continuation being captured. In other words, the only restriction is that we cannot yield across a C call. After that realization, and based on Ulrich’s proof-of- concept implementation, we were able to implement asym- metrical coroutines in Lua 5.0. The main change was that the interpreter loop, which executes the instructions for the vir- tual machine, ceased to be recursive. In previous versions, when the interpreter loop executed a CALL instruction, it called itself recursively to execute the called function. Since Lua 5.0, the interpreter behaves more like a real CPU: when it executes a CALL instruction, it pushes some context infor- mation onto a call stack and proceeds to execute the called function, restoring the context when that function returns.
After that change, the implementation of coroutines became straightforward. Unlike most implementations of asymmetrical corou- tines, in Lua coroutines are what we call stackfull [19]. With them, we can implement symmetrical coroutines and even the call/1cc operator (call with current one-shot continua- tion) proposed for Scheme [11]. However, the use of C func- tions is severely restricted within these implementations. We hope that the introduction of coroutines in Lua 5. marks a revival of coroutines as powerful control struc- tures [18].
6.8 Extensible semantics As mentioned in §5.2, we introduced extensible semantics in Lua 2.1 in the form of fallbacks as a general mechanism to allow the programmer to intervene whenever Lua did not know how to proceed. Fallbacks thus provided a restricted form of resumable exception handling. In particular, by us- ing fallbacks, we could make a value respond to operations not originally meant for it or make a value of one type be- have like a value of another type. For instance, we could make userdata and tables respond to arithmetic operations, userdata behave as tables, strings behave as functions, etc. Moreover, we could make a table respond to keys that were absent in it, which is fundamental for implementing inheri- tance. With fallbacks for table indexing and a little syntactic sugar for defining and calling methods, object-oriented pro- gramming with inheritance became possible in Lua. Although objects, classes, and inheritance were not core concepts in Lua, they could be implemented directly in Lua, in many flavors, according to the needs of the application. In other words, Lua provided mechanisms, not policy — a tenet that we have tried to follow closely ever since. The simplest kind of inheritance is inheritance by del- egation, which was introduced by Self and adopted in other prototype-based languages such as NewtonScript and JavaScript. The code below shows an implementation of in- heritance by delegation in Lua 2.1.
function Index(a,i) if i == "parent" then return nil end local p = a.parent if type(p) == "table" then return p[i] else return nil end end setfallback("index", Index)
When a table was accessed for an absent field (be it an attribute or a method), the index fallback was triggered. Inheritance was implemented by setting the index fallback to follow a chain of “parents” upwards, possibly triggering
the index fallback again, until a table had the required field or the chain ended. After setting that index fallback, the code below printed ‘red’ even though ‘b’ did not have a ‘color’ field:
a=Window{x=100, y=200, color="red"} b=Window{x=300, y=400, parent=a} print(b.color)
There was nothing magical or hard-coded about delega- tion through a “parent” field. Programmers had complete freedom: they could use a different name for the field con- taining the parent, they could implement multiple inheri- tance by trying a list of parents, etc. Our decision not to hard-code any of those possible behaviors led to one of the main design concepts of Lua: meta-mechanisms. Instead of littering the language with lots of features, we provided ways for users to program the features themselves, in the way they wanted them, and only for those features they needed. Fallbacks greatly increased the expressiveness of Lua. However, fallbacks were global handlers: there was only one function for each event that could occur. As a consequence, it was difficult to mix different inheritance mechanisms in the same program, because there was only one hook for implementing inheritance (the index fallback). While this might not be a problem for a program written by a single group on top of its own object system, it became a problem when one group tried to use code from other groups, because their visions of the object system might not be consistent with each other. Hooks for different mechanisms could be chained, but chaining was slow, complicated, error-prone, and not very polite. Fallback chaining did not encourage code sharing and reuse; in practice almost nobody did it. This made it very hard to use third-party libraries. Lua 2.1 allowed userdata to be tagged. In Lua 3.0 we extended tags to all values and replaced fallbacks with tag methods. Tag methods were fallbacks that operated only on values with a given tag. This made it possible to implement independent notions of inheritance, for instance. No chain- ing was needed because tag methods for one tag did not af- fect tag methods for another tag. The tag method scheme worked very well and lasted until Lua 5.0, when we replaced tags and tag methods by metatables and metamethods. Metatables are just ordinary Lua tables and so can be manipulated within Lua without the need for special functions. Like tags, metatables can be used to represent user-defined types with userdata and tables: all objects of the same “type” should share the same metatable. Unlike tags, metatables and their contents are naturally collected when no references remain to them. (In contrast, tags and their tag methods had to live until the end of the program.) The introduction of metatables also simplified the implementation: while tag methods had their own private representation inside Lua’s core, metatables use mainly the standard table machinery.
The code below shows the implementation of inheritance in Lua 5.0. The index metamethod replaces the index tag method and is represented by the ‘__index’ field in the metatable. The code makes ‘b’ inherit from ‘a’ by setting a metatable for ‘b’ whose ‘__index’ field points to ‘a’. (In general, index metamethods are functions, but we have allowed them to be tables to support simple inheritance by delegation directly.) a=Window{x=100, y=200, color="red"} b=Window{x=300, y=400} setmetatable(b,{ __index = a }) print(b.color) --> red
6.9 C API Lua is provided as a library of C functions and macros that allow the host program to communicate with Lua. This API between Lua and C is one of the main components of Lua; it is what makes Lua an embeddable language. Like the rest of the language, the API has gone through many changes during Lua’s evolution. Unlike the rest of the language, however, the API design received little outside in- fluence, mainly because there has been little research activity in this area. The API has always been bi-directional because, since Lua 1.0, we have considered calling Lua from C and call- ing C from Lua equally important. Being able to call Lua from C is what makes Lua an extension language, that is, a language for extending applications through configuration, macros, and other end-user customizations. Being able to call C from Lua makes Lua an extensible language, because we can use C functions to extend Lua with new facilities. (That is why we say that Lua is an extensible extension lan- guage [30].) Common to both these aspects are two mis- matches between C and Lua to which the API must adjust: static typing in C versus dynamic typing in Lua and manual memory management in C versus automatic garbage collec- tion in Lua. Currently, the C API solves both difficulties by using an abstract stack^15 to exchange data between Lua and C. Every C function called by Lua gets a new stack frame that initially contains the function arguments. If the C function wants to return values to Lua, it pushes those values onto the stack just before returning. Each stack slot can hold a Lua value of any type. For each Lua type that has a corresponding representation in C (e.g., strings and numbers), there are two API functions: an injec- tion function, which pushes onto the stack a Lua value cor- responding to the given C value; and a projection function, which returns a C value corresponding to the Lua value at a given stack position. Lua values that have no correspond- ing representation in C (e.g., tables and functions) can be manipulated via the API by using their stack positions.
(^15) Throughout this section, ‘stack’ always means this abstract stack. Lua never accesses the C stack.
When the C function returned, its whole array was erased, and the values used by the function could be collected if pos- sible. (This change also gave more freedom for implement- ing the garbage collector, because it could move objects if necessary; however, we did not followed this path.) For simple uses, the Lua 2.1 behavior was very practi- cal: it was safe and the C programmer did not have to worry about reference counts. Each lua_Object behaved like a local variable in C: the corresponding Lua value was guar- anteed to be alive during the lifetime of the C function that produced it. For more complex uses, however, this simple scheme had two shortcomings that demanded extra mecha- nisms: sometimes a lua_Object value had to be locked for longer than the lifetime of the C function that produced it; sometimes it had to be locked for a shorter time. The first of those shortcomings had a simple solution: Lua 2.1 introduced a system of references. The function lua_lock got a Lua value from the stack and returned a reference to it. This reference was an integer that could be used any time later to retrieve that value, using the lua_getlocked function. (There was also a lua_unlock function, which destroyed a reference.) With such refer- ences, it was easy to keep Lua values in non-local C vari- ables. The second shortcoming was more subtle. Objects stored in the internal array were released only when the function returned. If a function used too many values, it could over- flow the array or cause an out-of-memory error. For instance, consider the following higher-order iterator function, which repeatedly calls a function and prints the result until the call returns nil:
void l_loop (void) { lua_Object f = lua_getparam(1); for (;;) { lua_Object res; lua_callfunction(f); res = lua_getresult(1); if (lua_isnil(res)) break; printf("%s\n", lua_getstring(res)); } }
The problem with this code was that the string returned by each call could not be collected until the end of the loop (that is, of the whole C function), thus opening the possibility of array overflow or memory exhaustion. This kind of error can be very difficult to track, and so the implementation of Lua 2.1 set a hard limit on the size of the internal array that kept lua_Object values alive. That made the error easier to track because Lua could say “too many objects in a C function” instead of a generic out-of-memory error, but it did not avoid the problem. To address the problem, the API in Lua 2.1 offered two functions, lua_beginblock and lua_endblock, that cre- ated dynamic scopes (“blocks”) for lua_Object values;
all values created after a lua_beginblock were removed from the internal array at the corresponding lua_endblock. However, since a block discipline could not be forced onto C programmers, it was all too common to forget to use these blocks. Moreover, such explicit scope control was a little tricky to use. For instance, a naive attempt to correct our previous example by enclosing the for body within a block would fail: we had to call lua_endblock just before the break, too. This difficulty with the scope of Lua objects persisted through several versions and was solved only in Lua 4.0, when we redesigned the whole API. Nevertheless, as we said before, for typical uses the API was very easy to use, and most programmers never faced the kind of situation described here. More important, the API was safe. Erroneous use could produce well-defined errors, but not dangling ref- erences or memory leaks. Lua 2.1 brought other changes to the API. One was the introduction of lua_getsubscript, which allowed the use of any value to index a table. This function had no explicit arguments: it got both the table and the key from the stack. The old lua_getfield was redefined as a macro, for com- patibility: #define lua_getfield(o,f)
(lua_pushobject(o), lua_pushstring(f),
lua_getsubscript())
(Backward compatibility of the C API is usually imple- mented using macros, whenever feasible.) Despite all those changes, syntactically the API changed little from Lua 1 to Lua 2. For instance, our illustrative func- tion foo could be written in Lua 2 exactly as we wrote it for Lua 1.0. The meaning of lua_Object was quite different, and lua_getfield was implemented on top of new primi- tive operations, but for the average user it was as if nothing had changed. Thereafter, the API remained fairly stable until Lua 4.0. Lua 2.4 expanded the reference mechanism to support weak references. A common design in Lua programs is to have a Lua object (typically a table) acting as a proxy for a C object. Frequently the C object must know who its proxy is and so keeps a reference to the proxy. However, that reference prevents the collection of the proxy object, even when the object becomes inaccessible from Lua. In Lua 2.4, the program could create a weak reference to the proxy; that reference did not prevent the collection of the proxy object. Any attempt to retrieve a collected reference resulted in a special value LUA_NOOBJECT. Lua 4.0 brought two main novelties in the C API: support for multiple Lua states and a virtual stack for exchanging values between C and Lua. Support for multiple, indepen- dent Lua states was achieved by eliminating all global state. Until Lua 3.0, only one Lua state existed and it was imple- mented using many static variables scattered throughout the code. Lua 3.1 introduced multiple independent Lua states; all static variables were collected into a single C struct. An
API function was added to allow switching states, but only one Lua state could be active at any moment. All other API functions operated over the active Lua state, which remained implicit and did not appear in the calls. Lua 4.0 introduced explicit Lua states in the API. This created a big incompat- ibility with previous versions.^17 All C code that communi- cated with Lua (in particular, all C functions registered to Lua) had to be changed to include an explicit state argument in calls to the C API. Since all C functions had to be rewrit- ten anyway, we took this opportunity and made another ma- jor change in the C – Lua communication in Lua 4.0: we replaced the concept of lua_Object by an explicit virtual stack used for all communication between Lua and C in both directions. The stack could also be used to store temporary values. In Lua 4.0, our foo example could be written as follows:
int foo_l (lua_State *L) { lua_pushstring(L, "x"); lua_gettable(L, 1); return 1; }
The first difference is the function signature: foo_l now re- ceives a Lua state on which to operate and returns the num- ber of values returned by the function in the stack. In pre- vious versions, all values left in the stack when the func- tion ended were returned to Lua. Now, because the stack is used for all operations, it can contain intermediate values that are not to be returned, and so the function needs to tell Lua how many values in the stack to consider as return val- ues. Another difference is that lua_getparam is no longer needed, because function arguments come in the stack when the function starts and can be directly accessed by their in- dex, like any other stack value. The last difference is the use of lua_gettable, which replaced lua_getsubscript as the means to access table fields. lua_gettable receives the table to be indexed as a stack position (instead of as a Lua object), pops the key from the top of the stack, and pushes the result. Moreover, it leaves the table in the same stack position, because tables are frequently indexed repeatedly. In foo_l, the table used by lua_gettable is at stack position 1, because it is the first argument to that function, and the key is the string "x", which needs to be pushed onto the stack before calling lua_gettable. That call replaces the key in the stack with the corresponding table value. So, after lua_gettable, there are two values in the stack: the table at position 1 and the result of the indexing at position 2, which is the top of the stack. The C function returns 1 to tell Lua to use that top value as the single result returned by the function. To further illustrate the new API, here is an implementa- tion of our loop example in Lua 4.0:
(^17) We provided a module that emulated the 3.2 API on top of the 4.0 API, but we do not think it was used much.
int l_loop (lua_State *L) { for (;;) { lua_pushvalue(L, 1); lua_call(L, 0, 1); if (lua_isnil(L, -1)) break; printf("%s\n", lua_tostring(L, -1)); lua_pop(L, 1); } return 0; }
To call a Lua function, we push it onto the stack and then push its arguments, if any (none in the example). Then we call lua_call, telling how many arguments to get from the stack (and therefore implicitly also telling where the function is in the stack) and how many results we want from the call. In the example, we have no arguments and expect one result. The lua_call function removes the function and its arguments from the stack and pushes back exactly the requested number of results. The call to lua_pop removes the single result from the stack, leaving the stack at the same level as at the beginning of the loop. For convenience, we can index the stack from the bottom, with positive indices, or from the top, with negative indices. In the example, we use index -1 in lua_isnil and lua_tostring to refer to the top of the stack, which contains the function result. With hindsight, the use of a single stack in the API seems an obvious simplification, but when Lua 4.0 was released many users complained about the complexity of the new API. Although Lua 4.0 had a much cleaner conceptual model for its API, the direct manipulation of the stack requires some thought to get right. Many users were content to use the previous API without any clear conceptual model of what was going on behind the scenes. Simple tasks did not require a conceptual model at all and the previous API worked quite well for them. More complex tasks often broke whatever private models users had, but most users never programmed complex tasks in C. So, the new API was seen as too complex at first. However, such skepticism gradually vanished, as users came to understand and value the new model, which proved to be simpler and much less error- prone. The possibility of multiple states in Lua 4.0 created an un- expected problem for the reference mechanism. Previously, a C library that needed to keep some object fixed could cre- ate a reference to the object and store that reference in a global C variable. In Lua 4.0, if a C library was to work with several states, it had to keep an individual reference for each state and so could not keep the reference in a global C vari- able. To solve this difficulty, Lua 4.0 introduced the registry, which is simply a regular Lua table available to C only. With the registry, a C library that wants to keep a Lua object can choose a unique key and associate the object with this key in the registry. Because each independent Lua state has its own registry, the C library can use the same key in each state to manipulate the corresponding object.