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
impl OnionObject
Sourcepub fn upgrade(&self, collected: &mut Vec<GCArc<OnionObjectCell>>)
pub fn upgrade(&self, collected: &mut Vec<GCArc<OnionObjectCell>>)
Sourcepub fn to_cell(self) -> OnionObjectCell
pub fn to_cell(self) -> OnionObjectCell
将对象转换为对象单元格。
创建一个包含当前对象的 OnionObjectCell
,用于 GC 管理。
Sourcepub fn stabilize(&self) -> OnionStaticObject
pub fn stabilize(&self) -> OnionStaticObject
稳定化对象(引用方式)。
创建对象的 OnionStaticObject
包装,通过克隆实现。
Sourcepub fn consume_and_stabilize(self) -> OnionStaticObject
pub fn consume_and_stabilize(self) -> OnionStaticObject
稳定化对象(消费方式)。
通过消费当前对象创建 OnionStaticObject
,避免克隆。
Sourcepub fn with_data<T, F>(&self, f: F) -> Result<T, RuntimeError>
pub fn with_data<T, F>(&self, f: F) -> Result<T, RuntimeError>
Sourcepub fn with_data_mut<T, F>(&mut self, f: F) -> Result<T, RuntimeError>
pub fn with_data_mut<T, F>(&mut self, f: F) -> Result<T, RuntimeError>
Sourcepub fn assign(&self, other: &OnionObject) -> Result<(), RuntimeError>
pub fn assign(&self, other: &OnionObject) -> Result<(), RuntimeError>
Source§impl OnionObject
OnionObject 的核心方法实现。
impl OnionObject
OnionObject 的核心方法实现。
包含对象的相等性比较、类型转换、运算操作等核心功能。 所有方法都遵循 Onion VM 的不可变性和幂等性原则。
Sourcepub fn equals(&self, other: &Self) -> Result<bool, RuntimeError>
pub fn equals(&self, other: &Self) -> Result<bool, RuntimeError>
Sourcepub fn is_same(&self, other: &Self) -> Result<bool, RuntimeError>
pub fn is_same(&self, other: &Self) -> Result<bool, RuntimeError>
检查两个对象是否是同一个引用。
如果是 Mut
类型,则比较其指向的强引用对象地址。
对于其他类型,直接返回 false
。
Sourcepub fn len(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn len(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn contains(&self, other: &OnionObject) -> Result<bool, RuntimeError>
Sourcepub fn to_integer(&self) -> Result<i64, RuntimeError>
pub fn to_integer(&self) -> Result<i64, RuntimeError>
pub fn to_float(&self) -> Result<f64, RuntimeError>
pub fn to_string( &self, ptrs: &Vec<*const OnionObject>, ) -> Result<String, RuntimeError>
pub fn repr( &self, ptrs: &Vec<*const OnionObject>, ) -> Result<String, RuntimeError>
pub fn to_bytes(&self) -> Result<Box<[u8]>, RuntimeError>
pub fn to_boolean(&self) -> Result<bool, RuntimeError>
pub fn binary_add( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_sub( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_mul( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_div( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_mod( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_pow( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_and( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_or(&self, other: &Self) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_xor( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_shl( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_shr( &self, other: &Self, ) -> Result<OnionStaticObject, RuntimeError>
pub fn binary_eq(&self, other: &Self) -> Result<bool, RuntimeError>
pub fn binary_lt(&self, other: &Self) -> Result<bool, RuntimeError>
pub fn binary_gt(&self, other: &Self) -> Result<bool, RuntimeError>
pub fn unary_neg(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn unary_plus(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn unary_not(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn with_attribute<F, R>( &self, key: &OnionObject, f: &F, ) -> Result<R, RuntimeError>
pub fn apply( &self, value: &OnionObject, ) -> Result<OnionStaticObject, RuntimeError>
pub fn key_of(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn value_of(&self) -> Result<OnionStaticObject, RuntimeError>
pub fn type_of(&self) -> Result<String, RuntimeError>
pub fn copy(&self) -> Result<OnionStaticObject, RuntimeError>
Trait Implementations§
Source§impl Clone for OnionObject
impl Clone for OnionObject
Source§fn clone(&self) -> OnionObject
fn clone(&self) -> OnionObject
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for OnionObject
impl Debug for OnionObject
Source§impl From<OnionObject> for OnionObjectCell
impl From<OnionObject> for OnionObjectCell
Source§fn from(obj: OnionObject) -> Self
fn from(obj: OnionObject) -> Self
Converts to this type from the input type.
Source§impl GCTraceable<OnionObjectCell> for OnionObject
impl GCTraceable<OnionObjectCell> for OnionObject
Auto Trait Implementations§
impl Freeze for OnionObject
impl !RefUnwindSafe for OnionObject
impl Send for OnionObject
impl Sync for OnionObject
impl Unpin for OnionObject
impl !UnwindSafe for OnionObject
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