diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 3fe4a1ae8..34f46b169 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -594,7 +594,7 @@ function test_guard_resources() end local function is_flag_set(flags, flag) - return math.mod(flags, flag*2) - math.mod(flags, flag) == flag; + return math.fmod(flags, flag*2) - math.fmod(flags, flag) == flag; end function test_hero_hero_transfer() @@ -803,7 +803,7 @@ end local function find_in_report(f, pattern, extension) extension = extension or "nr" local filename = config.reportpath .. "/" .. get_turn() .. "-" .. itoa36(f.id) .. "." .. extension - local report = io.open(filename, 'rt'); + local report = io.open(filename, 'r'); assert_not_nil(report) t = report:read("*all") report:close() @@ -893,7 +893,7 @@ function test_parser() local u = unit.create(f, r, 1) local filename = config.basepath .. "/data/orders.txt" - local file = io.open(filename, "w+") + local file = io.open(filename, "w") assert_not_nil(file) file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('EINHEIT ' .. itoa36(u.id) .. "\n") @@ -956,7 +956,7 @@ function test_bug_1814() local u = unit.create(f, r, 1) local filename = config.basepath .. "/data/1814.txt" - local file = io.open(filename, "w+") + local file = io.open(filename, "w") file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write("; parse error follows: '\n") @@ -977,7 +977,7 @@ function test_bug_1679() local u = unit.create(f, r, 1) local filename = config.basepath .. "/data/1679.txt" - local file = io.open(filename, "w+") + local file = io.open(filename, "w") file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write("NACH W\n") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8421e4692..99c318a95 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,7 +13,7 @@ set (ERESSEA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "Eressea Cor add_subdirectory(bindings) -find_package (Lua51 REQUIRED) +find_package (Lua52 REQUIRED) find_package (ToLua REQUIRED) find_package (LibXml2 REQUIRED) find_package (Curses REQUIRED) diff --git a/src/bindings/CMakeLists.txt b/src/bindings/CMakeLists.txt index 94efddcb2..f3945b4f5 100755 --- a/src/bindings/CMakeLists.txt +++ b/src/bindings/CMakeLists.txt @@ -9,7 +9,7 @@ ENDIF(CMAKE_COMPILER_IS_GNUCC) set (BINDINGS_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "Eressea Lua Bindings") -find_package (Lua51 REQUIRED) +find_package (Lua52 REQUIRED) find_package (ToLua REQUIRED) find_package (LibXml2 REQUIRED) find_package (Curses REQUIRED) diff --git a/src/bindings/bind_building.c b/src/bindings/bind_building.c index 4e51a0a65..230ba5b88 100644 --- a/src/bindings/bind_building.c +++ b/src/bindings/bind_building.c @@ -21,7 +21,6 @@ without prior permission by the authors of Eressea. #include -#include #include int tolua_buildinglist_next(lua_State * L) diff --git a/src/bindings/bind_faction.c b/src/bindings/bind_faction.c index 111b49f37..d5efd226c 100644 --- a/src/bindings/bind_faction.c +++ b/src/bindings/bind_faction.c @@ -29,7 +29,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include int tolua_factionlist_next(lua_State * L) diff --git a/src/bindings/bind_gmtool.c b/src/bindings/bind_gmtool.c index a55cb75b7..654f16f30 100644 --- a/src/bindings/bind_gmtool.c +++ b/src/bindings/bind_gmtool.c @@ -11,7 +11,6 @@ #include #include -#include #include static int tolua_run_mapper(lua_State * L) diff --git a/src/bindings/bind_hashtable.c b/src/bindings/bind_hashtable.c index 2069e272b..17c6c4975 100644 --- a/src/bindings/bind_hashtable.c +++ b/src/bindings/bind_hashtable.c @@ -23,7 +23,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include diff --git a/src/bindings/bind_message.c b/src/bindings/bind_message.c index 6ff30e98a..2959c6c5c 100644 --- a/src/bindings/bind_message.c +++ b/src/bindings/bind_message.c @@ -13,7 +13,6 @@ #include /* lua includes */ -#include #include #include diff --git a/src/bindings/bind_region.c b/src/bindings/bind_region.c index 58f64203c..b5fb16108 100644 --- a/src/bindings/bind_region.c +++ b/src/bindings/bind_region.c @@ -37,7 +37,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include diff --git a/src/bindings/bind_ship.c b/src/bindings/bind_ship.c index d5319dfe0..ddfa883ad 100644 --- a/src/bindings/bind_ship.c +++ b/src/bindings/bind_ship.c @@ -22,7 +22,6 @@ without prior permission by the authors of Eressea. #include -#include #include int tolua_shiplist_next(lua_State * L) diff --git a/src/bindings/bind_sqlite.c b/src/bindings/bind_sqlite.c index 0df9e530f..6f6c5ea29 100644 --- a/src/bindings/bind_sqlite.c +++ b/src/bindings/bind_sqlite.c @@ -16,7 +16,6 @@ without prior permission by the authors of Eressea. #include "bindings.h" #include -#include #include #define LTYPE_DB TOLUA_CAST "db" diff --git a/src/bindings/bind_storage.c b/src/bindings/bind_storage.c index 5e7768d2d..e2b83b670 100644 --- a/src/bindings/bind_storage.c +++ b/src/bindings/bind_storage.c @@ -22,7 +22,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include static int tolua_storage_create(lua_State * L) diff --git a/src/bindings/bind_unit.c b/src/bindings/bind_unit.c index 543a9f9d5..5d4dd9b47 100755 --- a/src/bindings/bind_unit.c +++ b/src/bindings/bind_unit.c @@ -51,7 +51,6 @@ without prior permission by the authors of Eressea. #include #include -#include #include #include diff --git a/src/bindings/bindings.c b/src/bindings/bindings.c index b452eb366..23f90cd8a 100755 --- a/src/bindings/bindings.c +++ b/src/bindings/bindings.c @@ -1182,12 +1182,17 @@ static const struct { static void openlibs(lua_State * L) { - int i; + luaL_openlibs(L); +/* int i, err; for (i = 0; lualibs[i].func; ++i) { lua_pushcfunction(L, lualibs[i].func); lua_pushstring(L, lualibs[i].name); - lua_call(L, 1, 0); + err = lua_pcall(L, 1, 0, 0); + if (err != 0) { + log_lua_error(L); + } } +*/ } void lua_done(lua_State * L) { @@ -1195,7 +1200,7 @@ void lua_done(lua_State * L) { } lua_State *lua_init(void) { - lua_State *L = lua_open(); + lua_State *L = luaL_newstate(); openlibs(L); #ifdef BINDINGS_TOLUA diff --git a/src/bindings/helpers.c b/src/bindings/helpers.c index d6d73d7b2..5658f590c 100644 --- a/src/bindings/helpers.c +++ b/src/bindings/helpers.c @@ -31,8 +31,8 @@ without prior permission by the authors of Eressea. #include -#include #include +#include #include @@ -48,8 +48,7 @@ lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order *o strlcpy(fname, iname, sizeof(fname)); strlcat(fname, "_give", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, s, TOLUA_CAST "unit"); tolua_pushusertype(L, d, TOLUA_CAST "unit"); @@ -81,8 +80,7 @@ static int limit_resource(const region * r, const resource_type * rtype) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_limit", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); @@ -111,8 +109,7 @@ produce_resource(region * r, const resource_type * rtype, int norders) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_produce", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushnumber(L, (lua_Number) norders); @@ -140,8 +137,7 @@ static int lc_age(struct attrib *a) if (fname != NULL) { lua_State *L = (lua_State *) global.vm_state; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); if (fparam) { @@ -201,8 +197,7 @@ static int lua_callspell(castorder * co) fname = fbuf; } - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { int nparam = 4; tolua_pushusertype(L, r, TOLUA_CAST "region"); @@ -254,8 +249,7 @@ static int lua_initfamiliar(unit * u) strlcpy(fname, "initfamiliar_", sizeof(fname)); strlcat(fname, u->race->_name[0], sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); @@ -290,8 +284,7 @@ lua_changeresource(unit * u, const struct resource_type *rtype, int delta) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_changeresource", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) delta); @@ -321,8 +314,7 @@ static int lua_getresource(unit * u, const struct resource_type *rtype) strlcpy(fname, rtype->_name[0], sizeof(fname)); strlcat(fname, "_getresource", sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, u, TOLUA_CAST "unit"); @@ -351,8 +343,7 @@ static bool lua_canuse_item(const unit * u, const struct item_type *itype) lua_State *L = (lua_State *) global.vm_state; const char *fname = "item_canuse"; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushstring(L, itype->rtype->_name[0]); @@ -381,8 +372,7 @@ lua_wage(const region * r, const faction * f, const race * rc, int in_turn) const char *fname = "wage"; int result = -1; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushusertype(L, (void *)f, TOLUA_CAST "faction"); @@ -413,8 +403,7 @@ static void lua_agebuilding(building * b) strlcpy(fname, "age_", sizeof(fname)); strlcat(fname, b->type->_name, sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); @@ -435,8 +424,7 @@ static int lua_building_protection(building * b, unit * u) const char *fname = "building_protection"; int result = 0; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -462,8 +450,7 @@ static double lua_building_taxes(building * b, int level) const char *fname = "building_taxes"; double result = 0.0F; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushnumber(L, level); @@ -489,8 +476,7 @@ static int lua_maintenance(const unit * u) const char *fname = "maintenance"; int result = -1; - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -519,8 +505,7 @@ static int lua_equipmentcallback(const struct equipment *eq, unit * u) strlcpy(fname, "equip_", sizeof(fname)); strlcat(fname, eq->name, sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); @@ -551,8 +536,7 @@ lua_useitem(struct unit *u, const struct item_type *itype, int amount, strlcpy(fname, "use_", sizeof(fname)); strlcat(fname, itype->rtype->_name[0], sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) amount); @@ -582,8 +566,7 @@ static int lua_recruit(struct unit *u, const struct archetype *arch, int amount) strlcpy(fname, "recruit_", sizeof(fname)); strlcat(fname, arch->name[0], sizeof(fname)); - lua_pushstring(L, fname); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, fname); if (lua_isfunction(L, -1)) { tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushnumber(L, (lua_Number) amount); diff --git a/src/util/console.c b/src/util/console.c index 28586f704..0ce3704a8 100644 --- a/src/util/console.c +++ b/src/util/console.c @@ -29,7 +29,7 @@ #include #define default_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) #define lua_saveline(L,idx) \ - if (lua_strlen(L,idx) > 0) /* non-empty line? */ \ + if (lua_rawlen(L,idx) > 0) /* non-empty line? */ \ add_history(lua_tostring(L, idx)); /* add it to history */ #define lua_freeline(L,b) ((void)L, free(b)) #else @@ -109,7 +109,7 @@ static int report(lua_State * L, int status) static int traceback(lua_State * L) { - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + lua_getglobal(L, "debug"); if (!lua_istable(L, -1)) { lua_pop(L, 1); return 1; @@ -149,8 +149,7 @@ static int docall(lua_State * L, int narg, int clear) static const char *get_prompt(lua_State * L, int firstline) { const char *p = NULL; - lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); - lua_rawget(L, LUA_GLOBALSINDEX); + lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2"); p = lua_tostring(L, -1); if (p == NULL) p = (firstline ? PROMPT : PROMPT2); @@ -195,7 +194,7 @@ static int loadline(lua_State * L) if (!pushline(L, 1)) return -1; /* no input */ for (;;) { /* repeat until gets a complete line */ - status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin"); + status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_rawlen(L, 1), "=stdin"); if (!incomplete(L, status)) break; /* cannot try to add lines? */ if (!pushline(L, 0)) /* no more input? */