use crate::decompile::DecompileDialect;
use crate::parser::StringEncoding;
use super::{
DialectConstPoolExtra, DialectDebugExtra, DialectHeaderExtra, DialectInstrExtra,
DialectProtoExtra, DialectUpvalueExtra, RawInstrOpcode, RawInstrOperands,
};
#[derive(Debug, Clone, PartialEq)]
pub struct RawChunk {
pub header: ChunkHeader,
pub main: RawProto,
pub origin: Origin,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ChunkHeader {
pub dialect: Dialect,
pub version: DecompileDialect,
pub layout: ChunkLayout,
pub extra: DialectHeaderExtra,
pub origin: Origin,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ChunkLayout {
PucLua(PucLuaChunkLayout),
LuaJit(LuaJitChunkLayout),
Luau(LuauChunkLayout),
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct PucLuaChunkLayout {
pub format: u8,
pub endianness: Endianness,
pub integer_size: u8,
pub lua_integer_size: Option<u8>,
pub size_t_size: u8,
pub instruction_size: u8,
pub number_size: u8,
pub integral_number: bool,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct LuauChunkLayout {
pub bytecode_version: u8,
pub type_version: Option<u8>,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct LuaJitChunkLayout {
pub dump_version: u8,
pub flags: u32,
}
impl ChunkHeader {
pub fn puc_lua_layout(&self) -> Option<&PucLuaChunkLayout> {
match &self.layout {
ChunkLayout::PucLua(layout) => Some(layout),
ChunkLayout::LuaJit(_) | ChunkLayout::Luau(_) => None,
}
}
pub fn luajit_layout(&self) -> Option<&LuaJitChunkLayout> {
match &self.layout {
ChunkLayout::LuaJit(layout) => Some(layout),
ChunkLayout::Luau(_) => None,
ChunkLayout::PucLua(_) => None,
}
}
pub fn luau_layout(&self) -> Option<&LuauChunkLayout> {
match &self.layout {
ChunkLayout::PucLua(_) | ChunkLayout::LuaJit(_) => None,
ChunkLayout::Luau(layout) => Some(layout),
}
}
pub(crate) fn luajit_fr2(&self) -> Option<bool> {
Some(self.extra.luajit()?.fr2)
}
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Dialect {
PucLua,
LuaJit,
Luau,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Endianness {
Little,
Big,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawProto {
pub common: RawProtoCommon,
pub extra: DialectProtoExtra,
pub origin: Origin,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawProtoCommon {
pub source: Option<RawString>,
pub line_range: ProtoLineRange,
pub signature: ProtoSignature,
pub frame: ProtoFrameInfo,
pub instructions: Vec<RawInstr>,
pub constants: RawConstPool,
pub upvalues: RawUpvalueInfo,
pub debug_info: RawDebugInfo,
pub children: Vec<RawProto>,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ProtoLineRange {
pub defined_start: u32,
pub defined_end: u32,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ProtoSignature {
pub num_params: u8,
pub is_vararg: bool,
pub has_vararg_param_reg: bool,
pub named_vararg_table: bool,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct ProtoFrameInfo {
pub max_stack_size: u8,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawConstPool {
pub common: RawConstPoolCommon,
pub extra: DialectConstPoolExtra,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawConstPoolCommon {
pub literals: Vec<RawLiteralConst>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum RawLiteralConst {
Nil,
Boolean(bool),
Integer(i64),
Number(f64),
String(RawString),
Int64(i64),
UInt64(u64),
Complex { real: f64, imag: f64 },
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawUpvalueInfo {
pub common: RawUpvalueInfoCommon,
pub extra: DialectUpvalueExtra,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawUpvalueInfoCommon {
pub count: u8,
pub descriptors: Vec<RawUpvalueDescriptor>,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct RawUpvalueDescriptor {
pub in_stack: bool,
pub index: u8,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawDebugInfo {
pub common: RawDebugInfoCommon,
pub extra: DialectDebugExtra,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawDebugInfoCommon {
pub line_info: Vec<u32>,
pub local_vars: Vec<RawLocalVar>,
pub upvalue_names: Vec<RawString>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawLocalVar {
pub name: RawString,
pub start_pc: u32,
pub end_pc: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawInstr {
pub opcode: RawInstrOpcode,
pub operands: RawInstrOperands,
pub extra: DialectInstrExtra,
pub origin: Origin,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Origin {
pub span: Span,
pub raw_word: Option<u64>,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub struct Span {
pub offset: usize,
pub size: usize,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RawString {
pub bytes: Vec<u8>,
pub text: Option<DecodedText>,
pub origin: Origin,
}
#[derive(Debug, Clone, PartialEq)]
pub struct DecodedText {
pub encoding: StringEncoding,
pub value: String,
}