Rename DmAllocObject to DvmObject.
This commit is contained in:
parent
652c1dd7f2
commit
376ac2fa3a
@ -1,4 +1,4 @@
|
||||
use crate::vm::object::DmAllocObject;
|
||||
use crate::vm::object::DvmObject;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
@ -8,7 +8,7 @@ pub enum DvmValue {
|
||||
Long(i64),
|
||||
Double(f64),
|
||||
Boolean(bool),
|
||||
Object(Rc<DmAllocObject>),
|
||||
Object(Rc<DvmObject>),
|
||||
USize(usize),
|
||||
Uninit,
|
||||
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 {
|
||||
o.clone()
|
||||
} else {
|
||||
|
@ -1,10 +1,10 @@
|
||||
use crate::vm::dm_type::DmType;
|
||||
use crate::vm::dvm_value::DvmValue;
|
||||
use crate::vm::object::DmAllocObject;
|
||||
use crate::vm::object::DvmObject;
|
||||
use crate::vm::object_type::DmField;
|
||||
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 mut raw_data: Vec<u8> = Vec::with_capacity(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 => {
|
||||
// read the pointer's (address) value
|
||||
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(
|
||||
raw_data[0..data_size].try_into().unwrap(),
|
||||
|
@ -11,7 +11,7 @@ pub mod util;
|
||||
|
||||
use crate::vm::dvm_value::DvmValue;
|
||||
use crate::vm::lib::{DmConstant, DmLib};
|
||||
use crate::vm::object::DmAllocObject;
|
||||
use crate::vm::object::DvmObject;
|
||||
use crate::vm::object_type::DmFn;
|
||||
use op_codes::*;
|
||||
use std::alloc::{alloc, dealloc, Layout};
|
||||
@ -298,16 +298,16 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
|
||||
let target_register = next_8!(iter, usize);
|
||||
let source_register = next_8!(iter, usize);
|
||||
let offset = next_usize_le!(iter);
|
||||
|
||||
let source_object = state.registers
|
||||
|
||||
let source_object = state
|
||||
.registers
|
||||
.get(source_register)
|
||||
.unwrap()
|
||||
.expect_object();
|
||||
|
||||
let object = unsafe {
|
||||
Rc::from_raw(source_object.data.add(offset).cast::<DmAllocObject>())
|
||||
};
|
||||
|
||||
|
||||
let object =
|
||||
unsafe { Rc::from_raw(source_object.data.add(offset).cast::<DvmObject>()) };
|
||||
|
||||
state.registers[target_register] = DvmValue::Object(object);
|
||||
}
|
||||
STORE => {
|
||||
@ -320,9 +320,10 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
|
||||
.get(target_register)
|
||||
.unwrap()
|
||||
.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 {
|
||||
DvmValue::Int(i) => {
|
||||
write_bytes!(target_alloc_object.data, offset, i.to_ne_bytes());
|
||||
@ -373,7 +374,7 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
|
||||
})
|
||||
.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));
|
||||
}
|
||||
DEALLOC => {
|
||||
@ -590,9 +591,9 @@ pub fn run_byte_code(state: &mut DvmState, context: &DvmContext, byte_code: &[u8
|
||||
|
||||
#[cfg(test)]
|
||||
mod run_code_tests {
|
||||
use super::*;
|
||||
use crate::vm::dm_type::DmType;
|
||||
use crate::vm::object_type::{DmField, DmImplementation};
|
||||
use super::*;
|
||||
|
||||
macro_rules! assert_register {
|
||||
( $expected: expr, $state: expr, $register_number: expr ) => {
|
||||
@ -606,7 +607,7 @@ mod run_code_tests {
|
||||
fn setup() -> (DvmState, DvmContext) {
|
||||
(DvmState::new(), DvmContext::new())
|
||||
}
|
||||
|
||||
|
||||
fn impl_with_object_field() -> DmImplementation {
|
||||
let mut dm_impl = DmImplementation::new("ImplWithObjectField", "ImplWithObjectField", None);
|
||||
let object_field = DmField::new("object_field", DmType::Object, 0);
|
||||
@ -651,28 +652,40 @@ mod run_code_tests {
|
||||
run_byte_code(&mut state, &context, &code);
|
||||
assert_register!(DvmValue::Int(1), state, 0);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn load_object() {
|
||||
let dummy_impl = impl_with_object_field();
|
||||
let dummy_impl_rc = Rc::new(dummy_impl);
|
||||
let dummy_impl_object_field = dummy_impl_rc.fields.iter().find(|field| {
|
||||
field.name() == "object_field"
|
||||
}).unwrap();
|
||||
let dummy_impl_object_field = dummy_impl_rc
|
||||
.fields
|
||||
.iter()
|
||||
.find(|field| field.name() == "object_field")
|
||||
.unwrap();
|
||||
let mut dummy_lib = DmLib::new("dummy");
|
||||
dummy_lib.implementations.push(dummy_impl_rc.clone());
|
||||
|
||||
|
||||
let mut code = Vec::new();
|
||||
add_alloc(&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_alloc(
|
||||
&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_load_object(&mut code, 2, 0, dummy_impl_object_field.data_offset());
|
||||
|
||||
|
||||
let (mut state, mut context) = setup();
|
||||
state.registers.resize(3, DvmValue::Uninit);
|
||||
context.load_libs(vec![Rc::new(dummy_lib)]);
|
||||
run_byte_code(&mut state, &context, &code);
|
||||
|
||||
|
||||
let referenced_object = state.registers.get(1).unwrap().expect_object();
|
||||
assert_register!(DvmValue::Object(referenced_object), state, 2);
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use std::collections::HashSet;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct DmAllocObject {
|
||||
pub struct DvmObject {
|
||||
pub data: *mut u8,
|
||||
pub units: HashSet<usize>,
|
||||
pub size: usize,
|
||||
@ -12,10 +12,10 @@ pub struct DmAllocObject {
|
||||
pub implementation: Rc<DmImplementation>,
|
||||
}
|
||||
|
||||
impl DmAllocObject {
|
||||
impl DvmObject {
|
||||
pub fn new(size: usize, implementation: Rc<DmImplementation>) -> Self {
|
||||
let layout = Layout::from_size_align(size, 1).unwrap();
|
||||
DmAllocObject {
|
||||
DvmObject {
|
||||
data: unsafe { alloc(layout) },
|
||||
units: HashSet::new(),
|
||||
size,
|
||||
@ -25,7 +25,7 @@ impl DmAllocObject {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DmAllocObject {
|
||||
impl Drop for DvmObject {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
dealloc(self.data, self.layout);
|
||||
|
@ -1,5 +1,5 @@
|
||||
use crate::vm::dm_type::DmType;
|
||||
use crate::vm::object::DmAllocObject;
|
||||
use crate::vm::object::DvmObject;
|
||||
use std::fmt::Debug;
|
||||
use std::rc::Rc;
|
||||
|
||||
@ -131,7 +131,7 @@ impl DmInterface {
|
||||
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
|
||||
.virtual_methods
|
||||
.iter()
|
||||
@ -147,7 +147,7 @@ impl DmInterface {
|
||||
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!()
|
||||
}
|
||||
}
|
||||
@ -196,7 +196,7 @@ impl DmImplementation {
|
||||
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 {
|
||||
if method.dm_fn.fqn == name {
|
||||
return Some(method.clone());
|
||||
@ -209,11 +209,11 @@ impl DmImplementation {
|
||||
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!()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::vm::dvm_value::DvmValue;
|
||||
use crate::vm::mem::get_field_value;
|
||||
use crate::vm::object::DmAllocObject;
|
||||
use crate::vm::object::DvmObject;
|
||||
use crate::vm::{DvmContext, DvmState};
|
||||
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" {
|
||||
extract_string_from_string(alloc_object_rc.clone())
|
||||
} 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
|
||||
.implementation
|
||||
.get_field("bytes", &string_object)
|
||||
|
Loading…
Reference in New Issue
Block a user