2021-03-06 23:14:48 +08:00
|
|
|
// sol2
|
2017-12-20 17:58:32 +08:00
|
|
|
|
2017-09-13 14:46:56 +08:00
|
|
|
// The MIT License (MIT)
|
2016-04-04 14:28:39 +08:00
|
|
|
|
2022-06-25 16:00:53 +08:00
|
|
|
// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
|
2016-04-04 14:28:39 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_STACK_GUARD_HPP
|
|
|
|
#define SOL_STACK_GUARD_HPP
|
|
|
|
|
2020-08-10 05:17:06 +08:00
|
|
|
#include <sol/compatibility/lua_version.hpp>
|
|
|
|
#include <sol/error.hpp>
|
2016-04-04 14:28:39 +08:00
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace sol {
|
2016-06-20 05:59:40 +08:00
|
|
|
namespace detail {
|
|
|
|
inline void stack_fail(int, int) {
|
2022-06-24 09:43:37 +08:00
|
|
|
#if SOL_IS_ON(SOL_EXCEPTIONS)
|
2016-06-20 05:59:40 +08:00
|
|
|
throw error(detail::direct_error, "imbalanced stack after operation finish");
|
2016-04-04 14:28:39 +08:00
|
|
|
#else
|
2016-06-20 05:59:40 +08:00
|
|
|
// Lol, what do you want, an error printout? :3c
|
|
|
|
// There's no sane default here. The right way would be C-style abort(), and that's not acceptable, so
|
|
|
|
// hopefully someone will register their own stack_fail thing for the `fx` parameter of stack_guard.
|
2016-04-04 14:28:39 +08:00
|
|
|
#endif // No Exceptions
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace detail
|
2016-06-20 05:59:40 +08:00
|
|
|
|
|
|
|
struct stack_guard {
|
|
|
|
lua_State* L;
|
|
|
|
int top;
|
|
|
|
std::function<void(int, int)> on_mismatch;
|
|
|
|
|
2020-07-04 15:58:24 +08:00
|
|
|
stack_guard(lua_State* L) : stack_guard(L, lua_gettop(L)) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2020-07-04 15:58:24 +08:00
|
|
|
stack_guard(lua_State* L, int top, std::function<void(int, int)> fx = detail::stack_fail) : L(L), top(top), on_mismatch(std::move(fx)) {
|
2017-09-13 14:46:56 +08:00
|
|
|
}
|
2016-10-05 11:37:08 +08:00
|
|
|
bool check_stack(int modification = 0) const {
|
|
|
|
int bottom = lua_gettop(L) + modification;
|
2016-06-20 05:59:40 +08:00
|
|
|
if (top == bottom) {
|
2016-10-05 11:37:08 +08:00
|
|
|
return true;
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
on_mismatch(top, bottom);
|
2016-10-05 11:37:08 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
~stack_guard() {
|
|
|
|
check_stack();
|
2016-06-20 05:59:40 +08:00
|
|
|
}
|
|
|
|
};
|
2017-09-13 14:46:56 +08:00
|
|
|
} // namespace sol
|
2016-04-04 14:28:39 +08:00
|
|
|
|
|
|
|
#endif // SOL_STACK_GUARD_HPP
|