feat: add managed types, source classes and scoped native calls

This commit is contained in:
2026-10-21 01:47:08 +08:00
parent 7c842789c0
commit a970fd7707
48 changed files with 2069 additions and 134 deletions
+91 -3
View File
@@ -1,5 +1,8 @@
use crate::heap::Heap;
use crate::{CompareOp, EndorObject, ObjectPayload, ObjectRef, Record, Value, VmError};
use crate::{
ChangeBatch, ChangeKey, CompareOp, EndorObject, ObjectPayload, ObjectRef, Record, Value,
VmError, WeakObjectRef,
};
/// Object storage independent of the VM evaluator. Construction creates an empty
/// heap; dropping it releases all objects and their host resources. Mutable
@@ -10,6 +13,80 @@ pub struct ObjectStore {
}
impl ObjectStore {
pub fn downgrade(&self, reference: ObjectRef) -> Result<WeakObjectRef, VmError> {
self.object(reference)?;
Ok(WeakObjectRef(reference))
}
/// Returns a new owned root, or None when the original object was released.
/// A foreign store is an error, not an expired reference.
pub fn upgrade(&mut self, weak: WeakObjectRef) -> Result<Option<ObjectRef>, VmError> {
match self.object(weak.0) {
Ok(_) => {
self.retain(weak.0)?;
Ok(Some(weak.0))
}
Err(VmError::InvalidHandle) => Ok(None),
Err(error) => Err(error),
}
}
pub fn take_changes(&mut self) -> ChangeBatch {
self.heap.take_changes()
}
pub fn change_version(&self, reference: ObjectRef) -> Result<u64, VmError> {
Ok(self.object(reference)?.header().change_version())
}
/// Replace a host payload transactionally, including all owned GC edges.
pub fn replace_payload(
&mut self,
reference: ObjectRef,
payload: impl ObjectPayload,
) -> Result<(), VmError> {
if self
.object(reference)?
.payload()
.as_any()
.is::<crate::EndorTypeObject>()
{
return Err(VmError::UnsupportedOperation(
"replace immutable type definition",
));
}
self.validate_builtin(&payload)?;
self.heap.replace_payload(reference, payload)
}
pub fn assign_from(&mut self, target: ObjectRef, source: ObjectRef) -> Result<bool, VmError> {
self.heap.prepare_change(target)?;
self.object(source)?;
if let Some(cell) = self
.object(target)?
.payload()
.assignment_cell(source, self)?
{
// Replacing the Cell edge may otherwise reclaim a target which is
// reachable only through the edge being removed.
self.retain(target)?;
let result = self.cell_set(cell, Value::Object(source));
if result.is_ok() {
self.heap.record_change(target, ChangeKey::Contents);
}
self.release(target)?;
return result.map(|()| true);
}
let changed = self
.object(target)?
.payload()
.assign_from(self.object(source)?.payload())?;
if changed {
self.heap.record_change(target, ChangeKey::Contents);
}
Ok(changed)
}
pub fn new() -> Result<Self, VmError> {
Ok(Self { heap: Heap::new()? })
}
@@ -91,7 +168,11 @@ impl ObjectStore {
/// 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.lookup_attr(reference, name)
.map(|attribute| match attribute {
crate::types::AttributeLookup::Value(value) => value,
crate::types::AttributeLookup::Method { function, .. } => Value::Object(function),
})
}
/// Object-valued attributes own internal strong references independently
@@ -102,10 +183,17 @@ impl ObjectStore {
name: impl Into<String>,
value: Value,
) -> Result<(), VmError> {
self.heap.set_attr(reference, name.into(), value)
let name = name.into();
if name == "type" && self.type_of(reference)?.is_some() {
return Err(VmError::UnsupportedOperation("write object type"));
}
self.heap.set_attr(reference, name, value)
}
pub fn del_attr(&mut self, reference: ObjectRef, name: &str) -> Result<(), VmError> {
if name == "type" && self.type_of(reference)?.is_some() {
return Err(VmError::UnsupportedOperation("delete object type"));
}
self.heap.del_attr(reference, name)
}