Upload
fwso
View
77
Download
3
Tags:
Embed Size (px)
Citation preview
Lua Express GuideJames Tang<[email protected]>
Contents
1. What is Lua
2. Get started
3. Table: the data structure
4. OOP
5. Coroutine
6. LuaJIT
7. Nginx+Lua
1. What is Lua
• Scripting Language
• Dynamic Programming Language
• Embeddable Language
• Small Language
More Infomation: http://www.lua.org/about.html
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
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
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
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
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.
4. OOP
• Table comes again
5. Coroutine
• Collaborative multithreathing
• Like thread: own stack, local variables, etc
• Not like thread: not parallel, only one routine at a time
6. LuaJIT
• Just-in-time compiler
• FFI library, embed C code in Lua
• http://luajit.org
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