Struct Vm

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

The Spore virtual machine.

§Example

let mut vm = spore_vm::Vm::default();
vm.eval_str("(define foo 42)").unwrap();
let foo = vm.val_by_name("foo").unwrap().try_int().unwrap(); // 42
vm.eval_str("(define (bar x) (+ x foo))").unwrap();
let bar_10 = vm
    .eval_function_by_name("bar", std::iter::once(10.into()))
    .unwrap()
    .try_int()
    .unwrap(); // 52

Implementations§

Source§

impl Vm

Source

pub fn new(settings: Settings) -> Vm

Create a new virtual machine.

Source

pub fn with_native_function(self, name: &str, func: NativeFunction) -> Self

Return the VM with the native function registered.

Source

pub fn with_custom_value(self, name: &str, val: impl CustomType) -> Self

Return the VM with a custom value that is accessible globally.

§Example
#[derive(Debug, Default)]
pub struct MyType(i64);
impl spore_vm::val::CustomType for MyType {}
impl std::fmt::Display for MyType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "my number is {}", self.0)
    }
}

let mut vm = spore_vm::Vm::default()
    .with_custom_value("my_value", MyType(10));
let val = vm.val_by_name("my_value").unwrap();
let mut custom_val = val.try_custom_mut::<MyType>(&vm).unwrap();
custom_val.0 = 100;
Source

pub fn with(self, f: impl Fn(Vm) -> Vm) -> Self

Return the VM returned by calling fn.

Source§

impl Vm

Source

pub fn val_by_name(&self, name: &str) -> Option<Val<'_>>

Get the value with the given name or None if it does not exist.

Source

pub fn eval_str(&mut self, source: &str) -> VmResult<ProtectedVal<'_>>

Evaluate a string in the virtual machine.

let mut vm = spore_vm::Vm::default();
let x = vm.eval_str("(+ 20 22)").unwrap().try_int().unwrap();
Source

pub fn eval_function_by_name( &mut self, name: &str, args: impl ExactSizeIterator<Item = Val<'static>>, ) -> VmResult<ProtectedVal<'_>>

Call a function with the given name.

let mut vm = spore_vm::Vm::default();
vm.eval_str("(define (fib n) (if (< n 2) n (+ (fib (+ n -1)) (fib (+ n -2)))))")
    .unwrap();
let ans = vm
    .eval_function_by_name("fib", std::iter::once(10.into()))
    .unwrap()
    .try_int()
    .unwrap();
Source§

impl Vm

Source

pub unsafe fn run_gc(&mut self)

Run the garbage collector.

This does not need to be manually invoked as it is called automatically at the start of evaluation through functions like Self::eval_str and Self::eval_function_by_name.

§Safety
Source§

impl Vm

Source

pub fn get_symbol(&self, s: &str) -> Option<Symbol>

Get the symbol for the given s, or None if it does not exist within the VM.

Source

pub fn get_or_create_symbol(&mut self, s: &str) -> Symbol

Get the given symbol within the VM or create it if it does not exist.

Source

pub fn symbol_to_str(&self, s: Symbol) -> Option<&str>

Get the str representation for a symbol.

Trait Implementations§

Source§

impl Debug for Vm

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Vm

Source§

fn default() -> Vm

Create a new virtual machine.

Source§

impl Drop for Vm

Source§

fn drop(&mut self)

Executes the destructor for this type. 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.