ConstantValue initialization

This commit is contained in:
VegOwOtenks 2024-09-02 19:28:22 +02:00
parent 4ee673d5ff
commit 25d3509ccf
4 changed files with 167 additions and 69 deletions

View file

@ -1,12 +1,11 @@
use std::io::Read;
use std::error::Error as ErrorTrait;
use core::fmt::{Display, Formatter, Debug};
use core::mem::{ discriminant, Discriminant };
use core::str::Utf8Error;
use crate::accessmasks::*;
use crate::bytecode::Bytecode;
use crate::constantpool::{ ConstantPoolInfo, ConstantUtf8Info, ConstantMethodRefInfo, ConstantClassInfo, ConstantNameAndTypeInfo };
use crate::constantpool::{ ConstantPoolInfo, ConstantUtf8Info, ConstantMethodRefInfo, ConstantClassInfo, ConstantNameAndTypeInfo, ConstantIntegerInfo };
#[derive(Debug)]
pub enum Error {
@ -164,14 +163,14 @@ impl JavaClassFile {
}
pub fn get_classname(&self) -> Result<String, Error> {
let class_info_entry = pool_entry(&self.constant_pool, self.this_class as usize)?;
let class_info_entry = self.pool_entry(self.this_class)?;
let class_info_entry = match class_info_entry {
ConstantPoolInfo::Class(data) => data,
_ => return Err(Error::BadFileError(format!("Invalid this_class index, expected index to ClassInfo but found {:?}", class_info_entry)))
};
let name_entry = pool_entry(&self.constant_pool, class_info_entry.name_index.into())?;
let name_entry = self.pool_entry(class_info_entry.name_index.into())?;
let name_entry = match name_entry {
ConstantPoolInfo::Utf8(utf8data) => utf8data,
_ => return Err(Error::BadFileError(format!("Invalid class_info.name_index from this_class, expected index to Utf8 but found {:?}", name_entry)))
@ -192,28 +191,12 @@ impl JavaClassFile {
return None;
}
pub fn typed_pool_entry(&self, index: u16, variant: Discriminant<ConstantPoolInfo>) -> Result<&ConstantPoolInfo, Error> {
let pool_entry = &self.constant_pool[(index - 1) as usize];
if discriminant(pool_entry) != variant {
return Err(Error::BadFileError(format!("Expected constant pool entry {} in class {} to be of type {:#?} but found {:#?}", index, self.get_classname()?, variant, discriminant(pool_entry))));
}
return Ok(pool_entry);
fn pool_entry(&self, index: u16) -> Result<&ConstantPoolInfo, Error> {
return pool_entry(&self.constant_pool, index);
}
pub fn pool_methodref_entry(&self, index: u16) -> Result<&ConstantMethodRefInfo, Error> {
let pool_entry = self.typed_pool_entry(
index,
discriminant(
&ConstantPoolInfo::MethodRef(
ConstantMethodRefInfo {
class_index: 0,
name_and_type_index: 0
}
)
)
)?;
let pool_entry = self.pool_entry(index)?;
let methodref_entry = match pool_entry {
ConstantPoolInfo::MethodRef(data) => data,
@ -224,29 +207,38 @@ impl JavaClassFile {
}
pub fn pool_class_entry(&self, index: u16) -> Result<&ConstantClassInfo, Error> {
let pool_entry = self.typed_pool_entry(index, discriminant(&ConstantPoolInfo::Class(ConstantClassInfo {name_index: 0})))?;
let pool_entry = self.pool_entry(index)?;
return match pool_entry {
ConstantPoolInfo::Class(data) => Ok(data),
_ => unreachable!(),
_ => Err(Error::BadFileError(format!("Expected constant pool entry {} in class {} to be of type Class but found {:?}", index, self.get_classname()?, pool_entry)))
};
}
pub fn pool_utf8_entry(&self, index: u16) -> Result<&ConstantUtf8Info, Error> {
let pool_entry = self.typed_pool_entry(index, discriminant(&ConstantPoolInfo::Utf8(ConstantUtf8Info {utf8: "".to_string()})))?;
let pool_entry = self.pool_entry(index)?;
return match pool_entry {
ConstantPoolInfo::Utf8(data) => Ok(data),
_ => unreachable!(),
_ => Err(Error::BadFileError(format!("Expected constant pool entry {} in class {} to be of type Utf8 but found {:?}", index, self.get_classname()?, pool_entry)))
};
}
pub fn pool_int_entry(&self, index: u16) -> Result<&ConstantIntegerInfo, Error> {
let pool_entry = self.pool_entry(index)?;
return match pool_entry {
ConstantPoolInfo::Integer(data) => Ok(data),
_ => Err(Error::BadFileError(format!("Expected constant pool entry {} in class {} to be of type Integer but found {:?}", index, self.get_classname()?, pool_entry)))
};
}
pub fn pool_nameandtype_entry(&self, index: u16) -> Result<&ConstantNameAndTypeInfo, Error> {
let pool_entry = self.typed_pool_entry(index, discriminant(&ConstantPoolInfo::NameAndType(ConstantNameAndTypeInfo {name_index: 0, descriptor_index: 0})))?;
let pool_entry = self.pool_entry(index)?;
return match pool_entry {
ConstantPoolInfo::NameAndType(data) => Ok(data),
_ => unreachable!(),
_ => Err(Error::BadFileError(format!("Expected constant pool entry {} in class {} to be of type NameAndType but found {:?}", index, self.get_classname()?, pool_entry)))
};
}
@ -319,7 +311,7 @@ impl FieldInfo {
#[derive(Debug)]
pub struct ConstantValueAttributeData {
constant_value_index: u16,
pub constant_value_index: u16,
}
#[derive(Debug)]
@ -970,13 +962,13 @@ pub fn read_u8(reader: &mut dyn Read) -> Result<u8, std::io::Error> {
return Ok(u8::from_be_bytes(u8_buffer));
}
fn pool_entry<'a>(pool: &Box<[ConstantPoolInfo]>, index: usize) -> Result<&ConstantPoolInfo, Error> {
fn pool_entry(constant_pool: &Box<[ConstantPoolInfo]>, index: u16) -> Result<&ConstantPoolInfo, Error> {
if index == 0 {
return Err(Error::BadFileError(format!("Bad pool index: 0")));
}
if index - 1 >= pool.len() {
if usize::from(index - 1) >= constant_pool.len() {
return Err(Error::BadFileError(format!("Bad pool index: {}", index - 1)));
}
return Ok(&pool[index - 1]);
return Ok(&constant_pool[usize::from(index - 1)]);
}