From 6c790841ce2fbd59e63491c2056824ecb306c91f Mon Sep 17 00:00:00 2001 From: Igor Rudenko Date: Fri, 29 Nov 2024 19:04:41 +0200 Subject: [PATCH] Refactoring: move creation of instance_fields_hierarchy to JavaClass --- vm/src/heap/java_instance.rs | 2 +- vm/src/method_area/java_class.rs | 23 ++++++++++++++++++++++- vm/src/method_area/method_area.rs | 11 +++-------- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/vm/src/heap/java_instance.rs b/vm/src/heap/java_instance.rs index 9e7ca0bb..d279daf3 100644 --- a/vm/src/heap/java_instance.rs +++ b/vm/src/heap/java_instance.rs @@ -81,7 +81,7 @@ pub(crate) enum HeapValue { Arr(Array), } -impl<'a> JavaInstance { +impl JavaInstance { pub fn new( instance_name: String, fields: IndexMap>, diff --git a/vm/src/method_area/java_class.rs b/vm/src/method_area/java_class.rs index 5186af29..d84c20ab 100644 --- a/vm/src/method_area/java_class.rs +++ b/vm/src/method_area/java_class.rs @@ -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; @@ -23,6 +25,8 @@ pub(crate) struct JavaClass { access_flags: u16, static_fields_initialized: AtomicBool, + + instance_fields_hierarchy: OnceCell>>, } #[derive(Debug)] @@ -73,6 +77,7 @@ impl JavaClass { interfaces, access_flags, static_fields_initialized: AtomicBool::new(false), + instance_fields_hierarchy: OnceCell::new(), } } @@ -194,6 +199,22 @@ impl JavaClass { )) }) } + + pub fn instance_fields_hierarchy( + &self, + ) -> crate::error::Result<&IndexMap>> { + 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 { diff --git a/vm/src/method_area/method_area.rs b/vm/src/method_area/method_area.rs index 9ed1c447..9c232923 100644 --- a/vm/src/method_area/method_area.rs +++ b/vm/src/method_area/method_area.rs @@ -374,19 +374,14 @@ impl MethodArea { &self, class_name: &str, ) -> crate::error::Result { - 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>,