diff --git a/src/class.rs b/src/class.rs index 94e2770..313efbc 100644 --- a/src/class.rs +++ b/src/class.rs @@ -11,7 +11,7 @@ use crate::io::read_u16; pub struct Class { pub minor_version: u16, pub major_version: u16, - pub constant_pool: Rc>, + pub constant_pool: Rc>, pub access_flags: u16, pub this_class: u16, pub super_class: u16, @@ -35,10 +35,10 @@ impl Class { } pub struct Method { - pub(crate) constant_pool: Rc>, + pub(crate) constant_pool: Rc>, access_flags: u16, - name_index: usize, - descriptor_index: usize, + name_index: u16, + descriptor_index: u16, pub(crate) attributes: HashMap, } @@ -50,10 +50,10 @@ impl fmt::Debug for Method { } impl Method { - pub fn new(constant_pool: Rc>, + pub fn new(constant_pool: Rc>, access_flags: u16, - name_index: usize, - descriptor_index: usize, + name_index: u16, + descriptor_index: u16, attributes: HashMap, ) -> Self { Method { constant_pool, access_flags, name_index, descriptor_index, attributes } } @@ -73,10 +73,10 @@ impl Method { } pub struct Field { - constant_pool: Rc>, + constant_pool: Rc>, access_flags: u16, - name_index: usize, - descriptor_index: usize, + name_index: u16, + descriptor_index: u16, attributes: HashMap, } @@ -88,10 +88,10 @@ impl fmt::Debug for Field { } impl Field { - pub fn new(constant_pool: Rc>, + pub fn new(constant_pool: Rc>, access_flags: u16, - name_index: usize, - descriptor_index: usize, + name_index: u16, + descriptor_index: u16, attributes: HashMap, ) -> Self { Field { constant_pool, access_flags, name_index, descriptor_index, attributes: attributes } } diff --git a/src/lib.rs b/src/lib.rs index c1d3b55..27529bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,9 +15,9 @@ pub fn get_class(bytecode: Vec) -> Option { let constant_pool_count = read_u16(&bytecode, 8); // println!("cp count: {}", constant_pool_count); let mut index = 10; - let mut constant_pool: HashMap = HashMap::with_capacity(constant_pool_count as usize); + let mut constant_pool: HashMap = HashMap::with_capacity(constant_pool_count as usize); let mut cp_index = 1; - while cp_index < constant_pool_count as usize { + while cp_index < constant_pool_count { // println!("cp#{}", cp_index); constant_pool.insert(cp_index, read_constant_pool_entry(&mut cp_index, &mut index, &bytecode)); cp_index += 1; @@ -85,7 +85,7 @@ fn check_magic(bytecode: &[u8]) { } } -fn read_constant_pool_entry(cp_index: &mut usize, index: &mut usize, bytecode: &[u8]) -> CpEntry { +fn read_constant_pool_entry(cp_index: &mut u16, index: &mut usize, bytecode: &[u8]) -> CpEntry { let tag = bytecode[*index]; // println!("#{}: {}", cp_index, tag); match tag { @@ -164,10 +164,10 @@ fn read_constant_pool_entry(cp_index: &mut usize, index: &mut usize, bytecode: & } } -fn read_field(constant_pool: Rc>, index: &mut usize, bytecode: &[u8]) -> Field { +fn read_field(constant_pool: Rc>, index: &mut usize, bytecode: &[u8]) -> Field { let access_flags = read_u16(bytecode, *index); - let name_index = read_u16(bytecode, *index + 2) as usize; - let descriptor_index = read_u16(bytecode, *index + 4) as usize; + let name_index = read_u16(bytecode, *index + 2); + let descriptor_index = read_u16(bytecode, *index + 4); let attributes_count = read_u16(bytecode, *index + 6); *index += 8; let mut attributes = HashMap::new(); @@ -187,10 +187,10 @@ fn read_field(constant_pool: Rc>, index: &mut usize, byt ) } -fn read_method(constant_pool: Rc>, index: &mut usize, bytecode: &[u8]) -> Method { +fn read_method(constant_pool: Rc>, index: &mut usize, bytecode: &[u8]) -> Method { let access_flags = read_u16(bytecode, *index); - let name_index = read_u16(bytecode, *index + 2) as usize; - let descriptor_index = read_u16(bytecode, *index + 4) as usize; + let name_index = read_u16(bytecode, *index + 2); + let descriptor_index = read_u16(bytecode, *index + 4); let attributes_count = read_u16(bytecode, *index + 6); *index += 8; @@ -210,8 +210,8 @@ fn read_method(constant_pool: Rc>, index: &mut usize, by ) } -fn read_attribute(constant_pool: Rc>, bytecode: &[u8], index: &mut usize) -> Option<(String, AttributeType)> { - let attribute_name_index = read_u16(bytecode, *index) as usize; +fn read_attribute(constant_pool: Rc>, bytecode: &[u8], index: &mut usize) -> Option<(String, AttributeType)> { + let attribute_name_index = read_u16(bytecode, *index); *index += 2; let attribute_length = read_u32(bytecode, *index) as usize; *index += 4; diff --git a/src/main.rs b/src/main.rs index 98be196..915b031 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ 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); - } + // 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); + // } } diff --git a/src/vm.rs b/src/vm.rs index 478523a..fbabd98 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -39,7 +39,7 @@ impl Vm { } pub fn new_instance(&self, class: &Class) { - for f in class.fields { + for f in &class.fields { println!("{}", f.type_of()); } // Object::new(Rc::new(class)) @@ -60,7 +60,7 @@ impl Vm { pc += 1; } &opcodes::LDC => { - let cp_index = read_u8(&code.opcodes, pc) as usize; + let cp_index = read_u8(&code.opcodes, pc) as u16; match method.constant_pool.get(&cp_index).unwrap() { CpEntry::Integer(i) => { stack.push(Value::I32(*i)); @@ -73,7 +73,7 @@ impl Vm { pc += 1; } &opcodes::LDC_W => { - let cp_index = read_u16(&code.opcodes, pc) as usize; + let cp_index = read_u16(&code.opcodes, pc); match method.constant_pool.get(&cp_index).unwrap() { CpEntry::Integer(i) => { stack.push(Value::I32(*i)); @@ -86,7 +86,7 @@ impl Vm { pc += 2; } &opcodes::LDC2_W => { - let cp_index = read_u16(&code.opcodes, pc) as usize; + let cp_index = read_u16(&code.opcodes, pc); match method.constant_pool.get(&cp_index).unwrap() { CpEntry::Double(d) => { stack.push(Value::F64(*d)); @@ -110,7 +110,7 @@ impl Vm { return stack.pop(); } &opcodes::NEW => { - let cp_index = read_u16(&code.opcodes, pc) as usize; + let cp_index = read_u16(&code.opcodes, pc); if let CpEntry::ClassRef(class_name_index) = method.constant_pool.get(&cp_index).unwrap() { if let CpEntry::Utf8(class) = method.constant_pool.get(class_name_index).unwrap(){ diff --git a/tests/class_tests.rs b/tests/class_tests.rs index aecb0bd..909e58f 100644 --- a/tests/class_tests.rs +++ b/tests/class_tests.rs @@ -30,7 +30,7 @@ mod test { #[test] fn get_constant_foat() { let class = get_class(io::read_class_file("tests/Float.class")).unwrap(); - Vm::new().new_instance(class); + Vm::new().new_instance(&class); // assert_eq!((55, 0), class.get_version()); // if let Value::F32(v) = Vm::new().execute(class.methods.get("public static getF()F").unwrap()).unwrap() { // assert_eq!(v, 42.0);