From d326e84254b43942e0e7a33a69f495c5c0f34af3 Mon Sep 17 00:00:00 2001 From: Igor Rudenko Date: Sun, 8 Sep 2024 21:18:31 +0300 Subject: [PATCH] Slight refactoring --- src/main.rs | 2 +- vm/src/class_loader.rs | 23 ++++++++++------------- vm/src/method_area/method_area.rs | 27 ++++++++++++++------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 29094d42..bba57ed7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ fn main() { }; println!( - "result={}", + "\nresult={}", result.map_or_else(|| "".to_string(), |v| v.to_string()) ); } diff --git a/vm/src/class_loader.rs b/vm/src/class_loader.rs index a1ce89b1..515b7102 100644 --- a/vm/src/class_loader.rs +++ b/vm/src/class_loader.rs @@ -29,19 +29,16 @@ impl ClassLoader { let std_class_names = Self::get_class_files_in_dir(std_dir)?; for path in std_class_names { - let class = Self::load_class(path.to_str().ok_or(Error::new_io(io::Error::new( - Other, - "error getting path".to_string(), - )))?)?; + let (class_name, java_class) = Self::load_class(path.to_str().ok_or( + Error::new_io(io::Error::new(Other, "error getting path".to_string())), + )?)?; - loaded_classes.insert(class.0, class.1); + loaded_classes.insert(class_name, java_class); } - let mut classes: Vec = vec![]; for class_file_name in class_file_names { - let class = Self::load_class(class_file_name)?; - loaded_classes.insert(class.0.clone(), class.1); - classes.push(class.0); + let (class_name, java_class) = Self::load_class(class_file_name)?; + loaded_classes.insert(class_name.clone(), java_class); } Ok(Self { @@ -87,7 +84,7 @@ impl ClassLoader { .ok_or(Error::new_constant_pool( "Error getting method method_signature", ))?; - let code = Self::get_cpool_code_attribute(method.attributes()) + let (max_stack, max_locals, code) = Self::get_cpool_code_attribute(method.attributes()) .ok_or(Error::new_constant_pool("Error getting method code"))?; let key_signature = method_name + ":" + method_signature.as_str(); @@ -95,9 +92,9 @@ impl ClassLoader { key_signature.clone(), JavaMethod::new( Signature::from_str(method_signature.as_str())?, - code.0, - code.1, - code.2, + max_stack, + max_locals, + code, class_name, ), ); diff --git a/vm/src/method_area/method_area.rs b/vm/src/method_area/method_area.rs index 84fdefbe..4999b1dc 100644 --- a/vm/src/method_area/method_area.rs +++ b/vm/src/method_area/method_area.rs @@ -61,7 +61,7 @@ impl MethodArea { let cpool = java_class.class_file.constant_pool(); // Retrieve Methodref from the constant pool - let methodref = cpool + let (class_index, name_and_type_index) = cpool .get(methodref_cpool_index as usize) .and_then(|entry| match entry { Methodref { @@ -81,21 +81,22 @@ impl MethodArea { })?; // Retrieve class name from the constant pool - let class_name = get_class_name_by_cpool_class_index(methodref.0, &java_class.class_file); + let class_name = get_class_name_by_cpool_class_index(class_index, &java_class.class_file); // Retrieve method name and signature from the constant pool let (method_name, method_signature) = if let NameAndType { name_index, descriptor_index, - } = cpool.get(methodref.1).ok_or_else(|| { - Error::new_constant_pool( - format!( - "Invalid NameAndType reference at index {} in class {:?}", - methodref_cpool_index, java_class + } = + cpool.get(name_and_type_index).ok_or_else(|| { + Error::new_constant_pool( + format!( + "Invalid NameAndType reference at index {} in class {:?}", + methodref_cpool_index, java_class + ) + .as_str(), ) - .as_str(), - ) - })? { + })? { let name = get_cpool_string(&java_class.class_file, *name_index as usize); let signature = get_cpool_string(&java_class.class_file, *descriptor_index as usize); (name, signature) @@ -122,7 +123,7 @@ impl MethodArea { let cpool = java_class.class_file.constant_pool(); // Retrieve Fieldref from the constant pool - let fieldref = cpool + let (class_index, name_and_type_index) = cpool .get(fieldref_cpool_index as usize) .and_then(|entry| match entry { Fieldref { @@ -142,13 +143,13 @@ impl MethodArea { })?; // Retrieve class name from the constant pool - let class_name = get_class_name_by_cpool_class_index(fieldref.0, &java_class.class_file); + let class_name = get_class_name_by_cpool_class_index(class_index, &java_class.class_file); // Retrieve field name from the constant pool let field_name = if let NameAndType { name_index, descriptor_index: _, - } = cpool.get(fieldref.1).ok_or_else(|| { + } = cpool.get(name_and_type_index).ok_or_else(|| { Error::new_constant_pool( format!( "Invalid NameAndType reference at index {} in class {:?}",