more cleanup
This commit is contained in:
parent
075a94020e
commit
712816dc8b
4 changed files with 14 additions and 16 deletions
10
src/class.rs
10
src/class.rs
|
|
@ -1,6 +1,5 @@
|
|||
use std::cell::RefCell;
|
||||
use std::collections::{HashMap, LinkedList};
|
||||
use std::fmt;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
|
@ -8,7 +7,6 @@ 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;
|
||||
|
|
@ -42,6 +40,7 @@ pub struct Class {
|
|||
}
|
||||
|
||||
impl Class {
|
||||
/// gets the number of non-static fields on the class
|
||||
pub(crate) fn n_object_fields(&self) -> usize {
|
||||
self.object_field_mapping
|
||||
.iter()
|
||||
|
|
@ -103,7 +102,6 @@ pub enum ObjectRef {
|
|||
StringArray(Vec<String>),
|
||||
ObjectArray(ClassId, Vec<ObjectRef>),
|
||||
Object(Rc<RefCell<Object>>),
|
||||
//Box necessary??
|
||||
Class(Class),
|
||||
}
|
||||
|
||||
|
|
@ -178,13 +176,17 @@ fn into_vec_i8(v: Vec<u8>) -> Vec<i8> {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Object {
|
||||
/// unique id for instance
|
||||
pub id: u32,
|
||||
/// loose ref to class
|
||||
pub class_id: ClassId,
|
||||
/// instance field data
|
||||
pub data: Vec<Value>,
|
||||
} //arrays
|
||||
}
|
||||
|
||||
// object, not array
|
||||
impl Object {
|
||||
|
||||
pub fn new(class: &Class) -> Self {
|
||||
let instance_data = Object::init_fields(class);
|
||||
Self {
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ use once_cell::sync::Lazy;
|
|||
use crate::class::{ObjectRef, Value};
|
||||
use crate::class::ObjectRef::Object;
|
||||
use crate::class::Value::{I32, Void};
|
||||
use crate::classmanager;
|
||||
use crate::{class, classmanager};
|
||||
use crate::vm::stack::StackFrame;
|
||||
use crate::vm::Vm;
|
||||
|
||||
|
|
@ -59,8 +59,7 @@ fn jdk_internal_util_SystemProps_Raw(vm: &mut Vm, stackframes: &mut Vec<StackFra
|
|||
fn cmdProps(vm: &mut Vm, stackframes: &mut Vec<StackFrame>) -> Result<Value, Error> {
|
||||
classmanager::load_class_by_name("java/util/HashMap");
|
||||
let hashmap_class = classmanager::get_class_by_name("java/util/HashMap").unwrap();
|
||||
let hashmap = Vm::new_instance(hashmap_class);
|
||||
let hashmap = Value::Ref(Object(Rc::new(RefCell::new(hashmap))));
|
||||
let hashmap = Value::Ref(Object(Rc::new(RefCell::new(class::Object::new(hashmap_class))))); // this is convoluted
|
||||
vm.execute_special(stackframes, "java/util/HashMap", "<init>()V", vec![hashmap.clone()])?;
|
||||
Ok(hashmap)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,9 +6,10 @@ use anyhow::Error;
|
|||
use log::debug;
|
||||
|
||||
use crate::class::{Class, ObjectRef, Value};
|
||||
use crate::class::ObjectRef::Object;
|
||||
use crate::class::Value::{I32, Ref};
|
||||
use crate::classloader::classdef::{CpEntry, Method};
|
||||
use crate::classmanager;
|
||||
use crate::{class, classmanager};
|
||||
use crate::vm::stack::StackFrame;
|
||||
use crate::vm::Vm;
|
||||
use crate::vm::vm::{current_frame, Invocation, MethodSignature};
|
||||
|
|
@ -90,12 +91,12 @@ pub(crate) fn load_constant(cp_index: &u16, method: &Method, stackframes: &mut V
|
|||
let string: Vec<u8> = string.as_bytes().into();
|
||||
classmanager::load_class_by_name("java/lang/String");
|
||||
let stringclass = classmanager::get_class_by_name("java/lang/String").unwrap();
|
||||
let mut stringinstance = Vm::new_instance(stringclass);
|
||||
stringinstance.set(stringclass, "java/lang/String", "value", Value::Ref(ObjectRef::new_byte_array(string)));
|
||||
let mut stringinstance = class::Object::new(stringclass);
|
||||
stringinstance.set(stringclass, "java/lang/String", "value", Ref(ObjectRef::new_byte_array(string)));
|
||||
|
||||
debug!("new string \"{}\"", utf8);
|
||||
|
||||
current_frame(stackframes).push(Ref(ObjectRef::Object(Rc::new(RefCell::new(stringinstance)))));
|
||||
current_frame(stackframes).push(Ref(Object(Rc::new(RefCell::new(stringinstance)))));
|
||||
}
|
||||
CpEntry::Long(l) => {
|
||||
current_frame(stackframes).push(Value::I64(*l));
|
||||
|
|
|
|||
|
|
@ -55,10 +55,6 @@ impl Vm {
|
|||
vm.execute_static(stack, "java/lang/System", "initPhase1()V", vec![]).expect("cannot create VM");
|
||||
}
|
||||
|
||||
pub fn new_instance(class: &Class) -> Object {
|
||||
Object::new(class)
|
||||
}
|
||||
|
||||
/// execute the bytecode
|
||||
pub fn execute_virtual(
|
||||
&mut self,
|
||||
|
|
@ -516,7 +512,7 @@ impl Vm {
|
|||
classmanager::load_class_by_name(class_name);
|
||||
let class_to_instantiate = classmanager::get_class_by_name(class_name).unwrap();
|
||||
|
||||
let object = ObjectRef::Object(Rc::new(RefCell::new(Vm::new_instance(
|
||||
let object = ObjectRef::Object(Rc::new(RefCell::new(Object::new(
|
||||
class_to_instantiate,
|
||||
))));
|
||||
current_frame(stackframes).push(Ref(object));
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue