diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-26 17:15:12 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-04-26 17:15:12 +0200 |
commit | 4a2705af8a9a6b55cf848d53f8330447138a19c4 (patch) | |
tree | d9e1e72a59e0032833f52ba020bdcdca420190fc /src/macros_and_utils.h | |
parent | 8aff7818754d24e230a22220db1ed834487d0559 (diff) | |
download | lanes-4a2705af8a9a6b55cf848d53f8330447138a19c4.tar.gz lanes-4a2705af8a9a6b55cf848d53f8330447138a19c4.tar.bz2 lanes-4a2705af8a9a6b55cf848d53f8330447138a19c4.zip |
C++ migration: wrap all Lua error raising API functions in a [[noreturn]] raise_... equivalent
Diffstat (limited to 'src/macros_and_utils.h')
-rw-r--r-- | src/macros_and_utils.h | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 068ff26..9bc71e1 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -17,6 +17,47 @@ extern "C" { | |||
17 | 17 | ||
18 | using namespace std::chrono_literals; | 18 | using namespace std::chrono_literals; |
19 | 19 | ||
20 | // ################################################################################################# | ||
21 | |||
22 | // use this instead of Lua's lua_error | ||
23 | [[noreturn]] static inline void raise_lua_error(lua_State* L_) | ||
24 | { | ||
25 | std::ignore = lua_error(L_); // doesn't return | ||
26 | assert(false); // we should never get here, but i'm paranoid | ||
27 | } | ||
28 | |||
29 | // ################################################################################################# | ||
30 | |||
31 | // use this instead of Lua's luaL_error | ||
32 | template <typename... ARGS> | ||
33 | [[noreturn]] static inline void raise_luaL_error(lua_State* L_, char const* fmt_, ARGS... args_) | ||
34 | { | ||
35 | std::ignore = luaL_error(L_, fmt_, std::forward<ARGS>(args_)...); // doesn't return | ||
36 | assert(false); // we should never get here, but i'm paranoid | ||
37 | } | ||
38 | |||
39 | // ################################################################################################# | ||
40 | |||
41 | // use this instead of Lua's luaL_argerror | ||
42 | template <typename... ARGS> | ||
43 | [[noreturn]] static inline void raise_luaL_argerror(lua_State* L_, int arg_, char const* extramsg_) | ||
44 | { | ||
45 | std::ignore = luaL_argerror(L_, arg_, extramsg_); // doesn't return | ||
46 | assert(false); // we should never get here, but i'm paranoid | ||
47 | } | ||
48 | |||
49 | // ################################################################################################# | ||
50 | |||
51 | // use this instead of Lua's luaL_typeerror | ||
52 | template <typename... ARGS> | ||
53 | [[noreturn]] static inline void raise_luaL_typeerror(lua_State* L_, int arg_, char const* tname_) | ||
54 | { | ||
55 | std::ignore = luaL_typeerror(L_, arg_, tname_); // doesn't return | ||
56 | assert(false); // we should never get here, but i'm paranoid | ||
57 | } | ||
58 | |||
59 | // ################################################################################################# | ||
60 | |||
20 | #define USE_DEBUG_SPEW() 0 | 61 | #define USE_DEBUG_SPEW() 0 |
21 | #if USE_DEBUG_SPEW() | 62 | #if USE_DEBUG_SPEW() |
22 | #define INDENT_BEGIN "%.*s " | 63 | #define INDENT_BEGIN "%.*s " |
@@ -48,7 +89,7 @@ inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, size_t | |||
48 | { | 89 | { |
49 | if (!cond_) | 90 | if (!cond_) |
50 | { | 91 | { |
51 | luaL_error(L_, "LUA_ASSERT %s:%llu '%s'", file_, line_, txt_); // doesn't return | 92 | raise_luaL_error(L_, "LUA_ASSERT %s:%llu '%s'", file_, line_, txt_); |
52 | } | 93 | } |
53 | } | 94 | } |
54 | 95 | ||
@@ -82,7 +123,7 @@ class StackChecker | |||
82 | if ((offset_ < 0) || (m_oldtop < 0)) | 123 | if ((offset_ < 0) || (m_oldtop < 0)) |
83 | { | 124 | { |
84 | assert(false); | 125 | assert(false); |
85 | luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), offset_, file_, line_); // doesn't return | 126 | raise_luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), offset_, file_, line_); |
86 | } | 127 | } |
87 | } | 128 | } |
88 | 129 | ||
@@ -93,7 +134,7 @@ class StackChecker | |||
93 | if (lua_gettop(m_L) != pos_) | 134 | if (lua_gettop(m_L) != pos_) |
94 | { | 135 | { |
95 | assert(false); | 136 | assert(false); |
96 | luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), pos_, file_, line_); // doesn't return | 137 | raise_luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(m_L), pos_, file_, line_); |
97 | } | 138 | } |
98 | } | 139 | } |
99 | 140 | ||
@@ -113,7 +154,7 @@ class StackChecker | |||
113 | if (actual != expected_) | 154 | if (actual != expected_) |
114 | { | 155 | { |
115 | assert(false); | 156 | assert(false); |
116 | luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_); // doesn't return | 157 | raise_luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_); |
117 | } | 158 | } |
118 | } | 159 | } |
119 | } | 160 | } |
@@ -127,14 +168,18 @@ class StackChecker | |||
127 | 168 | ||
128 | #endif // NDEBUG | 169 | #endif // NDEBUG |
129 | 170 | ||
171 | // ################################################################################################# | ||
172 | |||
130 | inline void STACK_GROW(lua_State* L, int n_) | 173 | inline void STACK_GROW(lua_State* L, int n_) |
131 | { | 174 | { |
132 | if (!lua_checkstack(L, n_)) | 175 | if (!lua_checkstack(L, n_)) |
133 | { | 176 | { |
134 | luaL_error(L, "Cannot grow stack!"); // doesn't return | 177 | raise_luaL_error(L, "Cannot grow stack!"); |
135 | } | 178 | } |
136 | } | 179 | } |
137 | 180 | ||
181 | // ################################################################################################# | ||
182 | |||
138 | #define LUAG_FUNC(func_name) [[nodiscard]] int LG_##func_name(lua_State* L) | 183 | #define LUAG_FUNC(func_name) [[nodiscard]] int LG_##func_name(lua_State* L) |
139 | 184 | ||
140 | // ################################################################################################# | 185 | // ################################################################################################# |
@@ -169,13 +214,6 @@ template <typename T> | |||
169 | 214 | ||
170 | // ################################################################################################# | 215 | // ################################################################################################# |
171 | 216 | ||
172 | // use this instead of Lua's lua_error if possible | ||
173 | [[noreturn]] static inline void raise_lua_error(lua_State* L) | ||
174 | { | ||
175 | std::ignore = lua_error(L); // doesn't return | ||
176 | assert(false); // we should never get here, but i'm paranoid | ||
177 | } | ||
178 | |||
179 | using lua_Duration = std::chrono::template duration<lua_Number>; | 217 | using lua_Duration = std::chrono::template duration<lua_Number>; |
180 | 218 | ||
181 | // ################################################################################################# | 219 | // ################################################################################################# |
@@ -222,7 +260,7 @@ typename T::value_type const& OptionalValue(T const& x_, Ts... args_) | |||
222 | { | 260 | { |
223 | if (!x_.has_value()) | 261 | if (!x_.has_value()) |
224 | { | 262 | { |
225 | luaL_error(std::forward<Ts>(args_)...); // doesn't return | 263 | raise_luaL_error(std::forward<Ts>(args_)...); |
226 | } | 264 | } |
227 | return x_.value(); | 265 | return x_.value(); |
228 | } | 266 | } |