Skip to main content

luaur_bytecode/methods/
bytecode_builder_add_constant.rs

1use crate::records::bytecode_builder::BytecodeBuilder;
2use crate::records::constant::Constant;
3use crate::records::constant_key::ConstantKey;
4
5impl BytecodeBuilder {
6    pub fn add_constant(&mut self, key: ConstantKey, value: Constant) -> i32 {
7        if let Some(cache) = self.constant_map.find(&key) {
8            return *cache;
9        }
10
11        let id = self.constants.len() as u32;
12
13        const K_MAX_CONSTANT_COUNT: u32 = 0x007f_ffff;
14        if id >= K_MAX_CONSTANT_COUNT {
15            return -1;
16        }
17
18        self.constant_map.try_insert(key, id as i32);
19        self.constants.push(value);
20
21        id as i32
22    }
23}