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(); // 52Implementations§
Source§impl Vm
impl Vm
Sourcepub fn with_native_function(self, name: &str, func: NativeFunction) -> Self
pub fn with_native_function(self, name: &str, func: NativeFunction) -> Self
Return the VM with the native function registered.
Sourcepub fn with_custom_value(self, name: &str, val: impl CustomType) -> Self
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§impl Vm
impl Vm
Sourcepub fn val_by_name(&self, name: &str) -> Option<Val<'_>>
pub fn val_by_name(&self, name: &str) -> Option<Val<'_>>
Get the value with the given name or None if it does not exist.
Sourcepub fn eval_str(&mut self, source: &str) -> VmResult<ProtectedVal<'_>>
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();Sourcepub fn eval_function_by_name(
&mut self,
name: &str,
args: impl ExactSizeIterator<Item = Val<'static>>,
) -> VmResult<ProtectedVal<'_>>
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
impl Vm
Sourcepub unsafe fn run_gc(&mut self)
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
impl Vm
Sourcepub fn get_symbol(&self, s: &str) -> Option<Symbol>
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.
Sourcepub fn get_or_create_symbol(&mut self, s: &str) -> Symbol
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.
Sourcepub fn symbol_to_str(&self, s: Symbol) -> Option<&str>
pub fn symbol_to_str(&self, s: Symbol) -> Option<&str>
Get the str representation for a symbol.
Trait Implementations§
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> 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