From 02dfacd694ea11f0a05d8d31e3f1240526475505 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sat, 13 Jul 2013 23:55:28 +0200 Subject: [PATCH] Add support for lua 5.2 luamm is now able to be compiled with both lua 5.1 and 5.2 (assuming 5.2 has backward compatibility features compiled in). It is my intention to always support at least two versions of lua. --- cmake/ConkyPlatformChecks.cmake | 3 ++- src/luamm.cc | 41 +++++++++++++++++++++++++++++++++ src/luamm.hh | 6 ++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cmake/ConkyPlatformChecks.cmake b/cmake/ConkyPlatformChecks.cmake index 7594ff24..5a4b006a 100644 --- a/cmake/ConkyPlatformChecks.cmake +++ b/cmake/ConkyPlatformChecks.cmake @@ -233,9 +233,10 @@ if(BUILD_X11) endif(X11_FOUND) endif(BUILD_X11) -pkg_search_module(LUA REQUIRED lua5.1 lua-5.1 lua<=5.1.99) +pkg_search_module(LUA REQUIRED lua5.2 lua-5.2 lua>=5.1 lua5.1 lua-5.1) set(conky_libs ${conky_libs} ${LUA_LIBRARIES}) set(conky_includes ${conky_includes} ${LUA_INCLUDE_DIRS}) +link_directories(${LUA_LIBRARY_DIRS}) if(BUILD_LUA_CAIRO) set(WANT_TOLUA true) pkg_check_modules(CAIRO REQUIRED cairo cairo-xlib) diff --git a/src/luamm.cc b/src/luamm.cc index ffc68b03..2a1f5ee2 100644 --- a/src/luamm.cc +++ b/src/luamm.cc @@ -25,6 +25,20 @@ namespace lua { namespace { + +#if LUA_VERSION_NUM >= 502 + // These two functions were deprecated in 5.2. Limited backwards compatibility is + // provided by macros. We want them as real functions, because we take their addresses. + +#undef lua_equal + int lua_equal(lua_State *L, int index1, int index2) + { return lua_compare(L, index1, index2, LUA_OPEQ); } + +#undef lua_lessthan + int lua_lessthan(lua_State *L, int index1, int index2) + { return lua_compare(L, index1, index2, LUA_OPLT); } +#endif + // keys for storing values in lua registry const char cpp_exception_metatable[] = "lua::cpp_exception_metatable"; const char cpp_function_metatable [] = "lua::cpp_function_metatable"; @@ -336,6 +350,18 @@ namespace lua { gettable(index); } + void state::getglobal(const char *name) + { +#if LUA_VERSION_NUM >= 502 + checkstack(1); + pushinteger(LUA_RIDX_GLOBALS); + gettable(REGISTRYINDEX); + getfield(-1, name); +#else + getfield(LUA_GLOBALSINDEX, name); +#endif + } + void state::gettable(int index) { checkstack(2); @@ -446,6 +472,21 @@ namespace lua { settable(index); } + void state::setglobal(const char *name) + { +#if LUA_VERSION_NUM >= 502 + stack_sentry s(*this, -1); + checkstack(1); + pushinteger(LUA_RIDX_GLOBALS); + gettable(REGISTRYINDEX); + insert(-2); + setfield(-2, name); + pop(); +#else + setfield(LUA_GLOBALSINDEX, name); +#endif + } + void state::settable(int index) { checkstack(2); diff --git a/src/luamm.hh b/src/luamm.hh index 94271f93..801c6a03 100644 --- a/src/luamm.hh +++ b/src/luamm.hh @@ -37,8 +37,6 @@ namespace lua { typedef std::function cpp_function; enum { - ENVIRONINDEX = LUA_ENVIRONINDEX, - GLOBALSINDEX = LUA_GLOBALSINDEX, REGISTRYINDEX = LUA_REGISTRYINDEX }; @@ -273,8 +271,8 @@ namespace lua { bool equal(int index1, int index2); int gc(int what, int data); void getfield(int index, const char *k); + void getglobal(const char *name); void gettable(int index); - void getglobal(const char *name) { getfield(GLOBALSINDEX, name); } bool lessthan(int index1, int index2); void loadfile(const char *filename) throw(lua::syntax_error, lua::file_error, std::bad_alloc); void loadstring(const char *s) throw(lua::syntax_error, std::bad_alloc); @@ -282,7 +280,7 @@ namespace lua { // register is a reserved word :/ void register_fn(const char *name, const cpp_function &f) { pushfunction(f); setglobal(name); } void setfield(int index, const char *k); - void setglobal(const char *name) { setfield(GLOBALSINDEX, name); } + void setglobal(const char *name); void settable(int index); // lua_tostring uses NULL to indicate conversion error, since there is no such thing as a // NULL std::string, we throw an exception. Returned value may contain '\0'