2. Contents
1. What is Lua
2. Get started
3. Table: the data structure
4. OOP
5. Coroutine
6. LuaJIT
7. Nginx+Lua
3. 1. What is Lua
• Scripting Language
• Dynamic Programming Language
• Embeddable Language
• Small Language
More Infomation: http://www.lua.org/about.html
4. 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
5. 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
6. 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
7. 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
8. 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.
10. 5. Coroutine
• Collaborative multithreathing
• Like thread: own stack, local variables, etc
• Not like thread: not parallel, only one routine at a time