implement java main
This commit is contained in:
parent
121b0869f1
commit
d6a40a81a4
8 changed files with 29 additions and 34 deletions
10
src/class.rs
10
src/class.rs
|
|
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use anyhow::{anyhow, Error};
|
||||||
use crate::classloader::CpEntry;
|
use crate::classloader::CpEntry;
|
||||||
use crate::heap::Object;
|
use crate::heap::Object;
|
||||||
|
|
||||||
|
|
@ -27,13 +28,8 @@ impl Class {
|
||||||
(self.major_version, self.minor_version)
|
(self.major_version, self.minor_version)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn execute(&self, method_name: &str) -> Value {
|
pub fn get_method(&self, name: &str) -> Result<&Method, Error> {
|
||||||
// let m = self.methods.get(method_name).unwrap();
|
self.methods.get(name).ok_or(anyhow!("Method {} not found", name))
|
||||||
// execute(m).unwrap() //TODO
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub fn get_method(&self, name: &str) -> &Method {
|
|
||||||
self.methods.get(name).expect("ClassNountFoundException")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsafe impl Send for Class {}
|
unsafe impl Send for Class {}
|
||||||
|
|
|
||||||
17
src/main.rs
17
src/main.rs
|
|
@ -1,15 +1,10 @@
|
||||||
use regex::Regex;
|
use std::io::Error;
|
||||||
|
use classfile_reader::vm::Vm;
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), Error> {
|
||||||
// if let Some(class) = classfile_reader::get_class(classfile_reader::io::read_class_file("./Dummy.class")){
|
let mut vm = Vm::new("tests");
|
||||||
// println!("{:?}", class);
|
vm.execute("Main","public static main([Ljava/lang/String;)V", None).unwrap();
|
||||||
// let ret = class.execute("public static get()D");
|
Ok(())
|
||||||
// println!("{:?}", ret);
|
|
||||||
// }
|
|
||||||
|
|
||||||
let pattern = Regex::new(".*/(.+)").unwrap();
|
|
||||||
let c = pattern.captures("java/lang/String").unwrap().get(1);
|
|
||||||
println!("{}", c.unwrap().as_str());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,7 @@ pub struct Vm {
|
||||||
classes: HashMap<String, Arc<Class>>,
|
classes: HashMap<String, Arc<Class>>,
|
||||||
//TODO implement classloader
|
//TODO implement classloader
|
||||||
heap: Heap,
|
heap: Heap,
|
||||||
|
pub exitcode: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Vm {
|
impl Vm {
|
||||||
|
|
@ -42,13 +43,14 @@ impl Vm {
|
||||||
classpath: classpath.split(':').map(|s| s.to_owned()).collect(),
|
classpath: classpath.split(':').map(|s| s.to_owned()).collect(),
|
||||||
classes: HashMap::new(),
|
classes: HashMap::new(),
|
||||||
heap: Heap::new(),
|
heap: Heap::new(),
|
||||||
|
exitcode: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_class(&mut self, class_name: &str) -> Result<Arc<Class>, Error> {
|
pub fn get_class(&mut self, class_name: &str) -> Result<Arc<Class>, Error> {
|
||||||
let entry = self.classes.entry(class_name.into());
|
let entry = self.classes.entry(class_name.into());
|
||||||
let entry = entry.or_insert_with(|| {
|
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();
|
let bytecode = read_class_file(resolved_path).unwrap();
|
||||||
Arc::new(load_class(bytecode).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<Arc<Object>>) -> Result<Arc<Value>, Error> {
|
pub fn execute(&mut self, class_name: &str, method_name: &str, instance: Option<Arc<Object>>) -> Result<Arc<Value>, Error> {
|
||||||
let class = self.get_class(class_name)?;
|
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() {
|
if let AttributeType::Code(code) = method.attributes.get("Code").unwrap() {
|
||||||
let mut stack = StackFrame::new();
|
let mut stack = StackFrame::new();
|
||||||
let mut pc: usize = 0;
|
let mut pc: usize = 0;
|
||||||
|
|
|
||||||
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
BIN
tests/FloatBean.class
Normal file
BIN
tests/FloatBean.class
Normal file
Binary file not shown.
10
tests/FloatBean.java
Normal file
10
tests/FloatBean.java
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
public class FloatBean {
|
||||||
|
|
||||||
|
private final static float f =42.0F;
|
||||||
|
|
||||||
|
private float value;
|
||||||
|
|
||||||
|
public float getValue(){
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
tests/Main.class
Normal file
BIN
tests/Main.class
Normal file
Binary file not shown.
6
tests/Main.java
Normal file
6
tests/Main.java
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
new FloatBean().getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue