feat: add managed types, source classes and scoped native calls
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
| [runtime.rs](runtime.rs) | 空堆启动、默认操作、引用/借用、属性原子性、稳定地址、长链/循环、原生回调、VM 销毁 |
|
||||
| [builtins.rs](builtins.rs) | 无损 Number、Unicode 字符串、Dict 顺序与键、Cell、函数环境/共享代码、异常数据 |
|
||||
| [execution.rs](execution.rs) | wordcode 与校验、控制流、参数/默认值、嵌套帧、字节码函数创建/默认值/环境捕获、闭包、返回值存活、失败清理、限额 |
|
||||
| [types.rs](types.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)。
|
||||
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
//! Remote-only regression coverage. Do not execute locally.
|
||||
use endor_core::{
|
||||
ChangeKey, ContextNativeFunction, ExecutionLimits, NativeCallable, NativeContext, NativeResult,
|
||||
ObjectRef, Value, Vm, VmError,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Allocate(bool);
|
||||
impl NativeCallable for Allocate {
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, _args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
let value = cx.new_object()?;
|
||||
cx.set_attr(value, "label", Value::Text("scoped".into()))?;
|
||||
let _temporary = cx.new_object()?;
|
||||
if self.0 {
|
||||
return Err(VmError::InvalidArgument("intentional failure"));
|
||||
}
|
||||
Ok(NativeResult::Return(Value::Object(value)))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_allocations_return_one_root_and_clean_up_errors() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let function = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"allocate",
|
||||
Allocate(false),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let value = vm.call(function, &[]).unwrap();
|
||||
let Value::Object(object) = value else {
|
||||
panic!("expected object")
|
||||
};
|
||||
assert_eq!(vm.ref_count(object).unwrap(), 1);
|
||||
assert_eq!(vm.object_count(), 2);
|
||||
vm.release_value(value).unwrap();
|
||||
vm.replace_payload(
|
||||
function,
|
||||
ContextNativeFunction::new("fail", Allocate(true), vec![]),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(vm.call(function, &[]).is_err());
|
||||
assert_eq!(vm.object_count(), 1);
|
||||
vm.release(function).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Delegate(ObjectRef);
|
||||
impl NativeCallable for Delegate {
|
||||
fn invoke(&self, _cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
Ok(NativeResult::Invoke {
|
||||
function: self.0,
|
||||
arguments: args.to_vec(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_delegation_shares_budget_and_cleans_owned_arguments() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let target = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"target",
|
||||
Allocate(false),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let delegate = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"delegate",
|
||||
Delegate(target),
|
||||
vec![target],
|
||||
))
|
||||
.unwrap();
|
||||
let arg = vm.new_object().unwrap();
|
||||
assert!(
|
||||
vm.call_with_limits(
|
||||
delegate,
|
||||
&[Value::Object(arg)],
|
||||
ExecutionLimits {
|
||||
max_frames: 1,
|
||||
max_instructions: 1
|
||||
}
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(vm.ref_count(arg).unwrap(), 1);
|
||||
assert_eq!(vm.ref_count(target).unwrap(), 2);
|
||||
let value = vm.call(delegate, &[]).unwrap();
|
||||
vm.release_value(value).unwrap();
|
||||
vm.release(delegate).unwrap();
|
||||
vm.release(target).unwrap();
|
||||
vm.release(arg).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn weak_references_expire_without_retaining_reused_slots() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let object = vm.new_object().unwrap();
|
||||
let weak = vm.downgrade(object).unwrap();
|
||||
let upgraded = vm.upgrade(weak).unwrap().unwrap();
|
||||
vm.release(upgraded).unwrap();
|
||||
vm.release(object).unwrap();
|
||||
let replacement = vm.new_object().unwrap();
|
||||
assert!(vm.upgrade(weak).unwrap().is_none());
|
||||
let mut foreign = Vm::new().unwrap();
|
||||
assert_eq!(foreign.upgrade(weak), Err(VmError::ForeignHandle));
|
||||
vm.release(replacement).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changes_coalesce_cover_containers_and_do_not_keep_sources_alive() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let object = vm.new_object().unwrap();
|
||||
vm.set_attr(object, "value", Value::Bool(false)).unwrap();
|
||||
vm.set_attr(object, "value", Value::Bool(true)).unwrap();
|
||||
let version = vm.change_version(object).unwrap();
|
||||
let changes = vm.take_changes();
|
||||
assert!(!changes.rescan_required);
|
||||
assert_eq!(changes.changes.len(), 1);
|
||||
assert_eq!(changes.changes[0].key, ChangeKey::Attribute("value".into()));
|
||||
assert_eq!(changes.changes[0].version, version);
|
||||
let cell = vm.new_cell(None).unwrap();
|
||||
vm.cell_set(cell, Value::Object(object)).unwrap();
|
||||
vm.cell_clear(cell).unwrap();
|
||||
assert_eq!(vm.take_changes().changes[0].key, ChangeKey::Contents);
|
||||
vm.set_attr(object, "self", Value::Object(object)).unwrap();
|
||||
vm.release(object).unwrap();
|
||||
assert_eq!(vm.collect_cycles(), 1);
|
||||
vm.release(cell).unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn notification_overflow_requires_rescan_instead_of_losing_changes_silently() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let object = vm.new_object().unwrap();
|
||||
for index in 0..4100 {
|
||||
vm.set_attr(object, index.to_string(), Value::Nil).unwrap();
|
||||
}
|
||||
let changes = vm.take_changes();
|
||||
assert!(changes.rescan_required);
|
||||
assert!(changes.changes.is_empty());
|
||||
vm.set_attr(object, "after", Value::Nil).unwrap();
|
||||
assert_eq!(vm.take_changes().changes.len(), 1);
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
use endor_core::{TypeConstructor, Value, Vm, VmError};
|
||||
|
||||
#[test]
|
||||
fn nominal_types_have_distinct_inheritance_and_instance_relations() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
let roots = vm.bootstrap_types().unwrap();
|
||||
let block = vm
|
||||
.new_type("Block", roots.object, TypeConstructor::Abstract)
|
||||
.unwrap();
|
||||
let page = vm.new_type("Page", block, TypeConstructor::Record).unwrap();
|
||||
let other = vm.new_type("Page", block, TypeConstructor::Record).unwrap();
|
||||
assert!(vm.is_subtype(page, block).unwrap());
|
||||
assert!(!vm.is_subtype(block, page).unwrap());
|
||||
assert!(!vm.is_subtype(page, other).unwrap());
|
||||
assert!(vm.is_instance(page, roots.type_type).unwrap());
|
||||
assert!(vm.is_instance(roots.object, roots.type_type).unwrap());
|
||||
assert!(vm.is_instance(roots.type_type, roots.type_type).unwrap());
|
||||
assert!(!vm.is_subtype(roots.object, roots.type_type).unwrap());
|
||||
let Value::Object(instance) = vm.call(page, &[]).unwrap() else {
|
||||
panic!("instance required")
|
||||
};
|
||||
assert!(vm.is_instance(instance, block).unwrap());
|
||||
assert!(!vm.is_instance(instance, roots.type_type).unwrap());
|
||||
assert_eq!(vm.get_attr(instance, "type").unwrap(), Value::Object(page));
|
||||
assert_eq!(vm.get_attr(page, "base").unwrap(), Value::Object(block));
|
||||
assert!(vm.set_attr(instance, "type", Value::Object(other)).is_err());
|
||||
assert!(vm.attach_type(instance, other).is_err());
|
||||
assert!(vm.replace_payload(page, endor_core::Record).is_err());
|
||||
for object in [roots.object, roots.type_type, block, page, other] {
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
vm.collect_cycles();
|
||||
// The instance keeps its definition, base and metatype reachable without
|
||||
// any host-owned type registry. The unrelated same-name type was collected.
|
||||
assert_eq!(vm.object_count(), 5);
|
||||
assert!(vm.is_instance(instance, block).unwrap());
|
||||
vm.release(instance).unwrap();
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn foreign_type_edges_fail_without_changing_the_target() {
|
||||
let mut first = Vm::new().unwrap();
|
||||
let mut second = Vm::new().unwrap();
|
||||
let roots = first.bootstrap_types().unwrap();
|
||||
let object = second.new_object().unwrap();
|
||||
assert_eq!(
|
||||
second.attach_type(object, roots.object),
|
||||
Err(VmError::ForeignHandle)
|
||||
);
|
||||
assert_eq!(second.type_of(object).unwrap(), None);
|
||||
assert_eq!(second.ref_count(object).unwrap(), 1);
|
||||
second
|
||||
.set_attr(object, "type", Value::Text("existing field".into()))
|
||||
.unwrap();
|
||||
let local = second.bootstrap_types().unwrap();
|
||||
assert!(second.attach_type(object, local.object).is_err());
|
||||
assert_eq!(
|
||||
second.get_attr(object, "type").unwrap(),
|
||||
Value::Text("existing field".into())
|
||||
);
|
||||
assert_eq!(second.type_of(object).unwrap(), None);
|
||||
}
|
||||
Reference in New Issue
Block a user