@@ -3,28 +3,23 @@ pub mod global;
33use anyhow:: Context ;
44use autorun_core:: plugins:: Plugin ;
55use autorun_log:: * ;
6- use autorun_lua:: { LuaApi , RawHandle } ;
6+ use autorun_lua:: { Globals , LuaApi , LuaTable } ;
77use autorun_luajit:: { GCRef , LJState , index2adr} ;
88use autorun_types:: { LuaState , Realm } ;
99use std:: ffi:: { CStr , CString , c_int} ;
1010
11+ use crate :: functions;
12+
1113#[ derive( Debug , Clone , Copy ) ]
1214pub struct EnvHandle {
1315 realm : Realm ,
1416 env_gcr : GCRef ,
1517 chunk_nonce : u64 ,
16- handle : RawHandle ,
17- }
18-
19- impl core:: ops:: Deref for EnvHandle {
20- type Target = RawHandle ;
21-
22- fn deref ( & self ) -> & Self :: Target {
23- & self . handle
24- }
18+ env : LuaTable ,
19+ autorun : LuaTable ,
2520}
2621
27- macro_rules! as_env_lua_function {
22+ macro_rules! wrap {
2823 ( $func: expr) => {
2924 autorun_lua:: as_lua_function!( |lua: & LuaApi , state: * mut LuaState | {
3025 let realm = crate :: global:: get_realm( state) ;
@@ -48,6 +43,10 @@ macro_rules! as_env_lua_function {
4843}
4944
5045impl EnvHandle {
46+ pub fn push ( & self , lua : & LuaApi , state : * mut LuaState ) {
47+ self . env . push ( lua, state) ;
48+ }
49+
5150 pub fn realm ( & self ) -> Realm {
5251 self . realm
5352 }
@@ -86,93 +85,34 @@ impl EnvHandle {
8685 return None ;
8786 }
8887
89- self . push_autorun_table ( lua, state) ;
90- lua. get_field ( state, -1 , c"PLUGIN" . as_ptr ( ) ) ;
91-
92- let dir = lua. to_userdata ( state, -1 ) as * mut Plugin ;
93- if dir. is_null ( ) {
94- lua. pop ( state, 2 ) ;
95- return None ;
96- }
97- lua. pop ( state, 2 ) ;
88+ let dir: * mut Plugin = lua. get ( state, & self . autorun , "PLUGIN" ) ;
89+ let dir = unsafe { dir. as_ref ( ) } ?;
9890
99- unsafe { dir . as_ref ( ) }
91+ Some ( dir )
10092 }
10193
102- fn create_autorun_table ( lua : & LuaApi , state : * mut LuaState ) {
103- lua. create_table ( state, 0 , 6 ) ;
104-
105- lua. push ( state, c"print" ) ;
106- lua. push ( state, as_env_lua_function ! ( crate :: functions:: print) ) ;
107- lua. set_table ( state, -3 ) ;
108-
109- lua. push ( state, c"read" ) ;
110- lua. push ( state, as_env_lua_function ! ( crate :: functions:: read) ) ;
111- lua. set_table ( state, -3 ) ;
112-
113- lua. push ( state, c"write" ) ;
114- lua. push ( state, as_env_lua_function ! ( crate :: functions:: write) ) ;
115- lua. set_table ( state, -3 ) ;
116-
117- lua. push ( state, c"writeAsync" ) ;
118- lua. push ( state, as_env_lua_function ! ( crate :: functions:: write_async) ) ;
119- lua. set_table ( state, -3 ) ;
120-
121- lua. push ( state, c"mkdir" ) ;
122- lua. push ( state, as_env_lua_function ! ( crate :: functions:: mkdir) ) ;
123- lua. set_table ( state, -3 ) ;
124-
125- lua. push ( state, c"append" ) ;
126- lua. push ( state, as_env_lua_function ! ( crate :: functions:: append) ) ;
127- lua. set_table ( state, -3 ) ;
128-
129- lua. push ( state, c"exists" ) ;
130- lua. push ( state, as_env_lua_function ! ( crate :: functions:: exists) ) ;
131- lua. set_table ( state, -3 ) ;
132-
133- lua. push ( state, c"detour" ) ;
134- lua. push ( state, as_env_lua_function ! ( crate :: functions:: detour) ) ;
135- lua. set_table ( state, -3 ) ;
136-
137- lua. push ( state, c"enableDetour" ) ;
138- lua. push ( state, as_env_lua_function ! ( crate :: functions:: detour_enable) ) ;
139- lua. set_table ( state, -3 ) ;
140-
141- lua. push ( state, c"disableDetour" ) ;
142- lua. push ( state, as_env_lua_function ! ( crate :: functions:: detour_disable) ) ;
143- lua. set_table ( state, -3 ) ;
144-
145- lua. push ( state, c"removeDetour" ) ;
146- lua. push ( state, as_env_lua_function ! ( crate :: functions:: detour_remove) ) ;
147- lua. set_table ( state, -3 ) ;
148-
149- lua. push ( state, c"getOriginalFunction" ) ;
150- lua. push ( state, as_env_lua_function ! ( crate :: functions:: detour_get_original) ) ;
151- lua. set_table ( state, -3 ) ;
152-
153- lua. push ( state, c"copyFastFunction" ) ;
154- lua. push ( state, as_env_lua_function ! ( crate :: functions:: copy_fast_function) ) ;
155- lua. set_table ( state, -3 ) ;
156-
157- lua. push ( state, c"load" ) ;
158- lua. push ( state, as_env_lua_function ! ( crate :: functions:: load) ) ;
159- lua. set_table ( state, -3 ) ;
160-
161- lua. push ( state, c"triggerRemote" ) ;
162- lua. push ( state, as_env_lua_function ! ( crate :: functions:: trigger_remote) ) ;
163- lua. set_table ( state, -3 ) ;
164-
165- lua. push ( state, c"isFunctionAuthorized" ) ;
166- lua. push ( state, as_env_lua_function ! ( crate :: functions:: is_function_authorized) ) ;
167- lua. set_table ( state, -3 ) ;
168-
169- lua. push ( state, c"isProtoAuthorized" ) ;
170- lua. push ( state, as_env_lua_function ! ( crate :: functions:: is_proto_authorized) ) ;
171- lua. set_table ( state, -3 ) ;
172-
173- lua. push ( state, c"VERSION" ) ;
174- lua. push ( state, env ! ( "CARGO_PKG_VERSION" ) . to_string ( ) ) ;
175- lua. set_table ( state, -3 ) ;
94+ fn create_autorun_table ( lua : & LuaApi , state : * mut LuaState ) -> LuaTable {
95+ let t = lua. table ( state) ;
96+ lua. set ( state, & t, "print" , wrap ! ( functions:: print) ) ;
97+ lua. set ( state, & t, "read" , wrap ! ( functions:: read) ) ;
98+ lua. set ( state, & t, "write" , wrap ! ( functions:: write) ) ;
99+ lua. set ( state, & t, "writeAsync" , wrap ! ( functions:: write_async) ) ;
100+ lua. set ( state, & t, "mkdir" , wrap ! ( functions:: mkdir) ) ;
101+ lua. set ( state, & t, "append" , wrap ! ( functions:: append) ) ;
102+ lua. set ( state, & t, "exists" , wrap ! ( functions:: exists) ) ;
103+ lua. set ( state, & t, "detour" , wrap ! ( functions:: detour) ) ;
104+ lua. set ( state, & t, "enableDetour" , wrap ! ( functions:: detour_enable) ) ;
105+ lua. set ( state, & t, "disableDetour" , wrap ! ( functions:: detour_disable) ) ;
106+ lua. set ( state, & t, "removeDetour" , wrap ! ( functions:: detour_remove) ) ;
107+ lua. set ( state, & t, "getOriginalFunction" , wrap ! ( functions:: detour_get_original) ) ;
108+ lua. set ( state, & t, "copyFastFunction" , wrap ! ( functions:: copy_fast_function) ) ;
109+ lua. set ( state, & t, "load" , wrap ! ( functions:: load) ) ;
110+ lua. set ( state, & t, "triggerRemote" , wrap ! ( functions:: trigger_remote) ) ;
111+ lua. set ( state, & t, "isFunctionAuthorized" , wrap ! ( functions:: is_function_authorized) ) ;
112+ lua. set ( state, & t, "isProtoAuthorized" , wrap ! ( functions:: is_proto_authorized) ) ;
113+ lua. set ( state, & t, "VERSION" , env ! ( "CARGO_PKG_VERSION" ) ) ;
114+
115+ return t;
176116 }
177117
178118 pub fn execute ( & self , lua : & LuaApi , state : * mut LuaState , name : & CStr , src : & [ u8 ] ) -> anyhow:: Result < ( ) > {
@@ -194,30 +134,31 @@ impl EnvHandle {
194134 }
195135
196136 pub fn create ( lua : & LuaApi , state : * mut LuaState , realm : Realm ) -> anyhow:: Result < Self > {
197- // Create autorun environment
198- lua. create_table ( state, 0 , 2 ) ;
137+ let autorun = Self :: create_autorun_table ( lua, state) ;
199138
200- lua. push ( state, "Autorun" ) ;
201- Self :: create_autorun_table ( lua, state ) ;
202- lua. set_table ( state, - 3 ) ;
139+ let env = lua. table ( state) ;
140+ lua. set ( state , & env , "Autorun" , & autorun ) ;
141+ lua. set ( state, & env , "_G" , Globals ) ;
203142
204- lua. push ( state, "_G" ) ;
205- lua. push_globals ( state) ;
206- lua. set_table ( state, -3 ) ;
143+ // todo: refactor luajit code to not depend on the stack
144+ env. push ( lua, state) ;
207145
208146 // Can unwrap since we are sure there is something on the stack
209147 let lj_state = state as * mut LJState ;
210148 let lj_state = unsafe { lj_state. as_ref ( ) . context ( "Failed to dereference LJState" ) ? } ;
211149 let env_tvalue = index2adr ( lj_state, -1 ) . context ( "Failed to get TValue for environment" ) ?;
212150 let env_gcr = unsafe { ( * env_tvalue) . gcr } ;
213151
214- let handle = RawHandle :: from_stack ( lua, state) . unwrap ( ) ;
152+ // todo: refactor luajit code to not depend on the stack
153+ lua. pop ( state, 1 ) ;
154+
215155 let chunk_nonce = rand:: random :: < u64 > ( ) ;
216156 Ok ( Self {
217157 realm,
218158 env_gcr,
219159 chunk_nonce,
220- handle,
160+ env,
161+ autorun,
221162 } )
222163 }
223164
@@ -233,28 +174,16 @@ impl EnvHandle {
233174 }
234175 }
235176
236- fn push_autorun_table ( & self , lua : & LuaApi , state : * mut LuaState ) {
237- self . push ( lua, state) ;
238- lua. get_field ( state, -1 , c"Autorun" . as_ptr ( ) ) ;
239- lua. remove ( state, -2 ) ;
240- }
241-
242177 pub fn set_plugin ( & self , lua : & LuaApi , state : * mut LuaState , plugin : & Plugin ) -> anyhow:: Result < ( ) > {
243- self . push_autorun_table ( lua, state) ;
244-
245- lua. push ( state, c"PLUGIN" ) ;
246- lua. new_userdata ( state, plugin. try_clone ( ) ?) ;
247- lua. set_table ( state, -3 ) ;
248-
249- lua. pop ( state, 1 ) ;
178+ lua. set ( state, & self . autorun , "PLUGIN" , plugin. try_clone ( ) ?) ;
250179 Ok ( ( ) )
251180 }
252181
253182 pub fn trigger ( & self , lua : & LuaApi , state : * mut LuaState , event_name : & CStr , n_args : c_int ) -> anyhow:: Result < ( ) > {
254183 lua. push ( state, event_name) ;
255184 lua. insert ( state, -( n_args + 1 ) ) ;
256185
257- self . push_autorun_table ( lua, state) ;
186+ self . autorun . push ( lua, state) ;
258187 lua. get_field ( state, -1 , c"trigger" . as_ptr ( ) ) ;
259188 lua. remove ( state, -2 ) ; // remove Autorun table
260189
@@ -279,7 +208,7 @@ impl EnvHandle {
279208 lua. push ( state, event_name) ;
280209 lua. insert ( state, -( n_args + 1 ) ) ;
281210
282- self . push_autorun_table ( lua, state) ;
211+ self . autorun . push ( lua, state) ;
283212 lua. get_field ( state, -1 , c"runRemoteCallbacks" . as_ptr ( ) ) ;
284213 lua. remove ( state, -2 ) ; // remove Autorun table
285214
0 commit comments