Lua Seminar slides

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.

Contents

Customization

Configuration languages

Embedded languages

Requirements for extension languages

TeCGraf

Need for extension languages at TeCGraf

Overview of Lua

Interface Host x Lua

Data types and variables

Associative arrays

Associative arrays

Reflexive facilities

Persistence

	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

Support for object oriented programming

Fallbacks

Single inheritance with fallbacks

	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"

Overloading with fallbacks

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)
global common sub-expression identification!

Implementation

Comparison with other extension languages

Comparison with other extension languages

Conclusion

API

lua_Object     lua_setfallback          (char *name, lua_CFunction fallback);
void           lua_error                (char *s);
int            lua_dofile               (char *filename);
int            lua_dostring             (char *string);
int            lua_callfunction         (lua_Object function);
int            lua_call                 (char *funcname);
void           lua_beginblock           (void);
void           lua_endblock             (void);
lua_Object     lua_getparam             (int number);
#define        lua_getresult(_)         lua_getparam(_)
float          lua_getnumber            (lua_Object object);
char          *lua_getstring            (lua_Object object);
lua_CFunction  lua_getcfunction         (lua_Object object);
void          *lua_getuserdata          (lua_Object object);
void           lua_pushnil              (void);
void           lua_pushnumber           (float n);
void           lua_pushstring           (char *s);
void           lua_pushliteral          (char *s);
void           lua_pushcfunction        (lua_CFunction fn);
void           lua_pushusertag          (void *u, int tag);
void           lua_pushobject           (lua_Object object);
lua_Object     lua_getglobal            (char *name);
void           lua_storeglobal          (char *name);
void           lua_storesubscript       (void);
lua_Object     lua_getsubscript         (void);
int            lua_type                 (lua_Object object);
int            lua_lock                 (void);
lua_Object     lua_getlocked            (int ref);
void           lua_pushlocked           (int ref);
void           lua_unlock               (int ref);
lua_Object     lua_createtable          (void);