use std::any::TypeId;
use std::cell::RefCell;
use std::ffi::CString;
use std::marker::PhantomData;
use std::os::raw::{c_char, c_int, c_void};
use std::sync::Arc;
use std::{mem, ptr};
use crate::error::{Error, Result};
use crate::ffi;
use crate::function::Function;
use crate::lua::{extra_data, ExtraData, FUNCTION_METATABLE_REGISTRY_KEY};
use crate::markers::{Invariant, NoUnwindSafe};
use crate::scope::Scope;
use crate::string::String;
use crate::table::Table;
use crate::thread::Thread;
use crate::types::{Callback, Integer, LightUserData, LuaRef, Number, RegistryKey};
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataMethods};
use crate::util::{
assert_stack, callback_error, check_stack, get_userdata, get_wrapped_error,
init_userdata_metatable, pop_error, protect_lua, protect_lua_closure, push_string,
push_userdata, push_wrapped_error, StackGuard,
};
use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti, Value};
#[derive(Copy, Clone)]
pub struct Context<'lua> {
pub(crate) state: *mut ffi::lua_State,
_lua_invariant: Invariant<'lua>,
_no_unwind_safe: NoUnwindSafe,
}
impl<'lua> Context<'lua> {
pub fn load<'a, S>(self, source: &'a S) -> Chunk<'lua, 'a>
where
S: ?Sized + AsRef<[u8]>,
{
Chunk {
context: self,
source: source.as_ref(),
name: None,
env: None,
}
}
pub fn create_string<S>(self, s: &S) -> Result<String<'lua>>
where
S: ?Sized + AsRef<[u8]>,
{
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
push_string(self.state, s)?;
Ok(String(self.pop_ref()))
}
}
pub fn create_table(self) -> Result<Table<'lua>> {
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 3);
unsafe extern "C" fn new_table(state: *mut ffi::lua_State) -> c_int {
ffi::lua_newtable(state);
1
}
protect_lua(self.state, 0, new_table)?;
Ok(Table(self.pop_ref()))
}
}
pub fn create_table_from<K, V, I>(self, cont: I) -> Result<Table<'lua>>
where
K: ToLua<'lua>,
V: ToLua<'lua>,
I: IntoIterator<Item = (K, V)>,
{
unsafe {
let _sg = StackGuard::new(self.state);
check_stack(self.state, 5 + ffi::LUA_MINSTACK)?;
unsafe extern "C" fn new_table(state: *mut ffi::lua_State) -> c_int {
ffi::lua_newtable(state);
1
}
protect_lua(self.state, 0, new_table)?;
for (k, v) in cont {
self.push_value(k.to_lua(self)?)?;
self.push_value(v.to_lua(self)?)?;
unsafe extern "C" fn raw_set(state: *mut ffi::lua_State) -> c_int {
ffi::lua_rawset(state, -3);
1
}
protect_lua(self.state, 3, raw_set)?;
}
Ok(Table(self.pop_ref()))
}
}
pub fn create_sequence_from<T, I>(self, cont: I) -> Result<Table<'lua>>
where
T: ToLua<'lua>,
I: IntoIterator<Item = T>,
{
self.create_table_from(cont.into_iter().enumerate().map(|(k, v)| (k + 1, v)))
}
pub fn create_function<A, R, F>(self, func: F) -> Result<Function<'lua>>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + Fn(Context<'lua>, A) -> Result<R>,
{
self.create_callback(Box::new(move |lua, args| {
func(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua)
}))
}
pub fn create_function_mut<A, R, F>(self, func: F) -> Result<Function<'lua>>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + FnMut(Context<'lua>, A) -> Result<R>,
{
let func = RefCell::new(func);
self.create_function(move |lua, args| {
(&mut *func
.try_borrow_mut()
.map_err(|_| Error::RecursiveMutCallback)?)(lua, args)
})
}
pub fn create_thread(self, func: Function<'lua>) -> Result<Thread<'lua>> {
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
let thread_state =
protect_lua_closure(self.state, 0, 1, |state| ffi::lua_newthread(state))?;
self.push_ref(&func.0);
ffi::lua_xmove(self.state, thread_state, 1);
Ok(Thread(self.pop_ref()))
}
}
pub fn create_userdata<T>(self, data: T) -> Result<AnyUserData<'lua>>
where
T: 'static + Send + UserData,
{
unsafe { self.make_userdata(data) }
}
pub fn globals(self) -> Table<'lua> {
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
ffi::lua_rawgeti(self.state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_GLOBALS);
Table(self.pop_ref())
}
}
pub fn scope<'scope, F, R>(self, f: F) -> R
where
F: FnOnce(&Scope<'lua, 'scope>) -> R,
{
f(&Scope::new(unsafe { Context::new(self.state) }))
}
pub fn coerce_string(self, v: Value<'lua>) -> Result<Option<String<'lua>>> {
Ok(match v {
Value::String(s) => Some(s),
v => unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
self.push_value(v)?;
if protect_lua_closure(self.state, 1, 1, |state| {
!ffi::lua_tostring(state, -1).is_null()
})? {
Some(String(self.pop_ref()))
} else {
None
}
},
})
}
pub fn coerce_integer(self, v: Value<'lua>) -> Result<Option<Integer>> {
Ok(match v {
Value::Integer(i) => Some(i),
v => unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
self.push_value(v)?;
let mut isint = 0;
let i = ffi::lua_tointegerx(self.state, -1, &mut isint);
if isint == 0 {
None
} else {
Some(i)
}
},
})
}
pub fn coerce_number(self, v: Value<'lua>) -> Result<Option<Number>> {
Ok(match v {
Value::Number(n) => Some(n),
v => unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
self.push_value(v)?;
let mut isnum = 0;
let n = ffi::lua_tonumberx(self.state, -1, &mut isnum);
if isnum == 0 {
None
} else {
Some(n)
}
},
})
}
pub fn pack<T: ToLua<'lua>>(self, t: T) -> Result<Value<'lua>> {
t.to_lua(self)
}
pub fn unpack<T: FromLua<'lua>>(self, value: Value<'lua>) -> Result<T> {
T::from_lua(value, self)
}
pub fn pack_multi<T: ToLuaMulti<'lua>>(self, t: T) -> Result<MultiValue<'lua>> {
t.to_lua_multi(self)
}
pub fn unpack_multi<T: FromLuaMulti<'lua>>(self, value: MultiValue<'lua>) -> Result<T> {
T::from_lua_multi(value, self)
}
pub fn set_named_registry_value<S, T>(self, name: &S, t: T) -> Result<()>
where
S: ?Sized + AsRef<[u8]>,
T: ToLua<'lua>,
{
let t = t.to_lua(self)?;
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 5);
push_string(self.state, name)?;
self.push_value(t)?;
unsafe extern "C" fn set_registry(state: *mut ffi::lua_State) -> c_int {
ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
0
}
protect_lua(self.state, 2, set_registry)
}
}
pub fn named_registry_value<S, T>(self, name: &S) -> Result<T>
where
S: ?Sized + AsRef<[u8]>,
T: FromLua<'lua>,
{
let value = unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
push_string(self.state, name)?;
unsafe extern "C" fn get_registry(state: *mut ffi::lua_State) -> c_int {
ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
1
}
protect_lua(self.state, 1, get_registry)?;
self.pop_value()
};
T::from_lua(value, self)
}
pub fn unset_named_registry_value<S: ?Sized + AsRef<[u8]>>(self, name: &S) -> Result<()> {
self.set_named_registry_value(name, Nil)
}
pub fn create_registry_value<T: ToLua<'lua>>(self, t: T) -> Result<RegistryKey> {
let t = t.to_lua(self)?;
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
self.push_value(t)?;
let registry_id = protect_lua_closure(self.state, 1, 0, |state| {
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
})?;
Ok(RegistryKey {
registry_id,
unref_list: (*extra_data(self.state)).registry_unref_list.clone(),
})
}
}
pub fn registry_value<T: FromLua<'lua>>(self, key: &RegistryKey) -> Result<T> {
let value = unsafe {
if !self.owns_registry_value(key) {
return Err(Error::MismatchedRegistryKey);
}
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 2);
ffi::lua_rawgeti(
self.state,
ffi::LUA_REGISTRYINDEX,
key.registry_id as ffi::lua_Integer,
);
self.pop_value()
};
T::from_lua(value, self)
}
pub fn remove_registry_value(self, key: RegistryKey) -> Result<()> {
unsafe {
if !self.owns_registry_value(&key) {
return Err(Error::MismatchedRegistryKey);
}
ffi::luaL_unref(self.state, ffi::LUA_REGISTRYINDEX, key.take());
Ok(())
}
}
pub fn owns_registry_value(self, key: &RegistryKey) -> bool {
unsafe {
Arc::ptr_eq(
&key.unref_list,
&(*extra_data(self.state)).registry_unref_list,
)
}
}
pub fn expire_registry_values(self) {
unsafe {
let unref_list = mem::replace(
&mut *rlua_expect!(
(*extra_data(self.state)).registry_unref_list.lock(),
"unref list poisoned"
),
Some(Vec::new()),
);
for id in rlua_expect!(unref_list, "unref list not set") {
ffi::luaL_unref(self.state, ffi::LUA_REGISTRYINDEX, id);
}
}
}
pub(crate) unsafe fn push_value(self, value: Value<'lua>) -> Result<()> {
match value {
Value::Nil => {
ffi::lua_pushnil(self.state);
}
Value::Boolean(b) => {
ffi::lua_pushboolean(self.state, if b { 1 } else { 0 });
}
Value::LightUserData(ud) => {
ffi::lua_pushlightuserdata(self.state, ud.0);
}
Value::Integer(i) => {
ffi::lua_pushinteger(self.state, i);
}
Value::Number(n) => {
ffi::lua_pushnumber(self.state, n);
}
Value::String(s) => {
self.push_ref(&s.0);
}
Value::Table(t) => {
self.push_ref(&t.0);
}
Value::Function(f) => {
self.push_ref(&f.0);
}
Value::Thread(t) => {
self.push_ref(&t.0);
}
Value::UserData(ud) => {
self.push_ref(&ud.0);
}
Value::Error(e) => {
push_wrapped_error(self.state, e)?;
}
}
Ok(())
}
pub(crate) unsafe fn pop_value(self) -> Value<'lua> {
match ffi::lua_type(self.state, -1) {
ffi::LUA_TNIL => {
ffi::lua_pop(self.state, 1);
Nil
}
ffi::LUA_TBOOLEAN => {
let b = Value::Boolean(ffi::lua_toboolean(self.state, -1) != 0);
ffi::lua_pop(self.state, 1);
b
}
ffi::LUA_TLIGHTUSERDATA => {
let ud = Value::LightUserData(LightUserData(ffi::lua_touserdata(self.state, -1)));
ffi::lua_pop(self.state, 1);
ud
}
ffi::LUA_TNUMBER => {
if ffi::lua_isinteger(self.state, -1) != 0 {
let i = Value::Integer(ffi::lua_tointeger(self.state, -1));
ffi::lua_pop(self.state, 1);
i
} else {
let n = Value::Number(ffi::lua_tonumber(self.state, -1));
ffi::lua_pop(self.state, 1);
n
}
}
ffi::LUA_TSTRING => Value::String(String(self.pop_ref())),
ffi::LUA_TTABLE => Value::Table(Table(self.pop_ref())),
ffi::LUA_TFUNCTION => Value::Function(Function(self.pop_ref())),
ffi::LUA_TUSERDATA => {
if let Some(err) = get_wrapped_error(self.state, -1).as_ref() {
let err = err.clone();
ffi::lua_pop(self.state, 1);
Value::Error(err)
} else {
Value::UserData(AnyUserData(self.pop_ref()))
}
}
ffi::LUA_TTHREAD => Value::Thread(Thread(self.pop_ref())),
_ => rlua_panic!("LUA_TNONE in pop_value"),
}
}
pub(crate) unsafe fn push_ref(self, lref: &LuaRef<'lua>) {
let extra = extra_data(self.state);
ffi::lua_pushvalue((*extra).ref_thread, lref.index);
ffi::lua_xmove((*extra).ref_thread, self.state, 1);
}
pub(crate) unsafe fn pop_ref(self) -> LuaRef<'lua> {
let extra = extra_data(self.state);
ffi::lua_xmove(self.state, (*extra).ref_thread, 1);
let index = ref_stack_pop(extra);
LuaRef { lua: self, index }
}
pub(crate) fn clone_ref(self, lref: &LuaRef<'lua>) -> LuaRef<'lua> {
unsafe {
let extra = extra_data(self.state);
ffi::lua_pushvalue((*extra).ref_thread, lref.index);
let index = ref_stack_pop(extra);
LuaRef { lua: self, index }
}
}
pub(crate) fn drop_ref(self, lref: &mut LuaRef<'lua>) {
unsafe {
let extra = extra_data(self.state);
ffi::lua_pushnil((*extra).ref_thread);
ffi::lua_replace((*extra).ref_thread, lref.index);
(*extra).ref_free.push(lref.index);
}
}
pub(crate) unsafe fn userdata_metatable<T: 'static + UserData>(self) -> Result<c_int> {
if let Some(table_id) = (*extra_data(self.state))
.registered_userdata
.get(&TypeId::of::<T>())
{
return Ok(*table_id);
}
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 8);
let mut methods = StaticUserDataMethods::default();
T::add_methods(&mut methods);
protect_lua_closure(self.state, 0, 1, |state| {
ffi::lua_newtable(state);
})?;
for (k, m) in methods.meta_methods {
push_string(self.state, k.name())?;
self.push_value(Value::Function(self.create_callback(m)?))?;
protect_lua_closure(self.state, 3, 1, |state| {
ffi::lua_rawset(state, -3);
})?;
}
if methods.methods.is_empty() {
init_userdata_metatable::<RefCell<T>>(self.state, -1, None)?;
} else {
protect_lua_closure(self.state, 0, 1, |state| {
ffi::lua_newtable(state);
})?;
for (k, m) in methods.methods {
push_string(self.state, &k)?;
self.push_value(Value::Function(self.create_callback(m)?))?;
protect_lua_closure(self.state, 3, 1, |state| {
ffi::lua_rawset(state, -3);
})?;
}
init_userdata_metatable::<RefCell<T>>(self.state, -2, Some(-1))?;
ffi::lua_pop(self.state, 1);
}
let id = protect_lua_closure(self.state, 1, 0, |state| {
ffi::luaL_ref(state, ffi::LUA_REGISTRYINDEX)
})?;
(*extra_data(self.state))
.registered_userdata
.insert(TypeId::of::<T>(), id);
Ok(id)
}
pub(crate) fn create_callback(self, func: Callback<'lua, 'static>) -> Result<Function<'lua>> {
unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int {
callback_error(state, |nargs| {
if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL {
return Err(Error::CallbackDestructed);
}
if nargs < ffi::LUA_MINSTACK {
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
}
let context = Context::new(state);
let mut args = MultiValue::new();
args.reserve(nargs as usize);
for _ in 0..nargs {
args.push_front(context.pop_value());
}
let func = get_userdata::<Callback>(state, ffi::lua_upvalueindex(1));
let results = (*func)(context, args)?;
let nresults = results.len() as c_int;
check_stack(state, nresults)?;
for r in results {
context.push_value(r)?;
}
Ok(nresults)
})
}
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
push_userdata::<Callback>(self.state, func)?;
ffi::lua_pushlightuserdata(
self.state,
&FUNCTION_METATABLE_REGISTRY_KEY as *const u8 as *mut c_void,
);
ffi::lua_rawget(self.state, ffi::LUA_REGISTRYINDEX);
ffi::lua_setmetatable(self.state, -2);
protect_lua_closure(self.state, 1, 1, |state| {
ffi::lua_pushcclosure(state, call_callback, 1);
})?;
Ok(Function(self.pop_ref()))
}
}
pub(crate) unsafe fn make_userdata<T>(self, data: T) -> Result<AnyUserData<'lua>>
where
T: 'static + UserData,
{
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 4);
let ud_index = self.userdata_metatable::<T>()?;
push_userdata::<RefCell<T>>(self.state, RefCell::new(data))?;
ffi::lua_rawgeti(
self.state,
ffi::LUA_REGISTRYINDEX,
ud_index as ffi::lua_Integer,
);
ffi::lua_setmetatable(self.state, -2);
Ok(AnyUserData(self.pop_ref()))
}
pub(crate) unsafe fn new(state: *mut ffi::lua_State) -> Context<'lua> {
Context {
state,
_lua_invariant: PhantomData,
_no_unwind_safe: PhantomData,
}
}
fn load_chunk(
&self,
source: &[u8],
name: Option<&CString>,
env: Option<Value<'lua>>,
) -> Result<Function<'lua>> {
unsafe {
let _sg = StackGuard::new(self.state);
assert_stack(self.state, 1);
let source = source.as_ref();
match if let Some(name) = name {
ffi::luaL_loadbufferx(
self.state,
source.as_ptr() as *const c_char,
source.len(),
name.as_ptr() as *const c_char,
cstr!("t"),
)
} else {
ffi::luaL_loadbufferx(
self.state,
source.as_ptr() as *const c_char,
source.len(),
ptr::null(),
cstr!("t"),
)
} {
ffi::LUA_OK => {
if let Some(env) = env {
self.push_value(env)?;
ffi::lua_setupvalue(self.state, -2, 1);
}
Ok(Function(self.pop_ref()))
}
err => Err(pop_error(self.state, err)),
}
}
}
}
#[must_use = "`Chunk`s do nothing unless one of `exec`, `eval`, `call`, or `into_function` are called on them"]
pub struct Chunk<'lua, 'a> {
context: Context<'lua>,
source: &'a [u8],
name: Option<CString>,
env: Option<Value<'lua>>,
}
impl<'lua, 'a> Chunk<'lua, 'a> {
pub fn set_name<S: ?Sized + AsRef<[u8]>>(mut self, name: &S) -> Result<Chunk<'lua, 'a>> {
let name =
CString::new(name.as_ref().to_vec()).map_err(|e| Error::ToLuaConversionError {
from: "&str",
to: "string",
message: Some(e.to_string()),
})?;
self.name = Some(name);
Ok(self)
}
pub fn set_environment<V: ToLua<'lua>>(mut self, env: V) -> Result<Chunk<'lua, 'a>> {
self.env = Some(env.to_lua(self.context)?);
Ok(self)
}
pub fn exec(self) -> Result<()> {
self.call(())?;
Ok(())
}
pub fn eval<R: FromLuaMulti<'lua>>(self) -> Result<R> {
let mut expression_source = b"return ".to_vec();
expression_source.extend(self.source.as_ref());
if let Ok(function) =
self.context
.load_chunk(&expression_source, self.name.as_ref(), self.env.clone())
{
function.call(())
} else {
self.call(())
}
}
pub fn call<A: ToLuaMulti<'lua>, R: FromLuaMulti<'lua>>(self, args: A) -> Result<R> {
self.into_function()?.call(args)
}
pub fn into_function(self) -> Result<Function<'lua>> {
self.context
.load_chunk(self.source, self.name.as_ref(), self.env)
}
}
unsafe fn ref_stack_pop(extra: *mut ExtraData) -> c_int {
if let Some(free) = (*extra).ref_free.pop() {
ffi::lua_replace((*extra).ref_thread, free);
free
} else {
if (*extra).ref_stack_max >= (*extra).ref_stack_size {
if ffi::lua_checkstack((*extra).ref_thread, (*extra).ref_stack_size) == 0 {
rlua_panic!("cannot create a Lua reference, out of auxiliary stack space");
}
(*extra).ref_stack_size *= 2;
}
(*extra).ref_stack_max += 1;
(*extra).ref_stack_max
}
}
struct StaticUserDataMethods<'lua, T: 'static + UserData> {
methods: Vec<(Vec<u8>, Callback<'lua, 'static>)>,
meta_methods: Vec<(MetaMethod, Callback<'lua, 'static>)>,
_type: PhantomData<T>,
}
impl<'lua, T: 'static + UserData> Default for StaticUserDataMethods<'lua, T> {
fn default() -> StaticUserDataMethods<'lua, T> {
StaticUserDataMethods {
methods: Vec::new(),
meta_methods: Vec::new(),
_type: PhantomData,
}
}
}
impl<'lua, T: 'static + UserData> UserDataMethods<'lua, T> for StaticUserDataMethods<'lua, T> {
fn add_method<S, A, R, M>(&mut self, name: &S, method: M)
where
S: ?Sized + AsRef<[u8]>,
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
M: 'static + Send + Fn(Context<'lua>, &T, A) -> Result<R>,
{
self.methods
.push((name.as_ref().to_vec(), Self::box_method(method)));
}
fn add_method_mut<S, A, R, M>(&mut self, name: &S, method: M)
where
S: ?Sized + AsRef<[u8]>,
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
M: 'static + Send + FnMut(Context<'lua>, &mut T, A) -> Result<R>,
{
self.methods
.push((name.as_ref().to_vec(), Self::box_method_mut(method)));
}
fn add_function<S, A, R, F>(&mut self, name: &S, function: F)
where
S: ?Sized + AsRef<[u8]>,
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + Fn(Context<'lua>, A) -> Result<R>,
{
self.methods
.push((name.as_ref().to_vec(), Self::box_function(function)));
}
fn add_function_mut<S, A, R, F>(&mut self, name: &S, function: F)
where
S: ?Sized + AsRef<[u8]>,
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + FnMut(Context<'lua>, A) -> Result<R>,
{
self.methods
.push((name.as_ref().to_vec(), Self::box_function_mut(function)));
}
fn add_meta_method<A, R, M>(&mut self, meta: MetaMethod, method: M)
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
M: 'static + Send + Fn(Context<'lua>, &T, A) -> Result<R>,
{
self.meta_methods.push((meta, Self::box_method(method)));
}
fn add_meta_method_mut<A, R, M>(&mut self, meta: MetaMethod, method: M)
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
M: 'static + Send + FnMut(Context<'lua>, &mut T, A) -> Result<R>,
{
self.meta_methods.push((meta, Self::box_method_mut(method)));
}
fn add_meta_function<A, R, F>(&mut self, meta: MetaMethod, function: F)
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + Fn(Context<'lua>, A) -> Result<R>,
{
self.meta_methods.push((meta, Self::box_function(function)));
}
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(Context<'lua>, A) -> Result<R>,
{
self.meta_methods
.push((meta, Self::box_function_mut(function)));
}
}
impl<'lua, T: 'static + UserData> StaticUserDataMethods<'lua, T> {
fn box_method<A, R, M>(method: M) -> Callback<'lua, 'static>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
M: 'static + Send + Fn(Context<'lua>, &T, A) -> Result<R>,
{
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,
})
}
})
}
fn box_method_mut<A, R, M>(method: M) -> Callback<'lua, 'static>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
M: 'static + Send + FnMut(Context<'lua>, &mut T, A) -> Result<R>,
{
let method = RefCell::new(method);
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>()?;
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)
} else {
Err(Error::FromLuaConversionError {
from: "missing argument",
to: "userdata",
message: None,
})
}
})
}
fn box_function<A, R, F>(function: F) -> Callback<'lua, 'static>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + Fn(Context<'lua>, A) -> Result<R>,
{
Box::new(move |lua, args| function(lua, A::from_lua_multi(args, lua)?)?.to_lua_multi(lua))
}
fn box_function_mut<A, R, F>(function: F) -> Callback<'lua, 'static>
where
A: FromLuaMulti<'lua>,
R: ToLuaMulti<'lua>,
F: 'static + Send + FnMut(Context<'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)
})
}
}