Skip to main content

OnionLambdaRunnable

Struct OnionLambdaRunnable 

Source
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

Source

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

Source§

fn receive( &mut self, step_result: &StepResult, _gc: &mut GC<OnionObjectCell>, ) -> Result<(), RuntimeError>

接收其他可执行对象的执行结果。

当前仅支持接收返回值结果,将其推入操作数栈中。 这主要用于处理子函数调用的返回值或异步操作的结果。

§参数
  • step_result: 要接收的执行步骤结果
  • _gc: 垃圾收集器引用(当前未使用)
§返回
  • Ok(()): 成功接收结果
  • Err(RuntimeError): 接收失败或不支持的结果类型
§错误
  • DetailedError: 当接收非返回值类型的结果时
Source§

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

格式化当前执行上下文为可读字符串。

生成详细的调试信息,包括源码位置、当前指令和执行状态。 优先显示源码级别的调试信息,如果不可用则回退到字节码级别。

§输出格式
  1. 源码位置(如果可用):
    • 行号和列号
    • 带有错误高亮的源码片段
  2. 字节码信息(回退选项):
    • 当前指令指针位置
    • 反汇编的指令内容
  3. 执行状态
    • 调用栈信息
    • 操作数栈状态
    • 变量作用域内容
§返回

格式化的多行字符串,包含完整的调试上下文信息

§示例输出
-> 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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.