Skip to content

Commit c4956db

Browse files
committed
Make StateGuard automatically enabled inside callback_error_ext.
Remove manual usage of `StateGuard` in other places. Closes #567
1 parent d0ea428 commit c4956db

3 files changed

Lines changed: 10 additions & 13 deletions

File tree

src/state.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use serde::Serialize;
4646

4747
pub(crate) use extra::ExtraData;
4848
pub use raw::RawLua;
49-
use util::{callback_error_ext, StateGuard};
49+
use util::callback_error_ext;
5050

5151
/// Top level Lua struct which represents an instance of Lua VM.
5252
pub struct Lua {
@@ -430,7 +430,6 @@ impl Lua {
430430

431431
callback_error_ext(state, ptr::null_mut(), move |extra, nargs| {
432432
let rawlua = (*extra).raw_lua();
433-
let _guard = StateGuard::new(rawlua, state);
434433
let args = A::from_stack_args(nargs, 1, None, rawlua)?;
435434
func(rawlua.lua(), args)?.push_into_stack(rawlua)?;
436435
Ok(1)
@@ -648,7 +647,6 @@ impl Lua {
648647
if Rc::strong_count(&interrupt_cb) > 2 {
649648
return Ok(VmState::Continue); // Don't allow recursion
650649
}
651-
let _guard = StateGuard::new((*extra).raw_lua(), state);
652650
interrupt_cb((*extra).lua())
653651
});
654652
match result {

src/state/raw.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::chunk::ChunkMode;
1212
use crate::error::{Error, Result};
1313
use crate::function::Function;
1414
use crate::memory::{MemoryState, ALLOCATOR};
15-
use crate::state::util::{callback_error_ext, ref_stack_pop, StateGuard};
15+
use crate::state::util::{callback_error_ext, ref_stack_pop};
1616
use crate::stdlib::StdLib;
1717
use crate::string::String;
1818
use crate::table::Table;
@@ -401,7 +401,6 @@ impl RawLua {
401401
return Ok(VmState::Continue); // Don't allow recursion
402402
}
403403
let rawlua = (*extra).raw_lua();
404-
let _guard = StateGuard::new(rawlua, state);
405404
let debug = Debug::new(rawlua, ar);
406405
hook_cb((*extra).lua(), debug)
407406
});
@@ -1105,7 +1104,6 @@ impl RawLua {
11051104
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
11061105
// The lock must be already held as the callback is executed
11071106
let rawlua = (*extra).raw_lua();
1108-
let _guard = StateGuard::new(rawlua, state);
11091107
match (*upvalue).data {
11101108
Some(ref func) => func(rawlua, nargs),
11111109
None => Err(Error::CallbackDestructed),
@@ -1149,12 +1147,10 @@ impl RawLua {
11491147
// Async functions cannot be scoped and therefore destroyed,
11501148
// so the first upvalue is always valid
11511149
let upvalue = get_userdata::<AsyncCallbackUpvalue>(state, ffi::lua_upvalueindex(1));
1152-
let extra = (*upvalue).extra.get();
1153-
callback_error_ext(state, extra, |extra, nargs| {
1150+
callback_error_ext(state, (*upvalue).extra.get(), |extra, nargs| {
11541151
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
11551152
// The lock must be already held as the callback is executed
11561153
let rawlua = (*extra).raw_lua();
1157-
let _guard = StateGuard::new(rawlua, state);
11581154

11591155
let func = &*(*upvalue).data;
11601156
let fut = func(rawlua, nargs);
@@ -1179,7 +1175,6 @@ impl RawLua {
11791175
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
11801176
// The lock must be already held as the future is polled
11811177
let rawlua = (*extra).raw_lua();
1182-
let _guard = StateGuard::new(rawlua, state);
11831178

11841179
let fut = &mut (*upvalue).data;
11851180
let mut ctx = Context::from_waker(rawlua.waker());

src/state/util.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use crate::error::{Error, Result};
77
use crate::state::{ExtraData, RawLua};
88
use crate::util::{self, get_internal_metatable, WrappedFailure};
99

10-
pub(super) struct StateGuard<'a>(&'a RawLua, *mut ffi::lua_State);
10+
struct StateGuard<'a>(&'a RawLua, *mut ffi::lua_State);
1111

1212
impl<'a> StateGuard<'a> {
13-
pub(super) fn new(inner: &'a RawLua, mut state: *mut ffi::lua_State) -> Self {
13+
fn new(inner: &'a RawLua, mut state: *mut ffi::lua_State) -> Self {
1414
state = inner.state.replace(state);
1515
Self(inner, state)
1616
}
@@ -101,7 +101,11 @@ where
101101
// to store a wrapped failure (error or panic) *before* we proceed.
102102
let prealloc_failure = PreallocatedFailure::reserve(state, extra);
103103

104-
match catch_unwind(AssertUnwindSafe(|| f(extra, nargs))) {
104+
match catch_unwind(AssertUnwindSafe(|| {
105+
let rawlua = (*extra).raw_lua();
106+
let _guard = StateGuard::new(rawlua, state);
107+
f(extra, nargs)
108+
})) {
105109
Ok(Ok(r)) => {
106110
// Return unused `WrappedFailure` to the pool
107111
prealloc_failure.release(state, extra);

0 commit comments

Comments
 (0)