Implement validated wordcode execution with shared code objects and frames
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m27s

This commit is contained in:
2026-10-18 20:41:56 +08:00
parent 50e8fcb5e7
commit 6fa95b64b0
18 changed files with 2327 additions and 152 deletions
+36 -15
View File
@@ -1,7 +1,7 @@
use std::fmt;
use crate::heap::Heap;
use crate::{CompareOp, EndorObject, ObjectPayload, ObjectRef, Record, Value};
use crate::{CompareOp, EndorObject, ExecutionLocation, ObjectPayload, ObjectRef, Record, Value};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VmError {
@@ -15,6 +15,19 @@ pub enum VmError {
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 {
@@ -32,6 +45,21 @@ impl fmt::Display for VmError {
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}"),
}
}
}
@@ -43,11 +71,15 @@ impl std::error::Error for VmError {}
#[derive(Debug)]
pub struct Vm {
pub(crate) heap: Heap,
pub(crate) last_traceback: Vec<ExecutionLocation>,
}
impl Vm {
pub fn new() -> Result<Self, VmError> {
Ok(Self { heap: Heap::new()? })
Ok(Self {
heap: Heap::new()?,
last_traceback: Vec::new(),
})
}
pub fn object_count(&self) -> usize {
@@ -145,17 +177,6 @@ impl Vm {
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)?;
@@ -163,8 +184,8 @@ impl Vm {
Ok(())
}
/// Explicit full collection of unreachable cycles, using external strong
/// references as roots. A tracing execution stack can be added later.
/// 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()
}