small improvements

This commit is contained in:
Sander Hautvast 2023-10-14 06:57:27 +02:00
parent 1e6f7956e8
commit 08c4ba33ee
3 changed files with 14 additions and 14 deletions

View file

@ -22,13 +22,13 @@ pub struct Class {
pub access_flags: u16,
pub name: String,
pub super_class_name: Option<String>,
pub super_class: Option<Rc<Class>>,
pub super_class: Option<Arc<Class>>,
pub interface_indices: Vec<u16>,
pub interfaces: Vec<Class>,
pub fields: HashMap<String, Field>,
pub methods: HashMap<String, Method>,
pub attributes: HashMap<String, AttributeType>,
pub(crate) field_mapping: Option<HashMap<String, HashMap<String, (String, usize)>>>, // first key: this/super/supersuper-name(etc), second key: fieldname, value (type, index). See below
pub(crate) field_mapping: Option<HashMap<String, HashMap<String, (String, usize)>>>, // first key: this/super/supersuper-name(etc), second key: fieldname, value (type, index)
}
impl Class {

View file

@ -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<Class>,
pub class: Arc<Class>,
pub data: Vec<Arc<UnsafeCell<Value>>>,
}//arrays
@ -36,7 +34,7 @@ unsafe impl Sync for Object {}
// object, not array
impl Object {
pub fn new(class: Rc<Class>) -> Self {
pub fn new(class: Arc<Class>) -> Self {
let instance_data = Object::init_fields(&class);
Self { class, data: instance_data}
}

View file

@ -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<HashMap<String, Rc<Class>>> = Lazy::new(|| HashMap::new()); //TODO add mutex...and Arc most likely
static mut CLASSDEFS: Lazy<HashMap<String, Arc<Class>>> = Lazy::new(|| HashMap::new()); //TODO add mutex..
pub struct Vm {
classpath: Vec<String>,
@ -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<Rc<Class>, Error> {
pub fn get_class(&self, class_name: &str) -> Result<Arc<Class>, 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<Class>) -> Object {
pub fn new_instance(class: Arc<Class>) -> 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())?;