Skip to content

Commit

Permalink
Refactoring: move creation of instance_fields_hierarchy to JavaClass
Browse files Browse the repository at this point in the history
  • Loading branch information
hextriclosan committed Nov 29, 2024
1 parent 27db40b commit 6c79084
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion vm/src/heap/java_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) enum HeapValue {
Arr(Array),
}

impl<'a> JavaInstance {
impl JavaInstance {
pub fn new(
instance_name: String,
fields: IndexMap<ClassName, IndexMap<FieldNameType, Field>>,
Expand Down
23 changes: 22 additions & 1 deletion vm/src/method_area/java_class.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::execution_engine::executor::Executor;
use crate::heap::java_instance::FieldNameType;
use crate::heap::java_instance::{ClassName, FieldNameType};
use crate::method_area::cpool_helper::CPoolHelper;
use crate::method_area::field::Field;
use crate::method_area::java_method::JavaMethod;
use crate::method_area::method_area::with_method_area;
use indexmap::IndexMap;
use jdescriptor::TypeDescriptor;
use once_cell::sync::OnceCell;
use std::collections::{HashMap, HashSet};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand All @@ -23,6 +25,8 @@ pub(crate) struct JavaClass {
access_flags: u16,

static_fields_initialized: AtomicBool,

instance_fields_hierarchy: OnceCell<IndexMap<ClassName, IndexMap<FieldNameType, Field>>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -73,6 +77,7 @@ impl JavaClass {
interfaces,
access_flags,
static_fields_initialized: AtomicBool::new(false),
instance_fields_hierarchy: OnceCell::new(),
}
}

Expand Down Expand Up @@ -194,6 +199,22 @@ impl JavaClass {
))
})
}

pub fn instance_fields_hierarchy(
&self,
) -> crate::error::Result<&IndexMap<ClassName, IndexMap<FieldNameType, Field>>> {
Ok(&self.instance_fields_hierarchy.get_or_init(|| {
let mut instance_fields_hierarchy = IndexMap::new();
with_method_area(|area| {
area.lookup_and_fill_instance_fields_hierarchy(
&self.this_class_name,
&mut instance_fields_hierarchy,
)
}).expect("error getting instance fields hierarchy");

instance_fields_hierarchy
}))
}
}

impl Methods {
Expand Down
11 changes: 3 additions & 8 deletions vm/src/method_area/method_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,14 @@ impl MethodArea {
&self,
class_name: &str,
) -> crate::error::Result<JavaInstance> {
let mut instance_fields_hierarchy = IndexMap::new();
self.lookup_and_fill_instance_fields_hierarchy(
class_name,
&mut instance_fields_hierarchy,
)?;

let jc = with_method_area(|area| area.get(class_name))?;
Ok(JavaInstance::new(
class_name.to_string(),
instance_fields_hierarchy,
jc.instance_fields_hierarchy()?.clone(),
))
}

fn lookup_and_fill_instance_fields_hierarchy(
pub(crate) fn lookup_and_fill_instance_fields_hierarchy(
&self,
class_name: &str,
instance_fields_hierarchy: &mut IndexMap<ClassName, IndexMap<FieldNameType, Field>>,
Expand Down

0 comments on commit 6c79084

Please sign in to comment.