Skip to content

Commit

Permalink
Modernize luaW_getglobal
Browse files Browse the repository at this point in the history
The variadic form now uses a variadic template, and a vector form has been added.
Since the nullptr sentinel argument is no longer required, it has been removed from all calls.
  • Loading branch information
CelticMinstrel committed Mar 31, 2016
1 parent 86b4680 commit 4c45683
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/ai/lua/core.cpp
Expand Up @@ -820,7 +820,7 @@ static int impl_ai_aspect_get(lua_State* L)
(void) aspect;
} else if(typesafe_aspect<unit_advancements_aspect>* aspect = try_aspect_as<unit_advancements_aspect>(iter->second)) {
const unit_advancements_aspect& val = aspect->get();
int my_side = luaW_getglobal(L, "ai", "side", nullptr) - 1;
int my_side = luaW_getglobal(L, "ai", "side") - 1;
lua_newtable(L);
for (unit_map::const_iterator u = resources::units->begin(); u != resources::units->end(); ++u) {
if (!u.valid() || u->side() != my_side) {
Expand Down
22 changes: 11 additions & 11 deletions src/scripting/game_lua_kernel.cpp
Expand Up @@ -4075,7 +4075,7 @@ namespace {
{
lua_State *L = mState;
config cfg;
if (!luaW_getglobal(L, "wesnoth", "theme_items", name.c_str(), nullptr))
if (!luaW_getglobal(L, "wesnoth", "theme_items", name))
return cfg;
if (!luaW_pcall(L, 0, 1)) return cfg;
luaW_toconfig(L, -1, cfg);
Expand Down Expand Up @@ -4546,7 +4546,7 @@ game_lua_kernel::game_lua_kernel(CVideo * video, game_state & gs, play_controlle
{
set_wml_action(handler.first, handler.second);
}
luaW_getglobal(L, "wesnoth", "effects", nullptr);
luaW_getglobal(L, "wesnoth", "effects");
BOOST_FOREACH(const std::string& effect, unit::builtin_effects) {
lua_pushstring(L, effect.c_str());
push_builtin_effect();
Expand Down Expand Up @@ -4678,7 +4678,7 @@ void game_lua_kernel::load_game(const config& level)
{
lua_State *L = mState;

if (!luaW_getglobal(L, "wesnoth", "game_events", "on_load", nullptr))
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_load"))
return;

lua_newtable(L);
Expand All @@ -4705,7 +4705,7 @@ void game_lua_kernel::save_game(config &cfg)
{
lua_State *L = mState;

if (!luaW_getglobal(L, "wesnoth", "game_events", "on_save", nullptr))
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_save"))
return;

if (!luaW_pcall(L, 0, 1, false))
Expand Down Expand Up @@ -4743,7 +4743,7 @@ bool game_lua_kernel::run_event(game_events::queued_event const &ev)
{
lua_State *L = mState;

if (!luaW_getglobal(L, "wesnoth", "game_events", "on_event", nullptr))
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_event"))
return false;

queued_event_context dummy(&ev, queued_events_);
Expand Down Expand Up @@ -4847,7 +4847,7 @@ bool game_lua_kernel::run_wml_action(std::string const &cmd, vconfig const &cfg,
lua_State *L = mState;


if (!luaW_getglobal(L, "wesnoth", "wml_actions", cmd.c_str(), nullptr))
if (!luaW_getglobal(L, "wesnoth", "wml_actions", cmd))
return false;

queued_event_context dummy(&ev, queued_events_);
Expand All @@ -4868,7 +4868,7 @@ bool game_lua_kernel::run_wml_conditional(std::string const &cmd, vconfig const
lua_State *L = mState;


if (!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd.c_str(), nullptr)) {
if (!luaW_getglobal(L, "wesnoth", "wml_conditionals", cmd)) {
std::string err_msg = "unknown conditional wml: [";
err_msg += cmd;
err_msg += "]";
Expand Down Expand Up @@ -4896,7 +4896,7 @@ bool game_lua_kernel::run_filter(char const *name, unit const &u)
if (!ui.valid()) return false;

// Get the user filter by name.
if(!luaW_getglobal(L, name, nullptr))
if(!luaW_getglobal(L, name))
{
std::string message = std::string() + "function " + name + " not found";
log_error(message.c_str(), "Lua SUF Error");
Expand Down Expand Up @@ -4928,7 +4928,7 @@ std::string game_lua_kernel::apply_effect(const std::string& name, unit& u, cons
// (Note: The unit needs to be on the stack twice to prevent untimely GC.)
luaW_pushconfig(L, cfg);
// Stack: unit, cfg
if(luaW_getglobal(L, "wesnoth", "effects", name.c_str(), nullptr)) {
if(luaW_getglobal(L, "wesnoth", "effects", name)) {
map_locker(this);
// Stack: unit, cfg, effect
if(lua_istable(L, -1)) {
Expand Down Expand Up @@ -4997,7 +4997,7 @@ void game_lua_kernel::mouse_over_hex_callback(const map_location& loc)
{
lua_State *L = mState;

if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_move", nullptr)) {
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_move")) {
return;
}
lua_push(L, loc.x + 1);
Expand All @@ -5010,7 +5010,7 @@ void game_lua_kernel::select_hex_callback(const map_location& loc)
{
lua_State *L = mState;

if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_action", nullptr)) {
if (!luaW_getglobal(L, "wesnoth", "game_events", "on_mouse_action")) {
return;
}
lua_push(L, loc.x + 1);
Expand Down
14 changes: 3 additions & 11 deletions src/scripting/lua_common.cpp
Expand Up @@ -790,30 +790,22 @@ vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing)
return result;
}


#ifdef __GNUC__
__attribute__((sentinel))
#endif
bool luaW_getglobal(lua_State *L, ...)
bool luaW_getglobal(lua_State *L, const std::vector<std::string>& path)
{
lua_pushglobaltable(L);
va_list ap;
va_start(ap, L);
while (const char *s = va_arg(ap, const char *))
for (const std::string& s : path)
{
if (!lua_istable(L, -1)) goto discard;
lua_pushstring(L, s);
lua_pushlstring(L, s.c_str(), s.size());
lua_rawget(L, -2);
lua_remove(L, -2);
}

if (lua_isnil(L, -1)) {
discard:
va_end(ap);
lua_pop(L, 1);
return false;
}
va_end(ap);
return true;
}

Expand Down
13 changes: 12 additions & 1 deletion src/scripting/lua_common.hpp
Expand Up @@ -28,6 +28,7 @@ class vconfig;
#include "scripting/lua_types.hpp"
#include "variable_info.hpp"
#include "map/location.hpp"
#include <vector>

namespace lua_common {
int intf_textdomain(lua_State *L);
Expand Down Expand Up @@ -139,7 +140,17 @@ vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing = false);
* value is not nil.
* @return true if an element was pushed.
*/
bool luaW_getglobal(lua_State *L, ...);
bool luaW_getglobal(lua_State *L, const std::vector<std::string>& path);

/**
* Pushes the value found by following the variadic names (char *), if the
* value is not nil.
* @return true if an element was pushed.
*/
template<typename... T>
bool luaW_getglobal(lua_State *L, T... path) {
return luaW_getglobal(L, std::vector<std::string> {path...} );
}

bool luaW_toboolean(lua_State *L, int n);

Expand Down

0 comments on commit 4c45683

Please sign in to comment.