Skip to main content

luaur_compiler/functions/
constants_equal.rs

1use crate::enums::type_constant_folding::Type;
2use crate::records::constant::Constant;
3use luaur_common::macros::luau_assert::LUAU_ASSERT;
4use luaur_common::FFlag::LuauCompilePropagateTableProps2;
5use luaur_common::FFlag::LuauIntegerType2;
6
7pub fn constants_equal(la: &Constant, ra: &Constant) -> bool {
8    LUAU_ASSERT!(la.r#type != Type::Type_Unknown && ra.r#type != Type::Type_Unknown);
9
10    match la.r#type {
11        Type::Type_Nil => ra.r#type == Type::Type_Nil,
12        Type::Type_Boolean => {
13            ra.r#type == Type::Type_Boolean
14                && unsafe { la.data.value_boolean == ra.data.value_boolean }
15        }
16        Type::Type_Number => {
17            ra.r#type == Type::Type_Number
18                && unsafe { la.data.value_number == ra.data.value_number }
19        }
20        Type::Type_Vector => {
21            ra.r#type == Type::Type_Vector
22                && unsafe { la.data.value_vector[0] == ra.data.value_vector[0] }
23                && unsafe { la.data.value_vector[1] == ra.data.value_vector[1] }
24                && unsafe { la.data.value_vector[2] == ra.data.value_vector[2] }
25                && unsafe { la.data.value_vector[3] == ra.data.value_vector[3] }
26        }
27        Type::Type_String => {
28            ra.r#type == Type::Type_String
29                && la.string_length == ra.string_length
30                && unsafe {
31                    std::slice::from_raw_parts(
32                        la.data.value_string as *const u8,
33                        la.string_length as usize,
34                    ) == std::slice::from_raw_parts(
35                        ra.data.value_string as *const u8,
36                        ra.string_length as usize,
37                    )
38                }
39        }
40        Type::Type_Table => {
41            if LuauCompilePropagateTableProps2.get() {
42                ra.r#type == Type::Type_Table
43                    && unsafe { la.data.value_table == ra.data.value_table }
44            } else {
45                LUAU_ASSERT!(false);
46                false
47            }
48        }
49        Type::Type_Integer => {
50            if LuauIntegerType2.get() {
51                ra.r#type == Type::Type_Integer
52                    && unsafe { la.data.value_integer64 == ra.data.value_integer64 }
53            } else {
54                LUAU_ASSERT!(false);
55                false
56            }
57        }
58        _ => {
59            LUAU_ASSERT!(false);
60            false
61        }
62    }
63}