Skip to main content

luaur_bytecode/methods/
bytecode_builder_add_constant_number.rs

1use crate::enums::r#type::Type;
2use crate::records::bytecode_builder::BytecodeBuilder;
3use crate::records::constant::Constant;
4use crate::records::constant_key::ConstantKey;
5
6impl BytecodeBuilder {
7    pub fn add_constant_number(&mut self, value: f64) -> i32 {
8        let mut c = Constant {
9            r#type: Type::Type_Number,
10            value: unsafe { core::mem::zeroed() },
11        };
12        // The field name in the translated ConstantValue union is valueNumber, matching the C++ union field name.
13        c.value.valueNumber = value;
14
15        let mut k = ConstantKey {
16            r#type: Type::Type_Number,
17            value: 0,
18            extra: 0,
19        };
20
21        // Expecting double to be 64-bit
22        unsafe {
23            core::ptr::copy_nonoverlapping(
24                &value as *const f64 as *const u8,
25                &mut k.value as *mut u64 as *mut u8,
26                core::mem::size_of::<f64>(),
27            );
28        }
29
30        self.add_constant(k, c)
31    }
32}