File tree 3 files changed +16
-8
lines changed
3 files changed +16
-8
lines changed Original file line number Diff line number Diff line change @@ -703,18 +703,19 @@ impl Lua {
703
703
unsafe extern "C-unwind" fn warn_proc ( ud : * mut c_void , msg : * const c_char , tocont : c_int ) {
704
704
let extra = ud as * mut ExtraData ;
705
705
callback_error_ext ( ( * extra) . raw_lua ( ) . state ( ) , extra, |extra, _| {
706
- let cb = mlua_expect ! (
707
- ( * extra) . warn_callback. as_ref( ) ,
708
- "no warning callback set in warn_proc"
709
- ) ;
706
+ let warn_callback = ( * extra) . warn_callback . clone ( ) ;
707
+ let warn_callback = mlua_expect ! ( warn_callback, "no warning callback set in warn_proc" ) ;
708
+ if XRc :: strong_count ( & warn_callback) > 2 {
709
+ return Ok ( ( ) ) ;
710
+ }
710
711
let msg = StdString :: from_utf8_lossy ( CStr :: from_ptr ( msg) . to_bytes ( ) ) ;
711
- cb ( ( * extra) . lua ( ) , & msg, tocont != 0 )
712
+ warn_callback ( ( * extra) . lua ( ) , & msg, tocont != 0 )
712
713
} ) ;
713
714
}
714
715
715
716
let lua = self . lock ( ) ;
716
717
unsafe {
717
- ( * lua. extra . get ( ) ) . warn_callback = Some ( Box :: new ( callback) ) ;
718
+ ( * lua. extra . get ( ) ) . warn_callback = Some ( XRc :: new ( callback) ) ;
718
719
ffi:: lua_setwarnf ( lua. state ( ) , Some ( warn_proc) , lua. extra . get ( ) as * mut c_void ) ;
719
720
}
720
721
}
Original file line number Diff line number Diff line change @@ -91,10 +91,10 @@ pub(crate) type InterruptCallback = XRc<dyn Fn(&Lua) -> Result<VmState> + Send>;
91
91
pub ( crate ) type InterruptCallback = XRc < dyn Fn ( & Lua ) -> Result < VmState > > ;
92
92
93
93
#[ cfg( all( feature = "send" , feature = "lua54" ) ) ]
94
- pub ( crate ) type WarnCallback = Box < dyn Fn ( & Lua , & str , bool ) -> Result < ( ) > + Send > ;
94
+ pub ( crate ) type WarnCallback = XRc < dyn Fn ( & Lua , & str , bool ) -> Result < ( ) > + Send > ;
95
95
96
96
#[ cfg( all( not( feature = "send" ) , feature = "lua54" ) ) ]
97
- pub ( crate ) type WarnCallback = Box < dyn Fn ( & Lua , & str , bool ) -> Result < ( ) > > ;
97
+ pub ( crate ) type WarnCallback = XRc < dyn Fn ( & Lua , & str , bool ) -> Result < ( ) > > ;
98
98
99
99
/// A trait that adds `Send` requirement if `send` feature is enabled.
100
100
#[ cfg( feature = "send" ) ]
Original file line number Diff line number Diff line change @@ -1289,6 +1289,13 @@ fn test_warnings() -> Result<()> {
1289
1289
if matches!( * cause, Error :: RuntimeError ( ref err) if err == "warning error" )
1290
1290
) ) ;
1291
1291
1292
+ // Recursive warning
1293
+ lua. set_warning_function ( |lua, _, _| {
1294
+ lua. warning ( "inner" , false ) ;
1295
+ Ok ( ( ) )
1296
+ } ) ;
1297
+ lua. warning ( "hello" , false ) ;
1298
+
1292
1299
Ok ( ( ) )
1293
1300
}
1294
1301
You can’t perform that action at this time.
0 commit comments