@@ -23,7 +23,7 @@ use bevy_mod_scripting_core::{
2323 pretty_print:: { DisplayWithWorld , ReflectReferencePrinter } ,
2424 script_value:: ScriptValue ,
2525 ReflectAllocator , ReflectRefIter , ReflectReference , ReflectionPathExt , TypeIdSource ,
26- WorldCallbackAccess ,
26+ WorldCallbackAccess , WorldGuard ,
2727 } ,
2828 error:: { InteropError , ScriptError , ScriptResult } ,
2929 reflection_extensions:: { PartialReflectExt , TypeIdExtensions } ,
@@ -130,6 +130,27 @@ fn iter_dynamic_function_overloads(
130130 . into_iter ( )
131131}
132132
133+ fn try_call_overloads (
134+ lua : & Lua ,
135+ key : & str ,
136+ type_id : TypeId ,
137+ args : Vec < ScriptValue > ,
138+ world : WorldGuard ,
139+ ) -> Result < LuaScriptValue , InteropError > {
140+ let overloads = iter_dynamic_function_overloads ( lua, key, type_id) ;
141+ let mut last_error = None ;
142+ for overload in overloads {
143+ match overload. call_script_function ( args. clone ( ) , world. clone ( ) , lua_caller_context ( ) ) {
144+ Ok ( out) => return Ok ( out. into ( ) ) ,
145+ Err ( e) => last_error = Some ( e) ,
146+ }
147+ }
148+
149+ Err ( last_error
150+ . unwrap_or_else ( || InteropError :: missing_function ( type_id, key. to_string ( ) ) . into ( ) )
151+ . into ( ) )
152+ }
153+
133154impl UserData for LuaReflectReference {
134155 fn add_methods < T : UserDataMethods < Self > > ( m : & mut T ) {
135156 m. add_meta_function (
@@ -190,28 +211,101 @@ impl UserData for LuaReflectReference {
190211 let world = lua. get_world ( ) ;
191212 let self_: ReflectReference = self_. into ( ) ;
192213 let other: ScriptValue = other. into ( ) ;
214+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
215+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
216+ Ok ( try_call_overloads ( lua, "sub" , target_type_id, args, world) ?)
217+ } ,
218+ ) ;
193219
220+ m. add_meta_function (
221+ MetaMethod :: Add ,
222+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
223+ let world = lua. get_world ( ) ;
224+ let self_: ReflectReference = self_. into ( ) ;
225+ let other: ScriptValue = other. into ( ) ;
194226 let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
227+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
228+ Ok ( try_call_overloads ( lua, "add" , target_type_id, args, world) ?)
229+ } ,
230+ ) ;
195231
196- let overloads = iter_dynamic_function_overloads ( lua, "sub" , target_type_id) ;
197- let mut last_error = None ;
198- let call_args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
199- for overload in overloads {
200- match overload. call_script_function (
201- call_args. clone ( ) ,
202- world. clone ( ) ,
203- lua_caller_context ( ) ,
204- ) {
205- Ok ( out) => return LuaScriptValue :: from ( out) . into_lua ( lua) ,
206- Err ( e) => last_error = Some ( e) ,
207- }
208- }
232+ m. add_meta_function (
233+ MetaMethod :: Mul ,
234+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
235+ let world = lua. get_world ( ) ;
236+ let self_: ReflectReference = self_. into ( ) ;
237+ let other: ScriptValue = other. into ( ) ;
238+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
239+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
240+ Ok ( try_call_overloads ( lua, "mul" , target_type_id, args, world) ?)
241+ } ,
242+ ) ;
243+
244+ m. add_meta_function (
245+ MetaMethod :: Div ,
246+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
247+ let world = lua. get_world ( ) ;
248+ let self_: ReflectReference = self_. into ( ) ;
249+ let other: ScriptValue = other. into ( ) ;
250+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
251+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
252+ Ok ( try_call_overloads ( lua, "div" , target_type_id, args, world) ?)
253+ } ,
254+ ) ;
209255
210- Err ( last_error
211- . unwrap_or_else ( || {
212- InteropError :: missing_function ( target_type_id, "sub" . to_string ( ) ) . into ( )
213- } )
214- . into ( ) )
256+ m. add_meta_function (
257+ MetaMethod :: Mod ,
258+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
259+ let world = lua. get_world ( ) ;
260+ let self_: ReflectReference = self_. into ( ) ;
261+ let other: ScriptValue = other. into ( ) ;
262+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
263+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
264+ Ok ( try_call_overloads ( lua, "rem" , target_type_id, args, world) ?)
265+ } ,
266+ ) ;
267+
268+ m. add_meta_function ( MetaMethod :: Unm , |lua, self_ : LuaReflectReference | {
269+ let world = lua. get_world ( ) ;
270+ let self_: ReflectReference = self_. into ( ) ;
271+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
272+ let args = vec ! [ ScriptValue :: Reference ( self_) ] ;
273+ Ok ( try_call_overloads ( lua, "neg" , target_type_id, args, world) ?)
274+ } ) ;
275+
276+ m. add_meta_function (
277+ MetaMethod :: Pow ,
278+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
279+ let world = lua. get_world ( ) ;
280+ let self_: ReflectReference = self_. into ( ) ;
281+ let other: ScriptValue = other. into ( ) ;
282+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
283+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
284+ Ok ( try_call_overloads ( lua, "pow" , target_type_id, args, world) ?)
285+ } ,
286+ ) ;
287+
288+ m. add_meta_function (
289+ MetaMethod :: Eq ,
290+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
291+ let world = lua. get_world ( ) ;
292+ let self_: ReflectReference = self_. into ( ) ;
293+ let other: ScriptValue = other. into ( ) ;
294+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
295+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
296+ Ok ( try_call_overloads ( lua, "eq" , target_type_id, args, world) ?)
297+ } ,
298+ ) ;
299+
300+ m. add_meta_function (
301+ MetaMethod :: Lt ,
302+ |lua, ( self_, other) : ( LuaReflectReference , LuaScriptValue ) | {
303+ let world = lua. get_world ( ) ;
304+ let self_: ReflectReference = self_. into ( ) ;
305+ let other: ScriptValue = other. into ( ) ;
306+ let target_type_id = self_. tail_type_id ( world. clone ( ) ) ?. or_fake_id ( ) ;
307+ let args = vec ! [ ScriptValue :: Reference ( self_) , other] ;
308+ Ok ( try_call_overloads ( lua, "lt" , target_type_id, args, world) ?)
215309 } ,
216310 ) ;
217311
0 commit comments