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::cell::RefCell;
|
||||||
use std::collections::{HashMap, LinkedList};
|
use std::collections::{HashMap, LinkedList};
|
||||||
use std::fmt;
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
|
@ -8,7 +7,6 @@ use log::debug;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
|
|
||||||
use crate::class::ObjectRef::*;
|
use crate::class::ObjectRef::*;
|
||||||
use crate::classloader::classdef::CpEntry;
|
|
||||||
|
|
||||||
/// ClassId facilitates loose coupling between classes, classdefs and objects
|
/// ClassId facilitates loose coupling between classes, classdefs and objects
|
||||||
pub type ClassId = usize;
|
pub type ClassId = usize;
|
||||||
|
|
@ -42,6 +40,7 @@ pub struct Class {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Class {
|
impl Class {
|
||||||
|
/// gets the number of non-static fields on the class
|
||||||
pub(crate) fn n_object_fields(&self) -> usize {
|
pub(crate) fn n_object_fields(&self) -> usize {
|
||||||
self.object_field_mapping
|
self.object_field_mapping
|
||||||
.iter()
|
.iter()
|
||||||
|
|
@ -103,7 +102,6 @@ pub enum ObjectRef {
|
||||||
StringArray(Vec<String>),
|
StringArray(Vec<String>),
|
||||||
ObjectArray(ClassId, Vec<ObjectRef>),
|
ObjectArray(ClassId, Vec<ObjectRef>),
|
||||||
Object(Rc<RefCell<Object>>),
|
Object(Rc<RefCell<Object>>),
|
||||||
//Box necessary??
|
|
||||||
Class(Class),
|
Class(Class),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,13 +176,17 @@ fn into_vec_i8(v: Vec<u8>) -> Vec<i8> {
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Object {
|
pub struct Object {
|
||||||
|
/// unique id for instance
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
|
/// loose ref to class
|
||||||
pub class_id: ClassId,
|
pub class_id: ClassId,
|
||||||
|
/// instance field data
|
||||||
pub data: Vec<Value>,
|
pub data: Vec<Value>,
|
||||||
} //arrays
|
}
|
||||||
|
|
||||||
// object, not array
|
// object, not array
|
||||||
impl Object {
|
impl Object {
|
||||||
|
|
||||||
pub fn new(class: &Class) -> Self {
|
pub fn new(class: &Class) -> Self {
|
||||||
let instance_data = Object::init_fields(class);
|
let instance_data = Object::init_fields(class);
|
||||||
Self {
|
Self {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use once_cell::sync::Lazy;
|
||||||
use crate::class::{ObjectRef, Value};
|
use crate::class::{ObjectRef, Value};
|
||||||
use crate::class::ObjectRef::Object;
|
use crate::class::ObjectRef::Object;
|
||||||
use crate::class::Value::{I32, Void};
|
use crate::class::Value::{I32, Void};
|
||||||
use crate::classmanager;
|
use crate::{class, classmanager};
|
||||||
use crate::vm::stack::StackFrame;
|
use crate::vm::stack::StackFrame;
|
||||||
use crate::vm::Vm;
|
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> {
|
fn cmdProps(vm: &mut Vm, stackframes: &mut Vec<StackFrame>) -> Result<Value, Error> {
|
||||||
classmanager::load_class_by_name("java/util/HashMap");
|
classmanager::load_class_by_name("java/util/HashMap");
|
||||||
let hashmap_class = classmanager::get_class_by_name("java/util/HashMap").unwrap();
|
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(class::Object::new(hashmap_class))))); // this is convoluted
|
||||||
let hashmap = Value::Ref(Object(Rc::new(RefCell::new(hashmap))));
|
|
||||||
vm.execute_special(stackframes, "java/util/HashMap", "<init>()V", vec![hashmap.clone()])?;
|
vm.execute_special(stackframes, "java/util/HashMap", "<init>()V", vec![hashmap.clone()])?;
|
||||||
Ok(hashmap)
|
Ok(hashmap)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,10 @@ use anyhow::Error;
|
||||||
use log::debug;
|
use log::debug;
|
||||||
|
|
||||||
use crate::class::{Class, ObjectRef, Value};
|
use crate::class::{Class, ObjectRef, Value};
|
||||||
|
use crate::class::ObjectRef::Object;
|
||||||
use crate::class::Value::{I32, Ref};
|
use crate::class::Value::{I32, Ref};
|
||||||
use crate::classloader::classdef::{CpEntry, Method};
|
use crate::classloader::classdef::{CpEntry, Method};
|
||||||
use crate::classmanager;
|
use crate::{class, classmanager};
|
||||||
use crate::vm::stack::StackFrame;
|
use crate::vm::stack::StackFrame;
|
||||||
use crate::vm::Vm;
|
use crate::vm::Vm;
|
||||||
use crate::vm::vm::{current_frame, Invocation, MethodSignature};
|
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();
|
let string: Vec<u8> = string.as_bytes().into();
|
||||||
classmanager::load_class_by_name("java/lang/String");
|
classmanager::load_class_by_name("java/lang/String");
|
||||||
let stringclass = classmanager::get_class_by_name("java/lang/String").unwrap();
|
let stringclass = classmanager::get_class_by_name("java/lang/String").unwrap();
|
||||||
let mut stringinstance = Vm::new_instance(stringclass);
|
let mut stringinstance = class::Object::new(stringclass);
|
||||||
stringinstance.set(stringclass, "java/lang/String", "value", Value::Ref(ObjectRef::new_byte_array(string)));
|
stringinstance.set(stringclass, "java/lang/String", "value", Ref(ObjectRef::new_byte_array(string)));
|
||||||
|
|
||||||
debug!("new string \"{}\"", utf8);
|
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) => {
|
CpEntry::Long(l) => {
|
||||||
current_frame(stackframes).push(Value::I64(*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");
|
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
|
/// execute the bytecode
|
||||||
pub fn execute_virtual(
|
pub fn execute_virtual(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
@ -516,7 +512,7 @@ impl Vm {
|
||||||
classmanager::load_class_by_name(class_name);
|
classmanager::load_class_by_name(class_name);
|
||||||
let class_to_instantiate = classmanager::get_class_by_name(class_name).unwrap();
|
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,
|
class_to_instantiate,
|
||||||
))));
|
))));
|
||||||
current_frame(stackframes).push(Ref(object));
|
current_frame(stackframes).push(Ref(object));
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue