Shorten crate directory names and update workspace paths
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m53s
Verify databases and deploy Aster VM / verify-and-deploy (push) Successful in 14m53s
This commit is contained in:
@@ -0,0 +1,393 @@
|
||||
use std::any::Any;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::{CompareOp, ObjectPayload, ObjectRef, Value, VmError};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EndorStringObject {
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl EndorStringObject {
|
||||
pub fn new(text: impl Into<String>) -> Self {
|
||||
Self { text: text.into() }
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
|
||||
pub fn char_count(&self) -> usize {
|
||||
self.text.chars().count()
|
||||
}
|
||||
|
||||
pub fn char_at(&self, index: usize) -> Option<char> {
|
||||
self.text.chars().nth(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorStringObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"string"
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("{:?}", self.text))
|
||||
}
|
||||
fn str(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(self.text.clone())
|
||||
}
|
||||
fn is_true(&self) -> Result<bool, VmError> {
|
||||
Ok(!self.text.is_empty())
|
||||
}
|
||||
fn hash(&self, _identity: ObjectRef) -> Result<u64, VmError> {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
self.text.hash(&mut hasher);
|
||||
Ok(hasher.finish())
|
||||
}
|
||||
fn compare(&self, other: &dyn ObjectPayload, op: CompareOp) -> Result<Option<bool>, VmError> {
|
||||
let Some(other) = other.as_any().downcast_ref::<Self>() else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(match op {
|
||||
CompareOp::Eq => self.text == other.text,
|
||||
CompareOp::Ne => self.text != other.text,
|
||||
CompareOp::Lt => self.text < other.text,
|
||||
CompareOp::Le => self.text <= other.text,
|
||||
CompareOp::Gt => self.text > other.text,
|
||||
CompareOp::Ge => self.text >= other.text,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// Insertion-ordered entries. Mutation is only exposed through Vm so every
|
||||
/// key/value reference is validated, retained, traced, and released correctly.
|
||||
/// Linear lookup is intentional for the initial representation layer.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EndorDictObject {
|
||||
pub(crate) entries: Vec<(Value, Value)>,
|
||||
}
|
||||
|
||||
impl EndorDictObject {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn len(&self) -> usize {
|
||||
self.entries.len()
|
||||
}
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.entries.is_empty()
|
||||
}
|
||||
pub fn entries(&self) -> &[(Value, Value)] {
|
||||
&self.entries
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorDictObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"dict"
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
for (key, value) in &self.entries {
|
||||
trace_value(key, visit);
|
||||
trace_value(value, visit);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("<dict len={}>", self.len()))
|
||||
}
|
||||
fn is_true(&self) -> Result<bool, VmError> {
|
||||
Ok(!self.is_empty())
|
||||
}
|
||||
fn hash(&self, _identity: ObjectRef) -> Result<u64, VmError> {
|
||||
Err(VmError::UnsupportedOperation("dict hash"))
|
||||
}
|
||||
}
|
||||
|
||||
/// An empty cell differs from a cell containing Value::Nil.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct EndorCellObject {
|
||||
pub(crate) value: Option<Value>,
|
||||
}
|
||||
|
||||
impl EndorCellObject {
|
||||
pub fn new(value: Option<Value>) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
pub fn value(&self) -> Option<&Value> {
|
||||
self.value.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorCellObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"cell"
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
if let Some(value) = &self.value {
|
||||
trace_value(value, visit);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(if self.value.is_some() {
|
||||
"<cell occupied>"
|
||||
} else {
|
||||
"<cell empty>"
|
||||
}
|
||||
.into())
|
||||
}
|
||||
fn hash(&self, _identity: ObjectRef) -> Result<u64, VmError> {
|
||||
Err(VmError::UnsupportedOperation("cell hash"))
|
||||
}
|
||||
}
|
||||
|
||||
/// Code data only: bytecode is opaque until an instruction format/executor
|
||||
/// is defined. Constants may own managed objects through the enclosing function.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorCode {
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
}
|
||||
|
||||
impl EndorCode {
|
||||
pub fn new(
|
||||
bytecode: Vec<u8>,
|
||||
constants: Vec<Value>,
|
||||
parameters: Vec<String>,
|
||||
free_variables: Vec<String>,
|
||||
) -> Result<Self, VmError> {
|
||||
for names in [¶meters, &free_variables] {
|
||||
let mut seen = std::collections::BTreeSet::new();
|
||||
for name in names {
|
||||
if name.is_empty() || !seen.insert(name) {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"empty or duplicate code variable name",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
bytecode,
|
||||
constants,
|
||||
parameters,
|
||||
free_variables,
|
||||
})
|
||||
}
|
||||
pub fn bytecode(&self) -> &[u8] {
|
||||
&self.bytecode
|
||||
}
|
||||
pub fn constants(&self) -> &[Value] {
|
||||
&self.constants
|
||||
}
|
||||
pub fn parameters(&self) -> &[String] {
|
||||
&self.parameters
|
||||
}
|
||||
pub fn free_variables(&self) -> &[String] {
|
||||
&self.free_variables
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorFunctionObject {
|
||||
name: String,
|
||||
code: EndorCode,
|
||||
globals: ObjectRef,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
}
|
||||
|
||||
impl EndorFunctionObject {
|
||||
pub fn new(
|
||||
name: impl Into<String>,
|
||||
code: EndorCode,
|
||||
globals: ObjectRef,
|
||||
defaults: Vec<Value>,
|
||||
closure: Vec<ObjectRef>,
|
||||
) -> Result<Self, VmError> {
|
||||
if defaults.len() > code.parameters.len() || closure.len() != code.free_variables.len() {
|
||||
return Err(VmError::InvalidArgument(
|
||||
"function defaults or closure do not match code",
|
||||
));
|
||||
}
|
||||
Ok(Self {
|
||||
name: name.into(),
|
||||
code,
|
||||
globals,
|
||||
defaults,
|
||||
closure,
|
||||
})
|
||||
}
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
pub fn code(&self) -> &EndorCode {
|
||||
&self.code
|
||||
}
|
||||
pub fn globals(&self) -> ObjectRef {
|
||||
self.globals
|
||||
}
|
||||
pub fn defaults(&self) -> &[Value] {
|
||||
&self.defaults
|
||||
}
|
||||
pub fn closure(&self) -> &[ObjectRef] {
|
||||
&self.closure
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorFunctionObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"function"
|
||||
}
|
||||
fn allows_attributes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
visit(self.globals);
|
||||
for reference in &self.closure {
|
||||
visit(*reference);
|
||||
}
|
||||
for value in self.defaults.iter().chain(&self.code.constants) {
|
||||
trace_value(value, visit);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("<function {}>", self.name))
|
||||
}
|
||||
fn call(&self, _arguments: &[Value]) -> Result<Value, VmError> {
|
||||
Err(VmError::UnsupportedOperation("language function execution"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EndorTracebackEntry {
|
||||
pub function: String,
|
||||
pub file: String,
|
||||
pub line: usize,
|
||||
}
|
||||
|
||||
/// Common exception instance data, not an exception class or inheritance tree.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorBaseExceptionObject {
|
||||
name: String,
|
||||
args: Vec<Value>,
|
||||
cause: Option<ObjectRef>,
|
||||
context: Option<ObjectRef>,
|
||||
suppress_context: bool,
|
||||
notes: Vec<String>,
|
||||
traceback: Vec<EndorTracebackEntry>,
|
||||
}
|
||||
|
||||
impl EndorBaseExceptionObject {
|
||||
pub fn new(name: impl Into<String>, args: Vec<Value>) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
args,
|
||||
cause: None,
|
||||
context: None,
|
||||
suppress_context: false,
|
||||
notes: Vec::new(),
|
||||
traceback: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn with_cause(mut self, cause: ObjectRef) -> Self {
|
||||
self.cause = Some(cause);
|
||||
self.suppress_context = true;
|
||||
self
|
||||
}
|
||||
pub fn with_context(mut self, context: ObjectRef) -> Self {
|
||||
self.context = Some(context);
|
||||
self
|
||||
}
|
||||
pub fn suppress_context(mut self, suppress: bool) -> Self {
|
||||
self.suppress_context = suppress;
|
||||
self
|
||||
}
|
||||
pub fn with_note(mut self, note: impl Into<String>) -> Self {
|
||||
self.notes.push(note.into());
|
||||
self
|
||||
}
|
||||
pub fn with_traceback(mut self, entry: EndorTracebackEntry) -> Self {
|
||||
self.traceback.push(entry);
|
||||
self
|
||||
}
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
pub fn args(&self) -> &[Value] {
|
||||
&self.args
|
||||
}
|
||||
pub fn cause(&self) -> Option<ObjectRef> {
|
||||
self.cause
|
||||
}
|
||||
pub fn context(&self) -> Option<ObjectRef> {
|
||||
self.context
|
||||
}
|
||||
pub fn context_suppressed(&self) -> bool {
|
||||
self.suppress_context
|
||||
}
|
||||
pub fn notes(&self) -> &[String] {
|
||||
&self.notes
|
||||
}
|
||||
pub fn traceback(&self) -> &[EndorTracebackEntry] {
|
||||
&self.traceback
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorBaseExceptionObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"exception"
|
||||
}
|
||||
fn allows_attributes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
for value in &self.args {
|
||||
trace_value(value, visit);
|
||||
}
|
||||
for reference in [self.cause, self.context].into_iter().flatten() {
|
||||
visit(reference);
|
||||
}
|
||||
}
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!(
|
||||
"{}({})",
|
||||
self.name,
|
||||
self.args
|
||||
.iter()
|
||||
.map(|arg| format!("{arg:?}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
))
|
||||
}
|
||||
fn str(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(match self.args.as_slice() {
|
||||
[] => String::new(),
|
||||
[Value::Text(text)] => text.clone(),
|
||||
[Value::Number(number)] => number.to_string(),
|
||||
args => format!("{args:?}"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn trace_value(value: &Value, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
if let Value::Object(reference) = value {
|
||||
visit(*reference);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
use std::collections::BTreeMap;
|
||||
use std::fmt;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use crate::{EndorObject, ObjectHeader, ObjectPayload, Value, VmError};
|
||||
|
||||
static NEXT_HEAP_ID: AtomicU64 = AtomicU64::new(1);
|
||||
|
||||
/// Checked, process-local reference. Copying it never retains an object.
|
||||
/// The generation prevents a released handle from aliasing a reused slot.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct ObjectRef {
|
||||
heap_id: u64,
|
||||
index: usize,
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
impl fmt::Display for ObjectRef {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}:{}:{}", self.heap_id, self.index, self.generation)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Slot {
|
||||
generation: u64,
|
||||
object: Option<Box<EndorObject>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Heap {
|
||||
id: u64,
|
||||
slots: Vec<Slot>,
|
||||
free: Vec<usize>,
|
||||
live: usize,
|
||||
}
|
||||
|
||||
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,
|
||||
slots: Vec::new(),
|
||||
free: Vec::new(),
|
||||
live: 0,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn allocate(
|
||||
&mut self,
|
||||
payload: Box<dyn ObjectPayload>,
|
||||
) -> Result<ObjectRef, VmError> {
|
||||
let mut edges = Vec::new();
|
||||
payload.trace(&mut |reference| edges.push(reference));
|
||||
let increments = self.prepare_increments(&edges)?;
|
||||
let index = self.free.pop().unwrap_or(self.slots.len());
|
||||
if index == self.slots.len() {
|
||||
self.slots.push(Slot {
|
||||
generation: 0,
|
||||
object: None,
|
||||
});
|
||||
}
|
||||
let reference = ObjectRef {
|
||||
heap_id: self.id,
|
||||
index,
|
||||
generation: self.slots[index].generation,
|
||||
};
|
||||
let object = Box::new(EndorObject {
|
||||
header: ObjectHeader {
|
||||
identity: reference,
|
||||
strong_refs: 1,
|
||||
external_refs: 1,
|
||||
},
|
||||
payload,
|
||||
attributes: BTreeMap::new(),
|
||||
payload_edges: edges,
|
||||
});
|
||||
self.apply_increments(increments);
|
||||
self.slots[index].object = Some(object);
|
||||
self.live += 1;
|
||||
Ok(reference)
|
||||
}
|
||||
|
||||
pub(crate) fn get(&self, reference: ObjectRef) -> Result<&EndorObject, VmError> {
|
||||
self.validate_owner(reference)?;
|
||||
self.slots
|
||||
.get(reference.index)
|
||||
.filter(|slot| slot.generation == reference.generation)
|
||||
.and_then(|slot| slot.object.as_deref())
|
||||
.ok_or(VmError::InvalidHandle)
|
||||
}
|
||||
|
||||
/// Transactional copy-on-update for built-in containers. The EndorObject
|
||||
/// box and identity stay stable; only its payload and edge snapshot change.
|
||||
/// All new edges are validated/retained before any old edge is released.
|
||||
pub(crate) fn replace_payload(
|
||||
&mut self,
|
||||
reference: ObjectRef,
|
||||
payload: impl ObjectPayload,
|
||||
) -> Result<(), VmError> {
|
||||
self.get(reference)?;
|
||||
let mut edges = Vec::new();
|
||||
payload.trace(&mut |target| edges.push(target));
|
||||
let increments = self.prepare_increments(&edges)?;
|
||||
self.apply_increments(increments);
|
||||
let object = self.get_mut(reference)?;
|
||||
let old_edges = std::mem::replace(&mut object.payload_edges, edges);
|
||||
let old_payload = std::mem::replace(&mut object.payload, Box::new(payload));
|
||||
self.release_edges(old_edges);
|
||||
drop(old_payload);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_mut(&mut self, reference: ObjectRef) -> Result<&mut EndorObject, VmError> {
|
||||
self.get(reference)?;
|
||||
Ok(self.slots[reference.index].object.as_deref_mut().unwrap())
|
||||
}
|
||||
|
||||
fn validate_owner(&self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
if reference.heap_id != self.id {
|
||||
return Err(VmError::ForeignHandle);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepare_increments(
|
||||
&self,
|
||||
references: &[ObjectRef],
|
||||
) -> Result<BTreeMap<ObjectRef, usize>, VmError> {
|
||||
let mut counts = BTreeMap::<ObjectRef, usize>::new();
|
||||
for reference in references {
|
||||
self.get(*reference)?;
|
||||
let count = counts.entry(*reference).or_default();
|
||||
*count = count.checked_add(1).ok_or(VmError::RefCountOverflow)?;
|
||||
}
|
||||
for (reference, count) in &counts {
|
||||
self.get(*reference)?
|
||||
.header
|
||||
.strong_refs
|
||||
.checked_add(*count)
|
||||
.ok_or(VmError::RefCountOverflow)?;
|
||||
}
|
||||
Ok(counts)
|
||||
}
|
||||
|
||||
fn apply_increments(&mut self, increments: BTreeMap<ObjectRef, usize>) {
|
||||
for (reference, count) in increments {
|
||||
self.get_mut(reference).unwrap().header.strong_refs += count;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn retain(&mut self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
let header = &mut self.get_mut(reference)?.header;
|
||||
let strong = header
|
||||
.strong_refs
|
||||
.checked_add(1)
|
||||
.ok_or(VmError::RefCountOverflow)?;
|
||||
let external = header
|
||||
.external_refs
|
||||
.checked_add(1)
|
||||
.ok_or(VmError::RefCountOverflow)?;
|
||||
header.strong_refs = strong;
|
||||
header.external_refs = external;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn release(&mut self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
let header = &mut self.get_mut(reference)?.header;
|
||||
if header.external_refs == 0 {
|
||||
return Err(VmError::NoOwnedReference);
|
||||
}
|
||||
header.external_refs -= 1;
|
||||
header.strong_refs -= 1;
|
||||
let pending = if header.strong_refs == 0 {
|
||||
vec![reference]
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
self.drain_zero_counts(pending);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn set_attr(
|
||||
&mut self,
|
||||
reference: ObjectRef,
|
||||
name: String,
|
||||
value: Value,
|
||||
) -> Result<(), VmError> {
|
||||
self.check_attributes(reference)?;
|
||||
let edges: Vec<_> = match &value {
|
||||
Value::Object(target) => vec![*target],
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let increments = self.prepare_increments(&edges)?;
|
||||
self.apply_increments(increments);
|
||||
let old = self.get_mut(reference)?.attributes.insert(name, value);
|
||||
if let Some(Value::Object(target)) = old {
|
||||
self.release_edges(vec![target]);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn get_attr(&self, reference: ObjectRef, name: &str) -> Result<Value, VmError> {
|
||||
self.check_attributes(reference)?;
|
||||
self.get(reference)?
|
||||
.attributes
|
||||
.get(name)
|
||||
.cloned()
|
||||
.ok_or_else(|| VmError::AttributeNotFound(name.into()))
|
||||
}
|
||||
|
||||
pub(crate) fn del_attr(&mut self, reference: ObjectRef, name: &str) -> Result<(), VmError> {
|
||||
self.check_attributes(reference)?;
|
||||
let old = self
|
||||
.get_mut(reference)?
|
||||
.attributes
|
||||
.remove(name)
|
||||
.ok_or_else(|| VmError::AttributeNotFound(name.into()))?;
|
||||
if let Value::Object(target) = old {
|
||||
self.release_edges(vec![target]);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_attributes(&self, reference: ObjectRef) -> Result<(), VmError> {
|
||||
if !self.get(reference)?.payload.allows_attributes() {
|
||||
return Err(VmError::UnsupportedOperation("attributes"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn release_edges(&mut self, edges: Vec<ObjectRef>) {
|
||||
let mut pending = Vec::new();
|
||||
for target in edges {
|
||||
let header = &mut self.get_mut(target).unwrap().header;
|
||||
header.strong_refs -= 1;
|
||||
if header.strong_refs == 0 {
|
||||
pending.push(target);
|
||||
}
|
||||
}
|
||||
self.drain_zero_counts(pending);
|
||||
}
|
||||
|
||||
/// Removes the object before running Drop. Reuse increments its generation;
|
||||
/// an exhausted generation permanently retires that slot.
|
||||
fn take(&mut self, reference: ObjectRef) -> Box<EndorObject> {
|
||||
let slot = &mut self.slots[reference.index];
|
||||
let object = slot.object.take().unwrap();
|
||||
if let Some(next) = slot.generation.checked_add(1) {
|
||||
slot.generation = next;
|
||||
self.free.push(reference.index);
|
||||
}
|
||||
self.live -= 1;
|
||||
object
|
||||
}
|
||||
|
||||
fn drain_zero_counts(&mut self, mut pending: Vec<ObjectRef>) {
|
||||
while let Some(reference) = pending.pop() {
|
||||
let object = self.take(reference);
|
||||
for target in object.edges() {
|
||||
let header = &mut self.get_mut(target).unwrap().header;
|
||||
header.strong_refs -= 1;
|
||||
if header.strong_refs == 0 {
|
||||
pending.push(target);
|
||||
}
|
||||
}
|
||||
drop(object);
|
||||
}
|
||||
}
|
||||
|
||||
/// Full, explicit cycle collection. External references are roots; every
|
||||
/// managed attribute and declared payload edge participates in tracing.
|
||||
pub(crate) fn collect_cycles(&mut self) -> usize {
|
||||
let mut marked = vec![false; self.slots.len()];
|
||||
let mut pending = Vec::new();
|
||||
for slot in &self.slots {
|
||||
if let Some(object) = &slot.object
|
||||
&& object.header.external_refs > 0
|
||||
{
|
||||
pending.push(object.header.identity);
|
||||
}
|
||||
}
|
||||
while let Some(reference) = pending.pop() {
|
||||
if marked[reference.index] {
|
||||
continue;
|
||||
}
|
||||
marked[reference.index] = true;
|
||||
pending.extend(self.get(reference).unwrap().edges());
|
||||
}
|
||||
let garbage: Vec<_> = self
|
||||
.slots
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(index, _)| !marked[*index])
|
||||
.filter_map(|(_, slot)| slot.object.as_ref().map(|object| object.header.identity))
|
||||
.collect();
|
||||
let objects: Vec<_> = garbage
|
||||
.iter()
|
||||
.map(|reference| self.take(*reference))
|
||||
.collect();
|
||||
// All garbage slots are detached before touching edges or running Drop.
|
||||
for object in &objects {
|
||||
for target in object.edges() {
|
||||
if let Ok(survivor) = self.get_mut(target) {
|
||||
survivor.header.strong_refs -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let collected = objects.len();
|
||||
drop(objects);
|
||||
collected
|
||||
}
|
||||
|
||||
pub(crate) fn len(&self) -> usize {
|
||||
self.live
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Record;
|
||||
|
||||
#[test]
|
||||
fn invalid_slots_and_generations_are_rejected() {
|
||||
let mut heap = Heap::new().unwrap();
|
||||
let reference = heap.allocate(Box::new(Record)).unwrap();
|
||||
let invalid = ObjectRef {
|
||||
index: usize::MAX,
|
||||
..reference
|
||||
};
|
||||
let stale = ObjectRef {
|
||||
generation: 1,
|
||||
..reference
|
||||
};
|
||||
assert!(matches!(heap.get(invalid), Err(VmError::InvalidHandle)));
|
||||
assert!(matches!(heap.get_mut(stale), Err(VmError::InvalidHandle)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reused_slots_never_alias_and_exhausted_generations_retire() {
|
||||
let mut heap = Heap::new().unwrap();
|
||||
let first = heap.allocate(Box::new(Record)).unwrap();
|
||||
heap.release(first).unwrap();
|
||||
let second = heap.allocate(Box::new(Record)).unwrap();
|
||||
assert_eq!(first.index, second.index);
|
||||
assert_ne!(first.generation, second.generation);
|
||||
assert!(matches!(heap.get(first), Err(VmError::InvalidHandle)));
|
||||
heap.slots[second.index].generation = u64::MAX;
|
||||
let last = ObjectRef {
|
||||
generation: u64::MAX,
|
||||
..second
|
||||
};
|
||||
heap.slots[last.index]
|
||||
.object
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.header
|
||||
.identity = last;
|
||||
heap.release(last).unwrap();
|
||||
let next = heap.allocate(Box::new(Record)).unwrap();
|
||||
assert_ne!(next.index, last.index);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_reference_increments_leave_counts_and_attributes_unchanged() {
|
||||
let mut heap = Heap::new().unwrap();
|
||||
let owner = heap.allocate(Box::new(Record)).unwrap();
|
||||
let target = heap.allocate(Box::new(Record)).unwrap();
|
||||
heap.get_mut(target).unwrap().header.strong_refs = usize::MAX;
|
||||
assert_eq!(heap.retain(target), Err(VmError::RefCountOverflow));
|
||||
assert_eq!(heap.get(target).unwrap().header.external_refs, 1);
|
||||
assert_eq!(
|
||||
heap.set_attr(owner, "edge".into(), Value::Object(target)),
|
||||
Err(VmError::RefCountOverflow)
|
||||
);
|
||||
assert!(heap.get(owner).unwrap().attributes.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//! Embeddable object runtime. No language type system, parser or executor.
|
||||
//!
|
||||
//! ```
|
||||
//! use aster_core::{Value, Vm};
|
||||
//! let mut vm = Vm::new()?;
|
||||
//! let object = vm.new_object()?; // One owned external reference.
|
||||
//! vm.set_attr(object, "x", Value::Number(42_i64.into()))?;
|
||||
//! assert_eq!(vm.get_attr(object, "x")?, Value::Number(42_i64.into()));
|
||||
//! vm.retain(object)?; // Own an additional reference.
|
||||
//! vm.release(object)?;
|
||||
//! assert_eq!(vm.ref_count(object)?, 1);
|
||||
//! vm.release(object)?; // The object is destroyed.
|
||||
//! assert_eq!(vm.object_count(), 0);
|
||||
//! # Ok::<(), aster_core::VmError>(())
|
||||
//! ```
|
||||
|
||||
mod builtins;
|
||||
mod heap;
|
||||
mod number;
|
||||
mod object;
|
||||
mod vm;
|
||||
mod vm_builtins;
|
||||
|
||||
pub use builtins::{
|
||||
EndorBaseExceptionObject, EndorCellObject, EndorCode, EndorDictObject, EndorFunctionObject,
|
||||
EndorStringObject, EndorTracebackEntry,
|
||||
};
|
||||
pub use heap::ObjectRef;
|
||||
pub use number::{
|
||||
EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational, EndorReal,
|
||||
};
|
||||
pub use object::{
|
||||
CompareOp, EndorNativeCall, EndorNativeFunctionObject, EndorObject, NativeCall, NativeFunction,
|
||||
ObjectHeader, ObjectPayload, Record, Value,
|
||||
};
|
||||
pub use vm::{Vm, VmError};
|
||||
@@ -0,0 +1,317 @@
|
||||
//! Lossless number representations. Arithmetic and numeric equivalence are
|
||||
//! deliberately separate from representation equality.
|
||||
|
||||
use std::any::Any;
|
||||
use std::fmt;
|
||||
|
||||
use crate::{CompareOp, ObjectPayload, ObjectRef, VmError};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EndorInteger {
|
||||
negative: bool,
|
||||
digits: String,
|
||||
}
|
||||
|
||||
impl EndorInteger {
|
||||
/// Decimal digits of any length, limited only by available memory.
|
||||
pub fn parse(text: &str) -> Result<Self, VmError> {
|
||||
let (negative, digits) = split_sign(text);
|
||||
if !is_digits(digits) {
|
||||
return Err(VmError::InvalidArgument("invalid integer"));
|
||||
}
|
||||
let digits = digits.trim_start_matches('0');
|
||||
Ok(Self {
|
||||
negative: negative && !digits.is_empty(),
|
||||
digits: if digits.is_empty() { "0" } else { digits }.into(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn digits(&self) -> &str {
|
||||
&self.digits
|
||||
}
|
||||
|
||||
pub fn is_negative(&self) -> bool {
|
||||
self.negative
|
||||
}
|
||||
|
||||
pub fn is_zero(&self) -> bool {
|
||||
self.digits == "0"
|
||||
}
|
||||
|
||||
pub fn to_i64(&self) -> Option<i64> {
|
||||
self.to_string().parse().ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for EndorInteger {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if self.negative {
|
||||
f.write_str("-")?;
|
||||
}
|
||||
f.write_str(&self.digits)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EndorDecimal {
|
||||
literal: String,
|
||||
zero: bool,
|
||||
}
|
||||
|
||||
impl EndorDecimal {
|
||||
/// Retains all digits, trailing zeroes, sign, and an arbitrarily long
|
||||
/// decimal exponent. No intermediate binary floating-point conversion.
|
||||
pub fn parse(text: &str) -> Result<Self, VmError> {
|
||||
let (_, unsigned) = split_sign(text);
|
||||
let mut parts = unsigned.split(['e', 'E']);
|
||||
let mantissa = parts.next().unwrap_or_default();
|
||||
if let Some(exponent) = parts.next()
|
||||
&& (!is_digits(split_sign(exponent).1) || parts.next().is_some())
|
||||
{
|
||||
return Err(VmError::InvalidArgument("invalid decimal exponent"));
|
||||
}
|
||||
let mut decimal_point = false;
|
||||
let mut digits = 0_usize;
|
||||
let mut zero = true;
|
||||
for byte in mantissa.bytes() {
|
||||
match byte {
|
||||
b'.' if !decimal_point => decimal_point = true,
|
||||
b'0'..=b'9' => {
|
||||
digits += 1;
|
||||
zero &= byte == b'0';
|
||||
}
|
||||
_ => return Err(VmError::InvalidArgument("invalid decimal")),
|
||||
}
|
||||
}
|
||||
if digits == 0 {
|
||||
return Err(VmError::InvalidArgument("decimal needs digits"));
|
||||
}
|
||||
Ok(Self {
|
||||
literal: text.into(),
|
||||
zero,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn literal(&self) -> &str {
|
||||
&self.literal
|
||||
}
|
||||
|
||||
pub fn is_zero(&self) -> bool {
|
||||
self.zero
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for EndorDecimal {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.literal)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EndorRational {
|
||||
numerator: EndorInteger,
|
||||
denominator: EndorInteger,
|
||||
}
|
||||
|
||||
impl EndorRational {
|
||||
/// Normalizes denominator sign, without reducing or rounding the fraction.
|
||||
pub fn new(
|
||||
mut numerator: EndorInteger,
|
||||
mut denominator: EndorInteger,
|
||||
) -> Result<Self, VmError> {
|
||||
if denominator.is_zero() {
|
||||
return Err(VmError::InvalidArgument("zero rational denominator"));
|
||||
}
|
||||
if denominator.negative {
|
||||
denominator.negative = false;
|
||||
if !numerator.is_zero() {
|
||||
numerator.negative = !numerator.negative;
|
||||
}
|
||||
}
|
||||
Ok(Self {
|
||||
numerator,
|
||||
denominator,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn numerator(&self) -> &EndorInteger {
|
||||
&self.numerator
|
||||
}
|
||||
|
||||
pub fn denominator(&self) -> &EndorInteger {
|
||||
&self.denominator
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for EndorRational {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}/{}", self.numerator, self.denominator)
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal storage forms within one Endor number API, not language classes.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EndorReal {
|
||||
Integer(EndorInteger),
|
||||
Decimal(EndorDecimal),
|
||||
Rational(EndorRational),
|
||||
/// IEEE-754 bits preserve signed zero, infinities and NaN payloads.
|
||||
BinaryFloat(u64),
|
||||
}
|
||||
|
||||
impl EndorReal {
|
||||
pub fn is_zero(&self) -> bool {
|
||||
match self {
|
||||
Self::Integer(value) => value.is_zero(),
|
||||
Self::Decimal(value) => value.is_zero(),
|
||||
Self::Rational(value) => value.numerator.is_zero(),
|
||||
Self::BinaryFloat(bits) => f64::from_bits(*bits) == 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for EndorReal {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Integer(value) => value.fmt(f),
|
||||
Self::Decimal(value) => value.fmt(f),
|
||||
Self::Rational(value) => value.fmt(f),
|
||||
Self::BinaryFloat(bits) => f64::from_bits(*bits).fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Unified, lossless number value. PartialEq/Eq compare representations, NOT
|
||||
/// mathematical values: decimal scale and IEEE NaN bits remain observable.
|
||||
/// Language-level comparison and hashing are not defined by these Rust traits.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EndorNumber {
|
||||
Real(EndorReal),
|
||||
Complex {
|
||||
real: EndorReal,
|
||||
imaginary: EndorReal,
|
||||
},
|
||||
}
|
||||
|
||||
impl EndorNumber {
|
||||
pub fn integer(text: &str) -> Result<Self, VmError> {
|
||||
Ok(Self::Real(EndorReal::Integer(EndorInteger::parse(text)?)))
|
||||
}
|
||||
|
||||
pub fn decimal(text: &str) -> Result<Self, VmError> {
|
||||
Ok(Self::Real(EndorReal::Decimal(EndorDecimal::parse(text)?)))
|
||||
}
|
||||
|
||||
pub fn rational(numerator: &str, denominator: &str) -> Result<Self, VmError> {
|
||||
Ok(Self::Real(EndorReal::Rational(EndorRational::new(
|
||||
EndorInteger::parse(numerator)?,
|
||||
EndorInteger::parse(denominator)?,
|
||||
)?)))
|
||||
}
|
||||
|
||||
pub fn binary_float(value: f64) -> Self {
|
||||
Self::Real(EndorReal::BinaryFloat(value.to_bits()))
|
||||
}
|
||||
|
||||
pub fn complex(real: EndorReal, imaginary: EndorReal) -> Self {
|
||||
Self::Complex { real, imaginary }
|
||||
}
|
||||
|
||||
pub fn as_real(&self) -> Option<&EndorReal> {
|
||||
match self {
|
||||
Self::Real(value) => Some(value),
|
||||
Self::Complex { .. } => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_i64(&self) -> Option<i64> {
|
||||
match self {
|
||||
Self::Real(EndorReal::Integer(value)) => value.to_i64(),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_zero(&self) -> bool {
|
||||
match self {
|
||||
Self::Real(value) => value.is_zero(),
|
||||
Self::Complex { real, imaginary } => real.is_zero() && imaginary.is_zero(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for EndorNumber {
|
||||
fn from(value: i64) -> Self {
|
||||
Self::integer(&value.to_string()).expect("i64 always has valid decimal digits")
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for EndorNumber {
|
||||
fn from(value: f64) -> Self {
|
||||
Self::binary_float(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for EndorNumber {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Real(value) => value.fmt(f),
|
||||
Self::Complex { real, imaginary } => write!(f, "({real} + ({imaginary})i)"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EndorNumberObject {
|
||||
value: EndorNumber,
|
||||
}
|
||||
|
||||
impl EndorNumberObject {
|
||||
pub fn new(value: EndorNumber) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
|
||||
pub fn value(&self) -> &EndorNumber {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectPayload for EndorNumberObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"number"
|
||||
}
|
||||
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(self.value.to_string())
|
||||
}
|
||||
|
||||
fn is_true(&self) -> Result<bool, VmError> {
|
||||
Ok(!self.value.is_zero())
|
||||
}
|
||||
|
||||
fn hash(&self, _identity: ObjectRef) -> Result<u64, VmError> {
|
||||
Err(VmError::UnsupportedOperation("numeric hash"))
|
||||
}
|
||||
|
||||
fn compare(&self, other: &dyn ObjectPayload, _op: CompareOp) -> Result<Option<bool>, VmError> {
|
||||
if other.as_any().is::<Self>() {
|
||||
return Err(VmError::UnsupportedOperation("numeric comparison"));
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_sign(text: &str) -> (bool, &str) {
|
||||
if let Some(rest) = text.strip_prefix('-') {
|
||||
(true, rest)
|
||||
} else {
|
||||
(false, text.strip_prefix('+').unwrap_or(text))
|
||||
}
|
||||
}
|
||||
|
||||
fn is_digits(text: &str) -> bool {
|
||||
!text.is_empty() && text.bytes().all(|byte| byte.is_ascii_digit())
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
use std::any::Any;
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::{EndorNumber, ObjectRef, VmError};
|
||||
|
||||
/// Immediate values stay outside the object heap. Object values are borrowed
|
||||
/// handles: copying or cloning one does not retain an external strong reference.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Nil,
|
||||
Bool(bool),
|
||||
/// Rust equality preserves representation; it is not numeric equivalence.
|
||||
Number(EndorNumber),
|
||||
Text(String),
|
||||
Object(ObjectRef),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CompareOp {
|
||||
Eq,
|
||||
Ne,
|
||||
Lt,
|
||||
Le,
|
||||
Gt,
|
||||
Ge,
|
||||
}
|
||||
|
||||
impl CompareOp {
|
||||
pub(crate) fn reflected(self) -> Self {
|
||||
match self {
|
||||
Self::Eq => Self::Eq,
|
||||
Self::Ne => Self::Ne,
|
||||
Self::Lt => Self::Gt,
|
||||
Self::Le => Self::Ge,
|
||||
Self::Gt => Self::Lt,
|
||||
Self::Ge => Self::Le,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Rust-side operation interface, not a language type or metaclass.
|
||||
///
|
||||
/// Implementations must report every payload-owned object reference in trace(),
|
||||
/// including duplicates. Those edges must stay immutable after allocation;
|
||||
/// mutable object references belong in VM-managed attributes or built-in
|
||||
/// containers updated transactionally by the VM instead.
|
||||
/// Rust Drop releases host resources when the payload is destroyed.
|
||||
/// Equal payloads must provide compatible hashes, or reject hashing explicitly.
|
||||
pub trait ObjectPayload: Any + fmt::Debug + Send + Sync {
|
||||
fn as_any(&self) -> &dyn Any;
|
||||
|
||||
/// A diagnostic label only, with no language type or inheritance semantics.
|
||||
fn kind_name(&self) -> &'static str;
|
||||
|
||||
fn trace(&self, _visit: &mut dyn FnMut(ObjectRef)) {}
|
||||
|
||||
fn allows_attributes(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn repr(&self, identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("<{} {identity}>", self.kind_name()))
|
||||
}
|
||||
|
||||
fn str(&self, identity: ObjectRef) -> Result<String, VmError> {
|
||||
self.repr(identity)
|
||||
}
|
||||
|
||||
fn is_true(&self) -> Result<bool, VmError> {
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
fn hash(&self, identity: ObjectRef) -> Result<u64, VmError> {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
identity.hash(&mut hasher);
|
||||
Ok(hasher.finish())
|
||||
}
|
||||
|
||||
/// None means "not implemented"; the VM tries the reflected operation,
|
||||
/// then falls back to identity for Eq/Ne or rejects ordering.
|
||||
fn compare(
|
||||
&self,
|
||||
_other: &dyn ObjectPayload,
|
||||
_operation: CompareOp,
|
||||
) -> Result<Option<bool>, VmError> {
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Object arguments and object results are borrowed handles. Native calls
|
||||
/// compute values without mutating the VM; this is not a language executor.
|
||||
fn call(&self, _arguments: &[Value]) -> Result<Value, VmError> {
|
||||
Err(VmError::UnsupportedOperation("call"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Record;
|
||||
|
||||
impl ObjectPayload for Record {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"record"
|
||||
}
|
||||
|
||||
fn allows_attributes(&self) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
pub type EndorNativeCall = fn(&[Value]) -> Result<Value, VmError>;
|
||||
pub type NativeCall = EndorNativeCall;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EndorNativeFunctionObject {
|
||||
name: String,
|
||||
function: NativeCall,
|
||||
}
|
||||
|
||||
impl EndorNativeFunctionObject {
|
||||
pub fn new(name: impl Into<String>, function: NativeCall) -> Self {
|
||||
Self {
|
||||
name: name.into(),
|
||||
function,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
||||
/// Compatibility name for existing embedders.
|
||||
pub type NativeFunction = EndorNativeFunctionObject;
|
||||
|
||||
impl ObjectPayload for EndorNativeFunctionObject {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn kind_name(&self) -> &'static str {
|
||||
"native-function"
|
||||
}
|
||||
|
||||
fn repr(&self, _identity: ObjectRef) -> Result<String, VmError> {
|
||||
Ok(format!("<native-function {}>", self.name))
|
||||
}
|
||||
|
||||
fn call(&self, arguments: &[Value]) -> Result<Value, VmError> {
|
||||
(self.function)(arguments)
|
||||
}
|
||||
}
|
||||
|
||||
/// Common runtime state. There is deliberately no language type pointer.
|
||||
#[derive(Debug)]
|
||||
pub struct ObjectHeader {
|
||||
pub(crate) identity: ObjectRef,
|
||||
pub(crate) strong_refs: usize,
|
||||
pub(crate) external_refs: usize,
|
||||
}
|
||||
|
||||
impl ObjectHeader {
|
||||
pub fn identity(&self) -> ObjectRef {
|
||||
self.identity
|
||||
}
|
||||
|
||||
pub fn ref_count(&self) -> usize {
|
||||
self.strong_refs
|
||||
}
|
||||
|
||||
pub fn external_ref_count(&self) -> usize {
|
||||
self.external_refs
|
||||
}
|
||||
}
|
||||
|
||||
/// Runtime object foundation, analogous in role to CPython's common object
|
||||
/// structure, without its type system or binary layout. The heap boxes this
|
||||
/// structure so its address remains stable throughout its lifetime.
|
||||
#[derive(Debug)]
|
||||
pub struct EndorObject {
|
||||
pub(crate) header: ObjectHeader,
|
||||
pub(crate) payload: Box<dyn ObjectPayload>,
|
||||
pub(crate) attributes: BTreeMap<String, Value>,
|
||||
pub(crate) payload_edges: Vec<ObjectRef>,
|
||||
}
|
||||
|
||||
impl EndorObject {
|
||||
pub fn header(&self) -> &ObjectHeader {
|
||||
&self.header
|
||||
}
|
||||
|
||||
pub fn payload(&self) -> &dyn ObjectPayload {
|
||||
self.payload.as_ref()
|
||||
}
|
||||
|
||||
/// Visits owned edges, not external roots. Payload edges are snapshotted at
|
||||
/// allocation and refreshed on VM-managed payload replacement; attributes
|
||||
/// are maintained by the VM's reference accounting.
|
||||
pub fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
|
||||
for reference in &self.payload_edges {
|
||||
visit(*reference);
|
||||
}
|
||||
for value in self.attributes.values() {
|
||||
if let Value::Object(reference) = value {
|
||||
visit(*reference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn edges(&self) -> Vec<ObjectRef> {
|
||||
let mut edges = Vec::new();
|
||||
self.trace(&mut |reference| edges.push(reference));
|
||||
edges
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::{
|
||||
CompareOp, EndorBaseExceptionObject, EndorCellObject, EndorDictObject, EndorFunctionObject,
|
||||
EndorNumber, EndorNumberObject, EndorStringObject, ObjectPayload, ObjectRef, Value, Vm,
|
||||
VmError,
|
||||
};
|
||||
|
||||
impl Vm {
|
||||
pub(crate) fn validate_builtin(&self, payload: &dyn ObjectPayload) -> Result<(), VmError> {
|
||||
if let Some(function) = payload.as_any().downcast_ref::<EndorFunctionObject>() {
|
||||
self.payload::<EndorDictObject>(function.globals(), "dict")?;
|
||||
for cell in function.closure() {
|
||||
self.payload::<EndorCellObject>(*cell, "cell")?;
|
||||
}
|
||||
}
|
||||
if let Some(exception) = payload.as_any().downcast_ref::<EndorBaseExceptionObject>() {
|
||||
for reference in [exception.cause(), exception.context()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
self.payload::<EndorBaseExceptionObject>(reference, "exception")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn payload<T: ObjectPayload>(
|
||||
&self,
|
||||
reference: ObjectRef,
|
||||
expected: &'static str,
|
||||
) -> Result<&T, VmError> {
|
||||
self.object(reference)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.downcast_ref()
|
||||
.ok_or(VmError::ObjectKindMismatch(expected))
|
||||
}
|
||||
|
||||
pub fn new_string(&mut self, text: impl Into<String>) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(EndorStringObject::new(text))
|
||||
}
|
||||
|
||||
pub fn new_number(&mut self, number: EndorNumber) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(EndorNumberObject::new(number))
|
||||
}
|
||||
|
||||
pub fn new_dict(&mut self) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(EndorDictObject::new())
|
||||
}
|
||||
|
||||
/// Returns borrowed keys and values in insertion order.
|
||||
pub fn dict_items(&self, dictionary: ObjectRef) -> Result<&[(Value, Value)], VmError> {
|
||||
Ok(self
|
||||
.payload::<EndorDictObject>(dictionary, "dict")?
|
||||
.entries())
|
||||
}
|
||||
|
||||
pub fn dict_get(&self, dictionary: ObjectRef, key: &Value) -> Result<Option<Value>, VmError> {
|
||||
let dict = self.payload::<EndorDictObject>(dictionary, "dict")?;
|
||||
self.key_hash(key)?;
|
||||
Ok(self
|
||||
.dict_position(dict, key)?
|
||||
.map(|index| dict.entries[index].1.clone()))
|
||||
}
|
||||
|
||||
pub fn dict_set(
|
||||
&mut self,
|
||||
dictionary: ObjectRef,
|
||||
key: Value,
|
||||
value: Value,
|
||||
) -> Result<(), VmError> {
|
||||
let mut dict = self.payload::<EndorDictObject>(dictionary, "dict")?.clone();
|
||||
self.key_hash(&key)?;
|
||||
self.validate_value(&value)?;
|
||||
if let Some(index) = self.dict_position(&dict, &key)? {
|
||||
// Preserve the first key object and insertion position.
|
||||
dict.entries[index].1 = value;
|
||||
} else {
|
||||
dict.entries.push((key, value));
|
||||
}
|
||||
self.heap.replace_payload(dictionary, dict)
|
||||
}
|
||||
|
||||
pub fn dict_delete(&mut self, dictionary: ObjectRef, key: &Value) -> Result<bool, VmError> {
|
||||
let mut dict = self.payload::<EndorDictObject>(dictionary, "dict")?.clone();
|
||||
self.key_hash(key)?;
|
||||
let Some(index) = self.dict_position(&dict, key)? else {
|
||||
return Ok(false);
|
||||
};
|
||||
dict.entries.remove(index);
|
||||
self.heap.replace_payload(dictionary, dict)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn dict_clear(&mut self, dictionary: ObjectRef) -> Result<(), VmError> {
|
||||
self.payload::<EndorDictObject>(dictionary, "dict")?;
|
||||
self.heap
|
||||
.replace_payload(dictionary, EndorDictObject::new())
|
||||
}
|
||||
|
||||
fn dict_position(&self, dict: &EndorDictObject, key: &Value) -> Result<Option<usize>, VmError> {
|
||||
for (index, (existing, _)) in dict.entries.iter().enumerate() {
|
||||
if self.key_equal(existing, key)? {
|
||||
return Ok(Some(index));
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
fn key_hash(&self, value: &Value) -> Result<u64, VmError> {
|
||||
self.validate_value(value)?;
|
||||
let mut hasher = DefaultHasher::new();
|
||||
match value {
|
||||
Value::Nil => "nil".hash(&mut hasher),
|
||||
Value::Bool(value) => value.hash(&mut hasher),
|
||||
Value::Text(value) => value.hash(&mut hasher),
|
||||
Value::Number(_) => return Err(VmError::UnsupportedOperation("numeric hash")),
|
||||
Value::Object(reference) => return self.hash(*reference),
|
||||
}
|
||||
Ok(hasher.finish())
|
||||
}
|
||||
|
||||
fn key_equal(&self, left: &Value, right: &Value) -> Result<bool, VmError> {
|
||||
let text = |value: &Value| -> Result<Option<String>, VmError> {
|
||||
Ok(match value {
|
||||
Value::Text(text) => Some(text.clone()),
|
||||
Value::Object(reference) => self
|
||||
.object(*reference)?
|
||||
.payload()
|
||||
.as_any()
|
||||
.downcast_ref::<EndorStringObject>()
|
||||
.map(|string| string.as_str().to_owned()),
|
||||
_ => None,
|
||||
})
|
||||
};
|
||||
if let (Some(left), Some(right)) = (text(left)?, text(right)?) {
|
||||
return Ok(left == right);
|
||||
}
|
||||
match (left, right) {
|
||||
(Value::Object(left), Value::Object(right)) => {
|
||||
self.compare(*left, *right, CompareOp::Eq)
|
||||
}
|
||||
_ => Ok(left == right),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_cell(&mut self, value: Option<Value>) -> Result<ObjectRef, VmError> {
|
||||
self.allocate(EndorCellObject::new(value))
|
||||
}
|
||||
|
||||
/// Reads a borrowed value; an empty cell is not Value::Nil.
|
||||
pub fn cell_get(&self, cell: ObjectRef) -> Result<Value, VmError> {
|
||||
self.payload::<EndorCellObject>(cell, "cell")?
|
||||
.value()
|
||||
.cloned()
|
||||
.ok_or(VmError::EmptyCell)
|
||||
}
|
||||
|
||||
pub fn cell_set(&mut self, cell: ObjectRef, value: Value) -> Result<(), VmError> {
|
||||
self.payload::<EndorCellObject>(cell, "cell")?;
|
||||
self.heap
|
||||
.replace_payload(cell, EndorCellObject::new(Some(value)))
|
||||
}
|
||||
|
||||
pub fn cell_clear(&mut self, cell: ObjectRef) -> Result<(), VmError> {
|
||||
self.payload::<EndorCellObject>(cell, "cell")?;
|
||||
self.heap.replace_payload(cell, EndorCellObject::default())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user