73 lines
3.3 KiB
Rust
73 lines
3.3 KiB
Rust
//! compiler 和宿主导入的 VM 公共入口;对象表示与堆操作从 objects 重导出,避免维护第二套类型。
|
|
//! 两段示例分别展示显式根生命周期和直接执行已验证指令,文档测试使用同一公开 API。
|
|
//! Embeddable wordcode VM backed by the independent objects crate. No parser or
|
|
//! Python language type system; wordcode is not CPython bytecode compatible.
|
|
//!
|
|
//! ```
|
|
//! use endor_core::{Value, Vm};
|
|
//! let mut vm = Vm::new()?;
|
|
//! let object = vm.new_object()?; // One owned external reference.
|
|
//! vm.set_attr(object, "x", Value::Number(42_i64.into()))?;
|
|
//! assert_eq!(vm.get_attr(object, "x")?, Value::Number(42_i64.into()));
|
|
//! vm.retain(object)?; // Own an additional reference.
|
|
//! vm.release(object)?;
|
|
//! assert_eq!(vm.ref_count(object)?, 1);
|
|
//! vm.release(object)?; // The object is destroyed.
|
|
//! assert_eq!(vm.object_count(), 0);
|
|
//! # Ok::<(), endor_core::VmError>(())
|
|
//! ```
|
|
//!
|
|
//! Execute validated wordcode without a source parser:
|
|
//! ```
|
|
//! use endor_core::{CodeLayout, EndorCode, Instruction, Opcode, Value, Vm};
|
|
//! let mut vm = Vm::new()?;
|
|
//! let globals = vm.new_dict()?;
|
|
//! let code = EndorCode::from_instructions(
|
|
//! &[
|
|
//! Instruction::new(Opcode::LoadConst, 0),
|
|
//! Instruction::simple(Opcode::ReturnValue),
|
|
//! ],
|
|
//! vec![Value::Text("Hello Endor".into())],
|
|
//! CodeLayout::default(),
|
|
//! )?;
|
|
//! let code = vm.new_code(code)?;
|
|
//! let result = vm.execute(code, globals)?;
|
|
//! assert_eq!(result, Value::Text("Hello Endor".into()));
|
|
//! vm.release_value(result)?; // Object results own one external reference.
|
|
//! vm.release(code)?;
|
|
//! vm.release(globals)?;
|
|
//! assert_eq!(vm.object_count(), 0);
|
|
//! # Ok::<(), endor_core::VmError>(())
|
|
//! ```
|
|
|
|
// 指令求值、调用准备、暂停/协程与根清理的内部实现。
|
|
mod eval;
|
|
// 执行帧、预算、回溯与进度状态结构;不定义另一套对象堆。
|
|
mod frame;
|
|
// VM 持有 Store 与执行状态,并提供宿主访问门面。
|
|
mod vm;
|
|
|
|
// 公开执行观察和进度类型,内部 Coroutine 载荷状态不导出。
|
|
pub use frame::{
|
|
CoroutineProgress, EndorFrame, ExecutionLimits, ExecutionLocation, ExecutionProgress,
|
|
};
|
|
// Preserve existing embedding imports; objects owns these definitions.
|
|
// 独立存储入口来自 objects,宿主可不创建求值器就使用。
|
|
pub use objects::ObjectStore;
|
|
pub use objects::{
|
|
AnnotationType, AwaitPoll, ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement,
|
|
RootTypes, TypeConstructor, TypedMethodRequirement,
|
|
};
|
|
pub use objects::{
|
|
ArithmeticOp, CallArity, CallEffect, CallSignature, CallType, ChangeBatch, ChangeKey,
|
|
CodeLayout, CompareOp, ContextNativeFunction, DecodedInstruction, EndorBaseExceptionObject,
|
|
EndorCellObject, EndorCode, EndorCodeObject, EndorDecimal, EndorDictObject,
|
|
EndorFunctionObject, EndorInteger, EndorNativeCall, EndorNativeFunctionObject, EndorNumber,
|
|
EndorNumberObject, EndorObject, EndorRational, EndorReal, EndorStringObject,
|
|
EndorTracebackEntry, Instruction, NativeCall, NativeCallable, NativeContext, NativeFunction,
|
|
NativeResult, ObjectChange, ObjectHeader, ObjectPayload, ObjectRef, Opcode, ReadAccess,
|
|
ReadDependency, ReadSet, Record, SourceSignature, TrackedReads, Value, VmError, WeakObjectRef,
|
|
};
|
|
// 嵌入方启动/恢复调用和访问对象的主要入口。
|
|
pub use vm::Vm;
|