Skip to content

Commit e4d6e92

Browse files
committed
(async) Move "pending" poll value from env to poll_future() results
1 parent 642201a commit e4d6e92

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/lua.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,19 +2961,23 @@ impl Lua {
29612961
let fut = &mut (*upvalue).data;
29622962
let mut ctx = Context::from_waker(lua.waker());
29632963
match fut.as_mut().poll(&mut ctx) {
2964-
Poll::Pending => Ok(0),
2964+
Poll::Pending => {
2965+
ffi::lua_pushnil(state);
2966+
let pending = &ASYNC_POLL_PENDING as *const u8 as *mut c_void;
2967+
ffi::lua_pushlightuserdata(state, pending);
2968+
Ok(2)
2969+
}
29652970
Poll::Ready(nresults) => {
2966-
let nresults = nresults?;
2967-
match nresults {
2968-
0..=2 => {
2971+
match nresults? {
2972+
nresults @ 0..=2 => {
29692973
// Fast path for up to 2 results without creating a table
29702974
ffi::lua_pushinteger(state, nresults as _);
29712975
if nresults > 0 {
29722976
ffi::lua_insert(state, -nresults - 1);
29732977
}
29742978
Ok(nresults + 1)
29752979
}
2976-
_ => {
2980+
nresults => {
29772981
let results = MultiValue::from_stack_multi(nresults, lua)?;
29782982
ffi::lua_pushinteger(state, nresults as _);
29792983
lua.push_value(Value::Table(lua.create_sequence_from(results)?))?;
@@ -3017,15 +3021,13 @@ impl Lua {
30173021

30183022
let coroutine = self.globals().get::<_, Table>("coroutine")?;
30193023

3020-
let env = self.create_table_with_capacity(0, 4)?;
3024+
let env = self.create_table_with_capacity(0, 3)?;
30213025
env.set("get_poll", get_poll)?;
3026+
// Cache `yield` function
30223027
env.set("yield", coroutine.get::<_, Function>("yield")?)?;
30233028
unsafe {
30243029
env.set("unpack", self.create_c_function(unpack)?)?;
30253030
}
3026-
env.set("pending", {
3027-
LightUserData(&ASYNC_POLL_PENDING as *const u8 as *mut c_void)
3028-
})?;
30293031

30303032
self.load(
30313033
r#"
@@ -3043,7 +3045,7 @@ impl Lua {
30433045
return unpack(res, nres)
30443046
end
30453047
end
3046-
yield(pending)
3048+
yield(res) -- `res` is a "pending" value
30473049
end
30483050
"#,
30493051
)

0 commit comments

Comments
 (0)