@@ -2,8 +2,7 @@ use std::fmt;
2
2
use std:: os:: raw:: { c_int, c_void} ;
3
3
4
4
use crate :: error:: { Error , Result } ;
5
- #[ allow( unused) ]
6
- use crate :: state:: Lua ;
5
+ use crate :: function:: Function ;
7
6
use crate :: state:: RawLua ;
8
7
use crate :: traits:: { FromLuaMulti , IntoLuaMulti } ;
9
8
use crate :: types:: { LuaType , ValueRef } ;
@@ -232,7 +231,7 @@ impl Thread {
232
231
#[ cfg_attr( docsrs, doc( cfg( not( feature = "luau" ) ) ) ) ]
233
232
pub fn set_hook < F > ( & self , triggers : HookTriggers , callback : F )
234
233
where
235
- F : Fn ( & Lua , Debug ) -> Result < crate :: VmState > + MaybeSend + ' static ,
234
+ F : Fn ( & crate :: Lua , Debug ) -> Result < crate :: VmState > + MaybeSend + ' static ,
236
235
{
237
236
let lua = self . 0 . lua . lock ( ) ;
238
237
unsafe {
@@ -249,32 +248,37 @@ impl Thread {
249
248
/// In Luau: resets to the initial state of a newly created Lua thread.
250
249
/// Lua threads in arbitrary states (like yielded or errored) can be reset properly.
251
250
///
252
- /// Sets a Lua function for the thread afterwards .
251
+ /// Other Lua versions can reset only new or finished threads .
253
252
///
254
- /// Requires `feature = "lua54"` OR `feature = "luau"` .
253
+ /// Sets a Lua function for the thread afterwards .
255
254
///
256
255
/// [Lua 5.4]: https://www.lua.org/manual/5.4/manual.html#lua_closethread
257
- #[ cfg( any( feature = "lua54" , feature = "luau" ) ) ]
258
- #[ cfg_attr( docsrs, doc( cfg( any( feature = "lua54" , feature = "luau" ) ) ) ) ]
259
- pub fn reset ( & self , func : crate :: function:: Function ) -> Result < ( ) > {
256
+ pub fn reset ( & self , func : Function ) -> Result < ( ) > {
260
257
let lua = self . 0 . lua . lock ( ) ;
261
- if matches ! ( self . status_inner( & lua) , ThreadStatusInner :: Running ) {
262
- return Err ( Error :: runtime ( "cannot reset a running thread" ) ) ;
258
+ let thread_state = self . state ( ) ;
259
+ match self . status_inner ( & lua) {
260
+ ThreadStatusInner :: Running => return Err ( Error :: runtime ( "cannot reset a running thread" ) ) ,
261
+ // Any Lua can reuse new or finished thread
262
+ ThreadStatusInner :: New ( _) => unsafe { ffi:: lua_settop ( thread_state, 0 ) } ,
263
+ ThreadStatusInner :: Finished => { }
264
+ #[ cfg( not( any( feature = "lua54" , feature = "luau" ) ) ) ]
265
+ _ => return Err ( Error :: runtime ( "cannot reset non-finished thread" ) ) ,
266
+ #[ cfg( any( feature = "lua54" , feature = "luau" ) ) ]
267
+ _ => unsafe {
268
+ #[ cfg( all( feature = "lua54" , not( feature = "vendored" ) ) ) ]
269
+ let status = ffi:: lua_resetthread ( thread_state) ;
270
+ #[ cfg( all( feature = "lua54" , feature = "vendored" ) ) ]
271
+ let status = ffi:: lua_closethread ( thread_state, lua. state ( ) ) ;
272
+ #[ cfg( feature = "lua54" ) ]
273
+ if status != ffi:: LUA_OK {
274
+ return Err ( pop_error ( thread_state, status) ) ;
275
+ }
276
+ #[ cfg( feature = "luau" ) ]
277
+ ffi:: lua_resetthread ( thread_state) ;
278
+ } ,
263
279
}
264
280
265
- let thread_state = self . state ( ) ;
266
281
unsafe {
267
- #[ cfg( all( feature = "lua54" , not( feature = "vendored" ) ) ) ]
268
- let status = ffi:: lua_resetthread ( thread_state) ;
269
- #[ cfg( all( feature = "lua54" , feature = "vendored" ) ) ]
270
- let status = ffi:: lua_closethread ( thread_state, lua. state ( ) ) ;
271
- #[ cfg( feature = "lua54" ) ]
272
- if status != ffi:: LUA_OK {
273
- return Err ( pop_error ( thread_state, status) ) ;
274
- }
275
- #[ cfg( feature = "luau" ) ]
276
- ffi:: lua_resetthread ( thread_state) ;
277
-
278
282
// Push function to the top of the thread stack
279
283
ffi:: lua_xpush ( lua. ref_thread ( ) , thread_state, func. 0 . index ) ;
280
284
@@ -445,30 +449,19 @@ impl LuaType for Thread {
445
449
446
450
#[ cfg( feature = "async" ) ]
447
451
impl < R > AsyncThread < R > {
448
- #[ inline]
452
+ #[ inline( always ) ]
449
453
pub ( crate ) fn set_recyclable ( & mut self , recyclable : bool ) {
450
454
self . recycle = recyclable;
451
455
}
452
456
}
453
457
454
458
#[ cfg( feature = "async" ) ]
455
- #[ cfg( any( feature = "lua54" , feature = "luau" ) ) ]
456
459
impl < R > Drop for AsyncThread < R > {
457
460
fn drop ( & mut self ) {
458
461
if self . recycle {
459
462
if let Some ( lua) = self . thread . 0 . lua . try_lock ( ) {
460
- unsafe {
461
- // For Lua 5.4 this also closes all pending to-be-closed variables
462
- if !lua. recycle_thread ( & mut self . thread ) {
463
- #[ cfg( feature = "lua54" ) ]
464
- if matches ! ( self . thread. status_inner( & lua) , ThreadStatusInner :: Error ) {
465
- #[ cfg( not( feature = "vendored" ) ) ]
466
- ffi:: lua_resetthread ( self . thread . state ( ) ) ;
467
- #[ cfg( feature = "vendored" ) ]
468
- ffi:: lua_closethread ( self . thread . state ( ) , lua. state ( ) ) ;
469
- }
470
- }
471
- }
463
+ // For Lua 5.4 this also closes all pending to-be-closed variables
464
+ unsafe { lua. recycle_thread ( & mut self . thread ) } ;
472
465
}
473
466
}
474
467
}
@@ -549,7 +542,7 @@ impl<R: FromLuaMulti> Future for AsyncThread<R> {
549
542
#[ cfg( feature = "async" ) ]
550
543
#[ inline( always) ]
551
544
unsafe fn is_poll_pending ( state : * mut ffi:: lua_State ) -> bool {
552
- ffi:: lua_tolightuserdata ( state, -1 ) == Lua :: poll_pending ( ) . 0
545
+ ffi:: lua_tolightuserdata ( state, -1 ) == crate :: Lua :: poll_pending ( ) . 0
553
546
}
554
547
555
548
#[ cfg( feature = "async" ) ]
0 commit comments