pub struct OnionLambdaRunnable { /* private fields */ }Expand description
Onion Lambda 可执行对象。
表示一个可执行的 Lambda 函数实例,包含完整的执行上下文、 指令指针和字节码指令包。每个 Lambda 调用都会创建一个新的 可执行对象实例,提供独立的执行环境。
§执行模型
- 基于栈的虚拟机架构
- 支持增量执行和暂停/恢复
- 提供完整的调试信息和错误处理
- 支持垃圾回收和内存管理
§示例
ⓘ
let runnable = OnionLambdaRunnable::new(
&arguments, // 函数参数
&captures, // 闭包捕获的变量
&self_object, // self 引用
&this_lambda, // this 引用
instruction, // 字节码指令包
0, // 起始指令指针
)?;Implementations§
Source§impl OnionLambdaRunnable
impl OnionLambdaRunnable
Sourcepub fn new(
argument: &OnionFastMap<Box<str>, OnionStaticObject>,
capture: &OnionFastMap<Box<str>, OnionObject>,
self_object: &OnionObject,
this_lambda: &OnionStaticObject,
instruction: Arc<VMInstructionPackage>,
ip: isize,
) -> Result<Self, RuntimeError>
pub fn new( argument: &OnionFastMap<Box<str>, OnionStaticObject>, capture: &OnionFastMap<Box<str>, OnionObject>, self_object: &OnionObject, this_lambda: &OnionStaticObject, instruction: Arc<VMInstructionPackage>, ip: isize, ) -> Result<Self, RuntimeError>
创建新的 Lambda 可执行对象。
初始化完整的执行环境,包括变量绑定、作用域设置和内置变量配置。 会验证参数池和捕获池与指令包的字符串池一致性,确保执行安全。
§参数
argument: 函数参数映射,键为参数名索引,值为参数值capture: 闭包捕获的变量映射,键为变量名索引,值为变量值self_object: self 引用对象,用于方法调用this_lambda: this 引用对象,指向当前 Lambda 函数instruction: 字节码指令包,包含可执行代码和元数据ip: 起始指令指针位置
§返回
Ok(OnionLambdaRunnable): 成功创建的可执行对象Err(RuntimeError): 初始化失败,如缺少必要变量或池不匹配
§错误
InvalidOperation: 字符串池不匹配或缺少必要的内置变量
§内置变量
自动设置以下内置变量:
this: 当前 Lambda 函数引用self: 方法调用的对象引用- 函数参数:按名称绑定到对应值
- 捕获变量:闭包捕获的外部作用域变量
Trait Implementations§
Source§impl Runnable for OnionLambdaRunnable
impl Runnable for OnionLambdaRunnable
Source§fn receive(
&mut self,
step_result: &StepResult,
_gc: &mut GC<OnionObjectCell>,
) -> Result<(), RuntimeError>
fn receive( &mut self, step_result: &StepResult, _gc: &mut GC<OnionObjectCell>, ) -> Result<(), RuntimeError>
Source§fn step(&mut self, gc: &mut GC<OnionObjectCell>) -> StepResult
fn step(&mut self, gc: &mut GC<OnionObjectCell>) -> StepResult
执行一个或多个指令步骤。
实现高性能的指令执行循环,支持批量执行以减少函数调用开销。 通过静态指令表实现 O(1) 指令分发,提供优异的执行性能。
§执行策略
- 批量执行最多 1024 条指令以优化性能
- 使用 unsafe 代码避免重复边界检查
- 通过静态函数表实现快速指令分发
- 保存错误时的指令指针用于调试
§参数
gc: 垃圾收集器的可变引用,用于内存管理
§返回
StepResult::Continue: 需要继续执行更多指令StepResult::Return(value): 函数执行完成,返回结果值StepResult::Error(error): 执行过程中发生错误StepResult::Call(runnable): 需要调用其他可执行对象
§安全性
使用 unsafe 代码进行性能优化,但通过边界检查确保内存安全。 指令指针越界会立即返回错误而不是导致未定义行为。
Source§fn format_context(&self) -> String
fn format_context(&self) -> String
格式化当前执行上下文为可读字符串。
生成详细的调试信息,包括源码位置、当前指令和执行状态。 优先显示源码级别的调试信息,如果不可用则回退到字节码级别。
§输出格式
- 源码位置(如果可用):
- 行号和列号
- 带有错误高亮的源码片段
- 字节码信息(回退选项):
- 当前指令指针位置
- 反汇编的指令内容
- 执行状态:
- 调用栈信息
- 操作数栈状态
- 变量作用域内容
§返回
格式化的多行字符串,包含完整的调试上下文信息
§示例输出
-> at 15:8
15 | let x = 10 / 0;
| ^^^^^^
--- Lambda Execution State ---
Frame 0:
Variables: x=10, y="hello"
Stack: [Integer(10), Integer(0)]Auto Trait Implementations§
impl !RefUnwindSafe for OnionLambdaRunnable
impl !UnwindSafe for OnionLambdaRunnable
impl Freeze for OnionLambdaRunnable
impl Send for OnionLambdaRunnable
impl Sync for OnionLambdaRunnable
impl Unpin for OnionLambdaRunnable
impl UnsafeUnpin for OnionLambdaRunnable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more