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
+81 -1
View File
@@ -9,11 +9,91 @@ use crate::{
/// reference accounting and builtin operations live in the objects crate.
#[derive(Debug)]
pub struct Vm {
objects: ObjectStore,
pub(crate) objects: ObjectStore,
pub(crate) last_traceback: Vec<ExecutionLocation>,
}
impl Vm {
pub fn define_method(
&mut self,
type_object: ObjectRef,
name: impl Into<String>,
function: ObjectRef,
) -> Result<(), VmError> {
self.objects.define_method(type_object, name, function)
}
/// Owned result; release_value when finished. Unlike get_attr, methods bind
/// their receiver and retain it until the bound method itself is released.
pub fn bind_attr(&mut self, object: ObjectRef, name: &str) -> Result<Value, VmError> {
self.objects.bind_attr(object, name)
}
pub fn bootstrap_types(&mut self) -> Result<crate::RootTypes, VmError> {
self.objects.bootstrap_types()
}
pub fn new_type(
&mut self,
name: impl Into<String>,
base: ObjectRef,
constructor: crate::TypeConstructor,
) -> Result<ObjectRef, VmError> {
self.objects.new_type(name, base, constructor)
}
pub fn attach_type(
&mut self,
object: ObjectRef,
type_object: ObjectRef,
) -> Result<(), VmError> {
self.objects.attach_type(object, type_object)
}
pub fn type_of(&self, object: ObjectRef) -> Result<Option<ObjectRef>, VmError> {
self.objects.type_of(object)
}
pub fn is_subtype(&self, candidate: ObjectRef, expected: ObjectRef) -> Result<bool, VmError> {
self.objects.is_subtype(candidate, expected)
}
pub fn is_instance(&self, object: ObjectRef, expected: ObjectRef) -> Result<bool, VmError> {
self.objects.is_instance(object, expected)
}
/// Read-only access for embedding protocols sharing the same object graph.
pub fn object_store(&self) -> &ObjectStore {
&self.objects
}
pub fn downgrade(&self, reference: ObjectRef) -> Result<crate::WeakObjectRef, VmError> {
self.objects.downgrade(reference)
}
pub fn upgrade(
&mut self,
reference: crate::WeakObjectRef,
) -> Result<Option<ObjectRef>, VmError> {
self.objects.upgrade(reference)
}
pub fn take_changes(&mut self) -> crate::ChangeBatch {
self.objects.take_changes()
}
pub fn change_version(&self, reference: ObjectRef) -> Result<u64, VmError> {
self.objects.change_version(reference)
}
pub fn replace_payload(
&mut self,
reference: ObjectRef,
payload: impl ObjectPayload,
) -> Result<(), VmError> {
self.objects.replace_payload(reference, payload)
}
pub fn new() -> Result<Self, VmError> {
Ok(Self {
objects: ObjectStore::new()?,