Struct VM

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

Instance of a Neptune VM

Implementations§

Source§

impl VM

Source

pub fn new<M: ModuleLoader + 'static>(module_loader: M) -> Self

Source

pub async fn exec<S: Into<String>>( &self, module: S, source: &str, ) -> Result<(), InterpretError>

Executes source with module module

Source

pub fn exec_sync<S: Into<String>>( &self, module: S, source: &str, ) -> Result<(), InterpretError>

Executes source with module module. It panics if a asynchronous efunc is executed

Source

pub fn create_module(&self, name: &str) -> Result<(), Error>

Creates a module named name. It returns Err(ModuleAlreadyExists) if an existing module is named name

Source

pub fn create_efunc<F, T1, T2>( &self, name: &str, callback: F, ) -> Result<(), Error>
where F: FnMut(&mut EFuncContext<'_>) -> Result<T1, T2> + 'static, T1: ToNeptuneValue, T2: ToNeptuneValue,

Creates an synchronous efunc. Returns Err(EFuncAlreadyExists) if an existing efunc is named name Example:

use neptune_lang::*;
let n = VM::new(NoopModuleLoader);
// Methods of EFuncContext usually return EFuncError on error. To return NeptuneError(the
// Error class of Neptune) or EFuncError we can use the EFuncErrorOr enum.
n.create_efunc("inverse", |cx /*: &mut EFuncContext*/ | -> Result<f64,EFuncErrorOr<NeptuneError>> {
    // pop an int from the stack
    let i = cx.as_int()?;
    if i == 0 {
        // It would be better to create our own Error type and implement ToNeptuneValue for it.
        Err(EFuncErrorOr::Other(NeptuneError("Cannot divide by zero".into())))
    }else{
        Ok(1.0 / (i as f64))
    }
}).unwrap();
//This can now be called by ecall(@inverse, 6)
Source

pub fn create_efunc_async<F, Fut, T1, T2>( &self, name: &str, callback: F, ) -> Result<(), Error>
where F: FnMut(&mut EFuncContext<'_>) -> Fut + 'static, Fut: Future<Output = Result<T1, T2>> + 'static, T1: ToNeptuneValue + 'static, T2: ToNeptuneValue + 'static,

Creates an asynchronous efunc. Returns Err(EFuncAlreadyExists) if an existing efunc is named name

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.