Represent nullable and union native contracts with traced alternatives
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
|
||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
|
||||
| [types.rs](types.rs) | 名义身份、继承与实例关系、类型边保活/循环回收、只读类型元数据与外来句柄、显式Trait身份/继承/参数约束及后代更新原子性;统一函数分类、原生数量元数据/副作用前拒绝/能力更新/默认参数与返回类型错误根清理;仅远端执行 |
|
||||
| [native_context.rs](native_context.rs) | 原生作用域根、分配失败清理、原生签名/效果和委托返回根、绑定方法元数据、委托、只读结果分配/写入拒绝/依赖/校验与普通读取区分及失败恢复、弱引用过期及有界变更;仅远端执行 |
|
||||
| [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)。
|
||||
|
||||
|
||||
@@ -505,8 +505,8 @@ fn legacy_native_signatures_check_optional_variadic_types_and_metadata() {
|
||||
NativeFunction::new("legacy", echo).with_signature(CallSignature {
|
||||
arity: CallArity::range(0, usize::MAX),
|
||||
parameters: vec![],
|
||||
variadic: Some(T::Number),
|
||||
returns: T::Number,
|
||||
variadic: Some(T::Number.into()),
|
||||
returns: T::Number.into(),
|
||||
effect: CallEffect::ReadOnly,
|
||||
}),
|
||||
)
|
||||
@@ -585,15 +585,13 @@ fn delegated_return_type_survives_removal_of_the_original_signature_edges() {
|
||||
let source = vm
|
||||
.new_function("source", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
let mut signature = CallSignature::exact(vec![], T::Any, CallEffect::Mutating);
|
||||
signature.returns = endor_core::CallType::Union(vec![T::Nominal(definition), T::Number]);
|
||||
let wrapper = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"erased",
|
||||
EraseThenDelegate {
|
||||
signature: CallSignature::exact(
|
||||
vec![],
|
||||
T::Nominal(definition),
|
||||
CallEffect::Mutating,
|
||||
),
|
||||
signature,
|
||||
target: source,
|
||||
},
|
||||
vec![source],
|
||||
@@ -601,7 +599,7 @@ fn delegated_return_type_survives_removal_of_the_original_signature_edges() {
|
||||
.unwrap();
|
||||
vm.release(definition).unwrap();
|
||||
assert!(
|
||||
matches!(vm.call(wrapper, &[]), Err(VmError::TypeMismatch { function, expected, .. }) if function == "erased" && expected == "Transient")
|
||||
matches!(vm.call(wrapper, &[]), Err(VmError::TypeMismatch { function, expected, .. }) if function == "erased" && expected == "Transient | Number")
|
||||
);
|
||||
assert!(vm.upgrade(weak).unwrap().is_none());
|
||||
for object in [
|
||||
@@ -617,3 +615,107 @@ fn delegated_return_type_survives_removal_of_the_original_signature_edges() {
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_union_and_nullable_contracts_accept_each_branch_and_reject_invalid_metadata() {
|
||||
use endor_core::{AnnotationType as T, CallEffect, CallSignature, CallType};
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicUsize, Ordering},
|
||||
};
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let mut signature = CallSignature::exact(vec![T::Any], T::Any, CallEffect::ReadOnly);
|
||||
signature.parameters[0] = CallType::Union(vec![T::String, T::Bool]);
|
||||
signature.returns = signature.parameters[0].clone();
|
||||
let function = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"union",
|
||||
TypedNative {
|
||||
signature,
|
||||
calls: calls.clone(),
|
||||
delegate: None,
|
||||
allocate_result: false,
|
||||
},
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
for value in [
|
||||
Value::Text("capture".into()),
|
||||
Value::Bool(true),
|
||||
Value::Bool(false),
|
||||
] {
|
||||
assert_eq!(
|
||||
vm.call(function, std::slice::from_ref(&value)).unwrap(),
|
||||
value
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
matches!(vm.call(function, &[Value::Number(3.into())]), Err(VmError::TypeMismatch { expected, .. }) if expected == "String | Bool")
|
||||
);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 3);
|
||||
let mut signature = CallSignature::exact(vec![T::Any], T::Any, CallEffect::ReadOnly);
|
||||
signature.parameters[0] = CallType::nullable(T::Number);
|
||||
signature.returns = signature.parameters[0].clone();
|
||||
vm.replace_payload(
|
||||
function,
|
||||
ContextNativeFunction::new(
|
||||
"nullable",
|
||||
TypedNative {
|
||||
signature,
|
||||
calls: calls.clone(),
|
||||
delegate: None,
|
||||
allocate_result: false,
|
||||
},
|
||||
vec![],
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
for value in [Value::Nil, Value::Number(2.into())] {
|
||||
assert_eq!(
|
||||
vm.call(function, std::slice::from_ref(&value)).unwrap(),
|
||||
value
|
||||
);
|
||||
}
|
||||
let record = vm.new_object().unwrap();
|
||||
let mut signature = CallSignature::exact(vec![T::Any], T::Any, CallEffect::ReadOnly);
|
||||
// A matching first branch must not hide a later invalid nominal definition.
|
||||
signature.parameters[0] = CallType::Union(vec![T::Any, T::Nominal(record)]);
|
||||
vm.replace_payload(
|
||||
function,
|
||||
ContextNativeFunction::new(
|
||||
"invalid union",
|
||||
TypedNative {
|
||||
signature,
|
||||
calls: calls.clone(),
|
||||
delegate: None,
|
||||
allocate_result: false,
|
||||
},
|
||||
vec![],
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(vm.call(function, &[Value::Nil]).is_err());
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 5);
|
||||
let mut signature = CallSignature::exact(vec![], T::Any, CallEffect::ReadOnly);
|
||||
signature.returns = CallType::Union(vec![]);
|
||||
vm.replace_payload(
|
||||
function,
|
||||
ContextNativeFunction::new(
|
||||
"empty union",
|
||||
TypedNative {
|
||||
signature,
|
||||
calls: calls.clone(),
|
||||
delegate: None,
|
||||
allocate_result: false,
|
||||
},
|
||||
vec![],
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(vm.call(function, &[]).is_err());
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 5);
|
||||
vm.release(record).unwrap();
|
||||
vm.release(function).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user