Lua 5.4.0 Released

Lua logo

A new version of Lua has been released yesterday. Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.

Five years after Lua 5.3, the new Lua 5.4 branch comes with the following changes:

– new generational mode for garbage collection
– to-be-closed variables
– const variables
– userdata can have multiple user values
– new implementation for math.random
– warning system
– debug information about function arguments and returns
– new semantics for the integer ‘for’ loop
– optional ‘init’ argument to ‘string.gmatch’
– new functions ‘lua_resetthread’ and ‘coroutine.close’
– string-to-number coercions moved to the string library
– allocation function allowed to fail when shrinking a memory block
– new format ‘%p’ in ‘string.format’
– utf8 library accepts codepoints up to 2^31

In addition to that list, there is also a new keyword undef (source):

Once you have nils in tables, t[i]=nil does not remove elements from
a table anymore. For that, you need to write t[i]=undef.

No, this is not like JavaScript! ‘undef’ is NOT a value.

t[i]=undef is a special syntax form that means “remove the key ‘i’
from table ‘t'”. You can only use ‘undef’ in three specific forms:

t[i] = undef — remove a key from a table
t[i] == undef — test whether a table has a key
t[i] ~= undef — test whether a table has a key

Any other use of ‘undef’ gives a syntax error.

Another nice change is the new math.random() function. In Lua 5.3, math.random() was based on the standard C rand() function. The C rand() implementation produces different results on Windows (VC runtimes) or on Linux (GLIBC) which can be an issue on cross-platform applications (I quick talked about that here). Lua 5.4 comes with a new implementation of the random function, this time based on the xoshiro256** algorithm that produces pseudo-random 64-bit integers.

Constant variables are there as well. A new keyword const has been added to make a variable constant. Once initialized, a const variable can not be changed!

local _PI  = 3.14159265

-- The following line will produce an error: 
-- "attempt to assign to const variable '_PI'"
_PI = _PI + 3

To-be-closed variables allow to declare a local variable with a destructor-like feature (source):

A to-be-closed variable behaves like a constant local variable, except that its value is closed whenever the variable goes out of scope, including normal block termination, exiting its block by break/goto/return, or exiting by an error.

Here, to close a value means to call its __close metamethod. When calling the metamethod, the value itself is passed as the first argument and the error object that caused the exit (if any) is passed as a second argument; if there was no error, the second argument is nil.

The value assigned to a to-be-closed variable must have a __close metamethod or be a false value. (nil and false are ignored as to-be-closed values.)

If several to-be-closed variables go out of scope at the same event, they are closed in the reverse order that they were declared.

I’m currently updating GeeXLab with Lua 5.4.0 and it should be available very soon.