refactor(*): add pushinteger and pushnumeral inline functions

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-04-29 14:47:08 -07:00
parent 412a3797f4
commit 097f562016
9 changed files with 26 additions and 57 deletions

View file

@ -69,8 +69,7 @@ bool is_exception(lua_State * L, int i) {
}
static int exception_what(lua_State * L) {
lua_pushstring(L, to_exception(L, 1).what());
return 1;
return pushstring(L, to_exception(L, 1).what());
}
static int exception_rethrow(lua_State * L) {

View file

@ -133,6 +133,9 @@ lua_migrate_fn get_migrate_fn(lua_State * src, int i);
// =======================================
// Useful macros
inline int pushboolean(lua_State * L, bool b) { lua_pushboolean(L, b); return 1; }
inline int pushstring(lua_State * L, char const * s) { lua_pushstring(L, s); return 1; }
inline int pushinteger(lua_State * L, lua_Integer v) { lua_pushinteger(L, v); return 1; }
inline int pushnumber(lua_State * L, lua_Number v) { lua_pushnumber(L, v); return 1; }
// =======================================
}

View file

@ -472,26 +472,11 @@ name to_name_ext(lua_State * L, int idx) {
}
}
static int name_tostring(lua_State * L) {
lua_pushstring(L, to_name(L, 1).to_string().c_str());
return 1;
}
static int name_eq(lua_State * L) {
return pushboolean(L, to_name(L, 1) == to_name(L, 2));
}
static int name_lt(lua_State * L) {
return pushboolean(L, to_name(L, 1) < to_name(L, 2));
}
static int name_hash(lua_State * L) {
lua_pushinteger(L, to_name(L, 1).hash());
return 1;
}
static int name_tostring(lua_State * L) { return pushstring(L, to_name(L, 1).to_string().c_str()); }
static int name_eq(lua_State * L) { return pushboolean(L, to_name(L, 1) == to_name(L, 2)); }
static int name_lt(lua_State * L) { return pushboolean(L, to_name(L, 1) < to_name(L, 2)); }
static int name_hash(lua_State * L) { return pushinteger(L, to_name(L, 1).hash()); }
#define NAME_PRED(P) static int name_ ## P(lua_State * L) { return pushboolean(L, to_name(L, 1).P()); }
NAME_PRED(is_atomic)
NAME_PRED(is_anonymous)
NAME_PRED(is_string)
@ -506,15 +491,13 @@ static int name_get_prefix(lua_State * L) {
static int name_get_numeral(lua_State * L) {
if (!to_name(L, 1).is_numeral())
throw exception("invalid get_numeral, hierarchical name with numeric head expected");
lua_pushinteger(L, to_name(L, 1).get_numeral());
return 1;
return pushinteger(L, to_name(L, 1).get_numeral());
}
static int name_get_string(lua_State * L) {
if (!to_name(L, 1).is_string())
throw exception("invalid get_string, hierarchical name with string head expected");
lua_pushstring(L, to_name(L, 1).get_string());
return 1;
return pushstring(L, to_name(L, 1).get_string());
}
static const struct luaL_Reg name_m[] = {

View file

@ -173,8 +173,7 @@ static int mpq_tostring(lua_State * L) {
mpq * n = static_cast<mpq*>(luaL_checkudata(L, 1, mpq_mt));
std::ostringstream out;
out << *n;
lua_pushstring(L, out.str().c_str());
return 1;
return pushstring(L, out.str().c_str());
}
static int mpq_eq(lua_State * L) {

View file

@ -121,8 +121,7 @@ static int mpz_tostring(lua_State * L) {
mpz * n = static_cast<mpz*>(luaL_checkudata(L, 1, mpz_mt));
std::ostringstream out;
out << *n;
lua_pushstring(L, out.str().c_str());
return 1;
return pushstring(L, out.str().c_str());
}
static int mpz_eq(lua_State * L) {

View file

@ -18,8 +18,7 @@ static int mk_lua_rb_map(lua_State * L) {
}
static int lua_rb_map_size(lua_State * L) {
lua_pushinteger(L, to_lua_rb_map(L, 1).size());
return 1;
return pushinteger(L, to_lua_rb_map(L, 1).size());
}
static int lua_rb_map_contains(lua_State * L) {

View file

@ -416,8 +416,7 @@ format to_format_elem(lua_State * L, int idx) {
static int format_tostring(lua_State * L) {
std::ostringstream out;
out << mk_pair(to_format(L, 1), get_global_options(L));
lua_pushstring(L, out.str().c_str());
return 1;
return pushstring(L, out.str().c_str());
}
static int format_concat(lua_State * L) {

View file

@ -231,13 +231,11 @@ static int mk_options(lua_State * L) {
static int options_tostring(lua_State * L) {
std::ostringstream out;
out << to_options(L, 1);
lua_pushstring(L, out.str().c_str());
return 1;
return pushstring(L, out.str().c_str());
}
static int options_size(lua_State * L) {
lua_pushinteger(L, to_options(L, 1).size());
return 1;
return pushinteger(L, to_options(L, 1).size());
}
static int options_contains(lua_State * L) {
@ -257,29 +255,25 @@ static int options_get_bool(lua_State * L) {
static int options_get_int(lua_State * L) {
int nargs = lua_gettop(L);
int defval = nargs < 3 ? 0 : lua_tointeger(L, 3);
lua_pushinteger(L, to_options(L, 1).get_int(to_name_ext(L, 2), defval));
return 1;
return pushinteger(L, to_options(L, 1).get_int(to_name_ext(L, 2), defval));
}
static int options_get_unsigned(lua_State * L) {
int nargs = lua_gettop(L);
unsigned defval = nargs < 3 ? 0 : lua_tointeger(L, 3);
lua_pushnumber(L, to_options(L, 1).get_unsigned(to_name_ext(L, 2), defval));
return 1;
return pushnumber(L, to_options(L, 1).get_unsigned(to_name_ext(L, 2), defval));
}
static int options_get_double(lua_State * L) {
int nargs = lua_gettop(L);
double defval = nargs < 3 ? 0.0 : lua_tonumber(L, 3);
lua_pushnumber(L, to_options(L, 1).get_double(to_name_ext(L, 2), defval));
return 1;
return pushnumber(L, to_options(L, 1).get_double(to_name_ext(L, 2), defval));
}
static int options_get_string(lua_State * L) {
int nargs = lua_gettop(L);
char const * defval = nargs < 3 ? "" : lua_tostring(L, 3);
lua_pushstring(L, to_options(L, 1).get_string(to_name_ext(L, 2), defval));
return 1;
return pushstring(L, to_options(L, 1).get_string(to_name_ext(L, 2), defval));
}
static int options_update_bool(lua_State * L) {

View file

@ -365,8 +365,7 @@ DECL_UDATA(sexpr)
static int sexpr_tostring(lua_State * L) {
std::ostringstream out;
out << to_sexpr(L, 1);
lua_pushstring(L, out.str().c_str());
return 1;
return pushstring(L, out.str().c_str());
}
static sexpr to_sexpr_elem(lua_State * L, int idx) {
@ -427,8 +426,7 @@ static int sexpr_length(lua_State * L) {
sexpr const & e = to_sexpr(L, 1);
if (!is_list(e))
throw exception("s-expression is not a list");
lua_pushinteger(L, length(e));
return 1;
return pushinteger(L, length(e));
}
static int sexpr_head(lua_State * L) {
@ -456,24 +454,21 @@ static int sexpr_to_string(lua_State * L) {
sexpr const & e = to_sexpr(L, 1);
if (!is_string(e))
throw exception("s-expression is not a string");
lua_pushstring(L, to_string(e).c_str());
return 1;
return pushstring(L, to_string(e).c_str());
}
static int sexpr_to_int(lua_State * L) {
sexpr const & e = to_sexpr(L, 1);
if (!is_int(e))
throw exception("s-expression is not an integer");
lua_pushinteger(L, to_int(e));
return 1;
return pushinteger(L, to_int(e));
}
static int sexpr_to_double(lua_State * L) {
sexpr const & e = to_sexpr(L, 1);
if (!is_double(e))
throw exception("s-expression is not a double");
lua_pushnumber(L, to_double(e));
return 1;
return pushnumber(L, to_double(e));
}
static int sexpr_to_name(lua_State * L) {
@ -498,8 +493,7 @@ static int sexpr_to_mpq(lua_State * L) {
}
static int sexpr_get_kind(lua_State * L) {
lua_pushinteger(L, static_cast<int>(to_sexpr(L, 1).kind()));
return 1;
return pushinteger(L, static_cast<int>(to_sexpr(L, 1).kind()));
}
static int sexpr_fields(lua_State * L) {