Add explicit managed traits and inherited method arity contracts

This commit is contained in:
2026-10-21 21:34:46 +08:00
parent b7daabdaad
commit eb60212428
16 changed files with 454 additions and 16 deletions
+1 -1
View File
@@ -5,7 +5,7 @@
| 文件 | 职责 |
| --- | --- |
| [lib.rs](lib.rs) | VM 类型导出、objects 类型兼容重导出、宿主调用与字节码 rustdoc 示例 |
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 注册显式委托给 objects |
| [vm.rs](vm.rs) | Vm 拥有 ObjectStore 与错误位置记录;对象操作和宿主 new_class 和显式Trait注册/关系查询显式委托给 objects |
| [frame.rs](frame.rs) | EndorFrame、栈条目、执行位置元数据、ExecutionLimits |
| [eval.rs](eval.rs) | execute/call、只读调用与依赖结果、参数绑定、帧切换、取指分派、MakeFunction、结果根转移与失败清理 |
+2 -2
View File
@@ -443,7 +443,7 @@ impl Vm {
};
let fields = self
.dict_items(fields)?
.into_iter()
.iter()
.map(|(name, type_name)| match (name, type_name) {
(Value::Text(name), Value::Text(type_name)) => Ok(crate::ClassField {
name: name.clone(),
@@ -454,7 +454,7 @@ impl Vm {
.collect::<Result<Vec<_>, _>>()?;
let methods = self
.dict_items(methods)?
.into_iter()
.iter()
.map(|(name, function)| match (name, function) {
(Value::Text(name), Value::Object(function)) => Ok((name.clone(), *function)),
_ => Err(VmError::ObjectKindMismatch("class method")),
+3 -1
View File
@@ -54,5 +54,7 @@ pub use objects::{
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, Record, Value,
VmError, WeakObjectRef,
};
pub use objects::{ClassField, EndorTypeObject, RootTypes, TypeConstructor};
pub use objects::{
ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes, TypeConstructor,
};
pub use vm::Vm;
+25
View File
@@ -14,6 +14,31 @@ pub struct Vm {
}
impl Vm {
pub fn new_trait(
&mut self,
name: impl Into<String>,
requirements: Vec<crate::MethodRequirement>,
) -> Result<ObjectRef, VmError> {
self.objects.new_trait(name, requirements)
}
pub fn implement_trait(
&mut self,
definition: ObjectRef,
capability: ObjectRef,
) -> Result<(), VmError> {
self.objects.implement_trait(definition, capability)
}
pub fn implements_trait(
&self,
definition: ObjectRef,
capability: ObjectRef,
) -> Result<bool, VmError> {
self.objects.implements_trait(definition, capability)
}
pub fn supports(&self, object: ObjectRef, capability: ObjectRef) -> Result<bool, VmError> {
self.objects.supports(object, capability)
}
pub fn define_method(
&mut self,
type_object: ObjectRef,