@@ -3,29 +3,32 @@ use std::os::raw::c_int;
3
3
4
4
use ffi:: { lua_Debug, lua_State} ;
5
5
6
+ use crate :: function:: Function ;
6
7
use crate :: state:: RawLua ;
7
- use crate :: util:: { linenumber_to_usize, ptr_to_lossy_str, ptr_to_str} ;
8
+ use crate :: util:: { assert_stack , linenumber_to_usize, ptr_to_lossy_str, ptr_to_str, StackGuard } ;
8
9
9
10
/// Contains information about currently executing Lua code.
10
11
///
11
- /// The `Debug` structure is provided as a parameter to the hook function set with
12
- /// [`Lua::set_hook`]. You may call the methods on this structure to retrieve information about the
13
- /// Lua code executing at the time that the hook function was called. Further information can be
14
- /// found in the Lua [documentation].
12
+ /// You may call the methods on this structure to retrieve information about the Lua code executing
13
+ /// at the specific level. Further information can be found in the Lua [documentation].
15
14
///
16
15
/// [documentation]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
17
- /// [`Lua::set_hook`]: crate::Lua::set_hook
18
- pub struct Debug {
16
+ pub struct Debug < ' a > {
19
17
state : * mut lua_State ,
18
+ lua : & ' a RawLua ,
20
19
#[ cfg_attr( not( feature = "luau" ) , allow( unused) ) ]
21
20
level : c_int ,
22
21
ar : * mut lua_Debug ,
23
22
}
24
23
25
- impl Debug {
26
- pub ( crate ) fn new ( lua : & RawLua , level : c_int , ar : * mut lua_Debug ) -> Self {
27
- let state = lua. state ( ) ;
28
- Debug { state, ar, level }
24
+ impl < ' a > Debug < ' a > {
25
+ pub ( crate ) fn new ( lua : & ' a RawLua , level : c_int , ar : * mut lua_Debug ) -> Self {
26
+ Debug {
27
+ state : lua. state ( ) ,
28
+ lua,
29
+ ar,
30
+ level,
31
+ }
29
32
}
30
33
31
34
/// Returns the specific event that triggered the hook.
@@ -49,7 +52,31 @@ impl Debug {
49
52
}
50
53
}
51
54
52
- /// Corresponds to the `n` what mask.
55
+ /// Returns the function that is running at the given level.
56
+ ///
57
+ /// Corresponds to the `f` "what" mask.
58
+ pub fn function ( & self ) -> Function {
59
+ unsafe {
60
+ let _sg = StackGuard :: new ( self . state ) ;
61
+ assert_stack ( self . state , 1 ) ;
62
+
63
+ #[ cfg( not( feature = "luau" ) ) ]
64
+ mlua_assert ! (
65
+ ffi:: lua_getinfo( self . state, cstr!( "f" ) , self . ar) != 0 ,
66
+ "lua_getinfo failed with `f`"
67
+ ) ;
68
+ #[ cfg( feature = "luau" ) ]
69
+ mlua_assert ! (
70
+ ffi:: lua_getinfo( self . state, self . level, cstr!( "f" ) , self . ar) != 0 ,
71
+ "lua_getinfo failed with `f`"
72
+ ) ;
73
+
74
+ ffi:: lua_xmove ( self . state , self . lua . ref_thread ( ) , 1 ) ;
75
+ Function ( self . lua . pop_ref_thread ( ) )
76
+ }
77
+ }
78
+
79
+ /// Corresponds to the `n` "what" mask.
53
80
pub fn names ( & self ) -> DebugNames < ' _ > {
54
81
unsafe {
55
82
#[ cfg( not( feature = "luau" ) ) ]
@@ -76,7 +103,7 @@ impl Debug {
76
103
}
77
104
}
78
105
79
- /// Corresponds to the `S` what mask.
106
+ /// Corresponds to the `S` " what" mask.
80
107
pub fn source ( & self ) -> DebugSource < ' _ > {
81
108
unsafe {
82
109
#[ cfg( not( feature = "luau" ) ) ]
@@ -106,7 +133,7 @@ impl Debug {
106
133
}
107
134
}
108
135
109
- /// Corresponds to the `l` what mask. Returns the current line.
136
+ /// Corresponds to the `l` " what" mask. Returns the current line.
110
137
pub fn curr_line ( & self ) -> i32 {
111
138
unsafe {
112
139
#[ cfg( not( feature = "luau" ) ) ]
@@ -124,8 +151,8 @@ impl Debug {
124
151
}
125
152
}
126
153
127
- /// Corresponds to the `t` what mask. Returns true if the hook is in a function tail call, false
128
- /// otherwise.
154
+ /// Corresponds to the `t` " what" mask. Returns true if the hook is in a function tail call,
155
+ /// false otherwise.
129
156
#[ cfg( not( feature = "luau" ) ) ]
130
157
#[ cfg_attr( docsrs, doc( cfg( not( feature = "luau" ) ) ) ) ]
131
158
pub fn is_tail_call ( & self ) -> bool {
@@ -138,7 +165,7 @@ impl Debug {
138
165
}
139
166
}
140
167
141
- /// Corresponds to the `u` what mask.
168
+ /// Corresponds to the `u` " what" mask.
142
169
pub fn stack ( & self ) -> DebugStack {
143
170
unsafe {
144
171
#[ cfg( not( feature = "luau" ) ) ]
0 commit comments