Update UserDataMethods::add_async_method() functions to take &T as second argument instead of cloning T.

New functions: `UserDataMethods::add_async_method_mut()`, `UserDataMethods::add_async_meta_method_mut()`.
This commit is contained in:
Alex Orlenko
2023-06-15 00:34:41 +01:00
parent cf0524aa23
commit 9fdba541e9
13 changed files with 572 additions and 270 deletions
+16 -9
View File
@@ -2658,18 +2658,17 @@ impl Lua {
}
}
// Pushes a LuaRef value onto the stack, checking that it's a registered
// Returns `TypeId` for the LuaRef, checking that it's a registered
// and not destructed UserData.
// Uses 2 stack spaces, does not call checkstack.
pub(crate) unsafe fn push_userdata_ref(&self, lref: &LuaRef) -> Result<Option<TypeId>> {
let state = self.state();
self.push_ref(lref);
if ffi::lua_getmetatable(state, -1) == 0 {
ffi::lua_pop(state, 1);
//
// Returns `None` if the userdata is registered but non-static.
pub(crate) unsafe fn get_userdata_type_id(&self, lref: &LuaRef) -> Result<Option<TypeId>> {
let ref_thread = self.ref_thread();
if ffi::lua_getmetatable(ref_thread, lref.index) == 0 {
return Err(Error::UserDataTypeMismatch);
}
let mt_ptr = ffi::lua_topointer(state, -1);
ffi::lua_pop(state, 1);
let mt_ptr = ffi::lua_topointer(ref_thread, -1);
ffi::lua_pop(ref_thread, 1);
// Fast path to skip looking up the metatable in the map
let (last_mt, last_type_id) = (*self.extra.get()).last_checked_userdata_mt;
@@ -2689,6 +2688,14 @@ impl Lua {
}
}
// Pushes a LuaRef (userdata) value onto the stack, returning their `TypeId`.
// Uses 1 stack space, does not call checkstack.
pub(crate) unsafe fn push_userdata_ref(&self, lref: &LuaRef) -> Result<Option<TypeId>> {
let type_id = self.get_userdata_type_id(lref)?;
self.push_ref(lref);
Ok(type_id)
}
// Creates a Function out of a Callback containing a 'static Fn. This is safe ONLY because the
// Fn is 'static, otherwise it could capture 'lua arguments improperly. Without ATCs, we
// cannot easily deal with the "correct" callback type of: