Fix `Userdata::execute_function/method()`

The two methods weren't using __index to try to find the function on the userdata
This commit is contained in:
SeanOMik 2024-02-17 19:07:22 -05:00
parent 8b88612890
commit 2fe5dca6c4
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
1 changed files with 8 additions and 11 deletions

View File

@ -140,8 +140,8 @@ impl<'a> AnyUserdata<'a> {
/// Gets something from the userdata. /// Gets something from the userdata.
/// ///
/// Will trigger a call to the `__index` metamethod. Use [`AnyUserdata::raw_get`] if you /// Will likely trigger a call to the `__index` metamethod. Use [`AnyUserdata::raw_get`] if
/// don't want to call it. /// you don't want to call it, but if its not on the metatable that method one find anything
pub fn get<K, V>(&self, key: K) -> crate::Result<V> pub fn get<K, V>(&self, key: K) -> crate::Result<V>
where where
K: AsLua<'a>, 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<K, V>(&'a self, key: K) -> crate::Result<V> pub fn raw_get<K, V>(&'a self, key: K) -> crate::Result<V>
where where
K: AsLua<'a>, 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 /// 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, R>(&'a self, name: &str, args: A) -> crate::Result<R> pub fn execute_method<A, R>(&'a self, name: &str, args: A) -> crate::Result<R>
where where
A: IntoLuaVec<'a>, A: IntoLuaVec<'a>,
R: FromLuaVec<'a> + FromLua<'a>, R: FromLuaVec<'a> + FromLua<'a>,
{ {
let name = name.to_string(); let f: Function = self.get(name.to_string())?;
let mt = self.get_metatable()?;
let f = mt.get::<_, Function>(name)?;
let mut args = args.into_lua_value_vec(self.state)?; let mut args = args.into_lua_value_vec(self.state)?;
args.push_front(self.clone().as_lua(self.state)?); args.push_front(self.clone().as_lua(self.state)?);
@ -196,10 +196,7 @@ impl<'a> AnyUserdata<'a> {
A: IntoLuaVec<'a>, A: IntoLuaVec<'a>,
R: FromLuaVec<'a> + FromLua<'a>, R: FromLuaVec<'a> + FromLua<'a>,
{ {
let name = name.to_string(); let f: Function = self.get(name.to_string())?;
let mt = self.get_metatable()?;
let f = mt.get::<_, Function>(name)?;
f.exec(args) f.exec(args)
} }
} }