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:
- For writing Lua: The Lua module
compat53, which can be require'd from Lua scripts and run in Lua 5.1, 5.2, and 5.3, including a backport of theutf8module, the 5.3tablemodule, and the string packing functions straight from the Lua 5.3 sources. - 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.3 that do not exist in Lua 5.2 or 5.1, making it easier to write C code that compiles with all three versions of liblua.
require("compat53")compat53 makes changes to your global environment and does not return
a meaningful return value, so the usual idiom of storing the return of
require in 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 table functions are used as a fallback, and
Roberto's struct library is tried for string packing.
There are two ways of adding the C API compatibility functions/macros to your project:
- If
COMPAT53_PREFIXis not#defined,compat-5.3.h#includescompat-5.3.c, and all functions are madestatic. You don't have to compile/link/addcompat-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 linkcompat-5.3.cto your project yourself. You can change the way the functions are exported using theCOMPAT53_APImacro (e.g. if you need some__declspecmagic).
- the
utf8module backported from the Lua 5.3 sources string.pack,string.packsize, andstring.unpackfrom the Lua 5.3 sources or from thestructmodule. (structis not 100% compatible to Lua 5.3's string packing!)math.maxintegerandmath.mininteger,math.tointeger,math.type, andmath.ultipairsrespects__indexmetamethodtable.movetablelibrary respects metamethods
For Lua 5.1 additionally:
loadandloadfileacceptmodeandenvparameterstable.packandtable.unpack- string patterns may contain embedded zeros
string.repacceptssepargumentstring.formatcallstostringon arguments for%smath.logaccepts base argumentxpcalltakes additional argumentspcallandxpcallcan execute functions that yieldpairsrespects__pairsmetamethodrawlen(but#still doesn't respect__lenfor tables)package.searchersas alias forpackage.loaderspackage.searchpathcoroutinefunctions dealing with the main coroutinecoroutine.createaccepts functions written in C- return code of
os.execute io.writeandfile:writereturn file handleio.linesandfile:linesaccept format arguments (likeio.read)debug.setmetatablereturns objectdebug.getuservalueanddebug.setuservalue
lua_KContextlua_KFunctionlua_dump(extrastripparameter, ignored)lua_getfield(return value)lua_getiandlua_setilua_getglobal(return value)lua_getmetafield(return value)lua_gettable(return value)lua_getuservalueandlua_setuservalue(limited compatibility)lua_isintegerlua_numbertointegerlua_callkandlua_pcallk(limited compatibility)lua_rawgetandlua_rawgeti(return values)lua_rawgetpandlua_rawsetpluaL_requiref(now checkspackage.loadedfirst)lua_rotatelua_stringtonumber
For Lua 5.1 additionally:
LUA_OKLUA_OP*macros forlua_arithandlua_comparelua_UnsignedluaL_StreamLUA_FILEHANDLElua_absindexlua_arithlua_comparelua_len,lua_rawlen, andluaL_lenlua_copylua_pushglobaltableluaL_testudataluaL_setfuncs,luaL_newlibtable, andluaL_newlibluaL_setmetatableluaL_getsubtableluaL_tracebackluaL_fileresultluaL_checkversion(with empty body, only to avoid compile errors)luaL_tolstringluaL_buffinitsize,luaL_prepbuffsize, andluaL_pushresultsizelua_pushunsigned,lua_tounsignedx,lua_tounsigned,luaL_checkunsigned,luaL_optunsigned, ifLUA_COMPAT_APIINTCASTSis defined.
- bit operators
- integer division operator
- utf8 escape sequences
- 64 bit integers
coroutine.isyieldable- Lua 5.1:
_ENV,goto, labels, ephemeron tables, etc. Seelua-compat-5.2for a detailed list. - the following C API functions/macros:
lua_isyieldablelua_getextraspacelua_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)luaL_execresult(5.1)luaL_loadbufferx(5.1)luaL_loadfilex(5.1)
The emulation of lua_(p)callk for previous Lua versions is not 100%
perfect, because the continuation functions in Lua 5.2 have different
signatures than the ones in Lua 5.3 (and Lua 5.1 doesn't have
continuation functions at all). But with the help of a small macro the
same code can be used for all three Lua versions (the 5.1 version
won't support yielding though).
Original Lua 5.3 code (example adapted from the Lua 5.3 manual):
static int k (lua_State *L, int status, lua_KContext ctx) {
... /* code 2 */
}
int original_function (lua_State *L) {
... /* code 1 */
return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
}Portable version:
LUA_KFUNCTION( k ) {
... /* code 2; parameters L, status, and ctx available here */
}
int original_function (lua_State *L) {
... /* code 1 */
return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1);
}- For Lua-5.2-style APIs under Lua 5.1, see lua-compat-5.2, which also is the basis for most of the code in this project.
- For Lua-5.1-style APIs under Lua 5.0, see Compat-5.1
This package contains code written by:
- The Lua Team
- Philipp Janda (@siffiejoe)
- Tomás Guisasola Gorham (@tomasguisasola)
- Hisham Muhammad (@hishamhm)
- Renato Maia (@renatomaia)