2017-10-23 16:42:20 -04:00
|
|
|
use std::cell::{Ref, RefCell, RefMut};
|
2017-09-30 01:08:08 -04:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::string::String as StdString;
|
|
|
|
|
|
|
|
use ffi;
|
|
|
|
use error::*;
|
|
|
|
use util::*;
|
2017-09-30 01:27:18 -04:00
|
|
|
use types::{Callback, LuaRef};
|
2018-01-26 20:06:18 -05:00
|
|
|
use value::{FromLua, FromLuaMulti, ToLua, ToLuaMulti};
|
2017-12-04 01:04:12 -05:00
|
|
|
use lua::Lua;
|
2017-09-30 01:08:08 -04:00
|
|
|
|
|
|
|
/// Kinds of metamethods that can be overridden.
|
2018-01-26 13:44:35 -05:00
|
|
|
///
|
2018-01-26 15:31:01 -05:00
|
|
|
/// Currently, this mechanism does not allow overriding the `__gc` metamethod, since there is
|
|
|
|
/// generally no need to do so: [`UserData`] implementors can instead just implement `Drop`.
|
2018-01-26 13:44:35 -05:00
|
|
|
///
|
|
|
|
/// [`UserData`]: trait.UserData.html
|
2017-09-30 01:08:08 -04:00
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
|
|
|
|
pub enum MetaMethod {
|
|
|
|
/// The `+` operator.
|
|
|
|
Add,
|
|
|
|
/// The `-` operator.
|
|
|
|
Sub,
|
|
|
|
/// The `*` operator.
|
|
|
|
Mul,
|
|
|
|
/// The `/` operator.
|
|
|
|
Div,
|
|
|
|
/// The `%` operator.
|
|
|
|
Mod,
|
|
|
|
/// The `^` operator.
|
|
|
|
Pow,
|
|
|
|
/// The unary minus (`-`) operator.
|
|
|
|
Unm,
|
|
|
|
/// The floor division (//) operator.
|
|
|
|
IDiv,
|
|
|
|
/// The bitwise AND (&) operator.
|
|
|
|
BAnd,
|
|
|
|
/// The bitwise OR (|) operator.
|
|
|
|
BOr,
|
|
|
|
/// The bitwise XOR (binary ~) operator.
|
|
|
|
BXor,
|
|
|
|
/// The bitwise NOT (unary ~) operator.
|
|
|
|
BNot,
|
|
|
|
/// The bitwise left shift (<<) operator.
|
|
|
|
Shl,
|
|
|
|
/// The bitwise right shift (>>) operator.
|
|
|
|
Shr,
|
|
|
|
/// The string concatenation operator `..`.
|
|
|
|
Concat,
|
|
|
|
/// The length operator `#`.
|
|
|
|
Len,
|
|
|
|
/// The `==` operator.
|
|
|
|
Eq,
|
|
|
|
/// The `<` operator.
|
|
|
|
Lt,
|
|
|
|
/// The `<=` operator.
|
|
|
|
Le,
|
|
|
|
/// Index access `obj[key]`.
|
|
|
|
Index,
|
|
|
|
/// Index write access `obj[key] = value`.
|
|
|
|
NewIndex,
|
|
|
|
/// The call "operator" `obj(arg1, args2, ...)`.
|
|
|
|
Call,
|
2018-01-26 13:44:35 -05:00
|
|
|
/// The `__tostring` metamethod.
|
|
|
|
///
|
|
|
|
/// This is not an operator, but will be called by methods such as `tostring` and `print`.
|
2017-09-30 01:08:08 -04:00
|
|
|
ToString,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Method registry for [`UserData`] implementors.
|
|
|
|
///
|
|
|
|
/// [`UserData`]: trait.UserData.html
|
|
|
|
pub struct UserDataMethods<'lua, T> {
|
2018-02-11 08:51:17 -05:00
|
|
|
pub(crate) methods: HashMap<StdString, Callback<'lua, 'static>>,
|
|
|
|
pub(crate) meta_methods: HashMap<MetaMethod, Callback<'lua, 'static>>,
|
2017-09-30 01:08:08 -04:00
|
|
|
pub(crate) _type: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lua, T: UserData> UserDataMethods<'lua, T> {
|
|
|
|
/// Add a method which accepts a `&T` as the first parameter.
|
|
|
|
///
|
|
|
|
/// Regular methods are implemented by overriding the `__index` metamethod and returning the
|
|
|
|
/// accessed method. This allows them to be used with the expected `userdata:method()` syntax.
|
|
|
|
///
|
|
|
|
/// If `add_meta_method` is used to override the `__index` metamethod, this approach will fall
|
|
|
|
/// back to the user-provided metamethod if no regular method was found.
|
|
|
|
pub fn add_method<A, R, M>(&mut self, name: &str, method: M)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-09 23:35:29 -05:00
|
|
|
M: 'static + Send + for<'a> Fn(&'lua Lua, &'a T, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
2017-10-23 16:42:20 -04:00
|
|
|
self.methods
|
|
|
|
.insert(name.to_owned(), Self::box_method(method));
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a regular method which accepts a `&mut T` as the first parameter.
|
|
|
|
///
|
|
|
|
/// Refer to [`add_method`] for more information about the implementation.
|
|
|
|
///
|
|
|
|
/// [`add_method`]: #method.add_method
|
|
|
|
pub fn add_method_mut<A, R, M>(&mut self, name: &str, method: M)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-06 20:23:16 -05:00
|
|
|
M: 'static + Send + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
2017-10-23 16:42:20 -04:00
|
|
|
self.methods
|
|
|
|
.insert(name.to_owned(), Self::box_method_mut(method));
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a regular method as a function which accepts generic arguments, the first argument will
|
|
|
|
/// always be a `UserData` of type T.
|
|
|
|
///
|
|
|
|
/// Prefer to use [`add_method`] or [`add_method_mut`] as they are easier to use.
|
|
|
|
///
|
|
|
|
/// [`add_method`]: #method.add_method
|
|
|
|
/// [`add_method_mut`]: #method.add_method_mut
|
|
|
|
pub fn add_function<A, R, F>(&mut self, name: &str, function: F)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-09 23:35:29 -05:00
|
|
|
F: 'static + Send + Fn(&'lua Lua, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
2017-10-23 16:42:20 -04:00
|
|
|
self.methods
|
|
|
|
.insert(name.to_owned(), Self::box_function(function));
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
2018-02-11 17:53:25 -05:00
|
|
|
/// Add a regular method as a mutable function which accepts generic arguments, the first
|
|
|
|
/// argument will always be a `UserData` of type T.
|
|
|
|
///
|
|
|
|
/// This is a version of [`add_function`] that accepts a FnMut argument.
|
|
|
|
///
|
|
|
|
/// [`add_function`]: #method.add_function
|
|
|
|
pub fn add_function_mut<A, R, F>(&mut self, name: &str, function: F)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
|
|
|
F: 'static + Send + FnMut(&'lua Lua, A) -> Result<R>,
|
|
|
|
{
|
|
|
|
self.methods
|
|
|
|
.insert(name.to_owned(), Self::box_function_mut(function));
|
|
|
|
}
|
|
|
|
|
2017-09-30 01:08:08 -04:00
|
|
|
/// Add a metamethod which accepts a `&T` as the first parameter.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This can cause an error with certain binary metamethods that can trigger if only the right
|
|
|
|
/// side has a metatable. To prevent this, use [`add_meta_function`].
|
|
|
|
///
|
|
|
|
/// [`add_meta_function`]: #method.add_meta_function
|
|
|
|
pub fn add_meta_method<A, R, M>(&mut self, meta: MetaMethod, method: M)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-09 23:35:29 -05:00
|
|
|
M: 'static + Send + for<'a> Fn(&'lua Lua, &'a T, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
|
|
|
self.meta_methods.insert(meta, Self::box_method(method));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a metamethod as a function which accepts a `&mut T` as the first parameter.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This can cause an error with certain binary metamethods that can trigger if only the right
|
|
|
|
/// side has a metatable. To prevent this, use [`add_meta_function`].
|
|
|
|
///
|
|
|
|
/// [`add_meta_function`]: #method.add_meta_function
|
|
|
|
pub fn add_meta_method_mut<A, R, M>(&mut self, meta: MetaMethod, method: M)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-06 20:23:16 -05:00
|
|
|
M: 'static + Send + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
|
|
|
self.meta_methods.insert(meta, Self::box_method_mut(method));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a metamethod which accepts generic arguments.
|
|
|
|
///
|
|
|
|
/// Metamethods for binary operators can be triggered if either the left or right argument to
|
|
|
|
/// the binary operator has a metatable, so the first argument here is not necessarily a
|
|
|
|
/// userdata of type `T`.
|
|
|
|
pub fn add_meta_function<A, R, F>(&mut self, meta: MetaMethod, function: F)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-09 23:35:29 -05:00
|
|
|
F: 'static + Send + Fn(&'lua Lua, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
|
|
|
self.meta_methods.insert(meta, Self::box_function(function));
|
|
|
|
}
|
|
|
|
|
2018-02-11 17:53:25 -05:00
|
|
|
/// Add a metamethod as a mutable function which accepts generic arguments.
|
|
|
|
///
|
|
|
|
/// This is a version of [`add_meta_function`] that accepts a FnMut argument.
|
|
|
|
///
|
|
|
|
/// [`add_meta_function`]: #method.add_meta_function
|
|
|
|
pub fn add_meta_function_mut<A, R, F>(&mut self, meta: MetaMethod, function: F)
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
|
|
|
F: 'static + Send + FnMut(&'lua Lua, A) -> Result<R>,
|
|
|
|
{
|
|
|
|
self.meta_methods
|
|
|
|
.insert(meta, Self::box_function_mut(function));
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:51:17 -05:00
|
|
|
fn box_function<A, R, F>(function: F) -> Callback<'lua, 'static>
|
2017-09-30 01:08:08 -04:00
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-09 23:35:29 -05:00
|
|
|
F: 'static + Send + Fn(&'lua Lua, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
2017-12-03 18:25:53 -05:00
|
|
|
Box::new(move |lua, args| function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua))
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
2018-02-11 17:53:25 -05:00
|
|
|
fn box_function_mut<A, R, F>(function: F) -> Callback<'lua, 'static>
|
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
|
|
|
F: 'static + Send + FnMut(&'lua Lua, A) -> Result<R>,
|
|
|
|
{
|
|
|
|
let function = RefCell::new(function);
|
|
|
|
Box::new(move |lua, args| {
|
|
|
|
let function = &mut *function
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| Error::RecursiveMutCallback)?;
|
|
|
|
function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:51:17 -05:00
|
|
|
fn box_method<A, R, M>(method: M) -> Callback<'lua, 'static>
|
2017-09-30 01:08:08 -04:00
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-09 23:35:29 -05:00
|
|
|
M: 'static + Send + for<'a> Fn(&'lua Lua, &'a T, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
2017-12-02 18:56:14 -05:00
|
|
|
Box::new(move |lua, mut args| {
|
|
|
|
if let Some(front) = args.pop_front() {
|
|
|
|
let userdata = AnyUserData::from_lua(front, lua)?;
|
|
|
|
let userdata = userdata.borrow::<T>()?;
|
|
|
|
method(lua, &userdata, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
|
|
|
|
} else {
|
|
|
|
Err(Error::FromLuaConversionError {
|
|
|
|
from: "missing argument",
|
|
|
|
to: "userdata",
|
|
|
|
message: None,
|
|
|
|
})
|
|
|
|
}
|
2017-09-30 01:08:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-11 08:51:17 -05:00
|
|
|
fn box_method_mut<A, R, M>(method: M) -> Callback<'lua, 'static>
|
2017-09-30 01:08:08 -04:00
|
|
|
where
|
|
|
|
A: FromLuaMulti<'lua>,
|
|
|
|
R: ToLuaMulti<'lua>,
|
2018-02-06 20:23:16 -05:00
|
|
|
M: 'static + Send + for<'a> FnMut(&'lua Lua, &'a mut T, A) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
2018-02-09 23:35:29 -05:00
|
|
|
let method = RefCell::new(method);
|
2017-12-02 18:56:14 -05:00
|
|
|
Box::new(move |lua, mut args| {
|
|
|
|
if let Some(front) = args.pop_front() {
|
|
|
|
let userdata = AnyUserData::from_lua(front, lua)?;
|
|
|
|
let mut userdata = userdata.borrow_mut::<T>()?;
|
2018-02-09 23:35:29 -05:00
|
|
|
let mut method = method
|
|
|
|
.try_borrow_mut()
|
|
|
|
.map_err(|_| Error::RecursiveMutCallback)?;
|
|
|
|
(&mut *method)(lua, &mut userdata, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
|
2017-12-02 18:56:14 -05:00
|
|
|
} else {
|
|
|
|
Err(Error::FromLuaConversionError {
|
|
|
|
from: "missing argument",
|
|
|
|
to: "userdata",
|
|
|
|
message: None,
|
|
|
|
})
|
|
|
|
}
|
2017-09-30 01:08:08 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for custom userdata types.
|
|
|
|
///
|
|
|
|
/// By implementing this trait, a struct becomes eligible for use inside Lua code. Implementations
|
|
|
|
/// of [`ToLua`] and [`FromLua`] are automatically provided.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # extern crate rlua;
|
|
|
|
/// # use rlua::{Lua, UserData, Result};
|
|
|
|
/// # fn try_main() -> Result<()> {
|
|
|
|
/// struct MyUserData(i32);
|
|
|
|
///
|
|
|
|
/// impl UserData for MyUserData {}
|
|
|
|
///
|
|
|
|
/// let lua = Lua::new();
|
|
|
|
///
|
|
|
|
/// // `MyUserData` now implements `ToLua`:
|
|
|
|
/// lua.globals().set("myobject", MyUserData(123))?;
|
|
|
|
///
|
|
|
|
/// lua.exec::<()>("assert(type(myobject) == 'userdata')", None)?;
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// # fn main() {
|
|
|
|
/// # try_main().unwrap();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Custom methods and operators can be provided by implementing `add_methods` (refer to
|
|
|
|
/// [`UserDataMethods`] for more information):
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # extern crate rlua;
|
|
|
|
/// # use rlua::{Lua, MetaMethod, UserData, UserDataMethods, Result};
|
|
|
|
/// # fn try_main() -> Result<()> {
|
|
|
|
/// struct MyUserData(i32);
|
|
|
|
///
|
|
|
|
/// impl UserData for MyUserData {
|
|
|
|
/// fn add_methods(methods: &mut UserDataMethods<Self>) {
|
|
|
|
/// methods.add_method("get", |_, this, _: ()| {
|
|
|
|
/// Ok(this.0)
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// methods.add_method_mut("add", |_, this, value: i32| {
|
|
|
|
/// this.0 += value;
|
|
|
|
/// Ok(())
|
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// methods.add_meta_method(MetaMethod::Add, |_, this, value: i32| {
|
|
|
|
/// Ok(this.0 + value)
|
|
|
|
/// });
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let lua = Lua::new();
|
|
|
|
///
|
|
|
|
/// lua.globals().set("myobject", MyUserData(123))?;
|
|
|
|
///
|
|
|
|
/// lua.exec::<()>(r#"
|
|
|
|
/// assert(myobject:get() == 123)
|
|
|
|
/// myobject:add(7)
|
|
|
|
/// assert(myobject:get() == 130)
|
|
|
|
/// assert(myobject + 10 == 140)
|
|
|
|
/// "#, None)?;
|
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
|
|
|
/// # fn main() {
|
|
|
|
/// # try_main().unwrap();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`ToLua`]: trait.ToLua.html
|
|
|
|
/// [`FromLua`]: trait.FromLua.html
|
|
|
|
/// [`UserDataMethods`]: struct.UserDataMethods.html
|
|
|
|
pub trait UserData: 'static + Sized {
|
|
|
|
/// Adds custom methods and operators specific to this userdata.
|
|
|
|
fn add_methods(_methods: &mut UserDataMethods<Self>) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle to an internal Lua userdata for any type that implements [`UserData`].
|
|
|
|
///
|
|
|
|
/// Similar to `std::any::Any`, this provides an interface for dynamic type checking via the [`is`]
|
|
|
|
/// and [`borrow`] methods.
|
|
|
|
///
|
|
|
|
/// Internally, instances are stored in a `RefCell`, to best match the mutable semantics of the Lua
|
|
|
|
/// language.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This API should only be used when necessary. Implementing [`UserData`] already allows defining
|
|
|
|
/// methods which check the type and acquire a borrow behind the scenes.
|
|
|
|
///
|
|
|
|
/// [`UserData`]: trait.UserData.html
|
|
|
|
/// [`is`]: #method.is
|
|
|
|
/// [`borrow`]: #method.borrow
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct AnyUserData<'lua>(pub(crate) LuaRef<'lua>);
|
|
|
|
|
|
|
|
impl<'lua> AnyUserData<'lua> {
|
|
|
|
/// Checks whether the type of this userdata is `T`.
|
2017-12-03 23:45:00 -05:00
|
|
|
pub fn is<T: UserData>(&self) -> Result<bool> {
|
|
|
|
match self.inspect(|_: &RefCell<T>| Ok(())) {
|
|
|
|
Ok(()) => Ok(true),
|
|
|
|
Err(Error::UserDataTypeMismatch) => Ok(false),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
}
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrow this userdata immutably if it is of type `T`.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns a `UserDataBorrowError` if the userdata is already mutably borrowed. Returns a
|
|
|
|
/// `UserDataTypeMismatch` if the userdata is not of type `T`.
|
|
|
|
pub fn borrow<T: UserData>(&self) -> Result<Ref<T>> {
|
2017-12-03 18:25:53 -05:00
|
|
|
self.inspect(|cell| Ok(cell.try_borrow().map_err(|_| Error::UserDataBorrowError)?))
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrow this userdata mutably if it is of type `T`.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns a `UserDataBorrowMutError` if the userdata is already borrowed. Returns a
|
|
|
|
/// `UserDataTypeMismatch` if the userdata is not of type `T`.
|
|
|
|
pub fn borrow_mut<T: UserData>(&self) -> Result<RefMut<T>> {
|
|
|
|
self.inspect(|cell| {
|
2017-10-23 16:42:20 -04:00
|
|
|
Ok(cell.try_borrow_mut()
|
|
|
|
.map_err(|_| Error::UserDataBorrowMutError)?)
|
2017-12-03 23:45:00 -05:00
|
|
|
})
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
|
2017-12-03 23:45:00 -05:00
|
|
|
fn inspect<'a, T, R, F>(&'a self, func: F) -> Result<R>
|
2017-09-30 01:08:08 -04:00
|
|
|
where
|
|
|
|
T: UserData,
|
2017-12-03 23:45:00 -05:00
|
|
|
F: FnOnce(&'a RefCell<T>) -> Result<R>,
|
2017-09-30 01:08:08 -04:00
|
|
|
{
|
|
|
|
unsafe {
|
|
|
|
let lua = self.0.lua;
|
2017-12-04 02:46:57 -05:00
|
|
|
stack_err_guard(lua.state, 0, move || {
|
2017-09-30 01:08:08 -04:00
|
|
|
check_stack(lua.state, 3);
|
|
|
|
|
|
|
|
lua.push_ref(lua.state, &self.0);
|
|
|
|
|
Change strategies for handling the Lua stack during panics
Previously, on an internal panic, the Lua stack would be reset before panicking
in an attempt to make sure that such panics would not cause stack leaks or leave
the stack in an unknown state. Now, such panic handling is done in stack_guard
and stack_err_guard instead, and this is for a few reasons:
1) The previous approach did NOT handle user triggered panics that were outside
of `rlua`, such as a panic in a ToLua / FromLua implementation. This is
especially bad since most other panics would be indicative of an internal bug
anyway, so the utility of keeping `rlua` types usable after such panics was
questionable. It is much more sensible to ensure that `rlua` types are
usable after *user generated* panics.
2) Every entry point into `rlua` should be guarded by a stack_guard or
stack_err_guard anyway, so this should restore the Lua stack on exiting back
to user code in all cases.
3) The method of stack restoration no longer *clears* the stack, only resets it
to what it previously was. This allows us, potentially, to keep values at
the beginning of the Lua stack long term and know that panics will not
clobber them. There may be a way of dramatically speeding up ref types by
using a small static area at the beginning of the stack instead of only the
registry, so this may be important.
2018-03-08 10:59:50 -05:00
|
|
|
rlua_assert!(
|
2017-09-30 01:08:08 -04:00
|
|
|
ffi::lua_getmetatable(lua.state, -1) != 0,
|
|
|
|
"AnyUserData missing metatable"
|
|
|
|
);
|
|
|
|
|
|
|
|
ffi::lua_rawgeti(
|
|
|
|
lua.state,
|
|
|
|
ffi::LUA_REGISTRYINDEX,
|
2017-12-03 23:45:00 -05:00
|
|
|
lua.userdata_metatable::<T>()? as ffi::lua_Integer,
|
2017-09-30 01:08:08 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
if ffi::lua_rawequal(lua.state, -1, -2) == 0 {
|
|
|
|
ffi::lua_pop(lua.state, 3);
|
2017-12-03 23:45:00 -05:00
|
|
|
Err(Error::UserDataTypeMismatch)
|
2017-09-30 01:08:08 -04:00
|
|
|
} else {
|
Simplify handling of userdata __gc and resurrected userdata.
Now, simply remove the userdata table immediately before dropping the userdata.
This does two things, it prevents __gc from double dropping the userdata, and
after the first call to __gc, it prevents the userdata from being identified as
any particular userdata type, so it cannot be misused after being finalized.
This change thus removes the userdata invalidation error, and simplifies a lot
of userdata handling code.
It also fixes a panic bug. Because there is no predictable order for
finalizers, it is possible to run a userdata finalizer that does not resurrect
itself before a lua table finalizer that accesses that userdata, and this means
that there were several asserts that were possible to trigger in normal Lua code
in util.rs related to `WrappedError`.
Now, finalized userdata is simply a userdata with no methods, so any use of
finalized userdata becomes a normal script runtime error (though, with a
potentially confusing error message). As a future improvement, we could set
a metatable on finalized userdata that provides a better error message.
2018-01-27 18:12:39 -05:00
|
|
|
let res = func(&*get_userdata::<RefCell<T>>(lua.state, -3));
|
2017-09-30 01:08:08 -04:00
|
|
|
ffi::lua_pop(lua.state, 3);
|
2017-12-03 23:45:00 -05:00
|
|
|
res
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-01-26 20:06:18 -05:00
|
|
|
|
|
|
|
/// Sets an associated value to this `AnyUserData`.
|
|
|
|
///
|
|
|
|
/// The value may be any Lua value whatsoever, and can be retrieved with [`get_user_value`].
|
|
|
|
///
|
|
|
|
/// [`get_user_value`]: #method.get_user_value
|
|
|
|
pub fn set_user_value<V: ToLua<'lua>>(&self, v: V) -> Result<()> {
|
|
|
|
let lua = self.0.lua;
|
|
|
|
unsafe {
|
2018-02-11 16:47:39 -05:00
|
|
|
stack_err_guard(lua.state, 0, || {
|
2018-01-26 20:06:18 -05:00
|
|
|
check_stack(lua.state, 2);
|
|
|
|
lua.push_ref(lua.state, &self.0);
|
|
|
|
lua.push_value(lua.state, v.to_lua(lua)?);
|
|
|
|
ffi::lua_setuservalue(lua.state, -2);
|
|
|
|
ffi::lua_pop(lua.state, 1);
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an associated value set by [`set_user_value`].
|
|
|
|
///
|
|
|
|
/// [`set_user_value`]: #method.set_user_value
|
|
|
|
pub fn get_user_value<V: FromLua<'lua>>(&self) -> Result<V> {
|
|
|
|
let lua = self.0.lua;
|
|
|
|
unsafe {
|
2018-02-11 16:47:39 -05:00
|
|
|
stack_err_guard(lua.state, 0, || {
|
2018-02-12 13:42:13 -05:00
|
|
|
check_stack(lua.state, 3);
|
2018-01-26 20:06:18 -05:00
|
|
|
lua.push_ref(lua.state, &self.0);
|
|
|
|
ffi::lua_getuservalue(lua.state, -1);
|
|
|
|
let res = V::from_lua(lua.pop_value(lua.state), lua)?;
|
|
|
|
ffi::lua_pop(lua.state, 1);
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-09-30 01:08:08 -04:00
|
|
|
}
|