mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Optimize various parts of the code to remove unnecessary clone calls.
This is became possible after implementing `IntoLua` for references.
This commit is contained in:
+1
-1
@@ -272,7 +272,7 @@ impl<'lua> Function<'lua> {
|
||||
)
|
||||
.try_cache()
|
||||
.set_name("__mlua_bind")
|
||||
.call((self.clone(), args_wrapper))
|
||||
.call((self, args_wrapper))
|
||||
}
|
||||
|
||||
/// Returns the environment of the Lua function.
|
||||
|
||||
+4
-4
@@ -644,13 +644,13 @@ impl Lua {
|
||||
};
|
||||
|
||||
let modname = self.create_string(modname)?;
|
||||
let value = match loaded.raw_get(modname.clone())? {
|
||||
let value = match loaded.raw_get(&modname)? {
|
||||
Value::Nil => {
|
||||
let result = match func.call(modname.clone())? {
|
||||
let result = match func.call(&modname)? {
|
||||
Value::Nil => Value::Boolean(true),
|
||||
res => res,
|
||||
};
|
||||
loaded.raw_set(modname, result.clone())?;
|
||||
loaded.raw_set(modname, &result)?;
|
||||
result
|
||||
}
|
||||
res => res,
|
||||
@@ -3205,7 +3205,7 @@ impl Lua {
|
||||
let loader = self.create_function(|_, ()| Ok("\n\tcan't load C modules in safe mode"))?;
|
||||
|
||||
// The third and fourth searchers looks for a loader as a C library
|
||||
searchers.raw_set(3, loader.clone())?;
|
||||
searchers.raw_set(3, loader)?;
|
||||
searchers.raw_remove(4)?;
|
||||
|
||||
Ok(())
|
||||
|
||||
+3
-3
@@ -53,7 +53,7 @@ impl std::ops::DerefMut for LoadedDylibs {
|
||||
pub(crate) fn register_package_module(lua: &Lua) -> Result<()> {
|
||||
// Create the package table and store it in app_data for later use (bypassing globals lookup)
|
||||
let package = lua.create_table()?;
|
||||
lua.set_app_data(PackageKey(lua.create_registry_value(package.clone())?));
|
||||
lua.set_app_data(PackageKey(lua.create_registry_value(&package)?));
|
||||
|
||||
// Set `package.path`
|
||||
let mut search_path = env::var("LUAU_PATH")
|
||||
@@ -82,12 +82,12 @@ pub(crate) fn register_package_module(lua: &Lua) -> Result<()> {
|
||||
|
||||
// Set `package.loaded` (table with a list of loaded modules)
|
||||
let loaded = lua.create_table()?;
|
||||
package.raw_set("loaded", loaded.clone())?;
|
||||
package.raw_set("loaded", &loaded)?;
|
||||
lua.set_named_registry_value("_LOADED", loaded)?;
|
||||
|
||||
// Set `package.loaders`
|
||||
let loaders = lua.create_sequence_from([lua.create_function(lua_loader)?])?;
|
||||
package.raw_set("loaders", loaders.clone())?;
|
||||
package.raw_set("loaders", &loaders)?;
|
||||
#[cfg(unix)]
|
||||
{
|
||||
loaders.push(lua.create_function(dylib_loader)?)?;
|
||||
|
||||
+11
-22
@@ -240,16 +240,12 @@ impl<'lua> Table<'lua> {
|
||||
// If self does not define it, then check the other table.
|
||||
if let Some(mt) = self.get_metatable() {
|
||||
if mt.contains_key("__eq")? {
|
||||
return mt
|
||||
.get::<_, Function>("__eq")?
|
||||
.call((self.clone(), other.clone()));
|
||||
return mt.get::<_, Function>("__eq")?.call((self, other));
|
||||
}
|
||||
}
|
||||
if let Some(mt) = other.get_metatable() {
|
||||
if mt.contains_key("__eq")? {
|
||||
return mt
|
||||
.get::<_, Function>("__eq")?
|
||||
.call((self.clone(), other.clone()));
|
||||
return mt.get::<_, Function>("__eq")?.call((self, other));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1004,10 +1000,7 @@ impl<'lua> TableExt<'lua> for Table<'lua> {
|
||||
A: IntoLuaMulti<'lua>,
|
||||
R: FromLuaMulti<'lua>,
|
||||
{
|
||||
let lua = self.0.lua;
|
||||
let mut args = args.into_lua_multi(lua)?;
|
||||
args.push_front(Value::Table(self.clone()));
|
||||
self.get::<_, Function>(name)?.call(args)
|
||||
self.get::<_, Function>(name)?.call((self, args))
|
||||
}
|
||||
|
||||
fn call_function<A, R>(&self, name: &str, args: A) -> Result<R>
|
||||
@@ -1024,13 +1017,7 @@ impl<'lua> TableExt<'lua> for Table<'lua> {
|
||||
A: IntoLuaMulti<'lua>,
|
||||
R: FromLuaMulti<'lua> + 'lua,
|
||||
{
|
||||
let lua = self.0.lua;
|
||||
let mut args = match args.into_lua_multi(lua) {
|
||||
Ok(args) => args,
|
||||
Err(e) => return Box::pin(future::err(e)),
|
||||
};
|
||||
args.push_front(Value::Table(self.clone()));
|
||||
self.call_async_function(name, args)
|
||||
self.call_async_function(name, (self, args))
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
@@ -1040,12 +1027,14 @@ impl<'lua> TableExt<'lua> for Table<'lua> {
|
||||
R: FromLuaMulti<'lua> + 'lua,
|
||||
{
|
||||
let lua = self.0.lua;
|
||||
let args = match args.into_lua_multi(lua) {
|
||||
Ok(args) => args,
|
||||
Err(e) => return Box::pin(future::err(e)),
|
||||
};
|
||||
match self.get::<_, Function>(name) {
|
||||
Ok(func) => Box::pin(async move { func.call_async(args).await }),
|
||||
Ok(func) => {
|
||||
let args = match args.into_lua_multi(lua) {
|
||||
Ok(args) => args,
|
||||
Err(e) => return Box::pin(future::err(e)),
|
||||
};
|
||||
Box::pin(async move { func.call_async(args).await })
|
||||
}
|
||||
Err(e) => Box::pin(future::err(e)),
|
||||
}
|
||||
}
|
||||
|
||||
+1
-3
@@ -1161,9 +1161,7 @@ impl<'lua> AnyUserData<'lua> {
|
||||
}
|
||||
|
||||
if mt.contains_key("__eq")? {
|
||||
return mt
|
||||
.get::<_, Function>("__eq")?
|
||||
.call((self.clone(), other.clone()));
|
||||
return mt.get::<_, Function>("__eq")?.call((self, other));
|
||||
}
|
||||
|
||||
Ok(false)
|
||||
|
||||
+6
-7
@@ -83,7 +83,7 @@ impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua> {
|
||||
let metatable = self.get_metatable()?;
|
||||
match metatable.get::<Value>(MetaMethod::Index)? {
|
||||
Value::Table(table) => table.raw_get(key),
|
||||
Value::Function(func) => func.call((self.clone(), key)),
|
||||
Value::Function(func) => func.call((self, key)),
|
||||
_ => Err(Error::runtime("attempt to index a userdata value")),
|
||||
}
|
||||
}
|
||||
@@ -92,7 +92,7 @@ impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua> {
|
||||
let metatable = self.get_metatable()?;
|
||||
match metatable.get::<Value>(MetaMethod::NewIndex)? {
|
||||
Value::Table(table) => table.raw_set(key, value),
|
||||
Value::Function(func) => func.call((self.clone(), key, value)),
|
||||
Value::Function(func) => func.call((self, key, value)),
|
||||
_ => Err(Error::runtime("attempt to index a userdata value")),
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,7 @@ impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua> {
|
||||
{
|
||||
let metatable = self.get_metatable()?;
|
||||
match metatable.get::<Value>(MetaMethod::Call)? {
|
||||
Value::Function(func) => func.call((self.clone(), args)),
|
||||
Value::Function(func) => func.call((self, args)),
|
||||
_ => Err(Error::runtime("attempt to call a userdata value")),
|
||||
}
|
||||
}
|
||||
@@ -121,11 +121,10 @@ impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua> {
|
||||
};
|
||||
match metatable.get::<Value>(MetaMethod::Call) {
|
||||
Ok(Value::Function(func)) => {
|
||||
let mut args = match args.into_lua_multi(self.0.lua) {
|
||||
let args = match (self, args).into_lua_multi(self.0.lua) {
|
||||
Ok(args) => args,
|
||||
Err(e) => return Box::pin(future::err(e)),
|
||||
};
|
||||
args.push_front(Value::UserData(self.clone()));
|
||||
Box::pin(async move { func.call_async(args).await })
|
||||
}
|
||||
Ok(_) => Box::pin(future::err(Error::runtime(
|
||||
@@ -140,7 +139,7 @@ impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua> {
|
||||
A: IntoLuaMulti<'lua>,
|
||||
R: FromLuaMulti<'lua>,
|
||||
{
|
||||
self.call_function(name, (self.clone(), args))
|
||||
self.call_function(name, (self, args))
|
||||
}
|
||||
|
||||
#[cfg(feature = "async")]
|
||||
@@ -149,7 +148,7 @@ impl<'lua> AnyUserDataExt<'lua> for AnyUserData<'lua> {
|
||||
A: IntoLuaMulti<'lua>,
|
||||
R: FromLuaMulti<'lua> + 'lua,
|
||||
{
|
||||
self.call_async_function(name, (self.clone(), args))
|
||||
self.call_async_function(name, (self, args))
|
||||
}
|
||||
|
||||
fn call_function<A, R>(&self, name: &str, args: A) -> Result<R>
|
||||
|
||||
Reference in New Issue
Block a user