feat: add managed types, source classes and scoped native calls

This commit is contained in:
2026-10-21 01:47:08 +08:00
parent 7c842789c0
commit a970fd7707
48 changed files with 2069 additions and 134 deletions
+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。
没有 Python type/元类或语言根类。网络、数据库和进程环境属于宿主,源码阅读器与 lexer/parser 属于 [frontend](../frontend/README.md),AST 名称分析与字节码生成属于独立 [compiler](../compiler/README.md),见 [执行与版本模型](../../docs/architecture/execution-and-versions.md)。
宿主可显式引导托管 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)。
返回值所有权见 [生命周期](../../docs/runtime/lifecycle.md),执行契约见 [字节码 VM](../../docs/runtime/bytecode.md)。本地不构建或测试,见 [验证](../../docs/operations/validation.md)。返回 [工作区](../README.md)。
+141 -47
View File
@@ -4,6 +4,12 @@ use crate::{
EndorStringObject, ExecutionLimits, ExecutionLocation, Instruction, ObjectRef, Opcode, Value,
Vm, VmError,
};
use crate::{NativeContext, NativeResult};
enum PreparedCall {
Value(Value),
Frame(EndorFrame),
}
fn invalid(offset: usize, reason: &'static str) -> VmError {
VmError::InvalidBytecode { offset, reason }
@@ -49,20 +55,17 @@ impl Vm {
limits: ExecutionLimits,
) -> Result<Value, VmError> {
self.last_traceback.clear();
if !self
.object(function)?
.payload()
.as_any()
.is::<EndorFunctionObject>()
{
return self.invoke_native(function, arguments);
}
let mut executed = 0;
let frame = match self.prepare_call(function, arguments.to_vec(), &mut executed, limits)? {
PreparedCall::Value(value) => return Ok(value),
PreparedCall::Frame(frame) => frame,
};
if limits.max_frames == 0 {
self.clear_frame(frame);
return Err(VmError::ExecutionLimit("frames"));
}
let frame = self.prepare_frame(function, arguments)?;
let mut frames = vec![frame];
let result = self.eval_frames(&mut frames, limits);
let result = self.eval_frames(&mut frames, limits, &mut executed);
if result.is_err() {
self.last_traceback = frames
.iter()
@@ -196,14 +199,68 @@ impl Vm {
&mut self,
function: ObjectRef,
arguments: &[Value],
) -> Result<Value, VmError> {
) -> Result<NativeResult, VmError> {
self.object(function)?;
for argument in arguments {
self.validate_value(argument)?;
}
if let Some(callable) = self.object(function)?.payload().native_callable() {
return NativeContext::execute(&mut self.objects, function, arguments, callable);
}
let result = self.object(function)?.payload().call(arguments)?;
self.retain_value(&result)?;
Ok(result)
Ok(NativeResult::Return(result))
}
fn prepare_call(
&mut self,
mut function: ObjectRef,
mut arguments: Vec<Value>,
executed: &mut usize,
limits: ExecutionLimits,
) -> Result<PreparedCall, VmError> {
let mut owned = false;
let result = (|| loop {
if self
.object(function)?
.payload()
.as_any()
.is::<EndorFunctionObject>()
{
return self
.prepare_frame(function, &arguments)
.map(PreparedCall::Frame);
}
if *executed >= limits.max_instructions {
return Err(VmError::ExecutionLimit("instructions"));
}
*executed += 1;
match self.invoke_native(function, &arguments)? {
NativeResult::Return(value) => return Ok(PreparedCall::Value(value)),
NativeResult::Invoke {
function: next,
arguments: values,
} => {
if owned {
self.release(function)?;
for value in arguments.drain(..) {
self.release_value(value)?;
}
}
function = next;
arguments = values;
owned = true;
}
}
})();
if owned {
self.release(function)
.expect("delegated call owns its function");
for value in arguments {
self.drop_root(value);
}
}
result
}
fn stack_value(frame: &EndorFrame, index: usize) -> Result<Value, VmError> {
@@ -220,16 +277,15 @@ impl Vm {
Self::stack_value(frame, frame.stack.len() - 1)
}
fn assignment_value(&self, previous: Option<Value>, incoming: Value) -> Result<Value, VmError> {
if let (Some(Value::Object(old)), Value::Object(new)) = (&previous, &incoming) {
if old == new
|| self
.object(*old)?
.payload()
.assign_from(self.object(*new)?.payload())?
{
return Ok(Value::Object(*old));
}
fn assignment_value(
&mut self,
previous: Option<Value>,
incoming: Value,
) -> Result<Value, VmError> {
if let (Some(Value::Object(old)), Value::Object(new)) = (&previous, &incoming)
&& (old == new || self.objects.assign_from(*old, *new)?)
{
return Ok(Value::Object(*old));
}
Ok(incoming)
}
@@ -356,17 +412,53 @@ impl Vm {
self.new_function(name, code, frame.globals, defaults, closure)
}
fn build_class(&mut self, frame: &EndorFrame) -> Result<ObjectRef, VmError> {
let start = frame.stack.len() - 4;
let Value::Text(name) = Self::stack_value(frame, start)? else {
return Err(VmError::ObjectKindMismatch("class name"));
};
let Value::Object(base) = Self::stack_value(frame, start + 1)? else {
return Err(VmError::ObjectKindMismatch("base Type"));
};
let Value::Object(fields) = Self::stack_value(frame, start + 2)? else {
return Err(VmError::ObjectKindMismatch("field map"));
};
let Value::Object(methods) = Self::stack_value(frame, start + 3)? else {
return Err(VmError::ObjectKindMismatch("method map"));
};
let fields = self
.dict_items(fields)?
.into_iter()
.map(|(name, type_name)| match (name, type_name) {
(Value::Text(name), Value::Text(type_name)) => Ok(crate::ClassField {
name: name.clone(),
type_name: type_name.clone(),
}),
_ => Err(VmError::ObjectKindMismatch("class field annotation")),
})
.collect::<Result<Vec<_>, _>>()?;
let methods = self
.dict_items(methods)?
.into_iter()
.map(|(name, function)| match (name, function) {
(Value::Text(name), Value::Object(function)) => Ok((name.clone(), *function)),
_ => Err(VmError::ObjectKindMismatch("class method")),
})
.collect::<Result<std::collections::BTreeMap<_, _>, _>>()?;
self.objects.new_class(name, base, fields, methods)
}
fn eval_frames(
&mut self,
frames: &mut Vec<EndorFrame>,
limits: ExecutionLimits,
executed: &mut usize,
) -> Result<Value, VmError> {
let mut executed = 0;
loop {
if executed >= limits.max_instructions {
if *executed >= limits.max_instructions {
return Err(VmError::ExecutionLimit("instructions"));
}
executed += 1;
*executed += 1;
let frame = frames.last_mut().expect("an evaluation frame is active");
let decoded = frame.executable.instruction_at(frame.instruction_offset)?;
frame.current_offset = decoded.offset;
@@ -485,27 +577,24 @@ impl Vm {
for index in start + 2..frame.stack.len() {
arguments.push(Self::stack_value(frame, index)?);
}
let language_function = self
.object(function)?
.payload()
.as_any()
.is::<EndorFunctionObject>();
if language_function {
if frames.len() >= limits.max_frames {
return Err(VmError::ExecutionLimit("frames"));
match self.prepare_call(function, arguments, executed, limits)? {
PreparedCall::Frame(mut callee) => {
if frames.len() >= limits.max_frames {
self.clear_frame(callee);
return Err(VmError::ExecutionLimit("frames"));
}
callee.previous = Some(frames.len() - 1);
self.discard(frames.last_mut().unwrap(), argument + 2);
frames.push(callee);
}
PreparedCall::Value(value) => {
self.discard(frames.last_mut().unwrap(), argument + 2);
frames
.last_mut()
.unwrap()
.stack
.push(StackEntry::Value(value));
}
let mut callee = self.prepare_frame(function, &arguments)?;
callee.previous = Some(frames.len() - 1);
self.discard(frames.last_mut().unwrap(), argument + 2);
frames.push(callee);
} else {
let value = self.invoke_native(function, &arguments)?;
self.discard(frames.last_mut().unwrap(), argument + 2);
frames
.last_mut()
.unwrap()
.stack
.push(StackEntry::Value(value));
}
}
Opcode::ReturnValue => {
@@ -554,11 +643,16 @@ impl Vm {
let Value::Object(owner) = Self::top_value(frame)? else {
return Err(VmError::ObjectKindMismatch("object"));
};
let value = self.get_attr(owner, &frame.executable.layout().names[argument])?;
self.retain_value(&value)?;
let value =
self.bind_attr(owner, &frame.executable.layout().names[argument])?;
self.discard(frame, 1);
frame.stack.push(StackEntry::Value(value));
}
Opcode::BuildClass => {
let class = self.build_class(frame)?;
self.discard(frame, 4);
frame.stack.push(StackEntry::Value(Value::Object(class)));
}
Opcode::StoreAttr => {
let Value::Object(owner) = Self::top_value(frame)? else {
return Err(VmError::ObjectKindMismatch("object"));
+9 -5
View File
@@ -44,11 +44,15 @@ mod vm;
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation};
// Preserve existing embedding imports; objects owns these definitions.
pub use objects::ObjectStore;
pub use objects::{
CodeLayout, CompareOp, DecodedInstruction, EndorBaseExceptionObject, EndorCellObject,
EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject, EndorFunctionObject, EndorInteger,
EndorNativeCall, EndorNativeFunctionObject, EndorNumber, EndorNumberObject, EndorObject,
EndorRational, EndorReal, EndorStringObject, EndorTracebackEntry, Instruction, NativeCall,
NativeFunction, ObjectHeader, ObjectPayload, ObjectRef, Opcode, Record, Value, VmError,
ChangeBatch, ChangeKey, CodeLayout, CompareOp, ContextNativeFunction, DecodedInstruction,
EndorBaseExceptionObject, EndorCellObject, EndorCode, EndorCodeObject, EndorDecimal,
EndorDictObject, EndorFunctionObject, EndorInteger, EndorNativeCall, EndorNativeFunctionObject,
EndorNumber, EndorNumberObject, EndorObject, EndorRational, EndorReal, EndorStringObject,
EndorTracebackEntry, Instruction, NativeCall, NativeCallable, NativeContext, NativeFunction,
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, Record, Value,
VmError, WeakObjectRef,
};
pub use objects::{ClassField, EndorTypeObject, RootTypes, TypeConstructor};
pub use vm::Vm;
+81 -1
View File
@@ -9,11 +9,91 @@ use crate::{
/// reference accounting and builtin operations live in the objects crate.
#[derive(Debug)]
pub struct Vm {
objects: ObjectStore,
pub(crate) objects: ObjectStore,
pub(crate) last_traceback: Vec<ExecutionLocation>,
}
impl Vm {
pub fn define_method(
&mut self,
type_object: ObjectRef,
name: impl Into<String>,
function: ObjectRef,
) -> Result<(), VmError> {
self.objects.define_method(type_object, name, function)
}
/// Owned result; release_value when finished. Unlike get_attr, methods bind
/// their receiver and retain it until the bound method itself is released.
pub fn bind_attr(&mut self, object: ObjectRef, name: &str) -> Result<Value, VmError> {
self.objects.bind_attr(object, name)
}
pub fn bootstrap_types(&mut self) -> Result<crate::RootTypes, VmError> {
self.objects.bootstrap_types()
}
pub fn new_type(
&mut self,
name: impl Into<String>,
base: ObjectRef,
constructor: crate::TypeConstructor,
) -> Result<ObjectRef, VmError> {
self.objects.new_type(name, base, constructor)
}
pub fn attach_type(
&mut self,
object: ObjectRef,
type_object: ObjectRef,
) -> Result<(), VmError> {
self.objects.attach_type(object, type_object)
}
pub fn type_of(&self, object: ObjectRef) -> Result<Option<ObjectRef>, VmError> {
self.objects.type_of(object)
}
pub fn is_subtype(&self, candidate: ObjectRef, expected: ObjectRef) -> Result<bool, VmError> {
self.objects.is_subtype(candidate, expected)
}
pub fn is_instance(&self, object: ObjectRef, expected: ObjectRef) -> Result<bool, VmError> {
self.objects.is_instance(object, expected)
}
/// Read-only access for embedding protocols sharing the same object graph.
pub fn object_store(&self) -> &ObjectStore {
&self.objects
}
pub fn downgrade(&self, reference: ObjectRef) -> Result<crate::WeakObjectRef, VmError> {
self.objects.downgrade(reference)
}
pub fn upgrade(
&mut self,
reference: crate::WeakObjectRef,
) -> Result<Option<ObjectRef>, VmError> {
self.objects.upgrade(reference)
}
pub fn take_changes(&mut self) -> crate::ChangeBatch {
self.objects.take_changes()
}
pub fn change_version(&self, reference: ObjectRef) -> Result<u64, VmError> {
self.objects.change_version(reference)
}
pub fn replace_payload(
&mut self,
reference: ObjectRef,
payload: impl ObjectPayload,
) -> Result<(), VmError> {
self.objects.replace_payload(reference, payload)
}
pub fn new() -> Result<Self, VmError> {
Ok(Self {
objects: ObjectStore::new()?,
+2
View File
@@ -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)。
+150
View File
@@ -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();
}
+65
View File
@@ -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);
}