Skip to main content

luaur_compiler/records/
constant.rs

1use crate::enums::type_constant_folding::Type;
2
3#[repr(C)]
4#[derive(Clone, Copy)]
5pub struct Constant {
6    pub(crate) r#type: Type,
7    pub(crate) string_length: core::ffi::c_uint,
8    pub(crate) data: ConstantData,
9}
10
11#[repr(C)]
12#[derive(Clone, Copy)]
13pub union ConstantData {
14    pub(crate) value_boolean: bool,
15    pub(crate) value_number: f64,
16    pub(crate) value_integer64: i64,
17    pub(crate) value_vector: [f32; 4],
18    pub(crate) value_table: usize,
19    pub(crate) value_string: *const core::ffi::c_char,
20}
21
22impl luaur_common::records::dense_hash_table::DenseDefault for Constant {
23    fn dense_default() -> Self {
24        Self::default()
25    }
26}
27
28impl Default for Constant {
29    fn default() -> Self {
30        Self {
31            r#type: Type::Type_Unknown,
32            string_length: 0,
33            data: ConstantData {
34                value_string: core::ptr::null(),
35            },
36        }
37    }
38}
39
40impl Default for ConstantData {
41    fn default() -> Self {
42        Self {
43            value_string: core::ptr::null(),
44        }
45    }
46}
47
48// Manual Debug implementation for Constant because of the union field
49impl core::fmt::Debug for Constant {
50    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
51        let mut ds = f.debug_struct("Constant");
52        ds.field("type", &self.r#type);
53        ds.field("string_length", &self.string_length);
54        unsafe {
55            match self.r#type {
56                Type::Type_Unknown => ds.field("data", &"Unknown"),
57                Type::Type_Nil => ds.field("data", &"Nil"),
58                Type::Type_Boolean => ds.field("value_boolean", &self.data.value_boolean),
59                Type::Type_Number => ds.field("value_number", &self.data.value_number),
60                Type::Type_Integer => ds.field("value_integer64", &self.data.value_integer64),
61                Type::Type_Vector => ds.field("value_vector", &self.data.value_vector),
62                Type::Type_Table => ds.field("value_table", &self.data.value_table),
63                Type::Type_String => ds.field("value_string", &self.data.value_string),
64            };
65        }
66        ds.finish()
67    }
68}