1#![allow(dead_code)]
2
3use constant::Constant;
4use opcode::OpCode;
5
6mod buffer;
7pub mod constant;
8pub mod opcode;
9
10#[cfg(feature = "lua51")]
11pub mod lua51;
12#[cfg(feature = "luau")]
13pub mod luau;
14
15#[cfg(feature = "lua51")]
16pub const LUA_MAGIC: u32 = 0x61754c1b;
17
18enum Format {
19 Lua51,
20 Lua52,
21 Lua53,
22 Lua54,
23 LuaJit,
24 Luau,
25}
26
27type RawLuaString = Vec<u8>;
28
29#[cfg(feature = "lua51")]
30#[derive(Default)]
31pub struct Header {
32 pub version: u8,
33 pub format: u8,
34
35 pub is_big_endian: bool,
36
37 pub int_size: u8,
38 pub size_t_size: u8,
39 pub instruction_size: u8,
40 pub number_size: u8,
41
42 pub is_number_integral: bool,
43 pub luajit_flags: u8,
44}
45
46#[cfg(feature = "lua51")]
47#[derive(Default)]
48pub struct Bytecode {
49 pub header: Header,
50 pub protos: Vec<Proto>,
51 pub main_proto_id: u32,
52}
53
54pub struct LocalVariable {
55 name: RawLuaString,
56 start_pc: u32,
57 end_pc: u32,
58
59 #[cfg(feature = "luau")]
60 register: u8,
61}
62
63pub struct Instruction(pub u32);
64
65#[cfg(feature = "lua51")]
66pub trait LuaInstruction {
67 fn opcode(&self) -> OpCode;
68}
69
70#[cfg(feature = "lua51")]
71impl LuaInstruction for Instruction {
72 fn opcode(&self) -> OpCode {
73 let op = (self.0 & 0xff) as u8;
74 OpCode::LuaOpcode(opcode::LuaOpcode::index(op))
75 }
76}
77
78#[cfg(feature = "luau")]
79pub trait LuauInstruction {
80 fn opcode(&self) -> OpCode;
81}
82
83#[cfg(feature = "luau")]
84impl LuauInstruction for Instruction {
85 fn opcode(&self) -> OpCode {
86 let op = (self.0 & 0xff) as u8;
87 OpCode::LuauOpcode(opcode::LuauOpcode::index(op))
88 }
89}
90
91impl Instruction {
92 fn from_bytes(bytes: &[u8]) -> Self {
93 Instruction(u32::from_le_bytes(bytes.try_into().unwrap()))
94 }
95}
96
97#[derive(Default)]
98pub struct Proto {
99 #[cfg(feature = "luau")]
100 pub bytecode_id: u32,
101
102 pub max_stack_size: u8,
103 pub parameter_count: u8,
104 pub upvalue_count: u8,
105 pub is_vararg: bool,
106
107 pub flags: u8,
108 pub type_info: Vec<u8>,
109
110 pub line_defined: u32,
111 pub last_line_defined: u32,
112
113 pub name: Option<RawLuaString>,
114 pub line_info: Vec<u32>,
115 pub absolute_line_info: Vec<i32>,
116 pub linegaplog2: u8,
117
118 pub protos: Vec<u32>,
119 pub locals: Vec<LocalVariable>,
120 pub upvalues: Vec<RawLuaString>,
121 pub constants: Vec<Constant>,
122 pub instructions: Vec<Instruction>,
123}