Struct Vm

Source
pub struct Vm { /* private fields */ }
Expand description

A stack which references variables indirectly from a slab.

Implementations§

Source§

impl Vm

Source

pub fn new() -> Self

Construct a new stk virtual machine.

Source

pub fn clear(&mut self)

Reset this virtual machine, freeing all memory used.

Source

pub fn ip(&self) -> usize

Access the current instruction pointer.

Source

pub fn modify_ip(&mut self, offset: isize) -> Result<(), VmError>

Modify the current instruction pointer.

Source

pub fn iter_stack_debug( &self, ) -> impl Iterator<Item = (ValuePtr, Result<ValueRef<'_>, StackError>)> + '_

Iterate over the stack, producing the value associated with each stack item.

Source

pub fn call_function<'a, A, T, I>( &'a mut self, context: &'a Context, unit: &'a CompilationUnit, name: I, args: A, ) -> Result<Task<'a, T>, VmError>
where I: IntoIterator, I::Item: AsRef<str>, A: IntoArgs + 'a, T: FromValue,

Call the given function in the given compilation unit.

Source

pub fn run<'a, T>( &'a mut self, context: &'a Context, unit: &'a CompilationUnit, ) -> Task<'a, T>
where T: FromValue,

Run the given program on the virtual machine.

Source

pub fn push(&mut self, value: ValuePtr)

Push an unmanaged reference.

The reference count of the value being referenced won’t be modified.

Source

pub fn pop(&mut self) -> Result<ValuePtr, StackError>

Pop a reference to a value from the stack.

Source

pub fn external_allocate<T>(&mut self, value: T) -> ValuePtr
where T: Any,

Allocate and insert an external and return its reference.

This will leak memory unless the reference is pushed onto the stack to be managed.

Source

pub unsafe fn external_allocate_ptr<T>(&mut self, value: *const T) -> ValuePtr
where T: Any,

Allocate an external slot for the given reference.

§Safety

If the pointer was constructed from a reference, the reference passed in MUST NOT be used again until the VM has been cleared using clear since the VM is actively aliasing the reference for the duration of its life.

Source

pub unsafe fn external_allocate_mut_ptr<T>(&mut self, value: *mut T) -> ValuePtr
where T: Any,

Allocate an external slot for the given mutable reference.

§Safety

If the pointer was constructed from a reference, the reference passed in MUST NOT be used again until the VM has been cleared using clear since the VM is actively aliasing the reference for the duration of its life.

Source

pub fn string_allocate(&mut self, value: String) -> ValuePtr

Allocate a value and return its ptr.

This operation can leak memory unless the returned slot is pushed onto the stack.

Newly allocated items already have a refcount of 1. And should be pushed on the stack using [push], rather than [push.

Source

pub fn string_ref(&self, slot: Slot) -> Result<Ref<'_, String>, StackError>

Get a reference of the value at the given slot.

Source

pub fn string_clone(&self, slot: Slot) -> Result<String, StackError>

Get a cloned value from the given slot.

Source

pub fn string_mut(&self, slot: Slot) -> Result<Mut<'_, String>, StackError>

Get a reference of the value at the given slot.

Source

pub fn string_take(&mut self, slot: Slot) -> Result<String, StackError>

Take the value at the given slot.

After taking the value, the caller is responsible for deallocating it.

Source

pub fn array_allocate(&mut self, value: Vec<ValuePtr>) -> ValuePtr

Allocate a value and return its ptr.

This operation can leak memory unless the returned slot is pushed onto the stack.

Newly allocated items already have a refcount of 1. And should be pushed on the stack using [push], rather than [push.

Source

pub fn array_ref( &self, slot: Slot, ) -> Result<Ref<'_, Vec<ValuePtr>>, StackError>

Get a reference of the value at the given slot.

Source

pub fn array_clone(&self, slot: Slot) -> Result<Vec<ValuePtr>, StackError>

Get a cloned value from the given slot.

Source

pub fn array_mut( &self, slot: Slot, ) -> Result<Mut<'_, Vec<ValuePtr>>, StackError>

Get a reference of the value at the given slot.

Source

pub fn array_take(&mut self, slot: Slot) -> Result<Vec<ValuePtr>, StackError>

Take the value at the given slot.

After taking the value, the caller is responsible for deallocating it.

Source

pub fn object_allocate(&mut self, value: HashMap<String, ValuePtr>) -> ValuePtr

Allocate a value and return its ptr.

This operation can leak memory unless the returned slot is pushed onto the stack.

Newly allocated items already have a refcount of 1. And should be pushed on the stack using [push], rather than [push.

Source

pub fn object_ref( &self, slot: Slot, ) -> Result<Ref<'_, HashMap<String, ValuePtr>>, StackError>

Get a reference of the value at the given slot.

Source

pub fn object_clone( &self, slot: Slot, ) -> Result<HashMap<String, ValuePtr>, StackError>

Get a cloned value from the given slot.

Source

pub fn object_mut( &self, slot: Slot, ) -> Result<Mut<'_, HashMap<String, ValuePtr>>, StackError>

Get a reference of the value at the given slot.

Source

pub fn object_take( &mut self, slot: Slot, ) -> Result<HashMap<String, ValuePtr>, StackError>

Take the value at the given slot.

After taking the value, the caller is responsible for deallocating it.

Source

pub fn external_ref<T>(&self, slot: Slot) -> Result<Ref<'_, T>, StackError>
where T: Any,

Get a reference of the external value of the given type and the given slot.

Source

pub fn external_mut<T>(&self, slot: Slot) -> Result<Mut<'_, T>, StackError>
where T: Any,

Get a mutable reference of the external value of the given type and the given slot.

Mark the given value as mutably used, preventing it from being used again.

Source

pub fn external_clone<T: Clone + Any>( &self, slot: Slot, ) -> Result<T, StackError>

Get a clone of the given external.

Source

pub fn external_take<T>(&mut self, slot: Slot) -> Result<T, StackError>
where T: Any,

Take an external value from the virtual machine by its slot.

Source

pub fn external_ref_dyn(&self, slot: Slot) -> Result<Ref<'_, Any>, StackError>

Get a reference of the external value of the given type and the given slot.

Source

pub fn external_take_dyn(&mut self, slot: Slot) -> Result<Any, StackError>

Take an external value by dyn, assuming you have exlusive access to it.

Source

pub fn slot_type_name(&self, slot: Slot) -> Result<&'static str, StackError>

Access the type name of the slot.

Source

pub fn slot_type_id(&self, slot: Slot) -> Result<TypeId, StackError>

Access the type id of the slot.

Source

pub fn value_take(&mut self, value: ValuePtr) -> Result<Value, StackError>

Convert a value reference into an owned value.

Source

pub fn value_ref(&self, value: ValuePtr) -> Result<ValueRef<'_>, StackError>

Convert the given ptr into a type-erase ValueRef.

Source

pub fn value_array_ref( &self, values: &[ValuePtr], ) -> Result<Vec<ValueRef<'_>>, StackError>

Convert the given value pointers into an array.

Source

pub fn value_object_ref( &self, object: &HashMap<String, ValuePtr>, ) -> Result<HashMap<String, ValueRef<'_>>, StackError>

Convert the given value pointers into an array.

Source

pub async fn run_for( &mut self, context: &Context, unit: &CompilationUnit, limit: Option<usize>, ) -> Result<(), VmError>

Evaluate a single instruction.

Trait Implementations§

Source§

impl Debug for Vm

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Vm

§

impl !RefUnwindSafe for Vm

§

impl !Send for Vm

§

impl !Sync for Vm

§

impl Unpin for Vm

§

impl UnwindSafe for Vm

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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V