aboutsummaryrefslogtreecommitdiff
path: root/src/macros_and_utils.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-07 09:51:45 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-07 09:51:45 +0200
commit57ca292c8844e566184e3f7e5c98fa98991684bd (patch)
tree9356cf33935ac4dc130cb71ba701d8d9c286597d /src/macros_and_utils.h
parent91a34bd09900967e8b9cccdbd6d94a9f8cc8506c (diff)
downloadlanes-57ca292c8844e566184e3f7e5c98fa98991684bd.tar.gz
lanes-57ca292c8844e566184e3f7e5c98fa98991684bd.tar.bz2
lanes-57ca292c8844e566184e3f7e5c98fa98991684bd.zip
Reorganized debug stuff
Diffstat (limited to 'src/macros_and_utils.h')
-rw-r--r--src/macros_and_utils.h154
1 files changed, 3 insertions, 151 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index 926e756..d540cdb 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -11,164 +11,16 @@ extern "C"
11} 11}
12#endif // __cplusplus 12#endif // __cplusplus
13 13
14#include <cassert> 14#include "debug.h"
15#include "luaerrors.h"
16
15#include <chrono> 17#include <chrono>
16#include <tuple>
17#include <type_traits> 18#include <type_traits>
18 19
19using namespace std::chrono_literals; 20using namespace std::chrono_literals;
20 21
21// ################################################################################################# 22// #################################################################################################
22 23
23// use this instead of Lua's lua_error
24[[noreturn]] static inline void raise_lua_error(lua_State* const L_)
25{
26 std::ignore = lua_error(L_); // doesn't return
27 assert(false); // we should never get here, but i'm paranoid
28}
29
30// #################################################################################################
31
32// use this instead of Lua's luaL_error
33template <typename... ARGS>
34[[noreturn]] static inline void raise_luaL_error(lua_State* const L_, std::string_view const& fmt_, ARGS... args_)
35{
36 std::ignore = luaL_error(L_, fmt_.data(), std::forward<ARGS>(args_)...); // doesn't return
37 assert(false); // we should never get here, but i'm paranoid
38}
39
40// #################################################################################################
41
42// use this instead of Lua's luaL_argerror
43template <typename... ARGS>
44[[noreturn]] static inline void raise_luaL_argerror(lua_State* const L_, int const arg_, std::string_view const& extramsg_)
45{
46 std::ignore = luaL_argerror(L_, arg_, extramsg_.data()); // doesn't return
47 assert(false); // we should never get here, but i'm paranoid
48}
49
50// #################################################################################################
51
52#if LUA_VERSION_NUM >= 504
53// use this instead of Lua's luaL_typeerror
54template <typename... ARGS>
55[[noreturn]] static inline void raise_luaL_typeerror(lua_State* const L_, int const arg_, std::string_view const& tname_)
56{
57 std::ignore = luaL_typeerror(L_, arg_, tname_.data()); // doesn't return
58 assert(false); // we should never get here, but i'm paranoid
59}
60#endif // LUA_VERSION_NUM
61
62// #################################################################################################
63
64#ifdef NDEBUG
65
66#define LUA_ASSERT(L_, c) ; // nothing
67
68#define STACK_CHECK_START_REL(L_, offset_)
69#define STACK_CHECK_START_ABS(L_, offset_)
70#define STACK_CHECK_RESET_REL(L_, offset_)
71#define STACK_CHECK_RESET_ABS(L_, offset_)
72#define STACK_CHECK(L_, offset_)
73
74#else // NDEBUG
75
76inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, int const line_, char const* txt_)
77{
78 if (!cond_) {
79 raise_luaL_error(L_, "LUA_ASSERT %s:%d '%s'", file_, line_, txt_);
80 }
81}
82
83#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, __FILE__, __LINE__, #cond_)
84
85class StackChecker
86{
87 private:
88 lua_State* const L;
89 int oldtop;
90
91 public:
92 struct Relative
93 {
94 int const offset;
95
96 operator int() const { return offset; }
97 };
98
99 struct Absolute
100 {
101 int const offset;
102
103 operator int() const { return offset; }
104 };
105
106 StackChecker(lua_State* const L_, Relative offset_, char const* file_, size_t const line_)
107 : L{ L_ }
108 , oldtop{ lua_gettop(L_) - offset_ }
109 {
110 if ((offset_ < 0) || (oldtop < 0)) {
111 assert(false);
112 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), offset_, file_, line_);
113 }
114 }
115
116 StackChecker(lua_State* const L_, Absolute pos_, char const* file_, size_t const line_)
117 : L{ L_ }
118 , oldtop{ 0 }
119 {
120 if (lua_gettop(L) != pos_) {
121 assert(false);
122 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), pos_, file_, line_);
123 }
124 }
125
126 StackChecker& operator=(StackChecker const& rhs_)
127 {
128 assert(L == rhs_.L);
129 oldtop = rhs_.oldtop;
130 return *this;
131 }
132
133 // verify if the distance between the current top and the initial one is what we expect
134 void check(int expected_, char const* file_, size_t const line_)
135 {
136 if (expected_ != LUA_MULTRET) {
137 int const actual{ lua_gettop(L) - oldtop };
138 if (actual != expected_) {
139 assert(false);
140 raise_luaL_error(L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_);
141 }
142 }
143 }
144};
145
146#define STACK_CHECK_START_REL(L, offset_) \
147 StackChecker _stackChecker_##L \
148 { \
149 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
150 }
151#define STACK_CHECK_START_ABS(L, offset_) \
152 StackChecker _stackChecker_##L \
153 { \
154 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
155 }
156#define STACK_CHECK_RESET_REL(L, offset_) \
157 _stackChecker_##L = StackChecker \
158 { \
159 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
160 }
161#define STACK_CHECK_RESET_ABS(L, offset_) \
162 _stackChecker_##L = StackChecker \
163 { \
164 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
165 }
166#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__)
167
168#endif // NDEBUG
169
170// #################################################################################################
171
172inline void STACK_GROW(lua_State* L_, int n_) 24inline void STACK_GROW(lua_State* L_, int n_)
173{ 25{
174 if (!lua_checkstack(L_, n_)) { 26 if (!lua_checkstack(L_, n_)) {