lua 5.2 compat changes

This commit is contained in:
Enno Rehling 2012-07-01 19:17:47 +02:00
parent c1a6d4ac03
commit 3cee3d1415
16 changed files with 37 additions and 60 deletions

View File

@ -594,7 +594,7 @@ function test_guard_resources()
end end
local function is_flag_set(flags, flag) 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 end
function test_hero_hero_transfer() function test_hero_hero_transfer()
@ -803,7 +803,7 @@ end
local function find_in_report(f, pattern, extension) local function find_in_report(f, pattern, extension)
extension = extension or "nr" extension = extension or "nr"
local filename = config.reportpath .. "/" .. get_turn() .. "-" .. itoa36(f.id) .. "." .. extension 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) assert_not_nil(report)
t = report:read("*all") t = report:read("*all")
report:close() report:close()
@ -893,7 +893,7 @@ function test_parser()
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)
local filename = config.basepath .. "/data/orders.txt" local filename = config.basepath .. "/data/orders.txt"
local file = io.open(filename, "w+") local file = io.open(filename, "w")
assert_not_nil(file) assert_not_nil(file)
file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n') file:write('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n')
file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write('EINHEIT ' .. itoa36(u.id) .. "\n")
@ -956,7 +956,7 @@ function test_bug_1814()
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)
local filename = config.basepath .. "/data/1814.txt" 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('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n')
file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write('EINHEIT ' .. itoa36(u.id) .. "\n")
file:write("; parse error follows: '\n") file:write("; parse error follows: '\n")
@ -977,7 +977,7 @@ function test_bug_1679()
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)
local filename = config.basepath .. "/data/1679.txt" 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('ERESSEA ' .. itoa36(f.id) .. ' "' .. f.password .. '"\n')
file:write('EINHEIT ' .. itoa36(u.id) .. "\n") file:write('EINHEIT ' .. itoa36(u.id) .. "\n")
file:write("NACH W\n") file:write("NACH W\n")

View File

@ -13,7 +13,7 @@ set (ERESSEA_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "Eressea Cor
add_subdirectory(bindings) add_subdirectory(bindings)
find_package (Lua51 REQUIRED) find_package (Lua52 REQUIRED)
find_package (ToLua REQUIRED) find_package (ToLua REQUIRED)
find_package (LibXml2 REQUIRED) find_package (LibXml2 REQUIRED)
find_package (Curses REQUIRED) find_package (Curses REQUIRED)

View File

@ -9,7 +9,7 @@ ENDIF(CMAKE_COMPILER_IS_GNUCC)
set (BINDINGS_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "Eressea Lua Bindings") set (BINDINGS_LIBRARY ${PROJECT_NAME} CACHE INTERNAL "Eressea Lua Bindings")
find_package (Lua51 REQUIRED) find_package (Lua52 REQUIRED)
find_package (ToLua REQUIRED) find_package (ToLua REQUIRED)
find_package (LibXml2 REQUIRED) find_package (LibXml2 REQUIRED)
find_package (Curses REQUIRED) find_package (Curses REQUIRED)

View File

@ -21,7 +21,6 @@ without prior permission by the authors of Eressea.
#include <util/language.h> #include <util/language.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
int tolua_buildinglist_next(lua_State * L) int tolua_buildinglist_next(lua_State * L)

View File

@ -29,7 +29,6 @@ without prior permission by the authors of Eressea.
#include <util/log.h> #include <util/log.h>
#include <quicklist.h> #include <quicklist.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
int tolua_factionlist_next(lua_State * L) int tolua_factionlist_next(lua_State * L)

View File

@ -11,7 +11,6 @@
#include <modules/autoseed.h> #include <modules/autoseed.h>
#include <util/log.h> #include <util/log.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
static int tolua_run_mapper(lua_State * L) static int tolua_run_mapper(lua_State * L)

View File

@ -23,7 +23,6 @@ without prior permission by the authors of Eressea.
#include <util/variant.h> #include <util/variant.h>
#include <util/attrib.h> #include <util/attrib.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
#include <assert.h> #include <assert.h>

View File

@ -13,7 +13,6 @@
#include <util/message.h> #include <util/message.h>
/* lua includes */ /* lua includes */
#include <lua.h>
#include <tolua.h> #include <tolua.h>
#include <assert.h> #include <assert.h>

View File

@ -37,7 +37,6 @@ without prior permission by the authors of Eressea.
#include <util/language.h> #include <util/language.h>
#include <util/log.h> #include <util/log.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
#include <assert.h> #include <assert.h>

View File

@ -22,7 +22,6 @@ without prior permission by the authors of Eressea.
#include <util/language.h> #include <util/language.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
int tolua_shiplist_next(lua_State * L) int tolua_shiplist_next(lua_State * L)

View File

@ -16,7 +16,6 @@ without prior permission by the authors of Eressea.
#include "bindings.h" #include "bindings.h"
#include <sqlite3.h> #include <sqlite3.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
#define LTYPE_DB TOLUA_CAST "db" #define LTYPE_DB TOLUA_CAST "db"

View File

@ -22,7 +22,6 @@ without prior permission by the authors of Eressea.
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
static int tolua_storage_create(lua_State * L) static int tolua_storage_create(lua_State * L)

View File

@ -51,7 +51,6 @@ without prior permission by the authors of Eressea.
#include <util/log.h> #include <util/log.h>
#include <quicklist.h> #include <quicklist.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
#include <assert.h> #include <assert.h>

View File

@ -1182,12 +1182,17 @@ static const struct {
static void openlibs(lua_State * L) static void openlibs(lua_State * L)
{ {
int i; luaL_openlibs(L);
/* int i, err;
for (i = 0; lualibs[i].func; ++i) { for (i = 0; lualibs[i].func; ++i) {
lua_pushcfunction(L, lualibs[i].func); lua_pushcfunction(L, lualibs[i].func);
lua_pushstring(L, lualibs[i].name); 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) { void lua_done(lua_State * L) {
@ -1195,7 +1200,7 @@ void lua_done(lua_State * L) {
} }
lua_State *lua_init(void) { lua_State *lua_init(void) {
lua_State *L = lua_open(); lua_State *L = luaL_newstate();
openlibs(L); openlibs(L);
#ifdef BINDINGS_TOLUA #ifdef BINDINGS_TOLUA

View File

@ -31,8 +31,8 @@ without prior permission by the authors of Eressea.
#include <gamecode/archetype.h> #include <gamecode/archetype.h>
#include <lua.h>
#include <tolua.h> #include <tolua.h>
#include <lua.h>
#include <assert.h> #include <assert.h>
@ -48,8 +48,7 @@ lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order *o
strlcpy(fname, iname, sizeof(fname)); strlcpy(fname, iname, sizeof(fname));
strlcat(fname, "_give", sizeof(fname)); strlcat(fname, "_give", sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, s, TOLUA_CAST "unit"); tolua_pushusertype(L, s, TOLUA_CAST "unit");
tolua_pushusertype(L, d, 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)); strlcpy(fname, rtype->_name[0], sizeof(fname));
strlcat(fname, "_limit", sizeof(fname)); strlcat(fname, "_limit", sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); 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)); strlcpy(fname, rtype->_name[0], sizeof(fname));
strlcat(fname, "_produce", sizeof(fname)); strlcat(fname, "_produce", sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushusertype(L, (void *)r, TOLUA_CAST "region");
tolua_pushnumber(L, (lua_Number) norders); tolua_pushnumber(L, (lua_Number) norders);
@ -140,8 +137,7 @@ static int lc_age(struct attrib *a)
if (fname != NULL) { if (fname != NULL) {
lua_State *L = (lua_State *) global.vm_state; lua_State *L = (lua_State *) global.vm_state;
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushusertype(L, (void *)b, TOLUA_CAST "building");
if (fparam) { if (fparam) {
@ -201,8 +197,7 @@ static int lua_callspell(castorder * co)
fname = fbuf; fname = fbuf;
} }
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
int nparam = 4; int nparam = 4;
tolua_pushusertype(L, r, TOLUA_CAST "region"); tolua_pushusertype(L, r, TOLUA_CAST "region");
@ -254,8 +249,7 @@ static int lua_initfamiliar(unit * u)
strlcpy(fname, "initfamiliar_", sizeof(fname)); strlcpy(fname, "initfamiliar_", sizeof(fname));
strlcat(fname, u->race->_name[0], sizeof(fname)); strlcat(fname, u->race->_name[0], sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, u, TOLUA_CAST "unit"); 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)); strlcpy(fname, rtype->_name[0], sizeof(fname));
strlcat(fname, "_changeresource", sizeof(fname)); strlcat(fname, "_changeresource", sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, u, TOLUA_CAST "unit"); tolua_pushusertype(L, u, TOLUA_CAST "unit");
tolua_pushnumber(L, (lua_Number) delta); 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)); strlcpy(fname, rtype->_name[0], sizeof(fname));
strlcat(fname, "_getresource", sizeof(fname)); strlcat(fname, "_getresource", sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, u, TOLUA_CAST "unit"); 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; lua_State *L = (lua_State *) global.vm_state;
const char *fname = "item_canuse"; const char *fname = "item_canuse";
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
tolua_pushstring(L, itype->rtype->_name[0]); 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"; const char *fname = "wage";
int result = -1; int result = -1;
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region"); tolua_pushusertype(L, (void *)r, TOLUA_CAST "region");
tolua_pushusertype(L, (void *)f, TOLUA_CAST "faction"); tolua_pushusertype(L, (void *)f, TOLUA_CAST "faction");
@ -413,8 +403,7 @@ static void lua_agebuilding(building * b)
strlcpy(fname, "age_", sizeof(fname)); strlcpy(fname, "age_", sizeof(fname));
strlcat(fname, b->type->_name, sizeof(fname)); strlcat(fname, b->type->_name, sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); 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"; const char *fname = "building_protection";
int result = 0; int result = 0;
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushusertype(L, (void *)b, TOLUA_CAST "building");
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); 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"; const char *fname = "building_taxes";
double result = 0.0F; double result = 0.0F;
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)b, TOLUA_CAST "building"); tolua_pushusertype(L, (void *)b, TOLUA_CAST "building");
tolua_pushnumber(L, level); tolua_pushnumber(L, level);
@ -489,8 +476,7 @@ static int lua_maintenance(const unit * u)
const char *fname = "maintenance"; const char *fname = "maintenance";
int result = -1; int result = -1;
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); 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)); strlcpy(fname, "equip_", sizeof(fname));
strlcat(fname, eq->name, sizeof(fname)); strlcat(fname, eq->name, sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); 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)); strlcpy(fname, "use_", sizeof(fname));
strlcat(fname, itype->rtype->_name[0], sizeof(fname)); strlcat(fname, itype->rtype->_name[0], sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
tolua_pushnumber(L, (lua_Number) amount); 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)); strlcpy(fname, "recruit_", sizeof(fname));
strlcat(fname, arch->name[0], sizeof(fname)); strlcat(fname, arch->name[0], sizeof(fname));
lua_pushstring(L, fname); lua_getglobal(L, fname);
lua_rawget(L, LUA_GLOBALSINDEX);
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1)) {
tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit"); tolua_pushusertype(L, (void *)u, TOLUA_CAST "unit");
tolua_pushnumber(L, (lua_Number) amount); tolua_pushnumber(L, (lua_Number) amount);

View File

@ -29,7 +29,7 @@
#include <readline/history.h> #include <readline/history.h>
#define default_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL) #define default_readline(L,b,p) ((void)L, ((b)=readline(p)) != NULL)
#define lua_saveline(L,idx) \ #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 */ add_history(lua_tostring(L, idx)); /* add it to history */
#define lua_freeline(L,b) ((void)L, free(b)) #define lua_freeline(L,b) ((void)L, free(b))
#else #else
@ -109,7 +109,7 @@ static int report(lua_State * L, int status)
static int traceback(lua_State * L) static int traceback(lua_State * L)
{ {
lua_getfield(L, LUA_GLOBALSINDEX, "debug"); lua_getglobal(L, "debug");
if (!lua_istable(L, -1)) { if (!lua_istable(L, -1)) {
lua_pop(L, 1); lua_pop(L, 1);
return 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) static const char *get_prompt(lua_State * L, int firstline)
{ {
const char *p = NULL; const char *p = NULL;
lua_pushstring(L, firstline ? "_PROMPT" : "_PROMPT2"); lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
lua_rawget(L, LUA_GLOBALSINDEX);
p = lua_tostring(L, -1); p = lua_tostring(L, -1);
if (p == NULL) if (p == NULL)
p = (firstline ? PROMPT : PROMPT2); p = (firstline ? PROMPT : PROMPT2);
@ -195,7 +194,7 @@ static int loadline(lua_State * L)
if (!pushline(L, 1)) if (!pushline(L, 1))
return -1; /* no input */ return -1; /* no input */
for (;;) { /* repeat until gets a complete line */ 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)) if (!incomplete(L, status))
break; /* cannot try to add lines? */ break; /* cannot try to add lines? */
if (!pushline(L, 0)) /* no more input? */ if (!pushline(L, 0)) /* no more input? */