68 lines
2.3 KiB
Rust
68 lines
2.3 KiB
Rust
//! Embeddable object runtime and a small wordcode interpreter. No parser or
|
|
//! Python language type system; wordcode is not CPython bytecode compatible.
|
|
//!
|
|
//! ```
|
|
//! use aster_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::<(), aster_core::VmError>(())
|
|
//! ```
|
|
//!
|
|
//! Execute validated wordcode without a source parser:
|
|
//! ```
|
|
//! use aster_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::<(), aster_core::VmError>(())
|
|
//! ```
|
|
|
|
mod builtins;
|
|
mod code;
|
|
mod eval;
|
|
mod frame;
|
|
mod heap;
|
|
mod instruction;
|
|
mod number;
|
|
mod object;
|
|
mod vm;
|
|
mod vm_builtins;
|
|
|
|
pub use builtins::{
|
|
EndorBaseExceptionObject, EndorCellObject, EndorDictObject, EndorFunctionObject,
|
|
EndorStringObject, EndorTracebackEntry,
|
|
};
|
|
pub use code::{CodeLayout, EndorCode, EndorCodeObject};
|
|
pub use frame::{EndorFrame, ExecutionLimits, ExecutionLocation};
|
|
pub use heap::ObjectRef;
|
|
pub use instruction::{DecodedInstruction, Instruction, Opcode};
|
|
pub use number::{
|
|
EndorDecimal, EndorInteger, EndorNumber, EndorNumberObject, EndorRational, EndorReal,
|
|
};
|
|
pub use object::{
|
|
CompareOp, EndorNativeCall, EndorNativeFunctionObject, EndorObject, NativeCall, NativeFunction,
|
|
ObjectHeader, ObjectPayload, Record, Value,
|
|
};
|
|
pub use vm::{Vm, VmError};
|