Skip to content

Commit 7332c6a

Browse files
committed
Remove registered_userdata_mt check
1 parent 94670e3 commit 7332c6a

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/lua.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::any::TypeId;
22
use std::cell::{RefCell, UnsafeCell};
3-
use std::collections::{HashMap, HashSet};
3+
use std::collections::HashMap;
44
use std::ffi::CString;
55
use std::marker::PhantomData;
66
use std::os::raw::{c_char, c_int, c_void};
@@ -57,7 +57,6 @@ pub struct Lua {
5757
// Data associated with the lua_State.
5858
struct ExtraData {
5959
registered_userdata: HashMap<TypeId, c_int>,
60-
registered_userdata_mt: HashSet<isize>,
6160
registry_unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
6261

6362
libs: StdLib,
@@ -322,7 +321,6 @@ impl Lua {
322321

323322
let extra = Arc::new(Mutex::new(ExtraData {
324323
registered_userdata: HashMap::new(),
325-
registered_userdata_mt: HashSet::new(),
326324
registry_unref_list: Arc::new(Mutex::new(Some(Vec::new()))),
327325
ref_thread,
328326
libs: StdLib::NONE,
@@ -1561,40 +1559,31 @@ impl Lua {
15611559
ffi::lua_pop(self.state, 1);
15621560
}
15631561

1564-
let (ptr, id) = protect_lua_closure(self.state, 1, 0, |state| {
1565-
let ptr = ffi::lua_topointer(state, -1) as isize;
1566-
let id = ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX);
1567-
(ptr, id)
1562+
let id = protect_lua_closure(self.state, 1, 0, |state| {
1563+
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
15681564
})?;
15691565

15701566
let mut extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
15711567
extra.registered_userdata.insert(type_id, id);
1572-
extra.registered_userdata_mt.insert(ptr);
15731568

15741569
Ok(id)
15751570
}
15761571

1577-
// Pushes a LuaRef value onto the stack, checking that it's any registered userdata
1572+
// Pushes a LuaRef value onto the stack, checking that it's not destructed
15781573
// Uses 2 stack spaces, does not call checkstack
15791574
#[cfg(feature = "serialize")]
15801575
pub(crate) unsafe fn push_userdata_ref(&self, lref: &LuaRef) -> Result<()> {
15811576
self.push_ref(lref);
15821577
if ffi::lua_getmetatable(self.state, -1) == 0 {
15831578
Err(Error::UserDataTypeMismatch)
15841579
} else {
1585-
// Check that this is our metatable
1586-
let ptr = ffi::lua_topointer(self.state, -1) as isize;
1587-
let extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
1588-
if !extra.registered_userdata_mt.contains(&ptr) {
1589-
// Maybe UserData destructed?
1590-
get_destructed_userdata_metatable(self.state);
1591-
if ffi::lua_rawequal(self.state, -1, -2) == 1 {
1592-
Err(Error::UserDataDestructed)
1593-
} else {
1594-
Err(Error::UserDataTypeMismatch)
1595-
}
1580+
// Check that userdata is not destructed
1581+
get_destructed_userdata_metatable(self.state);
1582+
let eq = ffi::lua_rawequal(self.state, -1, -2) == 1;
1583+
ffi::lua_pop(self.state, 2);
1584+
if eq {
1585+
Err(Error::UserDataDestructed)
15961586
} else {
1597-
ffi::lua_pop(self.state, 1);
15981587
Ok(())
15991588
}
16001589
}

0 commit comments

Comments
 (0)