Skip to content

Commit edd508e

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

3 files changed

Lines changed: 10 additions & 16 deletions

File tree

src/state.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use serde::Serialize;
4747
pub(crate) use extra::ExtraData;
4848
pub use raw::RawLua;
4949
pub(crate) use util::callback_error_ext;
50-
use util::StateGuard;
5150

5251
/// Top level Lua struct which represents an instance of Lua VM.
5352
pub struct Lua {
@@ -457,7 +456,6 @@ impl Lua {
457456

458457
callback_error_ext(state, ptr::null_mut(), true, move |extra, nargs| {
459458
let rawlua = (*extra).raw_lua();
460-
let _guard = StateGuard::new(rawlua, state);
461459
let args = A::from_stack_args(nargs, 1, None, rawlua)?;
462460
func(rawlua.lua(), args)?.push_into_stack(rawlua)?;
463461
Ok(1)
@@ -694,7 +692,6 @@ impl Lua {
694692
if XRc::strong_count(&interrupt_cb) > 2 {
695693
return Ok(VmState::Continue); // Don't allow recursion
696694
}
697-
let _guard = StateGuard::new((*extra).raw_lua(), state);
698695
interrupt_cb((*extra).lua())
699696
});
700697
match result {
@@ -772,8 +769,7 @@ impl Lua {
772769
ffi::lua_pushthread(child);
773770
ffi::lua_xmove(child, (*extra).ref_thread, 1);
774771
let value = Thread((*extra).raw_lua().pop_ref_thread(), child);
775-
let _guard = StateGuard::new((*extra).raw_lua(), parent);
776-
callback_error_ext((*extra).raw_lua().state(), extra, false, move |extra, _| {
772+
callback_error_ext(parent, extra, false, move |extra, _| {
777773
callback((*extra).lua(), value)
778774
})
779775
} else {

src/state/raw.rs

Lines changed: 2 additions & 8 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;
@@ -436,7 +436,6 @@ impl RawLua {
436436
match (*extra).hook_callback.clone() {
437437
Some(hook_callback) => {
438438
let rawlua = (*extra).raw_lua();
439-
let _guard = StateGuard::new(rawlua, state);
440439
let debug = Debug::new(rawlua, ar);
441440
hook_callback((*extra).lua(), debug)
442441
}
@@ -467,7 +466,6 @@ impl RawLua {
467466

468467
let status = callback_error_ext(state, ptr::null_mut(), false, |extra, _| {
469468
let rawlua = (*extra).raw_lua();
470-
let _guard = StateGuard::new(rawlua, state);
471469
let debug = Debug::new(rawlua, ar);
472470
let hook_callback = (*hook_callback_ptr).clone();
473471
hook_callback((*extra).lua(), debug)
@@ -1197,7 +1195,6 @@ impl RawLua {
11971195
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
11981196
// The lock must be already held as the callback is executed
11991197
let rawlua = (*extra).raw_lua();
1200-
let _guard = StateGuard::new(rawlua, state);
12011198
match (*upvalue).data {
12021199
Some(ref func) => func(rawlua, nargs),
12031200
None => Err(Error::CallbackDestructed),
@@ -1241,12 +1238,10 @@ impl RawLua {
12411238
// Async functions cannot be scoped and therefore destroyed,
12421239
// so the first upvalue is always valid
12431240
let upvalue = get_userdata::<AsyncCallbackUpvalue>(state, ffi::lua_upvalueindex(1));
1244-
let extra = (*upvalue).extra.get();
1245-
callback_error_ext(state, extra, true, |extra, nargs| {
1241+
callback_error_ext(state, (*upvalue).extra.get(), true, |extra, nargs| {
12461242
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
12471243
// The lock must be already held as the callback is executed
12481244
let rawlua = (*extra).raw_lua();
1249-
let _guard = StateGuard::new(rawlua, state);
12501245

12511246
let func = &*(*upvalue).data;
12521247
let fut = func(rawlua, nargs);
@@ -1271,7 +1266,6 @@ impl RawLua {
12711266
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
12721267
// The lock must be already held as the future is polled
12731268
let rawlua = (*extra).raw_lua();
1274-
let _guard = StateGuard::new(rawlua, state);
12751269

12761270
let fut = &mut (*upvalue).data;
12771271
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
}
@@ -102,7 +102,11 @@ where
102102
// to store a wrapped failure (error or panic) *before* we proceed.
103103
let prealloc_failure = PreallocatedFailure::reserve(state, extra);
104104

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

0 commit comments

Comments
 (0)