Add ToString metamethod, make LuaError implement LuaUserData
LuaError implementing LuaUserData makes it easy to return LuaResult from a callback to implement callback functions that can error.
This commit is contained in:
parent
7b7f7f36fc
commit
8e3a9f0e84
|
@ -1,5 +1,6 @@
|
|||
use std::collections::{HashMap, BTreeMap};
|
||||
use std::hash::Hash;
|
||||
use error_chain::ChainedError;
|
||||
|
||||
use error::*;
|
||||
use lua::*;
|
||||
|
@ -263,3 +264,15 @@ impl<'lua, T: FromLua<'lua>> FromLua<'lua> for Option<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LuaUserDataType for LuaError {
|
||||
fn add_methods(methods: &mut LuaUserDataMethods<Self>) {
|
||||
methods.add_method("backtrace", |lua, err, _| {
|
||||
lua.pack(format!("{}", err.display()))
|
||||
});
|
||||
|
||||
methods.add_meta_method(LuaMetaMethod::ToString, |lua, err, _| {
|
||||
lua.pack(err.to_string())
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -748,6 +748,8 @@ pub enum LuaMetaMethod {
|
|||
NewIndex,
|
||||
/// The call "operator" `obj(arg1, args2, ...)`.
|
||||
Call,
|
||||
/// tostring(ud) will call this if it exists
|
||||
ToString,
|
||||
}
|
||||
|
||||
/// Stores methods of a userdata object.
|
||||
|
@ -1571,6 +1573,7 @@ impl Lua {
|
|||
LuaMetaMethod::Index => "__index",
|
||||
LuaMetaMethod::NewIndex => "__newIndex",
|
||||
LuaMetaMethod::Call => "__call",
|
||||
LuaMetaMethod::ToString => "__tostring",
|
||||
};
|
||||
push_string(self.state, name);
|
||||
self.push_value(
|
||||
|
|
Loading…
Reference in New Issue