In association with heise online

17 April 2012, 16:59

The H Speed Guide to Lua

by Dj Walker-Morgan

Some languages arrive with much publicity, often to then stall, while others quietly get on with a job and steadily spread till you can't help but notice them. Lua is a language from the latter category, mainly because it was designed to be embedded and to not draw attention to itself by chewing up resources.

But when you find open source projects such as VLC, Wireshark, Nginx and Celestia and commercial software such as Adobe Photoshop Lightroom using Lua as the basis for scripting in the applications, and it starts picking up awards because it's achieved critical mass in particular industries, it is worth finding out a little more about the language and why it is appearing as part of these applications.

Why Lua?

Lua logo Lua has been designed to be embedded into applications and devices from the start. This design goal has led to it being very compact, but delivering a lot of power for its size. The source code for implementing the language is only 20,000 lines of ANSI C code and, compiled on Linux with standard libraries, only takes 182KB of memory; another 240KB gets you the Lua library. That includes a register-based virtual machine for running Lua code which is compiled to its own byte code, along with automatic memory management and incremental garbage collection.

A focus on being embedded has also given Lua an advantage over scripting languages that have been built to be standalone, such as Python and Perl, by making it easy to include in an application and with a simple C API that allows C code to be compellingly integrated with the language. But, like any major element of code, it will have an impact on surrounding code and will need developers to be aware of its use of memory and other resources.

Lua's popularity is also, in part, because it supplies enough of a language to be useful, but not enough to enforce a programming style or paradigm on the people who use it. It doesn't come with a pile of data structures or a huge complex syntax so it is easy to pick up the basics. This can be a good characteristic in a language which typical users will only be exposed to occasionally and usually with a particular, application-related task in mind.

A Brief History of Lua

Lua was created by, and is still maintained by, a team at the Department of Computer Science of the Pontifical Catholic University Rio de Janerio, Brazil, made up of Professor Roberto Ierusalimschy, Professor Waldemar Celes and Luiz Henrique de Figueiredo.

Lua made its first public appearance in 1994 alongside a conference paper on a new language designed for extending applications, but had, since the previous year, been in use in research centres where industrial applications were being developed.

The language was not open source though. That changed in 1995 with Lua 2.1 (paper), when it became freely available for all purposes and added support for object-oriented programming. Since then, Lua has steadily improved with better function support (2.2), an external compiler (2.4), pattern matching and varargs (2.5), tag methods (3.0), anonymous functions and closures (3.1), a new debug library (3.2), for statements and states (4.0) and collaborative multi-threading (5.0). By 2006, Lua 5.1, which is still being supported, had arrived with a new module system and incremental garbage collection. The most recent release is Lua 5.2, which arrived in December last year adding, among other things, a yield ability for co-routines.

Alternative Lua

Lua has matured into a stable platform for developers, while still being enhanced over time. That is in part because Lua is a glue for applications and in part because other Lua developments take place in other projects. For example, in 2005 the LuaJit project began, creating a version of Lua with a just-in-time compiler; currently, the project is developing version 2.0.0 with a beta 9 having been released last year. In some benchmarks on Intel, PPC and ARM, the JITted Lua can be up to 64 times faster than the interpreter. JIT compilation is useful for some applications where the resources are available to do the compilation, but developers have also wanted to use Lua in much more confined situations. The Embedded Lua project, eLua, caters for running Lua on "bare-metal" with no operating system.

Next: Developing with Lua

Print Version | Permalink:
  • Twitter
  • Facebook
  • submit to slashdot
  • StumbleUpon
  • submit to reddit

  • July's Community Calendar

The H Open

The H Security

The H Developer

The H Internet Toolkit