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 Dismantle for Stack
A stack holds the values a machine is working over, so a machine which is
suspended - a generator or a stream - holds a graph of values through it.
impl Dismantle for Stack
A stack holds the values a machine is working over, so a machine which is suspended - a generator or a stream - holds a graph of values through it.