pub struct VM { /* private fields */ }
Expand description
Instance of a Neptune VM
Implementations§
Source§impl VM
impl VM
pub fn new<M: ModuleLoader + 'static>(module_loader: M) -> Self
Sourcepub async fn exec<S: Into<String>>(
&self,
module: S,
source: &str,
) -> Result<(), InterpretError>
pub async fn exec<S: Into<String>>( &self, module: S, source: &str, ) -> Result<(), InterpretError>
Executes source with module module
Sourcepub fn exec_sync<S: Into<String>>(
&self,
module: S,
source: &str,
) -> Result<(), InterpretError>
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
Sourcepub fn create_module(&self, name: &str) -> Result<(), Error>
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
Sourcepub 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,
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)
Sourcepub 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,
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> 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