Advertisement
Advertisement

More Related Content

Advertisement

Script up your application with Lua! -- RyanE -- OpenWest 2014

  1. Script up your application with Lua! OpenWest 2014 Ryan Erickson http://www.untestedhacks.com
  2. 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
  3. Why: Size • To embed, hook to a few functions. • Small as < 100kb DLL/LIB • Runs on mobile / embedded devices and platforms
  4. 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
  5. 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
  6. Why: Safety • Lua code runs in a sandbox. • Embedder chooses which modules to expose. • Host application can provide APIs / primitives to Lua engine.
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. Control Structures Cont’d i = 0 while i < 10 do print(i) i = i + 1 End repeat i = i - 1 print(i) until i < 0
  15. ‘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!
  16. 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
  17. 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.
  18. Questions? Ryan Erickson ryan@untestedhacks.com • Resources: – http://lua.org – http://lua.org/pil – http://lua-users.org/wiki/LuaShortReference
Advertisement