lua_kit/lib.rs
1//! Toolkit for working with serialized Lua functions and bytecode.
2//!
3//! Synced to Lua 5.3.
4
5extern crate byteorder;
6extern crate libc;
7
8pub mod bytecode;
9mod write;
10mod read;
11
12pub use write::write_file;
13pub use read::read_file;
14
15/// Signature to mark Lua bytecode files.
16pub const SIGNATURE: &'static [u8] = b"\x1bLua";
17/// The Lua version, in the form `(MAJOR << 4) | MINOR`.
18pub const VERSION: u8 = 0x53;
19/// The Lua bytecode format.
20pub const FORMAT: u8 = 0;
21/// Test text to catch translation errors.
22pub const DATA: &'static [u8] = b"\x19\x93\r\n\x1a\n";
23/// A test integer to know endianness.
24pub const TEST_INT: Integer = 0x5678;
25/// A test floating-point number to know endianness.
26pub const TEST_NUMBER: Number = 370.5;
27
28/// The bytecode's C `int` type.
29pub type Int = libc::c_int;
30/// The bytecodes' C `size_t` type.
31pub type Size = libc::size_t;
32/// The bytecode's `Instruction` type.
33pub type Instruction = u32;
34/// The bytecode's `Integer` type.
35pub type Integer = i64;
36/// The bytecode's `Number` (floating-point) type.
37pub type Number = f64;
38
39/// An entry in the constant pool.
40#[derive(Clone, Debug, PartialEq)]
41pub enum Constant {
42 /// The value `nil`.
43 Nil,
44 /// A boolean.
45 Boolean(bool),
46 /// A floating-point number.
47 Float(Number),
48 /// An integer.
49 Int(Integer),
50 /// A short string.
51 ShortString(String),
52 /// A long string. Behaves the same as `ShortString`.
53 LongString(String),
54}
55
56/// An entry in the upvalue table.
57#[derive(Clone, Copy, Debug, PartialEq)]
58pub enum Upvalue {
59 /// An upvalue inherited from the outer function's upvalues.
60 Outer(u8),
61 /// An upvalue in the outer function's registers.
62 Stack(u8),
63}
64
65/// An entry in the local variable debug table.
66#[derive(Clone, Debug, PartialEq)]
67pub struct LocalVar {
68 /// The local variable's name.
69 pub name: String,
70 /// The instruction at which the local variable is introduced.
71 pub start_pc: Int,
72 /// The instruction at which the local variable goes out of scope.
73 pub end_pc: Int,
74}
75
76/// Optional debugging information for a function.
77#[derive(Clone, Debug, PartialEq)]
78pub struct Debug {
79 /// The line number of each bytecode instruction.
80 pub lineinfo: Vec<Int>,
81 /// The names and scopes of local variables.
82 pub localvars: Vec<LocalVar>,
83 /// The names of upvalues.
84 pub upvalues: Vec<String>,
85}
86
87impl Debug {
88 /// A new, empty debug info.
89 pub fn none() -> Debug {
90 Debug {
91 lineinfo: vec![],
92 localvars: vec![],
93 upvalues: vec![],
94 }
95 }
96}
97
98/// A Lua function prototype.
99#[derive(Clone, Debug, PartialEq)]
100pub struct Function {
101 /// The source filename of the function. May be empty.
102 pub source: String,
103 /// The start line number of the function.
104 pub line_start: Int,
105 /// The end line number of the function.
106 pub line_end: Int,
107 /// The number of fixed parameters the function takes.
108 pub num_params: u8,
109 /// Whether the function accepts a variable number of arguments.
110 pub is_vararg: bool,
111 /// The number of registers needed by the function.
112 pub max_stack_size: u8,
113 /// The function's code.
114 pub code: Vec<Instruction>,
115 /// The function's constant table.
116 pub constants: Vec<Constant>,
117 /// The upvalue information of the function.
118 pub upvalues: Vec<Upvalue>,
119 /// The function's contained function prototypes.
120 pub protos: Vec<Function>,
121 /// Debugging information for the function.
122 pub debug: Debug,
123}