Script up your application with Lua! -- RyanE -- OpenWest 2014
Script up your application
with Lua!
OpenWest 2014
Ryan Erickson
http://www.untestedhacks.com
History of Lua
• Lua.org
• Created in 1993 at PUC-RIO
• Predecessors: DEL and Sol
• Lua == Moon, not an acronym.
• Focus:
– Simplicity for non-programmer audience
– Portability
– Designed to be easily embedded, extended
– Clean ANSI C code
– Garbage Collected
Why: Size
• To embed, hook to a few functions.
• Small as < 100kb DLL/LIB
• Runs on mobile / embedded devices and
platforms
Why: Performance
• Interpreted Lua is generally faster than
Python, Ruby, Perl, and PHP
• Not fast enough? LuaJIT!
• LuaJIT is in C++ / Java 6 territory.
– luajit.org
Why: Momentum
• Used extensively in games
• Adobe Lightroom (60% lua) and Photoshop
• First interpreted language allowed on iOS
• Angry Birds
• World of Warcraft
• Wikipedia lists over 100 games using Lua
• Nginx / OpenResty
• Control4 (us!)
• Many, Many more
Why: Safety
• Lua code runs in a sandbox.
• Embedder chooses which modules to expose.
• Host application can provide APIs / primitives
to Lua engine.
Control4’s use case
• Existing (C++) Driver architecture
• XML Driver ‘document’ + compiled driver
• Considered Python, JavaScript, and Lua
• Embedded Lua engine into C++ driver
• Embedded Lua code into the XML driver
– CDATA (non-XML) section read by driver on
startup
Intro: General
• Dynamically typed
• Whitespace is not significant.
– Spaces, linebreaks, tabs – use what you like
• Semicolons not required, discouraged
• -- single-line comment
• Variables are global by default, ‘local’ keyword
Intro: Types
• Few types:
number, string, boolean, nil, table, function, u
serdata
• Numbers are double by default
• Can represent Floats *and* Integers
• No i++, no i+=2. i = i + 1
Intro: Types
• Strings:
– Single-quote / Double-quote / backslash to escape
– String concatenation uses .. not +
• nil:
– Empty value
– Evaluates to ‘false’
– Frees item for garbage collection
Intro: Tables
• Lua Tables – Simultaneous array and hashmap
• a = ,“apple”, “banana”, “orange”-
• b = {lua = “cool”, java = “sucks”, fred = 3}
• c = a
• print(c[3], b.fred, b.*“java”+, a.grape)
• orange 3 sucks nil
Intro: Functions
• Functions are first-class objects
function add(a, b)
print(a + b)
end
multiply = function(a, b) print(a * b) end
plus = add
plus(3, 5)
-> 8
Control Structures
if … then … elseif .. else .. End
do -- block definition…
print(“one”)
print(“two”)
end
for i = 1, 10 do print(i) end
for k,v in pairs(,“apple”, “banana”-) do print(k,v) end
‘C Programmer’ Hangups
• No curly braces… What’s this ‘begin…end’
business?
• No +=, ++
• Not Equals is not !=, it’s ~=.
• Not is not ~, it’s not!
• Arrays start at 1
• #array is not always ‘right’
• Get past the hangups, and Lua is a fantastic little
language!
C Interface API
• Set of functions that allow C to interact with
Lua
– Functions to read and write Lua global variables
– Functions to call Lua functions
– Functions to register C functions within Lua
• Stack-based parameter passing
Demo
• C IRC Bot (~100 lines of C code), featureless
• Lua 5.1 source, previously compiled
• ~50 lines of Lua Integration C code
• 9k executable => ~200k with Lua + included libs
• Fast turnaround, feature development, etc.