Skip to main content

luaur_bytecode/methods/
bytecode_builder_dump_source_remarks.rs

1use crate::records::bytecode_builder::BytecodeBuilder;
2use luaur_common::functions::format_append::formatAppend;
3
4impl BytecodeBuilder {
5    pub fn dump_source_remarks(&self) -> alloc::string::String {
6        let mut result = alloc::string::String::new();
7        let mut next_remark: usize = 0;
8
9        let mut remarks: Vec<(i32, alloc::string::String)> = self.dump_remarks.clone();
10        // C++ `std::sort(remarks)` orders by the WHOLE (line, message) pair, so within a
11        // line remarks are ordered lexicographically by text ("builtin ..." before
12        // "inlining ...") and the consecutive-duplicate skip can collapse repeated inline
13        // remarks. Sorting by line only left them in insertion order.
14        remarks.sort();
15
16        for i in 0..self.dump_source.len() {
17            let line: &alloc::string::String = &self.dump_source[i];
18
19            let mut indent: usize = 0;
20            let line_len = line.len();
21            while indent < line_len
22                && (line.as_bytes()[indent] == b' ' || line.as_bytes()[indent] == b'\t')
23            {
24                indent += 1;
25            }
26
27            while next_remark < remarks.len() && remarks[next_remark].0 == (i as i32 + 1) {
28                formatAppend(
29                    &mut result,
30                    format_args!("{:.*}-- remark: {}\n", indent, line, remarks[next_remark].1),
31                );
32                next_remark += 1;
33
34                // skip duplicate remarks (due to inlining/unrolling)
35                while next_remark < remarks.len()
36                    && remarks[next_remark] == remarks[next_remark - 1]
37                {
38                    next_remark += 1;
39                }
40            }
41
42            result.push_str(line);
43            if i + 1 < self.dump_source.len() {
44                result.push('\n');
45            }
46        }
47
48        result
49    }
50}