Unify heap objects with typed headers and bootstrap Type
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m16s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m16s
This commit is contained in:
@@ -42,8 +42,8 @@ pub enum Value {
|
||||
Object(ObjectRef),
|
||||
}
|
||||
|
||||
/// The class object and its immutable description are one heap allocation.
|
||||
/// There is deliberately no metaclass or implicit class-of-class relationship.
|
||||
/// Immutable class metadata, stored in a language object's payload.
|
||||
/// This Rust structure is not a language object on its own.
|
||||
#[derive(Debug)]
|
||||
pub struct Class {
|
||||
pub(crate) name: String,
|
||||
@@ -62,26 +62,59 @@ impl Class {
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Instance {
|
||||
pub(crate) class: ClassRef,
|
||||
pub(crate) fields: BTreeMap<String, Value>,
|
||||
}
|
||||
|
||||
impl Instance {
|
||||
pub fn class(&self) -> ClassRef {
|
||||
self.class
|
||||
}
|
||||
|
||||
pub fn field(&self, name: &str) -> Option<&Value> {
|
||||
self.fields.get(name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Common header for every heap object, including class objects.
|
||||
/// The class handle is immutable and is always local to the owning VM.
|
||||
#[derive(Debug)]
|
||||
pub enum Object {
|
||||
pub struct ObjectHeader {
|
||||
class: ClassRef,
|
||||
}
|
||||
|
||||
impl ObjectHeader {
|
||||
pub fn class(&self) -> ClassRef {
|
||||
self.class
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ObjectData {
|
||||
Class(Class),
|
||||
Instance(Instance),
|
||||
}
|
||||
|
||||
/// A language object consists of a common header and a kind-specific payload.
|
||||
/// Object links use checked handles rather than addresses into the arena.
|
||||
#[derive(Debug)]
|
||||
pub struct Object {
|
||||
header: ObjectHeader,
|
||||
pub(crate) data: ObjectData,
|
||||
}
|
||||
|
||||
impl Object {
|
||||
pub(crate) fn new(class: ClassRef, data: ObjectData) -> Self {
|
||||
Self {
|
||||
header: ObjectHeader { class },
|
||||
data,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn header(&self) -> &ObjectHeader {
|
||||
&self.header
|
||||
}
|
||||
|
||||
pub fn data(&self) -> &ObjectData {
|
||||
&self.data
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocation-only arena. No deletion or slot reuse until a GC model is chosen.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Heap {
|
||||
@@ -90,6 +123,35 @@ pub(crate) struct Heap {
|
||||
}
|
||||
|
||||
impl Heap {
|
||||
/// Builds both foundational classes before exposing the heap. The two
|
||||
/// private handles permit Type's self-reference without null placeholders.
|
||||
pub(crate) fn bootstrap() -> Result<(Self, ClassRef, ClassRef), VmError> {
|
||||
let mut heap = Self::new()?;
|
||||
let root = ClassRef(ObjectRef {
|
||||
heap_id: heap.id,
|
||||
index: 0,
|
||||
});
|
||||
let type_class = ClassRef(ObjectRef {
|
||||
heap_id: heap.id,
|
||||
index: 1,
|
||||
});
|
||||
heap.allocate(Object::new(
|
||||
type_class,
|
||||
ObjectData::Class(Class {
|
||||
name: "Object".into(),
|
||||
parent: None,
|
||||
}),
|
||||
));
|
||||
heap.allocate(Object::new(
|
||||
type_class,
|
||||
ObjectData::Class(Class {
|
||||
name: "Type".into(),
|
||||
parent: Some(root),
|
||||
}),
|
||||
));
|
||||
Ok((heap, root, type_class))
|
||||
}
|
||||
|
||||
pub(crate) fn new() -> Result<Self, VmError> {
|
||||
let id = NEXT_HEAP_ID
|
||||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
|
||||
@@ -165,6 +227,16 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_lookup_rejects_an_invalid_slot() {
|
||||
let vm = crate::Vm::new().unwrap();
|
||||
let invalid = ObjectRef {
|
||||
heap_id: vm.root_class().0.heap_id,
|
||||
index: usize::MAX,
|
||||
};
|
||||
assert_eq!(vm.type_of(invalid), Err(VmError::InvalidHandle));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_value_reference_cannot_replace_a_field() {
|
||||
let mut vm = crate::Vm::new().unwrap();
|
||||
|
||||
@@ -6,12 +6,15 @@
|
||||
//! let point = vm.create_class("Point", vm.root_class())?;
|
||||
//! let instance = vm.create_instance(point)?;
|
||||
//! vm.set_field(instance, "x", Value::Integer(42))?;
|
||||
//! assert_eq!(vm.instance(instance)?.class(), point);
|
||||
//! assert_eq!(vm.type_of(instance.into())?, point);
|
||||
//! assert_eq!(vm.type_of(point.into())?, vm.type_class());
|
||||
//! # Ok::<(), aster_core::VmError>(())
|
||||
//! ```
|
||||
|
||||
mod heap;
|
||||
mod vm;
|
||||
|
||||
pub use heap::{Class, ClassRef, Instance, InstanceRef, Object, ObjectRef, Value};
|
||||
pub use heap::{
|
||||
Class, ClassRef, Instance, InstanceRef, Object, ObjectData, ObjectHeader, ObjectRef, Value,
|
||||
};
|
||||
pub use vm::{Vm, VmError};
|
||||
|
||||
+50
-28
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
|
||||
use crate::heap::Heap;
|
||||
use crate::{Class, ClassRef, Instance, InstanceRef, Object, ObjectRef, Value};
|
||||
use crate::{Class, ClassRef, Instance, InstanceRef, Object, ObjectData, ObjectRef, Value};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum VmError {
|
||||
@@ -12,6 +12,7 @@ pub enum VmError {
|
||||
ExpectedInstance,
|
||||
EmptyClassName,
|
||||
ReservedClassName,
|
||||
UnsupportedMetaclass,
|
||||
IdentityExhausted,
|
||||
}
|
||||
|
||||
@@ -23,7 +24,8 @@ impl fmt::Display for VmError {
|
||||
Self::ExpectedClass => "object is not a class",
|
||||
Self::ExpectedInstance => "object is not an instance",
|
||||
Self::EmptyClassName => "class name cannot be blank",
|
||||
Self::ReservedClassName => "Object is reserved for the root class",
|
||||
Self::ReservedClassName => "Object and Type are reserved for the bootstrap classes",
|
||||
Self::UnsupportedMetaclass => "Type only supports class creation through create_class",
|
||||
Self::IdentityExhausted => "process-local VM identities exhausted",
|
||||
})
|
||||
}
|
||||
@@ -31,22 +33,24 @@ impl fmt::Display for VmError {
|
||||
|
||||
impl std::error::Error for VmError {}
|
||||
|
||||
/// Owns its heap and exactly one root class. Not Clone: handles have one owner.
|
||||
/// Owns its heap, the root Object class and its Type metaclass.
|
||||
/// Not Clone: handles have one owner.
|
||||
#[derive(Debug)]
|
||||
pub struct Vm {
|
||||
heap: Heap,
|
||||
root: ClassRef,
|
||||
type_class: ClassRef,
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
/// Bootstraps Object before returning. Does not perform any host I/O.
|
||||
/// Bootstraps Object and Type before returning. Does not perform host I/O.
|
||||
pub fn new() -> Result<Self, VmError> {
|
||||
let mut heap = Heap::new()?;
|
||||
let root = ClassRef(heap.allocate(Object::Class(Class {
|
||||
name: "Object".into(),
|
||||
parent: None,
|
||||
})));
|
||||
Ok(Self { heap, root })
|
||||
let (heap, root, type_class) = Heap::bootstrap()?;
|
||||
Ok(Self {
|
||||
heap,
|
||||
root,
|
||||
type_class,
|
||||
})
|
||||
}
|
||||
|
||||
/// Idempotent: construction has already performed bootstrap exactly once.
|
||||
@@ -58,6 +62,10 @@ impl Vm {
|
||||
self.root
|
||||
}
|
||||
|
||||
pub fn type_class(&self) -> ClassRef {
|
||||
self.type_class
|
||||
}
|
||||
|
||||
pub fn object_count(&self) -> usize {
|
||||
self.heap.len()
|
||||
}
|
||||
@@ -70,26 +78,35 @@ impl Vm {
|
||||
parent: ClassRef,
|
||||
) -> Result<ClassRef, VmError> {
|
||||
self.class(parent)?;
|
||||
if parent == self.type_class {
|
||||
return Err(VmError::UnsupportedMetaclass);
|
||||
}
|
||||
let name = name.into();
|
||||
if name.trim().is_empty() {
|
||||
return Err(VmError::EmptyClassName);
|
||||
}
|
||||
if name == "Object" {
|
||||
if name == "Object" || name == "Type" {
|
||||
return Err(VmError::ReservedClassName);
|
||||
}
|
||||
Ok(ClassRef(self.heap.allocate(Object::Class(Class {
|
||||
name,
|
||||
parent: Some(parent),
|
||||
}))))
|
||||
Ok(ClassRef(self.heap.allocate(Object::new(
|
||||
self.type_class,
|
||||
ObjectData::Class(Class {
|
||||
name,
|
||||
parent: Some(parent),
|
||||
}),
|
||||
))))
|
||||
}
|
||||
|
||||
pub fn create_instance(&mut self, class: ClassRef) -> Result<InstanceRef, VmError> {
|
||||
self.class(class)?;
|
||||
Ok(InstanceRef(self.heap.allocate(Object::Instance(
|
||||
Instance {
|
||||
class,
|
||||
if class == self.type_class {
|
||||
return Err(VmError::UnsupportedMetaclass);
|
||||
}
|
||||
Ok(InstanceRef(self.heap.allocate(Object::new(
|
||||
class,
|
||||
ObjectData::Instance(Instance {
|
||||
fields: BTreeMap::new(),
|
||||
},
|
||||
}),
|
||||
))))
|
||||
}
|
||||
|
||||
@@ -97,17 +114,22 @@ impl Vm {
|
||||
self.heap.get(reference)
|
||||
}
|
||||
|
||||
/// Reads the same common header for class objects and ordinary instances.
|
||||
pub fn type_of(&self, reference: ObjectRef) -> Result<ClassRef, VmError> {
|
||||
Ok(self.heap.get(reference)?.header().class())
|
||||
}
|
||||
|
||||
pub fn class(&self, reference: ClassRef) -> Result<&Class, VmError> {
|
||||
match self.heap.get(reference.0)? {
|
||||
Object::Class(class) => Ok(class),
|
||||
Object::Instance(_) => Err(VmError::ExpectedClass),
|
||||
match self.heap.get(reference.0)?.data() {
|
||||
ObjectData::Class(class) => Ok(class),
|
||||
ObjectData::Instance(_) => Err(VmError::ExpectedClass),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn instance(&self, reference: InstanceRef) -> Result<&Instance, VmError> {
|
||||
match self.heap.get(reference.0)? {
|
||||
Object::Instance(instance) => Ok(instance),
|
||||
Object::Class(_) => Err(VmError::ExpectedInstance),
|
||||
match self.heap.get(reference.0)?.data() {
|
||||
ObjectData::Instance(instance) => Ok(instance),
|
||||
ObjectData::Class(_) => Err(VmError::ExpectedInstance),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,12 +157,12 @@ impl Vm {
|
||||
if let Value::Object(reference) = &value {
|
||||
self.heap.get(*reference)?;
|
||||
}
|
||||
match self.heap.get_mut(instance.0)? {
|
||||
Object::Instance(instance) => {
|
||||
match &mut self.heap.get_mut(instance.0)?.data {
|
||||
ObjectData::Instance(instance) => {
|
||||
instance.fields.insert(name.into(), value);
|
||||
Ok(())
|
||||
}
|
||||
Object::Class(_) => Err(VmError::ExpectedInstance),
|
||||
ObjectData::Class(_) => Err(VmError::ExpectedInstance),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use aster_core::{Value, Vm, VmError};
|
||||
use aster_core::{ObjectData, Value, Vm, VmError};
|
||||
|
||||
#[test]
|
||||
fn bootstrap_is_unique_and_root_has_no_parent() {
|
||||
@@ -6,11 +6,30 @@ fn bootstrap_is_unique_and_root_has_no_parent() {
|
||||
let root = vm.root_class();
|
||||
assert_eq!(vm.bootstrap(), root);
|
||||
assert_eq!(vm.bootstrap(), root);
|
||||
assert_eq!(vm.object_count(), 1);
|
||||
assert_eq!(vm.object_count(), 2);
|
||||
assert_eq!(vm.class(root).unwrap().name(), "Object");
|
||||
assert_eq!(vm.class(root).unwrap().parent(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bootstrap_types_have_headers_and_distinct_inheritance_links() {
|
||||
let vm = Vm::new().unwrap();
|
||||
let root = vm.root_class();
|
||||
let type_class = vm.type_class();
|
||||
assert_ne!(root, type_class);
|
||||
assert_eq!(vm.class(type_class).unwrap().name(), "Type");
|
||||
assert_eq!(vm.class(type_class).unwrap().parent(), Some(root));
|
||||
assert_eq!(vm.type_of(root.into()).unwrap(), type_class);
|
||||
assert_eq!(vm.type_of(type_class.into()).unwrap(), type_class);
|
||||
for class in [root, type_class] {
|
||||
let object = vm.object(class.into()).unwrap();
|
||||
assert_eq!(object.header().class(), type_class);
|
||||
assert!(matches!(object.data(), ObjectData::Class(_)));
|
||||
assert_eq!(vm.as_class(class.into()).unwrap(), class);
|
||||
assert_eq!(vm.as_instance(class.into()), Err(VmError::ExpectedInstance));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn factories_preserve_class_links_and_distinct_identity() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
@@ -22,8 +41,13 @@ fn factories_preserve_class_links_and_distinct_identity() {
|
||||
let plain = vm.create_instance(root).unwrap();
|
||||
assert_ne!(first, second);
|
||||
assert_eq!(vm.class(child).unwrap().parent(), Some(class));
|
||||
assert_eq!(vm.instance(first).unwrap().class(), class);
|
||||
assert_eq!(vm.instance(plain).unwrap().class(), root);
|
||||
assert_eq!(vm.type_of(class.into()).unwrap(), vm.type_class());
|
||||
assert_eq!(vm.type_of(child.into()).unwrap(), vm.type_class());
|
||||
assert_eq!(vm.type_of(first.into()).unwrap(), class);
|
||||
assert_eq!(vm.type_of(plain.into()).unwrap(), root);
|
||||
let object = vm.object(first.into()).unwrap();
|
||||
assert_eq!(object.header().class(), class);
|
||||
assert!(matches!(object.data(), ObjectData::Instance(_)));
|
||||
vm.set_field(first, "x", Value::Integer(1)).unwrap();
|
||||
vm.set_field(first, "next", Value::Object(second.into()))
|
||||
.unwrap();
|
||||
@@ -58,6 +82,13 @@ fn foreign_handles_are_rejected_without_mutation() {
|
||||
Err(VmError::ForeignHandle)
|
||||
));
|
||||
assert!(matches!(vm.instance(foreign), Err(VmError::ForeignHandle)));
|
||||
for reference in [
|
||||
foreign.into(),
|
||||
other.root_class().into(),
|
||||
other.type_class().into(),
|
||||
] {
|
||||
assert_eq!(vm.type_of(reference), Err(VmError::ForeignHandle));
|
||||
}
|
||||
assert_eq!(
|
||||
vm.set_field(foreign, "x", Value::Nil),
|
||||
Err(VmError::ForeignHandle)
|
||||
@@ -92,16 +123,35 @@ fn invalid_names_do_not_allocate_and_names_are_not_a_namespace() {
|
||||
vm.create_class(" ", vm.root_class()),
|
||||
Err(VmError::EmptyClassName)
|
||||
);
|
||||
assert_eq!(
|
||||
vm.create_class("Object", vm.root_class()),
|
||||
Err(VmError::ReservedClassName)
|
||||
);
|
||||
assert_eq!(vm.object_count(), 1);
|
||||
for name in ["Object", "Type"] {
|
||||
assert_eq!(
|
||||
vm.create_class(name, vm.root_class()),
|
||||
Err(VmError::ReservedClassName)
|
||||
);
|
||||
}
|
||||
assert_eq!(vm.object_count(), 2);
|
||||
let a = vm.create_class("Label", vm.root_class()).unwrap();
|
||||
let b = vm.create_class("Label", vm.root_class()).unwrap();
|
||||
assert_ne!(a, b);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn type_cannot_be_used_to_bypass_the_class_factory() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let type_class = vm.type_class();
|
||||
let count = vm.object_count();
|
||||
assert_eq!(
|
||||
vm.create_instance(type_class),
|
||||
Err(VmError::UnsupportedMetaclass)
|
||||
);
|
||||
assert_eq!(
|
||||
vm.create_class("CustomType", type_class),
|
||||
Err(VmError::UnsupportedMetaclass)
|
||||
);
|
||||
assert_eq!(vm.object_count(), count);
|
||||
assert_eq!(vm.type_of(type_class.into()).unwrap(), type_class);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_remain_valid_when_the_arena_grows() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
@@ -111,7 +161,9 @@ fn handles_remain_valid_when_the_arena_grows() {
|
||||
for _ in 0..4096 {
|
||||
vm.create_instance(root).unwrap();
|
||||
}
|
||||
assert_eq!(vm.instance(first).unwrap().class(), root);
|
||||
assert_eq!(vm.type_of(first.into()).unwrap(), root);
|
||||
assert_eq!(vm.type_of(root.into()).unwrap(), vm.type_class());
|
||||
assert_eq!(vm.type_of(vm.type_class().into()).unwrap(), vm.type_class());
|
||||
assert_eq!(
|
||||
vm.instance(first).unwrap().field("stable"),
|
||||
Some(&Value::Bool(true))
|
||||
|
||||
Reference in New Issue
Block a user