📜 ⬆️ ⬇️

What can be expected from Lua 5.2?

The first preview version of Lua 5.2 has been published.


Lua is a powerful, fast, easy, extensible and embedded scripting programming language. Luah is convenient to use for writing business logic applications.

Despite the fact that the source code of Lua is open, the development of new versions of the language is a rather closed, conservative and unhurried process (which, in the case of the programming language, is not so bad). Every new version of the language in the community is eagerly awaited and relishes the details leaking to the public.

What is posted to the public is not even alpha - it's just a snapshot of the working code. Anything can change to release.
')
Announcement here: http://article.gmane.org/gmane.comp.lang.lua.general/61229

Updated list of changes from the authors here: http://article.gmane.org/gmane.comp.lang.lua.general/61505

Download the source here: http://www.lua.org/work/lua-5.2.0-work1.tar.gz

Building under Windows is here: http://article.gmane.org/gmane.comp.lang.lua.general/61528

View the source online, for example, here .

Update: Posted 5.2.0-work2, announcement here .

What changed


Official information is not enough. The list of changes is mainly taken from the analysis of diffs of the user's manual. Details can be found there. Changes are given in no particular order. The list is most likely incomplete and will be updated to the best of power.

General changes

Working with Lua environments


This is the most controversial set of changes. Around him in a mailing list now breaks most of the copies.

Lexical environments

Added "lexical environments" (lexical environments), allowing to replace for a certain piece of code a global environment to the specified table.

New loadin () function

A new loadin () function has been added that allows you to load code into the specified environment:

Setfenv () and getfenv () functions

The setfenv () and getfenv () functions are no longer supported. Instead, it is proposed to use new lexical environments or a new loadin () function.

For the afflicted, debug.setfenv () and debug.getfenv () are left.

Corutin no longer has separate environments.

All korutin in steyt have one common fixed environment.

New metamethods


Metamethod __len works for tables and rows

The code for working with tables and rows now pays attention to the metamethod __len when determining the length of the data.

New metamethods __ipairs and __pairs

Now you can override the object traversal using ipairs and pairs.

Debug module


The debug module is no longer loaded by default.

If the code uses it, you need to explicitly call require ("debug").

New features for working with upvalue

Added debug.upvalueid (), debug.upvaluejoin () and their corresponding lua_upvalueid () and lua_upvaluejoin (). (Some of them have not yet been documented.)

Now you can get a unique identifier upvalue (you need to understand whether two upvalues ​​point to the same object).

With upvaluejoin, you can have two different upvalues ​​pointing to the same object. This should allow for serialization of functions with upvalue without patches of the Lua code.

Expanded debug information available

In the entry returned by debug.getinfo () and lua_getinfo (), new fields have appeared. Now about the function in the call stack, you can find out the following:

Other changes to the API Lua


Bitlib

Added library for performing binary operations.

Improved character escaping in% q

Now string.format ("% q") escapes all control characters.

Frontier pattern

The% f search pattern ( "frontier pattern" ) is now documented.

Changed ipairs () behavior

Now, when determining the length of the table, ipairs () follows the general rules (before, the crawl of tables was carried out strictly before the first hole).

Negative numbers in select ()

Now select () - you can pass a negative number as the number of the first argument of the sample - the corresponding number of arguments will be returned from the end (in direct order).

math.log10 () is no longer supported

Now in math.log () you can specify the base of the logarithm.

The table.maxn () function is no longer supported.

If you really need it, write it yourself.

Functions table.unpack () and table.pack ()

The unpack () function has been moved to the table module. A new table.pack () function has been added to it, which saves the arguments to a new table (the number of arguments is stored in the “n” field in this table).

Filtering by chunk type in load ()

The load () functions can now be prohibited from loading bytecode or text sources.

This is useful because, unlike text, loading bytecode from untrusted sources is not recommended. Lua 5.2 no longer tries to validate the correctness of the bytecode being loaded.

package.config is now documented

Now from Lua, the compile-time settings for the module system are officially available:
Added package.searchpath () function

The function used to search for files in a given set of paths. (Same as used in require.)

Arguments in xpcall ()

Now in xpcall () you can set the arguments for the called function, similarly to pcall ().

Checking arguments in os.date ()

The os.date () function now checks the format string passed to it in order to protect itself from falling implementations of strftime ().

Improved payp support

If the file was opened using io.popen (), file: close () will return the exit code of the corresponding process.

Improved file: write () interface

Now file: write () returns a file, which allows you to make a chain of calls.

Garbage collector


Emergency GC

Lua forces full garbage collection if he cannot allocate memory.

Ephemeral tables

The garbage collector now correctly handles circular references between keys and values ​​in tables with weak keys.

Adjusted behavior of incremental garbage collector

Attempting to perform the garbage collection step no longer restarts the garbage collector if it has been stopped.

Is GC stopped?

Now you can see if GC is stopped. In Lua: collectgarbage ("isrunning"), in C: lua_gc (LUA_GCISRUNNING).

Other language changes


Working with locale

The definition of the type of the character (ctype) at compilation is now fully carried out according to the internal fixed tables that do not depend on the current locale.

Identifiers can use only locale-independent characters.

Hex codes in string literals

Now you can write in lines, for example, \ xFF (before - only \ 255).

Simplified Constant Folding

In order to avoid problems with floating-point numbers, the compiler no longer does constant folding for division operators and deriving the remainder of division.

Improved support for non-string error messages

If an error handler in the interpreter comes instead of a string - an error message is an object, it tries to call the __tostring metamethod before displaying an error on the screen.

Working with C API environments


New fields in the registry

In the registry (registry, a special table that is available only to a shared code), there are predefined fields that are available with the following integer keys:
Removed pseudo-index LUA_GLOBALSINDEX

If the environment of the function has not been changed, you can use LUA_ENVIRONINDEX instead. Otherwise, you need to get a global environment from the registry.

Changed the logic of the macros lua_getglobal (), lua_setglobal (), and lua_register ()

They now work with the environment of functions instead of the global environment of the state. This is more consistent with how Lua works with global variables.

Other changes to the Lua C API


Renamed luaL_typerror () function

Now it is called luaL_typeerror ().

Added lua_compare () function

It should be used instead of the no longer supported lua_equal () and lua_lessthan ().

lua_arith ()

The new function lua_arith () allows to perform arithmetic operations with C values ​​from C according to the rules of Lua (taking into account metamethods, etc.).

Determination of object length

The lua_objlen () function has been renamed. Now it is called lua_rawlen (). Added function lua_len (), which knows about the metamethod __len in tables.

Improved lua_checkstack () function

Now it is guaranteed that it cannot cause a memory allocation error.

Improved lua_pushstring () and lua_pushlstring () functions

Now they return a pointer to the internal copy of the string.

lua_copy ()

The new lua_copy () function allows you to copy values ​​on the loash data stack from one slot to another (with the old value in the other slot being replaced with the one being copied).

Runtime version check

Now you can check runtime versions of Lua using the new functions lua_version () and luaL_checkversion ().

Moreover, since these functions return pointers, you can check that the loash state and the code currently running are in the same address space. This allows you to catch the situation when Lua several times linked with the application.

The luaL_checkversion () function performs this check automatically. This function is called by luaL_register (), so now runtime compliance will be checked automatically when each module is connected.

luaL_testudata ()

Added new function luaL_testudata () for the userdata test for belonging to this type. (Previously, only validation via luaL_checkudata () was available, which caused an error when passing the wrong type of userdata.)

luaL_traceback ()

The new luaL_traceback () function to make C-frames from C, like debug.traceback () in Lua.

luaL_tolstring ()

The luaL_tolstring () function is now documented. It allows you to convert luash values ​​similarly to the tostring () function in Lua. Implementing your own print () is now easier than ever!

Support for yield across the lua-c border

Added new API methods that allow you to write C code through which you could yield to yield in Lua:An example of usage can be seen, for example, in the new implementation of the table.foreach () function (which for some reason still lives in the source code, although it was declared deprecated in 5.1).

These methods are quite widely used in the Lua 5.2 implementation itself, so now it will be possible to make a yield through metamethods, iterators for the for loop, and the functions pcall () and xpcall ()!

New error code LUA_ERRGCMM

A new error code, LUA_ERRGCMM, appeared, indicating that an error occurred while executing the metamethod __gc during garbage collection. (For obvious reasons, it usually does not relate to the code indicated by the glass trace.)

For symmetry, the “error” code LUA_OK has also been added, meaning that the call was made successfully.

Improved loading of dynamic libraries under the Nix

Now loadlib works correctly with libraries with global symbols (using RTLD_GLOBAL).

Changes in implementation


Increased maximum number of constants per function (chunk)

Now there can be up to 2 26 constants in one chunk (it used to be up to 2 18 ). Exceeding this limit causes the “constant table overflow” compile time error.

Now you can save even more monstrous data in the form of table designers, without worrying about splitting the generated code into separate functions.

Parser optimized

The parser now eats away much smaller sishny stack due to the rejection of the use of automatic arrays (auto arrays).

New hash function for floating point numbers

Now, hashing is performed correctly regardless of the size of lua_Number (earlier on some 64-bit platforms there were problems if you redefine lua_Number in long double).

Known issues in current snapshot

Source: https://habr.com/ru/post/80429/


All Articles