1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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;

/// Trait for converting arguments into values.
pub trait IntoArgs {
    /// Encode arguments to the vm.
    ///
    /// # Safety
    ///
    /// This has the ability to encode references into the virtual machine.
    /// The caller must ensure that the virtual machine is cleared with
    /// [clear][Vm::clear] before the references are no longer valid.
    unsafe fn into_args(self, vm: &mut Vm) -> Result<(), VmError>;

    /// The number of arguments.
    fn count() -> usize;
}

/// Trait for converting types into values.
pub trait ReflectValueType: Sized {
    /// Convert into a value type.
    fn value_type() -> ValueType;

    /// Access diagnostical information on the value type.
    fn value_type_info() -> ValueTypeInfo;
}

/// Trait for converting types into values.
pub trait ToValue: Sized {
    /// Convert into a value.
    fn to_value(self, vm: &mut Vm) -> Result<ValuePtr, VmError>;
}

/// Trait for unsafe conversion of value types into values.
pub trait UnsafeToValue {
    /// Convert into a value.
    unsafe fn unsafe_to_value(self, vm: &mut Vm) -> Result<ValuePtr, VmError>;
}

/// Trait for converting from a value.
pub trait FromValue: Sized {
    /// Try to convert to the given type, from the given value.
    fn from_value(value: ValuePtr, vm: &mut Vm) -> Result<Self, VmError>;
}

/// A potentially unsafe conversion for value conversion.
pub trait UnsafeFromValue: Sized {
    /// The output type from the unsafe coercion.
    type Output: 'static;

    /// The raw guard returned.
    ///
    /// Must only be dropped *after* the value returned from this function is
    /// no longer live.
    type Guard: 'static;

    /// Convert the given reference using unsafe assumptions to a value.
    ///
    /// # Safety
    ///
    /// The return value of this function may only be used while a virtual
    /// machine is not being modified.
    ///
    /// You must also make sure that the returned value does not outlive the
    /// guard.
    unsafe fn unsafe_from_value(
        value: ValuePtr,
        vm: &mut Vm,
    ) -> Result<(Self::Output, Self::Guard), VmError>;

    /// Coerce the output of an unsafe from value into the final output type.
    ///
    /// # Safety
    ///
    /// The return value of this function may only be used while a virtual
    /// machine is not being modified.
    ///
    /// You must also make sure that the returned value does not outlive the
    /// guard.
    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},
);