cleanup
This commit is contained in:
parent
40a7c6cd1e
commit
075a94020e
5 changed files with 20 additions and 45 deletions
27
src/class.rs
27
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<u8>) -> Vec<i8> {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Object {
|
||||
// locked: bool,
|
||||
// hashcode: i32,
|
||||
pub id: u32,
|
||||
pub class_id: ClassId,
|
||||
pub data: Vec<Value>,
|
||||
|
|
@ -195,7 +196,7 @@ impl Object {
|
|||
|
||||
// initializes all non-static fields to their default values
|
||||
pub(crate) fn init_fields(class: &Class) -> Vec<Value> {
|
||||
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<String> = 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)
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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<StackFrame>) -> Result<Value, Err
|
|||
Ok(hashmap)
|
||||
}
|
||||
|
||||
fn vmProperties(_vm: &mut Vm, _stackframes: &mut Vec<StackFrame>) -> Result<Value, Error> {
|
||||
fn vmProperties(_vm: &mut Vm, _stackframes: &mut [StackFrame]) -> Result<Value, Error> {
|
||||
let props: Lazy<Vec<String>> = Lazy::new(|| {
|
||||
let vec: Vec<String> = Vec::new();
|
||||
//TODO insert some values
|
||||
|
|
|
|||
|
|
@ -302,7 +302,7 @@ pub const MONITOREXIT: u8 = 195;
|
|||
pub const IFNULL: u8 = 198;
|
||||
pub const IFNONNULL: u8 = 199;
|
||||
|
||||
pub const OPCODES: Lazy<Vec<&str>> = Lazy::new(|| {
|
||||
pub static OPCODES: Lazy<Vec<&str>> = Lazy::new(|| {
|
||||
let mut opcodes = vec![""; 256];
|
||||
opcodes[NOP as usize] = "NOP";
|
||||
opcodes[ACONST_NULL as usize] = "ACONST_NULL";
|
||||
|
|
|
|||
27
src/vm/vm.rs
27
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<Value>,
|
||||
) -> Result<Value, Error> {
|
||||
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(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue