From ce3619e619c99eeca253bc2a842d31451b57c378 Mon Sep 17 00:00:00 2001 From: Klaus Ethgen Date: Sat, 27 Jul 2019 09:30:15 +0100 Subject: [PATCH] Compatibility function for lua > 5.1 --- configure.ac | 29 ++++++++++++++++++----------- src/lua.c | 27 +++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/configure.ac b/configure.ac index 3fe49881..4e819436 100644 --- a/configure.ac +++ b/configure.ac @@ -518,22 +518,29 @@ AC_ARG_ENABLE([lua], [liblua=$enableval], [liblua=auto]) if test "x${liblua}" != "xno"; then - PKG_CHECK_MODULES(LUA, lua5.1 >= 5.1, + PKG_CHECK_MODULES(LUA, lua5.3 >= 5.3, [ HAVE_LUA=yes AC_DEFINE(HAVE_LUA, 1, [define to enable lua support]) ], [ - PKG_CHECK_MODULES(LUA, lua >= 5.1, - [ - HAVE_LUA=yes - AC_DEFINE(HAVE_LUA, 1, [define to enable lua support]) - ], - [ - HAVE_LUA=no - AC_MSG_WARN([$LUA_PKG_ERRORS]) - ]) - ]) + PKG_CHECK_MODULES(LUA, lua5.1 >= 5.1, + [ + HAVE_LUA=yes + AC_DEFINE(HAVE_LUA, 1, [define to enable lua support]) + ], + [ + PKG_CHECK_MODULES(LUA, lua >= 5.1, + [ + HAVE_LUA=yes + AC_DEFINE(HAVE_LUA, 1, [define to enable lua support]) + ], + [ + HAVE_LUA=no + AC_MSG_WARN([$LUA_PKG_ERRORS]) + ]) + ]) + ]) else HAVE_LUA=disabled fi diff --git a/src/lua.c b/src/lua.c index eabbad0e..7859162a 100644 --- a/src/lua.c +++ b/src/lua.c @@ -41,6 +41,25 @@ static lua_State *L; /** The LUA object needed for all operations (NOTE: That is * a upper-case variable to match the documentation!) */ +/* Taking that definition from lua 5.1 source */ +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 502 +int luaL_typerror(lua_State *L, int narg, const char *tname) +{ + const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); +} + +# define LUA_register_meta(L, meta) luaL_setfuncs(L, meta, 0); +# define LUA_register_global(L, string, func) \ + lua_newtable(L); \ + luaL_setfuncs(L, func, 0); \ + lua_pushvalue(L, -1); \ + lua_setglobal(L, string) +#else +# define LUA_register_meta(L, meta) luaL_register(L, NULL, meta) +# define LUA_register_global(L, string, func) luaL_register(L, string, func) +#endif + static FileData *lua_check_image(lua_State *L, int index) { FileData **fd; @@ -211,9 +230,9 @@ void lua_init(void) {"get_marks", lua_image_get_marks}, {NULL, NULL} }; - luaL_register(L, "Image", image_methods); + LUA_register_global(L, "Image", image_methods); luaL_newmetatable(L, "Image"); - luaL_register(L, NULL, meta_methods); + LUA_register_meta(L, meta_methods); lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); lua_settable(L, -3); @@ -228,9 +247,9 @@ void lua_init(void) {"get_datum", lua_exif_get_datum}, {NULL, NULL} }; - luaL_register(L, "Exif", exif_methods); + LUA_register_global(L, "Exif", exif_methods); luaL_newmetatable(L, "Exif"); - luaL_register(L, NULL, meta_methods); + LUA_register_meta(L, meta_methods); lua_pushliteral(L, "__index"); lua_pushvalue(L, -3); lua_settable(L, -3); -- 2.20.1