@@ -3,7 +3,7 @@ pub mod global;
33use anyhow:: Context ;
44use autorun_core:: plugins:: Plugin ;
55use autorun_log:: * ;
6- use autorun_lua:: { Globals , LuaApi , LuaTable } ;
6+ use autorun_lua:: { Globals , IntoLua , IntoLuaArgs , LuaApi , LuaFunction , LuaTable , LuaValue , RawLuaApi } ;
77use autorun_luajit:: { GCRef , LJState , index2adr} ;
88use autorun_types:: { LuaState , Realm } ;
99use std:: ffi:: { CStr , CString , c_int} ;
@@ -19,6 +19,12 @@ pub struct EnvHandle {
1919 autorun : LuaTable ,
2020}
2121
22+ impl IntoLua for & EnvHandle {
23+ fn into_lua ( self , lua : & RawLuaApi , state : * mut LuaState ) {
24+ lua. push ( state, & self . env ) ;
25+ }
26+ }
27+
2228macro_rules! wrap {
2329 ( $func: expr) => {
2430 autorun_lua:: as_lua_function!( |lua: & LuaApi , state: * mut LuaState | {
@@ -33,8 +39,7 @@ macro_rules! wrap {
3339
3440 // todo: potentially add a silenterror type so we can return that and it'll return a nil.
3541 // right now this would kind of leak the fact that it's an autorun function.
36- lua. push( state, c"" ) ;
37- lua. raw. error( state) ;
42+ lua. error( state, c"" ) ;
3843 } else {
3944 $func( lua, state, env)
4045 }
@@ -43,10 +48,6 @@ macro_rules! wrap {
4348}
4449
4550impl EnvHandle {
46- pub fn push ( & self , lua : & LuaApi , state : * mut LuaState ) {
47- lua. push ( state, & self . env ) ;
48- }
49-
5051 pub fn realm ( & self ) -> Realm {
5152 self . realm
5253 }
@@ -66,26 +67,23 @@ impl EnvHandle {
6667 }
6768
6869 pub fn is_active ( & self , lua : & LuaApi , state : * mut LuaState ) -> bool {
69- if lua. raw . getinfo ( state, 1 , c"f" ) . is_none ( ) {
70- // No function info available
70+ let Ok ( info) = lua. getinfo ( state, 1 , c"f" ) else {
7171 return false ;
72- }
72+ } ;
7373
74- lua. raw . getfenv ( state, -1 ) ;
75- self . push ( lua, state) ;
76-
77- let equal = lua. raw . rawequal ( state, -1 , -2 ) ;
78- lua. raw . pop ( state, 3 ) ;
74+ let Ok ( Some ( env) ) = lua. getfenv ( state, & info. func . unwrap ( ) ) else {
75+ return false ;
76+ } ;
7977
80- equal
78+ lua . equal ( state , & env , & self . env )
8179 }
8280
8381 pub fn get_active_plugin ( & self , lua : & LuaApi , state : * mut LuaState ) -> Option < & Plugin > {
8482 if !self . is_active ( lua, state) {
8583 return None ;
8684 }
8785
88- let dir: * mut Plugin = lua. get ( state, & self . autorun , "PLUGIN" ) ;
86+ let dir: * mut Plugin = lua. get ( state, & self . autorun , "PLUGIN" ) . ok ( ) ? ;
8987 let dir = unsafe { dir. as_ref ( ) } ?;
9088
9189 Some ( dir)
@@ -119,11 +117,7 @@ impl EnvHandle {
119117 let name = self . format_chunk_name ( name) ?;
120118 let chunk = lua. load ( state, src, & name) ?;
121119 lua. setfenv ( state, & chunk, & self . env ) ?;
122-
123- lua. push ( state, & chunk) ;
124- if let Err ( why) = lua. raw . pcall ( state, 0 , 0 , 0 ) {
125- anyhow:: bail!( "Failed to execute: {}" , why) ;
126- }
120+ lua. pcall ( state, & chunk, ( ) ) ?;
127121
128122 Ok ( ( ) )
129123 }
@@ -136,7 +130,7 @@ impl EnvHandle {
136130 lua. set ( state, & env, "_G" , Globals ) ;
137131
138132 // todo: refactor luajit code to not depend on the stack
139- lua. push ( state, & env) ;
133+ lua. raw . push ( state, & env) ;
140134
141135 // Can unwrap since we are sure there is something on the stack
142136 let lj_state = state as * mut LJState ;
@@ -174,46 +168,15 @@ impl EnvHandle {
174168 Ok ( ( ) )
175169 }
176170
177- pub fn trigger ( & self , lua : & LuaApi , state : * mut LuaState , event_name : & CStr , n_args : c_int ) -> anyhow:: Result < ( ) > {
178- lua. push ( state, event_name) ;
179- lua. raw . insert ( state, -( n_args + 1 ) ) ;
180-
181- lua. push ( state, & self . autorun ) ;
182- lua. raw . getfield ( state, -1 , c"trigger" . as_ptr ( ) ) ;
183- lua. raw . remove ( state, -2 ) ; // remove Autorun table
184-
185- if lua. raw . typeid ( state, -1 ) != autorun_lua:: LuaTypeId :: Function {
186- lua. raw . pop ( state, 1 ) ;
187- anyhow:: bail!( "don't remove Autorun.trigger lil bro." ) ;
188- }
189-
190- lua. raw . insert ( state, -( n_args + 2 ) ) ;
191- lua. raw . pcall ( state, n_args + 1 , 0 , 0 ) . map_err ( |e| anyhow:: anyhow!( e) ) ?;
192-
171+ pub fn trigger ( & self , lua : & LuaApi , state : * mut LuaState , args : impl IntoLuaArgs ) -> anyhow:: Result < ( ) > {
172+ let trigger: LuaFunction = lua. get ( state, & self . autorun , "trigger" ) ?;
173+ lua. pcall ( state, & trigger, args) ?;
193174 Ok ( ( ) )
194175 }
195176
196- pub fn run_remote_callbacks (
197- & self ,
198- lua : & LuaApi ,
199- state : * mut LuaState ,
200- event_name : & CStr ,
201- n_args : c_int ,
202- ) -> anyhow:: Result < ( ) > {
203- lua. push ( state, event_name) ;
204- lua. raw . insert ( state, -( n_args + 1 ) ) ;
205-
206- lua. push ( state, & self . autorun ) ;
207- lua. raw . getfield ( state, -1 , c"runRemoteCallbacks" . as_ptr ( ) ) ;
208- lua. raw . remove ( state, -2 ) ; // remove Autorun table
209-
210- if lua. raw . typeid ( state, -1 ) != autorun_lua:: LuaTypeId :: Function {
211- lua. raw . pop ( state, 1 ) ;
212- anyhow:: bail!( "don't remove Autorun.runRemoteCallbacks lil bro." ) ;
213- }
214-
215- lua. raw . insert ( state, -( n_args + 2 ) ) ;
216- lua. raw . pcall ( state, n_args + 1 , 0 , 0 ) . map_err ( |e| anyhow:: anyhow!( e) ) ?;
177+ pub fn run_remote_callbacks ( & self , lua : & LuaApi , state : * mut LuaState , args : impl IntoLuaArgs ) -> anyhow:: Result < ( ) > {
178+ let run_remote_callbacks = lua. get ( state, & self . autorun , "runRemoteCallbacks" ) ?;
179+ lua. pcall ( state, & run_remote_callbacks, args) ?;
217180
218181 Ok ( ( ) )
219182 }
0 commit comments