lua express guide

12
Lua Express Guide James Tang<[email protected]>

Upload: fwso

Post on 18-Jul-2015

73 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Lua Express Guide

Lua Express GuideJames Tang<[email protected]>

Page 2: Lua Express Guide

Contents

1. What is Lua

2. Get started

3. Table: the data structure

4. OOP

5. Coroutine

6. LuaJIT

7. Nginx+Lua

Page 3: Lua Express Guide

1. What is Lua

• Scripting Language

• Dynamic Programming Language

• Embeddable Language

• Small Language

More Infomation: http://www.lua.org/about.html

Page 4: Lua Express Guide

2. Get started

• Download and build: http://www.lua.org/download.html

• Interactive shell:• $ lua

• > print “Hello, world!”

• Run a script file:• $ lua helloworld.lua

Page 5: Lua Express Guide

3. Table – The data structure

• Table is the core of Lua language

• Table can be used to implement any data structure

• http://lua-users.org/wiki/TablesTutorial

Page 6: Lua Express Guide

3.1. Table: array

• Index starts from 1• t = {1, 3, 5, 7}

• t[5] = 9

• print(t[3])

• print(t[7])

• print(#t)

• print(table.getn(t))

• for i, v in ipairs(t) do• print(i, v)

• end

Page 7: Lua Express Guide

3.2. Table: Map

• m = {name = 'James', ['key key'] = 'OK’}

• m.age = 28

• print(m.name)

• print(m[‘age’])

• for k,v in pairs(m) do• print(k, v)

• end

• print(#m) -- 0

Page 8: Lua Express Guide

3.2. Table: mixed

• m = {4, name = ‘James’, 6, 8}

• print(table.getn(m)) -- 3, not 4

• Tips: use table as a specific role, mixed table should be avoid in practice.

Page 9: Lua Express Guide

4. OOP

• Table comes again

Page 10: Lua Express Guide

5. Coroutine

• Collaborative multithreathing

• Like thread: own stack, local variables, etc

• Not like thread: not parallel, only one routine at a time

Page 11: Lua Express Guide

6. LuaJIT

• Just-in-time compiler

• FFI library, embed C code in Lua

• http://luajit.org

Page 12: Lua Express Guide

7. Nginx+Lua

• Run application inside Nginx

• Lua-nginx-module: https://github.com/fwso/lua-nginx-module

• Module Reference: http://wiki.nginx.org/HttpLuaModule

• OpenResty: http://openresty.org