Check source function annotations and unify managed function identity

This commit is contained in:
2026-10-21 22:38:22 +08:00
parent eb60212428
commit 6346041f77
27 changed files with 568 additions and 40 deletions
+39 -2
View File
@@ -10,6 +10,7 @@ use crate::{
#[derive(Debug)]
pub struct ObjectStore {
pub(crate) heap: Heap,
function_type: Option<WeakObjectRef>,
}
impl ObjectStore {
@@ -125,7 +126,10 @@ impl ObjectStore {
}
pub fn new() -> Result<Self, VmError> {
Ok(Self { heap: Heap::new()? })
Ok(Self {
heap: Heap::new()?,
function_type: None,
})
}
pub fn object_count(&self) -> usize {
@@ -136,7 +140,40 @@ impl ObjectStore {
/// 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))
let function = payload.is_function();
let object = self.heap.allocate(Box::new(payload))?;
if function && let Some(weak) = self.function_type {
let result = (|| {
let definition = self.upgrade(weak)?.ok_or(VmError::InvalidArgument(
"configured Function type has expired",
))?;
let result = self.attach_type(object, definition);
self.release(definition)?;
result
})();
if let Err(error) = result {
self.release(object)?;
return Err(error);
}
}
Ok(object)
}
pub fn set_function_type(&mut self, definition: ObjectRef) -> Result<(), VmError> {
self.payload::<crate::EndorTypeObject>(definition, "Type")?;
if self.is_read_only() {
return Err(VmError::UnsupportedOperation(
"configure Function type during read-only call",
));
}
let weak = self.downgrade(definition)?;
if self.function_type.is_some_and(|old| old != weak) {
return Err(VmError::InvalidArgument(
"Function type is already configured",
));
}
self.function_type = Some(weak);
Ok(())
}
pub fn new_object(&mut self) -> Result<ObjectRef, VmError> {