61 lines
2.4 KiB
Rust
61 lines
2.4 KiB
Rust
//! 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;
|
|
mod vm;
|
|
|
|
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation};
|
|
// Preserve existing embedding imports; objects owns these definitions.
|
|
pub use objects::ObjectStore;
|
|
pub use objects::{
|
|
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, Record, Value,
|
|
VmError, WeakObjectRef,
|
|
};
|
|
pub use objects::{
|
|
ClassField, EndorTraitObject, EndorTypeObject, MethodRequirement, RootTypes, TypeConstructor,
|
|
};
|
|
pub use vm::Vm;
|