diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2022-01-16 20:57:43 +0000 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2022-01-16 20:57:43 +0000 |
commit | 5a06778fbcd09633fa70f6f25177c800eff4f694 (patch) | |
tree | 857da79d192f8fe0e360dee21a72ef053ed41f16 | |
parent | e33bdddc7a91c4483f24b963b34d5c56cd80f617 (diff) | |
download | mlua-5a06778fbcd09633fa70f6f25177c800eff4f694.zip |
Always restore original Lua state after creating Future in async call.
Fixes #121
-rw-r--r-- | src/lua.rs | 18 |
1 files changed, 17 insertions, 1 deletions
@@ -2283,6 +2283,22 @@ impl Lua { } } + struct StateGuard(*mut Lua, *mut ffi::lua_State); + + impl StateGuard { + unsafe fn new(lua: *mut Lua, state: *mut ffi::lua_State) -> Self { + let orig_state = (*lua).state; + (*lua).state = state; + Self(lua, orig_state) + } + } + + impl Drop for StateGuard { + fn drop(&mut self) { + unsafe { (*self.0).state = self.1 } + } + } + unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int { let extra = match ffi::lua_type(state, ffi::lua_upvalueindex(1)) { ffi::LUA_TUSERDATA => { @@ -2304,7 +2320,7 @@ impl Lua { } let lua = &mut (*upvalue).lua; - lua.state = state; + let _guard = StateGuard::new(lua, state); let mut args = MultiValue::new_or_cached(lua); args.reserve(nargs as usize); |