lua_bytecode/
lib.rs

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