Compatibility module providing Lua-5.3-style APIs for Lua 5.2 and 5.1
Lua-5.3-style APIs for Lua 5.2 and 5.1.
This is a small module that aims to make it easier to write code in a Lua-5.3-style that is compatible with Lua 5.1, Lua 5.2, and Lua 5.3. This does not make Lua 5.2 (or even Lua 5.1) entirely compatible with Lua 5.3, but it brings the API closer to that of Lua 5.3.
It includes:
compat53, which can be require'd from Lua scripts and run in Lua 5.1, 5.2, and 5.3, including a backport of the
utf8module, the 5.3
tablemodule, and the string packing functions straight from the Lua 5.3 sources.
require("compat53")
compat53makes changes to your global environment and does not return a meaningful return value, so the usual idiom of storing the return of
requirein a local variable makes no sense.
When run under Lua 5.3+, this module does nothing.
When run under Lua 5.2 or 5.1, it replaces some of your standard functions and adds new ones to bring your environment closer to that of Lua 5.3. It also tries to load the backported
utf8,
table, and string packing modules automatically. If unsuccessful, pure Lua versions of the new
tablefunctions are used as a fallback, and Roberto's struct library is tried for string packing.
local _ENV = require("compat53.module") if setfenv then setfenv(1, _ENV) end
The
compat53.modulemodule does not modify the global environment, and so it is safe to use in modules without affecting other Lua files. It is supposed to be set as the current environment (see above), i.e. cherry picking individual functions from this module is expressly not supported!). Not all features are available when using this module (e.g. yieldable (x)pcall support, string/file methods, etc.), so it is recommended to use plain
require("compat53")whenever possible.
There are two ways of adding the C API compatibility functions/macros to your project: * If
COMPAT53_PREFIXis not
#defined,
compat-5.3.h
#includes
compat-5.3.c, and all functions are made
static. You don't have to compile/link/add
compat-5.3.cyourself. This is useful for one-file projects. * If
COMPAT53_PREFIXis
#defined, all exported functions are renamed behind the scenes using this prefix to avoid linker conflicts with other code using this package. This doesn't change the way you call the compatibility functions in your code. You have to compile and link
compat-5.3.cto your project yourself. You can change the way the functions are exported using the
COMPAT53_APImacro (e.g. if you need some
__declspecmagic). While it is technically possible to use the "lua" prefix (and it looks better in the debugger), this is discouraged because LuaJIT has started to implement its own Lua 5.2+ C API functions, and with the "lua" prefix you'd violate the one-definition rule with recent LuaJIT versions.
utf8module backported from the Lua 5.3 sources
string.pack,
string.packsize, and
string.unpackfrom the Lua 5.3 sources or from the
structmodule. (
structis not 100% compatible to Lua 5.3's string packing!) (See here)
math.maxintegerand
math.mininteger,
math.tointeger,
math.type, and
math.ult(see here)
assertaccepts non-string error messages
ipairsrespects
__indexmetamethod
table.move
tablelibrary respects metamethods
For Lua 5.1 additionally: *
loadand
loadfileaccept
modeand
envparameters *
table.packand
table.unpack* string patterns may contain embedded zeros (but see here) *
string.repaccepts
separgument *
string.formatcalls
tostringon arguments for
%s*
math.logaccepts base argument *
xpcalltakes additional arguments *
pcalland
xpcallcan execute functions that yield (see here for a possible problem with
coroutine.running) *
pairsrespects
__pairsmetamethod (see here) *
rawlen(but
#still doesn't respect
__lenfor tables) *
package.searchersas alias for
package.loaders*
package.searchpath(see here) *
coroutinefunctions dealing with the main coroutine (see here for a possible problem with
coroutine.running) *
coroutine.createaccepts functions written in C * return code of
os.execute(see here) *
io.writeand
file:writereturn file handle *
io.linesand
file:linesaccept format arguments (like
io.read) (see here and here) *
debug.setmetatablereturns object *
debug.getuservalue(see here) *
debug.setuservalue(see here)
lua_KContext(see here)
lua_KFunction(see here)
lua_dump(extra
stripparameter, ignored, see here)
lua_getextraspace(limited compatibilitiy, see here)
lua_getfield(return value)
lua_getiand
lua_seti
lua_getglobal(return value)
lua_getmetafield(return value)
lua_gettable(return value)
lua_getuservalue(limited compatibility, see here)
lua_setuservalue(limited compatibility, see here)
lua_isinteger
lua_numbertointeger
lua_callkand
lua_pcallk(limited compatibility, see here)
lua_resume
lua_rawgetand
lua_rawgeti(return values)
lua_rawgetpand
lua_rawsetp
luaL_requiref(now checks
package.loadedfirst)
lua_rotate
lua_stringtonumber(see here)
For Lua 5.1 additionally: *
LUA_OK*
LUA_ERRGCMM*
LUA_OP*macros for
lua_arithand
lua_compare*
LUA_FILEHANDLE*
lua_Unsigned*
luaL_Stream(limited compatibility, see here) *
lua_absindex*
lua_arith(see here) *
lua_compare*
lua_len,
lua_rawlen, and
luaL_len*
lua_load(mode argument) *
lua_pushstring,
lua_pushlstring(return value) *
lua_copy*
lua_pushglobaltable*
luaL_testudata*
luaL_setfuncs,
luaL_newlibtable, and
luaL_newlib*
luaL_setmetatable*
luaL_getsubtable*
luaL_traceback*
luaL_execresult*
luaL_fileresult*
luaL_loadbufferx*
luaL_loadfilex*
luaL_checkversion(with empty body, only to avoid compile errors, see here) *
luaL_tolstring*
luaL_buffinitsize,
luaL_prepbuffsize, and
luaL_pushresultsize(see here) *
lua_pushunsigned,
lua_tounsignedx,
lua_tounsigned,
luaL_checkunsigned,
luaL_optunsigned, if
LUA_COMPAT_APIINTCASTSis defined.
coroutine.isyieldable
_ENV,
goto, labels, ephemeron tables, etc. See
lua-compat-5.2for a detailed list.
lua_isyieldable
lua_arith(new operators missing)
lua_push(v)fstring(new formats missing)
lua_upvalueid(5.1)
lua_upvaluejoin(5.1)
lua_version(5.1)
lua_yieldk(5.1)
This package contains code written by: