lua_bytecode/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#![allow(dead_code)]

mod buffer;

#[cfg(feature = "lua51")]
pub mod lua51;
#[cfg(feature = "luau")]
pub mod luau;

#[cfg(feature = "lua51")]
pub const LUA_MAGIC: u32 = 0x61754c1b;

enum Format {
    Lua51,
    Lua52,
    Lua53,
    Lua54,
    LuaJit,
    Luau,
}

#[cfg(feature = "lua51")]
#[derive(Default)]
struct Header {
    pub version: u8,
    pub format: u8,

    pub is_big_endian: bool,

    pub int_size: u8,
    pub size_t_size: u8,
    pub instruction_size: u8,
    pub number_size: u8,

    pub is_number_integral: bool,
    pub luajit_flags: u8,
}

#[cfg(feature = "lua51")]
#[derive(Default)]
pub struct Bytecode {
    pub header: Header,
    pub protos: Vec<Proto>,
    pub main_proto_id: u32,
}

pub struct LocalVariable {
    name: String,
    start_pc: u32,
    end_pc: u32,

    #[cfg(feature = "luau")]
    register: u8,
}

#[cfg(feature = "lua51")]
const LUA_CONSTANT_NIL: u8 = 0;
#[cfg(feature = "lua51")]
const LUA_CONSTANT_BOOLEAN: u8 = 1;
#[cfg(feature = "lua51")]
const LUA_CONSTANT_NUMBER: u8 = 3;
#[cfg(feature = "lua51")]
const LUA_CONSTANT_STRING: u8 = 4;

pub struct Constant {
    kind: u8,
    value: Vec<u8>,
}

impl Constant {
    fn new() -> Self {
        Constant {
            kind: 0,
            value: Vec::new(),
        }
    }
}

pub struct Instruction(pub u32);

impl Instruction {
    fn from_bytes(bytes: &[u8]) -> Self {
        Instruction(u32::from_le_bytes(bytes.try_into().unwrap()))
    }
}

#[derive(Default)]
pub struct Proto {
    #[cfg(feature = "luau")]
    pub bytecode_id: u32,

    pub max_stack_size: u8,
    pub parameter_count: u8,
    pub upvalue_count: u8,
    pub is_vararg: bool,

    pub flags: u8,
    pub type_info: Vec<u8>,

    pub line_defined: u32,
    pub last_line_defined: u32,

    pub name: Option<String>,
    pub line_info: Vec<u32>,
    pub absolute_line_info: Vec<i32>,
    pub linegaplog2: u8,

    pub protos: Vec<u32>,
    pub locals: Vec<LocalVariable>,
    pub upvalues: Vec<String>,
    pub constants: Vec<Constant>,
    pub instructions: Vec<Instruction>,
}

impl Proto {
    fn new() -> Self {
        Default::default()
    }
}