Parser Reentrant
• Lua can be freely called while parsing a chunk
• New function load
• Opens the door for Macro-processing
New Syntax for Long Strings
• [==[…]==]
• Also valid for long comments
• –[=[…]=]
• Allows insertion of anyliteral string
• does not need to end with newline
• Requirements:
• variable delimiter
• clear border around delimiter (e.g.,[[[…]]] does not work)
• Old [[…]]as a special case
New Syntax for Long Strings (2)
• No more nesting
• string ends with a fix mark
• simpler description (and implementation) string.find(s, “%[(=*)%[.-]%1]”)
Coroutine Debug
• Debug library works on any coroutine:
• On error, coroutines do not unwind the stack
• can be inspected later
print(debug.traceback(co))
ok = coroutine.resume(co)
if not ok then
print(debug.traceback(co))
end
New Mod Operator
• Why Lua did not have it?
• probably we forgot it 🙂
• Several uses
• helps with bitwise operations
New Mod Operator (2)
• Main rule: a = (a div b)b+ a%b
• But a divbhas several possible meanings
• floor(a/b), ceil(a/b), round(a/b), trunc(a/b)
• Which is best?
• floorhas some nice properties
• a =b mod c iffa%c =b%c
• a%b always in range [0..b) for positive b
New Length Operator
• Final syntax: #t
• Results in the length (or size, or last index) of an array (or list, or sequence)
• Computed in (log n)time
• with very low multiplier
• faster than table.getneven for huge arrays
• No more table.setn
New Length Operator (2)
• Subtle (and mostly useless) semantics for lists with holes
• use explicit size in those cases
• Nice idioms for list manipulation:
t[#t+1] = v — insertion
print(t[#t]) — last element
t[#t] = nil — removing
String Library
• string.findsplit in two functions
• string.find finds patterns
• string.matchextracts subpatterns (captures)
• For coherence, string.gfindshould be renamed string.gmatch
Specialized API Functions
• lua_tointeger/lua_pushinteger
• lua_getfield/lua_setfield
• Frequent cases
• Allows for small optimizations
• bigger ones for lua_tointeger
• lua_createtable(asize, rsize)
• bigger optimizations in specific cases
• in Lua, constructors do the job
Lua New Syntax for Long Strings (2.84 MO) (Cours PDF)