Enum OnionObject

Source
pub enum OnionObject {
Show 15 variants Integer(i64), Float(f64), String(Arc<str>), Bytes(Arc<[u8]>), Boolean(bool), Range(i64, i64), Null, Undefined(Option<Arc<str>>), InstructionPackage(Arc<VMInstructionPackage>), Tuple(OnionTuple), Pair(Arc<OnionPair>), LazySet(Arc<OnionLazySet>), Lambda((Arc<OnionLambdaDefinition>, Arc<OnionObject>)), Custom(Arc<dyn OnionObjectExt>), Mut(GCArcWeak<OnionObjectCell>),
}
Expand description

Onion VM 核心对象枚举。

定义了 Onion 语言运行时的所有对象类型。遵循严格的不可变性原则: 所有类型都是不可变的,“可变“操作通过创建新对象并更新引用实现。

§设计原则

  • 不可变性: 对象一旦创建就不能修改
  • 引用透明性: 相同输入总是产生相同输出
  • GC 友好: 所有类型都支持垃圾回收跟踪
  • 类型安全: 严格的类型检查与转换

§类型分类

§基础不可变类型

  • Integer(i64): 64位有符号整数
  • Float(f64): 64位浮点数
  • String(Arc<str>): 不可变字符串
  • Bytes(Arc<[u8]>): 不可变字节数组
  • Boolean(bool): 布尔值
  • Range(i64, i64): 整数范围
  • Null: 空值
  • Undefined(Option<Arc<str>>): 未定义值(可选错误信息)

§容器类型

  • Tuple(Arc<OnionTuple>): 有序对象集合
  • Pair(Arc<OnionPair>): 键值对
  • LazySet(Arc<OnionLazySet>): 惰性集合

§函数与扩展类型

  • Lambda((Arc<OnionLambdaDefinition>, Arc<OnionObject>)): Lambda 函数
  • Custom(Arc<dyn OnionObjectExt>): 自定义扩展类型
  • InstructionPackage(Arc<VMInstructionPackage>): 字节码指令包

§可变引用类型

  • Mut(GCArcWeak<OnionObjectCell>): 可变对象的弱引用容器

§重要说明

Mut 类型不应直接使用,应通过 mutablize() 方法创建。 Mut 只是一个指向 OnionObjectCell 的弱引用容器,不违反不可变性原则。

Variants§

§

Integer(i64)

§

Float(f64)

§

String(Arc<str>)

§

Bytes(Arc<[u8]>)

§

Boolean(bool)

§

Range(i64, i64)

§

Null

§

Undefined(Option<Arc<str>>)

§

InstructionPackage(Arc<VMInstructionPackage>)

§

Tuple(OnionTuple)

§

Pair(Arc<OnionPair>)

§

LazySet(Arc<OnionLazySet>)

§

Lambda((Arc<OnionLambdaDefinition>, Arc<OnionObject>))

§

Custom(Arc<dyn OnionObjectExt>)

§

Mut(GCArcWeak<OnionObjectCell>)

Implementations§

Source§

impl OnionObject

Source

pub fn upgrade(&self, collected: &mut Vec<GCArc<OnionObjectCell>>)

升级对象中的弱引用为强引用。

遍历对象结构,将所有弱引用升级为强引用,用于 GC 跟踪。

§参数
  • collected: 用于收集强引用的向量
Source

pub fn to_cell(self) -> OnionObjectCell

将对象转换为对象单元格。

创建一个包含当前对象的 OnionObjectCell,用于 GC 管理。

Source

pub fn stabilize(&self) -> OnionStaticObject

稳定化对象(引用方式)。

创建对象的 OnionStaticObject 包装,通过克隆实现。

Source

pub fn consume_and_stabilize(self) -> OnionStaticObject

稳定化对象(消费方式)。

通过消费当前对象创建 OnionStaticObject,避免克隆。

Source

pub fn with_data<T, F>(&self, f: F) -> Result<T, RuntimeError>

对象的不可变数据访问。

如果是 Mut 对象,则访问其指向的对象;否则直接访问当前对象。

§参数
  • f: 处理对象数据的闭包
Source

pub fn with_data_mut<T, F>(&mut self, f: F) -> Result<T, RuntimeError>
where F: FnOnce(&mut OnionObject) -> Result<T, RuntimeError>,

对象的可变数据访问。

如果是 Mut 对象,则访问其指向的对象;否则直接访问当前对象。

§参数
  • f: 处理对象数据的可变闭包
Source

pub fn assign(&self, other: &OnionObject) -> Result<(), RuntimeError>

对可变对象进行赋值操作。

只有 Mut 类型的对象可以被赋值,这保证了不可变性原则。 赋值实际上是更新 Mut 对象指向的内容。

§参数
  • other: 要赋予的新值
§错误
  • InvalidOperation: 尝试对不可变对象赋值
  • BrokenReference: Mut 对象的引用已失效
§安全性

由于不可变对象无法保证赋值后的稳定性,只允许对 Mut 对象赋值。 Assign a new value to a mutable object.

Source§

impl OnionObject

OnionObject 的核心方法实现。

包含对象的相等性比较、类型转换、运算操作等核心功能。 所有方法都遵循 Onion VM 的不可变性和幂等性原则。

Source

pub fn equals(&self, other: &Self) -> Result<bool, RuntimeError>

对象相等性比较。

实现了类型兼容的相等性检查,支持:

  • 基础类型的直接比较
  • 数值类型的隐式转换比较(Integer ↔ Float)
  • 容器类型的递归比较
  • 自定义类型的扩展比较
§参数
  • other: 要比较的另一个对象
§返回
  • Ok(true): 对象相等
  • Ok(false): 对象不相等
  • Err(RuntimeError): 比较过程中发生错误
Source

pub fn is_same(&self, other: &Self) -> Result<bool, RuntimeError>

检查两个对象是否是同一个引用。 如果是 Mut 类型,则比较其指向的强引用对象地址。 对于其他类型,直接返回 false

Source

pub fn len(&self) -> Result<OnionStaticObject, RuntimeError>

获取对象的长度。

支持多种容器类型的长度计算:

  • Tuple: 元素数量
  • String: 字符串长度
  • Bytes: 字节数组长度
  • Range: 范围大小
§返回

包含长度值的静态整数对象

Source

pub fn contains(&self, other: &OnionObject) -> Result<bool, RuntimeError>

Source

pub fn to_integer(&self) -> Result<i64, RuntimeError>

将对象转换为整数。

支持多种类型的整数转换:

  • Integer: 直接返回
  • Float: 截断为整数
  • String: 尝试解析为整数
  • Boolean: true=1, false=0
  • Custom: 调用自定义转换逻辑
§返回

转换后的整数值

§错误
  • InvalidType: 无法转换的类型或格式错误
Source

pub fn to_float(&self) -> Result<f64, RuntimeError>

Source

pub fn to_string( &self, ptrs: &Vec<*const OnionObject>, ) -> Result<String, RuntimeError>

Source

pub fn repr( &self, ptrs: &Vec<*const OnionObject>, ) -> Result<String, RuntimeError>

Source

pub fn to_bytes(&self) -> Result<Box<[u8]>, RuntimeError>

Source

pub fn to_boolean(&self) -> Result<bool, RuntimeError>

Source

pub fn binary_add( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_sub( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_mul( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_div( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_mod( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_pow( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_and( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_or(&self, other: &Self) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_xor( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_shl( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_shr( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn binary_eq(&self, other: &Self) -> Result<bool, RuntimeError>

Source

pub fn binary_lt(&self, other: &Self) -> Result<bool, RuntimeError>

Source

pub fn binary_gt(&self, other: &Self) -> Result<bool, RuntimeError>

Source

pub fn unary_neg(&self) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn unary_plus(&self) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn unary_not(&self) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn with_attribute<F, R>( &self, key: &OnionObject, f: &F, ) -> Result<R, RuntimeError>
where F: Fn(&OnionObject) -> Result<R, RuntimeError>,

Source

pub fn apply( &self, value: &OnionObject, ) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn key_of(&self) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn value_of(&self) -> Result<OnionStaticObject, RuntimeError>

Source

pub fn type_of(&self) -> Result<String, RuntimeError>

Source

pub fn copy(&self) -> Result<OnionStaticObject, RuntimeError>

Trait Implementations§

Source§

impl Clone for OnionObject

Source§

fn clone(&self) -> OnionObject

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OnionObject

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<OnionObject> for OnionObjectCell

Source§

fn from(obj: OnionObject) -> Self

Converts to this type from the input type.
Source§

impl GCTraceable<OnionObjectCell> for OnionObject

Source§

fn collect(&self, queue: &mut VecDeque<GCArcWeak<OnionObjectCell>>)

collects all reachable objects and adds them to the provided queue.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.