@@ -7,14 +7,29 @@ pub use result::*;
77mod returns;
88pub use returns:: * ;
99
10- use crate :: { FromLua , IntoLua , LuaFunction , LuaTable , LuaValue , types:: LuaState } ;
11- use std:: ffi:: { CStr , c_char , c_int} ;
10+ use crate :: { IntoLua , LuaFunction , LuaTable , LuaValue , types:: LuaState } ;
11+ use std:: ffi:: { CStr , c_int} ;
1212
1313#[ derive( Debug ) ]
1414pub struct LuaApi {
1515 pub raw : RawLuaApi ,
1616}
1717
18+ #[ derive( Debug ) ]
19+ pub struct DebugInfo {
20+ pub event : c_int ,
21+ pub name : Option < String > , // n
22+ pub namewhat : Option < String > , // n
23+ pub what : Option < String > , // S
24+ pub source : Option < String > , // S
25+ pub currentline : Option < c_int > , // l
26+ pub nups : Option < c_int > , // u
27+ pub linedefined : Option < c_int > , // S
28+ pub lastlinedefined : Option < c_int > , // S
29+ pub short_src : Option < String > , // S
30+ pub func : Option < LuaFunction > , // f
31+ }
32+
1833impl LuaApi {
1934 pub fn new ( lib : & libloading:: Library ) -> Result < Self , libloading:: Error > {
2035 let raw = RawLuaApi :: new ( lib) ?;
@@ -39,6 +54,15 @@ impl LuaApi {
3954 Ok ( ( ) )
4055 }
4156
57+ pub fn getfenv ( & self , state : * mut LuaState , f : & LuaFunction ) -> LuaResult < Option < LuaTable > > {
58+ self . raw . push ( state, f) ;
59+ self . raw . getfenv ( state, -1 ) ;
60+ let env: Option < LuaTable > = self . raw . try_to ( state, -1 ) ?;
61+ self . raw . pop ( state, 2 ) ;
62+
63+ Ok ( env)
64+ }
65+
4266 pub fn getregistry ( & self , state : * mut LuaState , key : impl IntoLua ) -> LuaValue < ' _ > {
4367 key. into_lua ( & self . raw , state) ;
4468 self . raw . rawget ( state, REGISTRY_INDEX ) ;
@@ -57,6 +81,83 @@ impl LuaApi {
5781 self . raw . push ( state, msg) ;
5882 self . raw . error ( state) ;
5983 }
84+
85+ pub fn equal ( & self , state : * mut LuaState , a : impl IntoLua , b : impl IntoLua ) -> bool {
86+ a. into_lua ( & self . raw , state) ;
87+ b. into_lua ( & self . raw , state) ;
88+ let result = self . raw . equal ( state, -2 , -1 ) ;
89+ self . raw . pop ( state, 2 ) ;
90+ result
91+ }
92+
93+ pub fn rawequal ( & self , state : * mut LuaState , a : impl IntoLua , b : impl IntoLua ) -> bool {
94+ a. into_lua ( & self . raw , state) ;
95+ b. into_lua ( & self . raw , state) ;
96+ let result = self . raw . rawequal ( state, -2 , -1 ) ;
97+ self . raw . pop ( state, 2 ) ;
98+ result
99+ }
100+
101+ pub fn getinfo ( & self , state : * mut LuaState , level : c_int , select : & CStr ) -> LuaResult < DebugInfo > {
102+ let info = self . raw . getinfo ( state, level, select) . ok_or ( LuaError :: GenericFailure ) ?;
103+ let select = select. to_bytes ( ) ;
104+
105+ let mut name = None ;
106+ let mut namewhat = None ;
107+ let mut what = None ;
108+ let mut source = None ;
109+ let mut currentline = None ;
110+ let mut nups = None ;
111+ let mut linedefined = None ;
112+ let mut lastlinedefined = None ;
113+ let mut short_src = None ;
114+ let mut func = None ;
115+
116+ if select. contains ( & b'f' ) {
117+ let f: LuaFunction = self . raw . try_to ( state, -1 ) ?;
118+ self . raw . pop ( state, 1 ) ;
119+ func = Some ( f) ;
120+ }
121+
122+ if select. contains ( & b'n' ) {
123+ name = Some ( unsafe { CStr :: from_ptr ( info. name ) } . to_string_lossy ( ) . to_string ( ) ) ;
124+ namewhat = Some ( unsafe { CStr :: from_ptr ( info. namewhat ) } . to_string_lossy ( ) . to_string ( ) ) ;
125+ }
126+
127+ if select. contains ( & b'S' ) {
128+ what = Some ( unsafe { CStr :: from_ptr ( info. what ) } . to_string_lossy ( ) . to_string ( ) ) ;
129+ source = Some ( unsafe { CStr :: from_ptr ( info. source ) } . to_string_lossy ( ) . to_string ( ) ) ;
130+ linedefined = Some ( info. linedefined ) ;
131+ lastlinedefined = Some ( info. lastlinedefined ) ;
132+ short_src = Some (
133+ unsafe { CStr :: from_ptr ( info. short_src . as_ptr ( ) ) }
134+ . to_string_lossy ( )
135+ . to_string ( ) ,
136+ ) ;
137+ }
138+
139+ if select. contains ( & b'l' ) {
140+ currentline = Some ( info. currentline ) ;
141+ }
142+
143+ if select. contains ( & b'u' ) {
144+ nups = Some ( info. nups ) ;
145+ }
146+
147+ Ok ( DebugInfo {
148+ event : info. event ,
149+ name,
150+ namewhat,
151+ what,
152+ source,
153+ currentline,
154+ nups,
155+ linedefined,
156+ lastlinedefined,
157+ short_src,
158+ func,
159+ } )
160+ }
60161}
61162
62163#[ macro_export]
0 commit comments