summaryrefslogtreecommitdiff
path: root/src/userdata.rs
diff options
context:
space:
mode:
authorkyren <kerriganw@gmail.com>2017-12-04 02:46:57 -0500
committerkyren <kerriganw@gmail.com>2017-12-04 02:50:27 -0500
commit66a4e9a8e73b9526eecb74d6e4dcbc06d6a53c60 (patch)
treeb19d6c815c0273b4e6787b5b3dead1c0b4c520e8 /src/userdata.rs
parent80a98ef29ce3dc912bf4a2950bb1d5c4fc9995eb (diff)
downloadmlua-66a4e9a8e73b9526eecb74d6e4dcbc06d6a53c60.zip
Add `ExpiredUserData` error and avoid what was previously a panic
Also make sure that panic messages clearly state that they are internal errors, so people report them as a bug. Since the only panics left are all internal errors, just move the internal error message into the panic / assert macros.
Diffstat (limited to 'src/userdata.rs')
-rw-r--r--src/userdata.rs17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/userdata.rs b/src/userdata.rs
index f3bbf12..08ea583 100644
--- a/src/userdata.rs
+++ b/src/userdata.rs
@@ -358,7 +358,7 @@ impl<'lua> AnyUserData<'lua> {
{
unsafe {
let lua = self.0.lua;
- stack_guard(lua.state, 0, move || {
+ stack_err_guard(lua.state, 0, move || {
check_stack(lua.state, 3);
lua.push_ref(lua.state, &self.0);
@@ -379,7 +379,7 @@ impl<'lua> AnyUserData<'lua> {
ffi::lua_pop(lua.state, 3);
Err(Error::UserDataTypeMismatch)
} else {
- let res = func(&*get_userdata::<RefCell<T>>(lua.state, -3));
+ let res = func(&*get_userdata::<RefCell<T>>(lua.state, -3)?);
ffi::lua_pop(lua.state, 3);
res
}
@@ -391,7 +391,7 @@ impl<'lua> AnyUserData<'lua> {
#[cfg(test)]
mod tests {
use super::{MetaMethod, UserData, UserDataMethods};
- use error::ExternalError;
+ use error::{Error, ExternalError};
use string::String;
use function::Function;
use lua::Lua;
@@ -505,7 +505,6 @@ mod tests {
}
#[test]
- #[should_panic]
fn test_expired_userdata() {
struct MyUserdata {
id: u8,
@@ -526,7 +525,7 @@ mod tests {
globals.set("userdata", MyUserdata { id: 123 }).unwrap();
}
- lua.eval::<()>(
+ match lua.eval::<()>(
r#"
local tbl = setmetatable({
userdata = userdata
@@ -541,7 +540,13 @@ mod tests {
hatch:access()
"#,
None,
- ).unwrap();
+ ) {
+ Err(Error::CallbackError { cause, .. }) => match *cause {
+ Error::ExpiredUserData { .. } => {}
+ ref other => panic!("incorrect result: {}", other),
+ },
+ other => panic!("incorrect result: {:?}", other),
+ }
}
#[test]