deimos-lang/src/vm/implementation.rs
2025-04-14 07:59:51 -05:00

67 lines
1.8 KiB
Rust

use crate::vm::field::DvmField;
use crate::vm::function::DvmFunction;
use crate::vm::interface::DvmInterface;
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<Rc<DvmInterface>>,
static_functions: HashMap<String, Rc<DvmFunction>>,
methods: HashMap<String, Rc<DvmMethod>>,
fields: HashMap<String, Rc<DvmField>>,
}
impl PartialEq for DvmImplementation {
fn eq(&self, other: &Self) -> bool {
self.fqn == other.fqn
}
}
impl DvmImplementation {
pub fn new(fqn: &str, interface: Option<Rc<DvmInterface>>) -> 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<Rc<DvmMethod>> {
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<Rc<DvmField>> {
self.fields.get(name).cloned()
}
pub fn field_count(&self) -> usize {
self.fields.len()
}
}