From 412a3797f4c4d0e2f8bfd7cc5e4df7262bafecb5 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Tue, 29 Apr 2014 14:37:16 -0700 Subject: [PATCH] refactor(*): add pushboolean inline function, and replace lua_pushboolean with it Signed-off-by: Leonardo de Moura --- src/library/kernel_bindings.cpp | 12 ++---------- src/util/exception.cpp | 3 +-- src/util/lua.h | 7 +++++++ src/util/name.cpp | 12 +++--------- src/util/numerics/mpq.cpp | 6 ++---- src/util/numerics/mpz.cpp | 6 ++---- src/util/rb_map.cpp | 6 ++---- src/util/script_state.cpp | 6 ++---- src/util/sexpr/options.cpp | 9 +++------ src/util/sexpr/sexpr.cpp | 13 ++++--------- 10 files changed, 28 insertions(+), 52 deletions(-) diff --git a/src/library/kernel_bindings.cpp b/src/library/kernel_bindings.cpp index 1c06a59bf5..1769ae18cf 100644 --- a/src/library/kernel_bindings.cpp +++ b/src/library/kernel_bindings.cpp @@ -26,16 +26,8 @@ static int level_tostring(lua_State * L) { return 1; } -static int level_eq(lua_State * L) { - lua_pushboolean(L, to_level(L, 1) == to_level(L, 2)); - return 1; -} - -static int level_lt(lua_State * L) { - lua_pushboolean(L, is_lt(to_level(L, 1), to_level(L, 2))); - return 1; -} - +static int level_eq(lua_State * L) { return pushboolean(L, to_level(L, 1) == to_level(L, 2)); } +static int level_lt(lua_State * L) { return pushboolean(L, is_lt(to_level(L, 1), to_level(L, 2))); } static int mk_level_zero(lua_State * L) { return push_level(L, mk_level_zero()); } static int mk_level_one(lua_State * L) { return push_level(L, mk_level_one()); } static int mk_level_succ(lua_State * L) { return push_level(L, mk_succ(to_level(L, 1))); } diff --git a/src/util/exception.cpp b/src/util/exception.cpp index 4d4483a46f..36965395e7 100644 --- a/src/util/exception.cpp +++ b/src/util/exception.cpp @@ -79,8 +79,7 @@ static int exception_rethrow(lua_State * L) { } static int exception_pred(lua_State * L) { - lua_pushboolean(L, is_exception(L, 1)); - return 1; + return pushboolean(L, is_exception(L, 1)); } static const struct luaL_Reg exception_m[] = { diff --git a/src/util/lua.h b/src/util/lua.h index 46b03d4e6b..94a99d2d90 100644 --- a/src/util/lua.h +++ b/src/util/lua.h @@ -128,4 +128,11 @@ void set_migrate_fn_field(lua_State * src, int i, lua_migrate_fn fn); for the userdata at position \c i. */ 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; } +// ======================================= + } diff --git a/src/util/name.cpp b/src/util/name.cpp index 723743ae05..17c026ae25 100644 --- a/src/util/name.cpp +++ b/src/util/name.cpp @@ -478,13 +478,11 @@ static int name_tostring(lua_State * L) { } static int name_eq(lua_State * L) { - lua_pushboolean(L, to_name(L, 1) == to_name(L, 2)); - return 1; + return pushboolean(L, to_name(L, 1) == to_name(L, 2)); } static int name_lt(lua_State * L) { - lua_pushboolean(L, to_name(L, 1) < to_name(L, 2)); - return 1; + return pushboolean(L, to_name(L, 1) < to_name(L, 2)); } static int name_hash(lua_State * L) { @@ -492,11 +490,7 @@ static int name_hash(lua_State * L) { return 1; } -#define NAME_PRED(P) \ -static int name_ ## P(lua_State * L) { \ - lua_pushboolean(L, to_name(L, 1).P()); \ - return 1; \ -} +#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) diff --git a/src/util/numerics/mpq.cpp b/src/util/numerics/mpq.cpp index 192d0429b1..c6141897aa 100644 --- a/src/util/numerics/mpq.cpp +++ b/src/util/numerics/mpq.cpp @@ -178,13 +178,11 @@ static int mpq_tostring(lua_State * L) { } static int mpq_eq(lua_State * L) { - lua_pushboolean(L, to_mpq<1>(L) == to_mpq<2>(L)); - return 1; + return pushboolean(L, to_mpq<1>(L) == to_mpq<2>(L)); } static int mpq_lt(lua_State * L) { - lua_pushboolean(L, to_mpq<1>(L) < to_mpq<2>(L)); - return 1; + return pushboolean(L, to_mpq<1>(L) < to_mpq<2>(L)); } static int mpq_add(lua_State * L) { diff --git a/src/util/numerics/mpz.cpp b/src/util/numerics/mpz.cpp index c3a6e2262f..9df063f520 100644 --- a/src/util/numerics/mpz.cpp +++ b/src/util/numerics/mpz.cpp @@ -126,13 +126,11 @@ static int mpz_tostring(lua_State * L) { } static int mpz_eq(lua_State * L) { - lua_pushboolean(L, to_mpz<1>(L) == to_mpz<2>(L)); - return 1; + return pushboolean(L, to_mpz<1>(L) == to_mpz<2>(L)); } static int mpz_lt(lua_State * L) { - lua_pushboolean(L, to_mpz<1>(L) < to_mpz<2>(L)); - return 1; + return pushboolean(L, to_mpz<1>(L) < to_mpz<2>(L)); } static int mpz_add(lua_State * L) { diff --git a/src/util/rb_map.cpp b/src/util/rb_map.cpp index 7450bb36b9..71e55f00e2 100644 --- a/src/util/rb_map.cpp +++ b/src/util/rb_map.cpp @@ -23,13 +23,11 @@ static int lua_rb_map_size(lua_State * L) { } static int lua_rb_map_contains(lua_State * L) { - lua_pushboolean(L, to_lua_rb_map(L, 1).contains(luaref(L, 2))); - return 1; + return pushboolean(L, to_lua_rb_map(L, 1).contains(luaref(L, 2))); } static int lua_rb_map_empty(lua_State * L) { - lua_pushboolean(L, to_lua_rb_map(L, 1).empty()); - return 1; + return pushboolean(L, to_lua_rb_map(L, 1).empty()); } static int lua_rb_map_insert(lua_State * L) { diff --git a/src/util/script_state.cpp b/src/util/script_state.cpp index c9912500e1..04d0e72f13 100644 --- a/src/util/script_state.cpp +++ b/src/util/script_state.cpp @@ -298,8 +298,7 @@ int state_set_global(lua_State * L) { } static int state_pred(lua_State * L) { - lua_pushboolean(L, is_state(L, 1)); - return 1; + return pushboolean(L, is_state(L, 1)); } static const struct luaL_Reg state_m[] = { @@ -535,8 +534,7 @@ static int thread_gc(lua_State * L) { } static int thread_pred(lua_State * L) { - lua_pushboolean(L, is_thread(L, 1)); - return 1; + return pushboolean(L, is_thread(L, 1)); } static int thread_write(lua_State * L) { diff --git a/src/util/sexpr/options.cpp b/src/util/sexpr/options.cpp index c246088172..4df015e792 100644 --- a/src/util/sexpr/options.cpp +++ b/src/util/sexpr/options.cpp @@ -241,20 +241,17 @@ static int options_size(lua_State * L) { } static int options_contains(lua_State * L) { - lua_pushboolean(L, to_options(L, 1).contains(to_name_ext(L, 2))); - return 1; + return pushboolean(L, to_options(L, 1).contains(to_name_ext(L, 2))); } static int options_empty(lua_State * L) { - lua_pushboolean(L, to_options(L, 1).empty()); - return 1; + return pushboolean(L, to_options(L, 1).empty()); } static int options_get_bool(lua_State * L) { int nargs = lua_gettop(L); bool defval = nargs < 3 ? false : lua_toboolean(L, 3); - lua_pushboolean(L, to_options(L, 1).get_bool(to_name_ext(L, 2), defval)); - return 1; + return pushboolean(L, to_options(L, 1).get_bool(to_name_ext(L, 2), defval)); } static int options_get_int(lua_State * L) { diff --git a/src/util/sexpr/sexpr.cpp b/src/util/sexpr/sexpr.cpp index 6c0ce4f0da..d8e8c372ba 100644 --- a/src/util/sexpr/sexpr.cpp +++ b/src/util/sexpr/sexpr.cpp @@ -406,14 +406,10 @@ static int mk_sexpr(lua_State * L) { return push_sexpr(L, r); } -static int sexpr_eq(lua_State * L) { lua_pushboolean(L, to_sexpr(L, 1) == to_sexpr(L, 2)); return 1; } -static int sexpr_lt(lua_State * L) { lua_pushboolean(L, to_sexpr(L, 1) < to_sexpr(L, 2)); return 1; } +static int sexpr_eq(lua_State * L) { return pushboolean(L, to_sexpr(L, 1) == to_sexpr(L, 2)); } +static int sexpr_lt(lua_State * L) { return pushboolean(L, to_sexpr(L, 1) < to_sexpr(L, 2)); } -#define SEXPR_PRED(P) \ -static int sexpr_ ## P(lua_State * L) { \ - lua_pushboolean(L, P(to_sexpr(L, 1))); \ - return 1; \ -} +#define SEXPR_PRED(P) static int sexpr_ ## P(lua_State * L) { return pushboolean(L, P(to_sexpr(L, 1))); } SEXPR_PRED(is_nil) SEXPR_PRED(is_cons) @@ -453,8 +449,7 @@ static int sexpr_to_bool(lua_State * L) { sexpr const & e = to_sexpr(L, 1); if (!is_bool(e)) throw exception("s-expression is not a Boolean"); - lua_pushboolean(L, to_bool(e)); - return 1; + return pushboolean(L, to_bool(e)); } static int sexpr_to_string(lua_State * L) {