diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/debug.h | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/src/debug.h b/src/debug.h index 99fe48a..e199c3e 100644 --- a/src/debug.h +++ b/src/debug.h | |||
| @@ -7,14 +7,25 @@ | |||
| 7 | 7 | ||
| 8 | #if HAVE_LUA_ASSERT() | 8 | #if HAVE_LUA_ASSERT() |
| 9 | 9 | ||
| 10 | inline void LUA_ASSERT_IMPL(lua_State* L_, bool cond_, char const* file_, int const line_, char const* txt_) | 10 | // file name, line number, function name |
| 11 | using SourceLocation = std::tuple<std::string_view, uint_least32_t, std::string_view>; | ||
| 12 | inline SourceLocation Where(std::source_location const& where_ = std::source_location::current()) | ||
| 13 | { | ||
| 14 | std::string_view const _path{ where_.file_name() }; | ||
| 15 | // drop the directory structure | ||
| 16 | std::string_view const _fileName{ _path.substr(_path.find_last_of("\\/")+1) }; | ||
| 17 | std::string_view const _func{ where_.function_name() }; | ||
| 18 | return std::make_tuple(_fileName, where_.line(), _func); | ||
| 19 | } | ||
| 20 | |||
| 21 | inline void LUA_ASSERT_IMPL(lua_State* const L_, bool cond_, std::string_view const& txt_, SourceLocation const& where_ = Where()) | ||
| 11 | { | 22 | { |
| 12 | if (!cond_) { | 23 | if (!cond_) { |
| 13 | raise_luaL_error(L_, "LUA_ASSERT %s:%d '%s'", file_, line_, txt_); | 24 | 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()); |
| 14 | } | 25 | } |
| 15 | } | 26 | } |
| 16 | 27 | ||
| 17 | #define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, __FILE__, __LINE__, #cond_) | 28 | #define LUA_ASSERT(L_, cond_) LUA_ASSERT_IMPL(L_, cond_, #cond_) |
| 18 | #define LUA_ASSERT_CODE(code_) code_ | 29 | #define LUA_ASSERT_CODE(code_) code_ |
| 19 | 30 | ||
| 20 | class StackChecker | 31 | class StackChecker |
| @@ -38,23 +49,23 @@ class StackChecker | |||
| 38 | operator int() const { return offset; } | 49 | operator int() const { return offset; } |
| 39 | }; | 50 | }; |
| 40 | 51 | ||
| 41 | StackChecker(lua_State* const L_, Relative offset_, char const* file_, size_t const line_) | 52 | StackChecker(lua_State* const L_, Relative const offset_, SourceLocation const& where_ = Where()) |
| 42 | : L{ L_ } | 53 | : L{ L_ } |
| 43 | , oldtop{ lua_gettop(L_) - offset_ } | 54 | , oldtop{ lua_gettop(L_) - offset_ } |
| 44 | { | 55 | { |
| 45 | if ((offset_ < 0) || (oldtop < 0)) { | 56 | if ((offset_ < 0) || (oldtop < 0)) { |
| 46 | assert(false); | 57 | assert(false); |
| 47 | raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), offset_, file_, line_); | 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), offset_, std::get<2>(where_).data()); |
| 48 | } | 59 | } |
| 49 | } | 60 | } |
| 50 | 61 | ||
| 51 | StackChecker(lua_State* const L_, Absolute pos_, char const* file_, size_t const line_) | 62 | StackChecker(lua_State* const L_, Absolute const pos_, SourceLocation const& where_ = Where()) |
| 52 | : L{ L_ } | 63 | : L{ L_ } |
| 53 | , oldtop{ 0 } | 64 | , oldtop{ 0 } |
| 54 | { | 65 | { |
| 55 | if (lua_gettop(L) != pos_) { | 66 | if (lua_gettop(L) != pos_) { |
| 56 | assert(false); | 67 | assert(false); |
| 57 | raise_luaL_error(L, "STACK INIT ASSERT failed (%d not %d): %s:%llu", lua_gettop(L), pos_, file_, line_); | 68 | 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()); |
| 58 | } | 69 | } |
| 59 | } | 70 | } |
| 60 | 71 | ||
| @@ -66,13 +77,13 @@ class StackChecker | |||
| 66 | } | 77 | } |
| 67 | 78 | ||
| 68 | // verify if the distance between the current top and the initial one is what we expect | 79 | // verify if the distance between the current top and the initial one is what we expect |
| 69 | void check(int expected_, char const* file_, size_t const line_) | 80 | void check(int expected_, SourceLocation const& where_ = Where()) |
| 70 | { | 81 | { |
| 71 | if (expected_ != LUA_MULTRET) { | 82 | if (expected_ != LUA_MULTRET) { |
| 72 | int const actual{ lua_gettop(L) - oldtop }; | 83 | int const _actual{ lua_gettop(L) - oldtop }; |
| 73 | if (actual != expected_) { | 84 | if (_actual != expected_) { |
| 74 | assert(false); | 85 | assert(false); |
| 75 | raise_luaL_error(L, "STACK ASSERT failed (%d not %d): %s:%llu", actual, expected_, file_, line_); | 86 | 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()); |
| 76 | } | 87 | } |
| 77 | } | 88 | } |
| 78 | } | 89 | } |
| @@ -81,24 +92,24 @@ class StackChecker | |||
| 81 | #define STACK_CHECK_START_REL(L, offset_) \ | 92 | #define STACK_CHECK_START_REL(L, offset_) \ |
| 82 | StackChecker _stackChecker_##L \ | 93 | StackChecker _stackChecker_##L \ |
| 83 | { \ | 94 | { \ |
| 84 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ | 95 | L, StackChecker::Relative{ offset_ }, \ |
| 85 | } | 96 | } |
| 86 | #define STACK_CHECK_START_ABS(L, offset_) \ | 97 | #define STACK_CHECK_START_ABS(L, offset_) \ |
| 87 | StackChecker _stackChecker_##L \ | 98 | StackChecker _stackChecker_##L \ |
| 88 | { \ | 99 | { \ |
| 89 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ | 100 | L, StackChecker::Absolute{ offset_ }, \ |
| 90 | } | 101 | } |
| 91 | #define STACK_CHECK_RESET_REL(L, offset_) \ | 102 | #define STACK_CHECK_RESET_REL(L, offset_) \ |
| 92 | _stackChecker_##L = StackChecker \ | 103 | _stackChecker_##L = StackChecker \ |
| 93 | { \ | 104 | { \ |
| 94 | L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ | 105 | L, StackChecker::Relative{ offset_ }, \ |
| 95 | } | 106 | } |
| 96 | #define STACK_CHECK_RESET_ABS(L, offset_) \ | 107 | #define STACK_CHECK_RESET_ABS(L, offset_) \ |
| 97 | _stackChecker_##L = StackChecker \ | 108 | _stackChecker_##L = StackChecker \ |
| 98 | { \ | 109 | { \ |
| 99 | L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ | 110 | L, StackChecker::Absolute{ offset_ }, \ |
| 100 | } | 111 | } |
| 101 | #define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__) | 112 | #define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_) |
| 102 | 113 | ||
| 103 | #else // HAVE_LUA_ASSERT() | 114 | #else // HAVE_LUA_ASSERT() |
| 104 | 115 | ||
