Shorten crate directory names and update workspace paths
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m53s

This commit is contained in:
2026-10-18 19:57:21 +08:00
parent 891defb34c
commit 50e8fcb5e7
24 changed files with 15 additions and 15 deletions
+171
View File
@@ -0,0 +1,171 @@
use std::fmt;
use crate::heap::Heap;
use crate::{CompareOp, EndorObject, ObjectPayload, ObjectRef, Record, Value};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VmError {
ForeignHandle,
InvalidHandle,
IdentityExhausted,
RefCountOverflow,
NoOwnedReference,
AttributeNotFound(String),
UnsupportedOperation(&'static str),
InvalidArgument(&'static str),
ObjectKindMismatch(&'static str),
EmptyCell,
}
impl fmt::Display for VmError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ForeignHandle => f.write_str("handle belongs to another VM"),
Self::InvalidHandle => f.write_str("handle does not identify a live object"),
Self::IdentityExhausted => f.write_str("process-local VM identities exhausted"),
Self::RefCountOverflow => f.write_str("object reference count exhausted"),
Self::NoOwnedReference => f.write_str("no external strong reference to release"),
Self::AttributeNotFound(name) => write!(f, "attribute not found: {name}"),
Self::UnsupportedOperation(operation) => {
write!(f, "object does not support {operation}")
}
Self::InvalidArgument(message) => f.write_str(message),
Self::ObjectKindMismatch(expected) => write!(f, "expected {expected} object"),
Self::EmptyCell => f.write_str("cell is empty"),
}
}
}
impl std::error::Error for VmError {}
/// Runtime owner. Construction creates an empty heap, without language classes,
/// metaclasses or a Python type system. Dropping it releases all host resources.
#[derive(Debug)]
pub struct Vm {
pub(crate) heap: Heap,
}
impl Vm {
pub fn new() -> Result<Self, VmError> {
Ok(Self { heap: Heap::new()? })
}
pub fn object_count(&self) -> usize {
self.heap.len()
}
/// Returns one external strong reference. Copies of the returned handle
/// are borrowed; use retain/release to manage additional owned references.
pub fn allocate(&mut self, payload: impl ObjectPayload) -> Result<ObjectRef, VmError> {
self.validate_builtin(&payload)?;
self.heap.allocate(Box::new(payload))
}
pub fn new_object(&mut self) -> Result<ObjectRef, VmError> {
self.allocate(Record)
}
pub fn object(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
self.heap.get(reference)
}
pub fn retain(&mut self, reference: ObjectRef) -> Result<(), VmError> {
self.heap.retain(reference)
}
pub fn release(&mut self, reference: ObjectRef) -> Result<(), VmError> {
self.heap.release(reference)
}
pub fn ref_count(&self, reference: ObjectRef) -> Result<usize, VmError> {
Ok(self.heap.get(reference)?.header().ref_count())
}
pub fn is_same(&self, left: ObjectRef, right: ObjectRef) -> Result<bool, VmError> {
self.heap.get(left)?;
self.heap.get(right)?;
Ok(left == right)
}
pub fn repr(&self, reference: ObjectRef) -> Result<String, VmError> {
self.heap.get(reference)?.payload().repr(reference)
}
pub fn str(&self, reference: ObjectRef) -> Result<String, VmError> {
self.heap.get(reference)?.payload().str(reference)
}
pub fn is_true(&self, reference: ObjectRef) -> Result<bool, VmError> {
self.heap.get(reference)?.payload().is_true()
}
pub fn hash(&self, reference: ObjectRef) -> Result<u64, VmError> {
self.heap.get(reference)?.payload().hash(reference)
}
pub fn compare(
&self,
left: ObjectRef,
right: ObjectRef,
operation: CompareOp,
) -> Result<bool, VmError> {
let left_payload = self.heap.get(left)?.payload();
let right_payload = self.heap.get(right)?.payload();
if let Some(result) = left_payload.compare(right_payload, operation)? {
return Ok(result);
}
if let Some(result) = right_payload.compare(left_payload, operation.reflected())? {
return Ok(result);
}
match operation {
CompareOp::Eq => Ok(left == right),
CompareOp::Ne => Ok(left != right),
_ => Err(VmError::UnsupportedOperation("ordering")),
}
}
/// Reads a borrowed value. An Object result needs retain() if the host
/// wants it to survive removal of the attribute that currently owns it.
pub fn get_attr(&self, reference: ObjectRef, name: &str) -> Result<Value, VmError> {
self.heap.get_attr(reference, name)
}
/// Object-valued attributes own internal strong references independently
/// of the caller's external references. Failure leaves the field unchanged.
pub fn set_attr(
&mut self,
reference: ObjectRef,
name: impl Into<String>,
value: Value,
) -> Result<(), VmError> {
self.heap.set_attr(reference, name.into(), value)
}
pub fn del_attr(&mut self, reference: ObjectRef, name: &str) -> Result<(), VmError> {
self.heap.del_attr(reference, name)
}
/// Results are borrowed values; native operations cannot mutate this VM.
pub fn call(&self, reference: ObjectRef, arguments: &[Value]) -> Result<Value, VmError> {
let payload = self.heap.get(reference)?.payload();
for argument in arguments {
self.validate_value(argument)?;
}
let result = payload.call(arguments)?;
self.validate_value(&result)?;
Ok(result)
}
pub(crate) fn validate_value(&self, value: &Value) -> Result<(), VmError> {
if let Value::Object(reference) = value {
self.heap.get(*reference)?;
}
Ok(())
}
/// Explicit full collection of unreachable cycles, using external strong
/// references as roots. A tracing execution stack can be added later.
pub fn collect_cycles(&mut self) -> usize {
self.heap.collect_cycles()
}
}