1
1
use std:: borrow:: Cow ;
2
- use std:: ops:: Deref ;
3
- #[ cfg( not( feature = "luau" ) ) ]
4
- use std:: ops:: { BitOr , BitOrAssign } ;
5
2
use std:: os:: raw:: c_int;
6
3
7
4
use ffi:: lua_Debug;
8
5
9
6
use crate :: state:: RawLua ;
10
- use crate :: types:: ReentrantMutexGuard ;
11
7
use crate :: util:: { linenumber_to_usize, ptr_to_lossy_str, ptr_to_str} ;
12
8
13
9
/// Contains information about currently executing Lua code.
@@ -20,51 +16,15 @@ use crate::util::{linenumber_to_usize, ptr_to_lossy_str, ptr_to_str};
20
16
/// [documentation]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
21
17
/// [`Lua::set_hook`]: crate::Lua::set_hook
22
18
pub struct Debug < ' a > {
23
- lua : EitherLua < ' a > ,
24
- ar : ActivationRecord ,
25
- #[ cfg( feature = "luau" ) ]
19
+ lua : & ' a RawLua ,
20
+ #[ cfg_attr( not( feature = "luau" ) , allow( unused) ) ]
26
21
level : c_int ,
27
- }
28
-
29
- enum EitherLua < ' a > {
30
- Owned ( ReentrantMutexGuard < ' a , RawLua > ) ,
31
- #[ cfg( not( feature = "luau" ) ) ]
32
- Borrowed ( & ' a RawLua ) ,
33
- }
34
-
35
- impl Deref for EitherLua < ' _ > {
36
- type Target = RawLua ;
37
-
38
- fn deref ( & self ) -> & Self :: Target {
39
- match self {
40
- EitherLua :: Owned ( guard) => guard,
41
- #[ cfg( not( feature = "luau" ) ) ]
42
- EitherLua :: Borrowed ( lua) => lua,
43
- }
44
- }
22
+ ar : * mut lua_Debug ,
45
23
}
46
24
47
25
impl < ' a > Debug < ' a > {
48
- // We assume the lock is held when this function is called.
49
- #[ cfg( not( feature = "luau" ) ) ]
50
- pub ( crate ) fn new ( lua : & ' a RawLua , ar : * mut lua_Debug ) -> Self {
51
- Debug {
52
- lua : EitherLua :: Borrowed ( lua) ,
53
- ar : ActivationRecord ( ar, false ) ,
54
- }
55
- }
56
-
57
- pub ( crate ) fn new_owned (
58
- guard : ReentrantMutexGuard < ' a , RawLua > ,
59
- _level : c_int ,
60
- ar : Box < lua_Debug > ,
61
- ) -> Self {
62
- Debug {
63
- lua : EitherLua :: Owned ( guard) ,
64
- ar : ActivationRecord ( Box :: into_raw ( ar) , true ) ,
65
- #[ cfg( feature = "luau" ) ]
66
- level : _level,
67
- }
26
+ pub ( crate ) fn new ( lua : & ' a RawLua , level : c_int , ar : * mut lua_Debug ) -> Self {
27
+ Debug { lua, ar, level }
68
28
}
69
29
70
30
/// Returns the specific event that triggered the hook.
@@ -77,7 +37,7 @@ impl<'a> Debug<'a> {
77
37
#[ cfg_attr( docsrs, doc( cfg( not( feature = "luau" ) ) ) ) ]
78
38
pub fn event ( & self ) -> DebugEvent {
79
39
unsafe {
80
- match ( * self . ar . get ( ) ) . event {
40
+ match ( * self . ar ) . event {
81
41
ffi:: LUA_HOOKCALL => DebugEvent :: Call ,
82
42
ffi:: LUA_HOOKRET => DebugEvent :: Ret ,
83
43
ffi:: LUA_HOOKTAILCALL => DebugEvent :: TailCall ,
@@ -93,19 +53,19 @@ impl<'a> Debug<'a> {
93
53
unsafe {
94
54
#[ cfg( not( feature = "luau" ) ) ]
95
55
mlua_assert ! (
96
- ffi:: lua_getinfo( self . lua. state( ) , cstr!( "n" ) , self . ar. get ( ) ) != 0 ,
56
+ ffi:: lua_getinfo( self . lua. state( ) , cstr!( "n" ) , self . ar) != 0 ,
97
57
"lua_getinfo failed with `n`"
98
58
) ;
99
59
#[ cfg( feature = "luau" ) ]
100
60
mlua_assert ! (
101
- ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "n" ) , self . ar. get ( ) ) != 0 ,
61
+ ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "n" ) , self . ar) != 0 ,
102
62
"lua_getinfo failed with `n`"
103
63
) ;
104
64
105
65
DebugNames {
106
- name : ptr_to_lossy_str ( ( * self . ar . get ( ) ) . name ) ,
66
+ name : ptr_to_lossy_str ( ( * self . ar ) . name ) ,
107
67
#[ cfg( not( feature = "luau" ) ) ]
108
- name_what : match ptr_to_str ( ( * self . ar . get ( ) ) . namewhat ) {
68
+ name_what : match ptr_to_str ( ( * self . ar ) . namewhat ) {
109
69
Some ( "" ) => None ,
110
70
val => val,
111
71
} ,
@@ -120,27 +80,27 @@ impl<'a> Debug<'a> {
120
80
unsafe {
121
81
#[ cfg( not( feature = "luau" ) ) ]
122
82
mlua_assert ! (
123
- ffi:: lua_getinfo( self . lua. state( ) , cstr!( "S" ) , self . ar. get ( ) ) != 0 ,
83
+ ffi:: lua_getinfo( self . lua. state( ) , cstr!( "S" ) , self . ar) != 0 ,
124
84
"lua_getinfo failed with `S`"
125
85
) ;
126
86
#[ cfg( feature = "luau" ) ]
127
87
mlua_assert ! (
128
- ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "s" ) , self . ar. get ( ) ) != 0 ,
88
+ ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "s" ) , self . ar) != 0 ,
129
89
"lua_getinfo failed with `s`"
130
90
) ;
131
91
132
92
DebugSource {
133
- source : ptr_to_lossy_str ( ( * self . ar . get ( ) ) . source ) ,
93
+ source : ptr_to_lossy_str ( ( * self . ar ) . source ) ,
134
94
#[ cfg( not( feature = "luau" ) ) ]
135
- short_src : ptr_to_lossy_str ( ( * self . ar . get ( ) ) . short_src . as_ptr ( ) ) ,
95
+ short_src : ptr_to_lossy_str ( ( * self . ar ) . short_src . as_ptr ( ) ) ,
136
96
#[ cfg( feature = "luau" ) ]
137
- short_src : ptr_to_lossy_str ( ( * self . ar . get ( ) ) . short_src ) ,
138
- line_defined : linenumber_to_usize ( ( * self . ar . get ( ) ) . linedefined ) ,
97
+ short_src : ptr_to_lossy_str ( ( * self . ar ) . short_src ) ,
98
+ line_defined : linenumber_to_usize ( ( * self . ar ) . linedefined ) ,
139
99
#[ cfg( not( feature = "luau" ) ) ]
140
- last_line_defined : linenumber_to_usize ( ( * self . ar . get ( ) ) . lastlinedefined ) ,
100
+ last_line_defined : linenumber_to_usize ( ( * self . ar ) . lastlinedefined ) ,
141
101
#[ cfg( feature = "luau" ) ]
142
102
last_line_defined : None ,
143
- what : ptr_to_str ( ( * self . ar . get ( ) ) . what ) . unwrap_or ( "main" ) ,
103
+ what : ptr_to_str ( ( * self . ar ) . what ) . unwrap_or ( "main" ) ,
144
104
}
145
105
}
146
106
}
@@ -150,16 +110,16 @@ impl<'a> Debug<'a> {
150
110
unsafe {
151
111
#[ cfg( not( feature = "luau" ) ) ]
152
112
mlua_assert ! (
153
- ffi:: lua_getinfo( self . lua. state( ) , cstr!( "l" ) , self . ar. get ( ) ) != 0 ,
113
+ ffi:: lua_getinfo( self . lua. state( ) , cstr!( "l" ) , self . ar) != 0 ,
154
114
"lua_getinfo failed with `l`"
155
115
) ;
156
116
#[ cfg( feature = "luau" ) ]
157
117
mlua_assert ! (
158
- ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "l" ) , self . ar. get ( ) ) != 0 ,
118
+ ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "l" ) , self . ar) != 0 ,
159
119
"lua_getinfo failed with `l`"
160
120
) ;
161
121
162
- ( * self . ar . get ( ) ) . currentline
122
+ ( * self . ar ) . currentline
163
123
}
164
124
}
165
125
@@ -170,10 +130,10 @@ impl<'a> Debug<'a> {
170
130
pub fn is_tail_call ( & self ) -> bool {
171
131
unsafe {
172
132
mlua_assert ! (
173
- ffi:: lua_getinfo( self . lua. state( ) , cstr!( "t" ) , self . ar. get ( ) ) != 0 ,
133
+ ffi:: lua_getinfo( self . lua. state( ) , cstr!( "t" ) , self . ar) != 0 ,
174
134
"lua_getinfo failed with `t`"
175
135
) ;
176
- ( * self . ar . get ( ) ) . currentline != 0
136
+ ( * self . ar ) . currentline != 0
177
137
}
178
138
}
179
139
@@ -182,51 +142,34 @@ impl<'a> Debug<'a> {
182
142
unsafe {
183
143
#[ cfg( not( feature = "luau" ) ) ]
184
144
mlua_assert ! (
185
- ffi:: lua_getinfo( self . lua. state( ) , cstr!( "u" ) , self . ar. get ( ) ) != 0 ,
145
+ ffi:: lua_getinfo( self . lua. state( ) , cstr!( "u" ) , self . ar) != 0 ,
186
146
"lua_getinfo failed with `u`"
187
147
) ;
188
148
#[ cfg( feature = "luau" ) ]
189
149
mlua_assert ! (
190
- ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "au" ) , self . ar. get ( ) ) != 0 ,
150
+ ffi:: lua_getinfo( self . lua. state( ) , self . level, cstr!( "au" ) , self . ar) != 0 ,
191
151
"lua_getinfo failed with `au`"
192
152
) ;
193
153
194
154
#[ cfg( not( feature = "luau" ) ) ]
195
155
let stack = DebugStack {
196
- num_ups : ( * self . ar . get ( ) ) . nups as _ ,
156
+ num_ups : ( * self . ar ) . nups as _ ,
197
157
#[ cfg( any( feature = "lua54" , feature = "lua53" , feature = "lua52" ) ) ]
198
- num_params : ( * self . ar . get ( ) ) . nparams as _ ,
158
+ num_params : ( * self . ar ) . nparams as _ ,
199
159
#[ cfg( any( feature = "lua54" , feature = "lua53" , feature = "lua52" ) ) ]
200
- is_vararg : ( * self . ar . get ( ) ) . isvararg != 0 ,
160
+ is_vararg : ( * self . ar ) . isvararg != 0 ,
201
161
} ;
202
162
#[ cfg( feature = "luau" ) ]
203
163
let stack = DebugStack {
204
- num_ups : ( * self . ar . get ( ) ) . nupvals ,
205
- num_params : ( * self . ar . get ( ) ) . nparams ,
206
- is_vararg : ( * self . ar . get ( ) ) . isvararg != 0 ,
164
+ num_ups : ( * self . ar ) . nupvals ,
165
+ num_params : ( * self . ar ) . nparams ,
166
+ is_vararg : ( * self . ar ) . isvararg != 0 ,
207
167
} ;
208
168
stack
209
169
}
210
170
}
211
171
}
212
172
213
- struct ActivationRecord ( * mut lua_Debug , bool ) ;
214
-
215
- impl ActivationRecord {
216
- #[ inline]
217
- fn get ( & self ) -> * mut lua_Debug {
218
- self . 0
219
- }
220
- }
221
-
222
- impl Drop for ActivationRecord {
223
- fn drop ( & mut self ) {
224
- if self . 1 {
225
- drop ( unsafe { Box :: from_raw ( self . 0 ) } ) ;
226
- }
227
- }
228
- }
229
-
230
173
/// Represents a specific event that triggered the hook.
231
174
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
232
175
pub enum DebugEvent {
@@ -385,7 +328,7 @@ impl HookTriggers {
385
328
}
386
329
387
330
#[ cfg( not( feature = "luau" ) ) ]
388
- impl BitOr for HookTriggers {
331
+ impl std :: ops :: BitOr for HookTriggers {
389
332
type Output = Self ;
390
333
391
334
fn bitor ( mut self , rhs : Self ) -> Self :: Output {
@@ -400,7 +343,7 @@ impl BitOr for HookTriggers {
400
343
}
401
344
402
345
#[ cfg( not( feature = "luau" ) ) ]
403
- impl BitOrAssign for HookTriggers {
346
+ impl std :: ops :: BitOrAssign for HookTriggers {
404
347
fn bitor_assign ( & mut self , rhs : Self ) {
405
348
* self = * self | rhs;
406
349
}
0 commit comments