Validate declared class fields at construction and attribute writes

This commit is contained in:
2026-10-22 11:44:09 +08:00
parent 6346041f77
commit 20e29e3c5a
13 changed files with 342 additions and 26 deletions
+19
View File
@@ -11,9 +11,21 @@ use crate::{
pub struct ObjectStore {
pub(crate) heap: Heap,
function_type: Option<WeakObjectRef>,
pub(crate) field_type_checks: bool,
}
impl ObjectStore {
/// Opt-in declared-field checks, shared by VM and NativeContext writes.
pub fn set_field_type_checks(&mut self, enabled: bool) -> Result<(), VmError> {
if self.is_read_only() {
return Err(VmError::UnsupportedOperation(
"configure field type checks during read-only call",
));
}
self.field_type_checks = enabled;
Ok(())
}
/// Existing objects are immutable; fresh result graphs may be constructed.
/// Scopes must end in LIFO order. Read handles are borrowed, never GC roots.
pub fn begin_read_only(&mut self) -> usize {
@@ -129,6 +141,7 @@ impl ObjectStore {
Ok(Self {
heap: Heap::new()?,
function_type: None,
field_type_checks: false,
})
}
@@ -280,6 +293,9 @@ impl ObjectStore {
if name == "type" && self.type_of(reference)?.is_some() {
return Err(VmError::UnsupportedOperation("write object type"));
}
if self.field_type_checks {
self.check_field_write(reference, &name, &value)?;
}
let storage = *self
.attribute_path(reference)?
.last()
@@ -288,6 +304,9 @@ impl ObjectStore {
}
pub fn del_attr(&mut self, reference: ObjectRef, name: &str) -> Result<(), VmError> {
if self.field_type_checks && self.declared_field(reference, name)?.is_some() {
return Err(VmError::UnsupportedOperation("delete declared class field"));
}
if name == "type" && self.type_of(reference)?.is_some() {
return Err(VmError::UnsupportedOperation("delete object type"));
}