Arc -> Rc
This commit is contained in:
parent
fe8f9f1370
commit
9a53f1785d
3 changed files with 27 additions and 29 deletions
|
|
@ -4,7 +4,6 @@ use anyhow::{anyhow, Error};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::io::read_u16;
|
use crate::io::read_u16;
|
||||||
|
|
||||||
|
|
@ -264,7 +263,7 @@ pub enum Value {
|
||||||
F64(f64),
|
F64(f64),
|
||||||
BOOL(bool),
|
BOOL(bool),
|
||||||
CHAR(char),
|
CHAR(char),
|
||||||
Ref(Arc<Object>),
|
Ref(Rc<Object>),
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Value {}
|
unsafe impl Send for Value {}
|
||||||
|
|
|
||||||
12
src/heap.rs
12
src/heap.rs
|
|
@ -1,14 +1,14 @@
|
||||||
use crate::class::{Class, Value};
|
use crate::class::{Class, Value};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::sync::Arc;
|
use std::rc::Rc;
|
||||||
use crate::classloader::CpEntry;
|
use crate::classloader::CpEntry;
|
||||||
|
|
||||||
pub struct Object {
|
pub struct Object {
|
||||||
// locked: bool,
|
// locked: bool,
|
||||||
// hashcode: i32,
|
// hashcode: i32,
|
||||||
pub class: Arc<Class>,
|
pub class: Rc<Class>,
|
||||||
pub data: HashMap<u16, Arc<Value>>, //TODO optimize
|
pub data: HashMap<u16, Rc<Value>>, //TODO optimize
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for Object {}
|
unsafe impl Send for Object {}
|
||||||
|
|
@ -16,7 +16,7 @@ unsafe impl Send for Object {}
|
||||||
unsafe impl Sync for Object {}
|
unsafe impl Sync for Object {}
|
||||||
|
|
||||||
impl Object {
|
impl Object {
|
||||||
pub fn new(class: Arc<Class>, data: HashMap<u16, Arc<Value>>) -> Self {
|
pub fn new(class: Rc<Class>, data: HashMap<u16, Rc<Value>>) -> Self {
|
||||||
Self { class, data }
|
Self { class, data }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,7 +46,7 @@ impl fmt::Debug for Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Heap {
|
pub(crate) struct Heap {
|
||||||
objects: Vec<Arc<Object>>,
|
objects: Vec<Rc<Object>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Heap {
|
impl Heap {
|
||||||
|
|
@ -54,7 +54,7 @@ impl Heap {
|
||||||
Self { objects: vec![] }
|
Self { objects: vec![] }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new_object(&mut self, object: Arc<Object>) {
|
pub(crate) fn new_object(&mut self, object: Rc<Object>) {
|
||||||
self.objects.push(object);
|
self.objects.push(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
41
src/vm.rs
41
src/vm.rs
|
|
@ -1,6 +1,5 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::{anyhow, Error};
|
||||||
|
|
||||||
|
|
@ -14,7 +13,7 @@ use crate::opcodes::*;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct StackFrame {
|
struct StackFrame {
|
||||||
at: String,
|
at: String,
|
||||||
data: Vec<Arc<Value>>,
|
data: Vec<Rc<Value>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StackFrame {
|
impl StackFrame {
|
||||||
|
|
@ -25,11 +24,11 @@ impl StackFrame {
|
||||||
Self { at, data: vec![] }
|
Self { at, data: vec![] }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push(&mut self, val: Arc<Value>) {
|
fn push(&mut self, val: Rc<Value>) {
|
||||||
self.data.push(val);
|
self.data.push(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pop(&mut self) -> Result<Arc<Value>, Error> {
|
fn pop(&mut self) -> Result<Rc<Value>, Error> {
|
||||||
Ok(self.data.pop().unwrap())
|
Ok(self.data.pop().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -37,7 +36,7 @@ impl StackFrame {
|
||||||
/// single threaded vm
|
/// single threaded vm
|
||||||
pub struct Vm {
|
pub struct Vm {
|
||||||
classpath: Vec<String>,
|
classpath: Vec<String>,
|
||||||
classes: HashMap<String, Arc<Class>>,
|
classes: HashMap<String, Rc<Class>>,
|
||||||
heap: Heap,
|
heap: Heap,
|
||||||
stack: Vec<StackFrame>,
|
stack: Vec<StackFrame>,
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +62,7 @@ impl Vm {
|
||||||
/// parse the binary data into a Class struct
|
/// parse the binary data into a Class struct
|
||||||
/// gets the file from cache, or reads it from classpath
|
/// gets the file from cache, or reads it from classpath
|
||||||
/// 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(&mut self, class_name: &str) -> Result<Arc<Class>, Error> {
|
pub fn get_class(&mut self, class_name: &str) -> Result<Rc<Class>, Error> {
|
||||||
println!("get_class {}", class_name);
|
println!("get_class {}", class_name);
|
||||||
let entry = self.classes.entry(class_name.into());
|
let entry = self.classes.entry(class_name.into());
|
||||||
let entry = entry.or_insert_with(|| {
|
let entry = entry.or_insert_with(|| {
|
||||||
|
|
@ -71,12 +70,12 @@ impl Vm {
|
||||||
let resolved_path = find_class(&self.classpath, class_name).expect("Class not found");
|
let resolved_path = find_class(&self.classpath, class_name).expect("Class not found");
|
||||||
// println!("full path {}", resolved_path);
|
// println!("full path {}", resolved_path);
|
||||||
let bytecode = read_bytecode(resolved_path).unwrap();
|
let bytecode = read_bytecode(resolved_path).unwrap();
|
||||||
Arc::new(load_class(bytecode).unwrap())
|
Rc::new(load_class(bytecode).unwrap())
|
||||||
});
|
});
|
||||||
Ok(entry.clone())
|
Ok(entry.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_instance(&self, class: Arc<Class>) -> Object {
|
pub fn new_instance(&self, class: Rc<Class>) -> Object {
|
||||||
//TODO add fields from superclasses
|
//TODO add fields from superclasses
|
||||||
let mut data = HashMap::new();
|
let mut data = HashMap::new();
|
||||||
for f in &class.fields {
|
for f in &class.fields {
|
||||||
|
|
@ -91,7 +90,7 @@ impl Vm {
|
||||||
"L" => Value::Null,
|
"L" => Value::Null,
|
||||||
_ => Value::Void,
|
_ => Value::Void,
|
||||||
};
|
};
|
||||||
data.insert(f.name_index, Arc::new(value));
|
data.insert(f.name_index, Rc::new(value));
|
||||||
}
|
}
|
||||||
Object::new(class.clone(), data)
|
Object::new(class.clone(), data)
|
||||||
}
|
}
|
||||||
|
|
@ -101,8 +100,8 @@ impl Vm {
|
||||||
&mut self,
|
&mut self,
|
||||||
class_name: &str,
|
class_name: &str,
|
||||||
method_name: &str,
|
method_name: &str,
|
||||||
args: Vec<Arc<Value>>,
|
args: Vec<Rc<Value>>,
|
||||||
) -> Result<Arc<Value>, Error> {
|
) -> Result<Rc<Value>, Error> {
|
||||||
println!("execute {}.{}", class_name, method_name);
|
println!("execute {}.{}", class_name, method_name);
|
||||||
let class = self.get_class(class_name)?;
|
let class = self.get_class(class_name)?;
|
||||||
let method = class.get_method(method_name)?;
|
let method = class.get_method(method_name)?;
|
||||||
|
|
@ -119,7 +118,7 @@ impl Vm {
|
||||||
BIPUSH => {
|
BIPUSH => {
|
||||||
println!("BISPUSH");
|
println!("BISPUSH");
|
||||||
let c = code.opcodes[pc] as i32;
|
let c = code.opcodes[pc] as i32;
|
||||||
self.local_stack().push(Arc::new(Value::I32(c)));
|
self.local_stack().push(Rc::new(Value::I32(c)));
|
||||||
pc += 1;
|
pc += 1;
|
||||||
}
|
}
|
||||||
LDC => {
|
LDC => {
|
||||||
|
|
@ -127,10 +126,10 @@ impl Vm {
|
||||||
let cp_index = read_u8(&code.opcodes, pc) as u16;
|
let cp_index = read_u8(&code.opcodes, pc) as u16;
|
||||||
match method.constant_pool.get(&cp_index).unwrap() {
|
match method.constant_pool.get(&cp_index).unwrap() {
|
||||||
CpEntry::Integer(i) => {
|
CpEntry::Integer(i) => {
|
||||||
self.local_stack().push(Arc::new(Value::I32(*i)));
|
self.local_stack().push(Rc::new(Value::I32(*i)));
|
||||||
}
|
}
|
||||||
CpEntry::Float(f) => {
|
CpEntry::Float(f) => {
|
||||||
self.local_stack().push(Arc::new(Value::F32(*f)));
|
self.local_stack().push(Rc::new(Value::F32(*f)));
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -140,10 +139,10 @@ impl Vm {
|
||||||
let cp_index = read_u16(&code.opcodes, pc);
|
let cp_index = read_u16(&code.opcodes, pc);
|
||||||
match method.constant_pool.get(&cp_index).unwrap() {
|
match method.constant_pool.get(&cp_index).unwrap() {
|
||||||
CpEntry::Integer(i) => {
|
CpEntry::Integer(i) => {
|
||||||
self.local_stack().push(Arc::new(Value::I32(*i)));
|
self.local_stack().push(Rc::new(Value::I32(*i)));
|
||||||
}
|
}
|
||||||
CpEntry::Float(f) => {
|
CpEntry::Float(f) => {
|
||||||
self.local_stack().push(Arc::new(Value::F32(*f)));
|
self.local_stack().push(Rc::new(Value::F32(*f)));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
panic!("unexpected")
|
panic!("unexpected")
|
||||||
|
|
@ -155,10 +154,10 @@ impl Vm {
|
||||||
let cp_index = read_u16(&code.opcodes, pc);
|
let cp_index = read_u16(&code.opcodes, pc);
|
||||||
match method.constant_pool.get(&cp_index).unwrap() {
|
match method.constant_pool.get(&cp_index).unwrap() {
|
||||||
CpEntry::Double(d) => {
|
CpEntry::Double(d) => {
|
||||||
self.local_stack().push(Arc::new(Value::F64(*d)));
|
self.local_stack().push(Rc::new(Value::F64(*d)));
|
||||||
}
|
}
|
||||||
CpEntry::Long(l) => {
|
CpEntry::Long(l) => {
|
||||||
self.local_stack().push(Arc::new(Value::I64(*l)));
|
self.local_stack().push(Rc::new(Value::I64(*l)));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
panic!("unexpected")
|
panic!("unexpected")
|
||||||
|
|
@ -221,7 +220,7 @@ impl Vm {
|
||||||
RETURN_VOID => {
|
RETURN_VOID => {
|
||||||
println!("return");
|
println!("return");
|
||||||
self.stack.pop();
|
self.stack.pop();
|
||||||
return Ok(Arc::new(Void));
|
return Ok(Rc::new(Void));
|
||||||
}
|
}
|
||||||
GETFIELD => {
|
GETFIELD => {
|
||||||
println!("GETFIELD");
|
println!("GETFIELD");
|
||||||
|
|
@ -303,8 +302,8 @@ impl Vm {
|
||||||
{
|
{
|
||||||
println!("new {}", new_class);
|
println!("new {}", new_class);
|
||||||
let class = self.get_class(new_class)?;
|
let class = self.get_class(new_class)?;
|
||||||
let object = Arc::new(self.new_instance(class));
|
let object = Rc::new(self.new_instance(class));
|
||||||
self.local_stack().push(Arc::new(Value::Ref(object.clone())));
|
self.local_stack().push(Rc::new(Value::Ref(object.clone())));
|
||||||
self.heap.new_object(object);
|
self.heap.new_object(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue