luaur_bytecode/methods/
bytecode_builder_bytecode_builder.rs1use crate::records::bytecode_builder::{BytecodeBuilder, EncoderBox};
2use crate::records::bytecode_encoder::BytecodeEncoder;
3use crate::records::constant_key::ConstantKey;
4use crate::records::string_ref::StringRef;
5use crate::records::table_shape::TableShape;
6use alloc::boxed::Box;
7use alloc::string::String;
8use alloc::vec::Vec;
9use luaur_common::macros::luau_assert::LUAU_ASSERT;
10use luaur_common::records::dense_hash_map::DenseHashMap;
11
12impl BytecodeBuilder {
13 pub fn new() -> Self {
15 Self::new_impl(None)
16 }
17
18 pub fn with_encoder(encoder: Box<dyn BytecodeEncoder>) -> Self {
21 Self::new_impl(Some(EncoderBox(encoder)))
22 }
23
24 pub fn take_encoder(&mut self) -> Option<Box<dyn BytecodeEncoder>> {
26 self.encoder.take().map(|EncoderBox(encoder)| encoder)
27 }
28
29 fn new_impl(encoder: Option<EncoderBox>) -> Self {
30 let constant_map = DenseHashMap::new(ConstantKey {
31 r#type: crate::enums::r#type::Type::Type_Nil,
32 value: !0u64,
33 extra: 0,
34 });
35
36 let table_shape_map = DenseHashMap::new(TableShape::default());
37
38 let proto_map = DenseHashMap::new(!0u32);
39
40 let string_table = DenseHashMap::new(StringRef {
41 data: core::ptr::null(),
42 length: 0,
43 });
44
45 let mut result = BytecodeBuilder {
46 functions: Vec::new(),
47 current_function: !0u32,
48 main_function: !0u32,
49 total_instruction_count: 0,
50 insns: Vec::new(),
51 lines: Vec::new(),
52 constants: Vec::new(),
53 protos: Vec::new(),
54 jumps: Vec::new(),
55 table_shapes: Vec::new(),
56 class_shapes: Vec::new(),
57 fb_slots: Vec::new(),
58 has_long_jumps: false,
59 constant_map,
60 table_shape_map,
61 proto_map,
62 debug_line: 0,
63 debug_locals: Vec::new(),
64 debug_upvals: Vec::new(),
65 typed_locals: Vec::new(),
66 typed_upvals: Vec::new(),
67 userdata_types: Vec::new(),
68 string_table,
69 debug_strings: Vec::new(),
70 debug_remarks: Vec::new(),
71 debug_remark_buffer: String::new(),
72 encoder,
73 bytecode: String::new(),
74 dump_flags: 0,
75 dump_source: Vec::new(),
76 dump_remarks: Vec::new(),
77 temp_type_info: String::new(),
78 dump_function_ptr: None,
79 };
80
81 result.insns.reserve(32);
83 result.lines.reserve(32);
84 result.constants.reserve(16);
85 result.protos.reserve(16);
86 result.functions.reserve(8);
87
88 let empty_key = StringRef {
90 data: b"\0".as_ptr() as *const i8,
91 length: 0,
92 };
93 LUAU_ASSERT!(result.string_table.find(&empty_key).is_none());
94
95 result
96 }
97}