luna_core/vm/
into_value.rs1use crate::runtime::heap::Gc;
29use crate::runtime::value::Value;
30use crate::runtime::{LuaClosure, NativeClosure, Table};
31use crate::vm::exec::Vm;
32
33pub trait IntoValue {
36 fn into_value(self, vm: &mut Vm) -> Value;
39}
40
41impl IntoValue for Value {
43 #[inline]
44 fn into_value(self, _vm: &mut Vm) -> Value {
45 self
46 }
47}
48
49impl IntoValue for () {
50 #[inline]
51 fn into_value(self, _vm: &mut Vm) -> Value {
52 Value::Nil
53 }
54}
55
56impl IntoValue for i64 {
58 #[inline]
59 fn into_value(self, _vm: &mut Vm) -> Value {
60 Value::Int(self)
61 }
62}
63
64macro_rules! impl_into_value_int {
65 ($($t:ty),+ $(,)?) => {
66 $(
67 impl IntoValue for $t {
68 #[inline]
69 fn into_value(self, _vm: &mut Vm) -> Value {
70 Value::Int(self as i64)
71 }
72 }
73 )+
74 };
75}
76impl_into_value_int!(i32, i16, i8, u32, u16, u8);
77
78impl IntoValue for f64 {
80 #[inline]
81 fn into_value(self, _vm: &mut Vm) -> Value {
82 Value::Float(self)
83 }
84}
85impl IntoValue for f32 {
86 #[inline]
87 fn into_value(self, _vm: &mut Vm) -> Value {
88 Value::Float(self as f64)
89 }
90}
91
92impl IntoValue for bool {
94 #[inline]
95 fn into_value(self, _vm: &mut Vm) -> Value {
96 Value::Bool(self)
97 }
98}
99
100impl IntoValue for &str {
102 #[inline]
103 fn into_value(self, vm: &mut Vm) -> Value {
104 Value::Str(vm.heap.intern(self.as_bytes()))
105 }
106}
107impl IntoValue for String {
108 #[inline]
109 fn into_value(self, vm: &mut Vm) -> Value {
110 Value::Str(vm.heap.intern(self.as_bytes()))
111 }
112}
113impl IntoValue for &[u8] {
114 #[inline]
115 fn into_value(self, vm: &mut Vm) -> Value {
116 Value::Str(vm.heap.intern(self))
117 }
118}
119impl IntoValue for Vec<u8> {
120 #[inline]
121 fn into_value(self, vm: &mut Vm) -> Value {
122 Value::Str(vm.heap.intern(&self))
123 }
124}
125
126impl IntoValue for Gc<Table> {
128 #[inline]
129 fn into_value(self, _vm: &mut Vm) -> Value {
130 Value::Table(self)
131 }
132}
133impl IntoValue for Gc<LuaClosure> {
134 #[inline]
135 fn into_value(self, _vm: &mut Vm) -> Value {
136 Value::Closure(self)
137 }
138}
139impl IntoValue for Gc<NativeClosure> {
140 #[inline]
141 fn into_value(self, _vm: &mut Vm) -> Value {
142 Value::Native(self)
143 }
144}
145
146impl<T: IntoValue> IntoValue for Option<T> {
148 #[inline]
149 fn into_value(self, vm: &mut Vm) -> Value {
150 match self {
151 Some(v) => v.into_value(vm),
152 None => Value::Nil,
153 }
154 }
155}