15. • Easy to write and understand (so no Lisp,
TCL or Scheme), Python wasn’t good
enough yet
• Diverse collection of computers (so highly
portable, not -nix only)
• Should be provided as a library with a C-
API
39. Types
print(type("Ponies")) --> string
print(type(10.4*3)) --> number
print(type(print)) --> function
print(type(type)) --> function
print(type(true)) --> boolean
print(type(nil)) --> nil
print(type(type(X))) --> string
40. Types
print(type(a)) --> nil
a = 10
print(type(a)) --> number
a = "Ponies!!"
print(type(a)) --> string
a = print -- yes, this is valid!
a(type(a)) --> function
41. More
• ~= == !=
• ‘local’ for local variables (garbage
collection)
• bytecode execution on Lua VM (.luac)
44. Functions
Functions are anonymous, and are pointers
to variables
a = {p = print} -- create a table
a.p("Ponies!") --> Ponies!
print = math.sin -- `print' now refers to the 'sin' function
a.p(print(1)) --> 0.841470
sin = a.p -- `sin' now refers to the print function
sin(10, 20) --> 10 20
45. Tables
• Can be used to define anything you want
• Classes, objects, namespaces, you name it
• The only complex data structure available
46. Tables
As array
a = {} -- create a table and reference it in 'a'
k = "x"
a[k] = 10 -- new entry, with key="x" and value=10
a[20] = "Ponies!!1" -- new entry, key=20 and value="Ponies!!1"
print(a["x"]) --> 10
k = 20
print(a[k]) --> "Ponies!!1"
a["x"] = a["x"] + 1 -- increments entry "x"
print(a["x"]) --> 11
print(a.x) --> 11
47. Tables
As structures (objects)
point = { x = 10, y = 20 } -- Create new table
print(point["x"]) -- Prints 10
print(point.x) -- Prints 10
48. Tables
As namespace
Point = {}
Point.new = function (x, y)
return {x = x, y = y}
end
Point.set_x = function (point, x)
point.x = x
end
49. MetaTables!
• The power to override events on tables, built into
the interpreter
• Useful for namespacing, classes, inheritance, etc.
50. MetaTables!
t1 = {a = "Ponies!"} -- t1 is a table with one name-value pair.
t2 = {} -- t2 is an empty table.
setmetatable(t1, t2) -- t2 is t1's metatable.
t3 = {c = "Rainbows!"} -- t3 is just another table like t1.
t2.__index = t3 -- when a lookup fails in t1, t2 will
-- look for a value in t3.
print(t1.a) -- Ponies!
print(t1.b) -- nil
print(t1.c) -- Rainbows!
51. MetaTables!
Another example
t1 = {a = "Ponies!"} -- t1 is a table with one name-value pair.
t2 = {} -- t2 is an empty table.
setmetatable(t1, t2) -- t2 is t1's metatable.
t3 = {c = "Rainbows!"} -- t3 is just another table like t1.
t2.__index = function (t, k)
return k -- When no value is found, return the key
end
print(t1.a) -- Ponies!
print(t1.b) -- b
print(t1.c) -- c
53. Python example
PyLux
import pylux
# Shorthand to run a Lua script with "lux('script')"
def lux(script):
pylux.eval("gv", "dostring", script)
# Shorthand to define a Python proc as callback for Lua
def luxcb(name,func):
pylux.eval("gvc", "setglobal", name, func)
luxGlobals=[]
luxcb("luxAppend", luxGlobals.append)
lux("""
for i,v in globals() do
luxAppend(i)
end
""")
luxGlobals.sort()
print luxGlobals
54. Ruby example
rubyluabridge et all
require 'rubyluabridge'
l = Lua::State.new # Create a new state for the bridge
# (this is your 'sandbox')
l.eval("return 1") # 1
l.n = 5
l.eval("return n") # 5
l.eval("s='Ponies!'")
print l.s # Ponies!
56. Background
• Medianetwork
• max 10gbit/s
• Millions of request/day (10 million+)
• Only binary data
• Custom solution built in CProxy (old)
• Needed new feature set
• API with several subsystems
57. Background
• Needed easy maintenance
• Scalable, lightweight, fast
• Future proof
• Features!
• No exposure of backend storage
63. LuaSocket
-- include libraries
socket = require("socket");
-- create a TCP socket and bind it to the local host at the given port
server = assert(socket.bind('0.0.0.0',50000))
-- loop forever waiting for clients
while 1 do
-- wait for a connection from any client
local client = server:accept()
-- make sure we don't block waiting for this client's line
client:settimeout(10)
-- receive the line
local line, err = client:receive()
-- if there was no error, send it back to the client
if not err then
-- send the same response as was send
client:send(line);
-- close the connection
client:close()
end
end
64. LTN12
Sinks, Pumps, Filters, Chains, etc.
-- load the ltn12 module
local ltn12 = require("ltn12")
-- copy a file
ltn12.pump.all(
ltn12.source.file(io.open("original.png")),
ltn12.sink.file(io.open("copy.png"))
)
65. LTN12
Sinks, Pumps, Filters, Chains, etc.
local outputSink = socket.sink("close-when-done", client)
local r, c, h = socket.http.request{
sink = outputSink,
method = 'GET',
url = storageLocation,
redirect = true
}