//! eval 创建、推进并清理执行状态的数据结构;这里不执行指令,也不自动释放复制句柄。 //! 活跃帧用外部执行根保留对象;停放到 Coroutine 载荷后由其 trace 边保留同一组身份。 //! Arc 只共享 Rust 帧/代码内存,根与内部边的转换由 eval 显式完成。 use std::sync::Arc; use crate::{EndorCode, ObjectRef, Value}; /// NULL is a call-protocol marker and can never escape as a language value. /// Each Value::Object in a live frame owns one execution root. #[derive(Debug, Clone)] pub(crate) enum StackEntry { /// 调用协议占位,不能作为语言 Value 逃逸。 Null, /// 活跃帧中对象值拥有执行根,Rust Clone 不替调用者增加计数。 Value(Value), } #[derive(Debug)] pub(crate) struct NativeReturn { /// 委托返回约束的诊断名,不代表按名字查回原函数。 pub name: String, /// 需要保持的返回类型约束;句柄 clone 本身不 retain。 pub expected: crate::CallType, } impl NativeReturn { /// eval 在普通/委托返回时对结果执行原生约束,错误保留诊断名和 return 位置。 pub fn check(&self, store: &objects::ObjectStore, value: &Value) -> Result<(), crate::VmError> { self.expected .check(store, &self.name, "return".into(), value) } } /// Internal activation record, analogous to _PyInterpreterFrame. It has no /// EndorObject header; introspection objects can be added independently later. #[derive(Debug)] pub struct EndorFrame { /// 活跃帧拥有的函数根;函数强边使 code/globals/builtins 保持可达。 pub(crate) function: ObjectRef, /// 已解析源码返回注解;含定义句柄时另持一份执行根。 pub(crate) return_type: Option, /// 原生委托链带来的返回检查,每项定义根由 eval 显式保留/释放。 pub(crate) native_returns: Vec, /// Index of the caller in the execution's frame vector, never a pointer. pub(crate) previous: Option, /// 借用函数指向的托管代码身份,不额外作为独立执行根。 pub(crate) code: ObjectRef, /// 共享不可变执行数据;不替代函数/代码的托管引用保活。 pub(crate) executable: Arc, /// 借用函数拥有的全局字典。 pub(crate) globals: ObjectRef, /// 借用函数创建时捕获的内置字典,可为 None。 pub(crate) builtins: Option, /// 本帧诊断名,回溯构造时复制。 pub(crate) name: String, /// 下一条/恢复时将执行的代码单元偏移。 pub(crate) instruction_offset: usize, /// 当前或最近执行的指令起点,错误回溯使用。 pub(crate) current_offset: usize, /// 当前是否因 await Pending 停放;不是普通切片预算耗尽。 pub(crate) waiting: bool, /// 局部和自由变量槽;None 为未绑定,每个对象值在活跃帧中拥有一根。 pub(crate) localsplus: Vec>, /// 求值栈对象项各拥有一根,Null 标记不拥有对象。 pub(crate) stack: Vec, } impl EndorFrame { /// 宿主只读查询调用者帧索引,None 表示此帧无调用者。 pub fn previous(&self) -> Option { self.previous } /// 宿主检查当前函数的借用身份,不新增根。 pub fn function(&self) -> ObjectRef { self.function } /// 宿主查询托管代码借用身份,不复制执行数据。 pub fn code(&self) -> ObjectRef { self.code } /// 宿主查询全局字典借用身份。 pub fn globals(&self) -> ObjectRef { self.globals } /// 宿主查询已捕获内置空间借用身份,未配置为 None。 pub fn builtins(&self) -> Option { self.builtins } /// 宿主查看下一次执行位置,单位为代码单元。 pub fn instruction_offset(&self) -> usize { self.instruction_offset } /// 宿主借用槽快照视图,克隆对象 Value 不自动变成拥有根。 pub fn localsplus(&self) -> &[Option] { &self.localsplus } /// 宿主/测试观察栈项数量,包含协议 Null。 pub fn stack_depth(&self) -> usize { self.stack.len() } } #[derive(Debug, Clone, PartialEq, Eq)] pub struct ExecutionLocation { /// 回溯的函数诊断名称。 pub function: String, /// 回溯的源码文件标识,不访问磁盘。 pub filename: String, /// 代码声明起始行,不是精确逐指令行表。 pub first_line: usize, /// 故障指令的代码单元偏移,不是源码字符位置。 pub instruction_offset: usize, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct ExecutionLimits { /// 嵌套源码帧硬上限,零可阻止源码帧执行。 pub max_frames: usize, /// 累计执行/调用准备预算,跨切片继续累计。 pub max_instructions: usize, } impl Default for ExecutionLimits { /// 未指定预算时默认最多 1000 个源码帧,指令限制为 usize::MAX。 fn default() -> Self { Self { max_frames: 1000, max_instructions: usize::MAX, } } } /// A read-only call either retains its VM-owned continuation or returns an /// owned value plus borrowed read dependencies. Instruction counts accumulate. #[derive(Debug)] pub enum ExecutionProgress { /// 软切片耗尽,状态仍保存于 VM/托管协程中,不能重放此前代码。 Pending { /// 从执行开始累计的计数,不是当前切片的局部数量。 instructions: usize, }, /// 已完成;对象结果移交调用者一份拥有根,须通过 VM 归还。 Complete { /// 完成时移出的拥有结果;直接标量没有托管根。 value: Value, /// 借用读取身份,不保留被观察图。 reads: Vec, /// 精确借用观察,含名称值快照也不建立拥有根。 dependencies: Vec, /// 从执行开始累计的计数,不是当前切片的局部数量。 instructions: usize, }, } #[derive(Debug)] pub(crate) struct SuspendedCall { /// 暂停的只读调用帧,执行根仍由 VM 持有。 pub frames: Vec, /// 跨 Pending 保持活跃的 Heap 只读作用域令牌。 pub scope: usize, /// 创建执行时固定的累计硬限制。 pub limits: ExecutionLimits, /// 已消耗的累计指令/调用准备计数,不随每个 quantum 清零。 pub executed: usize, } /// Independently parked mutable execution. Unlike read-only slicing, it does /// not keep a VM-wide heap scope active between quanta. #[derive(Debug)] pub enum CoroutineProgress { /// await 尚未就绪,等待对象句柄借自仍停放的协程帧。 Waiting { /// 待观察对象的借用身份,协程仍须保持有根才能保证它存活。 awaitable: ObjectRef, /// 从执行开始累计的计数,不是当前切片的局部数量。 instructions: usize, }, /// 软切片耗尽,状态仍保存于 VM/托管协程中,不能重放此前代码。 Pending { /// 从执行开始累计的计数,不是当前切片的局部数量。 instructions: usize, }, /// 已完成;对象结果移交调用者一份拥有根,须通过 VM 归还。 Complete { /// 完成时移出的拥有结果;直接标量没有托管根。 value: Value, /// 从执行开始累计的计数,不是当前切片的局部数量。 instructions: usize, }, } #[derive(Debug)] pub(crate) enum CoroutineState { /// 停放的帧快照,执行根已转为协程载荷强边。 Frames(Arc>), /// 准备阶段原生调用已返回结果,作为载荷边等待 resume 移交。 Ready(Value), /// eval 正推进帧的重入保护标记,执行根暂由 eval 持有。 Running, /// 已完成或取消,不再持有帧/结果图。 Finished, } #[derive(Debug)] pub(crate) struct Coroutine { /// 托管协程的当前状态,eval 用载荷替换提交状态变化。 pub state: CoroutineState, /// 创建执行时固定的累计硬限制。 pub limits: ExecutionLimits, /// 已消耗的累计指令/调用准备计数,不随每个 quantum 清零。 pub executed: usize, } impl EndorFrame { /// Exactly the references held as execution roots while this frame runs. /// code/globals/builtins remain reachable through its owning function. /// eval 停放/恢复协程时枚举根身份,也是 Coroutine 载荷 trace 的内部边来源。 pub(crate) fn trace_roots(&self, visit: &mut dyn FnMut(ObjectRef)) { visit(self.function); // 返回注解中只有名义/能力类别引用定义,这些根也要停放到协程图。 if let Some(reference) = self.return_type.and_then(|kind| kind.reference()) { visit(reference); } // 每个委托约束都独立持有定义根,重复引用不能去重。 for check in &self.native_returns { // 参数联合等元数据可能包含多个定义,枚举完整引用集合。 for reference in check.expected.references() { visit(reference); } } // 局部空槽和 Null 不持有根,其他对象槽逐项输出身份。 for value in self .localsplus .iter() .flatten() .chain(self.stack.iter().filter_map(|entry| match entry { StackEntry::Value(value) => Some(value), StackEntry::Null => None, })) { // 直接数值/文本等不形成托管对象边。 if let Value::Object(reference) = value { visit(*reference); } } } } impl crate::ObjectPayload for Coroutine { /// VM 从托管对象载荷下转型为 Coroutine 状态。 fn as_any(&self) -> &dyn std::any::Any { self } /// 协程协议错误与诊断的类别名称。 fn kind_name(&self) -> &'static str { "coroutine" } /// 协程载荷无旧式原地赋值,普通引用重绑定不用改其状态。 fn reference_assignment_only(&self) -> bool { true } /// Heap 分配/替换协程载荷时记录停放状态拥有边,运行/完成状态不保留帧图。 fn trace(&self, visit: &mut dyn FnMut(ObjectRef)) { // 停放帧与准备结果拥有图,Running/Finished 不重复登记执行器当前持有的根。 match &self.state { CoroutineState::Frames(frames) => { // 帧根枚举与 eval 恢复时的保护清单保持一致。 for frame in frames.iter() { frame.trace_roots(visit); } } CoroutineState::Ready(Value::Object(reference)) => visit(*reference), _ => {} } } }