lvm-class 0.4.1

JVM class file parser and writer for the Lagertha VM project
Documentation
use crate::error::ClassFormatErr;
use num_enum::TryFromPrimitive;

/// Reference to a method, field, or interface method in the constant pool.
///
/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.2
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Reference {
    pub class_index: u16,
    pub name_and_type_index: u16,
}

impl Reference {
    pub(crate) fn new(class_index: u16, name_and_type_index: u16) -> Self {
        Self {
            class_index,
            name_and_type_index,
        }
    }
}

/// Name and type descriptor pair in the constant pool.
///
/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.6
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NameAndType {
    pub name_index: u16,
    pub descriptor_index: u16,
}

impl NameAndType {
    pub(crate) fn new(name_index: u16, descriptor_index: u16) -> Self {
        Self {
            name_index,
            descriptor_index,
        }
    }
}

/// Dynamic constant or invoke dynamic info.
///
/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.10
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dynamic {
    pub bootstrap_method_attr_index: u16,
    pub name_and_type_index: u16,
}

impl Dynamic {
    pub(crate) fn new(bootstrap_method_attr_index: u16, name_and_type_index: u16) -> Self {
        Self {
            bootstrap_method_attr_index,
            name_and_type_index,
        }
    }
}

/// Method handle reference in the constant pool.
///
/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.8
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MethodHandle {
    pub reference_kind: u8,
    pub reference_index: u16,
}

impl MethodHandle {
    pub(crate) fn new(reference_kind: u8, reference_index: u16) -> Self {
        Self {
            reference_kind,
            reference_index,
        }
    }

    pub fn get_kind(&self) -> Result<MethodHandleKind, ClassFormatErr> {
        MethodHandleKind::try_from_primitive(self.reference_kind)
            .map_err(|_| ClassFormatErr::InvalidMethodHandleKind(self.reference_kind))
    }
}

/// Kind of method handle reference.
///
/// https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.4.8
#[derive(Debug, Clone, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)]
pub enum MethodHandleKind {
    GetField = 1,
    GetStatic = 2,
    PutField = 3,
    PutStatic = 4,
    InvokeVirtual = 5,
    InvokeStatic = 6,
    InvokeSpecial = 7,
    NewInvokeSpecial = 8,
    InvokeInterface = 9,
}