Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight refactoring #10

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() {
};

println!(
"result={}",
"\nresult={}",
result.map_or_else(|| "<empty>".to_string(), |v| v.to_string())
);
}
23 changes: 10 additions & 13 deletions vm/src/class_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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 {
Expand Down Expand Up @@ -87,17 +84,17 @@ 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();

method_by_signature.insert(
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,
),
);
Expand Down
27 changes: 14 additions & 13 deletions vm/src/method_area/method_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {:?}",
Expand Down