feat(util/lua): add functions for simulating python-like named arguments using Lua tables
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
850ec69538
commit
f568ed97b8
2 changed files with 80 additions and 0 deletions
|
@ -10,6 +10,7 @@ Author: Leonardo de Moura
|
|||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include "util/lua.h"
|
||||
#include "util/script_exception.h"
|
||||
#include "util/debug.h"
|
||||
|
@ -194,4 +195,76 @@ lua_migrate_fn get_migrate_fn(lua_State * L, int i) {
|
|||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool get_bool_named_param(lua_State * L, int idx, char const * opt_name, bool def_value) {
|
||||
if (lua_istable(L, idx)) {
|
||||
bool result = def_value;
|
||||
push_string(L, opt_name);
|
||||
lua_gettable(L, idx);
|
||||
if (lua_isboolean(L, -1)) {
|
||||
result = lua_toboolean(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return result;
|
||||
} else if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
return result;
|
||||
} else {
|
||||
lua_pop(L, 1);
|
||||
throw exception(sstream() << "field '" << opt_name << "' must be a Boolean in table at arg #" << idx);
|
||||
}
|
||||
} else if (idx <= lua_gettop(L) && !lua_isnil(L, idx)) {
|
||||
throw exception(sstream() << "arg #" << idx << " must be a table");
|
||||
} else {
|
||||
return def_value;
|
||||
}
|
||||
}
|
||||
|
||||
int get_int_named_param(lua_State * L, int idx, char const * opt_name, int def_value) {
|
||||
if (lua_istable(L, idx)) {
|
||||
int result = def_value;
|
||||
push_string(L, opt_name);
|
||||
lua_gettable(L, idx);
|
||||
if (lua_isnumber(L, -1)) {
|
||||
result = lua_tointeger(L, -1);
|
||||
lua_pop(L, 1);
|
||||
return result;
|
||||
} else if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
return result;
|
||||
} else {
|
||||
lua_pop(L, 1);
|
||||
throw exception(sstream() << "field '" << opt_name << "' must be an integer in table at arg #" << idx);
|
||||
}
|
||||
} else if (idx <= lua_gettop(L) && !lua_isnil(L, idx)) {
|
||||
throw exception(sstream() << "arg #" << idx << " must be a table");
|
||||
} else {
|
||||
return def_value;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned get_uint_named_param(lua_State * L, int idx, char const * opt_name, unsigned def_value) {
|
||||
if (lua_istable(L, idx)) {
|
||||
long result = def_value;
|
||||
push_string(L, opt_name);
|
||||
lua_gettable(L, idx);
|
||||
if (lua_isnumber(L, -1)) {
|
||||
long result = lua_tointeger(L, -1);
|
||||
lua_pop(L, 1);
|
||||
if (result < 0 || result > std::numeric_limits<unsigned>::max())
|
||||
throw exception(sstream() << "field '" << opt_name << "' must be a unsigned integer in table at arg #" << idx);
|
||||
return static_cast<unsigned>(result);
|
||||
} else if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
return static_cast<unsigned>(result);
|
||||
} else {
|
||||
lua_pop(L, 1);
|
||||
throw exception(sstream() << "field '" << opt_name << "' must be an integer in table at arg #" << idx);
|
||||
}
|
||||
} else if (idx <= lua_gettop(L) && !lua_isnil(L, idx)) {
|
||||
throw exception(sstream() << "arg #" << idx << " must be a table");
|
||||
} else {
|
||||
return def_value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -139,4 +139,11 @@ inline int push_number(lua_State * L, lua_Number v) { lua_pushnumber(L, v); retu
|
|||
inline int push_nil(lua_State * L) { lua_pushnil(L); return 1; }
|
||||
// =======================================
|
||||
|
||||
// =======================================
|
||||
// Utilities for simulating Python-like named parameters using Lua tables.
|
||||
// In the following function \c idx is the position of the Lua table on the Lua stack.
|
||||
bool get_bool_named_param(lua_State * L, int idx, char const * opt_name, bool def_value);
|
||||
int get_int_named_param(lua_State * L, int idx, char const * opt_name, int def_value);
|
||||
unsigned get_uint_named_param(lua_State * L, int idx, char const * opt_name, unsigned def_value);
|
||||
// =======================================
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue