pub struct Stack { /* private fields */ }Expand description
The stack of the virtual machine, where all values are stored.
Implementations§
Source§impl Stack
impl Stack
Sourcepub fn at(&self, addr: Address) -> &Value
pub fn at(&self, addr: Address) -> &Value
Access the value at the given frame offset.
§Examples
use rune::vm_try;
use rune::Module;
use rune::runtime::{Output, Stack, VmError, Address};
fn add_one(memory: &mut Stack, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let value = memory.at(addr).as_integer::<i64>()?;
memory.store(out, value + 1);
Ok(())
}Sourcepub fn at_mut(&mut self, addr: Address) -> Result<&mut Value, StackError>
pub fn at_mut(&mut self, addr: Address) -> Result<&mut Value, StackError>
Get a value mutable at the given index from the stack bottom.
§Examples
use rune::vm_try;
use rune::Module;
use rune::runtime::{Output, Stack, VmError, Address};
fn add_one(memory: &mut Stack, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let mut value = memory.at_mut(addr)?;
let number = value.as_integer::<i64>()?;
*value = rune::to_value(number + 1)?;
memory.store(out, ())?;
Ok(())
}Sourcepub fn slice_at(
&self,
addr: Address,
len: usize,
) -> Result<&[Value], SliceError>
pub fn slice_at( &self, addr: Address, len: usize, ) -> Result<&[Value], SliceError>
Get the slice at the given address with the given length.
§Examples
use rune::vm_try;
use rune::Module;
use rune::runtime::{Output, Stack, ToValue, VmError, Address};
fn sum(memory: &mut Stack, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let mut number = 0;
for value in memory.slice_at(addr, args)? {
number += value.as_integer::<i64>()?;
}
memory.store(out, number)?;
Ok(())
}Sourcepub fn slice_at_mut(
&mut self,
addr: Address,
len: usize,
) -> Result<&mut [Value], SliceError>
pub fn slice_at_mut( &mut self, addr: Address, len: usize, ) -> Result<&mut [Value], SliceError>
Get the mutable slice at the given address with the given length.
§Examples
use rune::vm_try;
use rune::Module;
use rune::runtime::{Output, Memory, VmError, Address};
fn sum(memory: &mut dyn Memory, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
for value in memory.slice_at_mut(addr, args)? {
let number = value.as_integer::<i64>()?;
*value = rune::to_value(number + 1)?;
}
memory.store(out, ())?;
Ok(())
}Sourcepub fn store<O>(
&mut self,
out: Output,
o: O,
) -> Result<(), StoreError<O::Error>>where
O: IntoOutput,
pub fn store<O>(
&mut self,
out: Output,
o: O,
) -> Result<(), StoreError<O::Error>>where
O: IntoOutput,
Write output using the provided IntoOutput implementation onto the
stack.
The IntoOutput trait primarily allows for deferring a computation
since it’s implemented by FnOnce. However, you must take care that
any side effects calling a function may have are executed outside of the
call to store. Like if the function would error.
§Examples
use rune::runtime::{Output, Memory, ToValue, VmError, Address};
use rune::vm_try;
fn sum(memory: &mut dyn Memory, addr: Address, args: usize, out: Output) -> Result<(), VmError> {
let mut number = 0;
for value in memory.slice_at(addr, args)? {
number += value.as_integer::<i64>()?;
}
memory.store(out, number)?;
Ok(())
}Trait Implementations§
Source§impl Memory for Stack
impl Memory for Stack
Source§fn slice_at(&self, addr: Address, len: usize) -> Result<&[Value], SliceError>
fn slice_at(&self, addr: Address, len: usize) -> Result<&[Value], SliceError>
Get the slice at the given address with the given length. Read more
Source§fn slice_at_mut(
&mut self,
addr: Address,
len: usize,
) -> Result<&mut [Value], SliceError>
fn slice_at_mut( &mut self, addr: Address, len: usize, ) -> Result<&mut [Value], SliceError>
Access the given slice mutably. Read more
Source§impl TryFromIteratorIn<Value, Global> for Stack
impl TryFromIteratorIn<Value, Global> for Stack
Source§fn try_from_iter_in<T: IntoIterator<Item = Value>>(
iter: T,
alloc: Global,
) -> Result<Self>
fn try_from_iter_in<T: IntoIterator<Item = Value>>( iter: T, alloc: Global, ) -> Result<Self>
Creates a value from an iterator within an allocator.
Auto Trait Implementations§
impl Freeze for Stack
impl !RefUnwindSafe for Stack
impl !Send for Stack
impl !Sync for Stack
impl Unpin for Stack
impl !UnwindSafe for Stack
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T, U> TryFromIterator<T> for Uwhere
U: TryFromIteratorIn<T, Global>,
impl<T, U> TryFromIterator<T> for Uwhere
U: TryFromIteratorIn<T, Global>,
Source§fn try_from_iter<I>(iter: I) -> Result<U, Error>where
I: IntoIterator<Item = T>,
fn try_from_iter<I>(iter: I) -> Result<U, Error>where
I: IntoIterator<Item = T>,
Creates a value from an iterator within an allocator.