Lua and the Web Roberto Ierusalimschy PUCRio Brazil
Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil WWW 8 Lua and the Web
What is Lua? 4 Yet Another Scripting Language 4 an “extension” language 4 implemented as a library in ANSI C Host Program WWW 8 Lua Interpreter -- a Lua script color = RED b = button { label = ‘OK’, x = 10, y = 20} Lua and the Web
Why Lua? 4 Simple and flexible 8“Simple things simple, complex things possible” 4 Small 4 Efficient 4 Portable 8 Whole library written in ANSI C, compiles the same source code in all platforms 8 Typical uses: MS-DOS, Windows (3. 1, 95, NT), Unix (Linux, Solaris, IRIX, AIX, ULTRIX), Next, OS/2, Mac WWW 8
What is CGILua? 4 a Web development tool for creating dynamic sites 4 Supports both templates and scripts 8 templates can embed scripts 8 scripts can “call” templates 4 based on Lua for 8 its scripts 8 expanding templates 8 its own configuration WWW 8 Lua and the Web
Why CGILua? 4 Small 8 whole distribution (source+binaries) fits in one floppy disk 4 Simple 8 smooth learning curve 4 Portable 8 runs on Unix and Windows 95 -NT 8 scripts run on any platform without changes 4 Easily extended with dynamic libraries written in Lua and C WWW 8 Lua and the Web
Where is Lua? 4 Te. CGraf 8 Lua’s birthplace 8 partnership between PUC-Rio and Petrobras (the Brazilian Oil Company) 8 dozens of products developed with Lua, since 1994 4 PUC-Rio 8 many academic projects 8 used by hundreds of programmers 8 Intranet developed with CGILua WWW 8 Lua and the Web
Where is Lua? 4 Inside Brazil 8 Petrobras, the Brazilian Oil Company 8 Embratel (the main telecommunication company in Brazil) 8 many other companies 4 Outside Brazil 8 Lua is used in hundreds of projects, both commercial and academic 8 CGILua still in restricted use » until recently all documentation was in Portuguese WWW 8 Lua and the Web
How is Lua? 4 Pascal-like Syntax. function fat (n) if n == 0 then return 1 else return n*fat(n-1) end 4 Interpreter executes sequence of statements. 8 function definitions are also statements (see later) 4 Six types: 8 numbers, tables, functions, strings, userdata, nil WWW 8 Lua and the Web
Tables 4 Implement associative arrays: 8 any value (including functions and other tables) can be used both for indices and values t = {} t[1] = "hello" t. x = print t. x(t[1]) t. next = t WWW 8 -- creates an empty table -- t. x is sugar for t[‘x’] -- prints ‘hello’ -- circular list Lua and the Web
Constructors 4 Expressions to create and initialize tables 4 Record style 8 point={x=10, y=20} 8 print(point. y) --> 20 4 List style 8 days={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"} 8 print(days[3]) --> Tue 4 Mixed style 8 points={{x=0, y=0}, point; n=2} 8 print(points[points. n]. y) --> 20 WWW 8 Lua and the Web
Constructors 4 Data description uses: calls function “article” article{ author="F. P. Brooks", title="The Mythical Man-Month", year=1975, } news = { {text = "New version 2. 0", date = "21/05/1997"}, {text = "New example", date = "21/05/1997"}, {text = "New version: 2. 1", date = "17/06/1997"}, } WWW 8 Lua and the Web
Tables x Objects 4 Tables are dynamically created objects. 8 in the sense of Hoare list = {value=v, next=list} list old list. . . value - v next - WWW 8 Lua and the Web
Functions in Lua 4 First class values function inc (x) return x+1 end sugar inc = function (x) return x+1 end 4 Example: cloning a table t clone = {} foreach(t, function (i, e) clone[i]=e end) WWW 8 Lua and the Web
Upvalues 4 Mechanism to allow functions to access non-local variables 4 An upvalue is a variable expression whose value is computed when the enclosing function is instantiated (and not when the function is executed) function add (x) return function (y) return y+%x end WWW 8 add 1 = add(1) print(add 1(10)) --> 11 upvalue Lua and the Web
Example: Security 4 User scripts need a “secure” Lua environment 4 Security can be set in Lua itself, through redefinition of “dangerous” functions 8 functions are first-class values!! 4 With upvalues, the new function still can use the old, unsecure, version to implement its core functionality 4 Because the old version is kept in the closure of the new version, it is no longer accessible by the script WWW 8 Lua and the Web
Example: Security -- redefines "openfile", to restrict files that -- can be open by a script openfile = function (filename) if is_ok(filename) then %openfile(filename) else error("cannot open ". . filename) end -- at this point, only the new "openfile" is -- visible, but the old function is still -- available inside the new to do the real job WWW 8 Lua and the Web
Objects 4 First class functions + tables = almost OO. 8 tables can have functions as field values ( methods) 4 Syntactic sugar for defining and calling methods 8 handles hidden parameter self function a: foo (x). . . end sugar a. foo = function (self, x). . . end a: foo(x) sugar a. foo(a, x) WWW 8 Lua and the Web
Strings 4 No size limits, good performance for long strings 8 common practice to read a whole text file in a single string before processing 4 Strings can store arbitrary binary data 8 not ‘ ’ terminated 4 Usual pattern matching facilities, implemented through a standard library 4 Pattern substitution can be called with a function to compute the replacement string WWW 8 Lua and the Web
Example: Decoding a URL encoding string 4 This function is used by CGILua to decode a URL encoding string: 8“lua%3 Dis+great” “lua=is great” function unescape (str) str = gsub(str, "+", " ") return gsub(str, "%%(%x%x)", function (x) return strchar(tonumber(x, 16)) end WWW 8 Lua and the Web
Example: Decoding URL data 4 This function collects all pairs <key, value> from a submission into the table cgi: function decode (string) cgi={} gsub(string, "([^&=]*)=([^&=]*)&? ", function (key, value) key=unescape(key) value=unescape(value) cgi[key]=value end) end WWW 8 Lua and the Web
CGILua Pages 4 a page can be created by a script: write("Content-type: text/htmlnn") write("<html>") write("<head><title>Simple page</title></head>") write("<body>") write("<h 1>Today's date is: ", date(), "</h 1>") write("</body>") write("</html>") WWW 8 Lua and the Web
4 or it can be a template: <html> <head><title>Simple page</title></head> <body> <h 1>Today's date is: $|date()|$ </h 1> </body> </html> Lua expression WWW 8 Lua and the Web
Example: echoing 4 the following script can be used for debugging, to show all data posted by a form: write("Content-type: text/htmlnn") write("<html>") write("<head><title>Your data</title></head>") write("<body><h 1>Your data</h 1>") write("<table border=1 width=100%>") foreach(cgi, function(key, value) write("<tr><td>", key, "</td>") write("<td>", value, "</td></tr>") end) write("</table></body></html>") WWW 8 Lua and the Web
Example: echoing 4 The same page can be generated by a template: <html> <head><title>Your data</title></head> <body><h 1>Your data</h 1> <table border=1 width=100%> <!--$$ LOOP start="key, value=next(cgi, nil)", test="key", action="key, value=next(cgi, key)" $$--> <tr><td> $|key|$ </td> <td> $|value|$ </td></tr> <!--$$ ENDLOOP $$--> </table></body></html> WWW 8 Lua and the Web
Template Preprocessing 4 Three kinds of marks: 8 expressions 8 statements 8 control fields: loops and conditionals 4 All marks has a sensible appearance when not preprocessed 8 statements and control fields handled as comments 8 expressions appear literally (place-holder) 4 Templates can be edited like a static page with conventional tools WWW 8 Lua and the Web
Template Example <h 1>Club member list</h 1> Template before preprocessing <table border=1 width=100%> <tr align=center> <td><strong>First Name</strong></td> <td><strong>Last Name</strong></td> </tr> <!--$$ LOOP start=‘i=1’, test=‘i<=field. n’, action=‘i=i+1’ $$-> <tr><td>$| m[i]. firstname |$</td> <td>$| m[i]. lastname |$</td> </tr> <!--$$ ENDLOOP $$--> </table> WWW 8 Lua and the Web
Template Example Template after preprocessing 4 Now, suppose table m defined as m = { {firstname='George', lastname='Harrison'}, {firstname='John', lastname='Lennon'}, {firstname='Paul', lastname='Mc. Cartney'}, {firstname='Ringo', lastname='Starr'} } WWW 8 Lua and the Web
Extensions 4 CGILua can include libraries written both in Lua and in C/C++ 8 e. g. database access, cryptography 4 Libraries can be loaded dynamically 8 no need of recompilations/relinks 4 Loading function can be erased after configuration 8 enhance security WWW 8 Lua and the Web
Conclusions 4 Lua and CGILua are smart choices 8 Small and simple 8 Flexible 8 Portable WWW 8 Lua and the Web
- Slides: 29