I’ve been hearing about Lua for a few years now, but I never took the time to sit down and read more about the language, the only thing I knew was that WoW used it, and that it was created by a group of teachers in Brazil

Speaking with my brother in law, that now works in the same university as the teachers that created Lua, recommended to me a podcast where the main creator of Lua talks about the language in general.

The podcast is VERY good, Roberto Ierusalimschy explains why he created Lua, all the architectural design behind it, his philosophy regarding the project, some good use cases for the language and the future for Lua(the podcast is in Portuguese)

After listening to the podcast, I was really surprised to hear that Lua’s only data structure is Tables, much like the object notation in javscript, and that a lot of features in the new JS engines today are somewhat derived from Lua.
I decided to give a try and play a bit with the language.

Bellow is a simple tutorial to get Lua up and running on Ubuntu

Installing Lua

There are a few different ways you can install Lua on your machine, I decided to get the source code and compile it.
Lua it self is not that big, ~20,000 lines of code.

You can download the code here:

I downloaded the latest stable version, 5.2.0

After downloading/extracting, you’ll ge this files:

/doc
Makefile
README
src

All it takes to compile Lua is a simple:

make linux test

linux species which platform you want to build.
The complete list of supported platforms:

  • aix
  • ansi
  • bsd
  • freebsd
  • generic
  • linux
  • macosx
  • mingw
  • posix
  • solaris

test just prints the version of Lua to stdout

After compiling, the Lua executable will be created in your /src dir

To open the Lua shell: ./lua

*If you want to add the lua executable to your path there are several different ways to do it. A simple way is to create a symbolic link to lua in your $HOME/bin dir. It will automatically add lua to your PATH next time you log in

**You might get this error if you don’t have the readline lib installed:

To install the readline lib:

sudo apt-get install libreadline5-dev

After installing the lib, you should get this output:

If you don’t want to install the readline lib, you can make a few modifications to their build system:
Changing src/Makefile

linux:
$(MAKE) $(ALL) SYSCFLAGS="-DLUAUSELINUX" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses"

to

linux:
$(MAKE) $(ALL) SYSCFLAGS="-DLUAUSELINUX" SYSLIBS="-Wl,-E -ldl -lncurses"

also removing the lib from src/luaconf.h

  
 #if defined(Lua_USE_LINUX)  
 #define Lua_USE_POSIX  
 #define Lua_USE_DLOPEN /* needs an extra library: -ldl */  
 #define Lua_USE_READLINE /* needs some extra libraries */  
 #define Lua_USE_STRTODHEX /* assume ‘strtod’ handles hexa formats */  
 #define Lua_USE_AFORMAT /* assume ‘printf’ handles ‘aA’ specifiers */  
 #define Lua_USE_LONGLONG /* assume support for long long */  
 #endif  

or just do a:

make ansi

More info here.

Running Some Programs:

To get started, lets run the famous Hello World program.
In Lua, all it takes is a simple:

print("Hello World!")

To run the program:

lua hello-world.lua

Another example using tables:

 
 obj = {  
 a = 1,  
 b = {  
 str = "b1",  
 dec = 2.1  
 },  
 c = 3,  
 f = function (x)  
 return x*2  
 end  
 }

for index,value in pairs(obj) do print(index,value) end

print(obj.f(2))  
 print(obj.b.str)  
 print(obj.b.dec)  

Much like in javascript, you can create tables using the object notation: { }

You can think of tables as an associative array, a key value pair structure, just like a hash.

That’s very powerful and gives a lot of flexibility when writing programs.
You can create very complex data structures with a few lines of code.

Use Cases

The Lua project has more than 15 year of existence, being tested and used by several different companies

Adobe has more than 100 engineers working specifically with Lua.
The Adobe Light Room was mainly written in Lua.

Huawei, the second largest network and telecommunications equipment company in the world has more than 1 million lines of Lua written in their products

Some other well known projects that use Lua are:

The list of games is huge, close to a total of 160 different titles:

You can check the complete list here:

Lua and the Web

What got me really excited about Lua, was the fact that it can also be used as a web server.
Actually, some benchmarks show that Lua can be up to 3 times faster than node.js and the VM is also a lot smaller than node.

There are a few projects that started porting Lua to be much like a nodejs server. Using the Lua VM instead of the V8 engine, but keeping all the awesome architecture existing in node

One of the projects is Luvit.
The project is still on its early stages of dev, but looks very promising :)

Other cool projects involving Lua:
LuaLibEvent
lua-ev
LuaNode
JSON4Lua
lunit