refactor: extract core objects into a standalone package
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m34s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 7m34s
This commit is contained in:
+106
-106
@@ -1,138 +1,74 @@
|
||||
use std::fmt;
|
||||
use objects::ObjectStore;
|
||||
|
||||
use crate::heap::Heap;
|
||||
use crate::{CompareOp, EndorObject, ExecutionLocation, ObjectPayload, ObjectRef, Record, Value};
|
||||
use crate::{
|
||||
CompareOp, EndorCode, EndorNumber, EndorObject, ExecutionLocation, ObjectPayload, ObjectRef,
|
||||
Value, VmError,
|
||||
};
|
||||
|
||||
#[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,
|
||||
InvalidBytecode {
|
||||
offset: usize,
|
||||
reason: &'static str,
|
||||
},
|
||||
UnboundLocal(String),
|
||||
NameNotFound(String),
|
||||
ArgumentCount {
|
||||
name: String,
|
||||
required: usize,
|
||||
maximum: usize,
|
||||
provided: usize,
|
||||
},
|
||||
ExecutionLimit(&'static str),
|
||||
}
|
||||
|
||||
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"),
|
||||
Self::InvalidBytecode { offset, reason } => {
|
||||
write!(f, "invalid bytecode at code unit {offset}: {reason}")
|
||||
}
|
||||
Self::UnboundLocal(name) => write!(f, "local variable is unbound: {name}"),
|
||||
Self::NameNotFound(name) => write!(f, "name not found: {name}"),
|
||||
Self::ArgumentCount {
|
||||
name,
|
||||
required,
|
||||
maximum,
|
||||
provided,
|
||||
} => write!(
|
||||
f,
|
||||
"{name} expects {required}..={maximum} positional arguments, received {provided}"
|
||||
),
|
||||
Self::ExecutionLimit(limit) => write!(f, "execution limit reached: {limit}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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.
|
||||
/// Owns the object store and execution state. Object representation, allocation,
|
||||
/// reference accounting and builtin operations live in the objects crate.
|
||||
#[derive(Debug)]
|
||||
pub struct Vm {
|
||||
pub(crate) heap: Heap,
|
||||
objects: ObjectStore,
|
||||
pub(crate) last_traceback: Vec<ExecutionLocation>,
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
pub fn new() -> Result<Self, VmError> {
|
||||
Ok(Self {
|
||||
heap: Heap::new()?,
|
||||
objects: ObjectStore::new()?,
|
||||
last_traceback: Vec::new(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn object_count(&self) -> usize {
|
||||
self.heap.len()
|
||||
self.objects.object_count()
|
||||
}
|
||||
|
||||
/// 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))
|
||||
self.objects.allocate(payload)
|
||||
}
|
||||
|
||||
pub fn new_object(&mut self) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(Record)
|
||||
self.objects.new_object()
|
||||
}
|
||||
|
||||
pub fn object(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
|
||||
self.heap.get(reference)
|
||||
self.objects.object(reference)
|
||||
}
|
||||
|
||||
pub fn retain(&mut self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
self.heap.retain(reference)
|
||||
self.objects.retain(reference)
|
||||
}
|
||||
|
||||
pub fn release(&mut self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
self.heap.release(reference)
|
||||
self.objects.release(reference)
|
||||
}
|
||||
|
||||
pub fn ref_count(&self, reference: ObjectRef) -> Result<usize, VmError> {
|
||||
Ok(self.heap.get(reference)?.header().ref_count())
|
||||
self.objects.ref_count(reference)
|
||||
}
|
||||
|
||||
pub fn is_same(&self, left: ObjectRef, right: ObjectRef) -> Result<bool, VmError> {
|
||||
self.heap.get(left)?;
|
||||
self.heap.get(right)?;
|
||||
Ok(left == right)
|
||||
self.objects.is_same(left, right)
|
||||
}
|
||||
|
||||
pub fn repr(&self, reference: ObjectRef) -> Result<String, VmError> {
|
||||
self.heap.get(reference)?.payload().repr(reference)
|
||||
self.objects.repr(reference)
|
||||
}
|
||||
|
||||
pub fn str(&self, reference: ObjectRef) -> Result<String, VmError> {
|
||||
self.heap.get(reference)?.payload().str(reference)
|
||||
self.objects.str(reference)
|
||||
}
|
||||
|
||||
pub fn is_true(&self, reference: ObjectRef) -> Result<bool, VmError> {
|
||||
self.heap.get(reference)?.payload().is_true()
|
||||
self.objects.is_true(reference)
|
||||
}
|
||||
|
||||
pub fn hash(&self, reference: ObjectRef) -> Result<u64, VmError> {
|
||||
self.heap.get(reference)?.payload().hash(reference)
|
||||
self.objects.hash(reference)
|
||||
}
|
||||
|
||||
pub fn compare(
|
||||
@@ -141,25 +77,13 @@ impl Vm {
|
||||
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")),
|
||||
}
|
||||
self.objects.compare(left, right, operation)
|
||||
}
|
||||
|
||||
/// 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)
|
||||
self.objects.get_attr(reference, name)
|
||||
}
|
||||
|
||||
/// Object-valued attributes own internal strong references independently
|
||||
@@ -170,23 +94,99 @@ impl Vm {
|
||||
name: impl Into<String>,
|
||||
value: Value,
|
||||
) -> Result<(), VmError> {
|
||||
self.heap.set_attr(reference, name.into(), value)
|
||||
self.objects.set_attr(reference, name, value)
|
||||
}
|
||||
|
||||
pub fn del_attr(&mut self, reference: ObjectRef, name: &str) -> Result<(), VmError> {
|
||||
self.heap.del_attr(reference, name)
|
||||
self.objects.del_attr(reference, name)
|
||||
}
|
||||
|
||||
pub(crate) fn validate_value(&self, value: &Value) -> Result<(), VmError> {
|
||||
if let Value::Object(reference) = value {
|
||||
self.heap.get(*reference)?;
|
||||
}
|
||||
Ok(())
|
||||
self.objects.validate_value(value)
|
||||
}
|
||||
|
||||
/// Explicit full collection. Execution slots use owned roots, so values
|
||||
/// on live execution stacks participate in the same root accounting.
|
||||
pub fn collect_cycles(&mut self) -> usize {
|
||||
self.heap.collect_cycles()
|
||||
self.objects.collect_cycles()
|
||||
}
|
||||
|
||||
pub fn payload<T: ObjectPayload>(
|
||||
&self,
|
||||
reference: ObjectRef,
|
||||
expected: &'static str,
|
||||
) -> Result<&T, VmError> {
|
||||
self.objects.payload::<T>(reference, expected)
|
||||
}
|
||||
|
||||
pub fn new_string(&mut self, text: impl Into<String>) -> Result<ObjectRef, VmError> {
|
||||
self.objects.new_string(text)
|
||||
}
|
||||
|
||||
pub fn new_code(&mut self, code: EndorCode) -> Result<ObjectRef, VmError> {
|
||||
self.objects.new_code(code)
|
||||
}
|
||||
|
||||
pub fn new_function(
|
||||
&mut self,
|
||||
name: impl Into<String>,
|
||||
code: ObjectRef,
|
||||
globals: ObjectRef,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
self.objects
|
||||
.new_function(name, code, globals, defaults, closure)
|
||||
}
|
||||
|
||||
pub fn new_number(&mut self, number: EndorNumber) -> Result<ObjectRef, VmError> {
|
||||
self.objects.new_number(number)
|
||||
}
|
||||
|
||||
pub fn new_dict(&mut self) -> Result<ObjectRef, VmError> {
|
||||
self.objects.new_dict()
|
||||
}
|
||||
|
||||
/// Returns borrowed keys and values in insertion order.
|
||||
pub fn dict_items(&self, dictionary: ObjectRef) -> Result<&[(Value, Value)], VmError> {
|
||||
self.objects.dict_items(dictionary)
|
||||
}
|
||||
|
||||
pub fn dict_get(&self, dictionary: ObjectRef, key: &Value) -> Result<Option<Value>, VmError> {
|
||||
self.objects.dict_get(dictionary, key)
|
||||
}
|
||||
|
||||
pub fn dict_set(
|
||||
&mut self,
|
||||
dictionary: ObjectRef,
|
||||
key: Value,
|
||||
value: Value,
|
||||
) -> Result<(), VmError> {
|
||||
self.objects.dict_set(dictionary, key, value)
|
||||
}
|
||||
|
||||
pub fn dict_delete(&mut self, dictionary: ObjectRef, key: &Value) -> Result<bool, VmError> {
|
||||
self.objects.dict_delete(dictionary, key)
|
||||
}
|
||||
|
||||
pub fn dict_clear(&mut self, dictionary: ObjectRef) -> Result<(), VmError> {
|
||||
self.objects.dict_clear(dictionary)
|
||||
}
|
||||
|
||||
pub fn new_cell(&mut self, value: Option<Value>) -> Result<ObjectRef, VmError> {
|
||||
self.objects.new_cell(value)
|
||||
}
|
||||
|
||||
/// Reads a borrowed value; an empty cell is not Value::Nil.
|
||||
pub fn cell_get(&self, cell: ObjectRef) -> Result<Value, VmError> {
|
||||
self.objects.cell_get(cell)
|
||||
}
|
||||
|
||||
pub fn cell_set(&mut self, cell: ObjectRef, value: Value) -> Result<(), VmError> {
|
||||
self.objects.cell_set(cell, value)
|
||||
}
|
||||
|
||||
pub fn cell_clear(&mut self, cell: ObjectRef) -> Result<(), VmError> {
|
||||
self.objects.cell_clear(cell)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user