use crate::any::Any;
use crate::value::{Value, ValuePtr, ValueType, ValueTypeInfo};
use crate::vm::{Vm, VmError};
mod array;
mod hash_map;
mod object;
mod option;
mod primitive;
mod string;
pub trait IntoArgs {
unsafe fn into_args(self, vm: &mut Vm) -> Result<(), VmError>;
fn count() -> usize;
}
pub trait ReflectValueType: Sized {
fn value_type() -> ValueType;
fn value_type_info() -> ValueTypeInfo;
}
pub trait ToValue: Sized {
fn to_value(self, vm: &mut Vm) -> Result<ValuePtr, VmError>;
}
pub trait UnsafeToValue {
unsafe fn unsafe_to_value(self, vm: &mut Vm) -> Result<ValuePtr, VmError>;
}
pub trait FromValue: Sized {
fn from_value(value: ValuePtr, vm: &mut Vm) -> Result<Self, VmError>;
}
pub trait UnsafeFromValue: Sized {
type Output: 'static;
type Guard: 'static;
unsafe fn unsafe_from_value(
value: ValuePtr,
vm: &mut Vm,
) -> Result<(Self::Output, Self::Guard), VmError>;
unsafe fn to_arg(output: Self::Output) -> Self;
}
impl<T> UnsafeFromValue for T
where
T: 'static + FromValue,
{
type Output = T;
type Guard = ();
unsafe fn unsafe_from_value(
value: ValuePtr,
vm: &mut Vm,
) -> Result<(Self, Self::Guard), VmError> {
Ok((T::from_value(value, vm)?, ()))
}
unsafe fn to_arg(output: Self::Output) -> Self {
output
}
}
impl<T> UnsafeToValue for T
where
T: ToValue,
{
unsafe fn unsafe_to_value(self, vm: &mut Vm) -> Result<ValuePtr, VmError> {
self.to_value(vm)
}
}
impl FromValue for ValuePtr {
fn from_value(value: ValuePtr, _: &mut Vm) -> Result<Self, VmError> {
Ok(value)
}
}
impl ToValue for ValuePtr {
fn to_value(self, _vm: &mut Vm) -> Result<ValuePtr, VmError> {
Ok(self)
}
}
impl FromValue for Value {
fn from_value(value: ValuePtr, vm: &mut Vm) -> Result<Self, VmError> {
vm.value_take(value)
}
}
impl FromValue for Any {
fn from_value(value: ValuePtr, vm: &mut Vm) -> Result<Self, VmError> {
let slot = value.into_external(vm)?;
vm.external_take_dyn(slot)
}
}
macro_rules! impl_into_args {
() => {
impl_into_args!{@impl 0,}
};
({$ty:ident, $var:ident, $count:expr}, $({$l_ty:ident, $l_var:ident, $l_count:expr},)*) => {
impl_into_args!{@impl $count, {$ty, $var, $count}, $({$l_ty, $l_var, $l_count},)*}
impl_into_args!{$({$l_ty, $l_var, $l_count},)*}
};
(@impl $count:expr, $({$ty:ident, $var:ident, $ignore_count:expr},)*) => {
impl<$($ty,)*> IntoArgs for ($($ty,)*)
where
$($ty: UnsafeToValue + std::fmt::Debug,)*
{
#[allow(unused)]
unsafe fn into_args(self, vm: &mut Vm) -> Result<(), VmError> {
let ($($var,)*) = self;
impl_into_args!(@push vm, [$($var)*]);
Ok(())
}
fn count() -> usize {
$count
}
}
};
(@push $vm:expr, [] $($var:ident)*) => {
$(
let $var = $var.unsafe_to_value($vm)?;
$vm.push($var);
)*
};
(@push $vm:expr, [$first:ident $($rest:ident)*] $($var:ident)*) => {
impl_into_args!(@push $vm, [$($rest)*] $first $($var)*)
};
}
impl_into_args!(
{H, h, 8},
{G, g, 7},
{F, f, 6},
{E, e, 5},
{D, d, 4},
{C, c, 3},
{B, b, 2},
{A, a, 1},
);
macro_rules! impl_from_value_tuple {
() => {
impl_from_value_tuple!{@impl 0,}
};
({$ty:ident, $var:ident, $count:expr}, $({$l_ty:ident, $l_var:ident, $l_count:expr},)*) => {
impl_from_value_tuple!{@impl $count, {$ty, $var, $count}, $({$l_ty, $l_var, $l_count},)*}
impl_from_value_tuple!{$({$l_ty, $l_var, $l_count},)*}
};
(@impl $count:expr, $({$ty:ident, $var:ident, $ignore_count:expr},)*) => {
impl<$($ty,)*> FromValue for ($($ty,)*)
where
$($ty: FromValue,)*
{
#[allow(unused)]
fn from_value(value: ValuePtr, vm: &mut Vm) -> Result<Self, VmError> {
let array = match value {
ValuePtr::Array(slot) => Clone::clone(&*vm.array_ref(slot)?),
actual => {
let actual = actual.type_info(vm)?;
return Err(VmError::ExpectedArray {
actual,
});
}
};
if array.len() != $count {
return Err(VmError::ExpectedArrayLength {
actual: array.len(),
expected: $count,
});
}
#[allow(unused_mut, unused_variables)]
let mut it = array.iter();
$(
let $var: $ty = match it.next() {
Some(value) => <$ty>::from_value(*value, vm)?,
None => {
return Err(VmError::IterationError);
},
};
)*
Ok(($($var,)*))
}
}
};
}
impl_from_value_tuple!(
{H, h, 8},
{G, g, 7},
{F, f, 6},
{E, e, 5},
{D, d, 4},
{C, c, 3},
{B, b, 2},
{A, a, 1},
);