Lua

An old truth in software development is to use tools of the proper level of abstraction for the task at hand. But all too often we find ourselves reaching for C or C++ even when absolute execution speed isn’t critically important. Developers end up dealing with issues like type conversion, memory management and implementing basic data structures without even knowing what benefit in speed the extra work brings.

I got the opportunity to look into the scripting language Lua for a client and I’m thrilled to see the great things this language offers. It’s a compact and flexible language that adds easy control over program flow and data structures. Calls and data sharing between C and Lua are fairly straight-forward. The platform-specific parts, heavy calculations and big data can stay in C or C++ to preserve performance while other components can enjoy replaceable code, garbage collection etc.

Lua is by no means an unknown language — it’s used as part in a great number of games and applications. For example, the photography suite Adobe Lightroom uses Lua to define the GUI, totalling a full 40% of the source code (source). Hit games World of Warcraft and Grand Theft Auto 4 use Lua. Although the standard distribution is very basic there are many plugins, for example Love 2D for creating games.

Maybe the biggest argument for a scripting language is the ability for third parties to change and adapt the program behavior without compiling code, creating native libraries or risking to compromise the system security. Both Lightroom and World of Warcraft use Lua for this.

Python is another scripting language that’s very popular for its ease of development and availability of libraries for almost any purpose. Indeed, I often use it myself and the whole Windsond PC application is written in Python. But binary size and memory footprint count in embedded systems and the lower demands of Lua make a big difference here. All Lua language features compiles to 182 KB with additional 244 KB needed for the standard libraries. When needed, Lua can allegedly be reduced to 100 KB with libraries. Standard Python is at least 1000 KB. Python-on-a-chip reimplements parts of Python to 55 KB program memory (flash) to potentially be a contender.

Lua also offers an alternative for for microcontroller projects, eLua. It adds direct access to many hardware features. It has support for some Cortex-M3 and AVR32 MCUs. The documentation is a bit fuzzy on the hardware requirements, but seem to be in the range of 100s of KB flash and down to 10 KB RAM.

Bookmark the permalink.

Comments are closed.