summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2022-01-16 20:57:43 +0000
committerAlex Orlenko <zxteam@protonmail.com>2022-01-16 20:57:43 +0000
commit5a06778fbcd09633fa70f6f25177c800eff4f694 (patch)
tree857da79d192f8fe0e360dee21a72ef053ed41f16 /src
parente33bdddc7a91c4483f24b963b34d5c56cd80f617 (diff)
downloadmlua-5a06778fbcd09633fa70f6f25177c800eff4f694.zip
Always restore original Lua state after creating Future in async call.
Fixes #121
Diffstat (limited to 'src')
-rw-r--r--src/lua.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/lua.rs b/src/lua.rs
index 6da6ccb..9e95b25 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -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);