2024-01-22 03:53:53 +00:00
|
|
|
use std::ffi::CStr;
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
use crate::{AsLua, FromLuaStack, LuaRef, PushToLuaStack, StackGuard, State};
|
2024-01-22 03:53:53 +00:00
|
|
|
|
|
|
|
use mlua_sys as lua;
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
#[derive(Clone)]
|
2024-01-22 03:53:53 +00:00
|
|
|
pub struct Function<'a> {
|
|
|
|
state: &'a State,
|
|
|
|
lref: LuaRef
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FromLuaStack<'a> for Function<'a> {
|
|
|
|
unsafe fn from_lua_stack(state: &'a crate::State) -> crate::Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
state,
|
|
|
|
lref: LuaRef::from_stack(state)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
impl<'a> PushToLuaStack<'a> for Function<'a> {
|
2024-01-22 03:53:53 +00:00
|
|
|
unsafe fn push_to_lua_stack(&self, state: &State) -> crate::Result<()> {
|
|
|
|
self.lref.push_to_lua_stack(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Function<'a> {
|
2024-01-26 04:47:30 +00:00
|
|
|
pub fn from_ref(state: &'a State, lref: LuaRef) -> Self {
|
|
|
|
Self {
|
|
|
|
state,
|
|
|
|
lref,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 03:53:53 +00:00
|
|
|
pub fn exec<A, R>(&self, args: A) -> crate::Result<R>
|
|
|
|
where
|
2024-01-26 04:47:30 +00:00
|
|
|
A: PushToLuaStackMulti<'a>,
|
|
|
|
R: FromLuaStackMulti<'a>,
|
2024-01-22 03:53:53 +00:00
|
|
|
{
|
|
|
|
unsafe {
|
2024-01-26 04:47:30 +00:00
|
|
|
let _g = StackGuard::new(self.state);
|
|
|
|
|
2024-01-22 03:53:53 +00:00
|
|
|
self.push_to_lua_stack(self.state)?;
|
|
|
|
args.push_args_to_lua_stack(self.state)?;
|
|
|
|
|
|
|
|
let arg_len = args.len() as i32;
|
|
|
|
let res_len = R::len() as i32;
|
|
|
|
let s = self.state.state_ptr();
|
|
|
|
if lua::lua_pcall(s, arg_len, res_len, 0) != 0 {
|
|
|
|
let s = lua::lua_tostring(s, -1);
|
|
|
|
let s = CStr::from_ptr(s);
|
|
|
|
let s = s.to_str().unwrap();
|
|
|
|
|
|
|
|
return Err(crate::Error::runtime(s));
|
|
|
|
}
|
|
|
|
|
|
|
|
R::results_from_lua_stack(self.state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
impl<'a> AsLua<'a> for Function<'a> {
|
|
|
|
fn as_lua(&self, lua: &'a State) -> crate::Result<crate::Value<'a>> {
|
|
|
|
Ok(crate::Value::Function(self.clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
pub trait PushToLuaStackMulti<'a> {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len(&self) -> usize;
|
2024-01-26 20:43:26 +00:00
|
|
|
fn push_args_to_lua_stack(&self, state: &'a State) -> crate::Result<()>;
|
2024-01-22 03:53:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
impl<'a, T> PushToLuaStackMulti<'a> for T
|
2024-01-22 03:53:53 +00:00
|
|
|
where
|
2024-01-26 20:43:26 +00:00
|
|
|
T: PushToLuaStack<'a>,
|
2024-01-22 03:53:53 +00:00
|
|
|
{
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
fn push_args_to_lua_stack(&self, state: &'a State) -> crate::Result<()> {
|
2024-01-22 03:53:53 +00:00
|
|
|
unsafe {
|
|
|
|
self.push_to_lua_stack(state)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
pub trait FromLuaStackMulti<'a>: Sized {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len() -> usize;
|
|
|
|
fn results_from_lua_stack(state: &'a State) -> crate::Result<Self>;
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
impl<'a> FromLuaStackMulti<'a> for () {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len() -> usize {
|
2024-01-26 04:47:30 +00:00
|
|
|
1
|
2024-01-22 03:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn results_from_lua_stack(_state: &'a State) -> crate::Result<Self> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
impl<'a, T: FromLuaStack<'a>> FromLuaStackMulti<'a> for T {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn results_from_lua_stack(state: &'a State) -> crate::Result<Self> {
|
|
|
|
unsafe { T::from_lua_stack(state) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_function_arg_tuple {
|
|
|
|
( $count: expr, $first: tt, $( $name: tt ),+ ) => (
|
|
|
|
#[allow(non_snake_case)]
|
2024-01-26 20:43:26 +00:00
|
|
|
impl<'a, $first: PushToLuaStack<'a>, $($name: PushToLuaStack<'a>,)+> PushToLuaStackMulti<'a> for ($first, $($name,)+) {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len(&self) -> usize {
|
|
|
|
// this will end up generating $count - 1 - 1 - 1... hopefully the compiler will
|
|
|
|
// optimize that out
|
|
|
|
$count
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
fn push_args_to_lua_stack(&self, state: &'a State) -> crate::Result<()> {
|
2024-01-22 03:53:53 +00:00
|
|
|
let ($first, $($name,)+) = self;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
$first.push_to_lua_stack(state)?;
|
|
|
|
$( $name.push_to_lua_stack(state)?; )+
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
impl<'a, $first: FromLuaStack<'a>, $($name: FromLuaStack<'a>,)+> FromLuaStackMulti<'a> for ($first, $($name,)+) {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len() -> usize {
|
|
|
|
$count
|
|
|
|
}
|
|
|
|
|
|
|
|
fn results_from_lua_stack(state: &'a State) -> crate::Result<Self> {
|
2024-01-26 20:43:26 +00:00
|
|
|
unsafe {
|
|
|
|
let ($( $name, )+ $first) = ( $( $name::from_lua_stack(state)?, )+ $first::from_lua_stack(state)? );
|
|
|
|
|
|
|
|
Ok( ($first, $( $name, )+) )
|
|
|
|
}
|
|
|
|
//Ok(unsafe { ( $( $name::from_lua_stack(state)?, )+ $first::from_lua_stack(state)? ) })
|
|
|
|
//Ok(unsafe { ( $first::from_lua_stack(state)?, $( $name::from_lua_stack(state)?, )+ ) })
|
2024-01-22 03:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_function_arg_tuple!( $count - 1, $( $name ),+ );
|
|
|
|
);
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
// implements PushToLuaStackMulti and FromLuaStackMulti for a tuple with a single element
|
2024-01-22 03:53:53 +00:00
|
|
|
( $count: expr, $only: tt ) => {
|
|
|
|
#[allow(non_snake_case)]
|
2024-01-26 20:43:26 +00:00
|
|
|
impl<'a, $only: PushToLuaStack<'a>> PushToLuaStackMulti<'a> for ($only,) {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:43:26 +00:00
|
|
|
fn push_args_to_lua_stack(&self, state: &'a State) -> crate::Result<()> {
|
2024-01-22 03:53:53 +00:00
|
|
|
let (a,) = self;
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
a.push_to_lua_stack(state)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 04:47:30 +00:00
|
|
|
impl<'a, $only: FromLuaStack<'a>> FromLuaStackMulti<'a> for ($only,) {
|
2024-01-22 03:53:53 +00:00
|
|
|
fn len() -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn results_from_lua_stack(state: &'a State) -> crate::Result<Self> {
|
|
|
|
Ok(unsafe { ( $only::from_lua_stack(state)?, ) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// hopefully 16 is more than enough
|
|
|
|
// if you have 16 function results, and need more than 16, you NEED help
|
|
|
|
impl_function_arg_tuple! { 16, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16 }
|