Cours Lua Specialized API Functions, tutoriel & guide de travaux pratiques en pdf.
New Mod Operator (2)
• Main rule: a = (a div b)b + a%b
• But a div b has several possible meanings
• floor(a/b), ceil(a/b), round(a/b), trunc(a/b)
• Which is best?
• floor has some nice properties
• a = b mod c iff a%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.getn even 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
String Library
• string.find split in two functions
• string.find finds patterns
• string.match extracts subpatterns (captures)
• For coherence, string.gfind should 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
Configurable Memory Allocation
• lua_newstate gets as argument an allocation function
• Allocation function must work as a generalized resize
• Access to original block size
• memory system does not need to keep it
• Access to an uninterpreted void *
• allow independent states to use different pools
Config. Memory Allocation (2)
• Lua core does not directly access OS services
• I/O, memory, etc.
• uses externally-provided functions for that
• Easy to convert the core to a freestanding C environment