diff --git a/src/class.rs b/src/class.rs index 3c67d89..d71ac5e 100644 --- a/src/class.rs +++ b/src/class.rs @@ -22,13 +22,13 @@ pub struct Class { pub access_flags: u16, pub name: String, pub super_class_name: Option, - pub super_class: Option>, + pub super_class: Option>, pub interface_indices: Vec, pub interfaces: Vec, pub fields: HashMap, pub methods: HashMap, pub attributes: HashMap, - pub(crate) field_mapping: Option>>, // first key: this/super/supersuper-name(etc), second key: fieldname, value (type, index). See below + pub(crate) field_mapping: Option>>, // first key: this/super/supersuper-name(etc), second key: fieldname, value (type, index) } impl Class { diff --git a/src/heap.rs b/src/heap.rs index 9baefb6..25f225a 100644 --- a/src/heap.rs +++ b/src/heap.rs @@ -1,7 +1,5 @@ use std::cell::UnsafeCell; -use std::collections::HashMap; use std::fmt; -use std::rc::Rc; use std::sync::Arc; use crate::class::{Class, Value}; @@ -11,7 +9,7 @@ use crate::classloader::CpEntry; pub struct Object { // locked: bool, // hashcode: i32, - pub class: Rc, + pub class: Arc, pub data: Vec>>, }//arrays @@ -36,7 +34,7 @@ unsafe impl Sync for Object {} // object, not array impl Object { - pub fn new(class: Rc) -> Self { + pub fn new(class: Arc) -> Self { let instance_data = Object::init_fields(&class); Self { class, data: instance_data} } diff --git a/src/vm.rs b/src/vm.rs index b759c7d..cd0669a 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -42,7 +42,7 @@ impl StackFrame { } //trying to be ready for multithreaded as much as possible, using Arc's and all, but it will still require (a lot of) extra work -static mut CLASSDEFS: Lazy>> = Lazy::new(|| HashMap::new()); //TODO add mutex...and Arc most likely +static mut CLASSDEFS: Lazy>> = Lazy::new(|| HashMap::new()); //TODO add mutex.. pub struct Vm { classpath: Vec, @@ -56,7 +56,9 @@ const PATH_SEPARATOR: char = ':'; #[cfg(target_family = "windows")] const PATH_SEPARATOR: char = ';'; -// The singlethreaded VM (maybe a future Thread) +/// The singlethreaded VM (maybe a future Thread) +//TODO goto +//TODO error handling impl Vm { fn local_stack(&mut self) -> &mut StackFrame { let i = self.stack.len() - 1; @@ -71,10 +73,10 @@ impl Vm { } } - // parse the binary data into a Class struct - // gets the file from cache, or reads it from classpath + // gets the Class from cache, or reads it from classpath, + // then parses the binary data into a Class struct // Vm keeps ownership of the class and hands out Arc references to it - pub fn get_class(&self, class_name: &str) -> Result, Error> { + pub fn get_class(&self, class_name: &str) -> Result, Error> { println!("get_class {}", class_name); unsafe { let entry = CLASSDEFS.entry(class_name.into()); @@ -92,13 +94,13 @@ impl Vm { } class.initialize(); - Rc::new(class) + Arc::new(class) }); Ok(entry.clone()) } } - pub fn new_instance(class: Rc) -> Object { + pub fn new_instance(class: Arc) -> Object { let mut class = class; let mut instance = Object::new(class.clone()); instance @@ -273,7 +275,7 @@ impl Vm { } GETSTATIC => { let cp_index = read_u16(&code.opcodes, pc); - let (class_index, _field_name_and_type_index) = class.get_field_ref(&cp_index).unwrap(); + let (class_index, _field_name_and_type_index) = class.get_field_ref(&cp_index).unwrap(); // all these unwraps are safe as long as the class is valid let class_name_index = class.get_class_ref(class_index).unwrap(); let class_name = class.get_utf8(class_name_index).unwrap(); let class = self.get_class(class_name.as_str())?;