Rename DmAllocObject to DvmObject.

This commit is contained in:
Jesse Brault 2024-12-28 10:23:05 -06:00
parent 652c1dd7f2
commit 376ac2fa3a
6 changed files with 55 additions and 42 deletions

View File

@ -1,4 +1,4 @@
use crate::vm::object::DmAllocObject; use crate::vm::object::DvmObject;
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -8,7 +8,7 @@ pub enum DvmValue {
Long(i64), Long(i64),
Double(f64), Double(f64),
Boolean(bool), Boolean(bool),
Object(Rc<DmAllocObject>), Object(Rc<DvmObject>),
USize(usize), USize(usize),
Uninit, Uninit,
Void, Void,
@ -23,7 +23,7 @@ impl DvmValue {
} }
} }
pub fn expect_object(&self) -> Rc<DmAllocObject> { pub fn expect_object(&self) -> Rc<DvmObject> {
if let DvmValue::Object(o) = self { if let DvmValue::Object(o) = self {
o.clone() o.clone()
} else { } else {

View File

@ -1,10 +1,10 @@
use crate::vm::dm_type::DmType; use crate::vm::dm_type::DmType;
use crate::vm::dvm_value::DvmValue; use crate::vm::dvm_value::DvmValue;
use crate::vm::object::DmAllocObject; use crate::vm::object::DvmObject;
use crate::vm::object_type::DmField; use crate::vm::object_type::DmField;
use std::rc::Rc; use std::rc::Rc;
pub unsafe fn get_field_value(dm_field: &DmField, self_object: &DmAllocObject) -> DvmValue { pub unsafe fn get_field_value(dm_field: &DmField, self_object: &DvmObject) -> DvmValue {
let data_size = dm_field.dm_type().size_in_bytes(); let data_size = dm_field.dm_type().size_in_bytes();
let mut raw_data: Vec<u8> = Vec::with_capacity(data_size); let mut raw_data: Vec<u8> = Vec::with_capacity(data_size);
for i in dm_field.data_offset()..(dm_field.data_offset() + data_size) { for i in dm_field.data_offset()..(dm_field.data_offset() + data_size) {
@ -25,7 +25,7 @@ pub unsafe fn get_field_value(dm_field: &DmField, self_object: &DmAllocObject) -
DmType::Object => { DmType::Object => {
// read the pointer's (address) value // read the pointer's (address) value
let address = usize::from_ne_bytes(raw_data[0..data_size].try_into().unwrap()); let address = usize::from_ne_bytes(raw_data[0..data_size].try_into().unwrap());
DvmValue::Object(Rc::from_raw(address as *const DmAllocObject)) DvmValue::Object(Rc::from_raw(address as *const DvmObject))
} }
DmType::USize => DvmValue::USize(usize::from_ne_bytes( DmType::USize => DvmValue::USize(usize::from_ne_bytes(
raw_data[0..data_size].try_into().unwrap(), raw_data[0..data_size].try_into().unwrap(),

View File

@ -11,7 +11,7 @@ pub mod util;
use crate::vm::dvm_value::DvmValue; use crate::vm::dvm_value::DvmValue;
use crate::vm::lib::{DmConstant, DmLib}; use crate::vm::lib::{DmConstant, DmLib};
use crate::vm::object::DmAllocObject; use crate::vm::object::DvmObject;
use crate::vm::object_type::DmFn; use crate::vm::object_type::DmFn;
use op_codes::*; use op_codes::*;
use std::alloc::{alloc, dealloc, Layout}; use std::alloc::{alloc, dealloc, Layout};
@ -299,14 +299,14 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
let source_register = next_8!(iter, usize); let source_register = next_8!(iter, usize);
let offset = next_usize_le!(iter); let offset = next_usize_le!(iter);
let source_object = state.registers let source_object = state
.registers
.get(source_register) .get(source_register)
.unwrap() .unwrap()
.expect_object(); .expect_object();
let object = unsafe { let object =
Rc::from_raw(source_object.data.add(offset).cast::<DmAllocObject>()) unsafe { Rc::from_raw(source_object.data.add(offset).cast::<DvmObject>()) };
};
state.registers[target_register] = DvmValue::Object(object); state.registers[target_register] = DvmValue::Object(object);
} }
@ -321,7 +321,8 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
.unwrap() .unwrap()
.expect_object(); .expect_object();
let source_value = std::mem::replace(&mut state.registers[source_register], DvmValue::Uninit); let source_value =
std::mem::replace(&mut state.registers[source_register], DvmValue::Uninit);
match source_value { match source_value {
DvmValue::Int(i) => { DvmValue::Int(i) => {
@ -373,7 +374,7 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
}) })
.expect(&format!("Implementation not found: {}", impl_name)); .expect(&format!("Implementation not found: {}", impl_name));
let dm_alloc_object = DmAllocObject::new(alloc_size, implementation.clone()); let dm_alloc_object = DvmObject::new(alloc_size, implementation.clone());
state.registers[target_register] = DvmValue::Object(Rc::new(dm_alloc_object)); state.registers[target_register] = DvmValue::Object(Rc::new(dm_alloc_object));
} }
DEALLOC => { DEALLOC => {
@ -590,9 +591,9 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
#[cfg(test)] #[cfg(test)]
mod run_code_tests { mod run_code_tests {
use super::*;
use crate::vm::dm_type::DmType; use crate::vm::dm_type::DmType;
use crate::vm::object_type::{DmField, DmImplementation}; use crate::vm::object_type::{DmField, DmImplementation};
use super::*;
macro_rules! assert_register { macro_rules! assert_register {
( $expected: expr, $state: expr, $register_number: expr ) => { ( $expected: expr, $state: expr, $register_number: expr ) => {
@ -656,15 +657,27 @@ mod run_code_tests {
fn load_object() { fn load_object() {
let dummy_impl = impl_with_object_field(); let dummy_impl = impl_with_object_field();
let dummy_impl_rc = Rc::new(dummy_impl); let dummy_impl_rc = Rc::new(dummy_impl);
let dummy_impl_object_field = dummy_impl_rc.fields.iter().find(|field| { let dummy_impl_object_field = dummy_impl_rc
field.name() == "object_field" .fields
}).unwrap(); .iter()
.find(|field| field.name() == "object_field")
.unwrap();
let mut dummy_lib = DmLib::new("dummy"); let mut dummy_lib = DmLib::new("dummy");
dummy_lib.implementations.push(dummy_impl_rc.clone()); dummy_lib.implementations.push(dummy_impl_rc.clone());
let mut code = Vec::new(); let mut code = Vec::new();
add_alloc(&mut code, 0, dummy_impl_rc.size_in_bytes() as u32, &dummy_impl_rc.fqn); add_alloc(
add_alloc(&mut code, 1, dummy_impl_rc.size_in_bytes() as u32, &dummy_impl_rc.fqn); &mut code,
0,
dummy_impl_rc.size_in_bytes() as u32,
&dummy_impl_rc.fqn,
);
add_alloc(
&mut code,
1,
dummy_impl_rc.size_in_bytes() as u32,
&dummy_impl_rc.fqn,
);
add_store(&mut code, 0, dummy_impl_object_field.data_offset(), 1); add_store(&mut code, 0, dummy_impl_object_field.data_offset(), 1);
add_load_object(&mut code, 2, 0, dummy_impl_object_field.data_offset()); add_load_object(&mut code, 2, 0, dummy_impl_object_field.data_offset());

View File

@ -4,7 +4,7 @@ use std::collections::HashSet;
use std::rc::Rc; use std::rc::Rc;
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub struct DmAllocObject { pub struct DvmObject {
pub data: *mut u8, pub data: *mut u8,
pub units: HashSet<usize>, pub units: HashSet<usize>,
pub size: usize, pub size: usize,
@ -12,10 +12,10 @@ pub struct DmAllocObject {
pub implementation: Rc<DmImplementation>, pub implementation: Rc<DmImplementation>,
} }
impl DmAllocObject { impl DvmObject {
pub fn new(size: usize, implementation: Rc<DmImplementation>) -> Self { pub fn new(size: usize, implementation: Rc<DmImplementation>) -> Self {
let layout = Layout::from_size_align(size, 1).unwrap(); let layout = Layout::from_size_align(size, 1).unwrap();
DmAllocObject { DvmObject {
data: unsafe { alloc(layout) }, data: unsafe { alloc(layout) },
units: HashSet::new(), units: HashSet::new(),
size, size,
@ -25,7 +25,7 @@ impl DmAllocObject {
} }
} }
impl Drop for DmAllocObject { impl Drop for DvmObject {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
dealloc(self.data, self.layout); dealloc(self.data, self.layout);

View File

@ -1,5 +1,5 @@
use crate::vm::dm_type::DmType; use crate::vm::dm_type::DmType;
use crate::vm::object::DmAllocObject; use crate::vm::object::DvmObject;
use std::fmt::Debug; use std::fmt::Debug;
use std::rc::Rc; use std::rc::Rc;
@ -131,7 +131,7 @@ impl DmInterface {
self.virtual_methods.push(Rc::new(dm_virtual_method)); self.virtual_methods.push(Rc::new(dm_virtual_method));
} }
pub fn get_method(&self, name: &str, self_object: &DmAllocObject) -> Option<Rc<DmMethod>> { pub fn get_method(&self, name: &str, self_object: &DvmObject) -> Option<Rc<DmMethod>> {
if self if self
.virtual_methods .virtual_methods
.iter() .iter()
@ -147,7 +147,7 @@ impl DmInterface {
self.virtual_methods.clone() self.virtual_methods.clone()
} }
pub fn get_property(&self, name: &str, self_object: &DmAllocObject) -> Option<&DmProperty> { pub fn get_property(&self, name: &str, self_object: &DvmObject) -> Option<&DmProperty> {
todo!() todo!()
} }
} }
@ -196,7 +196,7 @@ impl DmImplementation {
self.interface.clone() self.interface.clone()
} }
pub fn get_method(&self, name: &str, self_object: &DmAllocObject) -> Option<Rc<DmMethod>> { pub fn get_method(&self, name: &str, self_object: &DvmObject) -> Option<Rc<DmMethod>> {
for method in &self.methods { for method in &self.methods {
if method.dm_fn.fqn == name { if method.dm_fn.fqn == name {
return Some(method.clone()); return Some(method.clone());
@ -209,11 +209,11 @@ impl DmImplementation {
None None
} }
pub fn get_property(&self, name: &str, self_object: &DmAllocObject) -> Option<Rc<DmProperty>> { pub fn get_property(&self, name: &str, self_object: &DvmObject) -> Option<Rc<DmProperty>> {
todo!() todo!()
} }
pub fn get_field(&self, name: &str, self_object: &DmAllocObject) -> Option<&DmField> { pub fn get_field(&self, name: &str, self_object: &DvmObject) -> Option<&DmField> {
self.fields.iter().find(|field| field.name == name) self.fields.iter().find(|field| field.name == name)
} }

View File

@ -1,6 +1,6 @@
use crate::vm::dvm_value::DvmValue; use crate::vm::dvm_value::DvmValue;
use crate::vm::mem::get_field_value; use crate::vm::mem::get_field_value;
use crate::vm::object::DmAllocObject; use crate::vm::object::DvmObject;
use crate::vm::{DvmContext, DvmState}; use crate::vm::{DvmContext, DvmState};
use std::rc::Rc; use std::rc::Rc;
@ -38,7 +38,7 @@ unsafe fn get_string(dvm_value: &DvmValue) -> String {
} }
} }
fn object_to_string(alloc_object_rc: Rc<DmAllocObject>) -> String { fn object_to_string(alloc_object_rc: Rc<DvmObject>) -> String {
if alloc_object_rc.implementation.fqn == "std::core::StringImpl" { if alloc_object_rc.implementation.fqn == "std::core::StringImpl" {
extract_string_from_string(alloc_object_rc.clone()) extract_string_from_string(alloc_object_rc.clone())
} else { } else {
@ -46,7 +46,7 @@ fn object_to_string(alloc_object_rc: Rc<DmAllocObject>) -> String {
} }
} }
fn extract_string_from_string(string_object: Rc<DmAllocObject>) -> String { fn extract_string_from_string(string_object: Rc<DvmObject>) -> String {
let bytes_field = string_object let bytes_field = string_object
.implementation .implementation
.get_field("bytes", &string_object) .get_field("bytes", &string_object)