small improvements
This commit is contained in:
parent
1e6f7956e8
commit
08c4ba33ee
3 changed files with 14 additions and 14 deletions
|
|
@ -22,13 +22,13 @@ pub struct Class {
|
||||||
pub access_flags: u16,
|
pub access_flags: u16,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub super_class_name: Option<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 interface_indices: Vec<u16>,
|
||||||
pub interfaces: Vec<Class>,
|
pub interfaces: Vec<Class>,
|
||||||
pub fields: HashMap<String, Field>,
|
pub fields: HashMap<String, Field>,
|
||||||
pub methods: HashMap<String, Method>,
|
pub methods: HashMap<String, Method>,
|
||||||
pub attributes: HashMap<String, AttributeType>,
|
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 {
|
impl Class {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
use std::cell::UnsafeCell;
|
use std::cell::UnsafeCell;
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::class::{Class, Value};
|
use crate::class::{Class, Value};
|
||||||
|
|
@ -11,7 +9,7 @@ use crate::classloader::CpEntry;
|
||||||
pub struct Object {
|
pub struct Object {
|
||||||
// locked: bool,
|
// locked: bool,
|
||||||
// hashcode: i32,
|
// hashcode: i32,
|
||||||
pub class: Rc<Class>,
|
pub class: Arc<Class>,
|
||||||
pub data: Vec<Arc<UnsafeCell<Value>>>,
|
pub data: Vec<Arc<UnsafeCell<Value>>>,
|
||||||
}//arrays
|
}//arrays
|
||||||
|
|
||||||
|
|
@ -36,7 +34,7 @@ unsafe impl Sync for Object {}
|
||||||
|
|
||||||
// object, not array
|
// object, not array
|
||||||
impl Object {
|
impl Object {
|
||||||
pub fn new(class: Rc<Class>) -> Self {
|
pub fn new(class: Arc<Class>) -> Self {
|
||||||
let instance_data = Object::init_fields(&class);
|
let instance_data = Object::init_fields(&class);
|
||||||
Self { class, data: instance_data}
|
Self { class, data: instance_data}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/vm.rs
18
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
|
//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 {
|
pub struct Vm {
|
||||||
classpath: Vec<String>,
|
classpath: Vec<String>,
|
||||||
|
|
@ -56,7 +56,9 @@ const PATH_SEPARATOR: char = ':';
|
||||||
#[cfg(target_family = "windows")]
|
#[cfg(target_family = "windows")]
|
||||||
const PATH_SEPARATOR: char = ';';
|
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 {
|
impl Vm {
|
||||||
fn local_stack(&mut self) -> &mut StackFrame {
|
fn local_stack(&mut self) -> &mut StackFrame {
|
||||||
let i = self.stack.len() - 1;
|
let i = self.stack.len() - 1;
|
||||||
|
|
@ -71,10 +73,10 @@ impl Vm {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse the binary data into a Class struct
|
// gets the Class from cache, or reads it from classpath,
|
||||||
// gets the file 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
|
// 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);
|
println!("get_class {}", class_name);
|
||||||
unsafe {
|
unsafe {
|
||||||
let entry = CLASSDEFS.entry(class_name.into());
|
let entry = CLASSDEFS.entry(class_name.into());
|
||||||
|
|
@ -92,13 +94,13 @@ impl Vm {
|
||||||
}
|
}
|
||||||
class.initialize();
|
class.initialize();
|
||||||
|
|
||||||
Rc::new(class)
|
Arc::new(class)
|
||||||
});
|
});
|
||||||
Ok(entry.clone())
|
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 class = class;
|
||||||
let mut instance = Object::new(class.clone());
|
let mut instance = Object::new(class.clone());
|
||||||
instance
|
instance
|
||||||
|
|
@ -273,7 +275,7 @@ impl Vm {
|
||||||
}
|
}
|
||||||
GETSTATIC => {
|
GETSTATIC => {
|
||||||
let cp_index = read_u16(&code.opcodes, pc);
|
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_index = class.get_class_ref(class_index).unwrap();
|
||||||
let class_name = class.get_utf8(class_name_index).unwrap();
|
let class_name = class.get_utf8(class_name_index).unwrap();
|
||||||
let class = self.get_class(class_name.as_str())?;
|
let class = self.get_class(class_name.as_str())?;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue