feat: inherit native allocators and forward managed field storage

This commit is contained in:
2026-10-21 02:05:52 +08:00
parent a970fd7707
commit be9a032647
13 changed files with 275 additions and 23 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# 编译链路检查
[classes.rs](classes.rs) 覆盖源码 class、继承字段构造、动态方法覆盖、绑定接收者保活/回收、绑定后参数数量和重复成员/缺少 self 的预执行诊断。只编译,不在本地运行。
[classes.rs](classes.rs) 覆盖源码 class、NativeTyped 实际原生载荷继承和多层字段构造、继承字段构造、动态方法覆盖、绑定接收者保活/回收、绑定后参数数量和重复成员/缺少 self 的预执行诊断。只编译,不在本地运行。
[pipeline.rs](pipeline.rs) 将真实 Endor 示例交给 reader/parser/名称分析/代码生成/VM,检查函数值、前向引用、局部遮蔽、精确数字常量、语义诊断、宿主全局与属性回调、对象结果保活、跨 VM 模板装载、反复调用、错误帧清理、递归限额、共享交互命名空间及超过 255 的操作数。
+108
View File
@@ -1,7 +1,115 @@
use compiler::{CompileOptions, compile_source};
use endor_core::{
ContextNativeFunction, EndorTypeObject, NativeCallable, NativeContext, NativeResult,
ObjectPayload, TypeConstructor, VmError,
};
use endor_core::{ObjectRef, Value, Vm};
use frontend::{EndorReader, ParseMode};
#[derive(Debug)]
struct NativeWidget(String);
impl ObjectPayload for NativeWidget {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn kind_name(&self) -> &'static str {
"widget"
}
fn allows_attributes(&self) -> bool {
true
}
}
#[derive(Debug)]
struct WidgetFactory;
impl NativeCallable for WidgetFactory {
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
let [
Value::Object(requested),
Value::Text(native_label),
values @ ..,
] = args
else {
return Err(VmError::InvalidArgument("widget constructor arguments"));
};
let fields = cx
.store()
.payload::<EndorTypeObject>(*requested, "Type")?
.fields()
.to_vec();
if values.len() != fields.len() {
return Err(VmError::InvalidArgument("widget field count"));
}
let instance = cx.allocate(NativeWidget(native_label.clone()))?;
cx.attach_type(instance, *requested)?;
for (field, value) in fields.iter().zip(values) {
cx.set_attr(instance, &field.name, value.clone())?;
}
Ok(NativeResult::Return(Value::Object(instance)))
}
}
#[test]
fn source_subclasses_preserve_native_allocation_fields_and_nominal_identity() {
let mut vm = Vm::new().unwrap();
let globals = vm.new_dict().unwrap();
let roots = vm.bootstrap_types().unwrap();
let factory = vm
.allocate(ContextNativeFunction::new("widget", WidgetFactory, vec![]))
.unwrap();
let widget = vm
.new_type(
"Widget",
roots.object,
TypeConstructor::NativeTyped(factory),
)
.unwrap();
for (name, object) in [
("Object", roots.object),
("Type", roots.type_type),
("Widget", widget),
] {
vm.dict_set(globals, Value::Text(name.into()), Value::Object(object))
.unwrap();
vm.release(object).unwrap();
}
vm.release(factory).unwrap();
let source = "class Child: Widget { value: Number }\nclass Grand: Child { label: String }";
assert_eq!(
execute(&mut vm, globals, source, ParseMode::Module),
Value::Nil
);
let value = execute(
&mut vm,
globals,
"Grand(\"native\", 9, \"extra\")",
ParseMode::Expression,
);
let Value::Object(instance) = value else {
panic!("native instance")
};
assert_eq!(
vm.payload::<NativeWidget>(instance, "widget").unwrap().0,
"native"
);
assert!(vm.is_instance(instance, widget).unwrap());
assert_eq!(
vm.get_attr(instance, "value").unwrap(),
Value::Number(9_i64.into())
);
assert_eq!(
vm.get_attr(instance, "label").unwrap(),
Value::Text("extra".into())
);
vm.dict_clear(globals).unwrap();
vm.release(globals).unwrap();
vm.collect_cycles();
assert!(vm.is_instance(instance, widget).unwrap());
vm.release(instance).unwrap();
vm.collect_cycles();
assert_eq!(vm.object_count(), 0);
}
fn execute(vm: &mut Vm, globals: ObjectRef, source: &str, mode: ParseMode) -> Value {
let names = vm
.dict_items(globals)
+1 -1
View File
@@ -10,6 +10,6 @@ Endor VM 包,仅负责 Vm、执行帧、调用协议和 wordcode 求值循环
Vm::new 创建独立 ObjectStore 和空执行状态;代码对象、全局字典和函数仍按宿主需要显式创建,再 execute/call。EndorObject、Number、ObjectRef、内置载荷、代码对象/指令定义与 VmError 在 objects 中实现;endor_core 保留这些类型的兼容重导出,既有 Vm 对象方法委托到 ObjectStore。
宿主可显式引导托管 Object/Type 和名义继承,见[类型机制](../../docs/runtime/types.md);普通源码 class 和继承方法分派已经接入,trait、原生类分配继承与完整元类尚未接入。网络、数据库和进程环境属于宿主,源码阅读器与 lexer/parser 属于 [frontend](../frontend/README.md),AST 名称分析与字节码生成属于独立 [compiler](../compiler/README.md),见 [执行与版本模型](../../docs/architecture/execution-and-versions.md)。
宿主可显式引导托管 Object/Type 和名义继承,见[类型机制](../../docs/runtime/types.md);普通源码 class 和继承方法分派已经接入,显式 NativeTyped 原生分配继承已接入,trait 与完整元类尚未接入。网络、数据库和进程环境属于宿主,源码阅读器与 lexer/parser 属于 [frontend](../frontend/README.md),AST 名称分析与字节码生成属于独立 [compiler](../compiler/README.md),见 [执行与版本模型](../../docs/architecture/execution-and-versions.md)。
返回值所有权见 [生命周期](../../docs/runtime/lifecycle.md),执行契约见 [字节码 VM](../../docs/runtime/bytecode.md)。本地不构建或测试,见 [验证](../../docs/operations/validation.md)。返回 [工作区](../README.md)。
+1 -1
View File
@@ -1,6 +1,6 @@
# objects
Endor 核心对象与对象存储包,零外部依赖,不依赖 core。包含 EndorObject、ObjectPayload、Value、ObjectRef、Number、字符串/字典/Cell/函数/异常载荷,以及代码对象与 wordcode 数据定义、编解码和静态验证。提供显式引导的 Object/Type 名义类型,已接入普通源码 class 和继承方法分派,trait、原生分配继承与完整元类行为仍待实现。
Endor 核心对象与对象存储包,零外部依赖,不依赖 core。包含 EndorObject、ObjectPayload、Value、ObjectRef、Number、字符串/字典/Cell/函数/异常载荷,以及代码对象与 wordcode 数据定义、编解码和静态验证。提供显式引导的 Object/Type 名义类型,已接入普通源码 class 和继承方法分派,已支持显式 NativeTyped 分配继承和可转移字段存储;trait 与完整元类行为仍待实现。
| 内容 | 职责 |
| --- | --- |
+6
View File
@@ -51,6 +51,12 @@ impl CompareOp {
/// Rust Drop releases host resources when the payload is destroyed.
/// Equal payloads must provide compatible hashes, or reject hashing explicitly.
pub trait ObjectPayload: Any + fmt::Debug + Send + Sync {
/// Ordinary field storage may be indirect, independently of type identity.
/// Targets must be traced; retargeting records Contents on the forwarder.
fn attribute_target(&self, _store: &crate::ObjectStore) -> Result<Option<ObjectRef>, VmError> {
Ok(None)
}
/// Shared-reference facades may expose the current referent's type. The
/// referent must also be held by traced managed edges. Ordinary objects
/// keep their immutable header type; this does not change inheritance.
+28 -2
View File
@@ -110,6 +110,24 @@ impl ObjectStore {
self.heap.get(reference)
}
/// Borrowed storage path: depend on Contents for intermediate objects and
/// Attribute(name) on the final storage object. Every handle is validated.
pub fn attribute_path(&self, reference: ObjectRef) -> Result<Vec<ObjectRef>, VmError> {
let mut path = Vec::new();
let mut seen = std::collections::BTreeSet::new();
let mut current = reference;
loop {
if !seen.insert(current) {
return Err(VmError::InvalidArgument("cyclic attribute storage"));
}
path.push(current);
match self.object(current)?.payload().attribute_target(self)? {
Some(target) => current = target,
None => return Ok(path),
}
}
}
pub fn retain(&mut self, reference: ObjectRef) -> Result<(), VmError> {
self.heap.retain(reference)
}
@@ -187,14 +205,22 @@ impl ObjectStore {
if name == "type" && self.type_of(reference)?.is_some() {
return Err(VmError::UnsupportedOperation("write object type"));
}
self.heap.set_attr(reference, name, value)
let storage = *self
.attribute_path(reference)?
.last()
.expect("nonempty attribute path");
self.heap.set_attr(storage, name, value)
}
pub fn del_attr(&mut self, reference: ObjectRef, name: &str) -> Result<(), VmError> {
if name == "type" && self.type_of(reference)?.is_some() {
return Err(VmError::UnsupportedOperation("delete object type"));
}
self.heap.del_attr(reference, name)
let storage = *self
.attribute_path(reference)?
.last()
.expect("nonempty attribute path");
self.heap.del_attr(storage, name)
}
/// Validate a borrowed object handle without taking an owned reference.
+35 -10
View File
@@ -12,6 +12,9 @@ pub enum TypeConstructor {
Record,
/// Delegate to a managed callable. It must return an instance of this type.
Native(ObjectRef),
/// Opt-in allocator inheritance. The requested nominal type is prepended
/// to the constructor arguments; the allocator must create that real type.
NativeTyped(ObjectRef),
Abstract,
}
@@ -41,6 +44,9 @@ impl EndorTypeObject {
pub fn base(&self) -> Option<ObjectRef> {
self.base
}
pub fn fields(&self) -> &[ClassField] {
&self.fields
}
}
impl ObjectPayload for EndorTypeObject {
@@ -54,7 +60,9 @@ impl ObjectPayload for EndorTypeObject {
if let Some(base) = self.base {
visit(base);
}
if let TypeConstructor::Native(function) = self.constructor {
if let TypeConstructor::Native(function) | TypeConstructor::NativeTyped(function) =
self.constructor
{
visit(function);
}
for method in self.methods.values() {
@@ -84,6 +92,15 @@ impl NativeCallable for Construct {
function,
arguments: arguments.to_vec(),
}),
TypeConstructor::NativeTyped(function) => {
let mut values = Vec::with_capacity(arguments.len() + 1);
values.push(Value::Object(type_object));
values.extend_from_slice(arguments);
Ok(NativeResult::Invoke {
function,
arguments: values,
})
}
TypeConstructor::Abstract => {
Err(VmError::UnsupportedOperation("abstract type construction"))
}
@@ -157,7 +174,10 @@ impl ObjectStore {
base: ObjectRef,
constructor: TypeConstructor,
) -> Result<ObjectRef, VmError> {
self.payload::<EndorTypeObject>(base, "Type")?;
let fields = self
.payload::<EndorTypeObject>(base, "Type")?
.fields
.clone();
let metatype = self
.type_of(base)?
.ok_or(VmError::InvalidArgument("base type has no metatype"))?;
@@ -170,7 +190,7 @@ impl ObjectStore {
base: Some(base),
constructor,
methods: BTreeMap::new(),
fields: Vec::new(),
fields,
})?;
if let Err(error) = self.attach_type(definition, metatype) {
self.release(definition)?;
@@ -258,8 +278,8 @@ impl ObjectStore {
}
/// Atomically build a source class, including inherited constructor fields.
/// Native allocation needs its own protocol; never forge a Record payload
/// claiming to be a native visual instance.
/// Only explicitly inheritable native allocators are reused. Abstract
/// subclasses remain abstract; no Record can forge a native payload.
pub fn new_class(
&mut self,
name: impl Into<String>,
@@ -268,11 +288,12 @@ impl ObjectStore {
methods: BTreeMap<String, ObjectRef>,
) -> Result<ObjectRef, VmError> {
let definition = self.payload::<EndorTypeObject>(base, "Type")?;
if !matches!(definition.constructor, TypeConstructor::Record) {
if matches!(definition.constructor, TypeConstructor::Native(_)) {
return Err(VmError::UnsupportedOperation(
"source subclass of native or abstract allocator",
"native allocator does not opt into source subclass construction",
));
}
let constructor = definition.constructor;
let mut fields = definition.fields.clone();
for field in own_fields {
if field.name.is_empty()
@@ -304,7 +325,7 @@ impl ObjectStore {
let class = self.allocate(EndorTypeObject {
name,
base: Some(base),
constructor: TypeConstructor::Record,
constructor,
methods,
fields,
})?;
@@ -406,7 +427,11 @@ impl ObjectStore {
return Ok(AttributeLookup::Value(Value::Object(function)));
}
}
match self.heap.get_attr(receiver, name) {
let storage = *self
.attribute_path(receiver)?
.last()
.expect("nonempty attribute path");
match self.heap.get_attr(storage, name) {
Ok(value) => return Ok(AttributeLookup::Value(value)),
Err(VmError::AttributeNotFound(_) | VmError::UnsupportedOperation("attributes")) => {}
Err(error) => return Err(error),
@@ -418,7 +443,7 @@ impl ObjectStore {
return Err(VmError::AttributeNotFound(name.into()));
}
self.heap
.get_attr(receiver, name)
.get_attr(storage, name)
.map(AttributeLookup::Value)
}
}
+1
View File
@@ -4,6 +4,7 @@
| 文件 | 覆盖 |
| --- | --- |
| [attribute_storage.rs](attribute_storage.rs) | 可转移字段存储、原类型身份、替换释放、属性写删和非法路径拒绝 |
| [store.rs](store.rs) | 稳定地址、跨存储句柄拒绝与更新原子性、Dict/Cell 循环回收、函数校验失败无泄漏、共享代码与常量生命周期 |
非法句柄索引、槽位代数与计数溢出仍在 src/heap.rs 的私有单元测试内,不开放句柄构造器。VM 委托 API 与求值行为继续由 [core/tests](../../core/tests/README.md) 检查。随工作区在指定远端运行,本地不测试;现有测试源码不代表已通过,见 [验证](../../../docs/operations/validation.md)。返回 [objects](../README.md)。
+78
View File
@@ -0,0 +1,78 @@
use objects::{ObjectPayload, ObjectRef, ObjectStore, TypeConstructor, Value, VmError};
#[derive(Debug)]
struct Proxy(ObjectRef);
impl ObjectPayload for Proxy {
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn kind_name(&self) -> &'static str {
"proxy"
}
fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) {
visit(self.0);
}
fn attribute_target(&self, _: &ObjectStore) -> Result<Option<ObjectRef>, VmError> {
Ok(Some(self.0))
}
}
#[test]
fn storage_retargeting_preserves_type_identity_and_releases_old_state() {
let mut store = ObjectStore::new().unwrap();
let roots = store.bootstrap_types().unwrap();
let class = store
.new_type("Proxy", roots.object, TypeConstructor::Record)
.unwrap();
let storage = store.new_object().unwrap();
let old_value = store.new_object().unwrap();
let weak_value = store.downgrade(old_value).unwrap();
store
.set_attr(storage, "value", Value::Object(old_value))
.unwrap();
store.release(old_value).unwrap();
let proxy = store.allocate(Proxy(storage)).unwrap();
store.attach_type(proxy, class).unwrap();
store.release(storage).unwrap();
assert_eq!(store.attribute_path(proxy).unwrap(), vec![proxy, storage]);
assert_eq!(store.get_attr(proxy, "type").unwrap(), Value::Object(class));
assert_eq!(
store.get_attr(proxy, "value").unwrap(),
Value::Object(old_value)
);
let next = store.new_object().unwrap();
store.replace_payload(proxy, Proxy(next)).unwrap();
store.release(next).unwrap();
assert!(store.upgrade(weak_value).unwrap().is_none());
store
.set_attr(proxy, "value", Value::Number(9_i64.into()))
.unwrap();
assert_eq!(
store.get_attr(next, "value").unwrap(),
Value::Number(9_i64.into())
);
store.del_attr(proxy, "value").unwrap();
assert!(store.get_attr(next, "value").is_err());
for object in [proxy, class, roots.object, roots.type_type] {
store.release(object).unwrap();
}
store.collect_cycles();
assert_eq!(store.object_count(), 0);
}
#[test]
fn cyclic_and_foreign_attribute_storage_fail_without_mutating_fields() {
let mut store = ObjectStore::new().unwrap();
let storage = store.new_object().unwrap();
let proxy = store.allocate(Proxy(storage)).unwrap();
store.replace_payload(proxy, Proxy(proxy)).unwrap();
let version = store.change_version(proxy).unwrap();
assert!(store.get_attr(proxy, "field").is_err());
assert!(store.set_attr(proxy, "field", Value::Nil).is_err());
assert_eq!(store.change_version(proxy).unwrap(), version);
let other = ObjectStore::new().unwrap();
assert_eq!(other.attribute_path(proxy), Err(VmError::ForeignHandle));
store.release(storage).unwrap();
store.release(proxy).unwrap();
assert_eq!(store.collect_cycles(), 1);
}