1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-09-30 05:59:07 +00:00

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.
This commit is contained in:
Pavel Labath 2013-07-13 23:55:28 +02:00
parent 0f94f409a1
commit 02dfacd694
3 changed files with 45 additions and 5 deletions

View File

@ -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)

View File

@ -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);

View File

@ -37,8 +37,6 @@ namespace lua {
typedef std::function<int(state *)> 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'