From 5d7a1c10203fce463799d2737cfefceb0356f878 Mon Sep 17 00:00:00 2001 From: Jeffrey Clark Date: Wed, 2 Dec 2015 09:38:06 -0600 Subject: [PATCH] lua 5.3 compatiblity --- src/CMakeLists.txt | 18 +++++++++++--- src/script/common/c_converter.cpp | 2 +- src/script/common/c_types.cpp | 16 ++++++++++++ src/script/common/c_types.h | 12 ++++++++- src/script/lua_api/l_areastore.cpp | 5 ++++ src/script/lua_api/l_env.cpp | 10 ++++---- src/script/lua_api/l_inventory.cpp | 5 ++++ src/script/lua_api/l_item.cpp | 7 +++++- src/script/lua_api/l_nodemeta.cpp | 5 ++++ src/script/lua_api/l_nodetimer.cpp | 5 ++++ src/script/lua_api/l_noise.cpp | 40 ++++++++++++++++++++++++------ src/script/lua_api/l_object.cpp | 5 ++++ src/script/lua_api/l_settings.cpp | 5 ++++ src/script/lua_api/l_vmanip.cpp | 5 ++++ 14 files changed, 120 insertions(+), 20 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6256a8504..1d5396cf3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -167,10 +167,20 @@ else() message (STATUS "LuaJIT detection disabled! (ENABLE_LUAJIT=0)") endif() if(NOT USE_LUAJIT) - message(STATUS "LuaJIT not found, using bundled Lua.") - set(LUA_LIBRARY "lua") - set(LUA_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lua/src") - add_subdirectory(lua) + if(LUA_INCLUDE_DIR AND EXISTS "${LUA_INCLUDE_DIR}/lua.h") + if(NOT LUA_LIBRARY) + set(LUA_LIBRARY "lua") + endif() + FILE(STRINGS "${LUA_INCLUDE_DIR}/lua.h" lua_version_str REGEX "^#define[ \t]+LUA_VERSION_NUM[ \t]+[0-9]+.*") + STRING(REGEX REPLACE "^#define[ \t]+LUA_VERSION_NUM[ \t]+([0-9]+).*" "\\1" LUA_VERSION_STRING "${lua_version_str}") + UNSET(lua_version_str) + message(STATUS "Lua specified, using ${LUA_VERSION_STRING} ${LUA_LIBRARY}") + else() + message(STATUS "LuaJIT not found, using bundled Lua.") + set(LUA_LIBRARY "lua") + set(LUA_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/lua/src") + add_subdirectory(lua) + endif() endif() find_package(GMP REQUIRED) diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index f1d3cc421..1a39e749b 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -316,7 +316,7 @@ std::vector read_aabb3f_vector(lua_State *L, int index, f32 scale) { std::vector boxes; if(lua_istable(L, index)){ - int n = lua_objlen(L, index); + int n = LUA_LENGTH(L, index); // Check if it's a single box or a list of boxes bool possibly_single_box = (n == 6); for(int i = 1; i <= n && possibly_single_box; i++){ diff --git a/src/script/common/c_types.cpp b/src/script/common/c_types.cpp index e832ff2ab..70f45ff5d 100644 --- a/src/script/common/c_types.cpp +++ b/src/script/common/c_types.cpp @@ -32,3 +32,19 @@ struct EnumString es_ItemType[] = {ITEM_TOOL, "tool"}, {0, NULL}, }; + +#if LUA_VERSION_NUM >= 502 +LUALIB_API int luaL_pushtype (lua_State *L, int narg) { + if (!luaL_callmeta(L, narg, "__type")) + lua_pushstring(L, luaL_typename(L, narg)); + return 1; +} + +LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) { + const char *msg; + luaL_pushtype(L, narg); + msg = lua_pushfstring(L, "%s expected, got %s", + tname, lua_tostring(L, -1)); + return luaL_argerror(L, narg, msg); +} +#endif diff --git a/src/script/common/c_types.h b/src/script/common/c_types.h index 056f30251..e25ea1851 100644 --- a/src/script/common/c_types.h +++ b/src/script/common/c_types.h @@ -21,9 +21,19 @@ with this program; if not, write to the Free Software Foundation, Inc., #define C_TYPES_H_ extern "C" { -#include "lua.h" +#include +#include } +#if LUA_VERSION_NUM >= 502 +#define luaL_reg luaL_Reg +#define LUA_LENGTH(L, I) lua_rawlen((L), (I)) +LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); +LUALIB_API int (luaL_pushtype) (lua_State *L, int idx); +#else +#define LUA_LENGTH(L, I) lua_objlen((L), (I)) +#endif + #include #include "exceptions.h" diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp index 72fe24b20..fd255096d 100644 --- a/src/script/lua_api/l_areastore.cpp +++ b/src/script/lua_api/l_areastore.cpp @@ -378,7 +378,12 @@ void LuaAreaStore::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Can be created from Lua (AreaStore()) diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 084b1b440..94c4e83b3 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -676,8 +676,8 @@ int ModApiEnvMod::l_get_perlin(lua_State *L) if (lua_istable(L, 1)) { read_noiseparams(L, 1, ¶ms); } else { - params.seed = luaL_checkint(L, 1); - params.octaves = luaL_checkint(L, 2); + params.seed = luaL_checkinteger(L, 1); + params.octaves = luaL_checkinteger(L, 2); params.persist = luaL_checknumber(L, 3); params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4); } @@ -849,9 +849,9 @@ int ModApiEnvMod::l_find_path(lua_State *L) v3s16 pos1 = read_v3s16(L, 1); v3s16 pos2 = read_v3s16(L, 2); - unsigned int searchdistance = luaL_checkint(L, 3); - unsigned int max_jump = luaL_checkint(L, 4); - unsigned int max_drop = luaL_checkint(L, 5); + unsigned int searchdistance = luaL_checkinteger(L, 3); + unsigned int max_jump = luaL_checkinteger(L, 4); + unsigned int max_drop = luaL_checkinteger(L, 5); algorithm algo = A_PLAIN_NP; if (!lua_isnil(L, 6)) { std::string algorithm = luaL_checkstring(L,6); diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index de9f9374a..889755611 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -455,7 +455,12 @@ void InvRef::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Cannot be created from Lua diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index 842b15709..f82691239 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -427,7 +427,12 @@ void LuaItemStack::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Can be created from Lua (LuaItemStack(itemstack or itemstring or table or nil)) @@ -558,7 +563,7 @@ int ModApiItemMod::l_get_content_id(lua_State *L) int ModApiItemMod::l_get_name_from_content_id(lua_State *L) { NO_MAP_LOCK_REQUIRED; - content_t c = luaL_checkint(L, 1); + content_t c = luaL_checkinteger(L, 1); INodeDefManager *ndef = getServer(L)->getNodeDefManager(); const char *name = ndef->get(c).name.c_str(); diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp index c8bc7d558..b343638d6 100644 --- a/src/script/lua_api/l_nodemeta.cpp +++ b/src/script/lua_api/l_nodemeta.cpp @@ -338,7 +338,12 @@ void NodeMetaRef::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Cannot be created from Lua diff --git a/src/script/lua_api/l_nodetimer.cpp b/src/script/lua_api/l_nodetimer.cpp index 601113516..888e02d1f 100644 --- a/src/script/lua_api/l_nodetimer.cpp +++ b/src/script/lua_api/l_nodetimer.cpp @@ -154,7 +154,12 @@ void NodeTimerRef::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Cannot be created from Lua diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index 04dc6048f..6751dea2c 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -72,8 +72,8 @@ int LuaPerlinNoise::create_object(lua_State *L) if (lua_istable(L, 1)) { read_noiseparams(L, 1, ¶ms); } else { - params.seed = luaL_checkint(L, 1); - params.octaves = luaL_checkint(L, 2); + params.seed = luaL_checkinteger(L, 1); + params.octaves = luaL_checkinteger(L, 2); params.persist = luaL_checknumber(L, 3); params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4); } @@ -127,7 +127,12 @@ void LuaPerlinNoise::Register(lua_State *L) lua_pop(L, 1); - luaL_openlib(L, 0, methods, 0); +#if LUA_VERSION_NUM < 502 + luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); lua_register(L, className, create_object); @@ -359,7 +364,6 @@ LuaPerlinNoiseMap *LuaPerlinNoiseMap::checkobject(lua_State *L, int narg) void *ud = luaL_checkudata(L, narg, className); if (!ud) luaL_typerror(L, narg, className); - return *(LuaPerlinNoiseMap **)ud; } @@ -385,7 +389,12 @@ void LuaPerlinNoiseMap::Register(lua_State *L) lua_pop(L, 1); - luaL_openlib(L, 0, methods, 0); +#if LUA_VERSION_NUM < 502 + luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); lua_register(L, className, create_object); @@ -490,7 +499,12 @@ void LuaPseudoRandom::Register(lua_State *L) lua_pop(L, 1); - luaL_openlib(L, 0, methods, 0); +#if LUA_VERSION_NUM < 502 + luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); lua_register(L, className, create_object); @@ -589,7 +603,12 @@ void LuaPcgRandom::Register(lua_State *L) lua_pop(L, 1); - luaL_openlib(L, 0, methods, 0); +#if LUA_VERSION_NUM < 502 + luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); lua_register(L, className, create_object); @@ -704,7 +723,12 @@ void LuaSecureRandom::Register(lua_State *L) lua_pop(L, 1); - luaL_openlib(L, 0, methods, 0); +#if LUA_VERSION_NUM < 502 + luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); lua_register(L, className, create_object); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 52190d60e..fd7a258ea 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1685,7 +1685,12 @@ void ObjectRef::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Cannot be created from Lua diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index 35b82b435..f0448997d 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -176,7 +176,12 @@ void LuaSettings::Register(lua_State* L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Can be created from Lua (Settings(filename)) diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index f13866408..edb4aa9f7 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -450,7 +450,12 @@ void LuaVoxelManip::Register(lua_State *L) lua_pop(L, 1); // drop metatable +#if LUA_VERSION_NUM < 502 luaL_openlib(L, 0, methods, 0); // fill methodtable +#else + lua_newtable(L); + luaL_setfuncs(L, methods, 0); +#endif lua_pop(L, 1); // drop methodtable // Can be created from Lua (VoxelManip())