aboutsummaryrefslogtreecommitdiff
path: root/src/debug.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-10-28 17:41:13 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2024-10-28 17:41:13 +0100
commit524a62f2a2aa4b8b4515ff1f6b1addb8abdd4267 (patch)
treed85e58d3eef1331b0b3055db9e93f7126f50e65e /src/debug.h
parent53236cf0472b5093195bcc128dec6b45a6fe1446 (diff)
downloadlanes-524a62f2a2aa4b8b4515ff1f6b1addb8abdd4267.tar.gz
lanes-524a62f2a2aa4b8b4515ff1f6b1addb8abdd4267.tar.bz2
lanes-524a62f2a2aa4b8b4515ff1f6b1addb8abdd4267.zip
Renamed debug.h → debug.hpp
Diffstat (limited to 'src/debug.h')
-rw-r--r--src/debug.h115
1 files changed, 0 insertions, 115 deletions
diff --git a/src/debug.h b/src/debug.h
deleted file mode 100644
index 7c7f497..0000000
--- a/src/debug.h
+++ /dev/null
@@ -1,115 +0,0 @@
1#pragma once
2
3#include "lanesconf.h"
4#include "luaerrors.hpp"
5#include "unique.hpp"
6
7// #################################################################################################
8
9#if HAVE_LUA_ASSERT()
10
11// file name, line number, function name
12using SourceLocation = std::tuple<std::string_view, uint_least32_t, std::string_view>;
13inline SourceLocation Where(std::source_location const& where_ = std::source_location::current())
14{
15 std::string_view const _path{ where_.file_name() };
16 // drop the directory structure
17 std::string_view const _fileName{ _path.substr(_path.find_last_of("\\/")+1) };
18 std::string_view const _func{ where_.function_name() };
19 return std::make_tuple(_fileName, where_.line(), _func);
20}
21
22inline void LUA_ASSERT_IMPL(lua_State* const L_, bool const cond_, std::string_view const& txt_, SourceLocation const& where_ = Where())
23{
24 if (!cond_) {
25 raise_luaL_error(L_, "%s:%d: LUA_ASSERT '%s' IN %s", std::get<0>(where_).data(), std::get<1>(where_), txt_.data(), std::get<2>(where_).data());
26 }
27}
28
29#define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, (cond_) ? true : false, #cond_)
30#define LUA_ASSERT_CODE(code_) code_
31
32class StackChecker
33{
34 private:
35 lua_State* const L;
36 int oldtop;
37
38 public:
39 DECLARE_UNIQUE_TYPE(Relative, int);
40 DECLARE_UNIQUE_TYPE(Absolute, int);
41
42 StackChecker(lua_State* const L_, Relative const offset_, SourceLocation const& where_ = Where())
43 : L{ L_ }
44 , oldtop{ lua_gettop(L_) - offset_ }
45 {
46 if ((offset_ < 0) || (oldtop < 0)) {
47 assert(false);
48 raise_luaL_error(L, "%s:%d: STACK INIT ASSERT (%d not %d) IN %s", std::get<0>(where_).data(), std::get<1>(where_), lua_gettop(L), offset_, std::get<2>(where_).data());
49 }
50 }
51
52 StackChecker(lua_State* const L_, Absolute const pos_, SourceLocation const& where_ = Where())
53 : L{ L_ }
54 , oldtop{ 0 }
55 {
56 if (lua_gettop(L) != pos_) {
57 assert(false);
58 raise_luaL_error(L, "%s:%d: STACK INIT ASSERT (%d not %d) IN %s", std::get<0>(where_).data(), std::get<1>(where_), lua_gettop(L), pos_, std::get<2>(where_).data());
59 }
60 }
61
62 StackChecker& operator=(StackChecker const& rhs_)
63 {
64 assert(L == rhs_.L);
65 oldtop = rhs_.oldtop;
66 return *this;
67 }
68
69 // verify if the distance between the current top and the initial one is what we expect
70 void check(int const expected_, SourceLocation const& where_ = Where())
71 {
72 if (expected_ != LUA_MULTRET) {
73 int const _actual{ lua_gettop(L) - oldtop };
74 if (_actual != expected_) {
75 assert(false);
76 raise_luaL_error(L, "%s:%d: STACK CHECK ASSERT (%d not %d) IN %s", std::get<0>(where_).data(), std::get<1>(where_), _actual, expected_, std::get<2>(where_).data());
77 }
78 }
79 }
80};
81
82#define STACK_CHECK_START_REL(L, offset_) \
83 StackChecker _stackChecker_##L \
84 { \
85 L, StackChecker::Relative{ offset_ }, \
86 }
87#define STACK_CHECK_START_ABS(L, offset_) \
88 StackChecker _stackChecker_##L \
89 { \
90 L, StackChecker::Absolute{ offset_ }, \
91 }
92#define STACK_CHECK_RESET_REL(L, offset_) \
93 _stackChecker_##L = StackChecker \
94 { \
95 L, StackChecker::Relative{ offset_ }, \
96 }
97#define STACK_CHECK_RESET_ABS(L, offset_) \
98 _stackChecker_##L = StackChecker \
99 { \
100 L, StackChecker::Absolute{ offset_ }, \
101 }
102#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_)
103
104#else // HAVE_LUA_ASSERT()
105
106#define LUA_ASSERT(L_, c) ((void) 0) // nothing
107#define LUA_ASSERT_CODE(code_) ((void) 0)
108
109#define STACK_CHECK_START_REL(L_, offset_)
110#define STACK_CHECK_START_ABS(L_, offset_)
111#define STACK_CHECK_RESET_REL(L_, offset_)
112#define STACK_CHECK_RESET_ABS(L_, offset_)
113#define STACK_CHECK(L_, offset_)
114
115#endif // HAVE_LUA_ASSERT()