Skip to content

Commit 3c801e7

Browse files
committed
Expose internal POLL_PENDING constant (hidden)
1 parent a38e484 commit 3c801e7

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

mlua-sys/src/lua51/lua.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ pub unsafe fn lua_getglobal_(L: *mut lua_State, var: *const c_char) {
328328
lua_getfield_(L, LUA_GLOBALSINDEX, var)
329329
}
330330

331+
#[inline(always)]
332+
pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
333+
if lua_islightuserdata(L, idx) != 0 {
334+
return lua_touserdata(L, idx);
335+
}
336+
ptr::null_mut()
337+
}
338+
331339
#[inline(always)]
332340
pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
333341
lua_tolstring(L, i, ptr::null_mut())

mlua-sys/src/lua52/lua.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ pub unsafe fn lua_pushglobaltable(L: *mut lua_State) {
417417
lua_rawgeti_(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS as _)
418418
}
419419

420+
#[inline(always)]
421+
pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
422+
if lua_islightuserdata(L, idx) != 0 {
423+
return lua_touserdata(L, idx);
424+
}
425+
ptr::null_mut()
426+
}
427+
420428
#[inline(always)]
421429
pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
422430
lua_tolstring(L, i, ptr::null_mut())

mlua-sys/src/lua53/lua.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ pub unsafe fn lua_pushglobaltable(L: *mut lua_State) -> c_int {
424424
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
425425
}
426426

427+
#[inline(always)]
428+
pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
429+
if lua_islightuserdata(L, idx) != 0 {
430+
return lua_touserdata(L, idx);
431+
}
432+
ptr::null_mut()
433+
}
434+
427435
#[inline(always)]
428436
pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
429437
lua_tolstring(L, i, ptr::null_mut())

mlua-sys/src/lua54/lua.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,14 @@ pub unsafe fn lua_pushglobaltable(L: *mut lua_State) -> c_int {
457457
lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)
458458
}
459459

460+
#[inline(always)]
461+
pub unsafe fn lua_tolightuserdata(L: *mut lua_State, idx: c_int) -> *mut c_void {
462+
if lua_islightuserdata(L, idx) != 0 {
463+
return lua_touserdata(L, idx);
464+
}
465+
ptr::null_mut()
466+
}
467+
460468
#[inline(always)]
461469
pub unsafe fn lua_tostring(L: *mut lua_State, i: c_int) -> *const c_char {
462470
lua_tolstring(L, i, ptr::null_mut())

src/lua.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,8 +2965,7 @@ impl Lua {
29652965
match fut.as_mut().poll(&mut ctx) {
29662966
Poll::Pending => {
29672967
ffi::lua_pushnil(state);
2968-
let pending = &ASYNC_POLL_PENDING as *const u8 as *mut c_void;
2969-
ffi::lua_pushlightuserdata(state, pending);
2968+
ffi::lua_pushlightuserdata(state, Lua::poll_pending().0);
29702969
Ok(2)
29712970
}
29722971
Poll::Ready(nresults) => {
@@ -3069,6 +3068,14 @@ impl Lua {
30693068
mem::replace(&mut (*self.extra.get()).waker, waker)
30703069
}
30713070

3071+
/// Returns internal `Poll::Pending` constant used for executing async callbacks.
3072+
#[cfg(feature = "async")]
3073+
#[doc(hidden)]
3074+
#[inline]
3075+
pub fn poll_pending() -> LightUserData {
3076+
LightUserData(&ASYNC_POLL_PENDING as *const u8 as *mut c_void)
3077+
}
3078+
30723079
pub(crate) unsafe fn make_userdata<T>(&self, data: UserDataCell<T>) -> Result<AnyUserData>
30733080
where
30743081
T: UserData + 'static,

src/thread.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515

1616
#[cfg(feature = "async")]
1717
use {
18-
crate::{lua::ASYNC_POLL_PENDING, value::MultiValue},
18+
crate::value::MultiValue,
1919
futures_util::stream::Stream,
2020
std::{
2121
future::Future,
@@ -530,12 +530,7 @@ where
530530
#[cfg(feature = "async")]
531531
#[inline(always)]
532532
unsafe fn is_poll_pending(state: *mut ffi::lua_State) -> bool {
533-
if ffi::lua_islightuserdata(state, -1) != 0 {
534-
let stack_ptr = ffi::lua_touserdata(state, -1) as *const u8;
535-
let pending_ptr = &ASYNC_POLL_PENDING as *const u8;
536-
return std::ptr::eq(stack_ptr, pending_ptr);
537-
}
538-
false
533+
ffi::lua_tolightuserdata(state, -1) == Lua::poll_pending().0
539534
}
540535

541536
#[cfg(feature = "async")]

0 commit comments

Comments
 (0)