This page contains the slides of a talk about Lua
given at the
Software Engineering Lab
of the
University of Waterloo
in November 1995.
At that time, the current version of Lua was
2.4
and so these slides are somewhat outdated:
for instance,
fallbacks were replaced by tag methods in
3.0
and by metatables in
5.0,
and the C API changed in
4.0.
Nevertheless,
the main points remain valid.
Lua, an extensible extension language
by
Luiz Henrique de Figueiredo,
Roberto Ierusalimschy,
Waldemar Celes
Abstract.
There is an important trend nowadays to split complex systems in two
parts: kernel (written in a compiled language) and configuration
(written in an interpreted language). Lua is a new language for
configuring and extending applications, Lua combines procedural
features with powerful data description facilities, by using a
simple, yet powerful, mechanism of tables. This mechanism implements
the concepts of records, arrays, and recursive data types (pointers),
and adds some object-oriented facilities, such as methods with
dynamic dispatching.
Lua presents a mechanism of fallbacks that allows programmers to extend
the semantics of the language in some unconventional ways. As a
noteworthy example, fallbacks allow the user to add different kinds of
inheritance to the language.
Currently, Lua is being extensively used in production for several
tasks, including user configuration, general-purpose data-entry,
description of user interfaces, storage of structured graphical
metafiles, and generic attribute configuration for finite element
meshes. Lua is also being used in several research projects, such as
"active" graphical objects, computing with distributed objects, and
transparently extending WWW browsers with client-side code.
function save(i,v)
local t=type(v)
write(i..'=')
if t=='nil' then write('nil')
elseif t=='number' then write(v)
elseif t=='string' then write('"'..v..'"')
elseif t=='table' then write_record(v)
end
end
function write_record(t)
local i,v=next(t,nil)
write('{')
while i do
save(i,v)
i,v=next(t,i)
if i then write(',') end
end
write('}')
end
writeto("state") -- save env to file
i,v=nextvar(nil)
while i do
save(i,v) i,v=nextvar(i)
end
dofile("state") -- restore env from file
function Index(t,f)
if f=="parent" then
return oldIndex(t,f)
end
local p=t.parent
if type(p)=="table" then
return p[f]
else
return oldIndex(t,f)
end
end
oldIndex=setfallback("index", Index)
a=Window{x=100, y=200, color="red"}
b=Window{x=300, y=400, parent=a}
b.color == "red"
familiar syntax for computing with application objects
function Overload(a,b,op)
local i=op.."("..a.name..","..b.name..")"
if T[i]==nil then
n=n+1 T[i]=create("t"..n)
write(T[i].name..'='..i.."\n")
end
return T[i]
end
function create(v)
local t={name=v}
setglobal(v,t)
return t
end
setfallback("arith",Overload)
create("a") create("b") create("c")
n=0 T={}
E=(a*a+b*b)*(a*a-b*b)/(a*a+b*b+c)+(a*(b*b)*c)
t1=mul(a,a) t2=mul(b,b) t3=add(t1,t2)
t4=sub(t1,t2) t5=mul(t3,t4) t6=add(t3,c)
t7=div(t5,t6) t8=mul(a,t2) t9=mul(t8,c)
t10=add(t7,t9)