From 2f049249c8d861193e348e042a203a034982adc1 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 28 Mar 2024 12:33:05 +0100 Subject: C++ migration: cleanup usage of lua_error and luaL_error --- src/cancel.cpp | 14 +++++++------- src/cancel.h | 8 ++++---- src/deep.cpp | 6 +++--- src/keeper.cpp | 9 +++------ src/lanes.cpp | 10 ++++------ src/linda.cpp | 8 ++++---- src/macros_and_utils.h | 22 ++++++++++++++++++---- src/state.cpp | 2 +- 8 files changed, 44 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index 73e50f5..c0867a9 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -50,7 +50,7 @@ THE SOFTWARE. * Called by cancellation hooks and/or pending Linda operations (because then * the check won't affect performance). * -* Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'cancel_error()' called, +* Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, * to make execution of the lane end. */ static inline CancelRequest cancel_test(lua_State* L) @@ -78,13 +78,13 @@ LUAG_FUNC( cancel_test) // ################################################################################################ // ################################################################################################ -static void cancel_hook( lua_State* L, [[maybe_unused]] lua_Debug* ar) +static void cancel_hook(lua_State* L, [[maybe_unused]] lua_Debug* ar) { - DEBUGSPEW_CODE( fprintf( stderr, "cancel_hook\n")); + DEBUGSPEW_CODE(fprintf(stderr, "cancel_hook\n")); if (cancel_test(L) != CancelRequest::None) { - lua_sethook( L, nullptr, 0, 0); - cancel_error( L); + lua_sethook(L, nullptr, 0, 0); + raise_cancel_error(L); } } @@ -158,7 +158,7 @@ static CancelResult thread_cancel_hard(lua_State* L, Lane* s, double secs_, bool result = THREAD_WAIT(&s->thread, waitkill_timeout_, &s->done_signal, &s->done_lock, &s->status) ? CancelResult::Killed : CancelResult::Timeout; if (result == CancelResult::Timeout) { - (void) luaL_error( L, "force-killed lane failed to terminate within %f second%s", waitkill_timeout_, waitkill_timeout_ > 1 ? "s" : ""); + std::ignore = luaL_error( L, "force-killed lane failed to terminate within %f second%s", waitkill_timeout_, waitkill_timeout_ > 1 ? "s" : ""); } #else (void) waitkill_timeout_; // unused @@ -238,7 +238,7 @@ static CancelOp which_op( lua_State* L, int idx_) lua_remove( L, idx_); // argument is processed, remove it if( op == CO_Invalid) { - luaL_error( L, "invalid hook option %s", str); + std::ignore = luaL_error( L, "invalid hook option %s", str); } return op; } diff --git a/src/cancel.h b/src/cancel.h index 9a20774..9299ae1 100644 --- a/src/cancel.h +++ b/src/cancel.h @@ -46,18 +46,18 @@ enum CancelOp }; // crc64/we of string "CANCEL_ERROR" generated at http://www.nitrxgen.net/hashgen/ -static constexpr UniqueKey CANCEL_ERROR{ 0xe97d41626cc97577ull }; // 'cancel_error' sentinel +static constexpr UniqueKey CANCEL_ERROR{ 0xe97d41626cc97577ull }; // 'raise_cancel_error' sentinel // crc64/we of string "CANCEL_TEST_KEY" generated at http://www.nitrxgen.net/hashgen/ static constexpr UniqueKey CANCEL_TEST_KEY{ 0xe66f5960c57d133aull }; // used as registry key CancelResult thread_cancel(lua_State* L, Lane* s, CancelOp op_, double secs_, bool force_, double waitkill_timeout_); -static inline int cancel_error( lua_State* L) +[[noreturn]] static inline void raise_cancel_error(lua_State* L) { - STACK_GROW( L, 1); + STACK_GROW(L, 1); CANCEL_ERROR.push(L); // special error value - return lua_error( L); // doesn't return + raise_lua_error(L); // doesn't return } // ################################################################################################ diff --git a/src/deep.cpp b/src/deep.cpp index f59e051..74ecfb8 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -184,7 +184,7 @@ static int deep_userdata_gc( lua_State* L) // top was set to 0, then userdata was pushed. "delete" might want to pop the userdata (we don't care), but should not push anything! if ( lua_gettop( L) > 1) { - luaL_error( L, "Bad idfunc(eDO_delete): should not push anything"); + return luaL_error( L, "Bad idfunc(eDO_delete): should not push anything"); } } *proxy = nullptr; // make sure we don't use it any more, just in case @@ -478,8 +478,8 @@ bool copydeep(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, L if (errmsg != nullptr) { // raise the error in the proper state (not the keeper) - lua_State* errL = (mode_ == eLM_FromKeeper) ? L2 : L; - luaL_error( errL, errmsg); + lua_State* const errL { (mode_ == eLM_FromKeeper) ? L2 : L }; + std::ignore = luaL_error(errL, errmsg); } return true; } \ No newline at end of file diff --git a/src/keeper.cpp b/src/keeper.cpp index cb2b0a9..1919a68 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -655,7 +655,7 @@ void init_keepers(Universe* U, lua_State* L) lua_pop(L, 1); // if (nb_keepers < 1) { - (void) luaL_error(L, "Bad number of keepers (%d)", nb_keepers); + std::ignore = luaL_error(L, "Bad number of keepers (%d)", nb_keepers); } // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states @@ -664,8 +664,7 @@ void init_keepers(Universe* U, lua_State* L) U->keepers = static_cast(U->internal_allocator.alloc(bytes)); if (U->keepers == nullptr) { - (void) luaL_error(L, "init_keepers() failed while creating keeper array; out of memory"); - return; + std::ignore = luaL_error(L, "init_keepers() failed while creating keeper array; out of memory"); } memset(U->keepers, 0, bytes); U->keepers->nb_keepers = nb_keepers; @@ -677,7 +676,6 @@ void init_keepers(Universe* U, lua_State* L) if (K == nullptr) { std::ignore = luaL_error(L, "init_keepers() failed while creating keeper states; out of memory"); - return; } U->keepers->keeper_array[i].L = K; @@ -709,8 +707,7 @@ void init_keepers(Universe* U, lua_State* L) { // if something went wrong, the error message is at the top of the stack lua_remove(L, -2); // error_msg - (void) lua_error(L); - return; + raise_lua_error(L); } } lua_pop(L, 1); // diff --git a/src/lanes.cpp b/src/lanes.cpp index a378a88..2f17e6e 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -1110,7 +1110,7 @@ LUAG_FUNC( lane_new) if( lua_isnil( L2, -1)) { lua_pop( L2, 1); // - luaL_error( L, "cannot pre-require modules without loading 'package' library first"); + return luaL_error( L, "cannot pre-require modules without loading 'package' library first"); } else { @@ -1119,7 +1119,7 @@ LUAG_FUNC( lane_new) { // propagate error to main state if any luaG_inter_move( U, L2, L, 1, eLM_LaneBody); // func libs priority globals package required gc_cb [... args ...] n "modname" error - return lua_error( L); + raise_lua_error(L); } // after requiring the module, register the functions it exported in our name<->function database populate_func_lookup_table( L2, -1, name); @@ -1538,11 +1538,9 @@ LUAG_FUNC( thread_index) lua_pushliteral( L, "Unexpected status: "); lua_pushstring( L, thread_status_string( s)); lua_concat( L, 2); - lua_error( L); - break; + raise_lua_error(L); } - // fall through if we are killed, as we got nil, "killed" on the stack - [[fallthrough]]; + [[fallthrough]]; // fall through if we are killed, as we got nil, "killed" on the stack case DONE: // got regular return values { diff --git a/src/linda.cpp b/src/linda.cpp index 2830593..e1633b0 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -165,7 +165,7 @@ static void check_key_types(lua_State* L, int start_, int end_) { continue; } - (void) luaL_error( L, "argument #%d: invalid key type (not a boolean, string, number or light userdata)", i); + std::ignore = luaL_error(L, "argument #%d: invalid key type (not a boolean, string, number or light userdata)", i); } } @@ -192,7 +192,7 @@ LUAG_FUNC(linda_protected_call) // if there was an error, forward it if( rc != LUA_OK) { - return lua_error( L); + raise_lua_error(L); } // return whatever the actual operation provided return lua_gettop( L); @@ -339,7 +339,7 @@ LUAG_FUNC( linda_send) case CancelRequest::Hard: // raise an error interrupting execution only in case of hard cancel - return cancel_error( L); // raises an error and doesn't return + raise_cancel_error(L); // raises an error and doesn't return default: lua_pushboolean( L, ret); // true (success) or false (timeout) @@ -494,7 +494,7 @@ LUAG_FUNC( linda_receive) case CancelRequest::Hard: // raise an error interrupting execution only in case of hard cancel - return cancel_error( L); // raises an error and doesn't return + raise_cancel_error(L); // raises an error and doesn't return default: return pushed; diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 63bc8d1..31027d6 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h @@ -11,6 +11,7 @@ extern "C" { #endif // __cplusplus #include +#include #include #define USE_DEBUG_SPEW() 0 @@ -71,7 +72,7 @@ class StackChecker if ((offset_ < 0) || (m_oldtop < 0)) { assert(false); - (void) luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), offset_, file_, line_); + std::ignore = luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), offset_, file_, line_); } } @@ -82,7 +83,7 @@ class StackChecker if (lua_gettop(m_L) != pos_) { assert(false); - (void) luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), pos_, file_, line_); + std::ignore = luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), pos_, file_, line_); } } @@ -102,7 +103,7 @@ class StackChecker if (actual != expected_) { assert(false); - luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%d", actual, expected_, file_, line_); + std::ignore = luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%d", actual, expected_, file_, line_); } } } @@ -121,11 +122,15 @@ class StackChecker inline void STACK_GROW(lua_State* L, int n_) { if (!lua_checkstack(L, n_)) - luaL_error(L, "Cannot grow stack!"); + { + std::ignore = luaL_error(L, "Cannot grow stack!"); + } } #define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L) +// ################################################################################################# + // a small helper to extract a full userdata pointer from the stack in a safe way template T* lua_tofulluserdata(lua_State* L, int index_) @@ -153,3 +158,12 @@ T* lua_newuserdatauv(lua_State* L, int nuvalue_) { return static_cast(lua_newuserdatauv(L, sizeof(T), nuvalue_)); } + +// ################################################################################################# + +// use this instead of Lua's lua_error if possible +[[noreturn]] static inline void raise_lua_error(lua_State* L) +{ + std::ignore = lua_error(L); // doesn't return + assert(false); // we should never get here, but i'm paranoid +} diff --git a/src/state.cpp b/src/state.cpp index 817d5b4..b52bdb5 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -82,7 +82,7 @@ static int luaG_new_require( lua_State* L) if( rc != LUA_OK) // LUA_ERRRUN / LUA_ERRMEM ? { - return lua_error( L); + raise_lua_error(L); } // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 return lua_gettop(L); // result(s) -- cgit v1.2.3-55-g6feb