+ All Categories
Home > Documents > WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Date post: 18-Dec-2015
Category:
View: 221 times
Download: 3 times
Share this document with a friend
29
Lua and the Web WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil
Transcript
Page 1: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Lua and the Web

Roberto Ierusalimschy

PUC-Rio, Brazil

Page 2: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

What is Lua?

Yet Another Scripting Language

an “extension” language

implemented as a library in ANSI C

HostProgram

LuaInterpreter

-- a Lua scriptcolor = REDb = button { label = ‘OK’, x = 10, y = 20}

Page 3: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Why Lua?

Simple and flexible “Simple things simple, complex things possible”

Small

Efficient

Portable Whole library written in ANSI C, compiles the same source code

in all platforms Typical uses: MS-DOS, Windows (3.1, 95, NT), Unix (Linux,

Solaris, IRIX, AIX, ULTRIX), Next, OS/2, Mac

Page 4: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

What is CGILua?

a Web development tool for creating dynamic sites

Supports both templates and scripts templates can embed scripts scripts can “call” templates

based on Lua for its scripts expanding templates its own configuration

Page 5: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Why CGILua?

Small whole distribution (source+binaries) fits in one floppy disk

Simple smooth learning curve

Portable runs on Unix and Windows 95-NT scripts run on any platform without changes

Easily extended with dynamic libraries written in Lua and C

Page 6: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Where is Lua?

TeCGraf Lua’s birthplace partnership between PUC-Rio and Petrobras (the

Brazilian Oil Company) dozens of products developed with Lua, since

1994

PUC-Rio many academic projects used by hundreds of programmers Intranet developed with CGILua

Page 7: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Where is Lua?

Inside Brazil Petrobras, the Brazilian Oil Company Embratel (the main telecommunication company in Brazil) many other companies

Outside Brazil Lua is used in hundreds of projects, both commercial and

academic CGILua still in restricted use

» until recently all documentation was in Portuguese

Page 8: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

How is Lua?

Pascal-like Syntax.

Interpreter executes sequence of statements. function definitions are also statements (see later)

Six types: numbers, tables, functions, strings, userdata, nil

function fat (n) if n == 0 then return 1 else return n*fat(n-1) endend

Page 9: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Tables

Implement associative arrays: any value (including functions and other tables) can be used

both for indices and values

t = {} -- creates an empty tablet[1] = "hello"t.x = print -- t.x is sugar for t[‘x’]t.x(t[1]) -- prints ‘hello’t.next = t -- circular list

Page 10: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Constructors Expressions to create and initialize tables

Record style point={x=10,y=20} print(point.y) --> 20

List style days={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"} print(days[3]) --> Tue

Mixed style points={{x=0,y=0}, point; n=2} print(points[points.n].y) --> 20

Page 11: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Constructors

Data description uses:

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"},}

calls function“article”

Page 12: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Tables x Objects

Tables are dynamically created objects. in the sense of Hoare

list

value - vnext -

old list...

list = {value=v, next=list}

Page 13: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Functions in Lua

First class values

function inc (x) return x+1end

inc = function (x) return x+1 end

sugar

clone = {}foreach(t, function (i,e) clone[i]=e end)

Example: cloning a table t

Page 14: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Upvalues

Mechanism to allow functions to access non-local variables

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 endend

add1 = add(1)print(add1(10)) --> 11

upvalue

Page 15: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Example: Security

User scripts need a “secure” Lua environment

Security can be set in Lua itself, through redefinition of “dangerous” functions functions are first-class values!!

With upvalues, the new function still can use the old, unsecure, version to implement its core functionality

Because the old version is kept in the closure of the new version, it is no longer accessible by the script

Page 16: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Example: Security

-- redefines "openfile", to restrict files that-- can be open by a scriptopenfile = function (filename) if is_ok(filename) then %openfile(filename) else error("cannot open "..filename) end 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

Page 17: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Objects

First class functions + tables = almost OO. tables can have functions as field values (methods)

Syntactic sugar for defining and calling methods handles hidden parameter self

a.foo(a,x)a:foo(x)

a.foo = function (self,x) ...end

function a:foo (x) ...end

sugar

sugar

Page 18: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Strings

No size limits, good performance for long strings common practice to read a whole text file in a single string

before processing

Strings can store arbitrary binary data not ‘\0’ terminated

Usual pattern matching facilities, implemented through a standard library

Pattern substitution can be called with a function to compute the replacement string

Page 19: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Example: Decoding a URL encoding string

This function is used by CGILua to decode a URL encoding string: “lua%3Dis+great” “lua=is great”

function unescape (str) str = gsub(str, "+", " ") return gsub(str, "%%(%x%x)", function (x) return strchar(tonumber(x, 16)) end)end

Page 20: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Example: Decoding URL data

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

Page 21: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

write("Content-type: text/html\n\n")write("<html>")write("<head><title>Simple page</title></head>")write("<body>")write("<h1>Today's date is: ", date(), "</h1>")write("</body>")write("</html>")

CGILua Pages

a page can be created by a script:

Page 22: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

<html><head><title>Simple page</title></head><body><h1>Today's date is: $|date()|$ </h1></body></html>

or it can be a template:

Luaexpression

Page 23: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Example: echoing

the following script can be used for debugging, to show all data posted by a form:

write("Content-type: text/html\n\n")write("<html>")write("<head><title>Your data</title></head>")write("<body><h1>Your data</h1>") 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>")

Page 24: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Example: echoing

The same page can be generated by a template:

<html><head><title>Your data</title></head><body><h1>Your data</h1><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>

Page 25: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Template Preprocessing

Three kinds of marks: expressions statements control fields: loops and conditionals

All marks has a sensible appearance when not preprocessed statements and control fields handled as comments expressions appear literally (place-holder)

Templates can be edited like a static page with conventional tools

Page 26: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

<h1>Club member list</h1><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>

Template Example

Template before preprocessing

Page 27: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Template Example

Now, suppose table m defined as

m = { {firstname='George', lastname='Harrison'}, {firstname='John', lastname='Lennon'}, {firstname='Paul', lastname='McCartney'}, {firstname='Ringo', lastname='Starr'} }

Template after preprocessing

Page 28: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Extensions

CGILua can include libraries written both in Lua and in C/C++ e.g. database access, cryptography

Libraries can be loaded dynamically no need of recompilations/relinks

Loading function can be erased after configuration enhance security

Page 29: WWW8 Lua and the Web Roberto Ierusalimschy PUC-Rio, Brazil.

Lua and the WebWWW8

Conclusions

Lua and CGILua are smart choices

Small and simple

Flexible

Portable


Recommended