Lua Five Two |
|
While 5.1.x versions are strictly bug-fix only versions of 5.1[12][13][2], Lua 5.2 was allowed to make design changes that break full compatibility.
Feel free to update references! (As well as anything else here.) -- AlexanderGladysh
TODO: Some sections below may need updated to reflect the final Lua 5.2.0 version.
Lua core forces a GC when allocation fails.
References:
"generational"
LUA_GCGEN
Tables with weak keys only visit value when key is accessible.
References:
Userdata with finalizers are kept in a separated list for the GC
References:
Lua interpreter better handles non-string error messages.
References:
xpcall() now accepts arguments for the callback just like pcall() does
References:
New function package.searchpath() to search for your files just like require() does.
References:
Compile-time module system settings, available in package.config, are now documented.
References:
Loop iteration can be overloaded now with the help of new __pairs and __ipairs metamethods.
References: GeneralizedPairsAndIpairs
All types except string now respect __len metamethod. Previously, __len was not respected for table.
References:
string.format("%q") now escapes control characters.
References:
References:
You can write \xFF in strings in addition to old \255 notation.
References:
You can write numerical constants of the form 0x0.A or 0xA23p-4. In the latter case the 'p' indicates a binary exponent expressed in decimal (the manual does not make this clear: I assumed the exponent would be hexadecimal like the other parts). This feature is probably not useful in hand-written code but it allows precise de-serialisation of numbers serialised using the hexadecimal option of string.format.
However despite the inclusion of an official bit manipulation library, binary literals are still not supported.
References:
%b
in string.format
/ sprintf
os.date() now allows ANSI C options only.
References:
New luaL_testudata API function, similar to luaL_checkudata, but signaling invalid userdata to the caller rather than throwing an error.
References:
Division and modulo operations are no longer folded if the second argument is zero..
References:
Now you may yield() accross metamethods, for loop iterators pcall and xpcall calls and now-undocumented table.foreach[i] calls.
Also, C API calls are provided to implement your own yield-compliant C functions.
Now you can manipulate bits in Lua without the help of 3rd party modules or patches. None of existing implementations was used though.
References:
Op)
Now luaL_tolstring is documented. You may officially convert Lua values from C by the tostring() rules.
References: [20]
References:
_ENV
References:
getfenv() and setfenv() are deprecated in favor of the new lexical environments. Debug library counterparts are still there.
References:
Cache anonymous functions/closures for reuse. This optimization is now possible with the removal of getfenv/setfenv. TODO:add better description and examples.
New loadin(env,chunk) function to load chunk in a given environment. Note that the chunk
argument can also be a string.
fn,err = loadin({x=1,y=2},"return x+y")
References:
It is possible to forbid load()-ing of bytecode and/or source code chunks. Bytecode verification on load was removed.
References:
Lua threads (a.k.a coroutines) no longer have separate environments.
References: (Anyone?)
As expected Lua 5.2 ABI and bytecode is incompatible with 5.1.
References:
Almost all deprecated features from 5.1 were removed. New deprecated features are off by default.
References: (Anyone?)
If you need it, require() it.
luaL_openlibs
. --JohnHind
References:
linit.c
Previously the coroutine library was a sub-library loaded by the basic library loader function. In 5.2 it has been given its own loader function and promoted to a normal library. It is included in the list loaded by luaL_openlibs
so this change is only important if your runtime does not use luaL_openlibs
.
The manual has a slight error: this change is reflected in section 6, but section 6.2 still refers to it as "a sub-library of the basic library".
References:
linit.c, lcorolib.c
debug.getlocal gets parameter names of inactive functions. In debug.getlocal ([thread,] f, local)
, "The parameter f may also be a function. In that case, getlocal returns only the name of function parameters."
References:
New functions to detect if two upvalues are the same and to join them together if they are not:
References:
debug.getinfo() and lua_getinfo() now return extra information about function on call stack:
References: (Anyone?)
The frontier pattern is now documented.
References:
New pattern %g
represents all printable characters except space.
string.gsub
[6] now raises an error if the replacement string contains a '%' followed by a character other than the permitted '%' or digit.
Now ipairs() iterates over #t elements of the table. In 5.1 it did until first nil.
References: Source code: lua-5.2.0-work4/src/lbaselib.c line 213
You may pass negative index to select() to get arguments from the end of the vararg (in the direct order).
References:
math.log10() is deprecated. Use 10 as new second argument of math.log().
References: (Anyone?)
table.maxn is deprecated.
References:
unpack() is renamed to table.unpack(). table.pack() is added, packing arguments to a table and storing number of arguments to table's field "n".
References:
If file was opened by io.popen(), file:close() returns process' exit code.
References:
Calling collectgarbage("step") no longer restarts GC if it was stopped. Also, you may know if GC was stopped by calling collectgarbage("isrunning"). Of course you may do the same in C API.
References: (Anyone?)
Lua identifiers may no longer use locale-dependent characters.
References:
The following is a complete list of Lua's use of the registry table (at pseudo-index LUA_REGISTRYINDEX
) in the 5.2.0 release.
Installed by lua_newstate
Installed by 'package' library loader
package.loaded
package.preload
Installed by 'IO' library loader
The manual states: "As with global names, string keys starting with an underscore followed by uppercase letters are reserved for Lua." Note however that Lua does not rigerously follow this rule as with the "FILE*" key.
The metatable for strings has not been referenced in the registry, but it simply indexes the string library table itself, so functions added to that will automatically work as methods of string objects.
References:
lua_pushcfunction no longer allocates. This is more efficient and doesn't raise on failure. It also eliminates the need for lua_cpcall(), which has been removed from the API. Furthermore, a version of lua_cpcall supporting multiple arguments and return values is no longer needed either since lua_pcall now can do this.
If you need one, use LUA_RIDX_GLOBALS from the registry (or LUA_ENVIRONINDEX if you didn't changed your C function environment).
References: (Anyone?)
These functions work with C function environment instead of state's global environment to better reflect the way Lua works with global variables.
References: (Anyone?)
References:
New function lua_compare() was added to replace deprecated lua_equal() and lua_lessthan().
References:
New function lua_arith() to do arithmetics with Lua values the way Lua does it.
References:
lua_objlen() was renamed to lua_rawlen(). lua_len() was added to honor __len metamethod.
References:
lua_checkstack is now only returns error codes (never raises). Use luaL_checkstack if you want to raise (with error string).
References:
Now they return pointers to the internal string representations.
References: (Anyone?)
Allows to copy Lua values on stack (replacing existing value in the target slot).
References:
Two new functions were added, lua_version() and luaL_checkversion() to let user check runtime versions and address space correctness. This would help to prevent dreaded obscure bugs when several Lua instances are linked to the executable. luaL_checkversion() is called from luaL_register(), so such checks would be performed automatically when most modules are require()-d.
References:
New function to produce stacktraces just like debug.traceback() does.
References:
LUA_ERRGCMM is a new runtime error code to signify error in __gc metamethod. LUA_OK was added to keep symmetry and means that all went OK and there were no errors.
References:
Now Lua C modules may load dynamic libraries with global symbols (thanks to RTLD_GLOBAL).
References: (Anyone?)
DLL search path handling was improved on Windows.
LoadLibraryEx
.
Maximum number of constants per chunk increased to 2^26 from 2^18. Go, huge table dumps!
References:
Parser eats less c-stack space as it no longer uses auto arrays.
References:
New hash function for floating point values to better handle cases when lua_Number is defined as long double on some 64-bit platforms.
References:
file:write() returns file to allow chained calls.
References:
A new "*L" option to file:read is like "*l" but keeps end of line (if present). Also, a new keepNL
argument in file:lines
keeps new lines.
It may optionally call lua_close() on the Lua state.
References:
"Function os.execute now returns true when command terminates successfully and nil plus error information otherwise." [10]
Examples:
$ lua51 -e "os.exit(3)"; echo $? 3 $ lua51 -e 'print(os.execute[[lua -e "os.exit(3)"]])' 768 $ lua52 -e 'print(os.execute[[lua -e "os.exit(3)"]])' nil exit 3 $ perl -e 'print(system(qq(perl -e "exit(3)")),"\n")' 768 $ python -c "import os; print(os.system(\"python -c 'import os; os._exit(3)'\"))" 768
Info stack now is a linked list??
References:
The module/luaL_register
functions are deprecated and replaced by luaL_newlib and luaL_setfuncs. There is also a new function luaL_requiref
.
I believe this is a much more significant change than the manual or other material suggest. On the face of it, where you could previously write require 'coroutine'
for example, you now have to write coroutine = require 'coroutine'
. This is definitely a useful improvement, but if I have understood it correctly, it will break a lot of existing code. The 'changes' section of the manual is not sufficiently explicit about this. Worse, the documentation for require
does not clearly state the purpose of the return value. You have to read between the lines that the intent is to return either the module's global table or the value 'true' if the module does not export a global table (delivering this intent depends on the module loader following some rules, which are not explicitly stated).
true
, rather than a module table, to be assigned to package.loaded[modname]
is normally something to be avoided and is rather a fallback behavior, lacking any better value to use, to at least ensure that the module loader isn't executed more than once on multiple require calls. Now, concerning exactly what the loader should return, it should normally be a table [21], but the official Lua reference manual might be silent on this because of the MechanismNotPolicy design principle. Sometimes module loaders return functions (normally object constructors), though perhaps it would be preferable to return FuncTables (i.e. a table with a call operator) since it's quasi-standard for require'foo'._VERSION
to represent the module version (ModuleVersioning), which remains possible with FuncTables. BTW, the module
function, though deprecated in 5.2, is still available unless you turn off deprecated features, and the call to module
sets package.loaded[modname]
(as well as a global). --DavidManura
Compare luaL_requiref
(C API) with require
(Lua): the latter just takes the module name, while the former takes the module name, the library loader function and a flag which you set 'true' to store the return from the loader function in a global with the same name as the library. luaL_openlibs
uses luaL_requiref
with this flag set. Hence it will open the standard libraries with their global tables initialised as expected. However libraries opened from Lua with require
must set the global explicitly using the returned value.
Please feel free to correct the above two paragraphs if they are misconceived!
luaL_requiref
I consider to be just a convenience function for certain cases like linit.c. For example, it assumes the loader is a C function (lua_CFunction) and the destination table is LUA_RIDX_GLOBALS
, so it's not entirely general. The "must set the global explicitly" is not really true since it's normally preferred to set a local explicitly, local foo = require 'foo'
, which doesn't tamper with _G
. --DavidManura
preloads
table (I do this to save RAM). After converting from 5.1.4 to 5.2 I found that Lua with e.g. require 'coroutine'
no longer worked and I had to change it to coroutine = require 'coroutine'
. This leads to the consideration that, if third-party library developers follow the example (I will not say policy) set by the standard libraries, then they too may break existing Lua code. Most users will not notice this change with respect to the standard libraries because runtimes usually load these by calling luaL_openlibs
which in turn calls luaL_requiref
. -- JohnHind
References:
Added environmented variables LUA_PATH_5_2, etc.
...Or would we?
You're welcome to put your pet feature here, assuming it was supported by someone else on the list AND was declined by Lua authors for inclusion into 5.2. Please be polite. --AlexanderGladysh
Something along the lines of struct/lpack
References:
Decline motivation: (Have a link?)
Or other "meta" facility.
References:
Decline motivation: (Have a link?)
This would be similar to the C preprocessor's #line
directive [11]. The main motivation is to allow preprocessors to output this in generated Lua code so that debugging line numbers and file names correspond to the original (prior to preprocessing) source code.
Script "line number reversal, so errors that happen in the compiled Lua are rewritten to point back to the original Moon
Script line"
__LINE__
for SourceOptimizer
Get a string without coercions from numbers.
References:
Decline motivation: (Have a link?)
Module version number is currently expected to be on the left side of the name, which is rather unusual.
References:
Decline motivation: (Have a link?)
Replace package.seeall with something analogous to the package.clean
solutions in ModuleDefinition, but updated to support _ENV. package.seeall doesn't separate the public module table with the private implementation environment.
References:
Some features/changes proposed by users for 5.2 are listed in FeatureProposals. Requests should be posted to the mailing list.