@@ -2961,19 +2961,23 @@ impl Lua {
2961
2961
let fut = & mut ( * upvalue) . data ;
2962
2962
let mut ctx = Context :: from_waker ( lua. waker ( ) ) ;
2963
2963
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
+ }
2965
2970
Poll :: Ready ( nresults) => {
2966
- let nresults = nresults?;
2967
- match nresults {
2968
- 0 ..=2 => {
2971
+ match nresults? {
2972
+ nresults @ 0 ..=2 => {
2969
2973
// Fast path for up to 2 results without creating a table
2970
2974
ffi:: lua_pushinteger ( state, nresults as _ ) ;
2971
2975
if nresults > 0 {
2972
2976
ffi:: lua_insert ( state, -nresults - 1 ) ;
2973
2977
}
2974
2978
Ok ( nresults + 1 )
2975
2979
}
2976
- _ => {
2980
+ nresults => {
2977
2981
let results = MultiValue :: from_stack_multi ( nresults, lua) ?;
2978
2982
ffi:: lua_pushinteger ( state, nresults as _ ) ;
2979
2983
lua. push_value ( Value :: Table ( lua. create_sequence_from ( results) ?) ) ?;
@@ -3017,15 +3021,13 @@ impl Lua {
3017
3021
3018
3022
let coroutine = self . globals ( ) . get :: < _ , Table > ( "coroutine" ) ?;
3019
3023
3020
- let env = self . create_table_with_capacity ( 0 , 4 ) ?;
3024
+ let env = self . create_table_with_capacity ( 0 , 3 ) ?;
3021
3025
env. set ( "get_poll" , get_poll) ?;
3026
+ // Cache `yield` function
3022
3027
env. set ( "yield" , coroutine. get :: < _ , Function > ( "yield" ) ?) ?;
3023
3028
unsafe {
3024
3029
env. set ( "unpack" , self . create_c_function ( unpack) ?) ?;
3025
3030
}
3026
- env. set ( "pending" , {
3027
- LightUserData ( & ASYNC_POLL_PENDING as * const u8 as * mut c_void )
3028
- } ) ?;
3029
3031
3030
3032
self . load (
3031
3033
r#"
@@ -3043,7 +3045,7 @@ impl Lua {
3043
3045
return unpack(res, nres)
3044
3046
end
3045
3047
end
3046
- yield(pending)
3048
+ yield(res) -- `res` is a "pending" value
3047
3049
end
3048
3050
"# ,
3049
3051
)
0 commit comments