From 075a94020ed87ec4bcf16d7f0adb062654da6334 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Sat, 11 Nov 2023 12:43:32 +0100 Subject: [PATCH] cleanup --- src/class.rs | 27 ++++----------------------- src/classloader/classdef.rs | 4 ++++ src/vm/native.rs | 5 +++-- src/vm/opcodes.rs | 2 +- src/vm/vm.rs | 27 ++++++++------------------- 5 files changed, 20 insertions(+), 45 deletions(-) diff --git a/src/class.rs b/src/class.rs index 02a9e69..bb57e4d 100644 --- a/src/class.rs +++ b/src/class.rs @@ -1,11 +1,14 @@ use std::cell::RefCell; use std::collections::{HashMap, LinkedList}; +use std::fmt; +use std::fmt::Debug; use std::rc::Rc; use log::debug; use rand::random; use crate::class::ObjectRef::*; +use crate::classloader::classdef::CpEntry; /// ClassId facilitates loose coupling between classes, classdefs and objects pub type ClassId = usize; @@ -175,8 +178,6 @@ fn into_vec_i8(v: Vec) -> Vec { #[derive(Debug)] pub struct Object { - // locked: bool, - // hashcode: i32, pub id: u32, pub class_id: ClassId, pub data: Vec, @@ -195,7 +196,7 @@ impl Object { // initializes all non-static fields to their default values pub(crate) fn init_fields(class: &Class) -> Vec { - let mut field_data = vec![Value::Null;class.n_object_fields()]; + let mut field_data = vec![Value::Null; class.n_object_fields()]; for (_, fields) in &class.object_field_mapping { for (_, type_index) in fields { @@ -238,24 +239,4 @@ impl Object { debug!("from data {:?}", self.data); self.data.get(type_index.index).unwrap() } - - // fn get_field_name(&self, cp_index: &u16) -> &str { - // if let CpEntry::Utf8(name) = self.class.constant_pool.get(cp_index).unwrap() { - // return name; - // } - // panic!() - // } } - -// impl Debug for Object { -// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { -// // let fields: Vec = self.data.unwrap().iter().map(|(k)| { -// // // let mut r: String = self.get_field_name(k).into(); -// // // r.push(':'); -// // // r.push_str(format!("{:?}").as_str()); -// // // r -// // } -// // ).collect(); -// write!(f, "{}", self.class.name) -// } -// } diff --git a/src/classloader/classdef.rs b/src/classloader/classdef.rs index f2bd4c9..73f6c0b 100644 --- a/src/classloader/classdef.rs +++ b/src/classloader/classdef.rs @@ -66,6 +66,10 @@ impl ClassDef { self.methods.get(name) } + pub fn has_method(&self, name: &str) -> bool { + self.methods.contains_key(name) + } + pub fn cp_field_ref(&self, index: &u16) -> (&u16, &u16) { if let CpEntry::Fieldref(class_index, name_and_type_index) = self.constant_pool.get(index).unwrap() diff --git a/src/vm/native.rs b/src/vm/native.rs index 4d7deaa..765a595 100644 --- a/src/vm/native.rs +++ b/src/vm/native.rs @@ -2,7 +2,8 @@ use std::cell::RefCell; use std::rc::Rc; -use anyhow::{anyhow, Error}; + +use anyhow::Error; use log::debug; use once_cell::sync::Lazy; @@ -64,7 +65,7 @@ fn cmdProps(vm: &mut Vm, stackframes: &mut Vec) -> Result) -> Result { +fn vmProperties(_vm: &mut Vm, _stackframes: &mut [StackFrame]) -> Result { let props: Lazy> = Lazy::new(|| { let vec: Vec = Vec::new(); //TODO insert some values diff --git a/src/vm/opcodes.rs b/src/vm/opcodes.rs index 0ed1c6f..bfd4df2 100644 --- a/src/vm/opcodes.rs +++ b/src/vm/opcodes.rs @@ -302,7 +302,7 @@ pub const MONITOREXIT: u8 = 195; pub const IFNULL: u8 = 198; pub const IFNONNULL: u8 = 199; -pub const OPCODES: Lazy> = Lazy::new(|| { +pub static OPCODES: Lazy> = Lazy::new(|| { let mut opcodes = vec![""; 256]; opcodes[NOP as usize] = "NOP"; opcodes[ACONST_NULL as usize] = "ACONST_NULL"; diff --git a/src/vm/vm.rs b/src/vm/vm.rs index 7b61398..992b4bc 100644 --- a/src/vm/vm.rs +++ b/src/vm/vm.rs @@ -3,11 +3,11 @@ use std::io::Write; use std::rc::Rc; use anyhow::Error; -use log::{debug, error}; +use log::debug; use crate::class::{Class, Object, ObjectRef, Value}; -use crate::class::Value::{F32, F64, I32, I64, Null, Ref, Utf8, Void}; -use crate::classloader::classdef::{AttributeType, CpEntry, Method, Modifier}; +use crate::class::Value::{F32, F64, I32, I64, Null, Ref, Void}; +use crate::classloader::classdef::{AttributeType, Modifier}; use crate::classloader::io::{read_u16, read_u8}; use crate::classmanager; use crate::classmanager::get_class_by_id; @@ -67,17 +67,11 @@ impl Vm { method_name: &str, args: Vec, ) -> Result { - for arg in &args { - if let Ref(r) = arg { - debug!("arg {:?}",r); - } else { - debug!("arg {:?}",arg); - } - } - + // this can't be null if let Null = args[0] { panic!("NPE"); } + if let Ref(this) = &args[0] { if let ObjectRef::Object(this) = this { let thisb= this.borrow(); @@ -95,8 +89,7 @@ impl Vm { for parent_id in &class.parents { let classdef = classmanager::get_classdef(parent_id); - let method = classdef.get_method(method_name); - if let Some(_) = method { + if classdef.has_method(method_name) { let class= get_class_by_id(parent_id).unwrap(); return self.execute_class(stack, class, method_name, args.clone()); } @@ -105,16 +98,12 @@ impl Vm { panic!("ClassNotFound"); } } - } else if let ObjectRef::Class(_class) = this { - //TODO is this right?? - classmanager::load_class_by_name("java/lang/Class");//TODO preload, so this is not needed + } else if let ObjectRef::Class(_class) = this { // special case for Class ? let klazz = classmanager::get_class_by_name("java/lang/Class").unwrap(); - // let klazzdef = self.classmanager.get_classdef(&klazz.id); return self.execute_class(stack, klazz, method_name, args); } } - println!("this is not an object reference {}", class_name); - panic!(); + panic!("Method {} not found in class {}", method_name, class_name); } pub fn execute_special(