it pro: an introduction to lua

14
1 An Introduction to Lua

Upload: qhartman

Post on 18-Nov-2014

127 views

Category:

Documents


0 download

DESCRIPTION

An introduction to Lua discussing how and why one might use it.

TRANSCRIPT

Page 1: IT Pro: An Introduction To Lua

1

An Introduction to Lua

Quentin HartmanEugene IT Pro Forum

May 18, 2010

My name is Quentin Hartman, and I am here to give you an introduction to Lua. I do have a prepared presentation, but I much prefer to present in a �discussion� format, so if you have any questions as we go along, please feel free to ask. I should point out that I am by no means a Lua expert. I've only been usoing for a few weeks within a very specific context so there's a good chance I won't know the answer to your question. That said, I've really enjoyed using it and have thought of a number of projects where it might have application in the future and I fully intend to keep using it and becoming more proficient with it.

Page 2: IT Pro: An Introduction To Lua

2

� What is Lua?

� Features

� Why / Where is it used

now?

Tonight I plan on doing a high level overview of Lua and give some examples of how it currently being used. I am not going to include any code examples since this display doesn't really lend itself to viewing that kind of material well. My goal is to give you enough information about Lua to hopefully spark your interest and that you will be able to recognize when it might a well suited tool for a problem you are currently facing.

Page 3: IT Pro: An Introduction To Lua

3

What is Lua?

� Yet another scripting language

� Developed in Brazil in early 90's

� Reputation for speed

� Very light (~350k including standard lib)

� Easily embedded via C api (#include <lua.h>)

� Simple syntax, data structures

The first question I am usually asked when I talk to someone about Lua is, �Why would I learn another scripting language? Perl/python/shell/whatever serves me just fine.�

The first reason you might want to learn Lua is, like me, you have to in order to get a particular task done. I'll tell you about this project later. The second is that Lua offers a fairly unique combination of features that makes it particularly well suited for being embedded within other applications or devices and for providing a means of extending an application by end-users. If you are working on a project that would benefit from that kind of functionality, embeddig a Lua interpreter and using it to expose the internals of your application has some advantages over creating your own scripting language or writing a more low-level API.

Page 4: IT Pro: An Introduction To Lua

4

Features of Note

� Garbage collection

� Object-orientation via first-class functions

� Dynamic variable allocation / coercion

� Functions can return multiple values

� Tables are the only complex data type

The most interesting feature that I was previously unfamiliar with is the concept of a �first-class function�. In essence a function can be assigned to a variable and then that variable can effectively be treated as an object similar to other explicitly object-oriented languages.

Tables are often abused in Lua to behave as other complex structures used in other programs such as hashes, dictionaries, namespaces, arrays, etc.

Page 5: IT Pro: An Introduction To Lua

5

Features of Note

� Code processed in ”chunks”. All of these are

equivalent:

a = 1

b = a*2

a = 1 ; b = a*2

a = 1 b = a*2 -- ugly, but valid

Another interesting feature is how it parses code. Line endings mean nothing to the interpreter. It processes code in �chunks�, and a chunk is simply a string of statements. Statements can optionally be separated by semicolons, and by convention a semicolon is placed between two statements on the same line, but it is not required.

This allows lua code to be fairly clean, elimating a lot of the syntactical structures that intimidate newcomers.

Page 6: IT Pro: An Introduction To Lua

6

Where is it used now?

� Very popular in games

� World of Warcraft

� Escape from Monkey Island

� Far Cry

� Baldur's Gate

� Many more...

Lua is increasingly popular choice for the scripting language used in games, the most prominent would probably be World of Warcraft.

Page 7: IT Pro: An Introduction To Lua

7

Where is it used now?

� Also popular is general applications

� Adobe Lightroom

� Apache (via mod_lua)

� VLC

� Snort

� Many more...

It is also used in more general purpose applications, perhaps most famously Adobe Lightroom.

Page 8: IT Pro: An Introduction To Lua

8

Why embed a scripting language?

� Allows to separate engine code from game-

logic / interface code.

� Simple, fast, and safe enough for ”non-

programmers” to develop game logic,

automation, and addons.

� Allows for rapid experimentation during

development

It is at this point that I am often asked, �So, why would someone embed a scripting language into a larger application?� The biggest reason is that it allows the separation of tasks. Much like the separation of �content� and �presentation� in web design. Things which are �hard� in a computer science sense can be handled by the most experienced programmers in a low-level language to create the engine of the application. This can then be separated from the UI or business logic components and those tasks can be handled less experienced programmers within a relatively safe sandbox. It is becomming quite common in the gaming industry to have designers handle game logic themselves rather than having them spec the logic and then hand it over to a programmer. This allows much more rapid development cycles. Designers don't have to wait on programmers, and they do not have to wait on a recompile of the entire codebase to try out some new game logic. It also frees the dedicated programmers to focus on more low-level aspects of the game. This ultimately allows everyone involved to focus more on their particular area of expertise and each individual is less of a potential bottleneck for the overall process.

By creating the application from this perspective, it also makes it relatively easy to allow end-users to create plugins and addons. Not only is the majority of the infrastructure for a plugin system already in place, it utilizes a language that is easy to learn and high-level enough that technically inclined users can become developers relatively easily, adding value to the application.

Page 9: IT Pro: An Introduction To Lua

9

Example: World of Warcraft

� WoW uses Lua for addon development

� API hooks into virtually all games-state information

� Many add-ons for tracking economy, ”threat level”

� Automation for crafting

� Mini-games to kill time during travel.

WoW has a huge collection of addons that have been created for it. Hundreds (perhaps thousands) of them are available through various sites online, and they have added a lot of value to the WoW ecosystem. The most complete guide to writing WoW addons, �World of Warcraft Programming� released a second edition in January of this year. Most of the addons boil down to automation of one kind or another, or the creation of custom �dashboards� so people can stay on top the game state information they are most interested in easily. There are also a number of minigames that have been created through the addon system. Popcap games has even created an official port of Bejeweled as a WoW minigame addon. Apparently playing WoW involves a lot of waiting around...

Page 10: IT Pro: An Introduction To Lua

10

Example: Adobe Lightroom

� Only image manipulation engines are in C

� OS-specific ”substrate” in C# / Objective C

� Everything else (~63% of the code) is Lua,

including UI, plugins, etc.

A much more �serious� example is Adobe Lightroom. According to a presentation given by Troy Gaul from Adobe, there is an OS-specific �substrate� and image processing libraries written in a C derivative (mostly C++ and Objective C), and the remaining code, about 63%, is written in Lua. This code handles all of the UI and drawing elements as well as the �business logic� it uses internally. Again we see a thriving plugin community hat has grown up around the application, thanks in large part to the accessability that Lua brings with it.

Page 11: IT Pro: An Introduction To Lua

11

Lua in Devices

� Attributes that make it attractive to apps also make

it attractive in hardware devices

� Allows to abstract ”hard” hardware-specific

functionality in a way that is easier for end-users

to grasp.

A lot of the same attributes that make Lua attactive to application designers also make it attractive to hardware device manufacturers. It is small, fast, easy to embed, and easy to understand. This allows manufacturers to add signifcant value to their devices by allowing custom capabilities to be developed by their customers so that their device will exactly meet their needs.

Page 12: IT Pro: An Introduction To Lua

12

Where is it used now?

� Many different devices

� Asentria Siteboss 550

� Cisco ASA (Dynamic Access Policies)

� Logitech Squeezebox products

� Creative Xen X-fi2

� Many more...

Perhaps the most prominent devices including Lua are Cisco's ASA line of firewalls. They only use Lua for enabling their dynamic access policies right now, but there are rumors that it will be extended into other areas in the future. Logitech and Creative both use Lua for creating custom applications in their consumer electronics devices such as the Squeezebox and Xen X-fi2. Logitech also uses it to create small applets for it's G-15 gaming keyboard, which has a small LCD display built into it.

The device I am most familiar with though is the Asentria Siteboss 550, a data center monitoring and automation appliance.

Page 13: IT Pro: An Introduction To Lua

13

Example: Asentria Siteboss

� Create virtual ”sensors” from arbitrary serial inputs

� Simplifies communication via modbus

� Allow indirect monitoring of sensors to only raise

alarms if specific combinations of events happen.

(Describe siteboss)This application space makes up the vast majority of

my experience with Lua. I am using Lua to query an energy meter via a Modbus connection and then store the returned data. The API that Asentria has created for the Siteboss abstracts away much of the complexity of Modbus queries and data storage on the device so that in the end, I don't need to learn the nitty-gritty details, and I am able to get results much more quickly than if I had to do this integration from the ground up. I also am able to solve the problem of spurious alarms using Lua. Rather than having an alarm created for every contact that enters it's �bad� state, I can monitor the contacts with a Lua script, and selectively send out alarms only if a particular combination of contacts are tripped, ultimately improving the signal to noise ratio on the alarming system.

Page 14: IT Pro: An Introduction To Lua

14

Further reading

� Programming in Lua http://www.lua.org/pil/index.html#P1

� WoW Programming http://wowprogramming.com/docs

� Lua in Lightroom

http://www.sauria.com/blog/2008/10/09/lua-in-lightroom/

� Wikipedia Article (includes lists of apps that use Lua)

http://en.wikipedia.org/wiki/Lua_(programming_language)