Type correction and opcode implementations

This commit is contained in:
vegowotenks 2024-09-10 17:15:20 +02:00
parent c6e4ecec3d
commit b053461e74
5 changed files with 307 additions and 148 deletions

View file

@ -79,14 +79,14 @@ impl HeapArea {
let byte_array_ref = self.make_primitive_byte_array(utf16_bytes.len(), class_store);
for (index, byte) in utf16_bytes.iter().enumerate() {
self.object_area.set_array_element(byte_array_ref, index, FieldValue::Byte(*byte));
self.object_area.set_array_element(byte_array_ref, index, FieldValue::Byte(i8::from_ne_bytes([*byte])));
}
let string_class_index = class_store.class_idx_from_name(&String::from("java/lang/String")).unwrap();
let string_ref = self.make_object(class_store, string_class_index);
self.object_area.set_object_field(string_ref, "value", FieldValue::Reference(byte_array_ref), string_class_index, class_store).unwrap();
const UTF16_CODER: u8 = 1; // TODO: I don't like this
const UTF16_CODER: i8 = 1; // TODO: I don't like this
self.object_area.set_object_field(string_ref, "coder", FieldValue::Byte(UTF16_CODER), string_class_index, class_store).unwrap();
string_ref
@ -104,6 +104,12 @@ impl HeapArea {
pub struct ObjectReference(u32);
impl Into<FieldValue> for ObjectReference {
fn into(self) -> FieldValue {
FieldValue::Reference(self)
}
}
impl ObjectReference {
pub const NULL: ObjectReference = ObjectReference(0);
}
@ -125,7 +131,7 @@ impl ObjectArea {
fn fill_byte_cache(&mut self, class_store: &ClassStore) -> usize {
let byte_class_index = class_store.class_idx_from_name(&String::from("java/lang/Byte")).unwrap();
let mut total_memory_usage = 0;
for byte in 0..=u8::MAX {
for byte in i8::MIN..=i8::MAX {
let (byte_object_ref, object_memory) = self.make(class_store, byte_class_index);
self.set_object_field(byte_object_ref, "value", FieldValue::Byte(byte), byte_class_index, class_store).unwrap();
@ -154,7 +160,7 @@ impl ObjectArea {
let array_object = ByteArray {
class_ref: array_class_ref,
content: vec![0_u8; capacity].into(),
content: vec![0_i8; capacity].into(),
};
let array_ref = self.store_entry(CompartmentEntry::ByteArray(array_object));
@ -350,12 +356,17 @@ impl ObjectArea {
let array_element = array.content.get_mut(index).unwrap();
*array_element = match element { FieldValue::Reference(r) => r, _ => unreachable!() } ;
},
}
CompartmentEntry::ByteArray(array) => {
let array_element = array.content.get_mut(index).unwrap();
*array_element = match element { FieldValue::Byte(b) => b, _ => unreachable!() } ;
},
}
CompartmentEntry::CharArray(array) => {
let array_element = array.content.get_mut(index).unwrap();
*array_element = match element { FieldValue::Char(c) => c, _ => unreachable!() } ;
}
_ => unreachable!(),
};
@ -508,7 +519,7 @@ pub struct CharArray {
#[derive(Debug)]
pub struct ByteArray {
class_ref: ObjectReference,
content: Box<[u8]>,
content: Box<[i8]>,
}
#[derive(Debug)]
@ -639,7 +650,7 @@ pub struct ObjectField {
#[derive(Debug, Clone, Copy)]
pub enum FieldValue {
Boolean(bool),
Byte(u8),
Byte(i8),
Char(u16),
Short(i16),
Int(i32),
@ -674,7 +685,7 @@ impl FieldValue {
AbstractTypeKind::Long() => Self::Long(0),
AbstractTypeKind::Classname(_) => Self::Reference(ObjectReference::NULL),
AbstractTypeKind::Short() => Self::Short(0),
AbstractTypeKind::Boolean() => Self::Boolean(true),
AbstractTypeKind::Boolean() => Self::Boolean(false),
}
}
}