Initialize Aster VM and deploy PostgreSQL host on ces-control
Verify databases and deploy Aster VM / verify-and-deploy (push) Waiting to run

This commit is contained in:
2026-10-17 23:42:39 +08:00
commit 9648b9e8dd
34 changed files with 3717 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "aster-core"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
[lints]
workspace = true
+187
View File
@@ -0,0 +1,187 @@
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU64, Ordering};
use crate::VmError;
static NEXT_HEAP_ID: AtomicU64 = AtomicU64::new(1);
/// Opaque process-local identity. Cannot be constructed or deserialized by callers.
/// Handles do not keep the VM alive; every dereference goes through its owner VM.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ObjectRef {
heap_id: u64,
index: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClassRef(pub(crate) ObjectRef);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct InstanceRef(pub(crate) ObjectRef);
impl From<ClassRef> for ObjectRef {
fn from(value: ClassRef) -> Self {
value.0
}
}
impl From<InstanceRef> for ObjectRef {
fn from(value: InstanceRef) -> Self {
value.0
}
}
/// Primitive values are immediate; only classes and instances live on the heap.
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Nil,
Bool(bool),
Integer(i64),
Float(f64),
Text(String),
Object(ObjectRef),
}
/// The class object and its immutable description are one heap allocation.
/// There is deliberately no metaclass or implicit class-of-class relationship.
#[derive(Debug)]
pub struct Class {
pub(crate) name: String,
pub(crate) parent: Option<ClassRef>,
}
impl Class {
pub fn name(&self) -> &str {
&self.name
}
pub fn parent(&self) -> Option<ClassRef> {
self.parent
}
}
#[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)
}
}
#[derive(Debug)]
pub enum Object {
Class(Class),
Instance(Instance),
}
/// Allocation-only arena. No deletion or slot reuse until a GC model is chosen.
#[derive(Debug)]
pub(crate) struct Heap {
id: u64,
objects: Vec<Object>,
}
impl Heap {
pub(crate) fn new() -> Result<Self, VmError> {
let id = NEXT_HEAP_ID
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |id| id.checked_add(1))
.map_err(|_| VmError::IdentityExhausted)?;
Ok(Self {
id,
objects: Vec::new(),
})
}
pub(crate) fn allocate(&mut self, object: Object) -> ObjectRef {
let reference = ObjectRef {
heap_id: self.id,
index: self.objects.len(),
};
self.objects.push(object);
reference
}
pub(crate) fn get(&self, reference: ObjectRef) -> Result<&Object, VmError> {
self.validate_owner(reference)?;
self.objects
.get(reference.index)
.ok_or(VmError::InvalidHandle)
}
pub(crate) fn get_mut(&mut self, reference: ObjectRef) -> Result<&mut Object, VmError> {
self.validate_owner(reference)?;
self.objects
.get_mut(reference.index)
.ok_or(VmError::InvalidHandle)
}
fn validate_owner(&self, reference: ObjectRef) -> Result<(), VmError> {
if reference.heap_id != self.id {
return Err(VmError::ForeignHandle);
}
Ok(())
}
pub(crate) fn len(&self) -> usize {
self.objects.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn invalid_index_is_an_error_for_both_access_paths() {
let mut heap = Heap::new().unwrap();
let invalid = ObjectRef {
heap_id: heap.id,
index: usize::MAX,
};
assert!(matches!(heap.get(invalid), Err(VmError::InvalidHandle)));
assert!(matches!(heap.get_mut(invalid), Err(VmError::InvalidHandle)));
}
#[test]
fn typed_handles_are_checked_at_runtime_too() {
let mut vm = crate::Vm::new().unwrap();
let root = vm.root_class();
let instance = vm.create_instance(root).unwrap();
assert!(matches!(
vm.class(ClassRef(instance.0)),
Err(VmError::ExpectedClass)
));
assert!(matches!(
vm.instance(InstanceRef(root.0)),
Err(VmError::ExpectedInstance)
));
}
#[test]
fn invalid_value_reference_cannot_replace_a_field() {
let mut vm = crate::Vm::new().unwrap();
let root = vm.root_class();
let instance = vm.create_instance(root).unwrap();
vm.set_field(instance, "field", Value::Integer(1)).unwrap();
let invalid = ObjectRef {
heap_id: root.0.heap_id,
index: usize::MAX,
};
assert_eq!(
vm.set_field(instance, "field", Value::Object(invalid)),
Err(VmError::InvalidHandle)
);
assert_eq!(
vm.instance(instance).unwrap().field("field"),
Some(&Value::Integer(1))
);
}
}
+17
View File
@@ -0,0 +1,17 @@
//! Embeddable object runtime. No database, network, parser or executor dependencies.
//!
//! ```
//! use aster_core::{Value, Vm};
//! let mut vm = Vm::new()?;
//! 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);
//! # Ok::<(), aster_core::VmError>(())
//! ```
mod heap;
mod vm;
pub use heap::{Class, ClassRef, Instance, InstanceRef, Object, ObjectRef, Value};
pub use vm::{Vm, VmError};
+146
View File
@@ -0,0 +1,146 @@
use std::collections::BTreeMap;
use std::fmt;
use crate::heap::Heap;
use crate::{Class, ClassRef, Instance, InstanceRef, Object, ObjectRef, Value};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VmError {
ForeignHandle,
InvalidHandle,
ExpectedClass,
ExpectedInstance,
EmptyClassName,
ReservedClassName,
IdentityExhausted,
}
impl fmt::Display for VmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::ForeignHandle => "handle belongs to another VM",
Self::InvalidHandle => "handle does not identify a live object",
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::IdentityExhausted => "process-local VM identities exhausted",
})
}
}
impl std::error::Error for VmError {}
/// Owns its heap and exactly one root class. Not Clone: handles have one owner.
#[derive(Debug)]
pub struct Vm {
heap: Heap,
root: ClassRef,
}
impl Vm {
/// Bootstraps Object before returning. Does not perform any 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 })
}
/// Idempotent: construction has already performed bootstrap exactly once.
pub fn bootstrap(&mut self) -> ClassRef {
self.root
}
pub fn root_class(&self) -> ClassRef {
self.root
}
pub fn object_count(&self) -> usize {
self.heap.len()
}
/// All user classes have an existing parent. Class names are labels rather
/// than a global registry; duplicate non-reserved names are permitted.
pub fn create_class(
&mut self,
name: impl Into<String>,
parent: ClassRef,
) -> Result<ClassRef, VmError> {
self.class(parent)?;
let name = name.into();
if name.trim().is_empty() {
return Err(VmError::EmptyClassName);
}
if name == "Object" {
return Err(VmError::ReservedClassName);
}
Ok(ClassRef(self.heap.allocate(Object::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,
fields: BTreeMap::new(),
},
))))
}
pub fn object(&self, reference: ObjectRef) -> Result<&Object, VmError> {
self.heap.get(reference)
}
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),
}
}
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),
}
}
/// Converts a generic reference only after validating ownership and kind.
pub fn as_class(&self, reference: ObjectRef) -> Result<ClassRef, VmError> {
let typed = ClassRef(reference);
self.class(typed)?;
Ok(typed)
}
pub fn as_instance(&self, reference: ObjectRef) -> Result<InstanceRef, VmError> {
let typed = InstanceRef(reference);
self.instance(typed)?;
Ok(typed)
}
/// Checks both destination and referenced value before changing any field.
pub fn set_field(
&mut self,
instance: InstanceRef,
name: impl Into<String>,
value: Value,
) -> Result<(), VmError> {
self.instance(instance)?;
if let Value::Object(reference) = &value {
self.heap.get(*reference)?;
}
match self.heap.get_mut(instance.0)? {
Object::Instance(instance) => {
instance.fields.insert(name.into(), value);
Ok(())
}
Object::Class(_) => Err(VmError::ExpectedInstance),
}
}
}
+120
View File
@@ -0,0 +1,120 @@
use aster_core::{Value, Vm, VmError};
#[test]
fn bootstrap_is_unique_and_root_has_no_parent() {
let mut vm = Vm::new().unwrap();
let root = vm.root_class();
assert_eq!(vm.bootstrap(), root);
assert_eq!(vm.bootstrap(), root);
assert_eq!(vm.object_count(), 1);
assert_eq!(vm.class(root).unwrap().name(), "Object");
assert_eq!(vm.class(root).unwrap().parent(), None);
}
#[test]
fn factories_preserve_class_links_and_distinct_identity() {
let mut vm = Vm::new().unwrap();
let root = vm.root_class();
let class = vm.create_class("Point", root).unwrap();
let child = vm.create_class("ColoredPoint", class).unwrap();
let first = vm.create_instance(class).unwrap();
let second = vm.create_instance(class).unwrap();
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);
vm.set_field(first, "x", Value::Integer(1)).unwrap();
vm.set_field(first, "next", Value::Object(second.into()))
.unwrap();
assert_eq!(
vm.instance(first).unwrap().field("x"),
Some(&Value::Integer(1))
);
assert_eq!(vm.instance(second).unwrap().field("x"), None);
assert_eq!(vm.as_class(class.into()).unwrap(), class);
assert_eq!(vm.as_instance(first.into()).unwrap(), first);
assert_eq!(vm.as_instance(class.into()), Err(VmError::ExpectedInstance));
assert_eq!(vm.as_class(first.into()), Err(VmError::ExpectedClass));
}
#[test]
fn foreign_handles_are_rejected_without_mutation() {
let mut vm = Vm::new().unwrap();
let mut other = Vm::new().unwrap();
let local = vm.create_instance(vm.root_class()).unwrap();
let foreign = other.create_instance(other.root_class()).unwrap();
let count = vm.object_count();
assert_eq!(
vm.create_class("Alien", other.root_class()),
Err(VmError::ForeignHandle)
);
assert_eq!(
vm.create_instance(other.root_class()),
Err(VmError::ForeignHandle)
);
assert!(matches!(
vm.object(foreign.into()),
Err(VmError::ForeignHandle)
));
assert!(matches!(vm.instance(foreign), Err(VmError::ForeignHandle)));
assert_eq!(
vm.set_field(foreign, "x", Value::Nil),
Err(VmError::ForeignHandle)
);
vm.set_field(local, "x", Value::Integer(7)).unwrap();
assert_eq!(
vm.set_field(local, "x", Value::Object(foreign.into())),
Err(VmError::ForeignHandle)
);
assert_eq!(
vm.instance(local).unwrap().field("x"),
Some(&Value::Integer(7))
);
assert_eq!(vm.object_count(), count);
}
#[test]
fn handles_outliving_a_vm_cannot_alias_a_new_vm() {
let old = {
let vm = Vm::new().unwrap();
vm.root_class()
};
let vm = Vm::new().unwrap();
assert_ne!(old, vm.root_class());
assert!(matches!(vm.class(old), Err(VmError::ForeignHandle)));
}
#[test]
fn invalid_names_do_not_allocate_and_names_are_not_a_namespace() {
let mut vm = Vm::new().unwrap();
assert_eq!(
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);
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 handles_remain_valid_when_the_arena_grows() {
let mut vm = Vm::new().unwrap();
let root = vm.root_class();
let first = vm.create_instance(root).unwrap();
vm.set_field(first, "stable", Value::Bool(true)).unwrap();
for _ in 0..4096 {
vm.create_instance(root).unwrap();
}
assert_eq!(vm.instance(first).unwrap().class(), root);
assert_eq!(
vm.instance(first).unwrap().field("stable"),
Some(&Value::Bool(true))
);
assert_eq!(vm.class(root).unwrap().name(), "Object");
}