aboutsummaryrefslogtreecommitdiff
path: root/src/debug.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/debug.h
parent91a34bd09900967e8b9cccdbd6d94a9f8cc8506c (diff)
downloadlanes-57ca292c8844e566184e3f7e5c98fa98991684bd.tar.gz
lanes-57ca292c8844e566184e3f7e5c98fa98991684bd.tar.bz2
lanes-57ca292c8844e566184e3f7e5c98fa98991684bd.zip
Reorganized debug stuff
Diffstat (limited to 'src/debug.h')
-rw-r--r--src/debug.h112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/debug.h b/src/debug.h
new file mode 100644
index 0000000..a0a4b8d
--- /dev/null
+++ b/src/debug.h
@@ -0,0 +1,112 @@
1#pragma once
2
3#include "lanesconf.h"
4#include "luaerrors.h"
5
6// #################################################################################################
7
8#if HAVE_LUA_ASSERT()
9
10inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, int const line_, char const* txt_)
11{
12 if (!cond_) {
13 raise_luaL_error(L_, "LUA_ASSERT %s:%d '%s'", file_, line_, txt_);
14 }
15}
16
17#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, __FILE__, __LINE__, #cond_)
18
19class StackChecker
20{
21 private:
22 lua_State* const L;
23 int oldtop;
24
25 public:
26 struct Relative
27 {
28 int const offset;
29
30 operator int() const { return offset; }
31 };
32
33 struct Absolute
34 {
35 int const offset;
36
37 operator int() const { return offset; }
38 };
39
40 StackChecker(lua_State* const L_, Relative offset_, char const* file_, size_t const line_)
41 : L{ L_ }
42 , oldtop{ lua_gettop(L_) - offset_ }
43 {
44 if ((offset_ < 0) || (oldtop < 0)) {
45 assert(false);
46 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), offset_, file_, line_);
47 }
48 }
49
50 StackChecker(lua_State* const L_, Absolute pos_, char const* file_, size_t const line_)
51 : L{ L_ }
52 , oldtop{ 0 }
53 {
54 if (lua_gettop(L) != pos_) {
55 assert(false);
56 raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), pos_, file_, line_);
57 }
58 }
59
60 StackChecker& operator=(StackChecker const& rhs_)
61 {
62 assert(L == rhs_.L);
63 oldtop = rhs_.oldtop;
64 return *this;
65 }
66
67 // verify if the distance between the current top and the initial one is what we expect
68 void check(int expected_, char const* file_, size_t const line_)
69 {
70 if (expected_ != LUA_MULTRET) {
71 int const actual{ lua_gettop(L) - oldtop };
72 if (actual != expected_) {
73 assert(false);
74 raise_luaL_error(L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_);
75 }
76 }
77 }
78};
79
80#define STACK_CHECK_START_REL(L, offset_) \
81 StackChecker _stackChecker_##L \
82 { \
83 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
84 }
85#define STACK_CHECK_START_ABS(L, offset_) \
86 StackChecker _stackChecker_##L \
87 { \
88 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
89 }
90#define STACK_CHECK_RESET_REL(L, offset_) \
91 _stackChecker_##L = StackChecker \
92 { \
93 L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \
94 }
95#define STACK_CHECK_RESET_ABS(L, offset_) \
96 _stackChecker_##L = StackChecker \
97 { \
98 L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \
99 }
100#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__)
101
102#else // HAVE_LUA_ASSERT()
103
104#define LUA_ASSERT(L_, c) nullptr // nothing
105
106#define STACK_CHECK_START_REL(L_, offset_)
107#define STACK_CHECK_START_ABS(L_, offset_)
108#define STACK_CHECK_RESET_REL(L_, offset_)
109#define STACK_CHECK_RESET_ABS(L_, offset_)
110#define STACK_CHECK(L_, offset_)
111
112#endif // HAVE_LUA_ASSERT()