@@ -7,8 +7,6 @@ use crate::error::{Error, Result};
7
7
use crate :: state:: { ExtraData , RawLua } ;
8
8
use crate :: util:: { self , get_internal_metatable, WrappedFailure } ;
9
9
10
- const WRAPPED_FAILURE_POOL_SIZE : usize = 64 ;
11
-
12
10
pub ( super ) struct StateGuard < ' a > ( & ' a RawLua , * mut ffi:: lua_State ) ;
13
11
14
12
impl < ' a > StateGuard < ' a > {
@@ -42,39 +40,40 @@ where
42
40
43
41
enum PreallocatedFailure {
44
42
New ( * mut WrappedFailure ) ,
45
- Existing ( i32 ) ,
43
+ Reserved ,
46
44
}
47
45
48
46
impl PreallocatedFailure {
49
47
unsafe fn reserve ( state : * mut ffi:: lua_State , extra : * mut ExtraData ) -> Self {
50
- match ( * extra) . wrapped_failure_pool . pop ( ) {
51
- Some ( index) => PreallocatedFailure :: Existing ( index) ,
52
- None => {
53
- // We need to check stack for Luau in case when callback is called from interrupt
54
- // See https://github.com/Roblox/luau/issues/446 and mlua #142 and #153
55
- #[ cfg( feature = "luau" ) ]
56
- ffi:: lua_rawcheckstack ( state, 2 ) ;
57
- // Place it to the beginning of the stack
58
- let ud = WrappedFailure :: new_userdata ( state) ;
59
- ffi:: lua_insert ( state, 1 ) ;
60
- PreallocatedFailure :: New ( ud)
61
- }
48
+ if ( * extra) . wrapped_failure_top > 0 {
49
+ ( * extra) . wrapped_failure_top -= 1 ;
50
+ return PreallocatedFailure :: Reserved ;
62
51
}
52
+
53
+ // We need to check stack for Luau in case when callback is called from interrupt
54
+ // See https://github.com/Roblox/luau/issues/446 and mlua #142 and #153
55
+ #[ cfg( feature = "luau" ) ]
56
+ ffi:: lua_rawcheckstack ( state, 2 ) ;
57
+ // Place it to the beginning of the stack
58
+ let ud = WrappedFailure :: new_userdata ( state) ;
59
+ ffi:: lua_insert ( state, 1 ) ;
60
+ PreallocatedFailure :: New ( ud)
63
61
}
64
62
63
+ #[ cold]
65
64
unsafe fn r#use ( & self , state : * mut ffi:: lua_State , extra : * mut ExtraData ) -> * mut WrappedFailure {
66
65
let ref_thread = ( * extra) . ref_thread ;
67
66
match * self {
68
67
PreallocatedFailure :: New ( ud) => {
69
68
ffi:: lua_settop ( state, 1 ) ;
70
69
ud
71
70
}
72
- PreallocatedFailure :: Existing ( index) => {
71
+ PreallocatedFailure :: Reserved => {
72
+ let index = ( * extra) . wrapped_failure_pool . pop ( ) . unwrap ( ) ;
73
73
ffi:: lua_settop ( state, 0 ) ;
74
74
#[ cfg( feature = "luau" ) ]
75
75
ffi:: lua_rawcheckstack ( state, 2 ) ;
76
- ffi:: lua_pushvalue ( ref_thread, index) ;
77
- ffi:: lua_xmove ( ref_thread, state, 1 ) ;
76
+ ffi:: lua_xpush ( ref_thread, state, index) ;
78
77
ffi:: lua_pushnil ( ref_thread) ;
79
78
ffi:: lua_replace ( ref_thread, index) ;
80
79
( * extra) . ref_free . push ( index) ;
@@ -87,24 +86,13 @@ where
87
86
let ref_thread = ( * extra) . ref_thread ;
88
87
match self {
89
88
PreallocatedFailure :: New ( _) => {
90
- if ( * extra) . wrapped_failure_pool . len ( ) < WRAPPED_FAILURE_POOL_SIZE {
91
- ffi:: lua_rotate ( state, 1 , -1 ) ;
92
- ffi:: lua_xmove ( state, ref_thread, 1 ) ;
93
- let index = ref_stack_pop ( extra) ;
94
- ( * extra) . wrapped_failure_pool . push ( index) ;
95
- } else {
96
- ffi:: lua_remove ( state, 1 ) ;
97
- }
98
- }
99
- PreallocatedFailure :: Existing ( index) => {
100
- if ( * extra) . wrapped_failure_pool . len ( ) < WRAPPED_FAILURE_POOL_SIZE {
101
- ( * extra) . wrapped_failure_pool . push ( index) ;
102
- } else {
103
- ffi:: lua_pushnil ( ref_thread) ;
104
- ffi:: lua_replace ( ref_thread, index) ;
105
- ( * extra) . ref_free . push ( index) ;
106
- }
89
+ ffi:: lua_rotate ( state, 1 , -1 ) ;
90
+ ffi:: lua_xmove ( state, ref_thread, 1 ) ;
91
+ let index = ref_stack_pop ( extra) ;
92
+ ( * extra) . wrapped_failure_pool . push ( index) ;
93
+ ( * extra) . wrapped_failure_top += 1 ;
107
94
}
95
+ PreallocatedFailure :: Reserved => ( * extra) . wrapped_failure_top += 1 ,
108
96
}
109
97
}
110
98
}
0 commit comments