|
1 | 1 | use std::any::TypeId;
|
2 | 2 | use std::cell::{RefCell, UnsafeCell};
|
3 |
| -use std::collections::{HashMap, HashSet}; |
| 3 | +use std::collections::HashMap; |
4 | 4 | use std::ffi::CString;
|
5 | 5 | use std::marker::PhantomData;
|
6 | 6 | use std::os::raw::{c_char, c_int, c_void};
|
@@ -57,7 +57,6 @@ pub struct Lua {
|
57 | 57 | // Data associated with the lua_State.
|
58 | 58 | struct ExtraData {
|
59 | 59 | registered_userdata: HashMap<TypeId, c_int>,
|
60 |
| - registered_userdata_mt: HashSet<isize>, |
61 | 60 | registry_unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
|
62 | 61 |
|
63 | 62 | libs: StdLib,
|
@@ -322,7 +321,6 @@ impl Lua {
|
322 | 321 |
|
323 | 322 | let extra = Arc::new(Mutex::new(ExtraData {
|
324 | 323 | registered_userdata: HashMap::new(),
|
325 |
| - registered_userdata_mt: HashSet::new(), |
326 | 324 | registry_unref_list: Arc::new(Mutex::new(Some(Vec::new()))),
|
327 | 325 | ref_thread,
|
328 | 326 | libs: StdLib::NONE,
|
@@ -1561,40 +1559,31 @@ impl Lua {
|
1561 | 1559 | ffi::lua_pop(self.state, 1);
|
1562 | 1560 | }
|
1563 | 1561 |
|
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) |
1568 | 1564 | })?;
|
1569 | 1565 |
|
1570 | 1566 | let mut extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
|
1571 | 1567 | extra.registered_userdata.insert(type_id, id);
|
1572 |
| - extra.registered_userdata_mt.insert(ptr); |
1573 | 1568 |
|
1574 | 1569 | Ok(id)
|
1575 | 1570 | }
|
1576 | 1571 |
|
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 |
1578 | 1573 | // Uses 2 stack spaces, does not call checkstack
|
1579 | 1574 | #[cfg(feature = "serialize")]
|
1580 | 1575 | pub(crate) unsafe fn push_userdata_ref(&self, lref: &LuaRef) -> Result<()> {
|
1581 | 1576 | self.push_ref(lref);
|
1582 | 1577 | if ffi::lua_getmetatable(self.state, -1) == 0 {
|
1583 | 1578 | Err(Error::UserDataTypeMismatch)
|
1584 | 1579 | } 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) |
1596 | 1586 | } else {
|
1597 |
| - ffi::lua_pop(self.state, 1); |
1598 | 1587 | Ok(())
|
1599 | 1588 | }
|
1600 | 1589 | }
|
|
0 commit comments