From 69f7d1bd745ed5680b9bc4e3cfdd882ff2a5ad26 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 27 Mar 2019 22:18:14 +0800 Subject: +threading --- source/3rd-party/Luax/luax_state.h | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'source/3rd-party/Luax/luax_state.h') diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h index 20d5189..e9c394a 100644 --- a/source/3rd-party/Luax/luax_state.h +++ b/source/3rd-party/Luax/luax_state.h @@ -193,13 +193,13 @@ namespace Luax //------------------------------------------------------------------------------------------------------------ - private: + protected: friend class LuaxContext; LuaxState(lua_State* state); LuaxState(const LuaxState& state); - ~LuaxState(); + virtual ~LuaxState(); /// /// 屏蔽对LuaxState的地址相关操作 @@ -258,6 +258,39 @@ namespace Luax #define LUAX_STATE(L) \ Luax::LuaxState& state = Luax::LuaxRuntime::Get().GetLuaxState(L) + //-------------------------------------------------------------------------------------------------------------- + + /// + /// 确保不安全的lua调用能够在调用之后返回到最初stack状态。 + /// + class LuaxScopedState + : public LuaxState + { + public: + LuaxScopedState(lua_State* state) + : LuaxState(state) + { + mRestoreTop = lua_gettop(mState); + } + LuaxScopedState(const LuaxState& state) + : LuaxState(state) + { + mRestoreTop = lua_gettop(mState); + } + ~LuaxScopedState() + { + if (mState) { + if (lua_gettop(mState) != mRestoreTop) { + lua_settop(mState, mRestoreTop); + } + } + } + private: + void* operator new(size_t); + int mRestoreTop; + + }; + } #endif \ No newline at end of file -- cgit v1.1-26-g67d0