Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

# Lua: an Extensible Embedded Language

#### Example 1: The table definitions in (a) are equivalent to (b).

```(a)   t = {}  -- empty table t[1] = i t[2] = i*2 t[3] = i*3 t[4] = i+j
(b)   t = {i, i*2, i*3, i+j} s = {a=x, b=y}```

#### Example 2: The method definition in (a) is equivalent to (b).

```(a)   function object:method(params) ... end
(b)   function object.method(self, params) ... end```

#### Example 3: (a) Creating and storing a button; (b) explicitly mapping a window onto the screen; (c) showing a message with two bottom buttons.

```(a)   b  = button{label = "Hello world!" command =
"exit(0)" }

(b)   w = toplevel{b} w:show()

(c)   b1 = button{label="Yes", command="yes=1"} b2 =
button{label="No", command="yes=0"} w  =
toplevel{message{text="Overwrite file?"}, frame{b1, b2;
side="left"}; side="top" }```

#### Table 1: Fallback conditions.

String Condition
"arith" Arithmetic on invalid operands.
"order" Order comparison on invalid operands.
"concat" String concatenation on invalid operands.
"getglobal" Reading the value of a global variable that has not been defined.
"index" Retrieving the value of an index not present in a table.
"gettable" Reading the value of an index in a nontable value.
"settable" Writing the value of an index in a nontable value.
"function" Calling a nonfunction value.
"gc" Called during garbage collection for each table being collected.
"error" Called when a fatal error occurs.

### Listing One

```#include <stdio.h> #include "lua.h"

int main() { char line[BUFSIZ]; while (fgets(line,sizeof(line),stdin)!=0)
lua_dostring(line); return 0; }

```

### Listing Two

```function map(list, func) local newlist = {} local i = 1 while list[i] do
newlist[i] = func(list[i]) i = i+1 end return newlist end
```

### Listing Three

```list = {} i = 4 while i >= 1 do list = {head=i,tail=list} i = i-1 end

```

### Listing Four

```S = Separator{ drawStyle = DrawStyle{style = FILLED}, material =  Material{
ambientColor  = {0.377, 0.377, 0.377}, diffuseColor  = {0.800, 0.771, 0.093},
emissiveColor = {0.102, 0.102, 0.102}, specularColor = {0.0, 0.0, 0.0} },

transform = Transform{ translation = {64.293, 20.206, 0.0}, rotation    = {0.0,
0.0, 0.0, 0.0} }, shape = Sphere{radius = 10.0} }
```

### Listing Five

```function Inherit(t,f) if f == "parent" then  -- avoid loops return nil
end local p = t.parent if type(p) == "table" then return p[f] else
return nil end end

setfallback("index", Inherit)
```

### Listing Six

```TNumber="number" TPoint={x=TNumber, y=TNumber} TColor={red=TNumber,
blue=TNumber, green=TNumber} TRectangle={topleft=TPoint, botright=TPoint}
TWindow={title="string", bounds=TRectangle, color=TColor}
```

### Listing Seven

```function checkType(d, t) if type(t) == "string" then -- t is the name
of a type return (type(d) == t) else -- t is a table, so d must also be a table
if type(d) ~= "table" then return nil else -- d is also a table; check
its fields local i,v = next(t,nil) while i do if not checkType(d[i],v) then
return nil end i,v = next(t,i) end end end return 1 end
```

### Listing Eight

```function save() -- create table to hold environment local env = {} -- get first
global var and its value local n, v = nextvar(nil) while n do -- save global
variable in table env[n] = v -- get next global var and its value n, v =
nextvar(n) end return env end
```

### Listing Nine

```function runProtected(code) -- save current environment local oldenv = save() --
erase "dangerous" functions readfrom,writeto,execute = nil,nil,nil --
run untrusted code dostring(code) -- restore original environment restore(oldenv)
end
```

### Listing Ten

```widgetClass = {} function widgetClass:focus() if self.tkname then
tklua_setFocus(self.tkname) end end buttonClass = { parent = widgetClass,
tkwidget = "button" }
```

### Listing Eleven

```function button(self) self.parent = classButton tklua_ID = tklua_ID + 1
tklua_IDtable[tklua_ID] = self return tklua_ID end
```

### Listing Twelve

```function setFB(id, f, v) local h = tklua_IDtable[id] if h == nil then return
old_setFB(id,f,v) end if h.tkname and h:isAttrib(f) then
tklua_configure(h.tkname,f,v) end h[f] = v end old_setFB =
setfallback("settable",setFB)
```

### More Insights

 To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.