BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Lua: Embeddable Scripting Engine, LuaJIT 2.0 Includes New High-Performance VM

Lua: Embeddable Scripting Engine, LuaJIT 2.0 Includes New High-Performance VM

This item in japanese

Lua is a scripting language that can be embedded in any application that can provide hooks for its C API. Version 2.0 of its JIT compiler is in Beta and includes a completely re-written VM based on a trace compiler and an interpreter written in assembler.

Lua means Moon in Portuguese, and it was released internally by a group of three professors, namely Roberto Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo, back in 1993. Since then, the language has passed through a number of versions, the latest being Lua 5.1.4, released in 2008.

Lua is a general purpose embeddable scripting language with a syntax borrowed from Modula, with some influences from SNOBOL, AWK, C++, and Lisp. Scheme has also influenced the language over time. The data-description syntax came from SOL (Portuguese for 'Sun'), a language created by Ierusalimschy and later replaced by Lua. Other characteristics of the language are: functional, object-oriented through extensions, prototype-based and garbage collected.

Lua has a C API allowing it to be easily embedded in applications written in various languages.  The API is divided in a core API and an extension library. The binary package is extremely small at 200-700 KB depending on the platform. Lua runs on any operating system that has an ANSI or ISO C compiler. Practically, it runs on all flavors of Unix/Linux, on Windows, but also on handheld devices with ARM or Rabbit microprocessors/controllers.

One important feature is the table data type, the only complex data type but one that can be extended. A table is:

a collection of key and data pairs (known also as hashed heterogeneous associative array), where the data is referenced by key. The key (index) can be of any data type except nil. An integer key of 1 is considered distinct from a string key of "1".

Lua does not have the concept of a class, but they can be created through extensions using functions and tables:

By placing functions and related data into a table, an object is formed. Inheritance (both single and multiple) can be implemented via the "metatable" mechanism, telling the object to lookup nonexistent methods and fields in parent object(s).

A simple example of Lua code looks like this:

function factorial(n)
  if n == 0 then
    return 1
  else
    return n * factorial(n - 1)
  end
end

Unlike other scripting languages, Lua is not interpreted directly but rather compiled into bytecode that runs on its own register-based virtual machine. Michael Pall, an open source developer from Germany, has created a JIT compiler for Lua, version 2.0 currently being in Beta 2. Pall has created the JIT for the x86 architecture, and a porting to the 64-bit architecture is in the works. Its author commented on LuaJIT:

It combines high flexibility with high performance and an unmatched low memory footprint: less than 120K for the VM plus less than 80K for the JIT compiler. …

2009 also marks the first release of the long-awaited LuaJIT 2.0. The whole VM has been rewritten from the ground up and relentlessly optimized for performance. It combines a high-speed interpreter, written in assembler, with a state-of-the-art JIT compiler.

An innovative trace compiler is integrated with advanced, SSA-based optimizations and a highly tuned code generation backend. This allows a substantial reduction of the overhead associated with dynamic language features. It's destined to break into the performance range traditionally reserved for offline, static language compilers.

While Lua can be embedded in all sorts of applications, it has found more acceptance among game creators. The main reason besides its performance is the ability for a game developer to interrupt the game and tweak various parameters then to continue the game without actually stopping it to perform a rebuild. Games like World of Warcraft, League of Legends, the Star Wars series, and dozens of others have made use of Lua during development. There are also other types of applications using Lua or instrumented to use it, with examples including Adobe Photoshop Lightroom, Apache HTTP Server (through module mod_lua), MySQL Proxy, nmap, and many others. There are also at least two web frameworks written in Lua: Kepler, and Orbit.

Rate this Article

Adoption
Style

BT