Skip to content

Commit c6ef393

Browse files
committed
Turn Lua::entrypoint() to constructor (don't require initializing Lua first)
1 parent d25f2fc commit c6ef393

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

mlua_derive/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ pub fn lua_module(attr: TokenStream, item: TokenStream) -> TokenStream {
6464

6565
#[no_mangle]
6666
unsafe extern "C-unwind" fn #ext_entrypoint_name(state: *mut mlua::lua_State) -> ::std::os::raw::c_int {
67-
let lua = mlua::Lua::init_from_ptr(state);
68-
#skip_memory_check
69-
lua.entrypoint1(state, #func_name)
67+
mlua::Lua::entrypoint1(state, move |lua| {
68+
#skip_memory_check
69+
#func_name(lua)
70+
})
7071
}
7172
};
7273

src/state.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,36 +356,37 @@ impl Lua {
356356
// The returned value then pushed onto the stack.
357357
#[doc(hidden)]
358358
#[cfg(not(tarpaulin_include))]
359-
pub unsafe fn entrypoint<F, A, R>(self, state: *mut ffi::lua_State, func: F) -> c_int
359+
pub unsafe fn entrypoint<F, A, R>(state: *mut ffi::lua_State, func: F) -> c_int
360360
where
361-
F: Fn(&Lua, A) -> Result<R> + MaybeSend + 'static,
361+
F: FnOnce(&Lua, A) -> Result<R>,
362362
A: FromLuaMulti,
363363
R: IntoLua,
364364
{
365-
let extra = self.lock().extra.get();
366-
// `self` is no longer needed and must be dropped at this point to avoid possible memory leak
365+
// Make sure that Lua is initialized
366+
let mut lua = Self::init_from_ptr(state);
367+
lua.collect_garbage = false;
368+
// `Lua` is no longer needed and must be dropped at this point to avoid possible memory leak
367369
// in case of possible longjmp (lua_error) below
368-
drop(self);
369-
370-
callback_error_ext(state, extra, move |nargs| {
371-
let lua = (*extra).lua();
372-
let rawlua = lua.lock();
373-
let _guard = StateGuard::new(&rawlua, state);
374-
let args = A::from_stack_args(nargs, 1, None, &rawlua)?;
375-
func(lua, args)?.push_into_stack(&rawlua)?;
370+
drop(lua);
371+
372+
callback_error_ext(state, ptr::null_mut(), move |extra, nargs| {
373+
let rawlua = (*extra).raw_lua();
374+
let _guard = StateGuard::new(rawlua, state);
375+
let args = A::from_stack_args(nargs, 1, None, rawlua)?;
376+
func(rawlua.lua(), args)?.push_into_stack(rawlua)?;
376377
Ok(1)
377378
})
378379
}
379380

380381
// A simple module entrypoint without arguments
381382
#[doc(hidden)]
382383
#[cfg(not(tarpaulin_include))]
383-
pub unsafe fn entrypoint1<R, F>(self, state: *mut ffi::lua_State, func: F) -> c_int
384+
pub unsafe fn entrypoint1<F, R>(state: *mut ffi::lua_State, func: F) -> c_int
384385
where
386+
F: FnOnce(&Lua) -> Result<R>,
385387
R: IntoLua,
386-
F: Fn(&Lua) -> Result<R> + MaybeSend + 'static,
387388
{
388-
self.entrypoint(state, move |lua, _: ()| func(lua))
389+
Self::entrypoint(state, move |lua, _: ()| func(lua))
389390
}
390391

391392
/// Skips memory checks for some operations.
@@ -575,8 +576,7 @@ impl Lua {
575576
// We don't support GC interrupts since they cannot survive Lua exceptions
576577
return;
577578
}
578-
let extra = ExtraData::get(state);
579-
let result = callback_error_ext(state, extra, move |_| {
579+
let result = callback_error_ext(state, ptr::null_mut(), move |extra, _| {
580580
let interrupt_cb = (*extra).interrupt_callback.clone();
581581
let interrupt_cb = mlua_expect!(interrupt_cb, "no interrupt callback set in interrupt_proc");
582582
if Rc::strong_count(&interrupt_cb) > 2 {
@@ -629,7 +629,7 @@ impl Lua {
629629

630630
unsafe extern "C-unwind" fn warn_proc(ud: *mut c_void, msg: *const c_char, tocont: c_int) {
631631
let extra = ud as *mut ExtraData;
632-
callback_error_ext((*extra).raw_lua().state(), extra, |_| {
632+
callback_error_ext((*extra).raw_lua().state(), extra, |extra, _| {
633633
let cb = mlua_expect!(
634634
(*extra).warn_callback.as_ref(),
635635
"no warning callback set in warn_proc"

src/state/raw.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl RawLua {
360360
ffi::lua_sethook(state, None, 0, 0);
361361
return;
362362
}
363-
callback_error_ext(state, extra, move |_| {
363+
callback_error_ext(state, extra, move |extra, _| {
364364
let hook_cb = (*extra).hook_callback.clone();
365365
let hook_cb = mlua_expect!(hook_cb, "no hook callback set in hook_proc");
366366
if std::rc::Rc::strong_count(&hook_cb) > 2 {
@@ -1038,7 +1038,7 @@ impl RawLua {
10381038
}
10391039
_ => (ptr::null_mut(), ptr::null_mut()),
10401040
};
1041-
callback_error_ext(state, extra, |nargs| {
1041+
callback_error_ext(state, extra, |extra, nargs| {
10421042
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
10431043
if upvalue.is_null() {
10441044
return Err(Error::CallbackDestructed);
@@ -1089,7 +1089,7 @@ impl RawLua {
10891089
// so the first upvalue is always valid
10901090
let upvalue = get_userdata::<AsyncCallbackUpvalue>(state, ffi::lua_upvalueindex(1));
10911091
let extra = (*upvalue).extra.get();
1092-
callback_error_ext(state, extra, |nargs| {
1092+
callback_error_ext(state, extra, |extra, nargs| {
10931093
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
10941094
// The lock must be already held as the callback is executed
10951095
let rawlua = (*extra).raw_lua();
@@ -1114,8 +1114,7 @@ impl RawLua {
11141114

11151115
unsafe extern "C-unwind" fn poll_future(state: *mut ffi::lua_State) -> c_int {
11161116
let upvalue = get_userdata::<AsyncPollUpvalue>(state, ffi::lua_upvalueindex(1));
1117-
let extra = (*upvalue).extra.get();
1118-
callback_error_ext(state, extra, |_| {
1117+
callback_error_ext(state, (*upvalue).extra.get(), |extra, _| {
11191118
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
11201119
// The lock must be already held as the future is polled
11211120
let rawlua = (*extra).raw_lua();

src/state/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(super) unsafe fn callback_error_ext<F, R>(
3232
f: F,
3333
) -> R
3434
where
35-
F: FnOnce(c_int) -> Result<R>,
35+
F: FnOnce(*mut ExtraData, c_int) -> Result<R>,
3636
{
3737
if extra.is_null() {
3838
extra = ExtraData::get(state);
@@ -113,7 +113,7 @@ where
113113
// to store a wrapped failure (error or panic) *before* we proceed.
114114
let prealloc_failure = PreallocatedFailure::reserve(state, extra);
115115

116-
match catch_unwind(AssertUnwindSafe(|| f(nargs))) {
116+
match catch_unwind(AssertUnwindSafe(|| f(extra, nargs))) {
117117
Ok(Ok(r)) => {
118118
// Return unused `WrappedFailure` to the pool
119119
prealloc_failure.release(state, extra);

0 commit comments

Comments
 (0)