Lua-5.2-style APIs for Lua 5.1.
What is it
This is a small module that aims to make it easier to write code in a Lua-5.2-style that is compatible with both Lua 5.1 and Lua 5.2. This does not make Lua 5.1 entirely compatible with Lua 5.2, but it brings the API closer to that of Lua 5.2.
- For writing Lua: Lua modules,
compat52.strict, which can be require'd from Lua scripts and run in both Lua 5.1 and 5.2, plus a backport of
bit32straight from the Lua 5.2 sources, adapted to build as a Lua 5.1 module.
- For writing C: A C header and file which can be linked to your Lua module written in C, providing some functions from the C API of Lua 5.2 that do not exist in Lua 5.1, making it easier to write C code that compiles with both versions of liblua.
Both the Lua module and the C files should also work with LuaJIT.
How to use it
You have to launch it like this (instead of the usual idiom of storing
the return of
require in a local variable) because compat52 needs to
make changes to your global environment.
When run under Lua 5.2, this module does nothing.
When run under Lua 5.1, it replaces some of your standard functions and adds new ones to bring your environment closer to that of Lua 5.2.
You may also use the "strict mode" which removes from Lua 5.1 functions that were deprecated in 5.2; that is the equivalent of running Lua 5.2 with the LUA_COMPAT_ALL flag disabled:
The "strict mode" changes the global environment, so it affects all
loaded modules and chunks. If this is undesirable, you can use the
"modular strict mode" which only replaces the environment of the current
file. The usage is slightly different (you have to call the return value
The effects of
compat52 are still in effect for all chunks, though.
Add the files
compat-5.2.h to your project and link it
with the rest of your code as usual.
stringpatterns may contain embedded zeros (see here)
string.repaccepts sep argument
tostringon arguments for
math.logaccepts base argument
xpcalltakes additional arguments
xpcallcan execute functions that yield
- metamethods for
ipairs(see here, and here)
#still doesn't respect
coroutinefunctions dealing with the main coroutine
coroutine.createaccepts functions written in C
- return code of
file:writereturn file handle
file:linesaccept format arguments (like
io.read) (but see here, and here).
bit32(actual backport from the
bit32library from Lua 5.2, also available as a stand-alone rock in the LuaRocks repository)
- optional strict mode which removes functions removed or deprecated in
Lua 5.2, such as
luaL_Reg(for Lua 5.0)
luaL_addchar(for Lua 5.0)
What's not implemented
- obviously, this does not turn Lua 5.1 into Lua 5.2: syntactic changes
to the core language, such as the
gotostatement, and semantic changes such as ephemeron support for weak tables, remain unavailable.
"*L"format flag for
- second argument for
- return values of
pipehas been opened by
"*"as second argument for
- some functions in the debug library
- anything else missing in the Lua libraries?
- For more information about compatibility between Lua versions, see Compatibility With Lua Five at the Lua-Users wiki
- For Lua-5.1-style APIs under Lua 5.0, see Compat-5.1
- for C support in the opposite direction (ie, loading C code using Lua-5.1-style APIs under Lua 5.2), see Twoface
This package contains code written by: