diff --git a/src/class.rs b/src/class.rs index cb95969..e318983 100644 --- a/src/class.rs +++ b/src/class.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::fmt; use std::rc::Rc; use std::sync::Arc; +use anyhow::{anyhow, Error}; use crate::classloader::CpEntry; use crate::heap::Object; @@ -27,13 +28,8 @@ impl Class { (self.major_version, self.minor_version) } - // pub fn execute(&self, method_name: &str) -> Value { - // let m = self.methods.get(method_name).unwrap(); - // execute(m).unwrap() //TODO - // } - - pub fn get_method(&self, name: &str) -> &Method { - self.methods.get(name).expect("ClassNountFoundException") + pub fn get_method(&self, name: &str) -> Result<&Method, Error> { + self.methods.get(name).ok_or(anyhow!("Method {} not found", name)) } } unsafe impl Send for Class {} diff --git a/src/main.rs b/src/main.rs index 8849bcf..9a67592 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,10 @@ -use regex::Regex; +use std::io::Error; +use classfile_reader::vm::Vm; -fn main() { - // if let Some(class) = classfile_reader::get_class(classfile_reader::io::read_class_file("./Dummy.class")){ - // println!("{:?}", class); - // let ret = class.execute("public static get()D"); - // println!("{:?}", ret); - // } - - let pattern = Regex::new(".*/(.+)").unwrap(); - let c = pattern.captures("java/lang/String").unwrap().get(1); - println!("{}", c.unwrap().as_str()); +fn main() -> Result<(), Error> { + let mut vm = Vm::new("tests"); + vm.execute("Main","public static main([Ljava/lang/String;)V", None).unwrap(); + Ok(()) } diff --git a/src/vm.rs b/src/vm.rs index 46a8afd..572a245 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -34,6 +34,7 @@ pub struct Vm { classes: HashMap>, //TODO implement classloader heap: Heap, + pub exitcode: i32, } impl Vm { @@ -42,13 +43,14 @@ impl Vm { classpath: classpath.split(':').map(|s| s.to_owned()).collect(), classes: HashMap::new(), heap: Heap::new(), + exitcode: 0, } } pub fn get_class(&mut self, class_name: &str) -> Result, Error> { let entry = self.classes.entry(class_name.into()); let entry = entry.or_insert_with(|| { - let resolved_path = find_class(&self.classpath, class_name).unwrap(); + let resolved_path = find_class(&self.classpath, class_name).expect("Class not found"); let bytecode = read_class_file(resolved_path).unwrap(); Arc::new(load_class(bytecode).unwrap()) }); @@ -77,7 +79,7 @@ impl Vm { pub fn execute(&mut self, class_name: &str, method_name: &str, instance: Option>) -> Result, Error> { let class = self.get_class(class_name)?; - let method = class.get_method(method_name); + let method = class.get_method(method_name)?; if let AttributeType::Code(code) = method.attributes.get("Code").unwrap() { let mut stack = StackFrame::new(); let mut pc: usize = 0; diff --git a/tests/Float.java b/tests/Float.java deleted file mode 100644 index 9f66939..0000000 --- a/tests/Float.java +++ /dev/null @@ -1,14 +0,0 @@ -public class Float { - - private final static float f =42.0F; - - private float f2; - - public static float getF() { - return f; - } - - public float getF2(){ - return f2; - } -} diff --git a/tests/FloatBean.class b/tests/FloatBean.class new file mode 100644 index 0000000..748e440 Binary files /dev/null and b/tests/FloatBean.class differ diff --git a/tests/FloatBean.java b/tests/FloatBean.java new file mode 100644 index 0000000..977f0f8 --- /dev/null +++ b/tests/FloatBean.java @@ -0,0 +1,10 @@ +public class FloatBean { + + private final static float f =42.0F; + + private float value; + + public float getValue(){ + return value; + } +} diff --git a/tests/Main.class b/tests/Main.class new file mode 100644 index 0000000..3ee664f Binary files /dev/null and b/tests/Main.class differ diff --git a/tests/Main.java b/tests/Main.java new file mode 100644 index 0000000..e7401e9 --- /dev/null +++ b/tests/Main.java @@ -0,0 +1,6 @@ +public class Main { + + public static void main(String[] args){ + new FloatBean().getValue(); + } +}