use std::cell::{RefCell, UnsafeCell}; use crate::class::{Class, Value}; use std::collections::HashMap; use std::fmt; use std::rc::Rc; use std::sync::Arc; use crate::classloader::CpEntry; pub struct Object { // locked: bool, // hashcode: i32, pub class: Rc, pub data: HashMap>>, //TODO optimize }//arrays #[derive(Debug)] pub enum ObjectRef{ ByteArray(Vec), ShortArray(Vec), IntArray(Vec), LongArray(Vec), FloatArray(Vec), DoubleArray(Vec), BooleanArray(Vec), CharArray(Vec), ObjectArray(Vec), Object(Object), } unsafe impl Send for Object {} unsafe impl Sync for Object {} impl Object { pub fn new(class: Rc, data: HashMap>>) -> Self { Self { class, data } } fn get_field(&self, cp_index: &u16) -> &str { if let CpEntry::Utf8(name) = self.class.constant_pool.get(cp_index).unwrap() { return name; } panic!() } unsafe fn get_mut(ptr: &UnsafeCell) -> &mut T { unsafe { &mut *ptr.get() } } } impl fmt::Debug for Object { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let fields: Vec = self.data.iter().map(|(k, v)| { let mut r: String = self.get_field(k).into(); r.push(':'); r.push_str(format!("{:?}", v).as_str()); r } ).collect(); write!( f, "{} {{ {:?} }}", self.class.get_name(), fields ) } } pub(crate) struct Heap { objects: Vec>>, } impl Heap { pub fn new() -> Self { Self { objects: vec![] } } pub(crate) fn new_object(&mut self, object: Arc>) { self.objects.push(object); } }