Always restore original Lua state after creating Future in async call.

Fixes #121
This commit is contained in:
Alex Orlenko 2022-01-16 20:57:43 +00:00
parent e33bdddc7a
commit 5a06778fbc
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
1 changed files with 17 additions and 1 deletions

View File

@ -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 { unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int {
let extra = match ffi::lua_type(state, ffi::lua_upvalueindex(1)) { let extra = match ffi::lua_type(state, ffi::lua_upvalueindex(1)) {
ffi::LUA_TUSERDATA => { ffi::LUA_TUSERDATA => {
@ -2304,7 +2320,7 @@ impl Lua {
} }
let lua = &mut (*upvalue).lua; let lua = &mut (*upvalue).lua;
lua.state = state; let _guard = StateGuard::new(lua, state);
let mut args = MultiValue::new_or_cached(lua); let mut args = MultiValue::new_or_cached(lua);
args.reserve(nargs as usize); args.reserve(nargs as usize);