Skip to main content

luaur_bytecode/methods/
bytecode_builder_add_constant_table.rs

1use crate::enums::r#type::Type;
2use crate::records::bytecode_builder::BytecodeBuilder;
3use crate::records::constant::Constant;
4use crate::records::table_shape::TableShape;
5
6impl BytecodeBuilder {
7    pub fn add_constant_table(&mut self, shape: &TableShape) -> i32 {
8        if let Some(cache) = self.table_shape_map.find(shape) {
9            return *cache;
10        }
11
12        let id = self.constants.len() as u32;
13
14        const K_MAX_CONSTANT_COUNT: u32 = 0x007f_ffff;
15        if id >= K_MAX_CONSTANT_COUNT {
16            return -1;
17        }
18
19        let value = Constant {
20            r#type: Type::Type_Table,
21            value: crate::records::constant::ConstantValue {
22                // C++ `value.valueTable = uint32_t(tableShapes.size())`: valueTable
23                // indexes table_shapes, NOT constants. The previous `id` (= constants
24                // length) over-indexed table_shapes and panicked in write_function.
25                valueTable: self.table_shapes.len() as u32,
26            },
27        };
28
29        self.table_shape_map.try_insert(shape.clone(), id as i32);
30        self.table_shapes.push(shape.clone());
31        self.constants.push(value);
32
33        id as i32
34    }
35}