Fix enumeration issue
This commit is contained in:
parent
3c4921aa54
commit
9243c0b291
4 changed files with 62 additions and 57 deletions
|
@ -51,21 +51,37 @@ impl HeapArea {
|
|||
self.memory_used += self.static_area.make(class, class_index);
|
||||
}
|
||||
|
||||
pub fn byte_array_from_rust_string(&mut self, string: &str, class_store: &ClassStore) -> ObjectReference {
|
||||
let mut byte_obj_vec = Vec::<ObjectReference>::with_capacity(string.len());
|
||||
for byte in string.as_bytes() {
|
||||
// TODO: Take bytes from ByteCache
|
||||
pub fn fill_byte_cache(&mut self, class_store: &ClassStore) {
|
||||
self.memory_used += self.object_area.fill_byte_cache(class_store);
|
||||
}
|
||||
|
||||
let byte_class_idx = class_store.class_idx_from_name(&"java/lang/Byte".to_string()).unwrap();
|
||||
let byte_obj = self.make_object(&class_store, byte_class_idx);
|
||||
self.object_area.set_object_field(byte_obj, "value", FieldValue::Byte(*byte), byte_class_idx, &class_store).unwrap();
|
||||
pub fn make_handmade_string(&mut self, s: &String, class_store: &ClassStore) -> ObjectReference {
|
||||
let utf16_bytes = {
|
||||
let utf16 = s.encode_utf16();
|
||||
let mut byte_buffer = Vec::with_capacity(s.len() * 2);
|
||||
|
||||
byte_obj_vec.push(byte_obj);
|
||||
}
|
||||
for utf16_point in utf16 {
|
||||
let bytes = utf16_point.to_ne_bytes();
|
||||
byte_buffer.push(bytes[0]);
|
||||
byte_buffer.push(bytes[1]);
|
||||
}
|
||||
|
||||
let byte_array = self.make_array(&class_store, byte_obj_vec.into_boxed_slice());
|
||||
byte_buffer
|
||||
};
|
||||
|
||||
byte_array
|
||||
let byte_object_refs = utf16_bytes.iter().map(|byte| self.object_area.cached_byte_object(*byte)).collect();
|
||||
|
||||
let byte_array_ref = self.make_array(class_store, byte_object_refs);
|
||||
|
||||
|
||||
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
|
||||
self.object_area.set_object_field(string_ref, "coder", FieldValue::Byte(UTF16_CODER), string_class_index, class_store).unwrap();
|
||||
|
||||
string_ref
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -83,14 +99,34 @@ const DEFAULT_COMPARTMENT_CAPACITY: u32 = u16::MAX as u32;
|
|||
const INVALID_FIRST_OBJECT: usize = u16::MAX as usize;
|
||||
const INVALID_NEXT_COMPARTMENT: usize = 0;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct ObjectArea {
|
||||
memory_used: usize,
|
||||
byte_object_cache: Vec<ObjectReference>,
|
||||
compartments: Vec<ObjectCompartment>,
|
||||
first_free_compartment: usize,
|
||||
}
|
||||
|
||||
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 {
|
||||
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();
|
||||
|
||||
self.byte_object_cache.push(byte_object_ref);
|
||||
|
||||
total_memory_usage += object_memory;
|
||||
}
|
||||
|
||||
total_memory_usage
|
||||
}
|
||||
|
||||
pub fn cached_byte_object(&mut self, byte: u8) -> ObjectReference {
|
||||
self.byte_object_cache[byte as usize]
|
||||
}
|
||||
|
||||
fn make_empty_array(&mut self, class_store: &ClassStore, element_type_desc: AbstractTypeDescription, capacity: usize) -> (ObjectReference, usize) {
|
||||
//
|
||||
// make new type desc
|
||||
|
@ -225,8 +261,8 @@ impl ObjectArea {
|
|||
|
||||
let field_option = ClassFieldIterator::new(object.class_index, class_store)
|
||||
.filter(|f| ! (f.access_flags & FieldAccessFlag::Static))
|
||||
.filter(|f| f.name == field_name)
|
||||
.enumerate()
|
||||
.filter(|(_, f)| f.name == field_name)
|
||||
.next();
|
||||
|
||||
match field_option {
|
||||
|
@ -265,8 +301,8 @@ impl ObjectArea {
|
|||
|
||||
let field_option = ClassFieldIterator::new(object.class_index, class_store)
|
||||
.filter(|f| ! (f.access_flags & FieldAccessFlag::Static))
|
||||
.filter(|f| f.name == field_name)
|
||||
.enumerate()
|
||||
.filter(|(_, f)| f.name == field_name)
|
||||
.next();
|
||||
|
||||
match field_option {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue