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
+1 -1
View File
@@ -7,7 +7,7 @@
| [runtime.rs](runtime.rs) | 空堆启动、默认操作、引用/借用、属性原子性、稳定地址、长链/循环、原生回调、VM 销毁 |
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
| [types.rs](types.rs) | 名义身份、继承与实例关系、类型边保活/循环回收、只读类型元数据与外来句柄、显式Trait身份/继承/参数约束及后代更新原子性;仅远端执行 |
| [types.rs](types.rs) | 名义身份、继承与实例关系、类型边保活/循环回收、只读类型元数据与外来句柄、显式Trait身份/继承/参数约束及后代更新原子性;统一函数分类与返回类型错误根清理;仅远端执行 |
| [native_context.rs](native_context.rs) | 原生作用域根、分配失败清理、委托、只读结果分配/写入拒绝/依赖与失败恢复、弱引用过期及有界变更;仅远端执行 |
核心对象现由 objects 定义,本目录保留既有 endor_core 导入与 Vm 方法的兼容性检查。独立对象存储检查见 [objects/tests](../../objects/tests/README.md)。堆内部伪造句柄与计数溢出检查在 objects 的 src/heap.rs,公开示例由 src/lib.rs rustdoc 提供。文件职责见 [src](../src/README.md),检查方式和结果边界见 [验证](../../../docs/operations/validation.md)。本地不执行测试;存在测试文件不代表某次提交已通过。返回 [core](../README.md)。
+131
View File
@@ -198,3 +198,134 @@ fn late_trait_registration_and_ancestor_updates_validate_existing_descendants()
Err(VmError::ForeignHandle)
);
}
#[test]
fn configured_function_identity_covers_source_native_and_bound_methods() {
use endor_core::{NativeFunction, SourceSignature};
let mut vm = Vm::new().unwrap();
let roots = vm.bootstrap_types().unwrap();
let function_type = vm
.new_type("Function", roots.object, TypeConstructor::Abstract)
.unwrap();
vm.set_function_type(function_type).unwrap();
let source = contract_method(&mut vm, 1);
let native = vm
.allocate(NativeFunction::new("native", |_| Ok(Value::Nil)))
.unwrap();
assert!(vm.is_instance(source, function_type).unwrap());
assert!(vm.is_instance(native, function_type).unwrap());
vm.attach_type(source, function_type).unwrap();
let class = vm
.new_class(
"Data",
roots.object,
vec![],
[("read".into(), source)].into(),
)
.unwrap();
let Value::Object(instance) = vm.call(class, &[]).unwrap() else {
panic!("instance required");
};
let Value::Object(bound) = vm.bind_attr(instance, "read").unwrap() else {
panic!("method required");
};
assert!(vm.is_instance(bound, function_type).unwrap());
assert!(!vm.is_instance(class, function_type).unwrap());
assert!(vm.set_function_type(class).is_err());
let invalid = endor_core::EndorCode::from_instructions(
&[
endor_core::Instruction::new(endor_core::Opcode::LoadConst, 0),
endor_core::Instruction::simple(endor_core::Opcode::ReturnValue),
],
vec![Value::Nil],
endor_core::CodeLayout::default(),
)
.unwrap()
.with_source_signature(SourceSignature {
parameters: vec!["Number".into()],
return_type: "Unit".into(),
method_owner: None,
});
assert!(invalid.is_err());
for object in [
source,
native,
class,
instance,
bound,
function_type,
roots.object,
roots.type_type,
] {
vm.release(object).unwrap();
}
vm.collect_cycles();
assert_eq!(vm.object_count(), 0);
}
#[test]
fn failed_nominal_return_releases_value_and_captured_type_roots() {
use endor_core::{
CodeLayout, EndorCode, EndorCodeObject, Instruction, Opcode, SourceSignature,
};
let mut vm = Vm::new().unwrap();
vm.set_source_type_checks(true).unwrap();
let roots = vm.bootstrap_types().unwrap();
let expected = vm
.new_type("Expected", roots.object, TypeConstructor::Record)
.unwrap();
let other = vm
.new_type("Other", roots.object, TypeConstructor::Record)
.unwrap();
let Value::Object(value) = vm.call(other, &[]).unwrap() else {
panic!("instance required");
};
let globals = vm.new_dict().unwrap();
vm.dict_set(
globals,
Value::Text("Expected".into()),
Value::Object(expected),
)
.unwrap();
let code = EndorCode::from_instructions(
&[
Instruction::new(Opcode::LoadConst, 0),
Instruction::simple(Opcode::ReturnValue),
],
vec![Value::Object(value)],
CodeLayout::default(),
)
.unwrap()
.with_source_signature(SourceSignature {
parameters: vec![],
return_type: "Expected".into(),
method_owner: None,
})
.unwrap();
let code = vm.allocate(EndorCodeObject::new(code)).unwrap();
let function = vm
.new_function("bad return", code, globals, vec![], vec![])
.unwrap();
let before_type = vm.ref_count(expected).unwrap();
let before_value = vm.ref_count(value).unwrap();
assert!(matches!(
vm.call(function, &[]),
Err(VmError::TypeMismatch { .. })
));
assert_eq!(vm.ref_count(expected).unwrap(), before_type);
assert_eq!(vm.ref_count(value).unwrap(), before_value);
for object in [
function,
code,
globals,
value,
expected,
other,
roots.object,
roots.type_type,
] {
vm.release(object).unwrap();
}
vm.collect_cycles();
assert_eq!(vm.object_count(), 0);
}