From 411f4d9954b218a49ffc7fab85de2be220e80ad1 Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Fri, 7 Apr 2017 13:10:07 -0400 Subject: [PATCH] Lua: Add float mutator macros and some fixups/cleanup to other mutator/accessor macros - Enclose all macro arguments in parentheses, except those meant to be statements - Use pushlstring to allow strings with nulls - New float mutators --- src/scripting/game_lua_kernel.cpp | 4 +- src/scripting/lua_common.hpp | 81 +++++++++++++++++++------------ src/scripting/lua_unit.cpp | 4 +- 3 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index a4f76a32a0041..d0c919110e4b8 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -1282,8 +1282,8 @@ int game_lua_kernel::impl_game_config_set(lua_State *L) modify_int_attrib("kill_experience", game_config::kill_experience = value); modify_int_attrib("last_turn", tod_man().set_number_of_turns_by_wml(value)); modify_string_attrib("next_scenario", gamedata().set_next_scenario(value)); - modify_vector_string_attrib("defeat_music", gamedata().set_defeat_music(std::move(vector))); - modify_vector_string_attrib("victory_music", gamedata().set_victory_music(std::move(vector))); + modify_vector_string_attrib("defeat_music", gamedata().set_defeat_music(std::move(value))); + modify_vector_string_attrib("victory_music", gamedata().set_victory_music(std::move(value))); std::string err_msg = "unknown modifiable property of game_config: "; err_msg += m; return luaL_argerror(L, 2, err_msg.c_str()); diff --git a/src/scripting/lua_common.hpp b/src/scripting/lua_common.hpp index 6ca3dce51c550..3e68ddfddd393 100644 --- a/src/scripting/lua_common.hpp +++ b/src/scripting/lua_common.hpp @@ -187,59 +187,63 @@ int luaW_pcall_internal(lua_State *L, int nArgs, int nRets); int luaW_type_error (lua_State *L, int narg, const char *tname); #define return_tstring_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - luaW_pushtstring(L, accessor); \ + if (strcmp(m, (name)) == 0) { \ + luaW_pushtstring(L, (accessor)); \ return 1; \ } #define return_cstring_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - lua_pushstring(L, accessor); \ + if (strcmp(m, (name)) == 0) { \ + lua_pushstring(L, (accessor)); \ return 1; \ } #define return_string_attrib(name, accessor) \ - return_cstring_attrib(name, (accessor).c_str()) + if (strcmp(m, (name)) == 0) { \ + const std::string& str = (accessor); \ + lua_pushlstring(L, str.c_str(), str.length()); \ + return 1; \ + } #define return_int_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - lua_pushinteger(L, accessor); \ + if (strcmp(m, (name)) == 0) { \ + lua_pushinteger(L, (accessor)); \ return 1; \ } #define return_float_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - lua_pushnumber(L, accessor); \ + if (strcmp(m, (name)) == 0) { \ + lua_pushnumber(L, (accessor)); \ return 1; \ } #define return_bool_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - lua_pushboolean(L, accessor); \ + if (strcmp(m, (name)) == 0) { \ + lua_pushboolean(L, (accessor)); \ return 1; \ } #define return_cfg_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ + if (strcmp(m, (name)) == 0) { \ config cfg; \ - accessor; \ + {accessor;} \ luaW_pushconfig(L, cfg); \ return 1; \ } #define return_cfgref_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - luaW_pushconfig(L, accessor); \ + if (strcmp(m, (name)) == 0) { \ + luaW_pushconfig(L, (accessor)); \ return 1; \ } #define return_vector_string_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ - const std::vector& vector = accessor; \ + if (strcmp(m, (name)) == 0) { \ + const std::vector& vector = (accessor); \ lua_createtable(L, vector.size(), 0); \ int i = 1; \ for (const std::string& s : vector) { \ - lua_pushstring(L, s.c_str()); \ + lua_pushlstring(L, s.c_str(), s.length()); \ lua_rawseti(L, -2, i); \ ++i; \ } \ @@ -247,44 +251,59 @@ int luaW_type_error (lua_State *L, int narg, const char *tname); } #define modify_tstring_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ + if (strcmp(m, (name)) == 0) { \ t_string value = luaW_checktstring(L, 3); \ - accessor; \ + {accessor;} \ return 0; \ } #define modify_string_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ + if (strcmp(m, (name)) == 0) { \ const char *value = luaL_checkstring(L, 3); \ - accessor; \ + {accessor;} \ return 0; \ } #define modify_int_attrib(name, accessor) \ - if (strcmp(m, name) == 0) { \ + if (strcmp(m, (name)) == 0) { \ int value = static_cast(luaL_checknumber(L, 3)); \ - accessor; \ + {accessor;} \ return 0; \ } #define modify_int_attrib_check_range(name, accessor, allowed_min, allowed_max) \ - if (strcmp(m, name) == 0) { \ + if (strcmp(m, (name)) == 0) { \ int value = static_cast(luaL_checknumber(L, 3)); \ - if (value < allowed_min || allowed_max < value) return luaL_argerror(L, 3, "out of bounds"); \ - accessor; \ + if (value < (allowed_min) || (allowed_max) < value) return luaL_argerror(L, 3, "out of bounds"); \ + {accessor;} \ + return 0; \ + } + +#define modify_float_attrib(name, accessor) \ + if (strcmp(m, (name)) == 0) { \ + lua_Number value = luaL_checknumber(L, 3); \ + {accessor;} \ + return 0; \ + } + +#define modify_float_attrib_check_range(name, accessor, allowed_min, allowed_max) \ + if (strcmp(m, (name)) == 0) { \ + lua_Number value = luaL_checknumber(L, 3); \ + if (value < (allowed_min) || (allowed_max) < value) return luaL_argerror(L, 3, "out of bounds"); \ + {accessor;} \ return 0; \ } #define modify_bool_attrib(name, accessor) \ if (strcmp(m, name) == 0) { \ bool value = luaW_toboolean(L, 3); \ - accessor; \ + {accessor;} \ return 0; \ } #define modify_vector_string_attrib(name, accessor) \ if (strcmp(m, name) == 0) { \ - std::vector vector; \ + std::vector value; \ char const* message = "table with unnamed indices holding strings expected"; \ if (!lua_istable(L, 3)) return luaL_argerror(L, 3, message); \ unsigned length = lua_rawlen(L, 3); \ @@ -292,10 +311,10 @@ int luaW_type_error (lua_State *L, int narg, const char *tname); lua_rawgeti(L, 3, i); \ char const* string = lua_tostring(L, 4); \ if(!string) return luaL_argerror(L, 2 + i, message); \ - vector.push_back(string); \ + value.push_back(string); \ lua_pop(L, 1); \ } \ - accessor; \ + {accessor;} \ return 0; \ } diff --git a/src/scripting/lua_unit.cpp b/src/scripting/lua_unit.cpp index 76adc01dcb509..5c9737775c848 100644 --- a/src/scripting/lua_unit.cpp +++ b/src/scripting/lua_unit.cpp @@ -393,8 +393,8 @@ static int impl_unit_set(lua_State *L) modify_bool_attrib("zoc", u.set_emit_zoc(value)); modify_bool_attrib("canrecruit", u.set_can_recruit(value)); - modify_vector_string_attrib("extra_recruit", u.set_recruits(vector)); - modify_vector_string_attrib("advances_to", u.set_advances_to(vector)); + modify_vector_string_attrib("extra_recruit", u.set_recruits(value)); + modify_vector_string_attrib("advances_to", u.set_advances_to(value)); if(strcmp(m, "alignment") == 0) { u.set_alignment(lua_check(L, 3)); return 0;