From 2fe5dca6c4337f1528e4671d1f79464481739c61 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Sat, 17 Feb 2024 19:07:22 -0500 Subject: [PATCH] Fix `Userdata::execute_function/method()` The two methods weren't using __index to try to find the function on the userdata --- src/userdata/any.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/userdata/any.rs b/src/userdata/any.rs index 430bd3e..52efeec 100755 --- a/src/userdata/any.rs +++ b/src/userdata/any.rs @@ -140,8 +140,8 @@ impl<'a> AnyUserdata<'a> { /// Gets something from the userdata. /// - /// Will trigger a call to the `__index` metamethod. Use [`AnyUserdata::raw_get`] if you - /// don't want to call it. + /// Will likely trigger a call to the `__index` metamethod. Use [`AnyUserdata::raw_get`] if + /// you don't want to call it, but if its not on the metatable that method one find anything pub fn get(&self, key: K) -> crate::Result where K: AsLua<'a>, @@ -160,7 +160,8 @@ impl<'a> AnyUserdata<'a> { } } - /// Gets something from the userdata. Will **not** trigger a call to the `__index` metamethod. + /// Gets something from the userdata that is on the metatable. Will **not** trigger a call to + /// the `__index` metamethod. pub fn raw_get(&'a self, key: K) -> crate::Result where K: AsLua<'a>, @@ -171,15 +172,14 @@ impl<'a> AnyUserdata<'a> { } /// Execute a method on this userdata. This finds a function with `name` and executes it - /// with the userdata as the first argument. + /// with the userdata as the first argument. This may trigger an execution + /// of the `__index` meta method. pub fn execute_method(&'a self, name: &str, args: A) -> crate::Result where A: IntoLuaVec<'a>, R: FromLuaVec<'a> + FromLua<'a>, { - let name = name.to_string(); - let mt = self.get_metatable()?; - let f = mt.get::<_, Function>(name)?; + let f: Function = self.get(name.to_string())?; let mut args = args.into_lua_value_vec(self.state)?; args.push_front(self.clone().as_lua(self.state)?); @@ -196,10 +196,7 @@ impl<'a> AnyUserdata<'a> { A: IntoLuaVec<'a>, R: FromLuaVec<'a> + FromLua<'a>, { - let name = name.to_string(); - let mt = self.get_metatable()?; - let f = mt.get::<_, Function>(name)?; - + let f: Function = self.get(name.to_string())?; f.exec(args) } }