diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-11-22 17:12:53 +0100 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-11-22 17:12:53 +0100 |
commit | 95d8604d0822b32f2561c5b29f18acf909ead935 (patch) | |
tree | 5a7f955730793b06a24c5e91bc48d27549e51521 | |
parent | f8617acb4454b553df1dc82f38713777318bcf6d (diff) | |
download | lanes-95d8604d0822b32f2561c5b29f18acf909ead935.tar.gz lanes-95d8604d0822b32f2561c5b29f18acf909ead935.tar.bz2 lanes-95d8604d0822b32f2561c5b29f18acf909ead935.zip |
Tweak StackChecker to be able to unit-test it
-rw-r--r-- | src/debug.hpp | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/debug.hpp b/src/debug.hpp index 7c7f497..39d106a 100644 --- a/src/debug.hpp +++ b/src/debug.hpp | |||
@@ -22,7 +22,8 @@ inline SourceLocation Where(std::source_location const& where_ = std::source_loc | |||
22 | inline void LUA_ASSERT_IMPL(lua_State* const L_, bool const cond_, std::string_view const& txt_, SourceLocation const& where_ = Where()) | 22 | inline void LUA_ASSERT_IMPL(lua_State* const L_, bool const cond_, std::string_view const& txt_, SourceLocation const& where_ = Where()) |
23 | { | 23 | { |
24 | if (!cond_) { | 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()); | 25 | auto const& [_file, _line, _func] = where_; |
26 | raise_luaL_error(L_, "%s:%d: LUA_ASSERT '%s' IN %s", _file.data(), _line, txt_.data(), _func.data()); | ||
26 | } | 27 | } |
27 | } | 28 | } |
28 | 29 | ||
@@ -39,13 +40,16 @@ class StackChecker | |||
39 | DECLARE_UNIQUE_TYPE(Relative, int); | 40 | DECLARE_UNIQUE_TYPE(Relative, int); |
40 | DECLARE_UNIQUE_TYPE(Absolute, int); | 41 | DECLARE_UNIQUE_TYPE(Absolute, int); |
41 | 42 | ||
43 | static inline bool CallsCassert{ true }; | ||
44 | |||
42 | StackChecker(lua_State* const L_, Relative const offset_, SourceLocation const& where_ = Where()) | 45 | StackChecker(lua_State* const L_, Relative const offset_, SourceLocation const& where_ = Where()) |
43 | : L{ L_ } | 46 | : L{ L_ } |
44 | , oldtop{ lua_gettop(L_) - offset_ } | 47 | , oldtop{ lua_gettop(L_) - offset_ } |
45 | { | 48 | { |
46 | if ((offset_ < 0) || (oldtop < 0)) { | 49 | if ((offset_ < 0) || (oldtop < 0)) { |
47 | assert(false); | 50 | assert(!CallsCassert); |
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()); | 51 | auto const& [_file, _line, _func] = where_; |
52 | raise_luaL_error(L, "%s:%d: STACK INIT ASSERT (%d not %d) IN %s", _file.data(), _line, lua_gettop(L), offset_, _func.data()); | ||
49 | } | 53 | } |
50 | } | 54 | } |
51 | 55 | ||
@@ -54,8 +58,9 @@ class StackChecker | |||
54 | , oldtop{ 0 } | 58 | , oldtop{ 0 } |
55 | { | 59 | { |
56 | if (lua_gettop(L) != pos_) { | 60 | if (lua_gettop(L) != pos_) { |
57 | assert(false); | 61 | assert(!CallsCassert); |
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()); | 62 | auto const& [_file, _line, _func] = where_; |
63 | raise_luaL_error(L, "%s:%d: STACK INIT ASSERT (%d not %d) IN %s", _file.data(), _line, lua_gettop(L), pos_, _func.data()); | ||
59 | } | 64 | } |
60 | } | 65 | } |
61 | 66 | ||
@@ -72,8 +77,9 @@ class StackChecker | |||
72 | if (expected_ != LUA_MULTRET) { | 77 | if (expected_ != LUA_MULTRET) { |
73 | int const _actual{ lua_gettop(L) - oldtop }; | 78 | int const _actual{ lua_gettop(L) - oldtop }; |
74 | if (_actual != expected_) { | 79 | if (_actual != expected_) { |
75 | assert(false); | 80 | assert(!CallsCassert); |
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()); | 81 | auto const& [_file, _line, _func] = where_; |
82 | raise_luaL_error(L, "%s:%d: STACK CHECK ASSERT (%d not %d) IN %s", _file.data(), _line, _actual, expected_, _func.data()); | ||
77 | } | 83 | } |
78 | } | 84 | } |
79 | } | 85 | } |