Skip to main content

luaur_bytecode/records/
comp_time_bytecode_graph_serializer.rs

1use crate::records::bc_inst::BcInst;
2use crate::records::bc_vm_const::BcVmConst;
3use crate::records::bytecode_builder::BytecodeBuilder;
4use crate::records::bytecode_graph_serializer::BytecodeGraphSerializer;
5use crate::type_aliases::comp_time_bc_function::CompTimeBcFunction;
6use alloc::vec::Vec;
7
8#[derive(Debug)]
9pub struct CompTimeBytecodeGraphSerializer {
10    pub(crate) base: BytecodeGraphSerializer<'static>,
11    pub(crate) consts: Vec<u16>,
12}
13
14impl CompTimeBytecodeGraphSerializer {
15    pub fn comp_time_bytecode_graph_serializer_comp_time_bytecode_graph_serializer(
16        bcb: &mut BytecodeBuilder,
17        fn_: &mut CompTimeBcFunction,
18        consts: &mut Vec<u16>,
19    ) -> Self {
20        // BytecodeGraphSerializer is parameterized by the lifetimes of `bcb`/`fn_`.
21        // This record requires `base` to be stored as `'static`, so we extend the
22        // lifetimes via raw pointers (matching the original C++ serializer lifetime assumptions).
23        let bcb_ptr: *mut BytecodeBuilder = bcb;
24        let fn_ptr: *mut CompTimeBcFunction = fn_;
25
26        let mut base =
27            BytecodeGraphSerializer::new(unsafe { &mut *bcb_ptr }, unsafe { &mut *fn_ptr });
28        base.consts = Some(core::mem::take(consts));
29
30        Self {
31            base,
32            consts: Vec::new(),
33        }
34    }
35
36    pub fn get_vm_const_input_raw(&mut self, insn: &mut BcInst, index: u8) -> u32 {
37        let cid = BytecodeGraphSerializer::get_vm_const_input_raw(&mut self.base, insn, index);
38        assert!((cid as usize) < self.consts.len());
39        self.consts[cid as usize] as u32
40    }
41
42    pub fn emit_bytecode(&mut self) -> Vec<u32> {
43        self.base.emit_bytecode()
44    }
45
46    pub fn error(&self) -> bool {
47        self.base.error
48    }
49}