use crate::runtime::heap::Gc;
use crate::runtime::value::Value;
use crate::runtime::{LuaClosure, NativeClosure, Table};
use crate::vm::exec::Vm;
pub trait IntoValue {
fn into_value(self, vm: &mut Vm) -> Value;
}
impl IntoValue for Value {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
self
}
}
impl IntoValue for () {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Nil
}
}
impl IntoValue for i64 {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Int(self)
}
}
macro_rules! impl_into_value_int {
($($t:ty),+ $(,)?) => {
$(
impl IntoValue for $t {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Int(self as i64)
}
}
)+
};
}
impl_into_value_int!(i32, i16, i8, u32, u16, u8);
impl IntoValue for f64 {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Float(self)
}
}
impl IntoValue for f32 {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Float(self as f64)
}
}
impl IntoValue for bool {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Bool(self)
}
}
impl IntoValue for &str {
#[inline]
fn into_value(self, vm: &mut Vm) -> Value {
Value::Str(vm.heap.intern(self.as_bytes()))
}
}
impl IntoValue for String {
#[inline]
fn into_value(self, vm: &mut Vm) -> Value {
Value::Str(vm.heap.intern(self.as_bytes()))
}
}
impl IntoValue for &[u8] {
#[inline]
fn into_value(self, vm: &mut Vm) -> Value {
Value::Str(vm.heap.intern(self))
}
}
impl IntoValue for Vec<u8> {
#[inline]
fn into_value(self, vm: &mut Vm) -> Value {
Value::Str(vm.heap.intern(&self))
}
}
impl IntoValue for Gc<Table> {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Table(self)
}
}
impl IntoValue for Gc<LuaClosure> {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Closure(self)
}
}
impl IntoValue for Gc<NativeClosure> {
#[inline]
fn into_value(self, _vm: &mut Vm) -> Value {
Value::Native(self)
}
}
impl<T: IntoValue> IntoValue for Option<T> {
#[inline]
fn into_value(self, vm: &mut Vm) -> Value {
match self {
Some(v) => v.into_value(vm),
None => Value::Nil,
}
}
}