This was a talk I gave at the Sarasota Software Engineers User Group in Sarasota on August 26, 2015
- Abstract: Most popular scripting languages now contain many paradigm specific and platform specific features that increase the complexity of the language. Few of these languages are simple enough to be embedded in existing applications easily. Lua’s extensibility and simplicity makes it an ideal choice for embedded scripting. Lua is often embedded in video games, desktop GUI applications, server software, and even mobile applications. This talk covers the basics of the Lua. I will demonstrate how Lua’s simplicity allows for quick development of small utility scripts. I will also show how Lua’s powerful meta-mechanisms allow for rapid development of software in prototypal OO, classical OO, and functional paradigms. At the end of the talk I will integrate Lua into an existing application.
- Slides: https://speakerdeck.com/stratus3d/introduction-to-lua
I started learning Lua at the end of 2013 for a project that I was going to be working on. At the beginning of 2014 I started on the project - using Corona SDK to develop a simple native mobile app for Android and iOS. Development took about two months to complete. Corona SDK has a Lua API that allows you to write native iOS and Android apps in Lua code. Overall Corona SDK works pretty well. The documentation seems to be out of date in some places but in general was pretty good. The API did have a few quirks but it is more than sufficient for mobile games. Business apps are more of a challenge as the API is oriented more towards game type functionality, such as sprites, 2D physics and sound. In this post I am not going to talk about Corona but rather Lua, programming language itself.
I have really enjoyed programming in Lua. I only used it for about two months and it took me less than 2 weeks to learn.
Lua is a lightweight, interpreted programming language implemented in ANSI C. It is multi-paradigm, allowing you to write procedural, object-oriented, or prototypal code. It was created in 1993 by Roberto Ierusalimschy. It’s open source and has been released under the MIT license. While Lua is often embedded in larger applications to allow for extension via Lua scripting, it’s not limited to just that. It’s efficiency and extensibility make it possible to write large standalone applications in Lua. Lua is also one of the easiest languages to learn.
In this post I want to highlight the strengths of Lua as well as list the some well known applications that use it.
Where is Lua Used?
Here is a list of a few prominent pieces of software that use Lua for scripting/plugins/extensions:
- Adobe Photoshop Lightroom - for the UI and plugins
- Freeswitch - embedded scripting
- Lego Mindstorms NXT - scripting
- Redis - embedded scripting
- NeoVim - plugins
- Wireshark - plugins
- Both Nginx and Apache have Lua modules.
This isn’t a complete list! Lua seems to appear almost everywhere. Many other enterprise applications have a embedded Lua interpreters for scripting. Lua is also commonly used in game development due to it’s performance and short learning curve. Dozens of games use Lua for plugins and extensions, as well as core game logic.
Strengths of Lua
The first thing you will notice when learning Lua is how simple it is. The syntax is very clean and feels similar to Ruby or Basic. The control structures are simple and there are only a few of them. There are only a handful of data types. The complete list is: nil, boolean, number, string, table, function, userdata and thread. That’s it. There aren’t different types of numbers, they are all just numbers (they are stored as IEEE 754 floats). There aren’t arrays, tuples, and associative arrays, there are only tables. Tables are simple collections of key-value pairs that can be treated as arrays, objects or any other sort of compound data structure. Here is a short example of the different types and how they work (note that comments are started with “–”):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Those are the most commonly used first class values in Lua. There isn’t really anything else. Everything else can be created with some combination of tables, functions and the other data types. Control structures are very simple as well. There are if statements and while, repeat, and for loops. There are also break and return commands. Lua does not have a case statements but haven’t ever found myself needing them. Pretty much everything you would do with a case statement can be done with if/elseif statements or lookup tables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Lua’s simplicity also makes Lua relatively flexible. You can write procedural or object-oriented applications. You can create classical or prototypal objects. Since there are no classes or objects, you might think that implementing any sort of classical object oriented functionality in Lua would be difficult. This is not the case, using Lua’s tables and metatables it is relatively simple to create classes. Lua does tend to lend itself to a more prototypal object model but that doesn’t mean we can’t create our own classes. In order to understand how classes are created we first need to know how metatables work:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Metatables are very powerful and are used for many things. Now that we have seen how they work let’s use them to create a class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
That is pretty much all there is to creating classes. Of course this is a very simple example. Other object-oriented features like inheritance and information hiding can also be implemented with a combination of metatables and closures.
Lua can also be extended easily with C/C++. It has really easy to use C interface and there are many open source libraries for interfacing between Lua and other programming languages.
Lua is one of the fastest interpreted languages out there today. It is often significantly faster Ruby or Python. This is no doubt due in part to Lua’s simplicity. LuaJIT makes it even faster.
Lua runs on any architecture that can execute ANSI C code. That means Lua runs nearly everywhere. Almost any platform you can think of can run Lua. In addition to all the major desktop operating systems, Lua runs on Android, iOS, Kindle, Nook, Xbox, Playstation, Raspberry Pi, Adruino, and more. Since Lua is so portable it is a great choice for any piece of software that needs to run on multiple platforms. This is one of the reasons Lua is often used for cross-platform games.
Lua is a great language with some very unique strengths. It’s hard to beat Lua’s combination of flexibility, simplicity and performance.
Hopefully this has post has gotten you interested in Lua. If you haven’t ever used it before I encourage you to give it a try. If you are an experienced programmer you will likely find a few things annoying (one-based indexes for tables for example). And while you won’t likely end up writing large pieces of software in Lua there is a chance something in your large applications could benefit from a little Lua code (HTTP request preprocessing in nginx or Apache perhaps?). If your new to programming Lua’s simplicity will make it easy to learn the basics and become familiar with the programming constructs that are present in most other languages.