@@ -2,8 +2,7 @@ use std::fmt;
22use std:: os:: raw:: { c_int, c_void} ;
33
44use crate :: error:: { Error , Result } ;
5- #[ allow( unused) ]
6- use crate :: state:: Lua ;
5+ use crate :: function:: Function ;
76use crate :: state:: RawLua ;
87use crate :: traits:: { FromLuaMulti , IntoLuaMulti } ;
98use crate :: types:: { LuaType , ValueRef } ;
@@ -232,7 +231,7 @@ impl Thread {
232231 #[ cfg_attr( docsrs, doc( cfg( not( feature = "luau" ) ) ) ) ]
233232 pub fn set_hook < F > ( & self , triggers : HookTriggers , callback : F )
234233 where
235- F : Fn ( & Lua , Debug ) -> Result < crate :: VmState > + MaybeSend + ' static ,
234+ F : Fn ( & crate :: Lua , Debug ) -> Result < crate :: VmState > + MaybeSend + ' static ,
236235 {
237236 let lua = self . 0 . lua . lock ( ) ;
238237 unsafe {
@@ -249,32 +248,37 @@ impl Thread {
249248 /// In Luau: resets to the initial state of a newly created Lua thread.
250249 /// Lua threads in arbitrary states (like yielded or errored) can be reset properly.
251250 ///
252- /// Sets a Lua function for the thread afterwards .
251+ /// Other Lua versions can reset only new or finished threads .
253252 ///
254- /// Requires `feature = "lua54"` OR `feature = "luau"` .
253+ /// Sets a Lua function for the thread afterwards .
255254 ///
256255 /// [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 < ( ) > {
260257 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+ } ,
263279 }
264280
265- let thread_state = self . state ( ) ;
266281 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-
278282 // Push function to the top of the thread stack
279283 ffi:: lua_xpush ( lua. ref_thread ( ) , thread_state, func. 0 . index ) ;
280284
@@ -445,30 +449,19 @@ impl LuaType for Thread {
445449
446450#[ cfg( feature = "async" ) ]
447451impl < R > AsyncThread < R > {
448- #[ inline]
452+ #[ inline( always ) ]
449453 pub ( crate ) fn set_recyclable ( & mut self , recyclable : bool ) {
450454 self . recycle = recyclable;
451455 }
452456}
453457
454458#[ cfg( feature = "async" ) ]
455- #[ cfg( any( feature = "lua54" , feature = "luau" ) ) ]
456459impl < R > Drop for AsyncThread < R > {
457460 fn drop ( & mut self ) {
458461 if self . recycle {
459462 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 ) } ;
472465 }
473466 }
474467 }
@@ -549,7 +542,7 @@ impl<R: FromLuaMulti> Future for AsyncThread<R> {
549542#[ cfg( feature = "async" ) ]
550543#[ inline( always) ]
551544unsafe 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
553546}
554547
555548#[ cfg( feature = "async" ) ]
0 commit comments