2018 02 14 Lua
2018 02 14 Lua
Language
Davis Claiborne
NCSU LUG
1
What is Lua?
2
Where is Lua used?
Lua can be found embedded in many different areas:
• Web
• MediaWiki templates [1]
• Internet servers such as Apache [2] and NGINX [3]
• Moonshine is a Lua VM for browsers [4]
• Software
• VLC for custom scripting [5]
• LuaTeX is an extended version of TeX [6]
• Network diagnostic tools, including Nmap [7] and Wireshark [8]
3
How Fast is Lua?
4
What Does Lua’s Syntax Look Like?
--[[
Blocks comments are done with two square brackets, with
an optional number of `=' in between, allowing for
nesting of block comments.
]]
5
--[=[ There are 5 main types in Lua:
* boolean
* number
* string
* function
* table
(Lua actually has 8 types; I'm ignoring the rest for now)
]=]
6
How Embeddable is Lua?
• C
• C++
• Java
• Fortran
• Ada
• ...
Lua is a good choice for many applications due to its small size,
speed, small memory footprint, etc. [17]
7
How Portable is Lua?
Lua is written entirely in ANSI C [19]
“Unlike several other scripting languages, Lua does not use POSIX
regular expressions (regexp) for pattern matching. The main
reason for this is size: A typical implementation of POSIX regexp
takes more than 4,000 lines of code. This is bigger than all Lua
standard libraries together.”
-- Non-async code
function foo()
print( "first" )
function bar()
print( "second" )
print( "fourth" )
end
Is there any way to get these functions to pause and resume easily?
9
Coroutines create separate threads for each function, allowing for
easy and intuitive async events
function foo()
print( "first" )
coroutine.yield() -- Suspends thread until resumed
print( "third" )
end
function bar()
print( "second" )
coroutine.yield()
print( "fourth" )
end
10
Notable Aspects of Lua: Global by Default
function foo()
local bar = 'this is local'
baz = 'this is global'
foo()
print( bar ) -- "nil"
print( baz ) -- "this is global"
1
This can be protected against; implementation will follow here
2
This is considered by most to be one of the major flaws of Lua
11
Notable Aspects of Lua: Tables
my_table = {
string = 'asdf', -- Named keys
1, -- Non-named keys are automatically integers
3,
5,
}
12
Notable Aspects of Lua: Tables
Almost anything in Lua can act as a table key, even other tables
function foo()
end
other_table = {
[foo] = "function foo",
["1"] = "string 1", -- Different than numeric 1
foo, -- Integer that references a function
[true] = "true value",
[my_table] = "my_table is the key",
}
13
Notable Aspects of Lua: Tables
cyclic1 = {}
cyclic2 = {}
cyclic1[1] = cyclic2
cyclic2[1] = cyclic1
14
Notable Aspects of Lua: Global Variable Table
globalVariable = 'adsf'
print( _G.globalVariable ) -- 'asdf'
This table contains not only all global variables, but also all
base-library functions, such as print.
15
Notable Aspects of Lua: Metamethods
Live demo
16
point = {}
function point.new( x, y )
return setmetatable( { x = x or 0, y = y or 0 }, point )
end
17
pointA = point()
pointB = point( 3, 3 )
setmetatable( _G, {
-- Called every time a new key is added to a table
__newindex = function( tab, key, value )
assert( declaredGlobals[key],
"Error: value not declared"
)
-- Directly set the value
-- (assigning would cause infinite loop)
rawset( tab, key, value )
end,
} )
Live demo
20
-- Improper tail call, as it's multiplying; not "just" tail call
function factorial( n )
if n == 0 then
return 1
else
return n * fact( n - 1 )
end
end
if n == 0 then
return prod
else
return factorial( n - 1, n * prod )
end
end
for i = 1, #family do
print( family[i] )
end
-- "mom", "son", "sister", "father"
22
Notable Aspects of Lua: Closures and Lexical
Scoping
A closure is a type of function with full access to its calling
environment. This environment is called its lexical scope.
function sortNames( names )
table.sort( names, function( string1, string2 )
return #string1 < #string2 -- (# is "length of")
end )
end
for i = 1, #family do
print( family[i] )
end
-- "mom", "son", "father", "sister"
c1 = newCounter()
print( c1() ) -- "1"
print( c1() ) -- "2"
c2 = newCounter()
print( c2() ) -- "1"
print( c1() ) -- "3"
24
Implementations and Tools for Lua
Standard Lua: Currently in version 5.3 [12] ; first widespread use of
register-based virtual machine [18]
26
Moonscript
Moonscript features a number of differences from Lua [25]
Differences:
• Variables local by default
• Significant whitespace
• Built-in OOP
Moonscript:
-- Moonscript features implicit returns
sum = (x, y) -> print "sum", x + y
Equivalent Lua:
function sum( x, y )
print( "sum" )
return x + y
end
27
This code in Moonscript
-- Moonscript
evens = [i for i=1,100 when i % 2 == 0]
-- Lua
local evens
do
local _accum_0 = { }
local _len_0 = 1
for i = 1, 100 do
if i % 2 == 0 then
_accum_0[_len_0] = i
_len_0 = _len_0 + 1
end
end
evens = _accum_0
end
28
SciLua
Seeks to bridge the gap between the use of high-performance
languages and scripting languages in the scientific community
29
Julia
Example: [29]
# "Map" function.
# Takes a string. Returns a Dict with the number of times each
# word appears in that string.
function wordcount(t)
words=split(t,[' ','\n','\t','-','.',',',':',';'];keep=false)
counts=Dict()
for w = words
counts[w]=get(counts,w,0)+1
end
return counts
end
30
References I
[1] Why ”Lua” is on everybody’s lips, and when to expect MediaWiki 1.19
https://en.wikipedia.org/wiki/Wikipedia:Wikipedia_Signpost/2012-01-30/Technology_report
31
References II
32
The End
33