use crate::vm::field::DvmField; use crate::vm::function::DvmFunction; use crate::vm::interface::DmInterface; use crate::vm::method::DvmMethod; use crate::vm::object::DvmObject; use std::collections::HashMap; use std::fmt::Debug; use std::rc::Rc; #[derive(Debug, Eq)] pub struct DvmImplementation { fqn: String, interface: Option>, static_functions: HashMap>, methods: HashMap>, fields: HashMap>, } impl PartialEq for DvmImplementation { fn eq(&self, other: &Self) -> bool { self.fqn == other.fqn } } impl DvmImplementation { pub fn new(fqn: &str, interface: Option>) -> Self { DvmImplementation { fqn: fqn.to_string(), interface, static_functions: HashMap::new(), methods: HashMap::new(), fields: HashMap::new(), } } pub fn fqn(&self) -> &str { self.fqn.as_str() } pub fn add_function(&mut self, function: DvmFunction) { self.static_functions .insert(function.fqn().to_string(), Rc::new(function)); } pub fn add_method(&mut self, dvm_method: DvmMethod) { self.methods .insert(dvm_method.fqn().to_string(), Rc::new(dvm_method)); } pub fn find_method(&self, name: &str, self_object: &DvmObject) -> Option> { self.methods.get(name).cloned() } pub fn add_field(&mut self, dm_field: DvmField) { self.fields .insert(dm_field.name().to_string(), Rc::new(dm_field)); } pub fn find_field(&self, name: &str, self_object: &DvmObject) -> Option> { self.fields.get(name).cloned() } pub fn field_count(&self) -> usize { self.fields.len() } }