Skip to main content

luaur_bytecode/methods/
bytecode_builder_add_string_table_entry.rs

1use crate::enums::dump_flags::DumpFlags;
2use crate::records::bytecode_builder::BytecodeBuilder;
3use crate::records::string_ref::StringRef;
4
5impl BytecodeBuilder {
6    pub fn add_string_table_entry(&mut self, value: StringRef) -> u32 {
7        if let Some(idx) = self.string_table.find(&value) {
8            return *idx;
9        }
10
11        // Bytecode serialization uses 1-based string-table indices (0 is reserved
12        // to mean "no string"). C++ computes the index as `stringTable.size()`
13        // *after* inserting the new entry via `operator[]`, i.e. pre-insert size+1.
14        // Computing it pre-insert made the first string index 0, so
15        // `debugStrings[valueString - 1]` underflowed in `dumpConstant`.
16        let new_index = self.string_table.size() as u32 + 1;
17        self.string_table.try_insert(value, new_index);
18
19        if (self.dump_flags & DumpFlags::Dump_Code as u32) != 0 {
20            self.debug_strings.push(value);
21        }
22
23        new_index
24    }
25}