Skip to content

Commit b056ed2

Browse files
committed
Don't panic on mismatched RegistryKey use, instead return error
1 parent 823c2de commit b056ed2

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ pub enum Error {
8787
/// [`AnyUserData`]: struct.AnyUserData.html
8888
/// [`UserData`]: trait.UserData.html
8989
UserDataBorrowMutError,
90+
/// A `RegistryKey` produced from a different Lua state was used.
91+
MismatchedRegistryKey,
9092
/// A Rust callback returned `Err`, raising the contained `Error` as a Lua error.
9193
CallbackError {
9294
/// Lua call stack backtrace.
@@ -142,6 +144,9 @@ impl fmt::Display for Error {
142144
Error::UserDataTypeMismatch => write!(fmt, "userdata is not expected type"),
143145
Error::UserDataBorrowError => write!(fmt, "userdata already mutably borrowed"),
144146
Error::UserDataBorrowMutError => write!(fmt, "userdata already borrowed"),
147+
Error::MismatchedRegistryKey => {
148+
write!(fmt, "RegistryKey used from different Lua state")
149+
}
145150
Error::CallbackError { ref traceback, .. } => {
146151
write!(fmt, "callback error: {}", traceback)
147152
}

src/lua.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -504,11 +504,9 @@ impl Lua {
504504
/// value previously placed by `create_registry_value`.
505505
pub fn registry_value<'lua, T: FromLua<'lua>>(&'lua self, key: &RegistryKey) -> Result<T> {
506506
unsafe {
507-
lua_assert!(
508-
self.state,
509-
Arc::ptr_eq(&key.drop_list, &(*self.extra()).registry_drop_list),
510-
"Lua instance passed RegistryKey created from a different Lua"
511-
);
507+
if !Arc::ptr_eq(&key.drop_list, &(*self.extra()).registry_drop_list) {
508+
return Err(Error::MismatchedRegistryKey);
509+
}
512510

513511
stack_err_guard(self.state, 0, || {
514512
check_stack(self.state, 1);
@@ -528,25 +526,25 @@ impl Lua {
528526
/// `create_registry_value`. In addition to manual `RegistryKey` removal, you can also call
529527
/// `expire_registry_values` to automatically remove values from the registry whose
530528
/// `RegistryKey`s have been dropped.
531-
pub fn remove_registry_value(&self, mut key: RegistryKey) {
529+
pub fn remove_registry_value(&self, mut key: RegistryKey) -> Result<()> {
532530
unsafe {
533-
lua_assert!(
534-
self.state,
535-
Arc::ptr_eq(&key.drop_list, &(*self.extra()).registry_drop_list),
536-
"Lua instance passed RegistryKey created from a different Lua"
537-
);
531+
if !Arc::ptr_eq(&key.drop_list, &(*self.extra()).registry_drop_list) {
532+
return Err(Error::MismatchedRegistryKey);
533+
}
538534

539535
ffi::luaL_unref(self.state, ffi::LUA_REGISTRYINDEX, key.registry_id);
540536
// Don't adding to the registry drop list when dropping the key
541537
key.registry_id = ffi::LUA_REFNIL;
538+
Ok(())
542539
}
543540
}
544541

545542
/// Returns true if the given `RegistryKey` was created by a `Lua` which shares the underlying
546543
/// main state with this `Lua` instance.
547544
///
548-
/// Other than this, methods that accept a `RegistryKey` will panic if passed a `RegistryKey`
549-
/// that was not created with a matching `Lua` state.
545+
/// Other than this, methods that accept a `RegistryKey` will return
546+
/// `Error::MismatchedRegistryKey` if passed a `RegistryKey` that was not created with a
547+
/// matching `Lua` state.
550548
pub fn owns_registry_value(&self, key: &RegistryKey) -> bool {
551549
unsafe { Arc::ptr_eq(&key.drop_list, &(*self.extra()).registry_drop_list) }
552550
}

src/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,15 @@ fn test_lua_registry_ownership() {
586586
}
587587

588588
#[test]
589-
#[should_panic]
590589
fn test_mismatched_registry_key() {
591590
let lua1 = Lua::new();
592591
let lua2 = Lua::new();
593592

594593
let r = lua1.create_registry_value("hello").unwrap();
595-
lua2.remove_registry_value(r);
594+
match lua2.remove_registry_value(r) {
595+
Err(Error::MismatchedRegistryKey) => {}
596+
r => panic!("wrong result type for mismatched registry key, {:?}", r),
597+
};
596598
}
597599

598600
// TODO: Need to use compiletest-rs or similar to make sure these don't compile.

0 commit comments

Comments
 (0)